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