1# vim: filetype=sh
2#
3# Copyright (c) 2017 Spectra Logic Corporation
4# All rights reserved.
5#
6# Redistribution and use in source and binary forms, with or without
7# modification, are permitted provided that the following conditions
8# are met:
9# 1. Redistributions of source code must retain the above copyright
10#    notice, this list of conditions, and the following disclaimer,
11#    without modification.
12# 2. Redistributions in binary form must reproduce at minimum a disclaimer
13#    substantially similar to the "NO WARRANTY" disclaimer below
14#    ("Disclaimer") and any redistribution must be conditioned upon
15#    including a substantially similar Disclaimer requirement for further
16#    binary redistribution.
17#
18# NO WARRANTY
19# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
22# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23# HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
28# IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29# POSSIBILITY OF SUCH DAMAGES.
30#
31#
32
33#
34# Routines that use gnop(8) to simulate devices that can disappear at any time
35#
36
37# Create a gnop device on top of a real device.  Don't use the full extent; use
38# a portion in the middle so that any ZFS label present on the real device
39# won't be present on the gnop device and vice versa.
40function create_gnop
41{
42	# Name of disk to use, with or without /dev/
43	typeset disk=$1
44	# Optional physical path to use
45	typeset physpath=$2
46	# size of disk in bytes
47	typeset -li disk_size
48	# disk size, rounded down to multiple of 16384
49	typeset -li disk_size_rounded
50	# size of the nop device in bytes
51	typeset -li nop_size
52	# offset of the beginning of the nop device in bytes
53	typeset -li nop_offset
54	typeset args
55
56	disk_size=`diskinfo $disk | cut -f 3`
57	# Round it down so the nop device will be 4k-aligned
58	disk_size_rounded=$(( ${disk_size} / 16384 * 16384 ))
59	nop_size=$(( ${disk_size_rounded} / 4 ))
60	nop_offset=${nop_size}
61	args="-s ${nop_size} -o ${nop_offset}"
62	if [ -n "$physpath" ]; then
63		args="$args -z $physpath"
64	fi
65
66	gnop create ${args} ${disk}
67}
68
69# Create multiple gnop devices
70function create_gnops
71{
72	typeset disk
73	for disk in $@; do
74		create_gnop "$disk" || return 1
75	done
76	return 0
77}
78
79# Destroy a gnop device.
80function destroy_gnop
81{
82	# Name of the underlying (non-gnop) device
83	typeset disk=$1
84
85	# Use "-f" so we can destroy a gnop with a consumer (like ZFS)
86	gnop destroy -f ${disk}.nop
87
88	# Wait for it to disappear
89	for i in `seq 5`; do
90		gnop status ${disk}.nop >/dev/null 2>/dev/null || break
91		sleep $i
92	done
93}
94
95# Destroy multiple gnop devices.  Attempt to destroy them all, ignoring errors
96function destroy_gnops
97{
98	typeset disk
99	for disk in $@; do
100		destroy_gnop "$disk"
101	done
102	return 0
103}
104