]>
Commit | Line | Data |
---|---|---|
92f2c23e KZ |
1 | # |
2 | # Copyright (C) 2007 Karel Zak <kzak@redhat.com> | |
3 | # | |
601d12fb | 4 | # This file is part of util-linux. |
92f2c23e KZ |
5 | # |
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. | |
10 | # | |
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. | |
15 | # | |
e83446da | 16 | |
e83446da | 17 | |
a02f320d KZ |
18 | function ts_abspath { |
19 | cd $1 | |
20 | pwd | |
21 | } | |
d42bbae5 | 22 | |
6dfe6f0e KZ |
23 | function ts_canonicalize { |
24 | P="$1" | |
25 | C=$(readlink -f $P) | |
26 | ||
27 | if [ -n "$C" ]; then | |
28 | echo "$C" | |
29 | else | |
30 | echo "$P" | |
31 | fi | |
32 | } | |
33 | ||
723c7043 SK |
34 | function ts_cd { |
35 | if [ $# -eq 0 ]; then | |
36 | ts_failed "ul_cd: not enough arguments" | |
37 | fi | |
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" | |
41 | fi | |
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" | |
45 | fi | |
46 | } | |
47 | ||
40e6f7a0 SK |
48 | function ts_report { |
49 | if [ "$TS_PARALLEL" == "yes" ]; then | |
50 | echo "$TS_TITLE $1" | |
51 | else | |
52 | echo "$1" | |
53 | fi | |
2f791546 | 54 | } |
40e6f7a0 | 55 | |
2f791546 SK |
56 | function ts_check_test_command { |
57 | if [ ! -x "$1" ]; then | |
58 | ts_skip "${1##*/} not found" | |
59 | fi | |
40e6f7a0 SK |
60 | } |
61 | ||
d1962aae RM |
62 | function ts_check_prog { |
63 | local cmd=$1 | |
64 | type "$cmd" >/dev/null 2>&1 || ts_skip "missing in PATH: $cmd" | |
65 | } | |
66 | ||
5ec15aef RM |
67 | function ts_check_losetup { |
68 | local tmp | |
69 | ts_check_test_command "$TS_CMD_LOSETUP" | |
70 | ||
71 | # assuming that losetup -f works ... to be checked somewhere else | |
72 | tmp=$($TS_CMD_LOSETUP -f 2>/dev/null) | |
73 | if test -b "$tmp"; then | |
74 | return 0 | |
75 | fi | |
76 | ts_skip "no loop device support" | |
77 | } | |
78 | ||
09888efe | 79 | function ts_skip_subtest { |
40e6f7a0 | 80 | ts_report " IGNORE ($1)" |
09888efe KZ |
81 | } |
82 | ||
83 | function ts_skip { | |
84 | ts_skip_subtest "$1" | |
f0b561b6 | 85 | if [ -n "$2" -a -b "$2" ]; then |
3dfa278e KZ |
86 | ts_device_deinit "$2" |
87 | fi | |
b30cd7ee KZ |
88 | exit 0 |
89 | } | |
90 | ||
09892fb6 | 91 | function ts_skip_nonroot { |
f0b561b6 | 92 | if [ $UID -ne 0 ]; then |
09892fb6 KZ |
93 | ts_skip "not root permissions" |
94 | fi | |
95 | } | |
96 | ||
09888efe | 97 | function ts_failed_subtest { |
733094a8 RM |
98 | local msg="FAILED" |
99 | local ret=1 | |
100 | if [ "$TS_KNOWN_FAIL" = "yes" ]; then | |
101 | msg="KNOWN FAILED" | |
102 | ret=0 | |
103 | fi | |
104 | ||
05de8126 | 105 | if [ x"$1" == x"" ]; then |
733094a8 | 106 | ts_report " $msg ($TS_NS)" |
05de8126 | 107 | else |
733094a8 | 108 | ts_report " $msg ($1)" |
05de8126 | 109 | fi |
733094a8 RM |
110 | |
111 | return $ret | |
05de8126 KZ |
112 | } |
113 | ||
09888efe KZ |
114 | function ts_failed { |
115 | ts_failed_subtest "$1" | |
733094a8 | 116 | exit $? |
7641ccec RM |
117 | } |
118 | ||
09888efe | 119 | function ts_ok_subtest { |
05de8126 | 120 | if [ x"$1" == x"" ]; then |
40e6f7a0 | 121 | ts_report " OK" |
05de8126 | 122 | else |
40e6f7a0 | 123 | ts_report " OK ($1)" |
05de8126 | 124 | fi |
09888efe KZ |
125 | } |
126 | ||
127 | function ts_ok { | |
128 | ts_ok_subtest "$1" | |
05de8126 KZ |
129 | exit 0 |
130 | } | |
131 | ||
57a917d6 KZ |
132 | function ts_log { |
133 | echo "$1" >> $TS_OUTPUT | |
134 | [ "$TS_VERBOSE" == "yes" ] && echo "$1" | |
135 | } | |
136 | ||
1d9acab1 KZ |
137 | function ts_has_option { |
138 | NAME="$1" | |
139 | ALL="$2" | |
eac40eb0 RM |
140 | |
141 | # user may set options by env for a single test or whole component | |
142 | # e.g. TS_OPT_ipcs_limits2_fake="yes" or TS_OPT_ipcs_fake="yes" | |
c08863ff RM |
143 | local v_test=${TS_TESTNAME//[-.]/_} |
144 | local v_comp=${TS_COMPONENT//[-.]/_} | |
145 | local v_name=${NAME//[-.]/_} | |
146 | eval local env_opt_test=\$TS_OPT_${v_comp}_${v_test}_${v_name} | |
147 | eval local env_opt_comp=\$TS_OPT_${v_comp}_${v_name} | |
eac40eb0 RM |
148 | if [ "$env_opt_test" = "yes" \ |
149 | -o "$env_opt_comp" = "yes" -a "$env_opt_test" != "no" ]; then | |
150 | echo "yes" | |
151 | return | |
152 | elif [ "$env_opt_test" = "no" \ | |
153 | -o "$env_opt_comp" = "no" -a "$env_opt_test" != "yes" ]; then | |
154 | return | |
155 | fi | |
156 | ||
157 | # or just check the global command line options | |
a02f320d | 158 | echo -n $ALL | sed 's/ //g' | awk 'BEGIN { FS="="; RS="--" } /('$NAME'$|'$NAME'=)/ { print "yes" }' |
1d9acab1 KZ |
159 | } |
160 | ||
1b03e2cd KZ |
161 | function ts_option_argument { |
162 | NAME="$1" | |
163 | ALL="$2" | |
164 | echo -n $ALL | sed 's/ //g' | awk 'BEGIN { FS="="; RS="--" } /'$NAME'=/ { print $2 }' | |
165 | } | |
166 | ||
db17c74b KZ |
167 | function ts_init_core_env { |
168 | TS_NS="$TS_COMPONENT/$TS_TESTNAME" | |
169 | TS_OUTPUT="$TS_OUTDIR/$TS_TESTNAME" | |
495674f8 | 170 | TS_VGDUMP="$TS_OUTDIR/$TS_TESTNAME.vgdump" |
db17c74b KZ |
171 | TS_DIFF="$TS_DIFFDIR/$TS_TESTNAME" |
172 | TS_EXPECTED="$TS_TOPDIR/expected/$TS_NS" | |
173 | TS_MOUNTPOINT="$TS_OUTDIR/${TS_TESTNAME}-mnt" | |
174 | } | |
175 | ||
176 | function ts_init_core_subtest_env { | |
177 | TS_NS="$TS_COMPONENT/$TS_TESTNAME-$TS_SUBNAME" | |
178 | TS_OUTPUT="$TS_OUTDIR/$TS_TESTNAME-$TS_SUBNAME" | |
495674f8 | 179 | TS_VGDUMP="$TS_OUTDIR/$TS_TESTNAME-$TS_SUBNAME.vgdump" |
db17c74b KZ |
180 | TS_DIFF="$TS_DIFFDIR/$TS_TESTNAME-$TS_SUBNAME" |
181 | TS_EXPECTED="$TS_TOPDIR/expected/$TS_NS" | |
64a2331f | 182 | TS_MOUNTPOINT="$TS_OUTDIR/${TS_TESTNAME}-${TS_SUBNAME}-mnt" |
495674f8 KZ |
183 | |
184 | rm -f $TS_OUTPUT $TS_VGDUMP | |
4210c47a KZ |
185 | [ -d "$TS_OUTDIR" ] || mkdir -p "$TS_OUTDIR" |
186 | ||
495674f8 KZ |
187 | touch $TS_OUTPUT |
188 | [ -n "$TS_VALGRIND_CMD" ] && touch $TS_VGDUMP | |
db17c74b KZ |
189 | } |
190 | ||
905d0b9b | 191 | function ts_init_env { |
e130ce53 | 192 | local mydir=$(ts_abspath ${0%/*}) |
d9a9ff09 | 193 | local tmp |
d42bbae5 | 194 | |
08b825db YD |
195 | LANG="POSIX" |
196 | LANGUAGE="POSIX" | |
197 | LC_ALL="POSIX" | |
198 | CHARSET="UTF-8" | |
199 | ||
1b03e2cd KZ |
200 | export LANG LANGUAGE LC_ALL CHARSET |
201 | ||
6dfe6f0e KZ |
202 | mydir=$(ts_canonicalize "$mydir") |
203 | ||
1b03e2cd KZ |
204 | # automake directories |
205 | top_srcdir=$(ts_option_argument "srcdir" "$*") | |
206 | top_builddir=$(ts_option_argument "builddir" "$*") | |
d42bbae5 | 207 | |
1b03e2cd | 208 | # where is this script |
a02f320d | 209 | TS_TOPDIR=$(ts_abspath $mydir/../../) |
1b03e2cd KZ |
210 | |
211 | # default | |
212 | if [ -z "$top_srcdir" ]; then | |
213 | top_srcdir="$TS_TOPDIR/.." | |
214 | fi | |
215 | if [ -z "$top_builddir" ]; then | |
216 | top_builddir="$TS_TOPDIR/.." | |
217 | fi | |
218 | ||
219 | top_srcdir=$(ts_abspath $top_srcdir) | |
220 | top_builddir=$(ts_abspath $top_builddir) | |
221 | ||
a02f320d | 222 | TS_SCRIPT="$mydir/$(basename $0)" |
d42bbae5 KZ |
223 | TS_SUBDIR=$(dirname $TS_SCRIPT) |
224 | TS_TESTNAME=$(basename $TS_SCRIPT) | |
225 | TS_COMPONENT=$(basename $TS_SUBDIR) | |
226 | ||
09888efe KZ |
227 | TS_NSUBTESTS=0 |
228 | TS_NSUBFAILED=0 | |
229 | ||
d42bbae5 KZ |
230 | TS_SELF="$TS_SUBDIR" |
231 | ||
1b03e2cd KZ |
232 | TS_OUTDIR="$top_builddir/tests/output/$TS_COMPONENT" |
233 | TS_DIFFDIR="$top_builddir/tests/diff/$TS_COMPONENT" | |
db17c74b KZ |
234 | |
235 | ts_init_core_env | |
d42bbae5 | 236 | |
cbac71bd | 237 | TS_VERBOSE=$(ts_has_option "verbose" "$*") |
40e6f7a0 | 238 | TS_PARALLEL=$(ts_has_option "parallel" "$*") |
7641ccec | 239 | TS_KNOWN_FAIL=$(ts_has_option "known-fail" "$*") |
d42bbae5 | 240 | |
d9a9ff09 RM |
241 | tmp=$( ts_has_option "memcheck" "$*") |
242 | if [ "$tmp" == "yes" -a -f /usr/bin/valgrind ]; then | |
243 | TS_VALGRIND_CMD="/usr/bin/valgrind" | |
244 | fi | |
245 | ||
632830cc | 246 | BLKID_FILE="$TS_OUTDIR/${TS_TESTNAME}.blkidtab" |
d42bbae5 | 247 | |
1b5417ac KZ |
248 | declare -a TS_SUID_PROGS |
249 | declare -a TS_SUID_USER | |
250 | declare -a TS_SUID_GROUP | |
251 | ||
adc8c80f KZ |
252 | if [ -f $TS_TOPDIR/commands.sh ]; then |
253 | . $TS_TOPDIR/commands.sh | |
254 | fi | |
a02f320d | 255 | |
b7ea07e0 | 256 | export BLKID_FILE |
66822df3 | 257 | |
495674f8 | 258 | rm -f $TS_OUTPUT $TS_VGDUMP |
4210c47a KZ |
259 | [ -d "$TS_OUTDIR" ] || mkdir -p "$TS_OUTDIR" |
260 | ||
3dfa278e | 261 | touch $TS_OUTPUT |
495674f8 | 262 | [ -n "$TS_VALGRIND_CMD" ] && touch $TS_VGDUMP |
97cdb3cb | 263 | |
d42bbae5 KZ |
264 | if [ "$TS_VERBOSE" == "yes" ]; then |
265 | echo | |
266 | echo " script: $TS_SCRIPT" | |
d42bbae5 | 267 | echo " sub dir: $TS_SUBDIR" |
a02f320d | 268 | echo " top dir: $TS_TOPDIR" |
d42bbae5 KZ |
269 | echo " self: $TS_SELF" |
270 | echo " test name: $TS_TESTNAME" | |
271 | echo " test desc: $TS_DESC" | |
272 | echo " component: $TS_COMPONENT" | |
273 | echo " namespace: $TS_NS" | |
274 | echo " verbose: $TS_VERBOSE" | |
275 | echo " output: $TS_OUTPUT" | |
495674f8 | 276 | echo " valgrind: $TS_VGDUMP" |
d42bbae5 KZ |
277 | echo " expected: $TS_EXPECTED" |
278 | echo " mountpoint: $TS_MOUNTPOINT" | |
279 | echo | |
280 | fi | |
905d0b9b KZ |
281 | } |
282 | ||
09888efe KZ |
283 | function ts_init_subtest { |
284 | ||
285 | TS_SUBNAME="$1" | |
286 | ||
db17c74b | 287 | ts_init_core_subtest_env |
09888efe KZ |
288 | |
289 | [ $TS_NSUBTESTS -eq 0 ] && echo | |
290 | TS_NSUBTESTS=$(( $TS_NSUBTESTS + 1 )) | |
291 | ||
40e6f7a0 SK |
292 | if [ "$TS_PARALLEL" == "yes" ]; then |
293 | TS_TITLE=$(printf "%13s: %-30s ...\n%16s: %-27s ..." "$TS_COMPONENT" "$TS_DESC" "" "$TS_SUBNAME") | |
294 | else | |
295 | TS_TITLE=$(printf "%16s: %-27s ..." "" "$TS_SUBNAME") | |
296 | echo -n "$TS_TITLE" | |
297 | fi | |
09888efe KZ |
298 | } |
299 | ||
905d0b9b | 300 | function ts_init { |
d9a9ff09 RM |
301 | ts_init_env "$*" |
302 | ||
905d0b9b | 303 | local is_fake=$( ts_has_option "fake" "$*") |
949cf64b | 304 | local is_force=$( ts_has_option "force" "$*") |
905d0b9b | 305 | |
40e6f7a0 SK |
306 | if [ "$TS_PARALLEL" == "yes" ]; then |
307 | TS_TITLE=$(printf "%13s: %-30s ..." "$TS_COMPONENT" "$TS_DESC") | |
308 | else | |
309 | TS_TITLE=$(printf "%13s: %-30s ..." "$TS_COMPONENT" "$TS_DESC") | |
310 | echo -n "$TS_TITLE" | |
311 | fi | |
d42bbae5 KZ |
312 | |
313 | [ "$is_fake" == "yes" ] && ts_skip "fake mode" | |
949cf64b | 314 | [ "$TS_OPTIONAL" == "yes" -a "$is_force" != "yes" ] && ts_skip "optional" |
e83446da KZ |
315 | } |
316 | ||
1b5417ac KZ |
317 | function ts_init_suid { |
318 | PROG="$1" | |
319 | ct=${#TS_SUID_PROGS[*]} | |
320 | ||
321 | # Save info about original setting | |
322 | TS_SUID_PROGS[$ct]=$PROG | |
323 | TS_SUID_USER[$ct]=$(stat --printf="%U" $PROG) | |
324 | TS_SUID_GROUP[$ct]=$(stat --printf="%G" $PROG) | |
325 | ||
326 | chown root.root $PROG &> /dev/null | |
327 | chmod u+s $PROG &> /dev/null | |
328 | } | |
329 | ||
a731b541 KZ |
330 | function ts_init_py { |
331 | LIBNAME="$1" | |
332 | ||
15b2606c | 333 | [ -f "$top_builddir/py${LIBNAME}.la" ] || ts_skip "py${LIBNAME} not compiled" |
a731b541 | 334 | |
fa8f8b74 RM |
335 | export LD_LIBRARY_PATH="$top_builddir/.libs:$LD_LIBRARY_PATH" |
336 | export PYTHONPATH="$top_builddir/$LIBNAME/python:$top_builddir/.libs:$PYTHONPATH" | |
46407453 OO |
337 | |
338 | export PYTHON_VERSION=$(awk '/^PYTHON_VERSION/ { print $3 }' $top_builddir/Makefile) | |
339 | export PYTHON_MAJOR_VERSION=$(echo $PYTHON_VERSION | sed 's/\..*//') | |
340 | ||
341 | export PYTHON="python${PYTHON_MAJOR_VERSION}" | |
a731b541 KZ |
342 | } |
343 | ||
495674f8 KZ |
344 | function ts_valgrind { |
345 | if [ -z "$TS_VALGRIND_CMD" ]; then | |
346 | $* | |
347 | else | |
348 | $TS_VALGRIND_CMD --tool=memcheck --leak-check=full \ | |
349 | --leak-resolution=high --num-callers=20 \ | |
350 | --log-file="$TS_VGDUMP" $* | |
351 | fi | |
352 | } | |
353 | ||
09888efe KZ |
354 | function ts_gen_diff { |
355 | local res=0 | |
356 | ||
64a2331f | 357 | if [ -s "$TS_OUTPUT" ]; then |
4210c47a | 358 | |
46949388 KZ |
359 | # remove libtool lt- prefixes |
360 | sed --in-place 's/^lt\-\(.*\: \)/\1/g' $TS_OUTPUT | |
361 | ||
4210c47a | 362 | [ -d "$TS_DIFFDIR" ] || mkdir -p "$TS_DIFFDIR" |
09888efe | 363 | diff -u $TS_EXPECTED $TS_OUTPUT > $TS_DIFF |
4210c47a KZ |
364 | |
365 | if [ -s $TS_DIFF ]; then | |
366 | res=1 | |
367 | else | |
368 | rm -f $TS_DIFF; | |
369 | fi | |
09888efe KZ |
370 | else |
371 | res=1 | |
372 | fi | |
373 | return $res | |
374 | } | |
375 | ||
495674f8 KZ |
376 | function tt_gen_mem_report { |
377 | [ -z "$TS_VALGRIND_CMD" ] && echo "$1" | |
378 | ||
379 | grep -q -E 'ERROR SUMMARY: [1-9]' $TS_VGDUMP &> /dev/null | |
380 | if [ $? -eq 0 ]; then | |
381 | echo "mem-error detected!" | |
382 | fi | |
383 | } | |
384 | ||
09888efe | 385 | function ts_finalize_subtest { |
e83446da KZ |
386 | local res=0 |
387 | ||
64a2331f | 388 | if [ -s "$TS_EXPECTED" ]; then |
09888efe KZ |
389 | ts_gen_diff |
390 | if [ $? -eq 1 ]; then | |
391 | ts_failed_subtest "$1" | |
392 | res=1 | |
393 | else | |
495674f8 | 394 | ts_ok_subtest "$(tt_gen_mem_report "$1")" |
09888efe KZ |
395 | fi |
396 | else | |
397 | ts_skip_subtest "output undefined" | |
398 | fi | |
399 | ||
400 | [ $res -ne 0 ] && TS_NSUBFAILED=$(( $TS_NSUBFAILED + 1 )) | |
db17c74b KZ |
401 | |
402 | # reset environment back to parental test | |
403 | ts_init_core_env | |
404 | ||
09888efe KZ |
405 | return $res |
406 | } | |
407 | ||
408 | function ts_finalize { | |
1b5417ac KZ |
409 | for idx in $(seq 0 $((${#TS_SUID_PROGS[*]} - 1))); do |
410 | PROG=${TS_SUID_PROGS[$idx]} | |
411 | chmod a-s $PROG &> /dev/null | |
412 | chown ${TS_SUID_USER[$idx]}.${TS_SUID_GROUP[$idx]} $PROG &> /dev/null | |
413 | done | |
414 | ||
09888efe | 415 | if [ $TS_NSUBTESTS -ne 0 ]; then |
b8ab3744 | 416 | printf "%11s..." |
09888efe KZ |
417 | if [ $TS_NSUBFAILED -ne 0 ]; then |
418 | ts_failed "$TS_NSUBFAILED from $TS_NSUBTESTS sub-tests" | |
419 | else | |
420 | ts_ok "all $TS_NSUBTESTS sub-tests PASSED" | |
421 | fi | |
e83446da | 422 | fi |
425ca40a KZ |
423 | |
424 | if [ -s $TS_EXPECTED ]; then | |
425 | ts_gen_diff | |
426 | if [ $? -eq 1 ]; then | |
427 | ts_failed "$1" | |
428 | fi | |
429 | ts_ok "$1" | |
430 | fi | |
431 | ||
09888efe | 432 | ts_skip "output undefined" |
e83446da KZ |
433 | } |
434 | ||
3dfa278e | 435 | function ts_die { |
57a917d6 | 436 | ts_log "$1" |
3dfa278e KZ |
437 | if [ -n "$2" ] && [ -b "$2" ]; then |
438 | ts_device_deinit "$2" | |
2f791546 | 439 | ts_fstab_clean # for sure... |
3dfa278e KZ |
440 | fi |
441 | ts_finalize | |
442 | } | |
443 | ||
35c636e1 KZ |
444 | function ts_image_md5sum { |
445 | local img=${1:-"$TS_OUTDIR/${TS_TESTNAME}.img"} | |
446 | echo $(md5sum "$img" | awk '{printf $1}') $(basename "$img") | |
447 | } | |
05de8126 | 448 | |
35c636e1 KZ |
449 | function ts_image_init { |
450 | local mib=${1:-"5"} # size in MiBs | |
451 | local img=${2:-"$TS_OUTDIR/${TS_TESTNAME}.img"} | |
2f791546 | 452 | |
35c636e1 KZ |
453 | dd if=/dev/zero of="$img" bs=1M count=$mib &> /dev/null |
454 | echo "$img" | |
455 | return 0 | |
456 | } | |
05de8126 | 457 | |
35c636e1 | 458 | function ts_device_init { |
b5eb5097 RM |
459 | local img |
460 | local dev | |
df7e52d7 | 461 | |
b5eb5097 RM |
462 | img=$(ts_image_init $1 $2) |
463 | dev=$($TS_CMD_LOSETUP --show -f "$img") | |
df7e52d7 | 464 | |
632830cc | 465 | echo $dev |
df7e52d7 KZ |
466 | } |
467 | ||
468 | function ts_device_deinit { | |
3dfa278e KZ |
469 | local DEV="$1" |
470 | ||
471 | if [ -b "$DEV" ]; then | |
472 | $TS_CMD_UMOUNT "$DEV" &> /dev/null | |
473 | $TS_CMD_LOSETUP -d "$DEV" &> /dev/null | |
df7e52d7 KZ |
474 | fi |
475 | } | |
064b8c38 | 476 | |
3f5bda01 | 477 | function ts_uuid_by_devname { |
283a8c15 | 478 | echo $($TS_CMD_BLKID -p -s UUID -o value $1) |
3f5bda01 KZ |
479 | } |
480 | ||
481 | function ts_label_by_devname { | |
283a8c15 | 482 | echo $($TS_CMD_BLKID -p -s LABEL -o value $1) |
3f5bda01 KZ |
483 | } |
484 | ||
485 | function ts_fstype_by_devname { | |
283a8c15 | 486 | echo $($TS_CMD_BLKID -p -s TYPE -o value $1) |
3f5bda01 KZ |
487 | } |
488 | ||
489 | function ts_device_has { | |
490 | local TAG="$1" | |
491 | local VAL="$2" | |
492 | local DEV="$3" | |
3dfa278e | 493 | local vl="" |
3f5bda01 KZ |
494 | |
495 | case $TAG in | |
496 | "TYPE") vl=$(ts_fstype_by_devname $DEV);; | |
497 | "LABEL") vl=$(ts_label_by_devname $DEV);; | |
498 | "UUID") vl=$(ts_uuid_by_devname $DEV);; | |
499 | *) return 1;; | |
500 | esac | |
501 | ||
502 | if [ "$vl" == "$VAL" ]; then | |
503 | return 0 | |
504 | fi | |
505 | return 1 | |
506 | } | |
3dfa278e KZ |
507 | |
508 | function ts_device_has_uuid { | |
509 | ts_uuid_by_devname "$1" | egrep -q '^[0-9a-z]{8}-[0-9a-z]{4}-[0-9a-z]{4}-[0-9a-z]{4}-[0-9a-z]{12}$' | |
510 | return $? | |
511 | } | |
512 | ||
2f954947 RM |
513 | function ts_mount { |
514 | local out | |
515 | local result | |
516 | local msg | |
a23c435e | 517 | local fs |
d2cc2ba7 RM |
518 | local fs_exp=$1 |
519 | shift | |
2f954947 RM |
520 | |
521 | out=$($TS_CMD_MOUNT "$@" 2>&1) | |
522 | result=$? | |
523 | echo -n "$out" >> $TS_OUTPUT | |
524 | ||
a23c435e RM |
525 | if [ $result != 0 ] \ |
526 | && msg=$(echo "$out" | grep -m1 "unknown filesystem type") | |
527 | then | |
528 | # skip only if reported fs correctly and if it's not available | |
529 | fs=$(echo "$msg" | sed -n "s/.*type '\(.*\)'$/\1/p") | |
d2cc2ba7 | 530 | [ "$fs" = "fs_exp" ] \ |
a23c435e RM |
531 | && grep -qe "[[:space:]]${fs}$" /proc/filesystems &>/dev/null \ |
532 | || ts_skip "$msg" | |
2f954947 RM |
533 | fi |
534 | return $result | |
535 | } | |
536 | ||
3ff2557c | 537 | function ts_is_mounted { |
6dfe6f0e | 538 | local DEV=$(ts_canonicalize "$1") |
3ff2557c KZ |
539 | |
540 | grep -q $DEV /proc/mounts && return 0 | |
541 | ||
542 | if [ "${DEV#/dev/loop/}" != "$DEV" ]; then | |
543 | return grep -q "/dev/loop${DEV#/dev/loop/}" /proc/mounts | |
544 | fi | |
545 | return 1 | |
546 | } | |
547 | ||
c98825ac | 548 | function ts_fstab_open { |
601d12fb | 549 | echo "# <!-- util-linux test entry" >> /etc/fstab |
c98825ac KZ |
550 | } |
551 | ||
552 | function ts_fstab_close { | |
553 | echo "# -->" >> /etc/fstab | |
554 | } | |
555 | ||
556 | function ts_fstab_addline { | |
557 | local SPEC="$1" | |
558 | local MNT=${2:-"$TS_MOUNTPOINT"} | |
559 | local FS=${3:-"auto"} | |
b002d021 | 560 | local OPT=${4:-"defaults"} |
c98825ac | 561 | |
b002d021 | 562 | echo "$SPEC $MNT $FS $OPT 0 0" >> /etc/fstab |
c98825ac KZ |
563 | } |
564 | ||
565 | function ts_fstab_add { | |
566 | ts_fstab_open | |
b002d021 | 567 | ts_fstab_addline $* |
c98825ac KZ |
568 | ts_fstab_close |
569 | } | |
570 | ||
571 | function ts_fstab_clean { | |
572 | sed --in-place " | |
601d12fb | 573 | /# <!-- util-linux/!b |
c98825ac KZ |
574 | :a |
575 | /# -->/!{ | |
576 | N | |
577 | ba | |
578 | } | |
601d12fb | 579 | s/# <!-- util-linux.*-->//; |
c98825ac KZ |
580 | /^$/d" /etc/fstab |
581 | } | |
582 | ||
d0bcd9b3 | 583 | function ts_fdisk_clean { |
e1fe1815 | 584 | local DEVNAME=$1 |
618ec053 | 585 | |
d0bcd9b3 | 586 | # remove non comparable parts of fdisk output |
618ec053 | 587 | if [ x"${DEVNAME}" != x"" ]; then |
e1fe1815 | 588 | sed -i -e "s:${DEVNAME}:<removed>:g" $TS_OUTPUT |
618ec053 KZ |
589 | fi |
590 | ||
e1fe1815 KZ |
591 | sed -i -e 's/Disk identifier:.*/Disk identifier: <removed>/g' \ |
592 | -e 's/Created a new.*/Created a new <removed>./g' \ | |
593 | -e 's/^Device[[:blank:]]*Start/Device Start/g' \ | |
594 | -e 's/^Device[[:blank:]]*Boot/Device Boot/g' \ | |
595 | -e 's/^Device[[:blank:]]*Flag/Device Flag/g' \ | |
596 | -e 's/Welcome to fdisk.*/Welcome to fdisk <removed>./g' \ | |
d0bcd9b3 FC |
597 | $TS_OUTPUT |
598 | } | |
618ec053 KZ |
599 | |
600 | function ts_scsi_debug_init { | |
dd761f79 RM |
601 | local devname |
602 | TS_DEVICE="none" | |
618ec053 | 603 | |
9779f598 | 604 | # dry run is not really reliable, real modprobe may still fail |
f80c0d38 RM |
605 | modprobe --dry-run --quiet scsi_debug &>/dev/null \ |
606 | || ts_skip "missing scsi_debug module (dry-run)" | |
618ec053 | 607 | |
f80c0d38 RM |
608 | # skip if still in use or removal of modules not supported at all |
609 | modprobe -r scsi_debug &>/dev/null \ | |
610 | || ts_skip "cannot remove scsi_debug module (rmmod)" | |
611 | ||
612 | modprobe -b scsi_debug $* &>/dev/null \ | |
613 | || ts_skip "cannot load scsi_debug module (modprobe)" | |
614 | ||
615 | # it might be still not loaded, modprobe.conf or whatever | |
616 | lsmod | grep -q "^scsi_debug " \ | |
617 | || ts_skip "scsi_debug module not loaded (lsmod)" | |
618ec053 | 618 | |
dd761f79 | 619 | devname=$(grep --with-filename scsi_debug /sys/block/*/device/model | awk -F '/' '{print $4}') |
f80c0d38 | 620 | [ "x${devname}" == "x" ] && ts_die "cannot find scsi_debug device" |
618ec053 KZ |
621 | |
622 | sleep 1 | |
623 | udevadm settle | |
624 | ||
dd761f79 | 625 | TS_DEVICE="/dev/${devname}" |
618ec053 | 626 | } |
a98de969 RM |
627 | |
628 | function ts_resolve_host { | |
629 | local host="$1" | |
630 | local tmp | |
631 | ||
632 | # currently we just resolve default records (might be "A", ipv4 only) | |
633 | if type "dig" >/dev/null 2>&1; then | |
634 | tmp=$(dig "$host" +short 2>/dev/null) || return 1 | |
635 | elif type "nslookup" >/dev/null 2>&1; then | |
636 | tmp=$(nslookup "$host" 2>/dev/null) || return 1 | |
637 | tmp=$(echo "$tmp"| grep -A1 "^Name:"| grep "^Address:"| cut -d" " -f2) | |
eee79f28 AH |
638 | elif type "host" >/dev/null 2>&1; then |
639 | tmp=$(host "$host" 2>/dev/null) || return 1 | |
640 | tmp=$(echo "$tmp" | grep " has address " | cut -d " " -f4) | |
641 | elif type "getent" >/dev/null 2>&1; then | |
642 | tmp=$(getent ahosts "$host" 2>/dev/null) || return 1 | |
643 | tmp=$(echo "$tmp" | cut -d " " -f 1 | sort -u) | |
a98de969 RM |
644 | fi |
645 | ||
646 | # we return 1 if tmp is empty | |
647 | test -n "$tmp" || return 1 | |
648 | echo "$tmp" | sort -R | head -n 1 | |
649 | } |