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