cdown / rpms / util-linux

Forked from rpms/util-linux 2 years ago
Clone

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

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