cdown / rpms / util-linux

Forked from rpms/util-linux 2 years ago
Clone

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

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