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