overlay/{069,071}: fix undefined variables
[xfstests-dev.git] / common / scsi_debug
1 ##/bin/bash
2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright (c) 2012 Red Hat, Inc. All Rights Reserved.
4 #
5 # Functions useful for tests on unique block devices
6
7 _require_scsi_debug()
8 {
9         # make sure we have the module and it's not already used
10         modinfo scsi_debug 2>&1 > /dev/null || _notrun "scsi_debug module not found"
11         lsmod | grep -wq scsi_debug && (rmmod scsi_debug || _notrun "scsi_debug module in use")
12         # make sure it has the features we need
13         # logical/physical sectors plus unmap support all went in together
14         modinfo scsi_debug | grep -wq sector_size || _notrun "scsi_debug too old"
15 }
16
17 # Args: [physical sector size, [logical sector size, [unaligned(0|1), [size in megs]]]]
18 _get_scsi_debug_dev()
19 {
20         # Defaults to phys 512, logical 512, aligned
21         physical=${1-512}
22         logical=${2-512}
23         unaligned=${3-0}
24         size=${4-128}
25         test -n "$4" && shift
26         test -n "$3" && shift
27         test -n "$2" && shift
28         test -n "$1" && shift
29
30         phys_exp=0
31         while [ $logical -lt $physical ]; do
32                 let physical=physical/2
33                 let phys_exp=phys_exp+1
34         done
35         opts="sector_size=$logical physblk_exp=$phys_exp lowest_aligned=$unaligned dev_size_mb=$size $@"
36         echo "scsi_debug options $opts" >> $seqres.full
37         modprobe scsi_debug $opts
38         [ $? -eq 0 ] || _fail "scsi_debug modprobe failed"
39         sleep 1
40         device=`grep -wl scsi_debug /sys/block/sd*/device/model | awk -F / '{print $4}'`
41         echo "/dev/$device"
42 }
43
44 _put_scsi_debug_dev()
45 {
46         lsmod | grep -wq scsi_debug || return
47
48         n=2
49         # use redirection not -q option of modprobe here, because -q of old
50         # modprobe is only quiet when the module is not found, not when the
51         # module is in use.
52         while [ $n -ge 0 ] && ! modprobe -nr scsi_debug >/dev/null 2>&1; do
53                 sleep 1
54                 n=$((n-1))
55         done
56         rmmod scsi_debug || _fail "Could not remove scsi_debug module"
57 }