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
7 os_release
=$
(test -e /etc
/os-release
&& echo /etc
/os-release ||
echo /usr
/lib
/os-release
)
8 LOOKS_LIKE_DEBIAN
=$
(source $os_release && [[ "$ID" = "debian" ||
" $ID_LIKE " = *" debian "* ]] && echo yes ||
:)
9 LOOKS_LIKE_ARCH
=$
(source $os_release && [[ "$ID" = "arch" ||
" $ID_LIKE " = *" arch "* ]] && echo yes ||
:)
10 LOOKS_LIKE_SUSE
=$
(source $os_release && [[ " $ID_LIKE " = *" suse "* ]] && echo yes ||
:)
11 KERNEL_VER
=${KERNEL_VER-$(uname -r)}
12 KERNEL_MODS
="/lib/modules/$KERNEL_VER/"
13 QEMU_TIMEOUT
="${QEMU_TIMEOUT:-infinity}"
14 NSPAWN_TIMEOUT
="${NSPAWN_TIMEOUT:-infinity}"
15 TIMED_OUT
= # will be 1 after run_* if *_TIMEOUT is set and test timed out
16 [[ "$LOOKS_LIKE_SUSE" ]] && FSTYPE
="${FSTYPE:-btrfs}" || FSTYPE
="${FSTYPE:-ext4}"
17 UNIFIED_CGROUP_HIERARCHY
="${UNIFIED_CGROUP_HIERARCHY:-default}"
18 EFI_MOUNT
="${EFI_MOUNT:-$(bootctl -x 2>/dev/null || echo /boot)}"
19 QEMU_MEM
="${QEMU_MEM:-512M}"
20 IMAGE_NAME
=${IMAGE_NAME:-default}
21 TEST_REQUIRE_INSTALL_TESTS
="${TEST_REQUIRE_INSTALL_TESTS:-1}"
22 TEST_PARALLELIZE
="${TEST_PARALLELIZE:-0}"
25 # Decide if we can (and want to) run QEMU with KVM acceleration.
26 # Check if nested KVM is explicitly enabled (TEST_NESTED_KVM). If not,
27 # check if it's not explicitly disabled (TEST_NO_KVM) and we're not already
28 # running under KVM. If these conditions are met, enable KVM (and possibly
29 # nested KVM), otherwise disable it.
30 if [[ -n "$TEST_NESTED_KVM" ||
( -z "$TEST_NO_KVM" && $
(systemd-detect-virt
-v) != kvm
) ]]; then
36 if ! ROOTLIBDIR
=$
(pkg-config
--variable=systemdutildir systemd
); then
37 echo "WARNING! Cannot determine rootlibdir from pkg-config, assuming /usr/lib/systemd" >&2
38 ROOTLIBDIR
=/usr
/lib
/systemd
41 PATH_TO_INIT
=$ROOTLIBDIR/systemd
42 [ "$SYSTEMD_JOURNALD" ] || SYSTEMD_JOURNALD
=$
(which -a $BUILD_DIR/systemd-journald
$ROOTLIBDIR/systemd-journald
2>/dev
/null |
grep '^/' -m1)
43 [ "$SYSTEMD_JOURNAL_REMOTE" ] || SYSTEMD_JOURNAL_REMOTE
=$
(which -a $BUILD_DIR/systemd-journal-remote
$ROOTLIBDIR/systemd-journal-remote
2>/dev
/null |
grep '^/' -m1)
44 [ "$SYSTEMD" ] || SYSTEMD
=$
(which -a $BUILD_DIR/systemd
$ROOTLIBDIR/systemd
2>/dev
/null |
grep '^/' -m1)
45 [ "$SYSTEMD_NSPAWN" ] || SYSTEMD_NSPAWN
=$
(which -a $BUILD_DIR/systemd-nspawn systemd-nspawn
2>/dev
/null |
grep '^/' -m1)
46 [ "$JOURNALCTL" ] || JOURNALCTL
=$
(which -a $BUILD_DIR/journalctl journalctl
2>/dev
/null |
grep '^/' -m1)
146 STATEDIR
="${BUILD_DIR:-.}/test/$(basename $(dirname $(realpath $0)))"
147 STATEFILE
="$STATEDIR/.testdir"
148 IMAGESTATEDIR
="$STATEDIR/.."
149 TESTLOG
="$STATEDIR/test.log"
151 is_built_with_asan
() {
152 if ! type -P objdump
>/dev
/null
; then
153 ddebug
"Failed to find objdump. Assuming systemd hasn't been built with ASAN."
157 # Borrowed from https://github.com/google/oss-fuzz/blob/cd9acd02f9d3f6e80011cc1e9549be526ce5f270/infra/base-images/base-runner/bad_build_check#L182
158 local _asan_calls
=$
(objdump
-dC $SYSTEMD_JOURNALD |
egrep "callq\s+[0-9a-f]+\s+<__asan" -c)
159 if (( $_asan_calls < 1000 )); then
166 IS_BUILT_WITH_ASAN
=$
(is_built_with_asan
&& echo yes ||
echo no
)
168 if [[ "$IS_BUILT_WITH_ASAN" = "yes" ]]; then
170 SKIP_INITRD
="${SKIP_INITRD:-yes}"
171 PATH_TO_INIT
=$ROOTLIBDIR/systemd-under-asan
175 # We need to correctly distinguish between gcc's and clang's ASan DSOs.
176 if ldd
$SYSTEMD |
grep -q libasan.so
; then
178 elif ldd
$SYSTEMD |
grep -q libclang_rt.asan
; then
181 # As clang's ASan DSO is usually in a non-standard path, let's check if
182 # the environment is set accordingly. If not, warn the user and exit.
183 # We're not setting the LD_LIBRARY_PATH automagically here, because
184 # user should encounter (and fix) the same issue when running the unit
186 if ldd
"$SYSTEMD" |
grep -q "libclang_rt.asan.*not found"; then
187 _asan_rt_name
="$(ldd $SYSTEMD | awk '/libclang_rt.asan/ {print $1; exit}')"
188 _asan_rt_path
="$(find /usr/lib* /usr/local/lib* -type f -name "$_asan_rt_name" 2>/dev/null | sed 1q)"
189 echo >&2 "clang's ASan DSO ($_asan_rt_name) is not present in the runtime library path"
190 echo >&2 "Consider setting LD_LIBRARY_PATH=${_asan_rt_path%/*}"
194 echo >&2 "systemd is not linked against the ASan DSO"
195 echo >&2 "gcc does this by default, for clang compile with -shared-libasan"
200 function find_qemu_bin
() {
201 # SUSE and Red Hat call the binary qemu-kvm. Debian and Gentoo call it kvm.
202 if [[ $QEMU_KVM == "yes" ]]; then
203 [ "$QEMU_BIN" ] || QEMU_BIN
=$
(which -a kvm qemu-kvm
2>/dev
/null |
grep '^/' -m1)
206 [ "$ARCH" ] || ARCH
=$
(uname
-m)
209 # QEMU's own build system calls it qemu-system-x86_64
210 [ "$QEMU_BIN" ] || QEMU_BIN
=$
(which -a qemu-system-x86_64
2>/dev
/null |
grep '^/' -m1)
213 # new i386 version of QEMU
214 [ "$QEMU_BIN" ] || QEMU_BIN
=$
(which -a qemu-system-i386
2>/dev
/null |
grep '^/' -m1)
216 # i386 version of QEMU
217 [ "$QEMU_BIN" ] || QEMU_BIN
=$
(which -a qemu
2>/dev
/null |
grep '^/' -m1)
220 [ "$QEMU_BIN" ] || QEMU_BIN
=$
(which -a qemu-system-ppc64
2>/dev
/null |
grep '^/' -m1)
224 if [ ! -e "$QEMU_BIN" ]; then
225 echo "Could not find a suitable QEMU binary" >&2
230 # Return 0 if QEMU did run (then you must check the result state/logs for actual
231 # success), or 1 if QEMU is not available.
233 if [ -f /etc
/machine-id
]; then
234 read MACHINE_ID
< /etc
/machine-id
235 [ -z "$INITRD" ] && [ -e "$EFI_MOUNT/$MACHINE_ID/$KERNEL_VER/initrd" ] \
236 && INITRD
="$EFI_MOUNT/$MACHINE_ID/$KERNEL_VER/initrd"
237 [ -z "$KERNEL_BIN" ] && [ -e "$EFI_MOUNT/$MACHINE_ID/$KERNEL_VER/linux" ] \
238 && KERNEL_BIN
="$EFI_MOUNT/$MACHINE_ID/$KERNEL_VER/linux"
243 rm -f "$initdir"/{testok
,failed
,skipped
}
244 # make sure the initdir is not mounted to avoid concurrent access
248 if [[ ! "$KERNEL_BIN" ]]; then
249 if [[ "$LOOKS_LIKE_ARCH" ]]; then
250 KERNEL_BIN
=/boot
/vmlinuz-linux
252 [ "$ARCH" ] || ARCH
=$
(uname
-m)
255 KERNEL_BIN
=/boot
/vmlinux-
$KERNEL_VER
259 KERNEL_BIN
=/boot
/vmlinuz-
$KERNEL_VER
265 default_fedora_initrd
=/boot
/initramfs-
${KERNEL_VER}.img
266 default_debian_initrd
=/boot
/initrd.img-
${KERNEL_VER}
267 default_arch_initrd
=/boot
/initramfs-linux-fallback.img
268 default_suse_initrd
=/boot
/initrd-
${KERNEL_VER}
269 if [[ ! "$INITRD" ]]; then
270 if [[ -e "$default_fedora_initrd" ]]; then
271 INITRD
="$default_fedora_initrd"
272 elif [[ "$LOOKS_LIKE_DEBIAN" && -e "$default_debian_initrd" ]]; then
273 INITRD
="$default_debian_initrd"
274 elif [[ "$LOOKS_LIKE_ARCH" && -e "$default_arch_initrd" ]]; then
275 INITRD
="$default_arch_initrd"
276 elif [[ "$LOOKS_LIKE_SUSE" && -e "$default_suse_initrd" ]]; then
277 INITRD
="$default_suse_initrd"
281 # If QEMU_SMP was not explicitly set, try to determine the value 'dynamically'
282 # i.e. use the number of online CPUs on the host machine. If the nproc utility
283 # is not installed or there's some other error when calling it, fall back
284 # to the original value (QEMU_SMP=1).
285 if ! [ "$QEMU_SMP" ]; then
286 if ! QEMU_SMP
=$
(nproc
); then
287 dwarn
"nproc utility is not installed, falling back to QEMU_SMP=1"
292 find_qemu_bin ||
return 1
294 # Umount initdir to avoid concurrent access to the filesystem
298 if [[ "$UNIFIED_CGROUP_HIERARCHY" = "yes" ]]; then
299 _cgroup_args
="systemd.unified_cgroup_hierarchy=yes"
300 elif [[ "$UNIFIED_CGROUP_HIERARCHY" = "no" ]]; then
301 _cgroup_args
="systemd.unified_cgroup_hierarchy=no systemd.legacy_systemd_cgroup_controller=yes"
302 elif [[ "$UNIFIED_CGROUP_HIERARCHY" = "hybrid" ]]; then
303 _cgroup_args
="systemd.unified_cgroup_hierarchy=no systemd.legacy_systemd_cgroup_controller=no"
304 elif [[ "$UNIFIED_CGROUP_HIERARCHY" != "default" ]]; then
305 dfatal
"Unknown UNIFIED_CGROUP_HIERARCHY. Got $UNIFIED_CGROUP_HIERARCHY, expected [yes|no|hybrid|default]"
309 if [[ "$LOOKS_LIKE_SUSE" ]]; then
310 PARAMS
+="rd.hostonly=0"
314 if [[ ! "$INTERACTIVE_DEBUG" ]]; then
315 _end
="systemd.wants=end.service"
320 KERNEL_APPEND
="$PARAMS \
330 SYSTEMD_UNIT_PATH=/usr/lib/systemd/tests/testdata/testsuite-$1.units:/usr/lib/systemd/tests/testdata/units: \
331 systemd.unit=testsuite.target \
332 systemd.wants=testsuite-$1.service ${_end} \
336 [ -e "$IMAGE_PRIVATE" ] && image
="$IMAGE_PRIVATE" || image
="$IMAGE_PUBLIC"
337 QEMU_OPTIONS
="-smp $QEMU_SMP \
341 -kernel $KERNEL_BIN \
342 -drive format=raw,cache=unsafe,file=$image \
346 if [[ "$INITRD" && "$SKIP_INITRD" != "yes" ]]; then
347 QEMU_OPTIONS
="$QEMU_OPTIONS -initrd $INITRD"
350 # Let's use KVM if possible
351 if [[ -c /dev
/kvm
&& $QEMU_KVM == "yes" ]]; then
352 QEMU_OPTIONS
="$QEMU_OPTIONS -machine accel=kvm -enable-kvm -cpu host"
355 if [[ "$QEMU_TIMEOUT" != "infinity" ]]; then
356 QEMU_BIN
="timeout --foreground $QEMU_TIMEOUT $QEMU_BIN"
358 (set -x; $QEMU_BIN $QEMU_OPTIONS -append "$KERNEL_APPEND")
360 if [ "$rc" = 124 ] && [ "$QEMU_TIMEOUT" != "infinity" ]; then
361 derror
"test timed out after $QEMU_TIMEOUT s"
364 [ "$rc" != 0 ] && derror
"QEMU failed with exit code $rc"
369 # Return 0 if nspawn did run (then you must check the result state/logs for actual
370 # success), or 1 if nspawn is not available.
372 [[ -d /run
/systemd
/system
]] ||
return 1
373 rm -f "$initdir"/{testok
,failed
,skipped
}
377 --kill-signal=SIGKILL
379 --setenv=SYSTEMD_UNIT_PATH
=/usr
/lib
/systemd
/tests
/testdata
/testsuite-
$2.units
:/usr
/lib
/systemd
/tests
/testdata
/units
:
382 systemd.unit
=testsuite.target
383 systemd.wants
=testsuite-
$2.service
386 if [[ ! "$INTERACTIVE_DEBUG" ]]; then
387 _nspawn_cmd
+=( systemd.wants
=end.service
)
391 if [[ "$NSPAWN_TIMEOUT" != "infinity" ]]; then
392 _nspawn_pre
=(timeout
--foreground $NSPAWN_TIMEOUT)
397 if [[ "$UNIFIED_CGROUP_HIERARCHY" = "hybrid" ]]; then
398 dwarn
"nspawn doesn't support SYSTEMD_NSPAWN_UNIFIED_HIERARCHY=hybrid, skipping"
400 elif [[ "$UNIFIED_CGROUP_HIERARCHY" = "yes" ||
"$UNIFIED_CGROUP_HIERARCHY" = "no" ]]; then
401 _nspawn_pre
=("${_nspawn_pre[@]}" env SYSTEMD_NSPAWN_UNIFIED_HIERARCHY
=$UNIFIED_CGROUP_HIERARCHY)
402 elif [[ "$UNIFIED_CGROUP_HIERARCHY" = "default" ]]; then
403 _nspawn_pre
=("${_nspawn_pre[@]}" env
--unset=UNIFIED_CGROUP_HIERARCHY
--unset=SYSTEMD_NSPAWN_UNIFIED_HIERARCHY
)
405 dfatal
"Unknown UNIFIED_CGROUP_HIERARCHY. Got $UNIFIED_CGROUP_HIERARCHY, expected [yes|no|hybrid|default]"
409 (set -x; "${_nspawn_pre[@]}" "$SYSTEMD_NSPAWN" $NSPAWN_ARGUMENTS "${_nspawn_cmd[@]}")
411 if [ "$rc" = 124 ] && [ "$NSPAWN_TIMEOUT" != "infinity" ]; then
412 derror
"test timed out after $NSPAWN_TIMEOUT s"
415 [ "$rc" != 0 ] && derror
"nspawn failed with exit code $rc"
420 setup_basic_environment
() {
421 # create the basic filesystem layout
425 install_missing_libraries
442 has_user_dbus_socket
&& install_user_dbus
446 generate_module_dependencies
447 if [[ "$IS_BUILT_WITH_ASAN" = "yes" ]]; then
453 # don't forget KERNEL_APPEND='... selinux=1 ...'
454 if [[ "$SETUP_SELINUX" != "yes" ]]; then
455 ddebug
"Don't setup SELinux"
458 ddebug
"Setup SELinux"
459 local _conf_dir
=/etc
/selinux
460 local _fixfiles_tools
="bash uname cat sort uniq awk grep egrep head expr find rm secon setfiles"
462 rm -rf $initdir/$_conf_dir
463 if ! cp -ar $_conf_dir $initdir/$_conf_dir; then
464 dfatal
"Failed to copy $_conf_dir"
468 touch $initdir/.autorelabel
469 mkdir
-p $initdir/usr
/lib
/systemd
/tests
/testdata
/units
/basic.target.wants
470 ln -sf ..
/autorelabel.service
$initdir/usr
/lib
/systemd
/tests
/testdata
/units
/basic.target.wants
/
472 dracut_install
$_fixfiles_tools
473 dracut_install fixfiles
474 dracut_install sestatus
478 if ! type -p valgrind
; then
479 dfatal
"Failed to install valgrind"
483 local _valgrind_bins
=$
(strace
-e execve valgrind
/bin
/true
2>&1 >/dev
/null | perl
-lne 'print $1 if /^execve\("([^"]+)"/')
484 dracut_install
$_valgrind_bins
486 local _valgrind_libs
=$
(LD_DEBUG
=files valgrind
/bin
/true
2>&1 >/dev
/null | perl
-lne 'print $1 if m{calling init: (/.*vgpreload_.*)}')
487 dracut_install
$_valgrind_libs
489 local _valgrind_dbg_and_supp
=$
(
490 strace
-e open valgrind
/bin
/true
2>&1 >/dev
/null |
491 perl
-lne 'if (my ($fname) = /^open\("([^"]+).*= (?!-)\d+/) { print $fname if $fname =~ /debug|\.supp$/ }'
493 dracut_install
$_valgrind_dbg_and_supp
496 create_valgrind_wrapper
() {
497 local _valgrind_wrapper
=$initdir/$ROOTLIBDIR/systemd-under-valgrind
498 ddebug
"Create $_valgrind_wrapper"
499 cat >$_valgrind_wrapper <<EOF
502 mount -t proc proc /proc
503 exec valgrind --leak-check=full --log-file=/valgrind.out $ROOTLIBDIR/systemd "\$@"
505 chmod 0755 $_valgrind_wrapper
508 create_asan_wrapper
() {
509 local _asan_wrapper
=$initdir/$ROOTLIBDIR/systemd-under-asan
510 local _asan_rt_pattern
511 ddebug
"Create $_asan_wrapper"
513 case "$ASAN_COMPILER" in
515 _asan_rt_pattern
="*libasan*"
518 _asan_rt_pattern
="libclang_rt.asan-*"
519 # Install llvm-symbolizer to generate useful reports
520 # See: https://clang.llvm.org/docs/AddressSanitizer.html#symbolizing-the-reports
521 dracut_install
"llvm-symbolizer"
524 dfail
"Unsupported compiler: $ASAN_COMPILER"
528 cat >$_asan_wrapper <<EOF
533 DEFAULT_ASAN_OPTIONS=${ASAN_OPTIONS:-strict_string_checks=1:detect_stack_use_after_return=1:check_initialization_order=1:strict_init_order=1}
534 DEFAULT_UBSAN_OPTIONS=${UBSAN_OPTIONS:-print_stacktrace=1:print_summary=1:halt_on_error=1}
535 DEFAULT_ENVIRONMENT="ASAN_OPTIONS=\$DEFAULT_ASAN_OPTIONS UBSAN_OPTIONS=\$DEFAULT_UBSAN_OPTIONS"
537 # As right now bash is the PID 1, we can't expect PATH to have a sane value.
538 # Let's make one to prevent unexpected "<bin> not found" issues in the future
539 export PATH="/sbin:/bin:/usr/sbin:/usr/bin"
541 mount -t proc proc /proc
542 mount -t sysfs sysfs /sys
543 mount -o remount,rw /
545 PATH_TO_ASAN=\$(find / -name '$_asan_rt_pattern' | sed 1q)
546 if [[ "\$PATH_TO_ASAN" ]]; then
547 # A lot of services (most notably dbus) won't start without preloading libasan
548 # See https://github.com/systemd/systemd/issues/5004
549 DEFAULT_ENVIRONMENT="\$DEFAULT_ENVIRONMENT LD_PRELOAD=\$PATH_TO_ASAN"
550 # Let's add the ASan DSO's path to the dynamic linker's cache. This is pretty
551 # unnecessary for gcc & libasan, however, for clang this is crucial, as its
552 # runtime ASan DSO is in a non-standard (library) path.
553 echo \${PATH_TO_ASAN%/*} > /etc/ld.so.conf.d/asan-path-override.conf
556 echo DefaultEnvironment=\$DEFAULT_ENVIRONMENT >>/etc/systemd/system.conf
557 echo DefaultTimeoutStartSec=180s >>/etc/systemd/system.conf
558 echo DefaultStandardOutput=journal+console >>/etc/systemd/system.conf
560 # ASAN and syscall filters aren't compatible with each other.
561 find / -name '*.service' -type f | xargs sed -i 's/^\\(MemoryDeny\\|SystemCall\\)/#\\1/'
563 # The redirection of ASAN reports to a file prevents them from ending up in /dev/null.
564 # But, apparently, sometimes it doesn't work: https://github.com/google/sanitizers/issues/886.
565 JOURNALD_CONF_DIR=/etc/systemd/system/systemd-journald.service.d
566 mkdir -p "\$JOURNALD_CONF_DIR"
567 printf "[Service]\nEnvironment=ASAN_OPTIONS=\$DEFAULT_ASAN_OPTIONS:log_path=/systemd-journald.asan.log UBSAN_OPTIONS=\$DEFAULT_UBSAN_OPTIONS:log_path=/systemd-journald.ubsan.log\n" >"\$JOURNALD_CONF_DIR/env.conf"
569 # Sometimes UBSan sends its reports to stderr regardless of what is specified in log_path
570 # Let's try to catch them by redirecting stderr (and stdout just in case) to a file
571 # See https://github.com/systemd/systemd/pull/12524#issuecomment-491108821
572 printf "[Service]\nStandardOutput=file:/systemd-journald.out\n" >"\$JOURNALD_CONF_DIR/out.conf"
574 # 90s isn't enough for some services to finish when literally everything is run
575 # under ASan+UBSan in containers, which, in turn, are run in VMs.
576 # Let's limit which environments such services should be executed in.
577 mkdir -p /etc/systemd/system/systemd-hwdb-update.service.d
578 printf "[Unit]\nConditionVirtualization=container\n\n[Service]\nTimeoutSec=240s\n" >/etc/systemd/system/systemd-hwdb-update.service.d/env-override.conf
580 # Let's override another hard-coded timeout that kicks in too early
581 mkdir -p /etc/systemd/system/systemd-journal-flush.service.d
582 printf "[Service]\nTimeoutSec=180s\n" >/etc/systemd/system/systemd-journal-flush.service.d/timeout.conf
584 # The 'mount' utility doesn't behave well under libasan, causing unexpected
585 # fails during boot and subsequent test results check:
586 # bash-5.0# mount -o remount,rw -v /
587 # mount: /dev/sda1 mounted on /.
590 # Let's workaround this by clearing the previously set LD_PRELOAD env variable,
591 # so the libasan library is not loaded for this particular service
593 local _dropin_dir="/etc/systemd/system/\$1.service.d"
594 mkdir -p "\$_dropin_dir"
595 printf "[Service]\nUnsetEnvironment=LD_PRELOAD\n" >"\$_dropin_dir/unset_ld_preload.conf"
598 unset_ld_preload systemd-remount-fs
599 unset_ld_preload testsuite-
601 export ASAN_OPTIONS=\$DEFAULT_ASAN_OPTIONS:log_path=/systemd.asan.log UBSAN_OPTIONS=\$DEFAULT_UBSAN_OPTIONS
602 exec $ROOTLIBDIR/systemd "\$@"
605 chmod 0755 $_asan_wrapper
608 create_strace_wrapper
() {
609 local _strace_wrapper
=$initdir/$ROOTLIBDIR/systemd-under-strace
610 ddebug
"Create $_strace_wrapper"
611 cat >$_strace_wrapper <<EOF
614 exec strace -D -o /strace.out $ROOTLIBDIR/systemd "\$@"
616 chmod 0755 $_strace_wrapper
620 dracut_install
/sbin
/fsck
*
621 dracut_install
-o /bin
/fsck
*
623 # fskc.reiserfs calls reiserfsck. so, install it
624 dracut_install
-o reiserfsck
628 instmods dm_crypt
=crypto
630 if [[ "$LOOKS_LIKE_DEBIAN" ]]; then
631 # dmsetup installs 55-dm and 60-persistent-storage-dm on Debian/Ubuntu
632 # and since buster/bionic 95-dm-notify.rules
633 # see https://gitlab.com/debian-lvm/lvm2/blob/master/debian/patches/udev.patch
634 inst_rules
55-dm.rules
60-persistent-storage-dm.rules
95-dm-notify.rules
636 inst_rules
10-dm.rules
13-dm-disk.rules
95-dm-notify.rules
638 if [[ "$LOOKS_LIKE_SUSE" ]]; then
639 inst_rules
60-persistent-storage.rules
61-persistent-storage-compat.rules
99-systemd.rules
644 ddebug
"Install compiled systemd"
646 local _ninja_bin
=$
(type -P ninja ||
type -P ninja-build
)
647 if [[ -z "$_ninja_bin" ]]; then
648 dfatal
"ninja was not found"
651 (set -x; DESTDIR
=$initdir "$_ninja_bin" -C $BUILD_DIR install)
652 # remove unneeded documentation
653 rm -fr $initdir/usr
/share
/{man
,doc
}
655 [[ "$LOOKS_LIKE_SUSE" ]] && setup_suse
657 # enable debug logging in PID1
658 echo LogLevel
=debug
>> $initdir/etc
/systemd
/system.conf
659 # store coredumps in journal
660 echo Storage
=journal
>> $initdir/etc
/systemd
/coredump.conf
665 local rpath
=$
(objdump
-p "$_bin" 2>/dev
/null |
awk "/R(UN)?PATH/ { print \"$initdir\" \$2 }" |
paste -sd :)
667 if [ -z "$rpath" ] ; then
674 install_missing_libraries
() {
675 # install possible missing libraries
676 for i
in $initdir{,/usr
}/{sbin
,bin
}/* $initdir{,/usr
}/lib
/systemd
/{,tests
/{,manual
/,unsafe
/}}*; do
677 LD_LIBRARY_PATH
="${LD_LIBRARY_PATH:+$LD_LIBRARY_PATH:}$(get_ldpath $i):$(get_ldpath $i)/src/udev" inst_libs
$i
682 if [ -n "${LOOPDEV}" ]; then
683 ddebug
"losetup -d $LOOPDEV"
684 losetup
-d "${LOOPDEV}"
689 trap cleanup_loopdev EXIT INT QUIT PIPE
691 create_empty_image
() {
692 if [ -z "$IMAGE_NAME" ]; then
693 echo "create_empty_image: \$IMAGE_NAME not set"
698 if [[ "$STRIP_BINARIES" = "no" ]]; then
702 echo "Setting up $IMAGE_PUBLIC (${_size} MB)"
703 rm -f "$IMAGE_PRIVATE" "$IMAGE_PUBLIC"
705 # Create the blank file to use as a root filesystem
706 truncate
-s "${_size}M" "$IMAGE_PUBLIC"
708 LOOPDEV
=$
(losetup
--show -P -f "$IMAGE_PUBLIC")
709 [ -b "$LOOPDEV" ] ||
return 1
710 sfdisk
"$LOOPDEV" <<EOF
717 local _label
="-L systemd.${name}"
718 # mkfs.reiserfs doesn't know -L. so, use --label instead
719 [[ "$FSTYPE" == "reiserfs" ]] && _label
="--label systemd.${name}"
720 mkfs
-t "${FSTYPE}" ${_label} "${LOOPDEV}p1" -q; ret
=$?
721 if [ $ret -ne 0 ] ; then
722 dfatal
"Failed to mkfs -t ${FSTYPE}"
728 if [ -z "${LOOPDEV}" ]; then
729 [ -e "$IMAGE_PRIVATE" ] && image
="$IMAGE_PRIVATE" || image
="$IMAGE_PUBLIC"
730 LOOPDEV
=$
(losetup
--show -P -f "$image")
731 [ -b "$LOOPDEV" ] ||
return 1
736 if ! mountpoint
-q $initdir; then
738 mount
${LOOPDEV}p1
$initdir
739 TEST_SETUP_CLEANUP_ROOTDIR
=1
744 # only umount if create_empty_image_rootdir() was called to mount it
745 [[ -z $TEST_SETUP_CLEANUP_ROOTDIR ]] || _umount_dir
$initdir
749 # unmount the loopback device from all places. Otherwise we risk file
751 for device
in $
(losetup
-l |
awk '$6=="'"$IMAGE_PUBLIC"'" {print $1}'); do
752 ddebug
"Unmounting all uses of $device"
753 mount |
awk '/^'"${device}"'p/{print $1}' |
xargs --no-run-if-empty umount
-v
757 create_empty_image_rootdir
() {
762 check_asan_reports
() {
766 if [[ "$IS_BUILT_WITH_ASAN" = "yes" ]]; then
768 if [[ -e "$root/systemd.asan.log.1" ]]; then
769 cat "$root/systemd.asan.log.1"
773 journald_report
=$
(find "$root" -name "systemd-journald.*san.log*" -exec cat {} \
;)
774 if [[ ! -z "$journald_report" ]]; then
775 printf "%s\n" "$journald_report"
776 cat "$root/systemd-journald.out" ||
:
781 "$JOURNALCTL" -D "$root/var/log/journal" | perl
-alne '
783 %services_to_ignore = (
784 "dbus-daemon" => undef,
787 print $2 if /\s(\S*)\[(\d+)\]:\s*SUMMARY:\s+\w+Sanitizer/ && !exists $services_to_ignore{$1}'
789 if [[ ! -z "$pids" ]]; then
792 "$JOURNALCTL" -D "$root/var/log/journal" _PID
=$pid --no-pager
801 if [ -n "${ARTIFACT_DIRECTORY}" ]; then
802 dest
="${ARTIFACT_DIRECTORY}/${testname}.journal"
804 dest
="$TESTDIR/system.journal"
808 $SYSTEMD_JOURNAL_REMOTE \
810 --getter="$JOURNALCTL -o export -D $j"
812 if [ -n "${TEST_SHOW_JOURNAL}" ]; then
814 $JOURNALCTL --no-pager -o short-monotonic
--no-hostname --priority=${TEST_SHOW_JOURNAL} -D $j
820 # we want to print this sometime later, so save this in a variable
821 JOURNAL_LIST
="$(ls -l $dest*)"
824 check_result_nspawn
() {
826 local journald_report
=""
828 [[ -e $1/testok
]] && ret
=0
829 [[ -f $1/failed
]] && cp -a $1/failed
$TESTDIR
830 save_journal
$1/var
/log
/journal
831 [[ -f $TESTDIR/failed
]] && cat $TESTDIR/failed
833 test -s $TESTDIR/failed
&& ret
=$
(($ret+1))
834 [ -n "$TIMED_OUT" ] && ret
=$
(($ret+1))
835 check_asan_reports
"$1" || ret
=$
(($ret+1))
840 # can be overridden in specific test
841 check_result_qemu
() {
844 [[ -e $initdir/testok
]] && ret
=0
845 [[ -f $initdir/failed
]] && cp -a $initdir/failed
$TESTDIR
846 save_journal
$initdir/var
/log
/journal
847 check_asan_reports
"$initdir" || ret
=$
(($ret+1))
849 [[ -f $TESTDIR/failed
]] && cat $TESTDIR/failed
851 test -s $TESTDIR/failed
&& ret
=$
(($ret+1))
852 [ -n "$TIMED_OUT" ] && ret
=$
(($ret+1))
857 if [[ "$STRIP_BINARIES" = "no" ]]; then
858 ddebug
"Don't strip binaries"
861 ddebug
"Strip binaries"
862 find "$initdir" -executable -not -path '*/lib/modules/*.ko' -type f | \
863 xargs strip
--strip-unneeded |
& \
864 grep -vi 'file format not recognized' | \
869 mkdir
-p $initdir/etc
/rc.d
870 cat >$initdir/etc
/rc.d
/rc.
local <<EOF
874 chmod 0755 $initdir/etc
/rc.d
/rc.
local
878 ddebug
"install any Execs from the service files"
880 export PKG_CONFIG_PATH
=$BUILD_DIR/src
/core
/
881 systemdsystemunitdir
=$
(pkg-config
--variable=systemdsystemunitdir systemd
)
882 systemduserunitdir
=$
(pkg-config
--variable=systemduserunitdir systemd
)
883 sed -r -n 's|^Exec[a-zA-Z]*=[@+!-]*([^ ]+).*|\1|gp' $initdir/{$systemdsystemunitdir,$systemduserunitdir}/*.service \
884 |
sort -u |
while read i
; do
885 # some {rc,halt}.local scripts and programs are okay to not exist, the rest should
886 # also, plymouth is pulled in by rescue.service, but even there the exit code
887 # is ignored; as it's not present on some distros, don't fail if it doesn't exist
888 dinfo
"Attempting to install $i"
889 inst
$i ||
[ "${i%.local}" != "$i" ] || [ "${i%systemd-update-done}" != "$i" ] || [ "${i##*/}" == "plymouth" ]
894 generate_module_dependencies
() {
895 if [[ -d $initdir/lib
/modules
/$KERNEL_VER ]] && \
896 ! depmod
-a -b "$initdir" $KERNEL_VER; then
897 dfatal
"\"depmod -a $KERNEL_VER\" failed."
902 install_depmod_files
() {
903 inst
/lib
/modules
/$KERNEL_VER/modules.order
904 inst
/lib
/modules
/$KERNEL_VER/modules.
builtin
908 # install plymouth, if found... else remove plymouth service files
909 # if [ -x /usr/libexec/plymouth/plymouth-populate-initrd ]; then
910 # PLYMOUTH_POPULATE_SOURCE_FUNCTIONS="$TEST_BASE_DIR/test-functions" \
911 # /usr/libexec/plymouth/plymouth-populate-initrd -t $initdir
912 # dracut_install plymouth plymouthd
914 rm -f $initdir/{usr
/lib
,lib
,etc
}/systemd
/system
/plymouth
* $initdir/{usr
/lib
,lib
,etc
}/systemd
/system
/*/plymouth
*
918 install_ld_so_conf
() {
919 cp -a /etc
/ld.so.conf
* $initdir/etc
920 ldconfig
-r "$initdir"
924 # create unprivileged user for user manager tests
925 mkdir
-p $initdir/etc
/sysusers.d
926 cat >$initdir/etc
/sysusers.d
/testuser.conf
<<EOF
927 u testuser 4711 "Test User" /home/testuser
930 mkdir
-p $initdir/home
/testuser
-m 0700
931 chown
4711:4711 $initdir/home
/testuser
934 install_config_files
() {
935 inst
/etc
/sysconfig
/init ||
:
938 inst_any
/etc
/login.defs
/usr
/etc
/login.defs
941 inst_any
/etc
/nsswitch.conf
/usr
/etc
/nsswitch.conf
942 inst
/etc
/pam.conf ||
:
943 inst_any
/etc
/os-release
/usr
/lib
/os-release
945 # we want an empty environment
946 > $initdir/etc
/environment
947 > $initdir/etc
/machine-id
950 echo systemd-testsuite
> $initdir/etc
/hostname
952 # let's set up just one image with the traditional verbose output
953 if [ ${IMAGE_NAME} != "basic" ]; then
954 mkdir
-p $initdir/etc
/systemd
/system.conf.d
955 echo -e '[Manager]\nStatusUnitFormat=name' >$initdir/etc
/systemd
/system.conf.d
/status.conf
959 install_basic_tools
() {
960 dracut_install
"${BASICTOOLS[@]}"
961 dracut_install
-o sushell
962 # in Debian ldconfig is just a shell script wrapper around ldconfig.real
963 dracut_install
-o ldconfig.real
966 install_debug_tools
() {
967 dracut_install
"${DEBUGTOOLS[@]}"
969 if [[ $INTERACTIVE_DEBUG ]]; then
970 # Set default TERM from vt220 to linux, so at least basic key shortcuts work
971 local _getty_override
="$initdir/etc/systemd/system/serial-getty@.service.d"
972 mkdir
-p "$_getty_override"
973 echo -e "[Service]\nEnvironment=TERM=linux" > "$_getty_override/default-TERM.conf"
975 cat > "$initdir/etc/motd" << EOF
976 To adjust the terminal size use:
986 # install libnss_files for login
987 NSS_LIBS
=$
(LD_DEBUG
=files getent passwd
2>&1 >/dev
/null |
sed -n '/calling init: .*libnss_/ {s!^.* /!/!; p}')
988 dracut_install
$NSS_LIBS
992 inst
$ROOTLIBDIR/system
/dbus.socket
994 # Newer Fedora versions use dbus-broker by default. Let's install it if it's available.
995 if [ -f $ROOTLIBDIR/system
/dbus-broker.service
]; then
996 inst
$ROOTLIBDIR/system
/dbus-broker.service
997 inst_symlink
/etc
/systemd
/system
/dbus.service
998 inst
/usr
/bin
/dbus-broker
999 inst
/usr
/bin
/dbus-broker-launch
1000 elif [ -f $ROOTLIBDIR/system
/dbus-daemon.service
]; then
1001 # Fedora rawhide replaced dbus.service with dbus-daemon.service
1002 inst
$ROOTLIBDIR/system
/dbus-daemon.service
1004 inst_symlink
/etc
/systemd
/system
/dbus.service
1006 inst
$ROOTLIBDIR/system
/dbus.service
1010 /etc
/dbus-1
/usr
/share
/dbus-1
-xtype f \
1011 |
while read file; do
1015 # setup policy for Type=dbus test
1016 mkdir
-p $initdir/etc
/dbus-
1/system.d
1017 cat > $initdir/etc
/dbus-
1/system.d
/systemd.
test.ExecStopPost.conf
<<EOF
1018 <?xml version="1.0"?>
1019 <!DOCTYPE busconfig PUBLIC "-//freedesktop//DTD D-BUS Bus Configuration 1.0//EN"
1020 "http://www.freedesktop.org/standards/dbus/1.0/busconfig.dtd">
1022 <policy user="root">
1023 <allow own="systemd.test.ExecStopPost"/>
1029 install_user_dbus
() {
1030 inst
$ROOTLIBDIR/user
/dbus.socket
1031 inst_symlink
/usr
/lib
/systemd
/user
/sockets.target.wants
/dbus.socket || inst_symlink
/etc
/systemd
/user
/sockets.target.wants
/dbus.socket
1033 # Append the After= dependency on dbus in case it isn't already set up
1034 mkdir
-p "$initdir/etc/systemd/system/user@.service.d/"
1035 cat <<EOF >"$initdir/etc/systemd/system/user@.service.d/dbus.conf"
1040 # Newer Fedora versions use dbus-broker by default. Let's install it if it's available.
1041 if [ -f $ROOTLIBDIR/user
/dbus-broker.service
]; then
1042 inst
$ROOTLIBDIR/user
/dbus-broker.service
1043 inst_symlink
/etc
/systemd
/user
/dbus.service
1044 elif [ -f $ROOTLIBDIR/system
/dbus-daemon.service
]; then
1045 # Fedora rawhide replaced dbus.service with dbus-daemon.service
1046 inst
$ROOTLIBDIR/user
/dbus-daemon.service
1048 inst_symlink
/etc
/systemd
/user
/dbus.service
1050 inst
$ROOTLIBDIR/user
/dbus.service
1056 if [[ "$LOOKS_LIKE_DEBIAN" ]] && type -p dpkg-architecture
&>/dev
/null
; then
1057 find "/lib/$(dpkg-architecture -qDEB_HOST_MULTIARCH)/security" -xtype f
1059 find /lib
*/security
-xtype f
1061 find /etc
/pam.d
/etc
/security
-xtype f
1062 ) |
while read file; do
1066 # pam_unix depends on unix_chkpwd.
1067 # see http://www.linux-pam.org/Linux-PAM-html/sag-pam_unix.html
1068 dracut_install
-o unix_chkpwd
1070 [[ "$LOOKS_LIKE_DEBIAN" ]] &&
1071 cp /etc
/pam.d
/systemd-user
$initdir/etc
/pam.d
/
1073 # set empty root password for easy debugging
1074 sed -i 's/^root:x:/root::/' $initdir/etc
/passwd
1078 # The first three paths may be deprecated.
1079 # It seems now the last two paths are used by many distributions.
1081 /usr
/lib
/kbd
/keymaps
/include
/* \
1082 /usr
/lib
/kbd
/keymaps
/i386
/include
/* \
1083 /usr
/lib
/kbd
/keymaps
/i386
/qwerty
/us.
* \
1084 /usr
/lib
/kbd
/keymaps
/legacy
/include
/* \
1085 /usr
/lib
/kbd
/keymaps
/legacy
/i386
/qwerty
/us.
*; do
1086 [[ -f $i ]] ||
continue
1090 # When it takes any argument, then install more keymaps.
1091 if [[ -n $1 ]]; then
1093 /usr
/lib
/kbd
/keymaps
/i386
/*/* \
1094 /usr
/lib
/kbd
/keymaps
/legacy
/i386
/*/*; do
1095 [[ -f $i ]] ||
continue
1101 install_zoneinfo
() {
1102 inst_any
/usr
/share
/zoneinfo
/Asia
/Seoul
1103 inst_any
/usr
/share
/zoneinfo
/Asia
/Vladivostok
1104 inst_any
/usr
/share
/zoneinfo
/Australia
/Sydney
1105 inst_any
/usr
/share
/zoneinfo
/Europe
/Berlin
1106 inst_any
/usr
/share
/zoneinfo
/Europe
/Kiev
1107 inst_any
/usr
/share
/zoneinfo
/Pacific
/Auckland
1108 inst_any
/usr
/share
/zoneinfo
/Pacific
/Honolulu
1109 inst_any
/usr
/share
/zoneinfo
/CET
1110 inst_any
/usr
/share
/zoneinfo
/EET
1111 inst_any
/usr
/share
/zoneinfo
/UTC
1116 /usr
/lib
/kbd
/consolefonts
/eurlatgr
* \
1117 /usr
/lib
/kbd
/consolefonts
/latarcyrheb-sun16
*; do
1118 [[ -f $i ]] ||
continue
1123 install_terminfo
() {
1124 for _terminfodir
in /lib
/terminfo
/etc
/terminfo
/usr
/share
/terminfo
; do
1125 [ -f ${_terminfodir}/l
/linux
] && break
1127 dracut_install
-o ${_terminfodir}/l
/linux
1130 has_user_dbus_socket
() {
1131 if [ -f /usr
/lib
/systemd
/user
/dbus.socket
] ||
[ -f /etc
/systemd
/user
/dbus.socket
]; then
1134 echo "Per-user instances are not supported. Skipping..."
1139 setup_nspawn_root
() {
1140 if [ -z "${initdir}" ]; then
1141 dfatal
"\$initdir not defined"
1145 rm -rf "$TESTDIR/unprivileged-nspawn-root"
1147 if [[ "$RUN_IN_UNPRIVILEGED_CONTAINER" = "yes" ]]; then
1148 ddebug
"cp -ar $initdir $TESTDIR/unprivileged-nspawn-root"
1149 cp -ar $initdir $TESTDIR/unprivileged-nspawn-root
1153 setup_basic_dirs
() {
1154 mkdir
-p $initdir/run
1155 mkdir
-p $initdir/etc
/systemd
/system
1156 mkdir
-p $initdir/var
/log
/journal
1158 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
1159 if [ -L "/$d" ]; then
1166 ln -sfn /run
"$initdir/var/run"
1167 ln -sfn /run
/lock
"$initdir/var/lock"
1170 mask_supporting_services
() {
1171 # mask some services that we do not want to run in these tests
1172 ln -fs /dev
/null
$initdir/etc
/systemd
/system
/systemd-hwdb-update.service
1173 ln -fs /dev
/null
$initdir/etc
/systemd
/system
/systemd-journal-catalog-update.service
1174 ln -fs /dev
/null
$initdir/etc
/systemd
/system
/systemd-networkd.service
1175 ln -fs /dev
/null
$initdir/etc
/systemd
/system
/systemd-networkd.socket
1176 ln -fs /dev
/null
$initdir/etc
/systemd
/system
/systemd-resolved.service
1181 local _so_regex
='([^ ]*/lib[^/]*/[^ ]*\.so[^ ]*)'
1184 LC_ALL
=C ldd
"$_bin" 2>/dev
/null |
while read _line
; do
1185 [[ $_line = 'not a dynamic executable' ]] && break
1187 if [[ $_line =~
$_so_regex ]]; then
1188 _file
=${BASH_REMATCH[1]}
1189 [[ -e ${initdir}/$_file ]] && continue
1190 inst_library
"$_file"
1194 if [[ $_line =~ not\ found
]]; then
1195 dfatal
"Missing a shared library required by $_bin."
1196 dfatal
"Run \"ldd $_bin\" to find out what it is."
1198 dfatal
"dracut cannot create an initrd."
1205 # make sure we don't get a stale LOOPDEV value from old times
1207 [[ -e $STATEFILE ]] && .
$STATEFILE
1209 if [[ ! -d "$TESTDIR" ]]; then
1210 if [[ -z "$TESTDIR" ]]; then
1211 TESTDIR
=$
(mktemp
--tmpdir=/var
/tmp
-d -t systemd-test.XXXXXX
)
1216 cat >$STATEFILE<<EOF
1222 IMAGE_PRIVATE
="${TESTDIR}/${IMAGE_NAME}.img"
1223 IMAGE_PUBLIC
="${IMAGESTATEDIR}/${IMAGE_NAME}.img"
1227 initdir
=$TESTDIR/root
1232 ## @brief Converts numeric logging level to the first letter of level name.
1234 # @param lvl Numeric logging level in range from 1 to 6.
1235 # @retval 1 if @a lvl is out of range.
1236 # @retval 0 if @a lvl is correct.
1237 # @result Echoes first letter of level name.
1250 ## @brief Internal helper function for _do_dlog()
1252 # @param lvl Numeric logging level.
1253 # @param msg Message.
1254 # @retval 0 It's always returned, even if logging failed.
1256 # @note This function is not supposed to be called manually. Please use
1257 # dtrace(), ddebug(), or others instead which wrap this one.
1259 # This function calls _do_dlog() either with parameter msg, or if
1260 # none is given, it will read standard input and will use every line as
1264 # dwarn "This is a warning"
1265 # echo "This is a warning" | dwarn
1266 LOG_LEVEL
=${LOG_LEVEL:-4}
1269 [ -z "$LOG_LEVEL" ] && return 0
1270 [ $1 -le $LOG_LEVEL ] ||
return 0
1271 local lvl
="$1"; shift
1272 local lvlc
=$
(_lvl2char
"$lvl") ||
return 0
1274 if [ $# -ge 1 ]; then
1278 echo "$lvlc: " "$line"
1283 ## @brief Logs message at TRACE level (6)
1285 # @param msg Message.
1286 # @retval 0 It's always returned, even if logging failed.
1290 [ -n "$debug" ] && set -x ||
:
1293 ## @brief Logs message at DEBUG level (5)
1295 # @param msg Message.
1296 # @retval 0 It's always returned, even if logging failed.
1300 # [ -n "$debug" ] && set -x || :
1303 ## @brief Logs message at INFO level (4)
1305 # @param msg Message.
1306 # @retval 0 It's always returned, even if logging failed.
1310 [ -n "$debug" ] && set -x ||
:
1313 ## @brief Logs message at WARN level (3)
1315 # @param msg Message.
1316 # @retval 0 It's always returned, even if logging failed.
1320 [ -n "$debug" ] && set -x ||
:
1323 ## @brief Logs message at ERROR level (2)
1325 # @param msg Message.
1326 # @retval 0 It's always returned, even if logging failed.
1330 # [ -n "$debug" ] && set -x || :
1333 ## @brief Logs message at FATAL level (1)
1335 # @param msg Message.
1336 # @retval 0 It's always returned, even if logging failed.
1340 [ -n "$debug" ] && set -x ||
:
1344 # Generic substring function. If $2 is in $1, return 0.
1345 strstr
() { [ "${1#*$2*}" != "$1" ]; }
1347 # normalize_path <path>
1348 # Prints the normalized path, where it removes any duplicated
1349 # and trailing slashes.
1351 # $ normalize_path ///test/test//
1355 set -- "${1//+(\/)//}"
1360 # convert_abs_rel <from> <to>
1361 # Prints the relative path, when creating a symlink to <to> from <from>.
1363 # $ convert_abs_rel /usr/bin/test /bin/test-2
1365 # $ ln -s $(convert_abs_rel /usr/bin/test /bin/test-2) /usr/bin/test
1367 local __current __absolute __abssize __cursize __newpath
1368 local -i __i __level
1370 set -- "$(normalize_path "$1")" "$(normalize_path "$2")"
1372 # corner case #1 - self looping link
1373 [[ "$1" == "$2" ]] && { echo "${1##*/}"; return; }
1375 # corner case #2 - own dir link
1376 [[ "${1%/*}" == "$2" ]] && { echo "."; return; }
1378 IFS
="/" __current
=($1)
1379 IFS
="/" __absolute
=($2)
1381 __abssize
=${#__absolute[@]}
1382 __cursize
=${#__current[@]}
1384 while [[ ${__absolute[__level]} == ${__current[__level]} ]]
1387 if (( __level
> __abssize || __level
> __cursize
))
1393 for ((__i
= __level
; __i
< __cursize-1
; __i
++))
1395 if ((__i
> __level
))
1397 __newpath
=$__newpath"/"
1399 __newpath
=$__newpath".."
1402 for ((__i
= __level
; __i
< __abssize
; __i
++))
1404 if [[ -n $__newpath ]]
1406 __newpath
=$__newpath"/"
1408 __newpath
=$__newpath${__absolute[__i]}
1415 # Install a directory, keeping symlinks as on the original system.
1416 # Example: if /lib points to /lib64 on the host, "inst_dir /lib/file"
1417 # will create ${initdir}/lib64, ${initdir}/lib64/file,
1418 # and a symlink ${initdir}/lib -> lib64.
1420 [[ -e ${initdir}/"$1" ]] && return 0 # already there
1422 local _dir
="$1" _part
="${1%/*}" _file
1423 while [[ "$_part" != "${_part%/*}" ]] && ! [[ -e "${initdir}/${_part}" ]]; do
1428 # iterate over parent directories
1429 for _file
in $_dir; do
1430 [[ -e "${initdir}/$_file" ]] && continue
1431 if [[ -L $_file ]]; then
1432 inst_symlink
"$_file"
1435 mkdir
-m 0755 -p "${initdir}/$_file" ||
return 1
1436 [[ -e "$_file" ]] && chmod --reference="$_file" "${initdir}/$_file"
1437 chmod u
+w
"${initdir}/$_file"
1442 # $1 = file to copy to ramdisk
1443 # $2 (optional) Name for the file on the ramdisk
1444 # Location of the image dir is assumed to be $initdir
1445 # We never overwrite the target if it exists.
1447 [[ -f "$1" ]] ||
return 1
1448 strstr
"$1" "/" ||
return 1
1450 local _src
=$1 target
="${2:-$1}"
1451 if ! [[ -d ${initdir}/$target ]]; then
1452 [[ -e ${initdir}/$target ]] && return 0
1453 [[ -L ${initdir}/$target ]] && return 0
1454 [[ -d "${initdir}/${target%/*}" ]] || inst_dir "${target%/*}"
1456 # install checksum files also
1457 if [[ -e "${_src%/*}/.${_src##*/}.hmac" ]]; then
1458 inst
"${_src%/*}/.${_src##*/}.hmac" "${target%/*}/.${target##*/}.hmac"
1460 ddebug
"Installing $_src"
1461 cp --sparse=always
-pfL "$_src" "${initdir}/$target"
1464 # find symlinks linked to given library file
1466 # Function searches for symlinks by stripping version numbers appended to
1467 # library filename, checks if it points to the same target and finally
1468 # prints the list of symlinks to stdout.
1471 # rev_lib_symlinks libfoo.so.8.1
1472 # output: libfoo.so.8 libfoo.so
1473 # (Only if libfoo.so.8 and libfoo.so exists on host system.)
1474 rev_lib_symlinks
() {
1475 [[ ! $1 ]] && return 0
1477 local fn
="$1" orig
="$(readlink -f "$1")" links
=''
1479 [[ ${fn} =~ .
*\.so\..
* ]] ||
return 1
1481 until [[ ${fn##*.} == so
]]; do
1483 [[ -L ${fn} && $(readlink -f "${fn}") == ${orig} ]] && links+=" ${fn}"
1489 # Same as above, but specialized to handle dynamic libraries.
1490 # It handles making symlinks according to how the original library
1493 local _src="$1" _dest=${2:-$1} _lib _reallib _symlink
1494 strstr "$1" "/" || return 1
1495 [[ -e $initdir/$_dest ]] && return 0
1496 if [[ -L $_src ]]; then
1497 # install checksum files also
1498 if [[ -e "${_src%/*}/.
${_src##*/}.hmac
" ]]; then
1499 inst "${_src%/*}/.${_src##*/}.hmac" "${_dest%/*}/.${_dest##*/}.hmac
"
1501 _reallib=$(readlink -f "$_src")
1502 inst_simple "$_reallib" "$_reallib"
1503 inst_dir "${_dest%/*}"
1504 [[ -d "${_dest%/*}" ]] && _dest=$(readlink -f "${_dest%/*}")/${_dest##*/}
1505 ln -sfn $
(convert_abs_rel
"${_dest}" "${_reallib}") "${initdir}/${_dest}"
1507 inst_simple
"$_src" "$_dest"
1510 # Create additional symlinks. See rev_symlinks description.
1511 for _symlink
in $
(rev_lib_symlinks
$_src) $
(rev_lib_symlinks
$_reallib); do
1512 [[ -e $initdir/$_symlink ]] ||
{
1513 ddebug
"Creating extra symlink: $_symlink"
1514 inst_symlink
$_symlink
1519 # find a binary. If we were not passed the full path directly,
1520 # search in the usual places to find the binary.
1522 if [[ -z ${1##/*} ]]; then
1523 if [[ -x $1 ]] ||
{ strstr
"$1" ".so" && ldd
$1 &>/dev
/null
; }; then
1532 # Same as above, but specialized to install binary executables.
1533 # Install binary executable, and all shared library dependencies, if any.
1537 # In certain cases we might attempt to install a binary which is already
1538 # present in the test image, yet it's missing from the host system.
1539 # In such cases, let's check if the binary indeed exists in the image
1540 # before doing any other chcecks. If it does, immediately return with
1542 [[ $# -eq 1 && -e $initdir/$1 ]] && return 0
1544 _bin
=$
(find_binary
"$1") ||
return 1
1546 [[ -e $initdir/$_target ]] && return 0
1547 [[ -L $_bin ]] && inst_symlink
$_bin $_target && return 0
1549 local _so_regex
='([^ ]*/lib[^/]*/[^ ]*\.so[^ ]*)'
1551 LC_ALL
=C ldd
"$_bin" 2>/dev
/null |
while read _line
; do
1552 [[ $_line = 'not a dynamic executable' ]] && break
1554 if [[ $_line =~
$_so_regex ]]; then
1555 _file
=${BASH_REMATCH[1]}
1556 [[ -e ${initdir}/$_file ]] && continue
1557 inst_library
"$_file"
1561 if [[ $_line =~ not\ found
]]; then
1562 dfatal
"Missing a shared library required by $_bin."
1563 dfatal
"Run \"ldd $_bin\" to find out what it is."
1565 dfatal
"dracut cannot create an initrd."
1569 inst_simple
"$_bin" "$_target"
1572 # same as above, except for shell scripts.
1573 # If your shell script does not start with shebang, it is not a shell script.
1576 _bin
=$
(find_binary
"$1") ||
return 1
1578 local _line _shebang_regex
1579 read -r -n 80 _line
<"$_bin"
1580 # If debug is set, clean unprintable chars to prevent messing up the term
1581 [[ $debug ]] && _line
=$
(echo -n "$_line" |
tr -c -d '[:print:][:space:]')
1582 _shebang_regex
='(#! *)(/[^ ]+).*'
1583 [[ $_line =~
$_shebang_regex ]] ||
return 1
1584 inst
"${BASH_REMATCH[2]}" && inst_simple
"$_bin" "$@"
1587 # same as above, but specialized for symlinks
1589 local _src
=$1 _target
=${2:-$1} _realsrc
1590 strstr
"$1" "/" ||
return 1
1591 [[ -L $1 ]] ||
return 1
1592 [[ -L $initdir/$_target ]] && return 0
1593 _realsrc
=$
(readlink
-f "$_src")
1594 if ! [[ -e $initdir/$_realsrc ]]; then
1595 if [[ -d $_realsrc ]]; then
1596 inst_dir
"$_realsrc"
1601 [[ ! -e $initdir/${_target%/*} ]] && inst_dir
"${_target%/*}"
1602 [[ -d ${_target%/*} ]] && _target=$(readlink -f ${_target%/*})/${_target##*/}
1603 ln -sfn $
(convert_abs_rel
"${_target}" "${_realsrc}") "$initdir/$_target"
1606 # attempt to install any programs specified in a udev rule
1607 inst_rule_programs
() {
1610 if grep -qE 'PROGRAM==?"[^ "]+' "$1"; then
1611 for _prog
in $
(grep -E 'PROGRAM==?"[^ "]+' "$1" |
sed -r 's/.*PROGRAM==?"([^ "]+).*/\1/'); do
1612 if [ -x /lib
/udev
/$_prog ]; then
1613 _bin
=/lib
/udev
/$_prog
1615 _bin
=$
(find_binary
"$_prog") ||
{
1616 dinfo
"Skipping program $_prog using in udev rule $(basename $1) as it cannot be found"
1621 #dinfo "Installing $_bin due to it's use in the udev rule $(basename $1)"
1622 dracut_install
"$_bin"
1627 # udev rules always get installed in the same place, so
1628 # create a function to install them to make life simpler.
1630 local _target
=/etc
/udev
/rules.d _rule _found
1632 inst_dir
"/lib/udev/rules.d"
1634 for _rule
in "$@"; do
1635 if [ "${rule#/}" = "$rule" ]; then
1636 for r
in /lib
/udev
/rules.d
/etc
/udev
/rules.d
; do
1637 if [[ -f $r/$_rule ]]; then
1639 inst_simple
"$_found"
1640 inst_rule_programs
"$_found"
1644 for r
in '' .
/ $dracutbasedir/rules.d
/; do
1645 if [[ -f ${r}$_rule ]]; then
1647 inst_simple
"$_found" "$_target/${_found##*/}"
1648 inst_rule_programs
"$_found"
1651 [[ $_found ]] || dinfo
"Skipping udev rule: $_rule"
1656 # general purpose installation function
1657 # Same args as above.
1663 2) [[ ! $initdir && -d $2 ]] && export initdir
=$2
1664 [[ $initdir = $2 ]] && set $1;;
1665 3) [[ -z $initdir ]] && export initdir
=$2
1667 *) dfatal
"inst only takes 1 or 2 or 3 arguments"
1670 for _x
in inst_symlink inst_script inst_binary inst_simple
; do
1671 $_x "$@" && return 0
1676 # install any of listed files
1678 # If first argument is '-d' and second some destination path, first accessible
1679 # source is installed into this path, otherwise it will installed in the same
1680 # path as source. If none of listed files was installed, function return 1.
1681 # On first successful installation it returns with 0 status.
1685 # inst_any -d /bin/foo /bin/bar /bin/baz
1687 # Lets assume that /bin/baz exists, so it will be installed as /bin/foo in
1692 [[ $1 = '-d' ]] && to
="$2" && shift 2
1695 if [[ -e $f ]]; then
1696 [[ $to ]] && inst
"$f" "$to" && return 0
1697 inst
"$f" && return 0
1704 # dracut_install [-o ] <file> [<file> ... ]
1705 # Install <file> to the initramfs image
1706 # -o optionally install the <file> and don't fail, if it is not there
1709 if [[ $1 = '-o' ]]; then
1713 while (($# > 0)); do
1714 if ! inst
"$1" ; then
1715 if [[ $_optional = yes ]]; then
1716 dinfo
"Skipping program $1 as it cannot be found and is" \
1717 "flagged to be optional"
1719 dfatal
"Failed to install $1"
1727 # Install a single kernel module along with any firmware it may require.
1728 # $1 = full path to kernel module to install
1729 install_kmod_with_fw
() {
1730 # no need to go further if the module is already installed
1732 [[ -e "${initdir}/lib/modules/$KERNEL_VER/${1##*/lib/modules/$KERNEL_VER/}" ]] \
1735 [[ -e "$initdir/.kernelmodseen/${1##*/}" ]] && return 0
1737 if [[ $omit_drivers ]]; then
1738 local _kmod
=${1##*/}
1741 if [[ "$_kmod" =~
$omit_drivers ]]; then
1742 dinfo
"Omitting driver $_kmod"
1745 if [[ "${1##*/lib/modules/$KERNEL_VER/}" =~
$omit_drivers ]]; then
1746 dinfo
"Omitting driver $_kmod"
1751 [ -d "$initdir/.kernelmodseen" ] && \
1752 > "$initdir/.kernelmodseen/${1##*/}"
1754 inst_simple
"$1" "/lib/modules/$KERNEL_VER/${1##*/lib/modules/$KERNEL_VER/}" \
1757 local _modname
=${1##*/} _fwdir _found _fw
1758 _modname
=${_modname%.ko*}
1759 for _fw
in $
(modinfo
-k $KERNEL_VER -F firmware
$1 2>/dev
/null
); do
1761 for _fwdir
in $fw_dir; do
1762 if [[ -d $_fwdir && -f $_fwdir/$_fw ]]; then
1763 inst_simple
"$_fwdir/$_fw" "/lib/firmware/$_fw"
1767 if [[ $_found != yes ]]; then
1768 if ! grep -qe "\<${_modname//-/_}\>" /proc
/modules
; then
1769 dinfo
"Possible missing firmware \"${_fw}\" for kernel module" \
1770 "\"${_modname}.ko\""
1772 dwarn
"Possible missing firmware \"${_fw}\" for kernel module" \
1773 "\"${_modname}.ko\""
1780 # Do something with all the dependencies of a kernel module.
1781 # Note that kernel modules depend on themselves using the technique we use
1782 # $1 = function to call for each dependency we find
1783 # It will be passed the full path to the found kernel module
1784 # $2 = module to get dependencies for
1785 # rest of args = arguments to modprobe
1786 # _fderr specifies FD passed from surrounding scope
1787 for_each_kmod_dep
() {
1788 local _func
=$1 _kmod
=$2 _cmd _modpath _options _found
=0
1790 modprobe
"$@" --ignore-install --show-depends $_kmod 2>&${_fderr} |
(
1791 while read _cmd _modpath _options
; do
1792 [[ $_cmd = insmod
]] ||
continue
1793 $_func ${_modpath} ||
exit $?
1796 [[ $_found -eq 0 ]] && exit 1
1801 # filter kernel modules to install certain modules that meet specific
1803 # $1 = search only in subdirectory of /kernel/$1
1804 # $2 = function to call with module name to filter.
1805 # This function will be passed the full path to the module to test.
1806 # The behavior of this function can vary depending on whether $hostonly is set.
1807 # If it is, we will only look at modules that are already in memory.
1808 # If it is not, we will look at all kernel modules
1809 # This function returns the full filenames of modules that match $1
1810 filter_kernel_modules_by_path
() (
1811 local _modname _filtercmd
1812 if ! [[ $hostonly ]]; then
1813 _filtercmd
='find "$KERNEL_MODS/kernel/$1" "$KERNEL_MODS/extra"'
1814 _filtercmd
+=' "$KERNEL_MODS/weak-updates" -name "*.ko" -o -name "*.ko.gz"'
1815 _filtercmd
+=' -o -name "*.ko.xz"'
1816 _filtercmd
+=' 2>/dev/null'
1818 _filtercmd
='cut -d " " -f 1 </proc/modules|xargs modinfo -F filename '
1819 _filtercmd
+='-k $KERNEL_VER 2>/dev/null'
1821 for _modname
in $
(eval $_filtercmd); do
1823 *.ko
) "$2" "$_modname" && echo "$_modname";;
1824 *.ko.gz
) gzip -dc "$_modname" > $initdir/$$.ko
1825 $2 $initdir/$$.ko
&& echo "$_modname"
1826 rm -f $initdir/$$.ko
1828 *.ko.xz
) xz
-dc "$_modname" > $initdir/$$.ko
1829 $2 $initdir/$$.ko
&& echo "$_modname"
1830 rm -f $initdir/$$.ko
1835 find_kernel_modules_by_path
() (
1836 if ! [[ $hostonly ]]; then
1837 find "$KERNEL_MODS/kernel/$1" "$KERNEL_MODS/extra" "$KERNEL_MODS/weak-updates" \
1838 -name "*.ko" -o -name "*.ko.gz" -o -name "*.ko.xz" 2>/dev
/null
1840 cut
-d " " -f 1 </proc
/modules \
1841 |
xargs modinfo
-F filename
-k $KERNEL_VER 2>/dev
/null
1845 filter_kernel_modules
() {
1846 filter_kernel_modules_by_path drivers
"$1"
1849 find_kernel_modules
() {
1850 find_kernel_modules_by_path drivers
1853 # instmods [-c] <kernel module> [<kernel module> ... ]
1854 # instmods [-c] <kernel subsystem>
1855 # install kernel modules along with all their dependencies.
1856 # <kernel subsystem> can be e.g. "=block" or "=drivers/usb/storage"
1858 [[ $no_kernel = yes ]] && return
1859 # called [sub]functions inherit _fderr
1862 if [[ $1 = '-c' ]]; then
1867 function inst1mod
() {
1868 local _ret
=0 _mod
="$1"
1871 if [ -f $KERNEL_MODS/modules.
${_mod#=} ]; then
1872 ( [[ "$_mpargs" ]] && echo $_mpargs
1873 cat "${KERNEL_MODS}/modules.${_mod#=}" ) \
1876 ( [[ "$_mpargs" ]] && echo $_mpargs
1877 find "$KERNEL_MODS" -path "*/${_mod#=}/*" -type f
-printf '%f\n' ) \
1881 --*) _mpargs
+=" $_mod" ;;
1882 i2o_scsi
) return ;; # Do not load this diagnostic-only module
1885 # if we are already installed, skip this module and go on
1887 [[ -f "$initdir/.kernelmodseen/${_mod%.ko}.ko" ]] && return
1889 if [[ $omit_drivers ]] && [[ "$1" =~
$omit_drivers ]]; then
1890 dinfo
"Omitting driver ${_mod##$KERNEL_MODS}"
1893 # If we are building a host-specific initramfs and this
1894 # module is not already loaded, move on to the next one.
1895 [[ $hostonly ]] && ! grep -qe "\<${_mod//-/_}\>" /proc
/modules \
1896 && ! echo $add_drivers |
grep -qe "\<${_mod}\>" \
1899 # We use '-d' option in modprobe only if modules prefix path
1900 # differs from default '/'. This allows us to use Dracut with
1901 # old version of modprobe which doesn't have '-d' option.
1902 local _moddirname
=${KERNEL_MODS%%/lib/modules/*}
1903 [[ -n ${_moddirname} ]] && _moddirname
="-d ${_moddirname}/"
1905 # ok, load the module, all its dependencies, and any firmware
1907 for_each_kmod_dep install_kmod_with_fw
$_mod \
1908 --set-version $KERNEL_VER ${_moddirname} $_mpargs
1915 function instmods_1
() {
1917 if (($# == 0)); then # filenames from stdin
1919 inst1mod
"${_mod%.ko*}" ||
{
1920 if [ "$_check" = "yes" ]; then
1921 dfatal
"Failed to install $_mod"
1927 while (($# > 0)); do # filenames as arguments
1928 inst1mod
${1%.ko*} ||
{
1929 if [ "$_check" = "yes" ]; then
1930 dfatal
"Failed to install $1"
1939 local _ret _filter_not_found
='FATAL: Module .* not found.'
1941 # Capture all stderr from modprobe to _fderr. We could use {var}>...
1942 # redirections, but that would make dracut require bash4 at least.
1943 eval "( instmods_1 \"\$@\" ) ${_fderr}>&1" \
1944 |
while read line
; do [[ "$line" =~
$_filter_not_found ]] && echo $line ||
echo $line >&2 ;done | derror
1951 ln -fs ..
/usr
/bin
/systemctl
$initdir/bin
/
1952 ln -fs ..
/usr
/lib
/systemd
$initdir/lib
/
1953 inst_simple
"/usr/lib/systemd/system/haveged.service"
1958 if mountpoint
-q $1; then
1964 # can be overridden in specific test
1965 test_setup_cleanup
() {
1970 # (post-test) cleanup should always ignore failure and cleanup as much as possible
1973 _umount_dir
$initdir
1974 rm -vf "$IMAGE_PUBLIC"
1980 # can be overridden in specific test
1985 test_cleanup_again
() {
1986 [ -n "$TESTDIR" ] ||
return
1987 rm -rf "$TESTDIR/unprivileged-nspawn-root"
1988 _umount_dir
$initdir
1991 test_create_image
() {
1992 create_empty_image_rootdir
1994 # Create what will eventually be our root filesystem onto an overlay
1997 setup_basic_environment
1998 mask_supporting_services
2003 if [ ${TEST_REQUIRE_INSTALL_TESTS} -ne 0 ] && \
2004 type -P meson
>/dev
/null
&& \
2005 [[ "$(meson configure $BUILD_DIR | grep install-tests | awk '{ print $2 }')" != "true" ]]; then
2006 dfatal
"$BUILD_DIR needs to be built with -Dinstall-tests=true"
2010 if [ -e "$IMAGE_PRIVATE" ]; then
2011 echo "Reusing existing image $IMAGE_PRIVATE → $(realpath $IMAGE_PRIVATE)"
2014 if [ ! -e "$IMAGE_PUBLIC" ]; then
2015 # Create the backing public image, but then completely unmount
2016 # it and drop the loopback device responsible for it, since we're
2017 # going to symlink/copy the image and mount it again from
2025 echo "Reusing existing cached image $IMAGE_PUBLIC → $(realpath $IMAGE_PUBLIC)"
2026 if [ ${TEST_PARALLELIZE} -ne 0 ]; then
2027 cp -v "$(realpath $IMAGE_PUBLIC)" "$IMAGE_PRIVATE"
2029 ln -sv "$(realpath $IMAGE_PUBLIC)" "$IMAGE_PRIVATE"
2041 if [ -z "$TEST_NO_QEMU" ]; then
2042 if run_qemu
"$1"; then
2043 check_result_qemu ||
{ echo "QEMU test failed"; return 1; }
2045 dwarn
"can't run QEMU, skipping"
2048 if [ -z "$TEST_NO_NSPAWN" ]; then
2050 if run_nspawn
"$initdir" "$1"; then
2051 check_result_nspawn
"$initdir" ||
{ echo "nspawn-root test failed"; return 1; }
2053 dwarn
"can't run systemd-nspawn, skipping"
2056 if [[ "$RUN_IN_UNPRIVILEGED_CONTAINER" = "yes" ]]; then
2057 dir
="$TESTDIR/unprivileged-nspawn-root"
2058 if NSPAWN_ARGUMENTS
="-U --private-network $NSPAWN_ARGUMENTS" run_nspawn
"$dir" "$1"; then
2059 check_result_nspawn
"$dir" ||
{ echo "unprivileged-nspawn-root test failed"; return 1; }
2061 dwarn
"can't run systemd-nspawn, skipping"
2069 if [[ $UID != "0" ]]; then
2070 echo "TEST: $TEST_DESCRIPTION [SKIPPED]: not root" >&2
2075 [[ $libdir ]] ||
for libdir
in /lib64
/lib
; do
2076 [[ -d $libdir ]] && libdirs
+=" $libdir" && break
2079 [[ $usrlibdir ]] ||
for usrlibdir
in /usr
/lib64
/usr
/lib
; do
2080 [[ -d $usrlibdir ]] && libdirs
+=" $usrlibdir" && break
2083 mkdir
-p "$STATEDIR"
2088 testname
="$(basename $PWD)"
2090 while (($# > 0)); do
2093 echo "${testname} RUN: $TEST_DESCRIPTION"
2096 if (( $ret == 0 )); then
2097 echo "${testname} RUN: $TEST_DESCRIPTION [OK]"
2099 echo "${testname} RUN: $TEST_DESCRIPTION [FAILED]"
2103 echo "${testname} SETUP: $TEST_DESCRIPTION"
2108 echo "${testname} CLEANUP: $TEST_DESCRIPTION"
2112 echo "${testname} CLEANUP AGAIN: $TEST_DESCRIPTION"
2117 echo -n "${testname}: $TEST_DESCRIPTION "
2122 ) </dev
/null
>"$TESTLOG" 2>&1 || ret
=$?
2124 if [ $ret -eq 0 ]; then