]> git.ipfire.org Git - thirdparty/systemd.git/blob - test/test-functions
Merge pull request #2886 from fbuihuu/systemctl-list-units-inactive-state
[thirdparty/systemd.git] / test / test-functions
1 #!/bin/bash
2 # -*- mode: shell-script; indent-tabs-mode: nil; sh-basic-offset: 4; -*-
3 # ex: ts=8 sw=4 sts=4 et filetype=sh
4 PATH=/sbin:/bin:/usr/sbin:/usr/bin
5 export PATH
6
7 LOOKS_LIKE_DEBIAN=$(source /etc/os-release && [[ "$ID" = "debian" || "$ID_LIKE" = "debian" ]] && echo yes)
8 KERNEL_VER=${KERNEL_VER-$(uname -r)}
9 KERNEL_MODS="/lib/modules/$KERNEL_VER/"
10 QEMU_TIMEOUT="${QEMU_TIMEOUT:-infinity}"
11 FSTYPE="${FSTYPE:-ext3}"
12
13 if ! ROOTLIBDIR=$(pkg-config --variable=systemdutildir systemd); then
14 echo "WARNING! Cannot determine rootlibdir from pkg-config, assuming /usr/lib/systemd" >&2
15 ROOTLIBDIR=/usr/lib/systemd
16 fi
17
18 BASICTOOLS="sh bash setsid loadkeys setfont login sulogin gzip sleep echo mount umount cryptsetup date dmsetup modprobe sed cmp tee rm"
19 DEBUGTOOLS="df free ls stty cat ps ln ip route dmesg dhclient mkdir cp ping dhclient strace less grep id tty touch du sort hostname find"
20
21 function find_qemu_bin() {
22 # SUSE and Red Hat call the binary qemu-kvm
23 # Debian and Gentoo call it kvm
24 [ "$QEMU_BIN" ] || QEMU_BIN=$(which -a kvm qemu-kvm 2>/dev/null | grep '^/' -m1)
25
26 [ "$ARCH" ] || ARCH=$(uname -m)
27 case $ARCH in
28 x86_64)
29 # QEMU's own build system calls it qemu-system-x86_64
30 [ "$QEMU_BIN" ] || QEMU_BIN=$(which -a qemu-system-x86_64 2>/dev/null | grep '^/' -m1)
31 ;;
32 i*86)
33 # new i386 version of QEMU
34 [ "$QEMU_BIN" ] || QEMU_BIN=$(which -a qemu-system-i386 2>/dev/null | grep '^/' -m1)
35
36 # i386 version of QEMU
37 [ "$QEMU_BIN" ] || QEMU_BIN=$(which -a qemu 2>/dev/null | grep '^/' -m1)
38 ;;
39 esac
40
41 if [ ! -e "$QEMU_BIN" ]; then
42 echo "Could not find a suitable QEMU binary" >&2
43 return 1
44 fi
45 }
46
47 run_qemu() {
48 if [ -f /etc/machine-id ]; then
49 read MACHINE_ID < /etc/machine-id
50 [ -z "$INITRD" ] && [ -e "/boot/$MACHINE_ID/$KERNEL_VER/initrd" ] \
51 && INITRD="/boot/$MACHINE_ID/$KERNEL_VER/initrd"
52 [ -z "$KERNEL_BIN" ] && [ -e "/boot/$MACHINE_ID/$KERNEL_VER/linux" ] \
53 && KERNEL_BIN="/boot/$MACHINE_ID/$KERNEL_VER/linux"
54 fi
55
56 default_fedora_initrd=/boot/initramfs-${KERNEL_VER}.img
57 default_debian_initrd=/boot/initrd.img-${KERNEL_VER}
58 [ "$KERNEL_BIN" ] || KERNEL_BIN=/boot/vmlinuz-$KERNEL_VER
59 [ "$INITRD" ] || { [ -e "$default_fedora_initrd" ] && INITRD=$default_fedora_initrd; }
60 [ "$INITRD" ] || { [ "$LOOKS_LIKE_DEBIAN" ] && [ -e "$default_debian_initrd" ] && INITRD=$default_debian_initrd; }
61 [ "$QEMU_SMP" ] || QEMU_SMP=1
62
63 find_qemu_bin || return 1
64
65 KERNEL_APPEND="root=/dev/sda1 \
66 systemd.log_level=debug \
67 raid=noautodetect \
68 loglevel=2 \
69 init=$ROOTLIBDIR/systemd \
70 ro \
71 console=ttyS0 \
72 selinux=0 \
73 $KERNEL_APPEND \
74 "
75
76 QEMU_OPTIONS="-smp $QEMU_SMP \
77 -net none \
78 -m 512M \
79 -nographic \
80 -kernel $KERNEL_BIN \
81 "
82
83 if [[ "$INITRD" && "$SKIP_INITRD" != "yes" ]]; then
84 QEMU_OPTIONS="$QEMU_OPTIONS -initrd $INITRD"
85 fi
86
87 if [ -c /dev/kvm ]; then
88 QEMU_OPTIONS="$QEMU_OPTIONS -machine accel=kvm -enable-kvm -cpu host"
89 fi
90
91 if [[ "$QEMU_TIMEOUT" != "infinity" ]]; then
92 QEMU_BIN="timeout --foreground $QEMU_TIMEOUT $QEMU_BIN"
93 fi
94 ( set -x
95 $QEMU_BIN $QEMU_OPTIONS -append "$KERNEL_APPEND" $TESTDIR/rootdisk.img ) || return 1
96 }
97
98 run_nspawn() {
99 set -x
100 ../../systemd-nspawn --register=no --directory=$TESTDIR/nspawn-root $ROOTLIBDIR/systemd $KERNEL_APPEND
101 }
102
103 setup_basic_environment() {
104 # create the basic filesystem layout
105 setup_basic_dirs
106
107 install_systemd
108 install_missing_libraries
109 install_config_files
110 create_rc_local
111 install_basic_tools
112 install_libnss
113 install_pam
114 install_dbus
115 install_fonts
116 install_keymaps
117 install_terminfo
118 install_execs
119 install_fsck
120 install_plymouth
121 install_debug_tools
122 install_ld_so_conf
123 setup_selinux
124 strip_binaries
125 install_depmod_files
126 generate_module_dependencies
127 }
128
129 setup_selinux() {
130 # don't forget KERNEL_APPEND='... selinux=1 ...'
131 if [[ "$SETUP_SELINUX" != "yes" ]]; then
132 ddebug "Don't setup SELinux"
133 return 0
134 fi
135 ddebug "Setup SELinux"
136 local _conf_dir=/etc/selinux
137 local _fixfiles_tools="bash uname cat sort uniq awk grep egrep head expr find rm secon setfiles"
138
139 rm -rf $initdir/$_conf_dir
140 if ! cp -ar $_conf_dir $initdir/$_conf_dir; then
141 dfatal "Failed to copy $_conf_dir"
142 exit 1
143 fi
144
145 cat <<EOF >$initdir/etc/systemd/system/autorelabel.service
146 [Unit]
147 Description=Relabel all filesystems
148 DefaultDependencies=no
149 Requires=local-fs.target
150 Conflicts=shutdown.target
151 After=local-fs.target
152 Before=sysinit.target shutdown.target
153 ConditionSecurity=selinux
154 ConditionPathExists=|/.autorelabel
155
156 [Service]
157 ExecStart=/bin/sh -x -c 'echo 0 >/sys/fs/selinux/enforce && fixfiles -f -F relabel && rm /.autorelabel && systemctl --force reboot'
158 Type=oneshot
159 TimeoutSec=0
160 RemainAfterExit=yes
161 EOF
162
163 touch $initdir/.autorelabel
164 mkdir -p $initdir/etc/systemd/system/basic.target.wants
165 ln -fs autorelabel.service $initdir/etc/systemd/system/basic.target.wants/autorelabel.service
166
167 dracut_install $_fixfiles_tools
168 dracut_install fixfiles
169 dracut_install sestatus
170 }
171
172 install_valgrind() {
173 if ! type -p valgrind; then
174 dfatal "Failed to install valgrind"
175 exit 1
176 fi
177
178 local _valgrind_bins=$(strace -e execve valgrind /bin/true 2>&1 >/dev/null | perl -lne 'print $1 if /^execve\("([^"]+)"/')
179 dracut_install $_valgrind_bins
180
181 local _valgrind_libs=$(LD_DEBUG=files valgrind /bin/true 2>&1 >/dev/null | perl -lne 'print $1 if m{calling init: (/.*vgpreload_.*)}')
182 dracut_install $_valgrind_libs
183
184 local _valgrind_dbg_and_supp=$(
185 strace -e open valgrind /bin/true 2>&1 >/dev/null |
186 perl -lne 'if (my ($fname) = /^open\("([^"]+).*= (?!-)\d+/) { print $fname if $fname =~ /debug|\.supp$/ }'
187 )
188 dracut_install $_valgrind_dbg_and_supp
189 }
190
191 create_valgrind_wrapper() {
192 local _valgrind_wrapper=$initdir/$ROOTLIBDIR/systemd-under-valgrind
193 ddebug "Create $_valgrind_wrapper"
194 cat >$_valgrind_wrapper <<EOF
195 #!/bin/bash
196
197 exec valgrind --leak-check=full --log-file=/valgrind.out $ROOTLIBDIR/systemd "\$@"
198 EOF
199 chmod 0755 $_valgrind_wrapper
200 }
201
202 create_strace_wrapper() {
203 local _strace_wrapper=$initdir/$ROOTLIBDIR/systemd-under-strace
204 ddebug "Create $_strace_wrapper"
205 cat >$_strace_wrapper <<EOF
206 #!/bin/bash
207
208 exec strace -D -o /strace.out $ROOTLIBDIR/systemd "\$@"
209 EOF
210 chmod 0755 $_strace_wrapper
211 }
212
213 install_fsck() {
214 dracut_install /sbin/fsck*
215 dracut_install -o /bin/fsck*
216 }
217
218 install_dmevent() {
219 instmods dm_crypt =crypto
220 type -P dmeventd >/dev/null && dracut_install dmeventd
221 inst_libdir_file "libdevmapper-event.so*"
222 if [[ "$LOOKS_LIKE_DEBIAN" ]]; then
223 # dmsetup installs 55-dm and 60-persistent-storage-dm on Debian/Ubuntu
224 # see https://anonscm.debian.org/cgit/pkg-lvm/lvm2.git/tree/debian/patches/0007-udev.patch
225 inst_rules 55-dm.rules 60-persistent-storage-dm.rules
226 else
227 inst_rules 10-dm.rules 13-dm-disk.rules 95-dm-notify.rules
228 fi
229 }
230
231 install_systemd() {
232 # install compiled files
233 (cd $TEST_BASE_DIR/..; set -x; make DESTDIR=$initdir install)
234 # remove unneeded documentation
235 rm -fr $initdir/usr/share/{man,doc}
236 # we strip binaries since debug symbols increase binaries size a lot
237 # and it could fill the available space
238 strip_binaries
239 }
240
241 install_missing_libraries() {
242 # install possible missing libraries
243 for i in $initdir/{sbin,bin}/* $initdir/lib/systemd/*; do
244 inst_libs $i
245 done
246 }
247
248 create_empty_image() {
249 rm -f "$TESTDIR/rootdisk.img"
250 # Create the blank file to use as a root filesystem
251 dd if=/dev/null of="$TESTDIR/rootdisk.img" bs=1M seek=400
252 LOOPDEV=$(losetup --show -P -f $TESTDIR/rootdisk.img)
253 [ -b "$LOOPDEV" ] || return 1
254 echo "LOOPDEV=$LOOPDEV" >> $STATEFILE
255 sfdisk "$LOOPDEV" <<EOF
256 ,390M
257 ,
258 EOF
259
260 mkfs -t "$FSTYPE" -L systemd "${LOOPDEV}p1"
261 }
262
263 check_result_nspawn() {
264 ret=1
265 [[ -e $TESTDIR/nspawn-root/testok ]] && ret=0
266 [[ -f $TESTDIR/nspawn-root/failed ]] && cp -a $TESTDIR/nspawn-root/failed $TESTDIR
267 cp -a $TESTDIR/nspawn-root/var/log/journal $TESTDIR
268 [[ -f $TESTDIR/failed ]] && cat $TESTDIR/failed
269 ls -l $TESTDIR/journal/*/*.journal
270 test -s $TESTDIR/failed && ret=$(($ret+1))
271 return $ret
272 }
273
274 strip_binaries() {
275 if [[ "$STRIP_BINARIES" = "no" ]]; then
276 ddebug "Don't strip binaries"
277 return 0
278 fi
279 ddebug "Strip binaries"
280 find "$initdir" -executable -not -path '*/lib/modules/*.ko' -type f | xargs strip --strip-unneeded | ddebug
281 }
282
283 create_rc_local() {
284 mkdir -p $initdir/etc/rc.d
285 cat >$initdir/etc/rc.d/rc.local <<EOF
286 #!/bin/bash
287 exit 0
288 EOF
289 chmod 0755 $initdir/etc/rc.d/rc.local
290 }
291
292 install_execs() {
293 ddebug "install any Execs from the service files"
294 (
295 export PKG_CONFIG_PATH=$TEST_BASE_DIR/../src/core/
296 systemdsystemunitdir=$(pkg-config --variable=systemdsystemunitdir systemd)
297 systemduserunitdir=$(pkg-config --variable=systemduserunitdir systemd)
298 egrep -ho '^Exec[^ ]*=[^ ]+' $initdir/{$systemdsystemunitdir,$systemduserunitdir}/*.service \
299 | while read i; do
300 i=${i##Exec*=}; i=${i##-}
301 inst $i
302 done
303 )
304 }
305
306 generate_module_dependencies() {
307 if [[ -d $initdir/lib/modules/$KERNEL_VER ]] && \
308 ! depmod -a -b "$initdir" $KERNEL_VER; then
309 dfatal "\"depmod -a $KERNEL_VER\" failed."
310 exit 1
311 fi
312 }
313
314 install_depmod_files() {
315 inst /lib/modules/$KERNEL_VER/modules.order
316 inst /lib/modules/$KERNEL_VER/modules.builtin
317 }
318
319 install_plymouth() {
320 # install plymouth, if found... else remove plymouth service files
321 # if [ -x /usr/libexec/plymouth/plymouth-populate-initrd ]; then
322 # PLYMOUTH_POPULATE_SOURCE_FUNCTIONS="$TEST_BASE_DIR/test-functions" \
323 # /usr/libexec/plymouth/plymouth-populate-initrd -t $initdir
324 # dracut_install plymouth plymouthd
325 # else
326 rm -f $initdir/{usr/lib,etc}/systemd/system/plymouth* $initdir/{usr/lib,etc}/systemd/system/*/plymouth*
327 # fi
328 }
329
330 install_ld_so_conf() {
331 cp -a /etc/ld.so.conf* $initdir/etc
332 ldconfig -r "$initdir"
333 }
334
335 install_config_files() {
336 inst /etc/sysconfig/init
337 inst /etc/passwd
338 inst /etc/shadow
339 inst /etc/login.defs
340 inst /etc/group
341 inst /etc/shells
342 inst /etc/nsswitch.conf
343 inst /etc/pam.conf
344 inst /etc/securetty
345 inst /etc/os-release
346 inst /etc/localtime
347 # we want an empty environment
348 > $initdir/etc/environment
349 > $initdir/etc/machine-id
350 # set the hostname
351 echo systemd-testsuite > $initdir/etc/hostname
352 # fstab
353 cat >$initdir/etc/fstab <<EOF
354 LABEL=systemd / ext3 rw 0 1
355 EOF
356 }
357
358 install_basic_tools() {
359 [[ $BASICTOOLS ]] && dracut_install $BASICTOOLS
360 dracut_install -o sushell
361 # in Debian ldconfig is just a shell script wrapper around ldconfig.real
362 dracut_install -o ldconfig.real
363 }
364
365 install_debug_tools() {
366 [[ $DEBUGTOOLS ]] && dracut_install $DEBUGTOOLS
367 }
368
369 install_libnss() {
370 # install libnss_files for login
371 NSS_LIBS=$(LD_DEBUG=files getent passwd 2>&1 >/dev/null |sed -n '/calling init: .*libnss_/ {s!^.* /!/!; p}')
372 dracut_install $NSS_LIBS
373 }
374
375 install_dbus() {
376 inst $ROOTLIBDIR/system/dbus.socket
377 inst $ROOTLIBDIR/system/dbus.service
378
379 find \
380 /etc/dbus-1 /usr/share/dbus-1 -xtype f \
381 | while read file; do
382 inst $file
383 done
384 }
385
386 install_pam() {
387 (
388 [[ "$LOOKS_LIKE_DEBIAN" ]] && type -p dpkg-architecture &>/dev/null && find "/lib/$(dpkg-architecture -qDEB_HOST_MULTIARCH)/security" -xtype f
389 find \
390 /etc/pam.d \
391 /etc/security \
392 /lib64/security \
393 /lib/security -xtype f \
394 ) | while read file; do
395 inst $file
396 done
397
398 # pam_unix depends on unix_chkpwd.
399 # see http://www.linux-pam.org/Linux-PAM-html/sag-pam_unix.html
400 dracut_install -o unix_chkpwd
401
402 [[ "$LOOKS_LIKE_DEBIAN" ]] &&
403 cp /etc/pam.d/systemd-user $initdir/etc/pam.d/
404 }
405
406 install_keymaps() {
407 for i in \
408 /usr/lib/kbd/keymaps/include/* \
409 /usr/lib/kbd/keymaps/i386/include/* \
410 /usr/lib/kbd/keymaps/i386/qwerty/us.*; do
411 [[ -f $i ]] || continue
412 inst $i
413 done
414 }
415
416 install_fonts() {
417 for i in \
418 /usr/lib/kbd/consolefonts/eurlatgr* \
419 /usr/lib/kbd/consolefonts/latarcyrheb-sun16*; do
420 [[ -f $i ]] || continue
421 inst $i
422 done
423 }
424
425 install_terminfo() {
426 for _terminfodir in /lib/terminfo /etc/terminfo /usr/share/terminfo; do
427 [ -f ${_terminfodir}/l/linux ] && break
428 done
429 dracut_install -o ${_terminfodir}/l/linux
430 }
431
432 setup_testsuite() {
433 cp $TEST_BASE_DIR/testsuite.target $initdir/etc/systemd/system/
434 cp $TEST_BASE_DIR/end.service $initdir/etc/systemd/system/
435
436 mkdir -p $initdir/etc/systemd/system/testsuite.target.wants
437 ln -fs $TEST_BASE_DIR/testsuite.service $initdir/etc/systemd/system/testsuite.target.wants/testsuite.service
438 ln -fs $TEST_BASE_DIR/end.service $initdir/etc/systemd/system/testsuite.target.wants/end.service
439
440 # make the testsuite the default target
441 ln -fs testsuite.target $initdir/etc/systemd/system/default.target
442 }
443
444 setup_nspawn_root() {
445 rm -fr $TESTDIR/nspawn-root
446 ddebug "cp -ar $initdir $TESTDIR/nspawn-root"
447 cp -ar $initdir $TESTDIR/nspawn-root
448 # we don't mount in the nspawn root
449 rm -f $TESTDIR/nspawn-root/etc/fstab
450 }
451
452 setup_basic_dirs() {
453 mkdir -p $initdir/run
454 mkdir -p $initdir/etc/systemd/system
455 mkdir -p $initdir/var/log/journal
456
457 for d in usr/bin usr/sbin bin etc lib "$libdir" sbin tmp usr var var/log dev proc sys sysroot root run run/lock run/initramfs; do
458 if [ -L "/$d" ]; then
459 inst_symlink "/$d"
460 else
461 inst_dir "/$d"
462 fi
463 done
464
465 ln -sfn /run "$initdir/var/run"
466 ln -sfn /run/lock "$initdir/var/lock"
467 }
468
469 inst_libs() {
470 local _bin=$1
471 local _so_regex='([^ ]*/lib[^/]*/[^ ]*\.so[^ ]*)'
472 local _file _line
473
474 LC_ALL=C ldd "$_bin" 2>/dev/null | while read _line; do
475 [[ $_line = 'not a dynamic executable' ]] && break
476
477 if [[ $_line =~ $_so_regex ]]; then
478 _file=${BASH_REMATCH[1]}
479 [[ -e ${initdir}/$_file ]] && continue
480 inst_library "$_file"
481 continue
482 fi
483
484 if [[ $_line =~ not\ found ]]; then
485 dfatal "Missing a shared library required by $_bin."
486 dfatal "Run \"ldd $_bin\" to find out what it is."
487 dfatal "$_line"
488 dfatal "dracut cannot create an initrd."
489 exit 1
490 fi
491 done
492 }
493
494 import_testdir() {
495 STATEFILE=".testdir"
496 [[ -e $STATEFILE ]] && . $STATEFILE
497 if [[ -z "$TESTDIR" ]] || [[ ! -d "$TESTDIR" ]]; then
498 TESTDIR=$(mktemp --tmpdir=/var/tmp -d -t systemd-test.XXXXXX)
499 echo "TESTDIR=\"$TESTDIR\"" > $STATEFILE
500 export TESTDIR
501 fi
502 }
503
504 import_initdir() {
505 initdir=$TESTDIR/root
506 export initdir
507 }
508
509 ## @brief Converts numeric logging level to the first letter of level name.
510 #
511 # @param lvl Numeric logging level in range from 1 to 6.
512 # @retval 1 if @a lvl is out of range.
513 # @retval 0 if @a lvl is correct.
514 # @result Echoes first letter of level name.
515 _lvl2char() {
516 case "$1" in
517 1) echo F;;
518 2) echo E;;
519 3) echo W;;
520 4) echo I;;
521 5) echo D;;
522 6) echo T;;
523 *) return 1;;
524 esac
525 }
526
527 ## @brief Internal helper function for _do_dlog()
528 #
529 # @param lvl Numeric logging level.
530 # @param msg Message.
531 # @retval 0 It's always returned, even if logging failed.
532 #
533 # @note This function is not supposed to be called manually. Please use
534 # dtrace(), ddebug(), or others instead which wrap this one.
535 #
536 # This function calls _do_dlog() either with parameter msg, or if
537 # none is given, it will read standard input and will use every line as
538 # a message.
539 #
540 # This enables:
541 # dwarn "This is a warning"
542 # echo "This is a warning" | dwarn
543 LOG_LEVEL=4
544
545 dlog() {
546 [ -z "$LOG_LEVEL" ] && return 0
547 [ $1 -le $LOG_LEVEL ] || return 0
548 local lvl="$1"; shift
549 local lvlc=$(_lvl2char "$lvl") || return 0
550
551 if [ $# -ge 1 ]; then
552 echo "$lvlc: $*"
553 else
554 while read line; do
555 echo "$lvlc: " "$line"
556 done
557 fi
558 }
559
560 ## @brief Logs message at TRACE level (6)
561 #
562 # @param msg Message.
563 # @retval 0 It's always returned, even if logging failed.
564 dtrace() {
565 set +x
566 dlog 6 "$@"
567 [ -n "$debug" ] && set -x || :
568 }
569
570 ## @brief Logs message at DEBUG level (5)
571 #
572 # @param msg Message.
573 # @retval 0 It's always returned, even if logging failed.
574 ddebug() {
575 # set +x
576 dlog 5 "$@"
577 # [ -n "$debug" ] && set -x || :
578 }
579
580 ## @brief Logs message at INFO level (4)
581 #
582 # @param msg Message.
583 # @retval 0 It's always returned, even if logging failed.
584 dinfo() {
585 set +x
586 dlog 4 "$@"
587 [ -n "$debug" ] && set -x || :
588 }
589
590 ## @brief Logs message at WARN level (3)
591 #
592 # @param msg Message.
593 # @retval 0 It's always returned, even if logging failed.
594 dwarn() {
595 set +x
596 dlog 3 "$@"
597 [ -n "$debug" ] && set -x || :
598 }
599
600 ## @brief Logs message at ERROR level (2)
601 #
602 # @param msg Message.
603 # @retval 0 It's always returned, even if logging failed.
604 derror() {
605 # set +x
606 dlog 2 "$@"
607 # [ -n "$debug" ] && set -x || :
608 }
609
610 ## @brief Logs message at FATAL level (1)
611 #
612 # @param msg Message.
613 # @retval 0 It's always returned, even if logging failed.
614 dfatal() {
615 set +x
616 dlog 1 "$@"
617 [ -n "$debug" ] && set -x || :
618 }
619
620
621 # Generic substring function. If $2 is in $1, return 0.
622 strstr() { [ "${1#*$2*}" != "$1" ]; }
623
624 # normalize_path <path>
625 # Prints the normalized path, where it removes any duplicated
626 # and trailing slashes.
627 # Example:
628 # $ normalize_path ///test/test//
629 # /test/test
630 normalize_path() {
631 shopt -q -s extglob
632 set -- "${1//+(\/)//}"
633 shopt -q -u extglob
634 echo "${1%/}"
635 }
636
637 # convert_abs_rel <from> <to>
638 # Prints the relative path, when creating a symlink to <to> from <from>.
639 # Example:
640 # $ convert_abs_rel /usr/bin/test /bin/test-2
641 # ../../bin/test-2
642 # $ ln -s $(convert_abs_rel /usr/bin/test /bin/test-2) /usr/bin/test
643 convert_abs_rel() {
644 local __current __absolute __abssize __cursize __newpath
645 local -i __i __level
646
647 set -- "$(normalize_path "$1")" "$(normalize_path "$2")"
648
649 # corner case #1 - self looping link
650 [[ "$1" == "$2" ]] && { echo "${1##*/}"; return; }
651
652 # corner case #2 - own dir link
653 [[ "${1%/*}" == "$2" ]] && { echo "."; return; }
654
655 IFS="/" __current=($1)
656 IFS="/" __absolute=($2)
657
658 __abssize=${#__absolute[@]}
659 __cursize=${#__current[@]}
660
661 while [[ ${__absolute[__level]} == ${__current[__level]} ]]
662 do
663 (( __level++ ))
664 if (( __level > __abssize || __level > __cursize ))
665 then
666 break
667 fi
668 done
669
670 for ((__i = __level; __i < __cursize-1; __i++))
671 do
672 if ((__i > __level))
673 then
674 __newpath=$__newpath"/"
675 fi
676 __newpath=$__newpath".."
677 done
678
679 for ((__i = __level; __i < __abssize; __i++))
680 do
681 if [[ -n $__newpath ]]
682 then
683 __newpath=$__newpath"/"
684 fi
685 __newpath=$__newpath${__absolute[__i]}
686 done
687
688 echo "$__newpath"
689 }
690
691
692 # Install a directory, keeping symlinks as on the original system.
693 # Example: if /lib points to /lib64 on the host, "inst_dir /lib/file"
694 # will create ${initdir}/lib64, ${initdir}/lib64/file,
695 # and a symlink ${initdir}/lib -> lib64.
696 inst_dir() {
697 [[ -e ${initdir}/"$1" ]] && return 0 # already there
698
699 local _dir="$1" _part="${1%/*}" _file
700 while [[ "$_part" != "${_part%/*}" ]] && ! [[ -e "${initdir}/${_part}" ]]; do
701 _dir="$_part $_dir"
702 _part=${_part%/*}
703 done
704
705 # iterate over parent directories
706 for _file in $_dir; do
707 [[ -e "${initdir}/$_file" ]] && continue
708 if [[ -L $_file ]]; then
709 inst_symlink "$_file"
710 else
711 # create directory
712 mkdir -m 0755 -p "${initdir}/$_file" || return 1
713 [[ -e "$_file" ]] && chmod --reference="$_file" "${initdir}/$_file"
714 chmod u+w "${initdir}/$_file"
715 fi
716 done
717 }
718
719 # $1 = file to copy to ramdisk
720 # $2 (optional) Name for the file on the ramdisk
721 # Location of the image dir is assumed to be $initdir
722 # We never overwrite the target if it exists.
723 inst_simple() {
724 [[ -f "$1" ]] || return 1
725 strstr "$1" "/" || return 1
726
727 local _src=$1 target="${2:-$1}"
728 if ! [[ -d ${initdir}/$target ]]; then
729 [[ -e ${initdir}/$target ]] && return 0
730 [[ -L ${initdir}/$target ]] && return 0
731 [[ -d "${initdir}/${target%/*}" ]] || inst_dir "${target%/*}"
732 fi
733 # install checksum files also
734 if [[ -e "${_src%/*}/.${_src##*/}.hmac" ]]; then
735 inst "${_src%/*}/.${_src##*/}.hmac" "${target%/*}/.${target##*/}.hmac"
736 fi
737 ddebug "Installing $_src"
738 cp --sparse=always -pfL "$_src" "${initdir}/$target"
739 }
740
741 # find symlinks linked to given library file
742 # $1 = library file
743 # Function searches for symlinks by stripping version numbers appended to
744 # library filename, checks if it points to the same target and finally
745 # prints the list of symlinks to stdout.
746 #
747 # Example:
748 # rev_lib_symlinks libfoo.so.8.1
749 # output: libfoo.so.8 libfoo.so
750 # (Only if libfoo.so.8 and libfoo.so exists on host system.)
751 rev_lib_symlinks() {
752 [[ ! $1 ]] && return 0
753
754 local fn="$1" orig="$(readlink -f "$1")" links=''
755
756 [[ ${fn} =~ .*\.so\..* ]] || return 1
757
758 until [[ ${fn##*.} == so ]]; do
759 fn="${fn%.*}"
760 [[ -L ${fn} && $(readlink -f "${fn}") == ${orig} ]] && links+=" ${fn}"
761 done
762
763 echo "${links}"
764 }
765
766 # Same as above, but specialized to handle dynamic libraries.
767 # It handles making symlinks according to how the original library
768 # is referenced.
769 inst_library() {
770 local _src="$1" _dest=${2:-$1} _lib _reallib _symlink
771 strstr "$1" "/" || return 1
772 [[ -e $initdir/$_dest ]] && return 0
773 if [[ -L $_src ]]; then
774 # install checksum files also
775 if [[ -e "${_src%/*}/.${_src##*/}.hmac" ]]; then
776 inst "${_src%/*}/.${_src##*/}.hmac" "${_dest%/*}/.${_dest##*/}.hmac"
777 fi
778 _reallib=$(readlink -f "$_src")
779 inst_simple "$_reallib" "$_reallib"
780 inst_dir "${_dest%/*}"
781 [[ -d "${_dest%/*}" ]] && _dest=$(readlink -f "${_dest%/*}")/${_dest##*/}
782 ln -sfn $(convert_abs_rel "${_dest}" "${_reallib}") "${initdir}/${_dest}"
783 else
784 inst_simple "$_src" "$_dest"
785 fi
786
787 # Create additional symlinks. See rev_symlinks description.
788 for _symlink in $(rev_lib_symlinks $_src) $(rev_lib_symlinks $_reallib); do
789 [[ ! -e $initdir/$_symlink ]] && {
790 ddebug "Creating extra symlink: $_symlink"
791 inst_symlink $_symlink
792 }
793 done
794 }
795
796 # find a binary. If we were not passed the full path directly,
797 # search in the usual places to find the binary.
798 find_binary() {
799 if [[ -z ${1##/*} ]]; then
800 if [[ -x $1 ]] || { strstr "$1" ".so" && ldd $1 &>/dev/null; }; then
801 echo $1
802 return 0
803 fi
804 fi
805
806 type -P $1
807 }
808
809 # Same as above, but specialized to install binary executables.
810 # Install binary executable, and all shared library dependencies, if any.
811 inst_binary() {
812 local _bin _target
813 _bin=$(find_binary "$1") || return 1
814 _target=${2:-$_bin}
815 [[ -e $initdir/$_target ]] && return 0
816 [[ -L $_bin ]] && inst_symlink $_bin $_target && return 0
817 local _file _line
818 local _so_regex='([^ ]*/lib[^/]*/[^ ]*\.so[^ ]*)'
819 # I love bash!
820 LC_ALL=C ldd "$_bin" 2>/dev/null | while read _line; do
821 [[ $_line = 'not a dynamic executable' ]] && break
822
823 if [[ $_line =~ $_so_regex ]]; then
824 _file=${BASH_REMATCH[1]}
825 [[ -e ${initdir}/$_file ]] && continue
826 inst_library "$_file"
827 continue
828 fi
829
830 if [[ $_line =~ not\ found ]]; then
831 dfatal "Missing a shared library required by $_bin."
832 dfatal "Run \"ldd $_bin\" to find out what it is."
833 dfatal "$_line"
834 dfatal "dracut cannot create an initrd."
835 exit 1
836 fi
837 done
838 inst_simple "$_bin" "$_target"
839 }
840
841 # same as above, except for shell scripts.
842 # If your shell script does not start with shebang, it is not a shell script.
843 inst_script() {
844 local _bin
845 _bin=$(find_binary "$1") || return 1
846 shift
847 local _line _shebang_regex
848 read -r -n 80 _line <"$_bin"
849 # If debug is set, clean unprintable chars to prevent messing up the term
850 [[ $debug ]] && _line=$(echo -n "$_line" | tr -c -d '[:print:][:space:]')
851 _shebang_regex='(#! *)(/[^ ]+).*'
852 [[ $_line =~ $_shebang_regex ]] || return 1
853 inst "${BASH_REMATCH[2]}" && inst_simple "$_bin" "$@"
854 }
855
856 # same as above, but specialized for symlinks
857 inst_symlink() {
858 local _src=$1 _target=${2:-$1} _realsrc
859 strstr "$1" "/" || return 1
860 [[ -L $1 ]] || return 1
861 [[ -L $initdir/$_target ]] && return 0
862 _realsrc=$(readlink -f "$_src")
863 if ! [[ -e $initdir/$_realsrc ]]; then
864 if [[ -d $_realsrc ]]; then
865 inst_dir "$_realsrc"
866 else
867 inst "$_realsrc"
868 fi
869 fi
870 [[ ! -e $initdir/${_target%/*} ]] && inst_dir "${_target%/*}"
871 [[ -d ${_target%/*} ]] && _target=$(readlink -f ${_target%/*})/${_target##*/}
872 ln -sfn $(convert_abs_rel "${_target}" "${_realsrc}") "$initdir/$_target"
873 }
874
875 # attempt to install any programs specified in a udev rule
876 inst_rule_programs() {
877 local _prog _bin
878
879 if grep -qE 'PROGRAM==?"[^ "]+' "$1"; then
880 for _prog in $(grep -E 'PROGRAM==?"[^ "]+' "$1" | sed -r 's/.*PROGRAM==?"([^ "]+).*/\1/'); do
881 if [ -x /lib/udev/$_prog ]; then
882 _bin=/lib/udev/$_prog
883 else
884 _bin=$(find_binary "$_prog") || {
885 dinfo "Skipping program $_prog using in udev rule $(basename $1) as it cannot be found"
886 continue;
887 }
888 fi
889
890 #dinfo "Installing $_bin due to it's use in the udev rule $(basename $1)"
891 dracut_install "$_bin"
892 done
893 fi
894 }
895
896 # udev rules always get installed in the same place, so
897 # create a function to install them to make life simpler.
898 inst_rules() {
899 local _target=/etc/udev/rules.d _rule _found
900
901 inst_dir "/lib/udev/rules.d"
902 inst_dir "$_target"
903 for _rule in "$@"; do
904 if [ "${rule#/}" = "$rule" ]; then
905 for r in /lib/udev/rules.d /etc/udev/rules.d; do
906 if [[ -f $r/$_rule ]]; then
907 _found="$r/$_rule"
908 inst_simple "$_found"
909 inst_rule_programs "$_found"
910 fi
911 done
912 fi
913 for r in '' ./ $dracutbasedir/rules.d/; do
914 if [[ -f ${r}$_rule ]]; then
915 _found="${r}$_rule"
916 inst_simple "$_found" "$_target/${_found##*/}"
917 inst_rule_programs "$_found"
918 fi
919 done
920 [[ $_found ]] || dinfo "Skipping udev rule: $_rule"
921 done
922 }
923
924 # general purpose installation function
925 # Same args as above.
926 inst() {
927 local _x
928
929 case $# in
930 1) ;;
931 2) [[ ! $initdir && -d $2 ]] && export initdir=$2
932 [[ $initdir = $2 ]] && set $1;;
933 3) [[ -z $initdir ]] && export initdir=$2
934 set $1 $3;;
935 *) dfatal "inst only takes 1 or 2 or 3 arguments"
936 exit 1;;
937 esac
938 for _x in inst_symlink inst_script inst_binary inst_simple; do
939 $_x "$@" && return 0
940 done
941 return 1
942 }
943
944 # install any of listed files
945 #
946 # If first argument is '-d' and second some destination path, first accessible
947 # source is installed into this path, otherwise it will installed in the same
948 # path as source. If none of listed files was installed, function return 1.
949 # On first successful installation it returns with 0 status.
950 #
951 # Example:
952 #
953 # inst_any -d /bin/foo /bin/bar /bin/baz
954 #
955 # Lets assume that /bin/baz exists, so it will be installed as /bin/foo in
956 # initramfs.
957 inst_any() {
958 local to f
959
960 [[ $1 = '-d' ]] && to="$2" && shift 2
961
962 for f in "$@"; do
963 if [[ -e $f ]]; then
964 [[ $to ]] && inst "$f" "$to" && return 0
965 inst "$f" && return 0
966 fi
967 done
968
969 return 1
970 }
971
972 # dracut_install [-o ] <file> [<file> ... ]
973 # Install <file> to the initramfs image
974 # -o optionally install the <file> and don't fail, if it is not there
975 dracut_install() {
976 local _optional=no
977 if [[ $1 = '-o' ]]; then
978 _optional=yes
979 shift
980 fi
981 while (($# > 0)); do
982 if ! inst "$1" ; then
983 if [[ $_optional = yes ]]; then
984 dinfo "Skipping program $1 as it cannot be found and is" \
985 "flagged to be optional"
986 else
987 dfatal "Failed to install $1"
988 exit 1
989 fi
990 fi
991 shift
992 done
993 }
994
995 # Install a single kernel module along with any firmware it may require.
996 # $1 = full path to kernel module to install
997 install_kmod_with_fw() {
998 # no need to go further if the module is already installed
999
1000 [[ -e "${initdir}/lib/modules/$KERNEL_VER/${1##*/lib/modules/$KERNEL_VER/}" ]] \
1001 && return 0
1002
1003 [[ -e "$initdir/.kernelmodseen/${1##*/}" ]] && return 0
1004
1005 if [[ $omit_drivers ]]; then
1006 local _kmod=${1##*/}
1007 _kmod=${_kmod%.ko}
1008 _kmod=${_kmod/-/_}
1009 if [[ "$_kmod" =~ $omit_drivers ]]; then
1010 dinfo "Omitting driver $_kmod"
1011 return 1
1012 fi
1013 if [[ "${1##*/lib/modules/$KERNEL_VER/}" =~ $omit_drivers ]]; then
1014 dinfo "Omitting driver $_kmod"
1015 return 1
1016 fi
1017 fi
1018
1019 [ -d "$initdir/.kernelmodseen" ] && \
1020 > "$initdir/.kernelmodseen/${1##*/}"
1021
1022 inst_simple "$1" "/lib/modules/$KERNEL_VER/${1##*/lib/modules/$KERNEL_VER/}" \
1023 || return $?
1024
1025 local _modname=${1##*/} _fwdir _found _fw
1026 _modname=${_modname%.ko*}
1027 for _fw in $(modinfo -k $KERNEL_VER -F firmware $1 2>/dev/null); do
1028 _found=''
1029 for _fwdir in $fw_dir; do
1030 if [[ -d $_fwdir && -f $_fwdir/$_fw ]]; then
1031 inst_simple "$_fwdir/$_fw" "/lib/firmware/$_fw"
1032 _found=yes
1033 fi
1034 done
1035 if [[ $_found != yes ]]; then
1036 if ! grep -qe "\<${_modname//-/_}\>" /proc/modules; then
1037 dinfo "Possible missing firmware \"${_fw}\" for kernel module" \
1038 "\"${_modname}.ko\""
1039 else
1040 dwarn "Possible missing firmware \"${_fw}\" for kernel module" \
1041 "\"${_modname}.ko\""
1042 fi
1043 fi
1044 done
1045 return 0
1046 }
1047
1048 # Do something with all the dependencies of a kernel module.
1049 # Note that kernel modules depend on themselves using the technique we use
1050 # $1 = function to call for each dependency we find
1051 # It will be passed the full path to the found kernel module
1052 # $2 = module to get dependencies for
1053 # rest of args = arguments to modprobe
1054 # _fderr specifies FD passed from surrounding scope
1055 for_each_kmod_dep() {
1056 local _func=$1 _kmod=$2 _cmd _modpath _options _found=0
1057 shift 2
1058 modprobe "$@" --ignore-install --show-depends $_kmod 2>&${_fderr} | (
1059 while read _cmd _modpath _options; do
1060 [[ $_cmd = insmod ]] || continue
1061 $_func ${_modpath} || exit $?
1062 _found=1
1063 done
1064 [[ $_found -eq 0 ]] && exit 1
1065 exit 0
1066 )
1067 }
1068
1069 # filter kernel modules to install certain modules that meet specific
1070 # requirements.
1071 # $1 = search only in subdirectory of /kernel/$1
1072 # $2 = function to call with module name to filter.
1073 # This function will be passed the full path to the module to test.
1074 # The behavior of this function can vary depending on whether $hostonly is set.
1075 # If it is, we will only look at modules that are already in memory.
1076 # If it is not, we will look at all kernel modules
1077 # This function returns the full filenames of modules that match $1
1078 filter_kernel_modules_by_path () (
1079 local _modname _filtercmd
1080 if ! [[ $hostonly ]]; then
1081 _filtercmd='find "$KERNEL_MODS/kernel/$1" "$KERNEL_MODS/extra"'
1082 _filtercmd+=' "$KERNEL_MODS/weak-updates" -name "*.ko" -o -name "*.ko.gz"'
1083 _filtercmd+=' -o -name "*.ko.xz"'
1084 _filtercmd+=' 2>/dev/null'
1085 else
1086 _filtercmd='cut -d " " -f 1 </proc/modules|xargs modinfo -F filename '
1087 _filtercmd+='-k $KERNEL_VER 2>/dev/null'
1088 fi
1089 for _modname in $(eval $_filtercmd); do
1090 case $_modname in
1091 *.ko) "$2" "$_modname" && echo "$_modname";;
1092 *.ko.gz) gzip -dc "$_modname" > $initdir/$$.ko
1093 $2 $initdir/$$.ko && echo "$_modname"
1094 rm -f $initdir/$$.ko
1095 ;;
1096 *.ko.xz) xz -dc "$_modname" > $initdir/$$.ko
1097 $2 $initdir/$$.ko && echo "$_modname"
1098 rm -f $initdir/$$.ko
1099 ;;
1100 esac
1101 done
1102 )
1103 find_kernel_modules_by_path () (
1104 if ! [[ $hostonly ]]; then
1105 find "$KERNEL_MODS/kernel/$1" "$KERNEL_MODS/extra" "$KERNEL_MODS/weak-updates" \
1106 -name "*.ko" -o -name "*.ko.gz" -o -name "*.ko.xz" 2>/dev/null
1107 else
1108 cut -d " " -f 1 </proc/modules \
1109 | xargs modinfo -F filename -k $KERNEL_VER 2>/dev/null
1110 fi
1111 )
1112
1113 filter_kernel_modules () {
1114 filter_kernel_modules_by_path drivers "$1"
1115 }
1116
1117 find_kernel_modules () {
1118 find_kernel_modules_by_path drivers
1119 }
1120
1121 # instmods [-c] <kernel module> [<kernel module> ... ]
1122 # instmods [-c] <kernel subsystem>
1123 # install kernel modules along with all their dependencies.
1124 # <kernel subsystem> can be e.g. "=block" or "=drivers/usb/storage"
1125 instmods() {
1126 [[ $no_kernel = yes ]] && return
1127 # called [sub]functions inherit _fderr
1128 local _fderr=9
1129 local _check=no
1130 if [[ $1 = '-c' ]]; then
1131 _check=yes
1132 shift
1133 fi
1134
1135 function inst1mod() {
1136 local _ret=0 _mod="$1"
1137 case $_mod in
1138 =*)
1139 if [ -f $KERNEL_MODS/modules.${_mod#=} ]; then
1140 ( [[ "$_mpargs" ]] && echo $_mpargs
1141 cat "${KERNEL_MODS}/modules.${_mod#=}" ) \
1142 | instmods
1143 else
1144 ( [[ "$_mpargs" ]] && echo $_mpargs
1145 find "$KERNEL_MODS" -path "*/${_mod#=}/*" -printf '%f\n' ) \
1146 | instmods
1147 fi
1148 ;;
1149 --*) _mpargs+=" $_mod" ;;
1150 i2o_scsi) return ;; # Do not load this diagnostic-only module
1151 *)
1152 _mod=${_mod##*/}
1153 # if we are already installed, skip this module and go on
1154 # to the next one.
1155 [[ -f "$initdir/.kernelmodseen/${_mod%.ko}.ko" ]] && return
1156
1157 if [[ $omit_drivers ]] && [[ "$1" =~ $omit_drivers ]]; then
1158 dinfo "Omitting driver ${_mod##$KERNEL_MODS}"
1159 return
1160 fi
1161 # If we are building a host-specific initramfs and this
1162 # module is not already loaded, move on to the next one.
1163 [[ $hostonly ]] && ! grep -qe "\<${_mod//-/_}\>" /proc/modules \
1164 && ! echo $add_drivers | grep -qe "\<${_mod}\>" \
1165 && return
1166
1167 # We use '-d' option in modprobe only if modules prefix path
1168 # differs from default '/'. This allows us to use Dracut with
1169 # old version of modprobe which doesn't have '-d' option.
1170 local _moddirname=${KERNEL_MODS%%/lib/modules/*}
1171 [[ -n ${_moddirname} ]] && _moddirname="-d ${_moddirname}/"
1172
1173 # ok, load the module, all its dependencies, and any firmware
1174 # it may require
1175 for_each_kmod_dep install_kmod_with_fw $_mod \
1176 --set-version $KERNEL_VER ${_moddirname} $_mpargs
1177 ((_ret+=$?))
1178 ;;
1179 esac
1180 return $_ret
1181 }
1182
1183 function instmods_1() {
1184 local _mod _mpargs
1185 if (($# == 0)); then # filenames from stdin
1186 while read _mod; do
1187 inst1mod "${_mod%.ko*}" || {
1188 if [ "$_check" = "yes" ]; then
1189 dfatal "Failed to install $_mod"
1190 return 1
1191 fi
1192 }
1193 done
1194 fi
1195 while (($# > 0)); do # filenames as arguments
1196 inst1mod ${1%.ko*} || {
1197 if [ "$_check" = "yes" ]; then
1198 dfatal "Failed to install $1"
1199 return 1
1200 fi
1201 }
1202 shift
1203 done
1204 return 0
1205 }
1206
1207 local _ret _filter_not_found='FATAL: Module .* not found.'
1208 set -o pipefail
1209 # Capture all stderr from modprobe to _fderr. We could use {var}>...
1210 # redirections, but that would make dracut require bash4 at least.
1211 eval "( instmods_1 \"\$@\" ) ${_fderr}>&1" \
1212 | while read line; do [[ "$line" =~ $_filter_not_found ]] && echo $line || echo $line >&2 ;done | derror
1213 _ret=$?
1214 set +o pipefail
1215 return $_ret
1216 }
1217
1218 # inst_libdir_file [-n <pattern>] <file> [<file>...]
1219 # Install a <file> located on a lib directory to the initramfs image
1220 # -n <pattern> install non-matching files
1221 inst_libdir_file() {
1222 if [[ "$1" == "-n" ]]; then
1223 local _pattern=$1
1224 shift 2
1225 for _dir in $libdirs; do
1226 for _i in "$@"; do
1227 for _f in "$_dir"/$_i; do
1228 [[ "$_i" =~ $_pattern ]] || continue
1229 [[ -e "$_i" ]] && dracut_install "$_i"
1230 done
1231 done
1232 done
1233 else
1234 for _dir in $libdirs; do
1235 for _i in "$@"; do
1236 for _f in "$_dir"/$_i; do
1237 [[ -e "$_f" ]] && dracut_install "$_f"
1238 done
1239 done
1240 done
1241 fi
1242 }
1243
1244 check_nspawn() {
1245 [[ -d /run/systemd/system ]]
1246 }
1247
1248
1249 do_test() {
1250 if [[ $UID != "0" ]]; then
1251 echo "TEST: $TEST_DESCRIPTION [SKIPPED]: not root" >&2
1252 exit 0
1253 fi
1254
1255 # Detect lib paths
1256 [[ $libdir ]] || for libdir in /lib64 /lib; do
1257 [[ -d $libdir ]] && libdirs+=" $libdir" && break
1258 done
1259
1260 [[ $usrlibdir ]] || for usrlibdir in /usr/lib64 /usr/lib; do
1261 [[ -d $usrlibdir ]] && libdirs+=" $usrlibdir" && break
1262 done
1263
1264 import_testdir
1265 import_initdir
1266
1267 while (($# > 0)); do
1268 case $1 in
1269 --run)
1270 echo "TEST RUN: $TEST_DESCRIPTION"
1271 test_run
1272 ret=$?
1273 if [ $ret -eq 0 ]; then
1274 echo "TEST RUN: $TEST_DESCRIPTION [OK]"
1275 else
1276 echo "TEST RUN: $TEST_DESCRIPTION [FAILED]"
1277 fi
1278 exit $ret;;
1279 --setup)
1280 echo "TEST SETUP: $TEST_DESCRIPTION"
1281 test_setup
1282 exit $?;;
1283 --clean)
1284 echo "TEST CLEANUP: $TEST_DESCRIPTION"
1285 test_cleanup
1286 rm -fr "$TESTDIR"
1287 rm -f .testdir
1288 exit $?;;
1289 --all)
1290 echo -n "TEST: $TEST_DESCRIPTION ";
1291 (
1292 test_setup && test_run
1293 ret=$?
1294 test_cleanup
1295 rm -fr "$TESTDIR"
1296 rm -f .testdir
1297 exit $ret
1298 ) </dev/null >test.log 2>&1
1299 ret=$?
1300 if [ $ret -eq 0 ]; then
1301 rm test.log
1302 echo "[OK]"
1303 else
1304 echo "[FAILED]"
1305 echo "see $(pwd)/test.log"
1306 fi
1307 exit $ret;;
1308 *) break ;;
1309 esac
1310 shift
1311 done
1312 }