xref: /freebsd/tests/sys/cddl/zfs/include/libgnop.kshlib (revision d0b2dbfa0ecf2bbc9709efc5e20baf8e4b44bbbf)
12fae26bdSAlan Somers# vim: filetype=sh
22fae26bdSAlan Somers#
32fae26bdSAlan Somers# Copyright (c) 2017 Spectra Logic Corporation
42fae26bdSAlan Somers# All rights reserved.
52fae26bdSAlan Somers#
62fae26bdSAlan Somers# Redistribution and use in source and binary forms, with or without
72fae26bdSAlan Somers# modification, are permitted provided that the following conditions
82fae26bdSAlan Somers# are met:
92fae26bdSAlan Somers# 1. Redistributions of source code must retain the above copyright
102fae26bdSAlan Somers#    notice, this list of conditions, and the following disclaimer,
112fae26bdSAlan Somers#    without modification.
122fae26bdSAlan Somers# 2. Redistributions in binary form must reproduce at minimum a disclaimer
132fae26bdSAlan Somers#    substantially similar to the "NO WARRANTY" disclaimer below
142fae26bdSAlan Somers#    ("Disclaimer") and any redistribution must be conditioned upon
152fae26bdSAlan Somers#    including a substantially similar Disclaimer requirement for further
162fae26bdSAlan Somers#    binary redistribution.
172fae26bdSAlan Somers#
182fae26bdSAlan Somers# NO WARRANTY
192fae26bdSAlan Somers# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
202fae26bdSAlan Somers# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
212fae26bdSAlan Somers# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
222fae26bdSAlan Somers# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
232fae26bdSAlan Somers# HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
242fae26bdSAlan Somers# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
252fae26bdSAlan Somers# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
262fae26bdSAlan Somers# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
272fae26bdSAlan Somers# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
282fae26bdSAlan Somers# IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
292fae26bdSAlan Somers# POSSIBILITY OF SUCH DAMAGES.
302fae26bdSAlan Somers#
312fae26bdSAlan Somers#
322fae26bdSAlan Somers
332fae26bdSAlan Somers#
342fae26bdSAlan Somers# Routines that use gnop(8) to simulate devices that can disappear at any time
352fae26bdSAlan Somers#
362fae26bdSAlan Somers
372fae26bdSAlan Somers# Create a gnop device on top of a real device.  Don't use the full extent; use
382fae26bdSAlan Somers# a portion in the middle so that any ZFS label present on the real device
392fae26bdSAlan Somers# won't be present on the gnop device and vice versa.
402fae26bdSAlan Somersfunction create_gnop
412fae26bdSAlan Somers{
422fae26bdSAlan Somers	# Name of disk to use, with or without /dev/
432fae26bdSAlan Somers	typeset disk=$1
442fae26bdSAlan Somers	# Optional physical path to use
452fae26bdSAlan Somers	typeset physpath=$2
462fae26bdSAlan Somers	# size of disk in bytes
472fae26bdSAlan Somers	typeset -li disk_size
482fae26bdSAlan Somers	# disk size, rounded down to multiple of 16384
492fae26bdSAlan Somers	typeset -li disk_size_rounded
502fae26bdSAlan Somers	# size of the nop device in bytes
512fae26bdSAlan Somers	typeset -li nop_size
522fae26bdSAlan Somers	# offset of the beginning of the nop device in bytes
532fae26bdSAlan Somers	typeset -li nop_offset
542fae26bdSAlan Somers	typeset args
552fae26bdSAlan Somers
562fae26bdSAlan Somers	disk_size=`diskinfo $disk | cut -f 3`
572fae26bdSAlan Somers	# Round it down so the nop device will be 4k-aligned
582fae26bdSAlan Somers	disk_size_rounded=$(( ${disk_size} / 16384 * 16384 ))
592fae26bdSAlan Somers	nop_size=$(( ${disk_size_rounded} / 4 ))
602fae26bdSAlan Somers	nop_offset=${nop_size}
612fae26bdSAlan Somers	args="-s ${nop_size} -o ${nop_offset}"
622fae26bdSAlan Somers	if [ -n "$physpath" ]; then
632fae26bdSAlan Somers		args="$args -z $physpath"
642fae26bdSAlan Somers	fi
652fae26bdSAlan Somers
662fae26bdSAlan Somers	gnop create ${args} ${disk}
672fae26bdSAlan Somers}
682fae26bdSAlan Somers
692fae26bdSAlan Somers# Create multiple gnop devices
702fae26bdSAlan Somersfunction create_gnops
712fae26bdSAlan Somers{
722fae26bdSAlan Somers	typeset disk
732fae26bdSAlan Somers	for disk in $@; do
742fae26bdSAlan Somers		create_gnop "$disk" || return 1
752fae26bdSAlan Somers	done
762fae26bdSAlan Somers	return 0
772fae26bdSAlan Somers}
782fae26bdSAlan Somers
792fae26bdSAlan Somers# Destroy a gnop device.
802fae26bdSAlan Somersfunction destroy_gnop
812fae26bdSAlan Somers{
822fae26bdSAlan Somers	# Name of the underlying (non-gnop) device
832fae26bdSAlan Somers	typeset disk=$1
842fae26bdSAlan Somers
852fae26bdSAlan Somers	# Use "-f" so we can destroy a gnop with a consumer (like ZFS)
862fae26bdSAlan Somers	gnop destroy -f ${disk}.nop
87*dba2e89eSAlan Somers
88*dba2e89eSAlan Somers	# Wait for it to disappear
89*dba2e89eSAlan Somers	for i in `seq 5`; do
90*dba2e89eSAlan Somers		gnop status ${disk}.nop >/dev/null 2>/dev/null || break
91*dba2e89eSAlan Somers		sleep $i
92*dba2e89eSAlan Somers	done
932fae26bdSAlan Somers}
942fae26bdSAlan Somers
952fae26bdSAlan Somers# Destroy multiple gnop devices.  Attempt to destroy them all, ignoring errors
962fae26bdSAlan Somersfunction destroy_gnops
972fae26bdSAlan Somers{
982fae26bdSAlan Somers	typeset disk
992fae26bdSAlan Somers	for disk in $@; do
1002fae26bdSAlan Somers		destroy_gnop "$disk"
1012fae26bdSAlan Somers	done
1022fae26bdSAlan Somers	return 0
1032fae26bdSAlan Somers}
104