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 LOOKS_LIKE_DEBIAN
=$
(source /etc
/os-release
&& [[ "$ID" = "debian" ||
" $ID_LIKE " = *" debian "* ]] && echo yes ||
:)
8 LOOKS_LIKE_ARCH
=$
(source /etc
/os-release
&& [[ "$ID" = "arch" ||
" $ID_LIKE " = *" arch "* ]] && echo yes ||
:)
9 LOOKS_LIKE_SUSE
=$
(source /etc
/os-release
&& [[ " $ID_LIKE " = *" suse "* ]] && echo yes ||
:)
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
="${EFI_MOUNT:-$(bootctl -x 2>/dev/null || echo /boot)}"
18 QEMU_MEM
="${QEMU_MEM:-512M}"
19 IMAGE_NAME
=${IMAGE_NAME:-default}
20 TEST_REQUIRE_INSTALL_TESTS
="${TEST_REQUIRE_INSTALL_TESTS:-1}"
21 TEST_PARALLELIZE
="${TEST_PARALLELIZE:-0}"
24 # Decide if we can (and want to) run QEMU with KVM acceleration.
25 # Check if nested KVM is explicitly enabled (TEST_NESTED_KVM). If not,
26 # check if it's not explicitly disabled (TEST_NO_KVM) and we're not already
27 # running under KVM. If these conditions are met, enable KVM (and possibly
28 # nested KVM), otherwise disable it.
29 if [[ -n "$TEST_NESTED_KVM" ||
( -z "$TEST_NO_KVM" && $
(systemd-detect-virt
-v) != kvm
) ]]; then
35 if ! ROOTLIBDIR
=$
(pkg-config
--variable=systemdutildir systemd
); then
36 echo "WARNING! Cannot determine rootlibdir from pkg-config, assuming /usr/lib/systemd" >&2
37 ROOTLIBDIR
=/usr
/lib
/systemd
40 PATH_TO_INIT
=$ROOTLIBDIR/systemd
41 [ "$SYSTEMD_JOURNALD" ] || SYSTEMD_JOURNALD
=$
(which -a $BUILD_DIR/systemd-journald
$ROOTLIBDIR/systemd-journald
2>/dev
/null |
grep '^/' -m1)
42 [ "$SYSTEMD_JOURNAL_REMOTE" ] || SYSTEMD_JOURNAL_REMOTE
=$
(which -a $BUILD_DIR/systemd-journal-remote
$ROOTLIBDIR/systemd-journal-remote
2>/dev
/null |
grep '^/' -m1)
43 [ "$SYSTEMD" ] || SYSTEMD
=$
(which -a $BUILD_DIR/systemd
$ROOTLIBDIR/systemd
2>/dev
/null |
grep '^/' -m1)
44 [ "$SYSTEMD_NSPAWN" ] || SYSTEMD_NSPAWN
=$
(which -a $BUILD_DIR/systemd-nspawn systemd-nspawn
2>/dev
/null |
grep '^/' -m1)
45 [ "$JOURNALCTL" ] || JOURNALCTL
=$
(which -a $BUILD_DIR/journalctl journalctl
2>/dev
/null |
grep '^/' -m1)
144 STATEDIR
="${BUILD_DIR:-.}/test/$(basename $(dirname $(realpath $0)))"
145 STATEFILE
="$STATEDIR/.testdir"
146 IMAGESTATEDIR
="$STATEDIR/.."
147 TESTLOG
="$STATEDIR/test.log"
149 is_built_with_asan
() {
150 if ! type -P objdump
>/dev
/null
; then
151 ddebug
"Failed to find objdump. Assuming systemd hasn't been built with ASAN."
155 # Borrowed from https://github.com/google/oss-fuzz/blob/cd9acd02f9d3f6e80011cc1e9549be526ce5f270/infra/base-images/base-runner/bad_build_check#L182
156 local _asan_calls
=$
(objdump
-dC $SYSTEMD_JOURNALD |
egrep "callq\s+[0-9a-f]+\s+<__asan" -c)
157 if (( $_asan_calls < 1000 )); then
164 IS_BUILT_WITH_ASAN
=$
(is_built_with_asan
&& echo yes ||
echo no
)
166 if [[ "$IS_BUILT_WITH_ASAN" = "yes" ]]; then
168 SKIP_INITRD
="${SKIP_INITRD:-yes}"
169 PATH_TO_INIT
=$ROOTLIBDIR/systemd-under-asan
173 # We need to correctly distinguish between gcc's and clang's ASan DSOs.
174 if ldd
$SYSTEMD |
grep -q libasan.so
; then
176 elif ldd
$SYSTEMD |
grep -q libclang_rt.asan
; then
179 # As clang's ASan DSO is usually in a non-standard path, let's check if
180 # the environment is set accordingly. If not, warn the user and exit.
181 # We're not setting the LD_LIBRARY_PATH automagically here, because
182 # user should encounter (and fix) the same issue when running the unit
184 if ldd
"$SYSTEMD" |
grep -q "libclang_rt.asan.*not found"; then
185 _asan_rt_name
="$(ldd $SYSTEMD | awk '/libclang_rt.asan/ {print $1; exit}')"
186 _asan_rt_path
="$(find /usr/lib* /usr/local/lib* -type f -name "$_asan_rt_name" 2>/dev/null | sed 1q)"
187 echo >&2 "clang's ASan DSO ($_asan_rt_name) is not present in the runtime library path"
188 echo >&2 "Consider setting LD_LIBRARY_PATH=${_asan_rt_path%/*}"
192 echo >&2 "systemd is not linked against the ASan DSO"
193 echo >&2 "gcc does this by default, for clang compile with -shared-libasan"
198 function find_qemu_bin
() {
199 # SUSE and Red Hat call the binary qemu-kvm. Debian and Gentoo call it kvm.
200 if [[ $QEMU_KVM == "yes" ]]; then
201 [ "$QEMU_BIN" ] || QEMU_BIN
=$
(which -a kvm qemu-kvm
2>/dev
/null |
grep '^/' -m1)
204 [ "$ARCH" ] || ARCH
=$
(uname
-m)
207 # QEMU's own build system calls it qemu-system-x86_64
208 [ "$QEMU_BIN" ] || QEMU_BIN
=$
(which -a qemu-system-x86_64
2>/dev
/null |
grep '^/' -m1)
211 # new i386 version of QEMU
212 [ "$QEMU_BIN" ] || QEMU_BIN
=$
(which -a qemu-system-i386
2>/dev
/null |
grep '^/' -m1)
214 # i386 version of QEMU
215 [ "$QEMU_BIN" ] || QEMU_BIN
=$
(which -a qemu
2>/dev
/null |
grep '^/' -m1)
218 [ "$QEMU_BIN" ] || QEMU_BIN
=$
(which -a qemu-system-ppc64
2>/dev
/null |
grep '^/' -m1)
222 if [ ! -e "$QEMU_BIN" ]; then
223 echo "Could not find a suitable QEMU binary" >&2
228 # Return 0 if QEMU did run (then you must check the result state/logs for actual
229 # success), or 1 if QEMU is not available.
231 if [ -f /etc
/machine-id
]; then
232 read MACHINE_ID
< /etc
/machine-id
233 [ -z "$INITRD" ] && [ -e "$EFI_MOUNT/$MACHINE_ID/$KERNEL_VER/initrd" ] \
234 && INITRD
="$EFI_MOUNT/$MACHINE_ID/$KERNEL_VER/initrd"
235 [ -z "$KERNEL_BIN" ] && [ -e "$EFI_MOUNT/$MACHINE_ID/$KERNEL_VER/linux" ] \
236 && KERNEL_BIN
="$EFI_MOUNT/$MACHINE_ID/$KERNEL_VER/linux"
241 rm -f "$initdir"/{testok
,failed
,skipped
}
242 # make sure the initdir is not mounted to avoid concurrent access
246 if [[ ! "$KERNEL_BIN" ]]; then
247 if [[ "$LOOKS_LIKE_ARCH" ]]; then
248 KERNEL_BIN
=/boot
/vmlinuz-linux
250 [ "$ARCH" ] || ARCH
=$
(uname
-m)
253 KERNEL_BIN
=/boot
/vmlinux-
$KERNEL_VER
257 KERNEL_BIN
=/boot
/vmlinuz-
$KERNEL_VER
263 default_fedora_initrd
=/boot
/initramfs-
${KERNEL_VER}.img
264 default_debian_initrd
=/boot
/initrd.img-
${KERNEL_VER}
265 default_arch_initrd
=/boot
/initramfs-linux-fallback.img
266 default_suse_initrd
=/boot
/initrd-
${KERNEL_VER}
267 if [[ ! "$INITRD" ]]; then
268 if [[ -e "$default_fedora_initrd" ]]; then
269 INITRD
="$default_fedora_initrd"
270 elif [[ "$LOOKS_LIKE_DEBIAN" && -e "$default_debian_initrd" ]]; then
271 INITRD
="$default_debian_initrd"
272 elif [[ "$LOOKS_LIKE_ARCH" && -e "$default_arch_initrd" ]]; then
273 INITRD
="$default_arch_initrd"
274 elif [[ "$LOOKS_LIKE_SUSE" && -e "$default_suse_initrd" ]]; then
275 INITRD
="$default_suse_initrd"
279 # If QEMU_SMP was not explicitly set, try to determine the value 'dynamically'
280 # i.e. use the number of online CPUs on the host machine. If the nproc utility
281 # is not installed or there's some other error when calling it, fall back
282 # to the original value (QEMU_SMP=1).
283 if ! [ "$QEMU_SMP" ]; then
284 if ! QEMU_SMP
=$
(nproc
); then
285 dwarn
"nproc utility is not installed, falling back to QEMU_SMP=1"
290 find_qemu_bin ||
return 1
292 # Umount initdir to avoid concurrent access to the filesystem
296 if [[ "$UNIFIED_CGROUP_HIERARCHY" = "yes" ]]; then
297 _cgroup_args
="systemd.unified_cgroup_hierarchy=yes"
298 elif [[ "$UNIFIED_CGROUP_HIERARCHY" = "no" ]]; then
299 _cgroup_args
="systemd.unified_cgroup_hierarchy=no systemd.legacy_systemd_cgroup_controller=yes"
300 elif [[ "$UNIFIED_CGROUP_HIERARCHY" = "hybrid" ]]; then
301 _cgroup_args
="systemd.unified_cgroup_hierarchy=no systemd.legacy_systemd_cgroup_controller=no"
302 elif [[ "$UNIFIED_CGROUP_HIERARCHY" != "default" ]]; then
303 dfatal
"Unknown UNIFIED_CGROUP_HIERARCHY. Got $UNIFIED_CGROUP_HIERARCHY, expected [yes|no|hybrid|default]"
307 if [[ "$LOOKS_LIKE_SUSE" ]]; then
308 PARAMS
+="rd.hostonly=0"
312 if [[ ! "$INTERACTIVE_DEBUG" ]]; then
313 _end
="systemd.wants=end.service"
318 KERNEL_APPEND
="$PARAMS \
328 SYSTEMD_UNIT_PATH=/usr/lib/systemd/tests/testdata/testsuite-$1.units:/usr/lib/systemd/tests/testdata/units: \
329 systemd.unit=testsuite.target \
330 systemd.wants=testsuite-$1.service ${_end} \
334 [ -e "$IMAGE_PRIVATE" ] && image
="$IMAGE_PRIVATE" || image
="$IMAGE_PUBLIC"
335 QEMU_OPTIONS
="-smp $QEMU_SMP \
339 -kernel $KERNEL_BIN \
340 -drive format=raw,cache=unsafe,file=$image \
344 if [[ "$INITRD" && "$SKIP_INITRD" != "yes" ]]; then
345 QEMU_OPTIONS
="$QEMU_OPTIONS -initrd $INITRD"
348 # Let's use KVM if possible
349 if [[ -c /dev
/kvm
&& $QEMU_KVM == "yes" ]]; then
350 QEMU_OPTIONS
="$QEMU_OPTIONS -machine accel=kvm -enable-kvm -cpu host"
353 if [[ "$QEMU_TIMEOUT" != "infinity" ]]; then
354 QEMU_BIN
="timeout --foreground $QEMU_TIMEOUT $QEMU_BIN"
356 (set -x; $QEMU_BIN $QEMU_OPTIONS -append "$KERNEL_APPEND")
358 if [ "$rc" = 124 ] && [ "$QEMU_TIMEOUT" != "infinity" ]; then
359 derror
"test timed out after $QEMU_TIMEOUT s"
362 [ "$rc" != 0 ] && derror
"QEMU failed with exit code $rc"
367 # Return 0 if nspawn did run (then you must check the result state/logs for actual
368 # success), or 1 if nspawn is not available.
370 [[ -d /run
/systemd
/system
]] ||
return 1
371 rm -f "$initdir"/{testok
,failed
,skipped
}
375 --kill-signal=SIGKILL
377 --setenv=SYSTEMD_UNIT_PATH
=/usr
/lib
/systemd
/tests
/testdata
/testsuite-
$2.units
:/usr
/lib
/systemd
/tests
/testdata
/units
:
380 systemd.unit
=testsuite.target
381 systemd.wants
=testsuite-
$2.service
384 if [[ ! "$INTERACTIVE_DEBUG" ]]; then
385 _nspawn_cmd
+=( systemd.wants
=end.service
)
389 if [[ "$NSPAWN_TIMEOUT" != "infinity" ]]; then
390 _nspawn_pre
=(timeout
--foreground $NSPAWN_TIMEOUT)
395 if [[ "$UNIFIED_CGROUP_HIERARCHY" = "hybrid" ]]; then
396 dwarn
"nspawn doesn't support SYSTEMD_NSPAWN_UNIFIED_HIERARCHY=hybrid, skipping"
398 elif [[ "$UNIFIED_CGROUP_HIERARCHY" = "yes" ||
"$UNIFIED_CGROUP_HIERARCHY" = "no" ]]; then
399 _nspawn_pre
=("${_nspawn_pre[@]}" env SYSTEMD_NSPAWN_UNIFIED_HIERARCHY
=$UNIFIED_CGROUP_HIERARCHY)
400 elif [[ "$UNIFIED_CGROUP_HIERARCHY" = "default" ]]; then
401 _nspawn_pre
=("${_nspawn_pre[@]}" env
--unset=UNIFIED_CGROUP_HIERARCHY
--unset=SYSTEMD_NSPAWN_UNIFIED_HIERARCHY
)
403 dfatal
"Unknown UNIFIED_CGROUP_HIERARCHY. Got $UNIFIED_CGROUP_HIERARCHY, expected [yes|no|hybrid|default]"
407 (set -x; "${_nspawn_pre[@]}" "$SYSTEMD_NSPAWN" $NSPAWN_ARGUMENTS "${_nspawn_cmd[@]}")
409 if [ "$rc" = 124 ] && [ "$NSPAWN_TIMEOUT" != "infinity" ]; then
410 derror
"test timed out after $NSPAWN_TIMEOUT s"
413 [ "$rc" != 0 ] && derror
"nspawn failed with exit code $rc"
418 setup_basic_environment
() {
419 # create the basic filesystem layout
423 install_missing_libraries
440 has_user_dbus_socket
&& install_user_dbus
444 generate_module_dependencies
445 if [[ "$IS_BUILT_WITH_ASAN" = "yes" ]]; then
451 # don't forget KERNEL_APPEND='... selinux=1 ...'
452 if [[ "$SETUP_SELINUX" != "yes" ]]; then
453 ddebug
"Don't setup SELinux"
456 ddebug
"Setup SELinux"
457 local _conf_dir
=/etc
/selinux
458 local _fixfiles_tools
="bash uname cat sort uniq awk grep egrep head expr find rm secon setfiles"
460 rm -rf $initdir/$_conf_dir
461 if ! cp -ar $_conf_dir $initdir/$_conf_dir; then
462 dfatal
"Failed to copy $_conf_dir"
466 touch $initdir/.autorelabel
467 mkdir
-p $initdir/usr
/lib
/systemd
/tests
/testdata
/units
/basic.target.wants
468 ln -sf ..
/autorelabel.service
$initdir/usr
/lib
/systemd
/tests
/testdata
/units
/basic.target.wants
/
470 dracut_install
$_fixfiles_tools
471 dracut_install fixfiles
472 dracut_install sestatus
476 if ! type -p valgrind
; then
477 dfatal
"Failed to install valgrind"
481 local _valgrind_bins
=$
(strace
-e execve valgrind
/bin
/true
2>&1 >/dev
/null | perl
-lne 'print $1 if /^execve\("([^"]+)"/')
482 dracut_install
$_valgrind_bins
484 local _valgrind_libs
=$
(LD_DEBUG
=files valgrind
/bin
/true
2>&1 >/dev
/null | perl
-lne 'print $1 if m{calling init: (/.*vgpreload_.*)}')
485 dracut_install
$_valgrind_libs
487 local _valgrind_dbg_and_supp
=$
(
488 strace
-e open valgrind
/bin
/true
2>&1 >/dev
/null |
489 perl
-lne 'if (my ($fname) = /^open\("([^"]+).*= (?!-)\d+/) { print $fname if $fname =~ /debug|\.supp$/ }'
491 dracut_install
$_valgrind_dbg_and_supp
494 create_valgrind_wrapper
() {
495 local _valgrind_wrapper
=$initdir/$ROOTLIBDIR/systemd-under-valgrind
496 ddebug
"Create $_valgrind_wrapper"
497 cat >$_valgrind_wrapper <<EOF
500 mount -t proc proc /proc
501 exec valgrind --leak-check=full --log-file=/valgrind.out $ROOTLIBDIR/systemd "\$@"
503 chmod 0755 $_valgrind_wrapper
506 create_asan_wrapper
() {
507 local _asan_wrapper
=$initdir/$ROOTLIBDIR/systemd-under-asan
508 local _asan_rt_pattern
509 ddebug
"Create $_asan_wrapper"
511 case "$ASAN_COMPILER" in
513 _asan_rt_pattern
="*libasan*"
516 _asan_rt_pattern
="libclang_rt.asan-*"
517 # Install llvm-symbolizer to generate useful reports
518 # See: https://clang.llvm.org/docs/AddressSanitizer.html#symbolizing-the-reports
519 dracut_install
"llvm-symbolizer"
522 dfail
"Unsupported compiler: $ASAN_COMPILER"
526 cat >$_asan_wrapper <<EOF
531 DEFAULT_ASAN_OPTIONS=${ASAN_OPTIONS:-strict_string_checks=1:detect_stack_use_after_return=1:check_initialization_order=1:strict_init_order=1}
532 DEFAULT_UBSAN_OPTIONS=${UBSAN_OPTIONS:-print_stacktrace=1:print_summary=1:halt_on_error=1}
533 DEFAULT_ENVIRONMENT="ASAN_OPTIONS=\$DEFAULT_ASAN_OPTIONS UBSAN_OPTIONS=\$DEFAULT_UBSAN_OPTIONS"
535 # As right now bash is the PID 1, we can't expect PATH to have a sane value.
536 # Let's make one to prevent unexpected "<bin> not found" issues in the future
537 export PATH="/sbin:/bin:/usr/sbin:/usr/bin"
539 mount -t proc proc /proc
540 mount -t sysfs sysfs /sys
541 mount -o remount,rw /
543 PATH_TO_ASAN=\$(find / -name '$_asan_rt_pattern' | sed 1q)
544 if [[ "\$PATH_TO_ASAN" ]]; then
545 # A lot of services (most notably dbus) won't start without preloading libasan
546 # See https://github.com/systemd/systemd/issues/5004
547 DEFAULT_ENVIRONMENT="\$DEFAULT_ENVIRONMENT LD_PRELOAD=\$PATH_TO_ASAN"
548 # Let's add the ASan DSO's path to the dynamic linker's cache. This is pretty
549 # unnecessary for gcc & libasan, however, for clang this is crucial, as its
550 # runtime ASan DSO is in a non-standard (library) path.
551 echo \${PATH_TO_ASAN%/*} > /etc/ld.so.conf.d/asan-path-override.conf
554 echo DefaultEnvironment=\$DEFAULT_ENVIRONMENT >>/etc/systemd/system.conf
555 echo DefaultTimeoutStartSec=180s >>/etc/systemd/system.conf
556 echo DefaultStandardOutput=journal+console >>/etc/systemd/system.conf
558 # ASAN and syscall filters aren't compatible with each other.
559 find / -name '*.service' -type f | xargs sed -i 's/^\\(MemoryDeny\\|SystemCall\\)/#\\1/'
561 # The redirection of ASAN reports to a file prevents them from ending up in /dev/null.
562 # But, apparently, sometimes it doesn't work: https://github.com/google/sanitizers/issues/886.
563 JOURNALD_CONF_DIR=/etc/systemd/system/systemd-journald.service.d
564 mkdir -p "\$JOURNALD_CONF_DIR"
565 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"
567 # Sometimes UBSan sends its reports to stderr regardless of what is specified in log_path
568 # Let's try to catch them by redirecting stderr (and stdout just in case) to a file
569 # See https://github.com/systemd/systemd/pull/12524#issuecomment-491108821
570 printf "[Service]\nStandardOutput=file:/systemd-journald.out\n" >"\$JOURNALD_CONF_DIR/out.conf"
572 # 90s isn't enough for some services to finish when literally everything is run
573 # under ASan+UBSan in containers, which, in turn, are run in VMs.
574 # Let's limit which environments such services should be executed in.
575 mkdir -p /etc/systemd/system/systemd-hwdb-update.service.d
576 printf "[Unit]\nConditionVirtualization=container\n\n[Service]\nTimeoutSec=240s\n" >/etc/systemd/system/systemd-hwdb-update.service.d/env-override.conf
578 # Let's override another hard-coded timeout that kicks in too early
579 mkdir -p /etc/systemd/system/systemd-journal-flush.service.d
580 printf "[Service]\nTimeoutSec=180s\n" >/etc/systemd/system/systemd-journal-flush.service.d/timeout.conf
582 # The 'mount' utility doesn't behave well under libasan, causing unexpected
583 # fails during boot and subsequent test results check:
584 # bash-5.0# mount -o remount,rw -v /
585 # mount: /dev/sda1 mounted on /.
588 # Let's workaround this by clearing the previously set LD_PRELOAD env variable,
589 # so the libasan library is not loaded for this particular service
591 local _dropin_dir="/etc/systemd/system/\$1.service.d"
592 mkdir -p "\$_dropin_dir"
593 printf "[Service]\nUnsetEnvironment=LD_PRELOAD\n" >"\$_dropin_dir/unset_ld_preload.conf"
596 unset_ld_preload systemd-remount-fs
597 unset_ld_preload testsuite-
599 export ASAN_OPTIONS=\$DEFAULT_ASAN_OPTIONS:log_path=/systemd.asan.log UBSAN_OPTIONS=\$DEFAULT_UBSAN_OPTIONS
600 exec $ROOTLIBDIR/systemd "\$@"
603 chmod 0755 $_asan_wrapper
606 create_strace_wrapper
() {
607 local _strace_wrapper
=$initdir/$ROOTLIBDIR/systemd-under-strace
608 ddebug
"Create $_strace_wrapper"
609 cat >$_strace_wrapper <<EOF
612 exec strace -D -o /strace.out $ROOTLIBDIR/systemd "\$@"
614 chmod 0755 $_strace_wrapper
618 dracut_install
/sbin
/fsck
*
619 dracut_install
-o /bin
/fsck
*
621 # fskc.reiserfs calls reiserfsck. so, install it
622 dracut_install
-o reiserfsck
626 instmods dm_crypt
=crypto
628 if [[ "$LOOKS_LIKE_DEBIAN" ]]; then
629 # dmsetup installs 55-dm and 60-persistent-storage-dm on Debian/Ubuntu
630 # and since buster/bionic 95-dm-notify.rules
631 # see https://gitlab.com/debian-lvm/lvm2/blob/master/debian/patches/udev.patch
632 inst_rules
55-dm.rules
60-persistent-storage-dm.rules
95-dm-notify.rules
634 inst_rules
10-dm.rules
13-dm-disk.rules
95-dm-notify.rules
639 # install compiled files
640 local _ninja_bin
=$
(type -P ninja ||
type -P ninja-build
)
641 if [[ -z "$_ninja_bin" ]]; then
642 dfatal
"ninja was not found"
645 (set -x; DESTDIR
=$initdir "$_ninja_bin" -C $BUILD_DIR install)
646 # remove unneeded documentation
647 rm -fr $initdir/usr
/share
/{man
,doc
}
648 # we strip binaries since debug symbols increase binaries size a lot
649 # and it could fill the available space
652 [[ "$LOOKS_LIKE_SUSE" ]] && setup_suse
654 # enable debug logging in PID1
655 echo LogLevel
=debug
>> $initdir/etc
/systemd
/system.conf
656 # store coredumps in journal
657 echo Storage
=journal
>> $initdir/etc
/systemd
/coredump.conf
662 local rpath
=$
(objdump
-p "$_bin" 2>/dev
/null |
awk "/R(UN)?PATH/ { print \"$initdir\" \$2 }" |
paste -sd :)
664 if [ -z "$rpath" ] ; then
671 install_missing_libraries
() {
672 # install possible missing libraries
673 for i
in $initdir{,/usr
}/{sbin
,bin
}/* $initdir{,/usr
}/lib
/systemd
/{,tests
/{,manual
/,unsafe
/}}*; do
674 LD_LIBRARY_PATH
="${LD_LIBRARY_PATH:+$LD_LIBRARY_PATH:}$(get_ldpath $i)" inst_libs
$i
679 if [ -n "${LOOPDEV}" ]; then
680 ddebug
"losetup -d $LOOPDEV"
681 losetup
-d "${LOOPDEV}"
686 trap cleanup_loopdev EXIT INT QUIT PIPE
688 create_empty_image
() {
689 if [ -z "$IMAGE_NAME" ]; then
690 echo "create_empty_image: \$IMAGE_NAME not set"
695 if [[ "$STRIP_BINARIES" = "no" ]]; then
699 echo "Setting up $IMAGE_PUBLIC (${_size} MB)"
700 rm -f "$IMAGE_PRIVATE" "$IMAGE_PUBLIC"
702 # Create the blank file to use as a root filesystem
703 truncate
-s "${_size}M" "$IMAGE_PUBLIC"
705 LOOPDEV
=$
(losetup
--show -P -f "$IMAGE_PUBLIC")
706 [ -b "$LOOPDEV" ] ||
return 1
707 sfdisk
"$LOOPDEV" <<EOF
714 local _label
="-L systemd.${name}"
715 # mkfs.reiserfs doesn't know -L. so, use --label instead
716 [[ "$FSTYPE" == "reiserfs" ]] && _label
="--label systemd.${name}"
717 mkfs
-t "${FSTYPE}" ${_label} "${LOOPDEV}p1" -q; ret
=$?
718 if [ $ret -ne 0 ] ; then
719 dfatal
"Failed to mkfs -t ${FSTYPE}"
725 if [ -z "${LOOPDEV}" ]; then
726 [ -e "$IMAGE_PRIVATE" ] && image
="$IMAGE_PRIVATE" || image
="$IMAGE_PUBLIC"
727 LOOPDEV
=$
(losetup
--show -P -f "$image")
728 [ -b "$LOOPDEV" ] ||
return 1
733 if ! mountpoint
-q $initdir; then
735 mount
${LOOPDEV}p1
$initdir
736 TEST_SETUP_CLEANUP_ROOTDIR
=1
741 # only umount if create_empty_image_rootdir() was called to mount it
742 [[ -z $TEST_SETUP_CLEANUP_ROOTDIR ]] || _umount_dir
$initdir
746 # unmount the loopback device from all places. Otherwise we risk file
748 for device
in $
(losetup
-l |
awk '$6=="'"$IMAGE_PUBLIC"'" {print $1}'); do
749 ddebug
"Unmounting all uses of $device"
750 mount |
awk '/^'"${device}"'p/{print $1}' |
xargs --no-run-if-empty umount
-v
754 create_empty_image_rootdir
() {
759 check_asan_reports
() {
763 if [[ "$IS_BUILT_WITH_ASAN" = "yes" ]]; then
765 if [[ -e "$root/systemd.asan.log.1" ]]; then
766 cat "$root/systemd.asan.log.1"
770 journald_report
=$
(find "$root" -name "systemd-journald.*san.log*" -exec cat {} \
;)
771 if [[ ! -z "$journald_report" ]]; then
772 printf "%s\n" "$journald_report"
773 cat "$root/systemd-journald.out" ||
:
778 "$JOURNALCTL" -D "$root/var/log/journal" | perl
-alne '
780 %services_to_ignore = (
781 "dbus-daemon" => undef,
784 print $2 if /\s(\S*)\[(\d+)\]:\s*SUMMARY:\s+\w+Sanitizer/ && !exists $services_to_ignore{$1}'
786 if [[ ! -z "$pids" ]]; then
789 "$JOURNALCTL" -D "$root/var/log/journal" _PID
=$pid --no-pager
798 if [ -n "${ARTIFACT_DIRECTORY}" ]; then
799 dest
="${ARTIFACT_DIRECTORY}/${testname}.journal"
801 dest
="$TESTDIR/system.journal"
805 $SYSTEMD_JOURNAL_REMOTE \
807 --getter="$JOURNALCTL -o export -D $j"
809 if [ -n "${TEST_SHOW_JOURNAL}" ]; then
811 $JOURNALCTL --no-pager -o short-monotonic
--no-hostname --priority=${TEST_SHOW_JOURNAL} -D $j
817 # we want to print this sometime later, so save this in a variable
818 JOURNAL_LIST
="$(ls -l $dest*)"
821 check_result_nspawn
() {
823 local journald_report
=""
825 [[ -e $1/testok
]] && ret
=0
826 [[ -f $1/failed
]] && cp -a $1/failed
$TESTDIR
827 save_journal
$1/var
/log
/journal
828 [[ -f $TESTDIR/failed
]] && cat $TESTDIR/failed
830 test -s $TESTDIR/failed
&& ret
=$
(($ret+1))
831 [ -n "$TIMED_OUT" ] && ret
=$
(($ret+1))
832 check_asan_reports
"$1" || ret
=$
(($ret+1))
837 # can be overridden in specific test
838 check_result_qemu
() {
841 [[ -e $initdir/testok
]] && ret
=0
842 [[ -f $initdir/failed
]] && cp -a $initdir/failed
$TESTDIR
843 save_journal
$initdir/var
/log
/journal
844 check_asan_reports
"$initdir" || ret
=$
(($ret+1))
846 [[ -f $TESTDIR/failed
]] && cat $TESTDIR/failed
848 test -s $TESTDIR/failed
&& ret
=$
(($ret+1))
849 [ -n "$TIMED_OUT" ] && ret
=$
(($ret+1))
854 if [[ "$STRIP_BINARIES" = "no" ]]; then
855 ddebug
"Don't strip binaries"
858 ddebug
"Strip binaries"
859 find "$initdir" -executable -not -path '*/lib/modules/*.ko' -type f | \
860 xargs strip
--strip-unneeded |
& \
861 grep -vi 'file format not recognized' | \
866 mkdir
-p $initdir/etc
/rc.d
867 cat >$initdir/etc
/rc.d
/rc.
local <<EOF
871 chmod 0755 $initdir/etc
/rc.d
/rc.
local
875 ddebug
"install any Execs from the service files"
877 export PKG_CONFIG_PATH
=$BUILD_DIR/src
/core
/
878 systemdsystemunitdir
=$
(pkg-config
--variable=systemdsystemunitdir systemd
)
879 systemduserunitdir
=$
(pkg-config
--variable=systemduserunitdir systemd
)
880 sed -r -n 's|^Exec[a-zA-Z]*=[@+!-]*([^ ]+).*|\1|gp' $initdir/{$systemdsystemunitdir,$systemduserunitdir}/*.service \
881 |
sort -u |
while read i
; do
882 # some {rc,halt}.local scripts and programs are okay to not exist, the rest should
883 # also, plymouth is pulled in by rescue.service, but even there the exit code
884 # is ignored; as it's not present on some distros, don't fail if it doesn't exist
885 dinfo
"Attempting to install $i"
886 inst
$i ||
[ "${i%.local}" != "$i" ] || [ "${i%systemd-update-done}" != "$i" ] || [ "${i##*/}" == "plymouth" ]
891 generate_module_dependencies
() {
892 if [[ -d $initdir/lib
/modules
/$KERNEL_VER ]] && \
893 ! depmod
-a -b "$initdir" $KERNEL_VER; then
894 dfatal
"\"depmod -a $KERNEL_VER\" failed."
899 install_depmod_files
() {
900 inst
/lib
/modules
/$KERNEL_VER/modules.order
901 inst
/lib
/modules
/$KERNEL_VER/modules.
builtin
905 # install plymouth, if found... else remove plymouth service files
906 # if [ -x /usr/libexec/plymouth/plymouth-populate-initrd ]; then
907 # PLYMOUTH_POPULATE_SOURCE_FUNCTIONS="$TEST_BASE_DIR/test-functions" \
908 # /usr/libexec/plymouth/plymouth-populate-initrd -t $initdir
909 # dracut_install plymouth plymouthd
911 rm -f $initdir/{usr
/lib
,lib
,etc
}/systemd
/system
/plymouth
* $initdir/{usr
/lib
,lib
,etc
}/systemd
/system
/*/plymouth
*
915 install_ld_so_conf
() {
916 cp -a /etc
/ld.so.conf
* $initdir/etc
917 ldconfig
-r "$initdir"
921 # create unprivileged user for user manager tests
922 mkdir
-p $initdir/etc
/sysusers.d
923 cat >$initdir/etc
/sysusers.d
/testuser.conf
<<EOF
924 u testuser 4711 "Test User" /home/testuser
927 mkdir
-p $initdir/home
/testuser
-m 0700
928 chown
4711:4711 $initdir/home
/testuser
931 install_config_files
() {
932 inst
/etc
/sysconfig
/init ||
:
938 inst_any
/etc
/nsswitch.conf
/usr
/etc
/nsswitch.conf
939 inst
/etc
/pam.conf ||
:
942 # we want an empty environment
943 > $initdir/etc
/environment
944 > $initdir/etc
/machine-id
947 echo systemd-testsuite
> $initdir/etc
/hostname
949 # let's set up just one image with the traditional verbose output
950 if [ ${IMAGE_NAME} != "basic" ]; then
951 mkdir
-p $initdir/etc
/systemd
/system.conf.d
952 echo -e '[Manager]\nStatusUnitFormat=name' >$initdir/etc
/systemd
/system.conf.d
/status.conf
956 install_basic_tools
() {
957 dracut_install
"${BASICTOOLS[@]}"
958 dracut_install
-o sushell
959 # in Debian ldconfig is just a shell script wrapper around ldconfig.real
960 dracut_install
-o ldconfig.real
963 install_debug_tools
() {
964 dracut_install
"${DEBUGTOOLS[@]}"
966 if [[ $INTERACTIVE_DEBUG ]]; then
967 # Set default TERM from vt220 to linux, so at least basic key shortcuts work
968 local _getty_override
="$initdir/etc/systemd/system/serial-getty@.service.d"
969 mkdir
-p "$_getty_override"
970 echo -e "[Service]\nEnvironment=TERM=linux" > "$_getty_override/default-TERM.conf"
972 cat > "$initdir/etc/motd" << EOF
973 To adjust the terminal size use:
983 # install libnss_files for login
984 NSS_LIBS
=$
(LD_DEBUG
=files getent passwd
2>&1 >/dev
/null |
sed -n '/calling init: .*libnss_/ {s!^.* /!/!; p}')
985 dracut_install
$NSS_LIBS
989 inst
$ROOTLIBDIR/system
/dbus.socket
991 # Newer Fedora versions use dbus-broker by default. Let's install it if it's available.
992 if [ -f $ROOTLIBDIR/system
/dbus-broker.service
]; then
993 inst
$ROOTLIBDIR/system
/dbus-broker.service
994 inst_symlink
/etc
/systemd
/system
/dbus.service
995 inst
/usr
/bin
/dbus-broker
996 inst
/usr
/bin
/dbus-broker-launch
997 elif [ -f $ROOTLIBDIR/system
/dbus-daemon.service
]; then
998 # Fedora rawhide replaced dbus.service with dbus-daemon.service
999 inst
$ROOTLIBDIR/system
/dbus-daemon.service
1001 inst_symlink
/etc
/systemd
/system
/dbus.service
1003 inst
$ROOTLIBDIR/system
/dbus.service
1007 /etc
/dbus-1
/usr
/share
/dbus-1
-xtype f \
1008 |
while read file; do
1012 # setup policy for Type=dbus test
1013 mkdir
-p $initdir/etc
/dbus-
1/system.d
1014 cat > $initdir/etc
/dbus-
1/system.d
/systemd.
test.ExecStopPost.conf
<<EOF
1015 <?xml version="1.0"?>
1016 <!DOCTYPE busconfig PUBLIC "-//freedesktop//DTD D-BUS Bus Configuration 1.0//EN"
1017 "http://www.freedesktop.org/standards/dbus/1.0/busconfig.dtd">
1019 <policy user="root">
1020 <allow own="systemd.test.ExecStopPost"/>
1026 install_user_dbus
() {
1027 inst
$ROOTLIBDIR/user
/dbus.socket
1028 inst_symlink
/usr
/lib
/systemd
/user
/sockets.target.wants
/dbus.socket || inst_symlink
/etc
/systemd
/user
/sockets.target.wants
/dbus.socket
1030 # Append the After= dependency on dbus in case it isn't already set up
1031 mkdir
-p "$initdir/etc/systemd/system/user@.service.d/"
1032 cat <<EOF >"$initdir/etc/systemd/system/user@.service.d/dbus.conf"
1037 # Newer Fedora versions use dbus-broker by default. Let's install it if it's available.
1038 if [ -f $ROOTLIBDIR/user
/dbus-broker.service
]; then
1039 inst
$ROOTLIBDIR/user
/dbus-broker.service
1040 inst_symlink
/etc
/systemd
/user
/dbus.service
1041 elif [ -f $ROOTLIBDIR/system
/dbus-daemon.service
]; then
1042 # Fedora rawhide replaced dbus.service with dbus-daemon.service
1043 inst
$ROOTLIBDIR/user
/dbus-daemon.service
1045 inst_symlink
/etc
/systemd
/user
/dbus.service
1047 inst
$ROOTLIBDIR/user
/dbus.service
1053 if [[ "$LOOKS_LIKE_DEBIAN" ]] && type -p dpkg-architecture
&>/dev
/null
; then
1054 find "/lib/$(dpkg-architecture -qDEB_HOST_MULTIARCH)/security" -xtype f
1056 find /lib
*/security
-xtype f
1058 find /etc
/pam.d
/etc
/security
-xtype f
1059 ) |
while read file; do
1063 # pam_unix depends on unix_chkpwd.
1064 # see http://www.linux-pam.org/Linux-PAM-html/sag-pam_unix.html
1065 dracut_install
-o unix_chkpwd
1067 [[ "$LOOKS_LIKE_DEBIAN" ]] &&
1068 cp /etc
/pam.d
/systemd-user
$initdir/etc
/pam.d
/
1070 # set empty root password for easy debugging
1071 sed -i 's/^root:x:/root::/' $initdir/etc
/passwd
1075 # The first three paths may be deprecated.
1076 # It seems now the last two paths are used by many distributions.
1078 /usr
/lib
/kbd
/keymaps
/include
/* \
1079 /usr
/lib
/kbd
/keymaps
/i386
/include
/* \
1080 /usr
/lib
/kbd
/keymaps
/i386
/qwerty
/us.
* \
1081 /usr
/lib
/kbd
/keymaps
/legacy
/include
/* \
1082 /usr
/lib
/kbd
/keymaps
/legacy
/i386
/qwerty
/us.
*; do
1083 [[ -f $i ]] ||
continue
1087 # When it takes any argument, then install more keymaps.
1088 if [[ -n $1 ]]; then
1090 /usr
/lib
/kbd
/keymaps
/i386
/*/* \
1091 /usr
/lib
/kbd
/keymaps
/legacy
/i386
/*/*; do
1092 [[ -f $i ]] ||
continue
1098 install_zoneinfo
() {
1099 inst_any
/usr
/share
/zoneinfo
/Asia
/Seoul
1100 inst_any
/usr
/share
/zoneinfo
/Asia
/Vladivostok
1101 inst_any
/usr
/share
/zoneinfo
/Australia
/Sydney
1102 inst_any
/usr
/share
/zoneinfo
/Europe
/Berlin
1103 inst_any
/usr
/share
/zoneinfo
/Europe
/Kiev
1104 inst_any
/usr
/share
/zoneinfo
/Pacific
/Auckland
1105 inst_any
/usr
/share
/zoneinfo
/Pacific
/Honolulu
1106 inst_any
/usr
/share
/zoneinfo
/CET
1107 inst_any
/usr
/share
/zoneinfo
/EET
1108 inst_any
/usr
/share
/zoneinfo
/UTC
1113 /usr
/lib
/kbd
/consolefonts
/eurlatgr
* \
1114 /usr
/lib
/kbd
/consolefonts
/latarcyrheb-sun16
*; do
1115 [[ -f $i ]] ||
continue
1120 install_terminfo
() {
1121 for _terminfodir
in /lib
/terminfo
/etc
/terminfo
/usr
/share
/terminfo
; do
1122 [ -f ${_terminfodir}/l
/linux
] && break
1124 dracut_install
-o ${_terminfodir}/l
/linux
1127 has_user_dbus_socket
() {
1128 if [ -f /usr
/lib
/systemd
/user
/dbus.socket
] ||
[ -f /etc
/systemd
/user
/dbus.socket
]; then
1131 echo "Per-user instances are not supported. Skipping..."
1136 setup_nspawn_root
() {
1137 if [ -z "${initdir}" ]; then
1138 dfatal
"\$initdir not defined"
1142 rm -rf "$TESTDIR/unprivileged-nspawn-root"
1144 if [[ "$RUN_IN_UNPRIVILEGED_CONTAINER" = "yes" ]]; then
1145 ddebug
"cp -ar $initdir $TESTDIR/unprivileged-nspawn-root"
1146 cp -ar $initdir $TESTDIR/unprivileged-nspawn-root
1150 setup_basic_dirs
() {
1151 mkdir
-p $initdir/run
1152 mkdir
-p $initdir/etc
/systemd
/system
1153 mkdir
-p $initdir/var
/log
/journal
1155 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
1156 if [ -L "/$d" ]; then
1163 ln -sfn /run
"$initdir/var/run"
1164 ln -sfn /run
/lock
"$initdir/var/lock"
1167 mask_supporting_services
() {
1168 # mask some services that we do not want to run in these tests
1169 ln -fs /dev
/null
$initdir/etc
/systemd
/system
/systemd-hwdb-update.service
1170 ln -fs /dev
/null
$initdir/etc
/systemd
/system
/systemd-journal-catalog-update.service
1171 ln -fs /dev
/null
$initdir/etc
/systemd
/system
/systemd-networkd.service
1172 ln -fs /dev
/null
$initdir/etc
/systemd
/system
/systemd-networkd.socket
1173 ln -fs /dev
/null
$initdir/etc
/systemd
/system
/systemd-resolved.service
1178 local _so_regex
='([^ ]*/lib[^/]*/[^ ]*\.so[^ ]*)'
1181 LC_ALL
=C ldd
"$_bin" 2>/dev
/null |
while read _line
; do
1182 [[ $_line = 'not a dynamic executable' ]] && break
1184 if [[ $_line =~
$_so_regex ]]; then
1185 _file
=${BASH_REMATCH[1]}
1186 [[ -e ${initdir}/$_file ]] && continue
1187 inst_library
"$_file"
1191 if [[ $_line =~ not\ found
]]; then
1192 dfatal
"Missing a shared library required by $_bin."
1193 dfatal
"Run \"ldd $_bin\" to find out what it is."
1195 dfatal
"dracut cannot create an initrd."
1202 # make sure we don't get a stale LOOPDEV value from old times
1204 [[ -e $STATEFILE ]] && .
$STATEFILE
1206 if [[ ! -d "$TESTDIR" ]]; then
1207 if [[ -z "$TESTDIR" ]]; then
1208 TESTDIR
=$
(mktemp
--tmpdir=/var
/tmp
-d -t systemd-test.XXXXXX
)
1213 cat >$STATEFILE<<EOF
1219 IMAGE_PRIVATE
="${TESTDIR}/${IMAGE_NAME}.img"
1220 IMAGE_PUBLIC
="${IMAGESTATEDIR}/${IMAGE_NAME}.img"
1224 initdir
=$TESTDIR/root
1229 ## @brief Converts numeric logging level to the first letter of level name.
1231 # @param lvl Numeric logging level in range from 1 to 6.
1232 # @retval 1 if @a lvl is out of range.
1233 # @retval 0 if @a lvl is correct.
1234 # @result Echoes first letter of level name.
1247 ## @brief Internal helper function for _do_dlog()
1249 # @param lvl Numeric logging level.
1250 # @param msg Message.
1251 # @retval 0 It's always returned, even if logging failed.
1253 # @note This function is not supposed to be called manually. Please use
1254 # dtrace(), ddebug(), or others instead which wrap this one.
1256 # This function calls _do_dlog() either with parameter msg, or if
1257 # none is given, it will read standard input and will use every line as
1261 # dwarn "This is a warning"
1262 # echo "This is a warning" | dwarn
1263 LOG_LEVEL
=${LOG_LEVEL:-4}
1266 [ -z "$LOG_LEVEL" ] && return 0
1267 [ $1 -le $LOG_LEVEL ] ||
return 0
1268 local lvl
="$1"; shift
1269 local lvlc
=$
(_lvl2char
"$lvl") ||
return 0
1271 if [ $# -ge 1 ]; then
1275 echo "$lvlc: " "$line"
1280 ## @brief Logs message at TRACE level (6)
1282 # @param msg Message.
1283 # @retval 0 It's always returned, even if logging failed.
1287 [ -n "$debug" ] && set -x ||
:
1290 ## @brief Logs message at DEBUG level (5)
1292 # @param msg Message.
1293 # @retval 0 It's always returned, even if logging failed.
1297 # [ -n "$debug" ] && set -x || :
1300 ## @brief Logs message at INFO level (4)
1302 # @param msg Message.
1303 # @retval 0 It's always returned, even if logging failed.
1307 [ -n "$debug" ] && set -x ||
:
1310 ## @brief Logs message at WARN level (3)
1312 # @param msg Message.
1313 # @retval 0 It's always returned, even if logging failed.
1317 [ -n "$debug" ] && set -x ||
:
1320 ## @brief Logs message at ERROR level (2)
1322 # @param msg Message.
1323 # @retval 0 It's always returned, even if logging failed.
1327 # [ -n "$debug" ] && set -x || :
1330 ## @brief Logs message at FATAL level (1)
1332 # @param msg Message.
1333 # @retval 0 It's always returned, even if logging failed.
1337 [ -n "$debug" ] && set -x ||
:
1341 # Generic substring function. If $2 is in $1, return 0.
1342 strstr
() { [ "${1#*$2*}" != "$1" ]; }
1344 # normalize_path <path>
1345 # Prints the normalized path, where it removes any duplicated
1346 # and trailing slashes.
1348 # $ normalize_path ///test/test//
1352 set -- "${1//+(\/)//}"
1357 # convert_abs_rel <from> <to>
1358 # Prints the relative path, when creating a symlink to <to> from <from>.
1360 # $ convert_abs_rel /usr/bin/test /bin/test-2
1362 # $ ln -s $(convert_abs_rel /usr/bin/test /bin/test-2) /usr/bin/test
1364 local __current __absolute __abssize __cursize __newpath
1365 local -i __i __level
1367 set -- "$(normalize_path "$1")" "$(normalize_path "$2")"
1369 # corner case #1 - self looping link
1370 [[ "$1" == "$2" ]] && { echo "${1##*/}"; return; }
1372 # corner case #2 - own dir link
1373 [[ "${1%/*}" == "$2" ]] && { echo "."; return; }
1375 IFS
="/" __current
=($1)
1376 IFS
="/" __absolute
=($2)
1378 __abssize
=${#__absolute[@]}
1379 __cursize
=${#__current[@]}
1381 while [[ ${__absolute[__level]} == ${__current[__level]} ]]
1384 if (( __level
> __abssize || __level
> __cursize
))
1390 for ((__i
= __level
; __i
< __cursize-1
; __i
++))
1392 if ((__i
> __level
))
1394 __newpath
=$__newpath"/"
1396 __newpath
=$__newpath".."
1399 for ((__i
= __level
; __i
< __abssize
; __i
++))
1401 if [[ -n $__newpath ]]
1403 __newpath
=$__newpath"/"
1405 __newpath
=$__newpath${__absolute[__i]}
1412 # Install a directory, keeping symlinks as on the original system.
1413 # Example: if /lib points to /lib64 on the host, "inst_dir /lib/file"
1414 # will create ${initdir}/lib64, ${initdir}/lib64/file,
1415 # and a symlink ${initdir}/lib -> lib64.
1417 [[ -e ${initdir}/"$1" ]] && return 0 # already there
1419 local _dir
="$1" _part
="${1%/*}" _file
1420 while [[ "$_part" != "${_part%/*}" ]] && ! [[ -e "${initdir}/${_part}" ]]; do
1425 # iterate over parent directories
1426 for _file
in $_dir; do
1427 [[ -e "${initdir}/$_file" ]] && continue
1428 if [[ -L $_file ]]; then
1429 inst_symlink
"$_file"
1432 mkdir
-m 0755 -p "${initdir}/$_file" ||
return 1
1433 [[ -e "$_file" ]] && chmod --reference="$_file" "${initdir}/$_file"
1434 chmod u
+w
"${initdir}/$_file"
1439 # $1 = file to copy to ramdisk
1440 # $2 (optional) Name for the file on the ramdisk
1441 # Location of the image dir is assumed to be $initdir
1442 # We never overwrite the target if it exists.
1444 [[ -f "$1" ]] ||
return 1
1445 strstr
"$1" "/" ||
return 1
1447 local _src
=$1 target
="${2:-$1}"
1448 if ! [[ -d ${initdir}/$target ]]; then
1449 [[ -e ${initdir}/$target ]] && return 0
1450 [[ -L ${initdir}/$target ]] && return 0
1451 [[ -d "${initdir}/${target%/*}" ]] || inst_dir "${target%/*}"
1453 # install checksum files also
1454 if [[ -e "${_src%/*}/.${_src##*/}.hmac" ]]; then
1455 inst
"${_src%/*}/.${_src##*/}.hmac" "${target%/*}/.${target##*/}.hmac"
1457 ddebug
"Installing $_src"
1458 cp --sparse=always
-pfL "$_src" "${initdir}/$target"
1461 # find symlinks linked to given library file
1463 # Function searches for symlinks by stripping version numbers appended to
1464 # library filename, checks if it points to the same target and finally
1465 # prints the list of symlinks to stdout.
1468 # rev_lib_symlinks libfoo.so.8.1
1469 # output: libfoo.so.8 libfoo.so
1470 # (Only if libfoo.so.8 and libfoo.so exists on host system.)
1471 rev_lib_symlinks
() {
1472 [[ ! $1 ]] && return 0
1474 local fn
="$1" orig
="$(readlink -f "$1")" links
=''
1476 [[ ${fn} =~ .
*\.so\..
* ]] ||
return 1
1478 until [[ ${fn##*.} == so
]]; do
1480 [[ -L ${fn} && $(readlink -f "${fn}") == ${orig} ]] && links+=" ${fn}"
1486 # Same as above, but specialized to handle dynamic libraries.
1487 # It handles making symlinks according to how the original library
1490 local _src="$1" _dest=${2:-$1} _lib _reallib _symlink
1491 strstr "$1" "/" || return 1
1492 [[ -e $initdir/$_dest ]] && return 0
1493 if [[ -L $_src ]]; then
1494 # install checksum files also
1495 if [[ -e "${_src%/*}/.
${_src##*/}.hmac
" ]]; then
1496 inst "${_src%/*}/.${_src##*/}.hmac" "${_dest%/*}/.${_dest##*/}.hmac
"
1498 _reallib=$(readlink -f "$_src")
1499 inst_simple "$_reallib" "$_reallib"
1500 inst_dir "${_dest%/*}"
1501 [[ -d "${_dest%/*}" ]] && _dest=$(readlink -f "${_dest%/*}")/${_dest##*/}
1502 ln -sfn $
(convert_abs_rel
"${_dest}" "${_reallib}") "${initdir}/${_dest}"
1504 inst_simple
"$_src" "$_dest"
1507 # Create additional symlinks. See rev_symlinks description.
1508 for _symlink
in $
(rev_lib_symlinks
$_src) $
(rev_lib_symlinks
$_reallib); do
1509 [[ -e $initdir/$_symlink ]] ||
{
1510 ddebug
"Creating extra symlink: $_symlink"
1511 inst_symlink
$_symlink
1516 # find a binary. If we were not passed the full path directly,
1517 # search in the usual places to find the binary.
1519 if [[ -z ${1##/*} ]]; then
1520 if [[ -x $1 ]] ||
{ strstr
"$1" ".so" && ldd
$1 &>/dev
/null
; }; then
1529 # Same as above, but specialized to install binary executables.
1530 # Install binary executable, and all shared library dependencies, if any.
1534 # In certain cases we might attempt to install a binary which is already
1535 # present in the test image, yet it's missing from the host system.
1536 # In such cases, let's check if the binary indeed exists in the image
1537 # before doing any other chcecks. If it does, immediately return with
1539 [[ $# -eq 1 && -e $initdir/$1 ]] && return 0
1541 _bin
=$
(find_binary
"$1") ||
return 1
1543 [[ -e $initdir/$_target ]] && return 0
1544 [[ -L $_bin ]] && inst_symlink
$_bin $_target && return 0
1546 local _so_regex
='([^ ]*/lib[^/]*/[^ ]*\.so[^ ]*)'
1548 LC_ALL
=C ldd
"$_bin" 2>/dev
/null |
while read _line
; do
1549 [[ $_line = 'not a dynamic executable' ]] && break
1551 if [[ $_line =~
$_so_regex ]]; then
1552 _file
=${BASH_REMATCH[1]}
1553 [[ -e ${initdir}/$_file ]] && continue
1554 inst_library
"$_file"
1558 if [[ $_line =~ not\ found
]]; then
1559 dfatal
"Missing a shared library required by $_bin."
1560 dfatal
"Run \"ldd $_bin\" to find out what it is."
1562 dfatal
"dracut cannot create an initrd."
1566 inst_simple
"$_bin" "$_target"
1569 # same as above, except for shell scripts.
1570 # If your shell script does not start with shebang, it is not a shell script.
1573 _bin
=$
(find_binary
"$1") ||
return 1
1575 local _line _shebang_regex
1576 read -r -n 80 _line
<"$_bin"
1577 # If debug is set, clean unprintable chars to prevent messing up the term
1578 [[ $debug ]] && _line
=$
(echo -n "$_line" |
tr -c -d '[:print:][:space:]')
1579 _shebang_regex
='(#! *)(/[^ ]+).*'
1580 [[ $_line =~
$_shebang_regex ]] ||
return 1
1581 inst
"${BASH_REMATCH[2]}" && inst_simple
"$_bin" "$@"
1584 # same as above, but specialized for symlinks
1586 local _src
=$1 _target
=${2:-$1} _realsrc
1587 strstr
"$1" "/" ||
return 1
1588 [[ -L $1 ]] ||
return 1
1589 [[ -L $initdir/$_target ]] && return 0
1590 _realsrc
=$
(readlink
-f "$_src")
1591 if ! [[ -e $initdir/$_realsrc ]]; then
1592 if [[ -d $_realsrc ]]; then
1593 inst_dir
"$_realsrc"
1598 [[ ! -e $initdir/${_target%/*} ]] && inst_dir
"${_target%/*}"
1599 [[ -d ${_target%/*} ]] && _target=$(readlink -f ${_target%/*})/${_target##*/}
1600 ln -sfn $
(convert_abs_rel
"${_target}" "${_realsrc}") "$initdir/$_target"
1603 # attempt to install any programs specified in a udev rule
1604 inst_rule_programs
() {
1607 if grep -qE 'PROGRAM==?"[^ "]+' "$1"; then
1608 for _prog
in $
(grep -E 'PROGRAM==?"[^ "]+' "$1" |
sed -r 's/.*PROGRAM==?"([^ "]+).*/\1/'); do
1609 if [ -x /lib
/udev
/$_prog ]; then
1610 _bin
=/lib
/udev
/$_prog
1612 _bin
=$
(find_binary
"$_prog") ||
{
1613 dinfo
"Skipping program $_prog using in udev rule $(basename $1) as it cannot be found"
1618 #dinfo "Installing $_bin due to it's use in the udev rule $(basename $1)"
1619 dracut_install
"$_bin"
1624 # udev rules always get installed in the same place, so
1625 # create a function to install them to make life simpler.
1627 local _target
=/etc
/udev
/rules.d _rule _found
1629 inst_dir
"/lib/udev/rules.d"
1631 for _rule
in "$@"; do
1632 if [ "${rule#/}" = "$rule" ]; then
1633 for r
in /lib
/udev
/rules.d
/etc
/udev
/rules.d
; do
1634 if [[ -f $r/$_rule ]]; then
1636 inst_simple
"$_found"
1637 inst_rule_programs
"$_found"
1641 for r
in '' .
/ $dracutbasedir/rules.d
/; do
1642 if [[ -f ${r}$_rule ]]; then
1644 inst_simple
"$_found" "$_target/${_found##*/}"
1645 inst_rule_programs
"$_found"
1648 [[ $_found ]] || dinfo
"Skipping udev rule: $_rule"
1653 # general purpose installation function
1654 # Same args as above.
1660 2) [[ ! $initdir && -d $2 ]] && export initdir
=$2
1661 [[ $initdir = $2 ]] && set $1;;
1662 3) [[ -z $initdir ]] && export initdir
=$2
1664 *) dfatal
"inst only takes 1 or 2 or 3 arguments"
1667 for _x
in inst_symlink inst_script inst_binary inst_simple
; do
1668 $_x "$@" && return 0
1673 # install any of listed files
1675 # If first argument is '-d' and second some destination path, first accessible
1676 # source is installed into this path, otherwise it will installed in the same
1677 # path as source. If none of listed files was installed, function return 1.
1678 # On first successful installation it returns with 0 status.
1682 # inst_any -d /bin/foo /bin/bar /bin/baz
1684 # Lets assume that /bin/baz exists, so it will be installed as /bin/foo in
1689 [[ $1 = '-d' ]] && to
="$2" && shift 2
1692 if [[ -e $f ]]; then
1693 [[ $to ]] && inst
"$f" "$to" && return 0
1694 inst
"$f" && return 0
1701 # dracut_install [-o ] <file> [<file> ... ]
1702 # Install <file> to the initramfs image
1703 # -o optionally install the <file> and don't fail, if it is not there
1706 if [[ $1 = '-o' ]]; then
1710 while (($# > 0)); do
1711 if ! inst
"$1" ; then
1712 if [[ $_optional = yes ]]; then
1713 dinfo
"Skipping program $1 as it cannot be found and is" \
1714 "flagged to be optional"
1716 dfatal
"Failed to install $1"
1724 # Install a single kernel module along with any firmware it may require.
1725 # $1 = full path to kernel module to install
1726 install_kmod_with_fw
() {
1727 # no need to go further if the module is already installed
1729 [[ -e "${initdir}/lib/modules/$KERNEL_VER/${1##*/lib/modules/$KERNEL_VER/}" ]] \
1732 [[ -e "$initdir/.kernelmodseen/${1##*/}" ]] && return 0
1734 if [[ $omit_drivers ]]; then
1735 local _kmod
=${1##*/}
1738 if [[ "$_kmod" =~
$omit_drivers ]]; then
1739 dinfo
"Omitting driver $_kmod"
1742 if [[ "${1##*/lib/modules/$KERNEL_VER/}" =~
$omit_drivers ]]; then
1743 dinfo
"Omitting driver $_kmod"
1748 [ -d "$initdir/.kernelmodseen" ] && \
1749 > "$initdir/.kernelmodseen/${1##*/}"
1751 inst_simple
"$1" "/lib/modules/$KERNEL_VER/${1##*/lib/modules/$KERNEL_VER/}" \
1754 local _modname
=${1##*/} _fwdir _found _fw
1755 _modname
=${_modname%.ko*}
1756 for _fw
in $
(modinfo
-k $KERNEL_VER -F firmware
$1 2>/dev
/null
); do
1758 for _fwdir
in $fw_dir; do
1759 if [[ -d $_fwdir && -f $_fwdir/$_fw ]]; then
1760 inst_simple
"$_fwdir/$_fw" "/lib/firmware/$_fw"
1764 if [[ $_found != yes ]]; then
1765 if ! grep -qe "\<${_modname//-/_}\>" /proc
/modules
; then
1766 dinfo
"Possible missing firmware \"${_fw}\" for kernel module" \
1767 "\"${_modname}.ko\""
1769 dwarn
"Possible missing firmware \"${_fw}\" for kernel module" \
1770 "\"${_modname}.ko\""
1777 # Do something with all the dependencies of a kernel module.
1778 # Note that kernel modules depend on themselves using the technique we use
1779 # $1 = function to call for each dependency we find
1780 # It will be passed the full path to the found kernel module
1781 # $2 = module to get dependencies for
1782 # rest of args = arguments to modprobe
1783 # _fderr specifies FD passed from surrounding scope
1784 for_each_kmod_dep
() {
1785 local _func
=$1 _kmod
=$2 _cmd _modpath _options _found
=0
1787 modprobe
"$@" --ignore-install --show-depends $_kmod 2>&${_fderr} |
(
1788 while read _cmd _modpath _options
; do
1789 [[ $_cmd = insmod
]] ||
continue
1790 $_func ${_modpath} ||
exit $?
1793 [[ $_found -eq 0 ]] && exit 1
1798 # filter kernel modules to install certain modules that meet specific
1800 # $1 = search only in subdirectory of /kernel/$1
1801 # $2 = function to call with module name to filter.
1802 # This function will be passed the full path to the module to test.
1803 # The behavior of this function can vary depending on whether $hostonly is set.
1804 # If it is, we will only look at modules that are already in memory.
1805 # If it is not, we will look at all kernel modules
1806 # This function returns the full filenames of modules that match $1
1807 filter_kernel_modules_by_path
() (
1808 local _modname _filtercmd
1809 if ! [[ $hostonly ]]; then
1810 _filtercmd
='find "$KERNEL_MODS/kernel/$1" "$KERNEL_MODS/extra"'
1811 _filtercmd
+=' "$KERNEL_MODS/weak-updates" -name "*.ko" -o -name "*.ko.gz"'
1812 _filtercmd
+=' -o -name "*.ko.xz"'
1813 _filtercmd
+=' 2>/dev/null'
1815 _filtercmd
='cut -d " " -f 1 </proc/modules|xargs modinfo -F filename '
1816 _filtercmd
+='-k $KERNEL_VER 2>/dev/null'
1818 for _modname
in $
(eval $_filtercmd); do
1820 *.ko
) "$2" "$_modname" && echo "$_modname";;
1821 *.ko.gz
) gzip -dc "$_modname" > $initdir/$$.ko
1822 $2 $initdir/$$.ko
&& echo "$_modname"
1823 rm -f $initdir/$$.ko
1825 *.ko.xz
) xz
-dc "$_modname" > $initdir/$$.ko
1826 $2 $initdir/$$.ko
&& echo "$_modname"
1827 rm -f $initdir/$$.ko
1832 find_kernel_modules_by_path
() (
1833 if ! [[ $hostonly ]]; then
1834 find "$KERNEL_MODS/kernel/$1" "$KERNEL_MODS/extra" "$KERNEL_MODS/weak-updates" \
1835 -name "*.ko" -o -name "*.ko.gz" -o -name "*.ko.xz" 2>/dev
/null
1837 cut
-d " " -f 1 </proc
/modules \
1838 |
xargs modinfo
-F filename
-k $KERNEL_VER 2>/dev
/null
1842 filter_kernel_modules
() {
1843 filter_kernel_modules_by_path drivers
"$1"
1846 find_kernel_modules
() {
1847 find_kernel_modules_by_path drivers
1850 # instmods [-c] <kernel module> [<kernel module> ... ]
1851 # instmods [-c] <kernel subsystem>
1852 # install kernel modules along with all their dependencies.
1853 # <kernel subsystem> can be e.g. "=block" or "=drivers/usb/storage"
1855 [[ $no_kernel = yes ]] && return
1856 # called [sub]functions inherit _fderr
1859 if [[ $1 = '-c' ]]; then
1864 function inst1mod
() {
1865 local _ret
=0 _mod
="$1"
1868 if [ -f $KERNEL_MODS/modules.
${_mod#=} ]; then
1869 ( [[ "$_mpargs" ]] && echo $_mpargs
1870 cat "${KERNEL_MODS}/modules.${_mod#=}" ) \
1873 ( [[ "$_mpargs" ]] && echo $_mpargs
1874 find "$KERNEL_MODS" -path "*/${_mod#=}/*" -type f
-printf '%f\n' ) \
1878 --*) _mpargs
+=" $_mod" ;;
1879 i2o_scsi
) return ;; # Do not load this diagnostic-only module
1882 # if we are already installed, skip this module and go on
1884 [[ -f "$initdir/.kernelmodseen/${_mod%.ko}.ko" ]] && return
1886 if [[ $omit_drivers ]] && [[ "$1" =~
$omit_drivers ]]; then
1887 dinfo
"Omitting driver ${_mod##$KERNEL_MODS}"
1890 # If we are building a host-specific initramfs and this
1891 # module is not already loaded, move on to the next one.
1892 [[ $hostonly ]] && ! grep -qe "\<${_mod//-/_}\>" /proc
/modules \
1893 && ! echo $add_drivers |
grep -qe "\<${_mod}\>" \
1896 # We use '-d' option in modprobe only if modules prefix path
1897 # differs from default '/'. This allows us to use Dracut with
1898 # old version of modprobe which doesn't have '-d' option.
1899 local _moddirname
=${KERNEL_MODS%%/lib/modules/*}
1900 [[ -n ${_moddirname} ]] && _moddirname
="-d ${_moddirname}/"
1902 # ok, load the module, all its dependencies, and any firmware
1904 for_each_kmod_dep install_kmod_with_fw
$_mod \
1905 --set-version $KERNEL_VER ${_moddirname} $_mpargs
1912 function instmods_1
() {
1914 if (($# == 0)); then # filenames from stdin
1916 inst1mod
"${_mod%.ko*}" ||
{
1917 if [ "$_check" = "yes" ]; then
1918 dfatal
"Failed to install $_mod"
1924 while (($# > 0)); do # filenames as arguments
1925 inst1mod
${1%.ko*} ||
{
1926 if [ "$_check" = "yes" ]; then
1927 dfatal
"Failed to install $1"
1936 local _ret _filter_not_found
='FATAL: Module .* not found.'
1938 # Capture all stderr from modprobe to _fderr. We could use {var}>...
1939 # redirections, but that would make dracut require bash4 at least.
1940 eval "( instmods_1 \"\$@\" ) ${_fderr}>&1" \
1941 |
while read line
; do [[ "$line" =~
$_filter_not_found ]] && echo $line ||
echo $line >&2 ;done | derror
1948 ln -fs ..
/usr
/bin
/systemctl
$initdir/bin
/
1949 ln -fs ..
/usr
/lib
/systemd
$initdir/lib
/
1950 inst_simple
"/usr/lib/systemd/system/haveged.service"
1954 if mountpoint
-q $1; then
1960 # can be overridden in specific test
1961 test_setup_cleanup
() {
1966 # (post-test) cleanup should always ignore failure and cleanup as much as possible
1969 _umount_dir
$initdir
1970 rm -vf "$IMAGE_PUBLIC"
1976 # can be overridden in specific test
1981 test_cleanup_again
() {
1982 [ -n "$TESTDIR" ] ||
return
1983 rm -rf "$TESTDIR/unprivileged-nspawn-root"
1984 _umount_dir
$initdir
1987 test_create_image
() {
1988 create_empty_image_rootdir
1990 # Create what will eventually be our root filesystem onto an overlay
1993 setup_basic_environment
1994 mask_supporting_services
1999 if [ ${TEST_REQUIRE_INSTALL_TESTS} -ne 0 ] && \
2000 type -P meson
>/dev
/null
&& \
2001 [[ "$(meson configure $BUILD_DIR | grep install-tests | awk '{ print $2 }')" != "true" ]]; then
2002 dfatal
"$BUILD_DIR needs to be built with -Dinstall-tests=true"
2006 if [ -e "$IMAGE_PRIVATE" ]; then
2007 echo "Reusing existing image $IMAGE_PRIVATE → $(realpath $IMAGE_PRIVATE)"
2010 if [ ! -e "$IMAGE_PUBLIC" ]; then
2011 # Create the backing public image, but then completely unmount
2012 # it and drop the loopback device responsible for it, since we're
2013 # going to symlink/copy the image and mount it again from
2021 echo "Reusing existing cached image $IMAGE_PUBLIC → $(realpath $IMAGE_PUBLIC)"
2022 if [ ${TEST_PARALLELIZE} -ne 0 ]; then
2023 cp -v "$(realpath $IMAGE_PUBLIC)" "$IMAGE_PRIVATE"
2025 ln -sv "$(realpath $IMAGE_PUBLIC)" "$IMAGE_PRIVATE"
2037 if [ -z "$TEST_NO_QEMU" ]; then
2038 if run_qemu
"$1"; then
2039 check_result_qemu ||
{ echo "QEMU test failed"; return 1; }
2041 dwarn
"can't run QEMU, skipping"
2044 if [ -z "$TEST_NO_NSPAWN" ]; then
2046 if run_nspawn
"$initdir" "$1"; then
2047 check_result_nspawn
"$initdir" ||
{ echo "nspawn-root test failed"; return 1; }
2049 dwarn
"can't run systemd-nspawn, skipping"
2052 if [[ "$RUN_IN_UNPRIVILEGED_CONTAINER" = "yes" ]]; then
2053 dir
="$TESTDIR/unprivileged-nspawn-root"
2054 if NSPAWN_ARGUMENTS
="-U --private-network $NSPAWN_ARGUMENTS" run_nspawn
"$dir" "$1"; then
2055 check_result_nspawn
"$dir" ||
{ echo "unprivileged-nspawn-root test failed"; return 1; }
2057 dwarn
"can't run systemd-nspawn, skipping"
2065 if [[ $UID != "0" ]]; then
2066 echo "TEST: $TEST_DESCRIPTION [SKIPPED]: not root" >&2
2071 [[ $libdir ]] ||
for libdir
in /lib64
/lib
; do
2072 [[ -d $libdir ]] && libdirs
+=" $libdir" && break
2075 [[ $usrlibdir ]] ||
for usrlibdir
in /usr
/lib64
/usr
/lib
; do
2076 [[ -d $usrlibdir ]] && libdirs
+=" $usrlibdir" && break
2079 mkdir
-p "$STATEDIR"
2084 testname
="$(basename $PWD)"
2086 while (($# > 0)); do
2089 echo "${testname} RUN: $TEST_DESCRIPTION"
2092 if (( $ret == 0 )); then
2093 echo "${testname} RUN: $TEST_DESCRIPTION [OK]"
2095 echo "${testname} RUN: $TEST_DESCRIPTION [FAILED]"
2099 echo "${testname} SETUP: $TEST_DESCRIPTION"
2104 echo "${testname} CLEANUP: $TEST_DESCRIPTION"
2108 echo "${testname} CLEANUP AGAIN: $TEST_DESCRIPTION"
2113 echo -n "${testname}: $TEST_DESCRIPTION "
2118 ) </dev
/null
>"$TESTLOG" 2>&1 || ret
=$?
2120 if [ $ret -eq 0 ]; then