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