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