xref: /freebsd/tests/sys/cddl/zfs/include/libgnop.kshlib (revision f39bffc62c1395bde25d152c7f68fdf7cbaab414)
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# $FreeBSD$
32#
33
34#
35# Routines that use gnop(8) to simulate devices that can disappear at any time
36#
37
38# Create a gnop device on top of a real device.  Don't use the full extent; use
39# a portion in the middle so that any ZFS label present on the real device
40# won't be present on the gnop device and vice versa.
41function create_gnop
42{
43	# Name of disk to use, with or without /dev/
44	typeset disk=$1
45	# Optional physical path to use
46	typeset physpath=$2
47	# size of disk in bytes
48	typeset -li disk_size
49	# disk size, rounded down to multiple of 16384
50	typeset -li disk_size_rounded
51	# size of the nop device in bytes
52	typeset -li nop_size
53	# offset of the beginning of the nop device in bytes
54	typeset -li nop_offset
55	typeset args
56
57	disk_size=`diskinfo $disk | cut -f 3`
58	# Round it down so the nop device will be 4k-aligned
59	disk_size_rounded=$(( ${disk_size} / 16384 * 16384 ))
60	nop_size=$(( ${disk_size_rounded} / 4 ))
61	nop_offset=${nop_size}
62	args="-s ${nop_size} -o ${nop_offset}"
63	if [ -n "$physpath" ]; then
64		args="$args -z $physpath"
65	fi
66
67	gnop create ${args} ${disk}
68}
69
70# Create multiple gnop devices
71function create_gnops
72{
73	typeset disk
74	for disk in $@; do
75		create_gnop "$disk" || return 1
76	done
77	return 0
78}
79
80# Destroy a gnop device.
81function destroy_gnop
82{
83	# Name of the underlying (non-gnop) device
84	typeset disk=$1
85
86	# Use "-f" so we can destroy a gnop with a consumer (like ZFS)
87	gnop destroy -f ${disk}.nop
88}
89
90# Destroy multiple gnop devices.  Attempt to destroy them all, ignoring errors
91function destroy_gnops
92{
93	typeset disk
94	for disk in $@; do
95		destroy_gnop "$disk"
96	done
97	return 0
98}
99