_have_module scsi_debug
}
-_get_scsi_debug_dev() {
- modprobe -r scsi_debug
- if ! modprobe scsi_debug; then
+_init_scsi_debug() {
+ if ! modprobe -r scsi_debug || ! modprobe scsi_debug "$@"; then
return 1
fi
udevadm settle
- local dev
- for dev in /sys/block/sd*; do
- if [[ "$(cat "${dev}/device/model")" =~ ^scsi_debug[[:space:]]*$ ]]; then
- SCSI_DEBUG_NAME="${dev#/sys/block}"
- SCSI_DEBUG_DEV="/dev/${SCSI_DEBUG_NAME}"
- return 0
+ local host_sysfs host target_sysfs target
+ SCSI_DEBUG_HOSTS=()
+ SCSI_DEBUG_TARGETS=()
+ SCSI_DEBUG_DEVICES=()
+ for host_sysfs in /sys/class/scsi_host/*; do
+ if [[ "$(cat "${host_sysfs}/proc_name")" = scsi_debug ]]; then
+ host="${host_sysfs#/sys/class/scsi_host/host}"
+ SCSI_DEBUG_HOSTS+=("$host")
+ for target_sysfs in /sys/class/scsi_device/"$host":*; do
+ target="${target_sysfs#/sys/class/scsi_device/}"
+ SCSI_DEBUG_TARGETS+=("$target")
+ SCSI_DEBUG_DEVICES+=("$(ls "$target_sysfs/device/block")")
+ done
fi
done
- echo "Could not get scsi_debug device" >&2
- return 1
+ if [[ ${#SCSI_DEBUG_HOSTS[@]} -eq 0 ]]; then
+ echo "Could not find scsi_debug hosts" >&2
+ _exit_scsi_debug
+ return 1
+ fi
+
+ if [[ ${#SCSI_DEBUG_TARGETS[@]} -eq 0 ]]; then
+ echo "Could not find scsi_debug targets" >&2
+ _exit_scsi_debug
+ return 1
+ fi
+
+ return 0
}
-_put_scsi_debug_dev() {
+_exit_scsi_debug() {
+ unset SCSI_DEBUG_HOSTS
+ unset SCSI_DEBUG_TARGETS
+ unset SCSI_DEBUG_DEVICES
modprobe -r scsi_debug
}
}
stress_scsi_debug() {
- modprobe -r scsi_debug
- modprobe scsi_debug "$@"
+ if ! _init_scsi_debug "$@"; then
+ return
+ fi
- targets=()
- for host in /sys/class/scsi_host/*; do
- if [[ "$(cat "${host}/proc_name")" = scsi_debug ]]; then
- host="$(basename "${host}")"
- for target in "/sys/class/scsi_device/${host#host}:"*; do
- targets+=("$(basename "${target}")")
- done
- fi
- done
-
- for target in "${targets[@]}"; do
+ local host target
+ for target in "${SCSI_DEBUG_TARGETS[@]}"; do
(
host="${target%%:*}"
scan="${target#*:}"
touch "$TMPDIR/stop"
wait
rm -f "$TMPDIR/stop"
- modprobe -r scsi_debug
+
+ _exit_scsi_debug
}
test() {
test() {
echo "Running ${TEST_NAME}"
- if ! _get_scsi_debug_dev; then
+ if ! _init_scsi_debug; then
return 1
fi
- blktrace -D "$TMP_DIR" "$SCSI_DEBUG_DEV" >"$FULL" 2>&1 &
+ blktrace -D "$TMP_DIR" "/dev/${SCSI_DEBUG_DEVICES[0]}" >"$FULL" 2>&1 &
sleep 0.5
- echo 1 > "/sys/block/${SCSI_DEBUG_NAME}/device/delete"
- if [[ ! -d /sys/kernel/debug/block/${SCSI_DEBUG_NAME} ]]; then
+ echo 1 > "/sys/block/${SCSI_DEBUG_DEVICES[0]}/device/delete"
+ if [[ ! -d /sys/kernel/debug/block/${SCSI_DEBUG_DEVICES[0]} ]]; then
echo "debugfs directory deleted with blktrace active"
fi
{ kill $!; wait; } >/dev/null 2>/dev/null
- if [[ -d /sys/kernel/debug/block/${SCSI_DEBUG_NAME} ]]; then
+ if [[ -d /sys/kernel/debug/block/${SCSI_DEBUG_DEVICES[0]} ]]; then
echo "debugfs directory leaked"
fi
- _put_scsi_debug_dev
+ _exit_scsi_debug
echo "Test complete"
}