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