Blame SOURCES/0138-tests-backport-new-ts_scsi_debug_init.patch

304038
From 84995ef8ff7b76cff1cce438fc448f0afa560e23 Mon Sep 17 00:00:00 2001
304038
From: Karel Zak <kzak@redhat.com>
304038
Date: Tue, 17 Oct 2017 12:16:27 +0200
304038
Subject: [PATCH 138/141] tests: backport new ts_scsi_debug_init
304038
304038
Signed-off-by: Karel Zak <kzak@redhat.com>
304038
---
304038
 tests/functions.sh | 76 +++++++++++++++++++++++++++++++++++++++++++++++-------
304038
 1 file changed, 66 insertions(+), 10 deletions(-)
304038
304038
diff --git a/tests/functions.sh b/tests/functions.sh
304038
index 0d1c9c88a..b930dfe7e 100644
304038
--- a/tests/functions.sh
304038
+++ b/tests/functions.sh
304038
@@ -502,21 +502,77 @@ function ts_fdisk_clean {
304038
 }
304038
 
304038
 function ts_scsi_debug_init {
304038
+	local devname
304038
+	local t
304038
+	TS_DEVICE="none"
304038
 
304038
-	modprobe --dry-run --quiet scsi_debug
304038
-	[ "$?" == 0 ] || ts_skip "missing scsi_debug module"
304038
+	# dry run is not really reliable, real modprobe may still fail
304038
+	modprobe --dry-run --quiet scsi_debug &>/dev/null \
304038
+		|| ts_skip "missing scsi_debug module (dry-run)"
304038
 
304038
-	rmmod scsi_debug &> /dev/null
304038
-	modprobe scsi_debug $*
304038
-	[ "$?" == 0 ] || ts_die "Cannot init device"
304038
+	# skip if still in use or removal of modules not supported at all
304038
+	# We don't want a slow timeout here so we don't use ts_scsi_debug_rmmod!
304038
+	modprobe -r scsi_debug &>/dev/null \
304038
+		|| ts_skip "cannot remove scsi_debug module (rmmod)"
304038
 
304038
-	DEVNAME=$(grep --with-filename scsi_debug /sys/block/*/device/model | awk -F '/' '{print $4}')
304038
-	[ "x${DEVNAME}" == "x" ] && ts_die "Cannot find device"
304038
+	modprobe -b scsi_debug "$@" &>/dev/null \
304038
+		|| ts_skip "cannot load scsi_debug module (modprobe)"
304038
 
304038
-	DEVICE="/dev/${DEVNAME}"
304038
+	# it might be still not loaded, modprobe.conf or whatever
304038
+	lsmod 2>/dev/null | grep -q "^scsi_debug " \
304038
+		|| ts_skip "scsi_debug module not loaded (lsmod)"
304038
 
304038
-	sleep 1
304038
 	udevadm settle
304038
 
304038
-	echo $DEVICE
304038
+	# wait for device if udevadm settle does not work
304038
+	for t in 0 0.02 0.05 0.1 1; do
304038
+		sleep $t
304038
+		devname=$(grep --with-filename scsi_debug /sys/block/*/device/model) && break
304038
+	done
304038
+	[ -n "${devname}" ] || ts_die "timeout waiting for scsi_debug device"
304038
+
304038
+	devname=$(echo $devname | awk -F '/' '{print $4}')
304038
+	TS_DEVICE="/dev/${devname}"
304038
+
304038
+	# TODO validate that device is really up, for now just a warning on stderr
304038
+	test -b $TS_DEVICE || echo "warning: scsi_debug device is still down" >&2
304038
+}
304038
+
304038
+# automatically called once in ts_cleanup_on_exit()
304038
+function ts_scsi_debug_rmmod {
304038
+	local err=1
304038
+	local t
304038
+	local lastmsg
304038
+
304038
+	# Return early most importantly in case we are not root or the module does
304038
+	# not exist at all.
304038
+	[ $UID -eq 0 ] || return 0
304038
+	[ -n "$TS_DEVICE" ] || return 0
304038
+	lsmod 2>/dev/null | grep -q "^scsi_debug " || return 0
304038
+
304038
+	udevadm settle
304038
+
304038
+	# wait for successful rmmod if udevadm settle does not work
304038
+	for t in 0 0.02 0.05 0.1 1; do
304038
+		sleep $t
304038
+		lastmsg="$(modprobe -r scsi_debug 2>&1)" && err=0 && break
304038
+	done
304038
+
304038
+	if [ "$err" = "1" ]; then
304038
+		ts_log "rmmod failed: '$lastmsg'"
304038
+		ts_log "timeout removing scsi_debug module (rmmod)"
304038
+		return 1
304038
+	fi
304038
+	if lsmod | grep -q "^scsi_debug "; then
304038
+		ts_log "BUG! scsi_debug still loaded"
304038
+		return 1
304038
+	fi
304038
+
304038
+	# TODO Do we need to validate that all devices are gone?
304038
+	udevadm settle
304038
+	test -b "$TS_DEVICE" && echo "warning: scsi_debug device is still up" >&2
304038
+
304038
+	# TODO unset TS_DEVICE, check that nobody uses it later, e.g. ts_fdisk_clean
304038
+
304038
+	return 0
304038
 }
304038
-- 
304038
2.13.6
304038