diff --git a/scripts/common.sh.in b/scripts/common.sh.in index bb602474b0..00418696c8 100644 --- a/scripts/common.sh.in +++ b/scripts/common.sh.in @@ -15,6 +15,7 @@ MODULES=(zlib_deflate spl splat zavl znvpair zunicode zcommon zfs) fi PROG="" +CLEANUP= VERBOSE= VERBOSE_FLAG= FORCE= @@ -207,6 +208,24 @@ unused_loop_device() { die "Error: Unable to find unused loopback device" } +# +# This can be slightly dangerous because the loop devices we are +# cleanup up may not be ours. However, if the devices are currently +# in use we will not be able to remove them, and we only remove +# devices which include 'zpool' in the name. So any damage we might +# do should be limited to other zfs related testing. +# +cleanup_loop_devices() { + local TMP_FILE=`mktemp` + + ${LOSETUP} -a | tr -d '()' >${TMP_FILE} + ${AWK} -F":" -v losetup="$LOSETUP" \ + '/zpool/ { system("losetup -d "$1) }' ${TMP_FILE} + ${AWK} -F" " '/zpool/ { system("rm -f "$3) }' ${TMP_FILE} + + rm -f ${TMP_FILE} +} + # # The following udev helper functions assume that the provided # udev rules file will create a /dev/disk/zpool/ diff --git a/scripts/zconfig.sh b/scripts/zconfig.sh index 0220d9f5fa..c671206186 100755 --- a/scripts/zconfig.sh +++ b/scripts/zconfig.sh @@ -16,7 +16,7 @@ PROG=zconfig.sh usage() { cat << EOF USAGE: -$0 [hv] +$0 [hvc] DESCRIPTION: ZFS/ZPOOL configuration tests @@ -24,11 +24,12 @@ DESCRIPTION: OPTIONS: -h Show this message -v Verbose + -c Cleanup lo+file devices at start EOF } -while getopts 'hv' OPTION; do +while getopts 'hvc?' OPTION; do case $OPTION in h) usage @@ -37,6 +38,9 @@ while getopts 'hv' OPTION; do v) VERBOSE=1 ;; + c) + CLEANUP=1 + ;; ?) usage exit @@ -48,6 +52,12 @@ if [ $(id -u) != 0 ]; then die "Must run as root" fi +# Perform pre-cleanup is requested +if [ ${CLEANUP} ]; then + cleanup_loop_devices + rm -f /tmp/zpool.cache.* +fi + zconfig_partition() { local DEVICE=$1 local START=$2