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