2 # Copyright (C) 2007 Karel Zak <kzak@redhat.com>
4 # This file is part of util-linux.
6 # This file is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
11 # This file is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
23 function ts_canonicalize
{
36 ts_failed
"ul_cd: not enough arguments"
38 DEST
=$
(readlink
-f "$1" 2>/dev
/null
)
39 if [ "x$DEST" = "x" ] ||
[ ! -d "$DEST" ]; then
40 ts_failed
"ul_cd: $1: no such directory"
42 cd "$DEST" 2>/dev
/null || ts_failed
"ul_cd: $1: cannot change directory"
43 if [ "$PWD" != "$DEST" ]; then
44 ts_failed
"ul_cd: $PWD is not $DEST"
48 function ts_separator
{
51 if [ -z "$header" ]; then
52 echo "============================================" >> $TS_OUTPUT
54 echo "=====$header================================" >> $TS_OUTPUT
61 if [ "$TS_PARSABLE" != "yes" ]; then
62 if [ $TS_NSUBTESTS -ne 0 ] && [ -z "$TS_SUBNAME" ]; then
63 desc
=$
(printf "%11s...")
69 if [ -n "$TS_SUBNAME" ]; then
70 desc
=$
(printf "%s: [%02d] %s" "$TS_DESC" "$TS_NSUBTESTS" "$TS_SUBNAME")
74 printf "%13s: %-45s ...%s\n" "$TS_COMPONENT" "$desc" "$1"
77 function ts_check_test_command
{
81 if [ ! -x "$1" ]; then
82 ts_skip
"${1##*/} not found"
86 # just command names (e.g. --use-system-commands)
88 type "$cmd" >/dev
/null
2>&1
90 if [ "$TS_NOSKIP_COMMANDS" = "yes" ]; then
91 ts_failed
"missing in PATH: $cmd"
93 ts_skip
"missing in PATH: $cmd"
99 function ts_check_prog
{
101 type "$cmd" >/dev
/null
2>&1 || ts_skip
"missing in PATH: $cmd"
104 function ts_check_losetup
{
106 ts_check_test_command
"$TS_CMD_LOSETUP"
108 if [ "$TS_SKIP_LOOPDEVS" = "yes" ]; then
109 ts_skip
"loop-device tests disabled"
112 # assuming that losetup -f works ... to be checked somewhere else
113 tmp
=$
($TS_CMD_LOSETUP -f 2>/dev
/null
)
114 if test -b "$tmp"; then
117 ts_skip
"no loop-device support"
120 function ts_report_skip
{
121 ts_report
" SKIPPED ($1)"
131 function ts_skip_nonroot
{
132 if [ $UID -ne 0 ]; then
133 ts_skip
"no root permissions"
137 function ts_failed_subtest
{
140 if [ "$TS_KNOWN_FAIL" = "yes" ]; then
145 if [ x
"$1" == x
"" ]; then
146 ts_report
" $msg ($TS_NS)"
148 ts_report
" $msg ($1)"
155 ts_failed_subtest
"$1"
159 function ts_report_ok
{
160 if [ x
"$1" == x
"" ]; then
173 echo "$1" >> $TS_OUTPUT
174 [ "$TS_VERBOSE" == "yes" ] && echo "$1"
178 echo "$1" >> $TS_ERRLOG
179 [ "$TS_VERBOSE" == "yes" ] && echo "$1"
182 function ts_log_both
{
183 echo "$1" >> $TS_OUTPUT
184 echo "$1" >> $TS_ERRLOG
185 [ "$TS_VERBOSE" == "yes" ] && echo "$1"
188 function ts_has_option
{
192 # user may set options by env for a single test or whole component
193 # e.g. TS_OPT_ipcs_limits2_fake="yes" or TS_OPT_ipcs_fake="yes"
194 local v_test
=${TS_TESTNAME//[-.]/_}
195 local v_comp
=${TS_COMPONENT//[-.]/_}
196 local v_name
=${NAME//[-.]/_}
197 eval local env_opt_test
=\
$TS_OPT_${v_comp}_${v_test}_${v_name}
198 eval local env_opt_comp
=\
$TS_OPT_${v_comp}_
${v_name}
199 if [ "$env_opt_test" = "yes" \
200 -o "$env_opt_comp" = "yes" -a "$env_opt_test" != "no" ]; then
203 elif [ "$env_opt_test" = "no" \
204 -o "$env_opt_comp" = "no" -a "$env_opt_test" != "yes" ]; then
208 # or just check the global command line options
209 if [[ $ALL =~
([$
' \t\n']|^
)--$NAME([$
'= \t\n']|$
) ]]; then
214 # or the _global_ env, e.g TS_OPT_parsable="yes"
215 eval local env_opt
=\
$TS_OPT_${v_name}
216 if [ "$env_opt" = "yes" ]; then echo "yes"; fi
219 function ts_option_argument
{
224 echo "$ALL" |
sed -n "s/.*[ \t\n]--$NAME=\([^ \t\n]*\).*/\1/p" |
tail -n 1
227 function ts_init_core_env
{
229 TS_NS
="$TS_COMPONENT/$TS_TESTNAME"
230 TS_OUTPUT
="$TS_OUTDIR/$TS_TESTNAME"
231 TS_ERRLOG
="$TS_OUTDIR/$TS_TESTNAME.err"
232 TS_VGDUMP
="$TS_OUTDIR/$TS_TESTNAME.vgdump"
233 TS_DIFF
="$TS_DIFFDIR/$TS_TESTNAME"
234 TS_EXPECTED
="$TS_TOPDIR/expected/$TS_NS"
235 TS_EXPECTED_ERR
="$TS_TOPDIR/expected/$TS_NS.err"
236 TS_MOUNTPOINT
="$TS_OUTDIR/${TS_TESTNAME}-mnt"
239 function ts_init_core_subtest_env
{
240 TS_NS
="$TS_COMPONENT/$TS_TESTNAME-$TS_SUBNAME"
241 TS_OUTPUT
="$TS_OUTDIR/$TS_TESTNAME-$TS_SUBNAME"
242 TS_ERRLOG
="$TS_OUTDIR/$TS_TESTNAME-$TS_SUBNAME.err"
243 TS_VGDUMP
="$TS_OUTDIR/$TS_TESTNAME-$TS_SUBNAME.vgdump"
244 TS_DIFF
="$TS_DIFFDIR/$TS_TESTNAME-$TS_SUBNAME"
245 TS_EXPECTED
="$TS_TOPDIR/expected/$TS_NS"
246 TS_EXPECTED_ERR
="$TS_TOPDIR/expected/$TS_NS.err"
247 TS_MOUNTPOINT
="$TS_OUTDIR/${TS_TESTNAME}-${TS_SUBNAME}-mnt"
249 rm -f $TS_OUTPUT $TS_ERRLOG $TS_VGDUMP
250 [ -d "$TS_OUTDIR" ] || mkdir
-p "$TS_OUTDIR"
252 touch $TS_OUTPUT $TS_ERRLOG
253 [ -n "$TS_VALGRIND_CMD" ] && touch $TS_VGDUMP
256 function ts_init_env
{
257 local mydir
=$
(ts_abspath
${0%/*})
264 ASAN_OPTIONS
="detect_leaks=0"
266 export LANG LANGUAGE LC_ALL CHARSET ASAN_OPTIONS
268 mydir
=$
(ts_canonicalize
"$mydir")
270 # automake directories
271 top_srcdir
=$
(ts_option_argument
"srcdir" "$*")
272 top_builddir
=$
(ts_option_argument
"builddir" "$*")
274 # where is this script
275 TS_TOPDIR
=$
(ts_abspath
$mydir/..
/..
/)
278 if [ -z "$top_srcdir" ]; then
279 top_srcdir
="$TS_TOPDIR/.."
281 if [ -z "$top_builddir" ]; then
282 top_builddir
="$TS_TOPDIR/.."
285 top_srcdir
=$
(ts_abspath
$top_srcdir)
286 top_builddir
=$
(ts_abspath
$top_builddir)
288 # We use helpser always from build tree
289 ts_helpersdir
="${top_builddir}/"
291 TS_USE_SYSTEM_COMMANDS
=$
(ts_has_option
"use-system-commands" "$*")
292 if [ "$TS_USE_SYSTEM_COMMANDS" == "yes" ]; then
293 # Don't define anything, just follow current PATH
296 # The default is to use commands from build tree
297 ts_commandsdir
="${top_builddir}/"
299 # some ul commands search other ul commands in $PATH
300 export PATH
="$ts_commandsdir:$PATH"
303 TS_SCRIPT
="$mydir/$(basename $0)"
304 TS_SUBDIR
=$
(dirname $TS_SCRIPT)
305 TS_TESTNAME
=$
(basename $TS_SCRIPT)
306 TS_COMPONENT
=$
(basename $TS_SUBDIR)
307 TS_DESC
=${TS_DESC:-$TS_TESTNAME}
314 TS_OUTDIR
="$top_builddir/tests/output/$TS_COMPONENT"
315 TS_DIFFDIR
="$top_builddir/tests/diff/$TS_COMPONENT"
317 TS_NOLOCKS
=$
(ts_has_option
"nolocks" "$*")
318 TS_LOCKDIR
="$top_builddir/tests/output"
320 # Don't lock if flock(1) is missing
321 type "flock" >/dev
/null
2>&1 || TS_NOLOCKS
="yes"
325 TS_NOSKIP_COMMANDS
=$
(ts_has_option
"noskip-commands" "$*")
326 TS_VERBOSE
=$
(ts_has_option
"verbose" "$*")
327 TS_SHOWDIFF
=$
(ts_has_option
"show-diff" "$*")
328 TS_PARALLEL
=$
(ts_has_option
"parallel" "$*")
329 TS_KNOWN_FAIL
=$
(ts_has_option
"known-fail" "$*")
330 TS_SKIP_LOOPDEVS
=$
(ts_has_option
"skip-loopdevs" "$*")
331 TS_PARSABLE
=$
(ts_has_option
"parsable" "$*")
332 [ "$TS_PARSABLE" = "yes" ] || TS_PARSABLE
="$TS_PARALLEL"
334 tmp
=$
( ts_has_option
"memcheck-valgrind" "$*")
335 if [ "$tmp" == "yes" -a -f /usr
/bin
/valgrind
]; then
336 TS_VALGRIND_CMD
="/usr/bin/valgrind"
338 tmp
=$
( ts_has_option
"memcheck-asan" "$*")
339 if [ "$tmp" == "yes" ]; then
343 BLKID_FILE
="$TS_OUTDIR/${TS_TESTNAME}.blkidtab"
345 declare -a TS_SUID_PROGS
346 declare -a TS_SUID_USER
347 declare -a TS_SUID_GROUP
348 declare -a TS_LOOP_DEVS
349 declare -a TS_LOCKFILE_FD
351 if [ -f $TS_TOPDIR/commands.sh
]; then
352 .
$TS_TOPDIR/commands.sh
357 rm -f $TS_OUTPUT $TS_ERRLOG $TS_VGDUMP
358 [ -d "$TS_OUTDIR" ] || mkdir
-p "$TS_OUTDIR"
360 touch $TS_OUTPUT $TS_ERRLOG
361 [ -n "$TS_VALGRIND_CMD" ] && touch $TS_VGDUMP
363 if [ "$TS_VERBOSE" == "yes" ]; then
365 echo " script: $TS_SCRIPT"
366 echo " commands: $ts_commandsdir"
367 echo " helpers: $ts_helpersdir"
368 echo " sub dir: $TS_SUBDIR"
369 echo " top dir: $TS_TOPDIR"
370 echo " self: $TS_SELF"
371 echo " test name: $TS_TESTNAME"
372 echo " test desc: $TS_DESC"
373 echo " component: $TS_COMPONENT"
374 echo " namespace: $TS_NS"
375 echo " verbose: $TS_VERBOSE"
376 echo " output: $TS_OUTPUT"
377 echo " error log: $TS_ERRLOG"
378 echo " valgrind: $TS_VGDUMP"
379 echo " expected: $TS_EXPECTED{.err}"
380 echo " mountpoint: $TS_MOUNTPOINT"
385 function ts_init_subtest
{
388 ts_init_core_subtest_env
389 TS_NSUBTESTS
=$
(( $TS_NSUBTESTS + 1 ))
391 if [ "$TS_PARSABLE" != "yes" ]; then
392 [ $TS_NSUBTESTS -eq 1 ] && echo
393 printf "%16s: %-27s ..." "" "$TS_SUBNAME"
400 local is_fake
=$
( ts_has_option
"fake" "$*")
401 local is_force
=$
( ts_has_option
"force" "$*")
403 if [ "$TS_PARSABLE" != "yes" ]; then
404 printf "%13s: %-30s ..." "$TS_COMPONENT" "$TS_DESC"
407 [ "$is_fake" == "yes" ] && ts_skip
"fake mode"
408 [ "$TS_OPTIONAL" == "yes" -a "$is_force" != "yes" ] && ts_skip
"optional"
411 function ts_init_suid
{
413 ct
=${#TS_SUID_PROGS[*]}
415 # Save info about original setting
416 TS_SUID_PROGS
[$ct]=$PROG
417 TS_SUID_USER
[$ct]=$
(stat
--printf="%U" $PROG)
418 TS_SUID_GROUP
[$ct]=$
(stat
--printf="%G" $PROG)
420 chown root.root
$PROG &> /dev
/null
421 chmod u
+s
$PROG &> /dev
/null
424 function ts_init_py
{
427 [ -f "$top_builddir/py${LIBNAME}.la" ] || ts_skip
"py${LIBNAME} not compiled"
429 export LD_LIBRARY_PATH
="$top_builddir/.libs:$LD_LIBRARY_PATH"
430 export PYTHONPATH
="$top_builddir/$LIBNAME/python:$top_builddir/.libs:$PYTHONPATH"
432 export PYTHON_VERSION
=$
(awk '/^PYTHON_VERSION/ { print $3 }' $top_builddir/Makefile
)
433 export PYTHON_MAJOR_VERSION
=$
(echo $PYTHON_VERSION |
sed 's/\..*//')
435 export PYTHON
="python${PYTHON_MAJOR_VERSION}"
444 if [ "$TS_ENABLE_ASAN" == "yes" ]; then
445 args
+=(env ASAN_OPTIONS
=detect_leaks
=1)
451 if [ -n "$TS_VALGRIND_CMD" ]; then
452 args
+=(libtool
--mode=execute
"$TS_VALGRIND_CMD" --tool=memcheck
--leak-check=full
)
453 args
+=(--leak-resolution=high
--num-callers=20 --log-file="$TS_VGDUMP")
459 function ts_gen_diff_from
{
465 diff -u $expected $output > $difffile
467 if [ $?
-ne 0 ] ||
[ -s $difffile ]; then
469 if [ "$TS_SHOWDIFF" == "yes" -a "$TS_KNOWN_FAIL" != "yes" ]; then
483 function ts_gen_diff
{
487 [ -f "$TS_OUTPUT" ] ||
return 1
488 [ -f "$TS_EXPECTED" ] || TS_EXPECTED
=/dev
/null
490 # remove libtool lt- prefixes
491 sed --in-place 's/^lt\-\(.*\: \)/\1/g' $TS_OUTPUT
492 sed --in-place 's/^lt\-\(.*\: \)/\1/g' $TS_ERRLOG
494 [ -d "$TS_DIFFDIR" ] || mkdir
-p "$TS_DIFFDIR"
496 ts_gen_diff_from
$TS_EXPECTED $TS_OUTPUT $TS_DIFF
499 # error log is fully optional
500 [ -f "$TS_EXPECTED_ERR" ] || TS_EXPECTED_ERR
=/dev
/null
501 [ -f "$TS_ERRLOG" ] || TS_ERRLOG
=/dev
/null
503 ts_gen_diff_from
$TS_EXPECTED_ERR $TS_ERRLOG $TS_DIFF.err
506 if [ $status_out -ne 0 -o $status_err -ne 0 ]; then
512 function tt_gen_mem_report
{
513 if [ -n "$TS_VALGRIND_CMD" ]; then
514 grep -q -E 'ERROR SUMMARY: [1-9]' $TS_VGDUMP &> /dev
/null
515 if [ $?
-eq 0 ]; then
516 echo "mem-error detected!"
523 function ts_finalize_subtest
{
527 if [ $?
-eq 1 ]; then
528 ts_failed_subtest
"$1"
531 ts_report_ok
"$(tt_gen_mem_report "$1")"
534 [ $res -ne 0 ] && TS_NSUBFAILED
=$
(( $TS_NSUBFAILED + 1 ))
536 # reset environment back to parental test
542 function ts_skip_subtest
{
544 # reset environment back to parental test
549 function ts_finalize
{
552 if [ $TS_NSUBTESTS -ne 0 ]; then
553 if ! ts_gen_diff ||
[ $TS_NSUBFAILED -ne 0 ]; then
554 ts_failed
"$TS_NSUBFAILED from $TS_NSUBTESTS sub-tests"
556 ts_ok
"all $TS_NSUBTESTS sub-tests PASSED"
560 ts_gen_diff || ts_failed
"$1"
569 function ts_cleanup_on_exit
{
571 for idx
in $
(seq 0 $
((${#TS_SUID_PROGS[*]} - 1))); do
572 PROG
=${TS_SUID_PROGS[$idx]}
573 chmod a-s
$PROG &> /dev
/null
574 chown
${TS_SUID_USER[$idx]}.
${TS_SUID_GROUP[$idx]} $PROG &> /dev
/null
577 for dev
in "${TS_LOOP_DEVS[@]}"; do
578 ts_device_deinit
"$dev"
585 function ts_image_md5sum
{
586 local img
=${1:-"$TS_OUTDIR/${TS_TESTNAME}.img"}
587 echo $
("$TS_HELPER_MD5" < "$img") $
(basename "$img")
590 function ts_image_init
{
591 local mib
=${1:-"5"} # size in MiBs
592 local img
=${2:-"$TS_OUTDIR/${TS_TESTNAME}.img"}
595 truncate
-s "${mib}M" "$img"
600 function ts_register_loop_device
{
601 local ct
=${#TS_LOOP_DEVS[*]}
605 function ts_device_init
{
609 img
=$
(ts_image_init
$1 $2)
610 dev
=$
($TS_CMD_LOSETUP --show -f "$img")
611 if [ "$?" != "0" -o "$dev" = "" ]; then
612 ts_die
"Cannot init device"
615 ts_register_loop_device
"$dev"
619 # call from ts_cleanup_on_exit() only because of TS_LOOP_DEVS maintenance
620 function ts_device_deinit
{
623 if [ -b "$DEV" ]; then
624 $TS_CMD_UMOUNT "$DEV" &> /dev
/null
625 $TS_CMD_LOSETUP -d "$DEV" &> /dev
/null
629 function ts_blkidtag_by_devname
()
636 out
=$
($TS_CMD_BLKID -p -s "$tag" -o value
"$dev")
640 test -n "$out" -a "$rval" = "0"
644 function ts_uuid_by_devname
{
645 ts_blkidtag_by_devname
"UUID" "$1"
649 function ts_label_by_devname
{
650 ts_blkidtag_by_devname
"LABEL" "$1"
654 function ts_fstype_by_devname
{
655 ts_blkidtag_by_devname
"TYPE" "$1"
659 function ts_device_has
{
665 vl
=$
(ts_blkidtag_by_devname
"$TAG" "$DEV")
666 test $?
= 0 -a "$vl" = "$VAL"
670 function ts_is_uuid
()
672 printf "%s\n" "$1" |
egrep -q '^[0-9a-z]{8}-[0-9a-z]{4}-[0-9a-z]{4}-[0-9a-z]{4}-[0-9a-z]{12}$'
676 function ts_udevadm_settle
()
678 local dev
=$1 # optional, might be empty
679 shift # all other args are tags, LABEL, UUID, ...
691 out
=$
($TS_CMD_MOUNT "$@" 2>&1)
693 echo -n "$out" >> $TS_OUTPUT
695 if [ $result != 0 ] \
696 && msg
=$
(echo "$out" |
grep -m1 "unknown filesystem type")
698 # skip only if reported fs correctly and if it's not available
699 fs
=$
(echo "$msg" |
sed -n "s/.*type '\(.*\)'$/\1/p")
700 [ "$fs" = "fs_exp" ] \
701 && grep -qe "[[:space:]]${fs}$" /proc
/filesystems
&>/dev
/null \
707 function ts_is_mounted
{
708 local DEV
=$
(ts_canonicalize
"$1")
710 grep -q "\(^\| \)$DEV " /proc
/mounts
&& return 0
712 if [ "${DEV#/dev/loop/}" != "$DEV" ]; then
713 grep -q "^/dev/loop${DEV#/dev/loop/} " /proc
/mounts
&& return 0
718 function ts_fstab_open
{
719 echo "# <!-- util-linux test entry" >> /etc
/fstab
722 function ts_fstab_close
{
723 echo "# -->" >> /etc
/fstab
724 sync
/etc
/fstab
2>/dev
/null
727 function ts_fstab_addline
{
729 local MNT
=${2:-"$TS_MOUNTPOINT"}
730 local FS
=${3:-"auto"}
731 local OPT
=${4:-"defaults"}
733 echo "$SPEC $MNT $FS $OPT 0 0" >> /etc
/fstab
736 function ts_fstab_lock
{
740 function ts_fstab_add
{
747 function ts_fstab_clean
{
748 ts_have_lock
"fstab" ||
return 0
750 /# <!-- util-linux/!b
756 s/# <!-- util-linux.*-->//;
759 sync
/etc
/fstab
2>/dev
/null
763 function ts_fdisk_clean
{
766 # remove non comparable parts of fdisk output
767 if [ -n "${DEVNAME}" ]; then
768 sed -i -e "s@${DEVNAME}@<removed>@;" $TS_OUTPUT $TS_ERRLOG
772 -e 's/Disk identifier:.*/Disk identifier: <removed>/' \
773 -e 's/Created a new.*/Created a new <removed>./' \
774 -e 's/^Device[[:blank:]]*Start/Device Start/' \
775 -e 's/^Device[[:blank:]]*Boot/Device Boot/' \
776 -e 's/Welcome to fdisk.*/Welcome to fdisk <removed>./' \
777 -e 's/typescript file.*/typescript file <removed>./' \
778 -e 's@^\(I/O size (minimum/op.* bytes /\) [1-9][0-9]* @\1 <removed> @' \
779 $TS_OUTPUT $TS_ERRLOG
783 # https://stackoverflow.com/questions/41603787/how-to-find-next-available-file-descriptor-in-bash
784 function ts_find_free_fd
()
788 for fd
in {3.
.200}; do
789 rco
="$(true 2>/dev/null >&${fd}; echo $?)"
790 rci
="$(true 2>/dev/null <&${fd}; echo $?)"
791 if [[ "${rco}${rci}" = "11" ]]; then
799 function ts_get_lock_fd
{
803 for fd
in "${!TS_LOCKFILE_FD[@]}"; do
804 if [ "${TS_LOCKFILE_FD["$fd"]}" = "$resource" ]; then
812 function ts_have_lock
{
815 test "$TS_NOLOCKS" = "yes" && return 0
816 ts_get_lock_fd
"$resource" >/dev
/null
&& return 0
822 local lockfile
="${TS_LOCKDIR}/${resource}.lock"
825 if [ "$TS_NOLOCKS" == "yes" ]; then
830 fd
=$
(ts_get_lock_fd
"$resource")
831 if [ -n "$fd" ]; then
832 echo "[$$ $TS_TESTNAME] ${resource} already locked!"
836 fd
=$
(ts_find_free_fd
) || ts_skip
"failed to find lock fd"
838 eval "exec $fd>$lockfile"
839 flock
--exclusive "$fd" || ts_skip
"failed to lock $resource"
841 TS_LOCKFILE_FD
["$fd"]="$resource"
842 ###echo "[$$ $TS_TESTNAME] Locked $resource"
847 local lockfile
="${TS_LOCKDIR}/${resource}.lock"
850 if [ "$TS_NOLOCKS" == "yes" ]; then
854 fd
=$
(ts_get_lock_fd
"$resource")
855 if [ -n "$fd" ]; then
857 TS_LOCKFILE_FD
["$fd"]=""
858 ###echo "[$$ $TS_TESTNAME] Unlocked $resource"
860 echo "[$$ $TS_TESTNAME] unlocking unlocked $resource!?"
864 function ts_scsi_debug_init
{
871 # dry run is not really reliable, real modprobe may still fail
872 modprobe
--dry-run --quiet scsi_debug
&>/dev
/null \
873 || ts_skip
"missing scsi_debug module (dry-run)"
875 # skip if still in use or removal of modules not supported at all
876 # We don't want a slow timeout here so we don't use ts_scsi_debug_rmmod!
877 modprobe
-r scsi_debug
&>/dev
/null
878 if [ "$?" -eq 1 ]; then
879 ts_unlock
"scsi_debug"
880 ts_skip
"cannot remove scsi_debug module (rmmod)"
883 modprobe
-b scsi_debug
"$@" &>/dev
/null \
884 || ts_skip
"cannot load scsi_debug module (modprobe)"
886 # it might be still not loaded, modprobe.conf or whatever
887 lsmod
2>/dev
/null |
grep -q "^scsi_debug " \
888 || ts_skip
"scsi_debug module not loaded (lsmod)"
892 # wait for device if udevadm settle does not work
893 for t
in 0 0.02 0.05 0.1 1; do
895 devname
=$
(grep --with-filename scsi_debug
/sys
/block
/*/device
/model
) && break
897 [ -n "${devname}" ] || ts_die
"timeout waiting for scsi_debug device"
899 devname
=$
(echo $devname |
awk -F '/' '{print $4}')
900 TS_DEVICE
="/dev/${devname}"
902 # TODO validate that device is really up, for now just a warning on stderr
903 test -b $TS_DEVICE ||
echo "warning: scsi_debug device is still down" >&2
906 # automatically called once in ts_cleanup_on_exit()
907 function ts_scsi_debug_rmmod
{
912 # We must not run if we don't have the lock
913 ts_have_lock
"scsi_debug" ||
return 0
915 # Return early most importantly in case we are not root or the module does
917 [ $UID -eq 0 ] ||
return 0
918 [ -n "$TS_DEVICE" ] ||
return 0
919 lsmod
2>/dev
/null |
grep -q "^scsi_debug " ||
return 0
923 # wait for successful rmmod if udevadm settle does not work
924 for t
in 0 0.02 0.05 0.1 1; do
926 lastmsg
="$(modprobe -r scsi_debug 2>&1)" && err
=0 && break
929 if [ "$err" = "1" ]; then
930 ts_log
"rmmod failed: '$lastmsg'"
931 ts_log
"timeout removing scsi_debug module (rmmod)"
934 if lsmod |
grep -q "^scsi_debug "; then
935 ts_log
"BUG! scsi_debug still loaded"
939 # TODO Do we need to validate that all devices are gone?
941 test -b "$TS_DEVICE" && echo "warning: scsi_debug device is still up" >&2
943 # TODO unset TS_DEVICE, check that nobody uses it later, e.g. ts_fdisk_clean
945 ts_unlock
"scsi_debug"
949 function ts_resolve_host
{
953 # currently we just resolve default records (might be "A", ipv4 only)
954 if type "dig" >/dev
/null
2>&1; then
955 tmp
=$
(dig "$host" +short
2>/dev
/null
) ||
return 1
956 elif type "nslookup" >/dev
/null
2>&1; then
957 tmp
=$
(nslookup "$host" 2>/dev
/null
) ||
return 1
958 tmp
=$
(echo "$tmp"|
grep -A1 "^Name:"|
grep "^Address:"| cut
-d" " -f2)
959 elif type "host" >/dev
/null
2>&1; then
960 tmp
=$
(host "$host" 2>/dev
/null
) ||
return 1
961 tmp
=$
(echo "$tmp" |
grep " has address " | cut
-d " " -f4)
962 elif type "getent" >/dev
/null
2>&1; then
963 tmp
=$
(getent ahosts
"$host" 2>/dev
/null
) ||
return 1
964 tmp
=$
(echo "$tmp" | cut
-d " " -f 1 |
sort -u)
967 # we return 1 if tmp is empty
968 test -n "$tmp" ||
return 1
969 echo "$tmp" |
sort -R |
head -n 1
972 # listen to unix socket (background socat)
973 function ts_init_socket_to_file
{
978 ts_check_prog
"socat"
979 rm -f "$socket" "$outfile"
981 # if socat is too old for these options we'll skip it below
982 socat
-u UNIX-LISTEN
:$socket,fork
,max-children
=1,backlog
=128 \
983 STDOUT
> "$outfile" 2>/dev
/null
&
986 # check for running background process
987 if [ "$pid" -le "0" ] ||
! kill -s 0 "$pid" &>/dev
/null
; then
988 ts_skip
"unable to run socat"
990 # wait for the socket listener
991 if ! socat
-u /dev
/null UNIX-CONNECT
:$socket,retry
=30,interval
=0.1 &>/dev
/null
; then
992 kill -9 "$pid" &>/dev
/null
993 ts_skip
"timeout waiting for socat socket"
996 if ! socat
-u /dev
/null UNIX-CONNECT
:$socket &>/dev
/null
; then
997 kill -9 "$pid" &>/dev
/null
998 ts_skip
"socat socket stopped listening"
1002 function ts_has_mtab_support
{
1003 grep -q '#define USE_LIBMOUNT_SUPPORT_MTAB' ${top_builddir}/config.h
1004 if [ $?
== 0 ]; then
1011 function ts_has_ncurses_support
{
1012 grep -q '#define HAVE_LIBNCURSES' ${top_builddir}/config.h
1013 if [ $?
== 0 ]; then