3 # Common support functions for testing scripts. If a script-config
4 # files is available it will be sourced so in-tree kernel modules and
5 # utilities will be used. If no script-config can be found then the
6 # installed kernel modules and utilities will be used.
8 basedir
="$(dirname $0)"
10 SCRIPT_CONFIG
=zfs-script-config.sh
11 if [ -f "${basedir}/../${SCRIPT_CONFIG}" ]; then
12 .
"${basedir}/../${SCRIPT_CONFIG}"
14 KERNEL_MODULES
=(zlib_deflate zlib_inflate
)
15 MODULES
=(spl splat zavl znvpair zunicode zcommon zfs
)
30 TESTS_RUN
=${TESTS_RUN:-'*'}
31 TESTS_SKIP
=${TESTS_SKIP:-}
34 exec_prefix
=@exec_prefix@
35 pkgdatadir
=@datarootdir@
/@PACKAGE@
39 udevruledir
=@udevruledir@
40 sysconfdir
=@sysconfdir@
41 localstatedir
=@localstatedir@
43 ETCDIR
=${ETCDIR:-/etc}
44 DEVDIR
=${DEVDIR:-/dev/disk/by-vdev}
45 ZPOOLDIR
=${ZPOOLDIR:-${pkgdatadir}/zpool-config}
46 ZPIOSDIR
=${ZPIOSDIR:-${pkgdatadir}/zpios-test}
47 ZPIOSPROFILEDIR
=${ZPIOSPROFILEDIR:-${pkgdatadir}/zpios-profile}
48 TESTSDIR
=${TESTSDIR:-${pkgdatadir}/zfs-tests}
49 RUNFILEDIR
=${RUNFILEDIR:-${pkgdatadir}/runfiles}
51 ZDB
=${ZDB:-${sbindir}/zdb}
52 ZFS
=${ZFS:-${sbindir}/zfs}
53 ZINJECT
=${ZINJECT:-${sbindir}/zinject}
54 ZHACK
=${ZHACK:-${sbindir}/zhack}
55 ZPOOL
=${ZPOOL:-${sbindir}/zpool}
56 ZTEST
=${ZTEST:-${sbindir}/ztest}
57 ZPIOS
=${ZPIOS:-${sbindir}/zpios}
59 COMMON_SH
=${COMMON_SH:-${pkgdatadir}/common.sh}
60 ZFS_SH
=${ZFS_SH:-${pkgdatadir}/zfs.sh}
61 ZPOOL_CREATE_SH
=${ZPOOL_CREATE_SH:-${pkgdatadir}/zpool-create.sh}
62 ZPIOS_SH
=${ZPIOS_SH:-${pkgdatadir}/zpios.sh}
63 ZPIOS_SURVEY_SH
=${ZPIOS_SURVEY_SH:-${pkgdatadir}/zpios-survey.sh}
64 TEST_RUNNER
=${TEST_RUNNER:-${pkgdatadir}/test-runner/bin/test-runner.py}
65 STF_TOOLS
=${STF_TOOLS:-${pkgdatadir}/test-runner}
66 STF_SUITE
=${STF_SUITE:-${pkgdatadir}/zfs-tests}
68 LDMOD
=${LDMOD:-/sbin/modprobe}
69 LSMOD
=${LSMOD:-/sbin/lsmod}
70 RMMOD
=${RMMOD:-/sbin/rmmod}
71 INFOMOD
=${INFOMOD:-/sbin/modinfo}
72 LOSETUP
=${LOSETUP:-/sbin/losetup}
73 MDADM
=${MDADM:-/sbin/mdadm}
74 DMSETUP
=${DMSETUP:-/sbin/dmsetup}
75 PARTED
=${PARTED:-/sbin/parted}
76 BLOCKDEV
=${BLOCKDEV:-/sbin/blockdev}
77 LSSCSI
=${LSSCSI:-/usr/bin/lsscsi}
78 SCSIRESCAN
=${SCSIRESCAN:-/usr/bin/scsi-rescan}
79 SYSCTL
=${SYSCTL:-/sbin/sysctl}
80 UDEVADM
=${UDEVADM:-/sbin/udevadm}
81 AWK
=${AWK:-/usr/bin/awk}
82 GDB
=${GDB:-/usr/bin/gdb}
84 ZED_PIDFILE
=${ZED_PIDFILE:-${localstatedir}/run/zed.pid}
86 COLOR_BLACK
="\033[0;30m"
87 COLOR_DK_GRAY
="\033[1;30m"
88 COLOR_BLUE
="\033[0;34m"
89 COLOR_LT_BLUE
="\033[1;34m"
90 COLOR_GREEN
="\033[0;32m"
91 COLOR_LT_GREEN
="\033[1;32m"
92 COLOR_CYAN
="\033[0;36m"
93 COLOR_LT_CYAN
="\033[1;36m"
94 COLOR_RED
="\033[0;31m"
95 COLOR_LT_RED
="\033[1;31m"
96 COLOR_PURPLE
="\033[0;35m"
97 COLOR_LT_PURPLE
="\033[1;35m"
98 COLOR_BROWN
="\033[0;33m"
99 COLOR_YELLOW
="\033[1;33m"
100 COLOR_LT_GRAY
="\033[0;37m"
101 COLOR_WHITE
="\033[1;37m"
102 COLOR_RESET
="\033[0m"
105 echo -e "${PROG}: $1" >&2
110 if [ ${VERBOSE} ]; then
116 echo -e "${COLOR_GREEN}Pass${COLOR_RESET}"
120 echo -e "${COLOR_RED}Fail${COLOR_RESET} ($1)"
125 echo -e "${COLOR_BROWN}Skip${COLOR_RESET}"
130 local MAX_DIR_SIZE
=$2
131 local MAX_FILE_SIZE
=$3
133 mkdir
-p $ROOT/{a
,b
,c
,d
,e
,f
,g
}/{h
,i
}
137 COUNT
=$
(($RANDOM % $MAX_DIR_SIZE))
139 for i
in `seq $COUNT`; do
140 FILE
=`mktemp -p ${DIR}`
141 SIZE
=$
(($RANDOM % $MAX_FILE_SIZE))
142 dd if=/dev
/urandom of
=$FILE bs
=1k count
=$SIZE &>/dev
/null
150 # Disable the udev rule 90-zfs.rules to prevent the zfs module
151 # stack from being loaded due to the detection of a zfs device.
152 # This is important because the test scripts require full control
153 # over when and how the modules are loaded/unloaded. A trap is
154 # set to ensure the udev rule is correctly replaced on exit.
155 local RULE
=${udevruledir}/90-zfs.rules
156 if test -e ${RULE}; then
157 trap "mv ${RULE}.disabled ${RULE}" INT TERM EXIT
158 mv ${RULE} ${RULE}.disabled
161 # Create a random directory tree of files and sub-directories to
162 # to act as a copy source for the various regression tests.
163 SRC_DIR
=`mktemp -d -p /var/tmp/ zfs.src.XXXXXXXX`
164 trap "rm -Rf $SRC_DIR" INT TERM EXIT
165 populate
$SRC_DIR 10 100
169 ${SYSCTL} -w kernel.spl.debug.dump
=1 &>/dev
/null
170 local NAME
=`dmesg | tail -n 1 | cut -f5 -d' '`
171 ${SPLBUILD}/cmd/spl ${NAME} >${NAME}.log
173 echo "Dumped debug log: ${NAME}.log"
180 local LOADED_MODULES
=()
181 local MISSING_MODULES
=()
183 for MOD
in ${MODULES[*]}; do
184 local NAME
=`basename $MOD .ko`
186 if ${LSMOD} |
egrep -q "^${NAME}"; then
187 LOADED_MODULES
=(${NAME} ${LOADED_MODULES[*]})
190 if [ ${INFOMOD} ${MOD} 2>/dev
/null
]; then
191 MISSING_MODULES
=("\t${MOD}\n" ${MISSING_MODULES[*]})
195 if [ ${#LOADED_MODULES[*]} -gt 0 ]; then
196 ERROR
="Unload these modules with '${PROG} -u':\n"
197 ERROR
="${ERROR}${LOADED_MODULES[*]}"
201 if [ ${#MISSING_MODULES[*]} -gt 0 ]; then
202 ERROR
="The following modules can not be found,"
203 ERROR
="${ERROR} ensure your source trees are built:\n"
204 ERROR
="${ERROR}${MISSING_MODULES[*]}"
212 local NAME
=`basename $1 .ko`
214 if [ ${VERBOSE} ]; then
215 echo "Loading ${NAME} ($@)"
218 ${LDMOD} $
* &>/dev
/null
219 if [ $?
-ne 0 ]; then
220 echo "Failed to load ${NAME} ($@)"
230 for MOD
in ${KERNEL_MODULES[*]}; do
231 load_module
${MOD} >/dev
/null
234 for MOD
in ${MODULES[*]}; do
235 local NAME
=`basename ${MOD} .ko`
239 OPT_NAME
=`echo ${OPT} | cut -f1 -d'='`
241 if [ ${NAME} = "${OPT_NAME}" ]; then
242 VALUE
=`echo ${OPT} | cut -f2- -d'='`
246 load_module
${MOD} ${VALUE} ||
return 1
249 if [ ${VERBOSE} ]; then
250 echo "Successfully loaded ZFS module stack"
257 local NAME
=`basename $1 .ko`
259 if [ ${VERBOSE} ]; then
260 echo "Unloading ${NAME} ($@)"
263 ${RMMOD} ${NAME} || ERROR="Failed to unload ${NAME}" return 1
269 local MODULES_REVERSE=( $(echo ${MODULES[@]} |
270 ${AWK} '{for (i=NF;i>=1;i--) printf $i" "} END{print ""}') )
272 for MOD in ${MODULES_REVERSE[*]}; do
273 local NAME=`basename ${MOD} .ko`
274 local USE_COUNT=`${LSMOD} |
275 egrep "^
${NAME} "| ${AWK} '{print $3}'`
277 if [ "${USE_COUNT}" = 0 ] ; then
279 if [ "${DUMP_LOG}" -a ${NAME} = "spl" ]; then
283 unload_module ${MOD} || return 1
287 if [ ${VERBOSE} ]; then
288 echo "Successfully unloaded ZFS module stack"
295 # Check that the mdadm utilities are installed.
298 test -f ${LOSETUP} || die "${LOSETUP} utility must be installed"
303 # Find and return an unused loop device. A new /dev/loopN node will be
304 # created if required. The kernel loop driver will automatically register
305 # the minor as long as it's
less than
/sys
/module
/loop
/parameters
/max_loop.
307 unused_loop_device
() {
308 local DEVICE
=$
(${LOSETUP} -f)
309 local MAX_LOOP_PATH
="/sys/module/loop/parameters/max_loop"
312 # An existing /dev/loopN device was available.
313 if [ -n "${DEVICE}" ]; then
318 # Create a new /dev/loopN provided we are not at MAX_LOOP.
319 if [ -f "${MAX_LOOP_PATH}" ]; then
320 MAX_LOOP
=`cat /sys/module/loop/parameters/max_loop`
321 if [ ${MAX_LOOP} -eq 0 ]; then
325 for (( i
=0; i
<=${MAX_LOOP}; i
++ )); do
328 if [ -b "${DEVICE}" ]; then
331 mknod
-m660 "${DEVICE}" b
7 $i
332 chown root.disk
"${DEVICE}"
333 chmod 666 "${DEVICE}"
341 die
"Error: Unable to create new loopback device"
345 # This can be slightly dangerous because the loop devices we are
346 # cleaning up may not be ours. However, if the devices are currently
347 # in use we will not be able to remove them, and we only remove
348 # devices which include 'zpool' or 'deleted' in the name. So any
349 # damage we might do should be limited to other zfs related testing.
351 cleanup_loop_devices
() {
352 local TMP_FILE
=`mktemp`
354 ${LOSETUP} -a |
tr -d '()' >${TMP_FILE}
355 ${AWK} -F":" -v losetup
="$LOSETUP" \
356 '/zpool/ || /deleted/ { system("losetup -d "$1) }' ${TMP_FILE}
357 ${AWK} -F" " '/zpool/ || /deleted/ { system("rm -f "$3) }' ${TMP_FILE}
363 # Destroy the passed loopback devices, this is used when you know
364 # the names of the loopback devices.
366 destroy_loop_devices
() {
369 msg
"Destroying ${LODEVICES}"
370 ${LOSETUP} -d ${LODEVICES} || \
371 die
"Error $? destroying ${FILE} -> ${DEVICE} loopback"
378 # Create a device label taking care to briefly wait if udev needs to settle.
384 wait_udev
${DEVICE} 30 ||
return 1
385 ${PARTED} ${DEVICE} --script -- mklabel ${LABEL} ||
return 2
391 # Create a primary partition on a block device.
399 ${PARTED} --align optimal
${DEVICE} --script -- \
400 mkpart
${TYPE} ${START} ${END} ||
return 1
407 # Create a filesystem on the block device
413 # Force 4K blocksize, else mkfs.ext2 tries to use 8K, which
415 /sbin
/mkfs.
${FSTYPE} -b 4096 -F -q ${DEVICE} >/dev
/null ||
return 1
421 # Check that the mdadm utilities are installed.
424 test -f ${MDADM} || die
"${MDADM} utility must be installed"
425 test -f ${PARTED} || die
"${PARTED} utility must be installed"
428 check_md_partitionable
() {
429 local LOFILE
=`mktemp -p /tmp zpool-lo.XXXXXXXX`
430 local LODEVICE
=`unused_loop_device`
431 local MDDEVICE
=`unused_md_device`
437 dd if=/dev
/zero of
=${LOFILE} bs
=1M count
=0 seek
=16 \
438 &>/dev
/null ||
return ${RESULT}
440 msg
"Creating ${LODEVICE} using ${LOFILE}"
441 ${LOSETUP} ${LODEVICE} ${LOFILE}
442 if [ $?
-ne 0 ]; then
447 msg
"Creating ${MDDEVICE} using ${LODEVICE}"
448 ${MDADM} --build ${MDDEVICE} --level=faulty \
449 --raid-devices=1 ${LODEVICE} &>/dev
/null
450 if [ $?
-ne 0 ]; then
451 destroy_loop_devices
${LODEVICE}
455 wait_udev
${MDDEVICE} 30
457 ${BLOCKDEV} --rereadpt ${MDDEVICE} 2>/dev
/null
460 destroy_md_devices
${MDDEVICE}
461 destroy_loop_devices
${LODEVICE}
468 # Find and return an unused md device.
471 for (( i
=0; i
<32; i
++ )); do
474 # Skip active devicesudo in /proc/mdstat.
475 grep -q "${MDDEVICE} " /proc
/mdstat
&& continue
477 # Device doesn't exist, use it.
478 if [ ! -e $
/dev
/{MDDEVICE
} ]; then
479 echo /dev
/${MDDEVICE}
483 # Device exists but may not be in use.
484 if [ -b /dev
/${MDDEVICE} ]; then
485 ${MDADM} --detail /dev
/${MDDEVICE} &>/dev
/null
486 if [ $?
-eq 1 ]; then
487 echo /dev
/${MDDEVICE}
493 die
"Error: Unable to find unused md device"
497 # This can be slightly dangerous because it is possible the md devices
498 # we are cleaning up may not be ours. However, if the devices are
499 # currently in use we will not be able to remove them, and even if
500 # we remove devices which were not out we do not zero the super block
501 # so you should be able to reconstruct them.
503 cleanup_md_devices
() {
504 destroy_md_devices
"`ls /dev/md* 2>/dev/null | grep -v p`"
509 # Destroy the passed md devices, this is used when you know
510 # the names of the md devices.
512 destroy_md_devices
() {
515 msg
"Destroying ${MDDEVICES}"
516 for MDDEVICE
in ${MDDEVICES}; do
517 ${MDADM} --stop ${MDDEVICE} &>/dev
/null
518 ${MDADM} --remove ${MDDEVICE} &>/dev
/null
519 ${MDADM} --detail ${MDDEVICE} &>/dev
/null
526 # Check that the scsi utilities are installed.
529 ${INFOMOD} scsi_debug
&>/dev
/null || die
"scsi_debug module required"
530 test -f ${LSSCSI} || die
"${LSSCSI} utility must be installed"
534 # Rescan the scsi bus for scsi_debug devices. It is preferable to use the
535 # scsi-rescan tool if it is installed, but if it's not we can fall back to
536 # removing and readding the device manually. This rescan will only effect
537 # the first scsi_debug device if scsi-rescan is missing.
540 local AWK_SCRIPT
="/scsi_debug/ { print \$1; exit }"
542 if [ -f ${SCSIRESCAN} ]; then
543 ${SCSIRESCAN} --forcerescan --remove &>/dev
/null
545 local SCSIID
=`${LSSCSI} | ${AWK} "${AWK_SCRIPT}" | tr -d '[]'`
546 local SCSIHOST
=`echo ${SCSIID} | cut -f1 -d':'`
547 echo 1 >"/sys/class/scsi_device/${SCSIID}/device/delete"
549 echo "- - -" >/sys
/class
/scsi_host
/host${SCSIHOST}/scan
555 # Trigger udev and wait for it to settle.
558 if [ -f ${UDEVADM} ]; then
559 ${UDEVADM} trigger
--action=change
--subsystem-match=block
568 # The following udev helper functions assume that the provided
569 # udev rules file will create a /dev/disk/by-vdev/<CHANNEL><RANK>
570 # disk mapping. In this mapping each CHANNEL is represented by
571 # the letters a-z, and the RANK is represented by the numbers
572 # 1-n. A CHANNEL should identify a group of RANKS which are all
573 # attached to a single controller, each RANK represents a disk.
574 # This provides a simply mechanism to locate a specific drive
575 # given a known hardware configuration.
580 # When running in tree manually contruct symlinks in tree to
581 # the proper devices. Symlinks are installed for all entires
582 # in the config file regardless of if that device actually
583 # exists. When installed as a package udev can be relied on for
584 # this and it will only create links for devices which exist.
585 if [ ${INTREE} ]; then
589 ${AWK} '!/^#/ && /./ { system( \
590 "ln -f -s /dev/disk/by-path/"$2" "$1";" \
591 "ln -f -s /dev/disk/by-path/"$2"-part1 "$1"p1;" \
592 "ln -f -s /dev/disk/by-path/"$2"-part9 "$1"p9;" \
596 DST_FILE
=`basename ${SRC_PATH} | cut -f1-2 -d'.'`
597 DST_PATH
=/etc
/zfs
/${DST_FILE}
599 if [ -e ${DST_PATH} ]; then
600 die
"Error: Config ${DST_PATH} already exists"
603 cp ${SRC_PATH} ${DST_PATH}
613 if [ ${INTREE} ]; then
616 ${AWK} '!/^#/ && /./ { system( \
617 "rm -f "$1" "$1"p1 "$1"p9") }' $SRC_PATH
625 local CHANNEL
=`echo "obase=16; $1+96" | bc`
628 printf "\x${CHANNEL}${RANK}"
637 for RANK
in `seq 1 ${RANKS}`; do
638 for CHANNEL
in `seq 1 ${CHANNELS}`; do
639 DISK
=`udev_cr2d ${CHANNEL} ${RANK}`
640 RAID0S
[${IDX}]="${DEVDIR}/${DISK}"
648 udev_raid10_setup() {
654 for RANK in `seq 1 ${RANKS}`; do
655 for CHANNEL1 in `seq 1 2 ${CHANNELS}`; do
656 let CHANNEL2=CHANNEL1+1
657 DISK1=`udev_cr2d ${CHANNEL1} ${RANK}`
658 DISK2=`udev_cr2d ${CHANNEL2} ${RANK}`
659 GROUP="${DEVDIR}/${DISK1} ${DEVDIR}/${DISK2}"
660 RAID10S[${IDX}]="mirror
${GROUP}"
673 for RANK in `seq 1 ${RANKS}`; do
676 for CHANNEL in `seq 1 ${CHANNELS}`; do
677 DISK=`udev_cr2d ${CHANNEL} ${RANK}`
678 RAIDZ[${CHANNEL}]="${DEVDIR}/${DISK}"
681 RAIDZS
[${RANK}]="${RAIDZ[*]}"
687 udev_raidz2_setup
() {
692 for RANK
in `seq 1 ${RANKS}`; do
695 for CHANNEL
in `seq 1 ${CHANNELS}`; do
696 DISK
=`udev_cr2d ${CHANNEL} ${RANK}`
697 RAIDZ2
[${CHANNEL}]="${DEVDIR}/${DISK}"
700 RAIDZ2S[${RANK}]="${RAIDZ2[*]}"
710 printf "%-4d %-34s " ${TEST_NUM} "${TEST_NAME}"
718 printf "%-4d %-34s " ${TEST_NUM} "${TEST_NAME}"
726 for i in ${TESTS_SKIP[@]}; do
727 if [[ $i == ${TEST_NUM} ]] ; then
728 skip_one_test ${TEST_NUM} "${TEST_NAME}"
733 if [ "${TESTS_RUN[0]}" = "*" ]; then
734 run_one_test ${TEST_NUM} "${TEST_NAME}"
736 for i in ${TESTS_RUN[@]}; do
737 if [[ $i == ${TEST_NUM} ]] ; then
738 run_one_test ${TEST_NUM} "${TEST_NAME}"
743 skip_one_test ${TEST_NUM} "${TEST_NAME}"
753 while [ ! -e ${DEVICE} ]; do
754 if [ ${COUNT} -gt ${DELAY} ]; then
766 local STACK_MAX_SIZE=/sys/kernel/debug/tracing/stack_max_size
767 local STACK_TRACER_ENABLED=/proc/sys/kernel/stack_tracer_enabled
769 if [ -e $STACK_MAX_SIZE ]; then
770 echo 1 >$STACK_TRACER_ENABLED
771 echo 0 >$STACK_MAX_SIZE
776 local STACK_MAX_SIZE=/sys/kernel/debug/tracing/stack_max_size
777 local STACK_TRACE=/sys/kernel/debug/tracing/stack_trace
778 local STACK_LIMIT=7000
780 if [ -e $STACK_MAX_SIZE ]; then
781 STACK_SIZE=`cat $STACK_MAX_SIZE`
783 if [ $STACK_SIZE -ge $STACK_LIMIT ]; then
785 echo "Warning
: max stack size
$STACK_SIZE bytes
"
792 if [ -f $ZED_PIDFILE ]; then
793 kill $(cat $ZED_PIDFILE)