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}"
23 # Decide if we can (and want to) run QEMU with KVM acceleration.
24 # Check if nested KVM is explicitly enabled (TEST_NESTED_KVM). If not,
25 # check if it's not explicitly disabled (TEST_NO_KVM) and we're not already
26 # running under KVM. If these conditions are met, enable KVM (and possibly
27 # nested KVM), otherwise disable it.
28 if [[ -n "$TEST_NESTED_KVM" ||
( -z "$TEST_NO_KVM" && $
(systemd-detect-virt
-v) != kvm
) ]]; then
34 if ! ROOTLIBDIR
=$
(pkg-config
--variable=systemdutildir systemd
); then
35 echo "WARNING! Cannot determine rootlibdir from pkg-config, assuming /usr/lib/systemd" >&2
36 ROOTLIBDIR
=/usr
/lib
/systemd
39 PATH_TO_INIT
=$ROOTLIBDIR/systemd
40 [ "$SYSTEMD_JOURNALD" ] || SYSTEMD_JOURNALD
=$
(which -a $BUILD_DIR/systemd-journald
$ROOTLIBDIR/systemd-journald
2>/dev
/null |
grep '^/' -m1)
41 [ "$SYSTEMD" ] || SYSTEMD
=$
(which -a $BUILD_DIR/systemd
$ROOTLIBDIR/systemd
2>/dev
/null |
grep '^/' -m1)
42 [ "$SYSTEMD_NSPAWN" ] || SYSTEMD_NSPAWN
=$
(which -a $BUILD_DIR/systemd-nspawn systemd-nspawn
2>/dev
/null |
grep '^/' -m1)
43 [ "$JOURNALCTL" ] || JOURNALCTL
=$
(which -a $BUILD_DIR/journalctl journalctl
2>/dev
/null |
grep '^/' -m1)
140 STATEDIR
="${BUILD_DIR:-.}/test/$(basename $(dirname $(realpath $0)))"
141 STATEFILE
="$STATEDIR/.testdir"
142 IMAGESTATEDIR
="$STATEDIR/.."
143 TESTLOG
="$STATEDIR/test.log"
145 is_built_with_asan
() {
146 if ! type -P objdump
>/dev
/null
; then
147 ddebug
"Failed to find objdump. Assuming systemd hasn't been built with ASAN."
151 # Borrowed from https://github.com/google/oss-fuzz/blob/cd9acd02f9d3f6e80011cc1e9549be526ce5f270/infra/base-images/base-runner/bad_build_check#L182
152 local _asan_calls
=$
(objdump
-dC $SYSTEMD_JOURNALD |
egrep "callq\s+[0-9a-f]+\s+<__asan" -c)
153 if (( $_asan_calls < 1000 )); then
160 IS_BUILT_WITH_ASAN
=$
(is_built_with_asan
&& echo yes ||
echo no
)
162 if [[ "$IS_BUILT_WITH_ASAN" = "yes" ]]; then
164 SKIP_INITRD
="${SKIP_INITRD:-yes}"
165 PATH_TO_INIT
=$ROOTLIBDIR/systemd-under-asan
169 # We need to correctly distinguish between gcc's and clang's ASan DSOs.
170 if ldd
$SYSTEMD |
grep -q libasan.so
; then
172 elif ldd
$SYSTEMD |
grep -q libclang_rt.asan
; then
175 # As clang's ASan DSO is usually in a non-standard path, let's check if
176 # the environment is set accordingly. If not, warn the user and exit.
177 # We're not setting the LD_LIBRARY_PATH automagically here, because
178 # user should encounter (and fix) the same issue when running the unit
180 if ldd
"$SYSTEMD" |
grep -q "libclang_rt.asan.*not found"; then
181 _asan_rt_name
="$(ldd $SYSTEMD | awk '/libclang_rt.asan/ {print $1; exit}')"
182 _asan_rt_path
="$(find /usr/lib* /usr/local/lib* -type f -name "$_asan_rt_name" 2>/dev/null | sed 1q)"
183 echo >&2 "clang's ASan DSO ($_asan_rt_name) is not present in the runtime library path"
184 echo >&2 "Consider setting LD_LIBRARY_PATH=${_asan_rt_path%/*}"
188 echo >&2 "systemd is not linked against the ASan DSO"
189 echo >&2 "gcc does this by default, for clang compile with -shared-libasan"
194 function find_qemu_bin
() {
195 # SUSE and Red Hat call the binary qemu-kvm. Debian and Gentoo call it kvm.
196 if [[ $QEMU_KVM == "yes" ]]; then
197 [ "$QEMU_BIN" ] || QEMU_BIN
=$
(which -a kvm qemu-kvm
2>/dev
/null |
grep '^/' -m1)
200 [ "$ARCH" ] || ARCH
=$
(uname
-m)
203 # QEMU's own build system calls it qemu-system-x86_64
204 [ "$QEMU_BIN" ] || QEMU_BIN
=$
(which -a qemu-system-x86_64
2>/dev
/null |
grep '^/' -m1)
207 # new i386 version of QEMU
208 [ "$QEMU_BIN" ] || QEMU_BIN
=$
(which -a qemu-system-i386
2>/dev
/null |
grep '^/' -m1)
210 # i386 version of QEMU
211 [ "$QEMU_BIN" ] || QEMU_BIN
=$
(which -a qemu
2>/dev
/null |
grep '^/' -m1)
214 [ "$QEMU_BIN" ] || QEMU_BIN
=$
(which -a qemu-system-ppc64
2>/dev
/null |
grep '^/' -m1)
218 if [ ! -e "$QEMU_BIN" ]; then
219 echo "Could not find a suitable QEMU binary" >&2
224 # Return 0 if QEMU did run (then you must check the result state/logs for actual
225 # success), or 1 if QEMU is not available.
227 if [ -f /etc
/machine-id
]; then
228 read MACHINE_ID
< /etc
/machine-id
229 [ -z "$INITRD" ] && [ -e "$EFI_MOUNT/$MACHINE_ID/$KERNEL_VER/initrd" ] \
230 && INITRD
="$EFI_MOUNT/$MACHINE_ID/$KERNEL_VER/initrd"
231 [ -z "$KERNEL_BIN" ] && [ -e "$EFI_MOUNT/$MACHINE_ID/$KERNEL_VER/linux" ] \
232 && KERNEL_BIN
="$EFI_MOUNT/$MACHINE_ID/$KERNEL_VER/linux"
237 rm -f "$initdir"/{testok
,failed
,skipped
}
238 # make sure the initdir is not mounted to avoid concurrent access
242 if [[ ! "$KERNEL_BIN" ]]; then
243 if [[ "$LOOKS_LIKE_ARCH" ]]; then
244 KERNEL_BIN
=/boot
/vmlinuz-linux
246 [ "$ARCH" ] || ARCH
=$
(uname
-m)
249 KERNEL_BIN
=/boot
/vmlinux-
$KERNEL_VER
253 KERNEL_BIN
=/boot
/vmlinuz-
$KERNEL_VER
259 default_fedora_initrd
=/boot
/initramfs-
${KERNEL_VER}.img
260 default_debian_initrd
=/boot
/initrd.img-
${KERNEL_VER}
261 default_arch_initrd
=/boot
/initramfs-linux-fallback.img
262 default_suse_initrd
=/boot
/initrd-
${KERNEL_VER}
263 if [[ ! "$INITRD" ]]; then
264 if [[ -e "$default_fedora_initrd" ]]; then
265 INITRD
="$default_fedora_initrd"
266 elif [[ "$LOOKS_LIKE_DEBIAN" && -e "$default_debian_initrd" ]]; then
267 INITRD
="$default_debian_initrd"
268 elif [[ "$LOOKS_LIKE_ARCH" && -e "$default_arch_initrd" ]]; then
269 INITRD
="$default_arch_initrd"
270 elif [[ "$LOOKS_LIKE_SUSE" && -e "$default_suse_initrd" ]]; then
271 INITRD
="$default_suse_initrd"
275 # If QEMU_SMP was not explicitly set, try to determine the value 'dynamically'
276 # i.e. use the number of online CPUs on the host machine. If the nproc utility
277 # is not installed or there's some other error when calling it, fall back
278 # to the original value (QEMU_SMP=1).
279 if ! [ "$QEMU_SMP" ]; then
280 if ! QEMU_SMP
=$
(nproc
); then
281 dwarn
"nproc utility is not installed, falling back to QEMU_SMP=1"
286 find_qemu_bin ||
return 1
288 # Umount initdir to avoid concurrent access to the filesystem
292 if [[ "$UNIFIED_CGROUP_HIERARCHY" = "yes" ]]; then
293 _cgroup_args
="systemd.unified_cgroup_hierarchy=yes"
294 elif [[ "$UNIFIED_CGROUP_HIERARCHY" = "no" ]]; then
295 _cgroup_args
="systemd.unified_cgroup_hierarchy=no systemd.legacy_systemd_cgroup_controller=yes"
296 elif [[ "$UNIFIED_CGROUP_HIERARCHY" = "hybrid" ]]; then
297 _cgroup_args
="systemd.unified_cgroup_hierarchy=no systemd.legacy_systemd_cgroup_controller=no"
298 elif [[ "$UNIFIED_CGROUP_HIERARCHY" != "default" ]]; then
299 dfatal
"Unknown UNIFIED_CGROUP_HIERARCHY. Got $UNIFIED_CGROUP_HIERARCHY, expected [yes|no|hybrid|default]"
303 if [[ "$LOOKS_LIKE_SUSE" ]]; then
304 PARAMS
+="rd.hostonly=0"
308 if [[ ! "$INTERACTIVE_DEBUG" ]]; then
309 _end
="systemd.wants=end.service"
314 KERNEL_APPEND
="$PARAMS \
324 SYSTEMD_UNIT_PATH=/usr/lib/systemd/tests/testdata/testsuite-$1.units:/usr/lib/systemd/tests/testdata/units: \
325 systemd.unit=testsuite.target \
326 systemd.wants=testsuite-$1.service ${_end} \
330 QEMU_OPTIONS
="-smp $QEMU_SMP \
334 -kernel $KERNEL_BIN \
335 -drive format=raw,cache=unsafe,file=${IMAGESTATEDIR}/${IMAGE_NAME}.img \
339 if [[ "$INITRD" && "$SKIP_INITRD" != "yes" ]]; then
340 QEMU_OPTIONS
="$QEMU_OPTIONS -initrd $INITRD"
343 # Let's use KVM if possible
344 if [[ -c /dev
/kvm
&& $QEMU_KVM == "yes" ]]; then
345 QEMU_OPTIONS
="$QEMU_OPTIONS -machine accel=kvm -enable-kvm -cpu host"
348 if [[ "$QEMU_TIMEOUT" != "infinity" ]]; then
349 QEMU_BIN
="timeout --foreground $QEMU_TIMEOUT $QEMU_BIN"
351 (set -x; $QEMU_BIN $QEMU_OPTIONS -append "$KERNEL_APPEND")
353 if [ "$rc" = 124 ] && [ "$QEMU_TIMEOUT" != "infinity" ]; then
354 derror
"test timed out after $QEMU_TIMEOUT s"
357 [ "$rc" != 0 ] && derror
"QEMU failed with exit code $rc"
362 # Return 0 if nspawn did run (then you must check the result state/logs for actual
363 # success), or 1 if nspawn is not available.
365 [[ -d /run
/systemd
/system
]] ||
return 1
366 rm -f "$initdir"/{testok
,failed
,skipped
}
370 --kill-signal=SIGKILL
372 --setenv=SYSTEMD_UNIT_PATH
=/usr
/lib
/systemd
/tests
/testdata
/testsuite-
$2.units
:/usr
/lib
/systemd
/tests
/testdata
/units
:
375 systemd.unit
=testsuite.target
376 systemd.wants
=testsuite-
$2.service
379 if [[ ! "$INTERACTIVE_DEBUG" ]]; then
380 _nspawn_cmd
+=( systemd.wants
=end.service
)
384 if [[ "$NSPAWN_TIMEOUT" != "infinity" ]]; then
385 _nspawn_pre
=(timeout
--foreground $NSPAWN_TIMEOUT)
390 if [[ "$UNIFIED_CGROUP_HIERARCHY" = "hybrid" ]]; then
391 dwarn
"nspawn doesn't support SYSTEMD_NSPAWN_UNIFIED_HIERARCHY=hybrid, skipping"
393 elif [[ "$UNIFIED_CGROUP_HIERARCHY" = "yes" ||
"$UNIFIED_CGROUP_HIERARCHY" = "no" ]]; then
394 _nspawn_pre
=("${nspawn_pre[@]}" env SYSTEMD_NSPAWN_UNIFIED_HIERARCHY
=$UNIFIED_CGROUP_HIERARCHY)
395 elif [[ "$UNIFIED_CGROUP_HIERARCHY" = "default" ]]; then
396 _nspawn_pre
=("${nspawn_pre[@]}" env
--unset=UNIFIED_CGROUP_HIERARCHY
--unset=SYSTEMD_NSPAWN_UNIFIED_HIERARCHY
)
398 dfatal
"Unknown UNIFIED_CGROUP_HIERARCHY. Got $UNIFIED_CGROUP_HIERARCHY, expected [yes|no|hybrid|default]"
402 (set -x; "${_nspawn_pre[@]}" "$SYSTEMD_NSPAWN" $NSPAWN_ARGUMENTS "${_nspawn_cmd[@]}")
404 if [ "$rc" = 124 ] && [ "$NSPAWN_TIMEOUT" != "infinity" ]; then
405 derror
"test timed out after $NSPAWN_TIMEOUT s"
408 [ "$rc" != 0 ] && derror
"nspawn failed with exit code $rc"
413 setup_basic_environment
() {
414 # create the basic filesystem layout
418 install_missing_libraries
435 has_user_dbus_socket
&& install_user_dbus
439 generate_module_dependencies
440 if [[ "$IS_BUILT_WITH_ASAN" = "yes" ]]; then
446 # don't forget KERNEL_APPEND='... selinux=1 ...'
447 if [[ "$SETUP_SELINUX" != "yes" ]]; then
448 ddebug
"Don't setup SELinux"
451 ddebug
"Setup SELinux"
452 local _conf_dir
=/etc
/selinux
453 local _fixfiles_tools
="bash uname cat sort uniq awk grep egrep head expr find rm secon setfiles"
455 rm -rf $initdir/$_conf_dir
456 if ! cp -ar $_conf_dir $initdir/$_conf_dir; then
457 dfatal
"Failed to copy $_conf_dir"
461 touch $initdir/.autorelabel
462 mkdir
-p $initdir/usr
/lib
/systemd
/tests
/testdata
/units
/basic.target.wants
463 ln -sf ..
/autorelabel.service
$initdir/usr
/lib
/systemd
/tests
/testdata
/units
/basic.target.wants
/
465 dracut_install
$_fixfiles_tools
466 dracut_install fixfiles
467 dracut_install sestatus
471 if ! type -p valgrind
; then
472 dfatal
"Failed to install valgrind"
476 local _valgrind_bins
=$
(strace
-e execve valgrind
/bin
/true
2>&1 >/dev
/null | perl
-lne 'print $1 if /^execve\("([^"]+)"/')
477 dracut_install
$_valgrind_bins
479 local _valgrind_libs
=$
(LD_DEBUG
=files valgrind
/bin
/true
2>&1 >/dev
/null | perl
-lne 'print $1 if m{calling init: (/.*vgpreload_.*)}')
480 dracut_install
$_valgrind_libs
482 local _valgrind_dbg_and_supp
=$
(
483 strace
-e open valgrind
/bin
/true
2>&1 >/dev
/null |
484 perl
-lne 'if (my ($fname) = /^open\("([^"]+).*= (?!-)\d+/) { print $fname if $fname =~ /debug|\.supp$/ }'
486 dracut_install
$_valgrind_dbg_and_supp
489 create_valgrind_wrapper
() {
490 local _valgrind_wrapper
=$initdir/$ROOTLIBDIR/systemd-under-valgrind
491 ddebug
"Create $_valgrind_wrapper"
492 cat >$_valgrind_wrapper <<EOF
495 mount -t proc proc /proc
496 exec valgrind --leak-check=full --log-file=/valgrind.out $ROOTLIBDIR/systemd "\$@"
498 chmod 0755 $_valgrind_wrapper
501 create_asan_wrapper
() {
502 local _asan_wrapper
=$initdir/$ROOTLIBDIR/systemd-under-asan
503 local _asan_rt_pattern
504 ddebug
"Create $_asan_wrapper"
506 case "$ASAN_COMPILER" in
508 _asan_rt_pattern
="*libasan*"
511 _asan_rt_pattern
="libclang_rt.asan-*"
512 # Install llvm-symbolizer to generate useful reports
513 # See: https://clang.llvm.org/docs/AddressSanitizer.html#symbolizing-the-reports
514 dracut_install
"llvm-symbolizer"
517 dfail
"Unsupported compiler: $ASAN_COMPILER"
521 cat >$_asan_wrapper <<EOF
526 DEFAULT_ASAN_OPTIONS=${ASAN_OPTIONS:-strict_string_checks=1:detect_stack_use_after_return=1:check_initialization_order=1:strict_init_order=1}
527 DEFAULT_UBSAN_OPTIONS=${UBSAN_OPTIONS:-print_stacktrace=1:print_summary=1:halt_on_error=1}
528 DEFAULT_ENVIRONMENT="ASAN_OPTIONS=\$DEFAULT_ASAN_OPTIONS UBSAN_OPTIONS=\$DEFAULT_UBSAN_OPTIONS"
530 # As right now bash is the PID 1, we can't expect PATH to have a sane value.
531 # Let's make one to prevent unexpected "<bin> not found" issues in the future
532 export PATH="/sbin:/bin:/usr/sbin:/usr/bin"
534 mount -t proc proc /proc
535 mount -t sysfs sysfs /sys
536 mount -o remount,rw /
538 PATH_TO_ASAN=\$(find / -name '$_asan_rt_pattern' | sed 1q)
539 if [[ "\$PATH_TO_ASAN" ]]; then
540 # A lot of services (most notably dbus) won't start without preloading libasan
541 # See https://github.com/systemd/systemd/issues/5004
542 DEFAULT_ENVIRONMENT="\$DEFAULT_ENVIRONMENT LD_PRELOAD=\$PATH_TO_ASAN"
543 # Let's add the ASan DSO's path to the dynamic linker's cache. This is pretty
544 # unnecessary for gcc & libasan, however, for clang this is crucial, as its
545 # runtime ASan DSO is in a non-standard (library) path.
546 echo \${PATH_TO_ASAN%/*} > /etc/ld.so.conf.d/asan-path-override.conf
549 echo DefaultEnvironment=\$DEFAULT_ENVIRONMENT >>/etc/systemd/system.conf
550 echo DefaultTimeoutStartSec=180s >>/etc/systemd/system.conf
551 echo DefaultStandardOutput=journal+console >>/etc/systemd/system.conf
553 # ASAN and syscall filters aren't compatible with each other.
554 find / -name '*.service' -type f | xargs sed -i 's/^\\(MemoryDeny\\|SystemCall\\)/#\\1/'
556 # The redirection of ASAN reports to a file prevents them from ending up in /dev/null.
557 # But, apparently, sometimes it doesn't work: https://github.com/google/sanitizers/issues/886.
558 JOURNALD_CONF_DIR=/etc/systemd/system/systemd-journald.service.d
559 mkdir -p "\$JOURNALD_CONF_DIR"
560 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"
562 # Sometimes UBSan sends its reports to stderr regardless of what is specified in log_path
563 # Let's try to catch them by redirecting stderr (and stdout just in case) to a file
564 # See https://github.com/systemd/systemd/pull/12524#issuecomment-491108821
565 printf "[Service]\nStandardOutput=file:/systemd-journald.out\n" >"\$JOURNALD_CONF_DIR/out.conf"
567 # 90s isn't enough for some services to finish when literally everything is run
568 # under ASan+UBSan in containers, which, in turn, are run in VMs.
569 # Let's limit which environments such services should be executed in.
570 mkdir -p /etc/systemd/system/systemd-hwdb-update.service.d
571 printf "[Unit]\nConditionVirtualization=container\n\n[Service]\nTimeoutSec=180s\n" >/etc/systemd/system/systemd-hwdb-update.service.d/env-override.conf
573 # Let's override another hard-coded timeout that kicks in too early
574 mkdir -p /etc/systemd/system/systemd-journal-flush.service.d
575 printf "[Service]\nTimeoutSec=180s\n" >/etc/systemd/system/systemd-journal-flush.service.d/timeout.conf
577 # The 'mount' utility doesn't behave well under libasan, causing unexpected
578 # fails during boot and subsequent test results check:
579 # bash-5.0# mount -o remount,rw -v /
580 # mount: /dev/sda1 mounted on /.
583 # Let's workaround this by clearing the previously set LD_PRELOAD env variable,
584 # so the libasan library is not loaded for this particular service
586 local _dropin_dir="/etc/systemd/system/\$1.service.d"
587 mkdir -p "\$_dropin_dir"
588 printf "[Service]\nUnsetEnvironment=LD_PRELOAD\n" >"\$_dropin_dir/unset_ld_preload.conf"
591 unset_ld_preload systemd-remount-fs
592 unset_ld_preload testsuite-
594 export ASAN_OPTIONS=\$DEFAULT_ASAN_OPTIONS:log_path=/systemd.asan.log UBSAN_OPTIONS=\$DEFAULT_UBSAN_OPTIONS
595 exec $ROOTLIBDIR/systemd "\$@"
598 chmod 0755 $_asan_wrapper
601 create_strace_wrapper
() {
602 local _strace_wrapper
=$initdir/$ROOTLIBDIR/systemd-under-strace
603 ddebug
"Create $_strace_wrapper"
604 cat >$_strace_wrapper <<EOF
607 exec strace -D -o /strace.out $ROOTLIBDIR/systemd "\$@"
609 chmod 0755 $_strace_wrapper
613 dracut_install
/sbin
/fsck
*
614 dracut_install
-o /bin
/fsck
*
616 # fskc.reiserfs calls reiserfsck. so, install it
617 dracut_install
-o reiserfsck
621 instmods dm_crypt
=crypto
623 if [[ "$LOOKS_LIKE_DEBIAN" ]]; then
624 # dmsetup installs 55-dm and 60-persistent-storage-dm on Debian/Ubuntu
625 # and since buster/bionic 95-dm-notify.rules
626 # see https://gitlab.com/debian-lvm/lvm2/blob/master/debian/patches/udev.patch
627 inst_rules
55-dm.rules
60-persistent-storage-dm.rules
95-dm-notify.rules
629 inst_rules
10-dm.rules
13-dm-disk.rules
95-dm-notify.rules
634 # install compiled files
635 local _ninja_bin
=$
(type -P ninja ||
type -P ninja-build
)
636 if [[ -z "$_ninja_bin" ]]; then
637 dfatal
"ninja was not found"
640 (set -x; DESTDIR
=$initdir "$_ninja_bin" -C $BUILD_DIR install)
641 # remove unneeded documentation
642 rm -fr $initdir/usr
/share
/{man
,doc
}
643 # we strip binaries since debug symbols increase binaries size a lot
644 # and it could fill the available space
647 [[ "$LOOKS_LIKE_SUSE" ]] && setup_suse
649 # enable debug logging in PID1
650 echo LogLevel
=debug
>> $initdir/etc
/systemd
/system.conf
651 # store coredumps in journal
652 echo Storage
=journal
>> $initdir/etc
/systemd
/coredump.conf
657 local rpath
=$
(objdump
-p "$_bin" 2>/dev
/null |
awk "/R(UN)?PATH/ { print \"$initdir\" \$2 }" |
paste -sd :)
659 if [ -z "$rpath" ] ; then
666 install_missing_libraries
() {
667 # install possible missing libraries
668 for i
in $initdir{,/usr
}/{sbin
,bin
}/* $initdir{,/usr
}/lib
/systemd
/{,tests
/{,manual
/,unsafe
/}}*; do
669 LD_LIBRARY_PATH
="${LD_LIBRARY_PATH:+$LD_LIBRARY_PATH:}$(get_ldpath $i)" inst_libs
$i
674 if [ -n "${LOOPDEV}" ]; then
675 ddebug
"losetup -d $LOOPDEV"
676 losetup
-d "${LOOPDEV}"
680 trap cleanup_loopdev EXIT INT QUIT PIPE
682 create_empty_image
() {
683 if [ -z "$IMAGE_NAME" ]; then
684 echo "create_empty_image: \$IMAGE_NAME not set"
689 if [[ "$STRIP_BINARIES" = "no" ]]; then
693 image
="${TESTDIR}/${IMAGE_NAME}.img"
694 public
="$IMAGESTATEDIR/${IMAGE_NAME}.img"
695 echo "Setting up $public (${_size} MB)"
696 rm -f "$image" "$public"
698 # Create the blank file to use as a root filesystem
699 truncate
-s "${_size}M" "$image"
700 ln -vs "$(realpath $image)" "$public"
702 LOOPDEV
=$
(losetup
--show -P -f "$public")
703 [ -b "$LOOPDEV" ] ||
return 1
704 sfdisk
"$LOOPDEV" <<EOF
711 local _label
="-L systemd.${name}"
712 # mkfs.reiserfs doesn't know -L. so, use --label instead
713 [[ "$FSTYPE" == "reiserfs" ]] && _label
="--label systemd.${name}"
714 mkfs
-t "${FSTYPE}" ${_label} "${LOOPDEV}p1" -q; ret
=$?
715 if [ $ret -ne 0 ] ; then
716 dfatal
"Failed to mkfs -t ${FSTYPE}"
722 if [ -z "${LOOPDEV}" ]; then
723 image
="${IMAGESTATEDIR}/${IMAGE_NAME}.img"
724 LOOPDEV
=$
(losetup
--show -P -f "$image")
725 [ -b "$LOOPDEV" ] ||
return 1
730 if ! mountpoint
-q $initdir; then
732 mount
${LOOPDEV}p1
$initdir
733 TEST_SETUP_CLEANUP_ROOTDIR
=1
738 # only umount if create_empty_image_rootdir() was called to mount it
739 [[ -z $TEST_SETUP_CLEANUP_ROOTDIR ]] || _umount_dir
$initdir
743 # unmount the loopback device from all places. Otherwise we risk file
745 image
="${IMAGESTATEDIR}/${IMAGE_NAME}.img"
746 for device
in $
(losetup
-l |
awk '$6=="'"$image"'" {print $1}'); do
747 ddebug
"Unmounting all uses of $device"
748 mount |
awk '/^'"${device}"'p/{print $1}' |
xargs --no-run-if-empty umount
-v
752 create_empty_image_rootdir
() {
757 check_asan_reports
() {
761 if [[ "$IS_BUILT_WITH_ASAN" = "yes" ]]; then
763 if [[ -e "$root/systemd.asan.log.1" ]]; then
764 cat "$root/systemd.asan.log.1"
768 journald_report
=$
(find "$root" -name "systemd-journald.*san.log*" -exec cat {} \
;)
769 if [[ ! -z "$journald_report" ]]; then
770 printf "%s\n" "$journald_report"
771 cat "$root/systemd-journald.out" ||
:
776 "$JOURNALCTL" -D "$root/var/log/journal" | perl
-alne '
778 %services_to_ignore = (
779 "dbus-daemon" => undef,
782 print $2 if /\s(\S*)\[(\d+)\]:\s*SUMMARY:\s+\w+Sanitizer/ && !exists $services_to_ignore{$1}'
784 if [[ ! -z "$pids" ]]; then
787 "$JOURNALCTL" -D "$root/var/log/journal" _PID
=$pid --no-pager
796 if [ -n "${ARTIFACT_DIRECTORY}" ]; then
797 dest
="${ARTIFACT_DIRECTORY}/${testname}.journal"
799 dest
="$TESTDIR/system.journal"
803 /usr
/lib
/systemd
/systemd-journal-remote \
805 --getter="journalctl -o export -D $j"
807 if [ -n "${TEST_SHOW_JOURNAL}" ]; then
809 journalctl
--no-pager -o short-monotonic
--no-hostname --priority=${TEST_SHOW_JOURNAL} -D $j
815 # we want to print this sometime later, so save this in a variable
816 JOURNAL_LIST
="$(ls -l $dest*)"
819 check_result_nspawn
() {
821 local journald_report
=""
823 [[ -e $1/testok
]] && ret
=0
824 [[ -f $1/failed
]] && cp -a $1/failed
$TESTDIR
825 save_journal
$1/var
/log
/journal
826 [[ -f $TESTDIR/failed
]] && cat $TESTDIR/failed
828 test -s $TESTDIR/failed
&& ret
=$
(($ret+1))
829 [ -n "$TIMED_OUT" ] && ret
=$
(($ret+1))
830 check_asan_reports
"$1" || ret
=$
(($ret+1))
835 # can be overridden in specific test
836 check_result_qemu
() {
839 [[ -e $initdir/testok
]] && ret
=0
840 [[ -f $initdir/failed
]] && cp -a $initdir/failed
$TESTDIR
841 save_journal
$initdir/var
/log
/journal
842 check_asan_reports
"$initdir" || ret
=$
(($ret+1))
844 [[ -f $TESTDIR/failed
]] && cat $TESTDIR/failed
846 test -s $TESTDIR/failed
&& ret
=$
(($ret+1))
847 [ -n "$TIMED_OUT" ] && ret
=$
(($ret+1))
852 if [[ "$STRIP_BINARIES" = "no" ]]; then
853 ddebug
"Don't strip binaries"
856 ddebug
"Strip binaries"
857 find "$initdir" -executable -not -path '*/lib/modules/*.ko' -type f | \
858 xargs strip
--strip-unneeded |
& \
859 grep -vi 'file format not recognized' | \
864 mkdir
-p $initdir/etc
/rc.d
865 cat >$initdir/etc
/rc.d
/rc.
local <<EOF
869 chmod 0755 $initdir/etc
/rc.d
/rc.
local
873 ddebug
"install any Execs from the service files"
875 export PKG_CONFIG_PATH
=$BUILD_DIR/src
/core
/
876 systemdsystemunitdir
=$
(pkg-config
--variable=systemdsystemunitdir systemd
)
877 systemduserunitdir
=$
(pkg-config
--variable=systemduserunitdir systemd
)
878 sed -r -n 's|^Exec[a-zA-Z]*=[@+!-]*([^ ]+).*|\1|gp' $initdir/{$systemdsystemunitdir,$systemduserunitdir}/*.service \
879 |
sort -u |
while read i
; do
880 # some {rc,halt}.local scripts and programs are okay to not exist, the rest should
881 # also, plymouth is pulled in by rescue.service, but even there the exit code
882 # is ignored; as it's not present on some distros, don't fail if it doesn't exist
883 dinfo
"Attempting to install $i"
884 inst
$i ||
[ "${i%.local}" != "$i" ] || [ "${i%systemd-update-done}" != "$i" ] || [ "${i##*/}" == "plymouth" ]
889 generate_module_dependencies
() {
890 if [[ -d $initdir/lib
/modules
/$KERNEL_VER ]] && \
891 ! depmod
-a -b "$initdir" $KERNEL_VER; then
892 dfatal
"\"depmod -a $KERNEL_VER\" failed."
897 install_depmod_files
() {
898 inst
/lib
/modules
/$KERNEL_VER/modules.order
899 inst
/lib
/modules
/$KERNEL_VER/modules.
builtin
903 # install plymouth, if found... else remove plymouth service files
904 # if [ -x /usr/libexec/plymouth/plymouth-populate-initrd ]; then
905 # PLYMOUTH_POPULATE_SOURCE_FUNCTIONS="$TEST_BASE_DIR/test-functions" \
906 # /usr/libexec/plymouth/plymouth-populate-initrd -t $initdir
907 # dracut_install plymouth plymouthd
909 rm -f $initdir/{usr
/lib
,etc
}/systemd
/system
/plymouth
* $initdir/{usr
/lib
,etc
}/systemd
/system
/*/plymouth
*
913 install_ld_so_conf
() {
914 cp -a /etc
/ld.so.conf
* $initdir/etc
915 ldconfig
-r "$initdir"
919 # create unprivileged user for user manager tests
920 mkdir
-p $initdir/etc
/sysusers.d
921 cat >$initdir/etc
/sysusers.d
/testuser.conf
<<EOF
922 u testuser 4711 "Test User" /home/testuser
925 mkdir
-p $initdir/home
/testuser
-m 0700
926 chown
4711:4711 $initdir/home
/testuser
929 install_config_files
() {
930 inst
/etc
/sysconfig
/init ||
:
936 inst
/etc
/nsswitch.conf
937 inst
/etc
/pam.conf ||
:
940 # we want an empty environment
941 > $initdir/etc
/environment
942 > $initdir/etc
/machine-id
945 echo systemd-testsuite
> $initdir/etc
/hostname
947 # let's set up just one image with the traditional verbose output
948 if [ ${IMAGE_NAME} != "basic" ]; then
949 mkdir
-p $initdir/etc
/systemd
/system.conf.d
950 echo -e '[Manager]\nStatusUnitFormat=name' >$initdir/etc
/systemd
/system.conf.d
/status.conf
954 install_basic_tools
() {
955 dracut_install
"${BASICTOOLS[@]}"
956 dracut_install
-o sushell
957 # in Debian ldconfig is just a shell script wrapper around ldconfig.real
958 dracut_install
-o ldconfig.real
961 install_debug_tools
() {
962 dracut_install
"${DEBUGTOOLS[@]}"
964 if [[ $INTERACTIVE_DEBUG ]]; then
965 # Set default TERM from vt220 to linux, so at least basic key shortcuts work
966 local _getty_override
="$initdir/etc/systemd/system/serial-getty@.service.d"
967 mkdir
-p "$_getty_override"
968 echo -e "[Service]\nEnvironment=TERM=linux" > "$_getty_override/default-TERM.conf"
970 cat > "$initdir/etc/motd" << EOF
971 To adjust the terminal size use:
981 # install libnss_files for login
982 NSS_LIBS
=$
(LD_DEBUG
=files getent passwd
2>&1 >/dev
/null |
sed -n '/calling init: .*libnss_/ {s!^.* /!/!; p}')
983 dracut_install
$NSS_LIBS
987 inst
$ROOTLIBDIR/system
/dbus.socket
989 # Newer Fedora versions use dbus-broker by default. Let's install it if it's available.
990 if [ -f $ROOTLIBDIR/system
/dbus-broker.service
]; then
991 inst
$ROOTLIBDIR/system
/dbus-broker.service
992 inst_symlink
/etc
/systemd
/system
/dbus.service
993 inst
/usr
/bin
/dbus-broker
994 inst
/usr
/bin
/dbus-broker-launch
995 elif [ -f $ROOTLIBDIR/system
/dbus-daemon.service
]; then
996 # Fedora rawhide replaced dbus.service with dbus-daemon.service
997 inst
$ROOTLIBDIR/system
/dbus-daemon.service
999 inst_symlink
/etc
/systemd
/system
/dbus.service
1001 inst
$ROOTLIBDIR/system
/dbus.service
1005 /etc
/dbus-1
/usr
/share
/dbus-1
-xtype f \
1006 |
while read file; do
1010 # setup policy for Type=dbus test
1011 mkdir
-p $initdir/etc
/dbus-
1/system.d
1012 cat > $initdir/etc
/dbus-
1/system.d
/systemd.
test.ExecStopPost.conf
<<EOF
1013 <?xml version="1.0"?>
1014 <!DOCTYPE busconfig PUBLIC "-//freedesktop//DTD D-BUS Bus Configuration 1.0//EN"
1015 "http://www.freedesktop.org/standards/dbus/1.0/busconfig.dtd">
1017 <policy user="root">
1018 <allow own="systemd.test.ExecStopPost"/>
1024 install_user_dbus
() {
1025 inst
$ROOTLIBDIR/user
/dbus.socket
1026 inst_symlink
/usr
/lib
/systemd
/user
/sockets.target.wants
/dbus.socket || inst_symlink
/etc
/systemd
/user
/sockets.target.wants
/dbus.socket
1028 # Append the After= dependency on dbus in case it isn't already set up
1029 mkdir
-p "$initdir/etc/systemd/system/user@.service.d/"
1030 cat <<EOF >"$initdir/etc/systemd/system/user@.service.d/dbus.conf"
1035 # Newer Fedora versions use dbus-broker by default. Let's install it if it's available.
1036 if [ -f $ROOTLIBDIR/user
/dbus-broker.service
]; then
1037 inst
$ROOTLIBDIR/user
/dbus-broker.service
1038 inst_symlink
/etc
/systemd
/user
/dbus.service
1039 elif [ -f $ROOTLIBDIR/system
/dbus-daemon.service
]; then
1040 # Fedora rawhide replaced dbus.service with dbus-daemon.service
1041 inst
$ROOTLIBDIR/user
/dbus-daemon.service
1043 inst_symlink
/etc
/systemd
/user
/dbus.service
1045 inst
$ROOTLIBDIR/user
/dbus.service
1051 if [[ "$LOOKS_LIKE_DEBIAN" ]] && type -p dpkg-architecture
&>/dev
/null
; then
1052 find "/lib/$(dpkg-architecture -qDEB_HOST_MULTIARCH)/security" -xtype f
1054 find /lib
*/security
-xtype f
1056 find /etc
/pam.d
/etc
/security
-xtype f
1057 ) |
while read file; do
1061 # pam_unix depends on unix_chkpwd.
1062 # see http://www.linux-pam.org/Linux-PAM-html/sag-pam_unix.html
1063 dracut_install
-o unix_chkpwd
1065 [[ "$LOOKS_LIKE_DEBIAN" ]] &&
1066 cp /etc
/pam.d
/systemd-user
$initdir/etc
/pam.d
/
1068 # set empty root password for easy debugging
1069 sed -i 's/^root:x:/root::/' $initdir/etc
/passwd
1073 # The first three paths may be deprecated.
1074 # It seems now the last two paths are used by many distributions.
1076 /usr
/lib
/kbd
/keymaps
/include
/* \
1077 /usr
/lib
/kbd
/keymaps
/i386
/include
/* \
1078 /usr
/lib
/kbd
/keymaps
/i386
/qwerty
/us.
* \
1079 /usr
/lib
/kbd
/keymaps
/legacy
/include
/* \
1080 /usr
/lib
/kbd
/keymaps
/legacy
/i386
/qwerty
/us.
*; do
1081 [[ -f $i ]] ||
continue
1085 # When it takes any argument, then install more keymaps.
1086 if [[ -n $1 ]]; then
1088 /usr
/lib
/kbd
/keymaps
/i386
/*/* \
1089 /usr
/lib
/kbd
/keymaps
/legacy
/i386
/*/*; do
1090 [[ -f $i ]] ||
continue
1096 install_zoneinfo
() {
1097 inst_any
/usr
/share
/zoneinfo
/Asia
/Seoul
1098 inst_any
/usr
/share
/zoneinfo
/Asia
/Vladivostok
1099 inst_any
/usr
/share
/zoneinfo
/Australia
/Sydney
1100 inst_any
/usr
/share
/zoneinfo
/Europe
/Berlin
1101 inst_any
/usr
/share
/zoneinfo
/Europe
/Kiev
1102 inst_any
/usr
/share
/zoneinfo
/Pacific
/Auckland
1103 inst_any
/usr
/share
/zoneinfo
/Pacific
/Honolulu
1104 inst_any
/usr
/share
/zoneinfo
/CET
1105 inst_any
/usr
/share
/zoneinfo
/EET
1106 inst_any
/usr
/share
/zoneinfo
/UTC
1111 /usr
/lib
/kbd
/consolefonts
/eurlatgr
* \
1112 /usr
/lib
/kbd
/consolefonts
/latarcyrheb-sun16
*; do
1113 [[ -f $i ]] ||
continue
1118 install_terminfo
() {
1119 for _terminfodir
in /lib
/terminfo
/etc
/terminfo
/usr
/share
/terminfo
; do
1120 [ -f ${_terminfodir}/l
/linux
] && break
1122 dracut_install
-o ${_terminfodir}/l
/linux
1125 has_user_dbus_socket
() {
1126 if [ -f /usr
/lib
/systemd
/user
/dbus.socket
] ||
[ -f /etc
/systemd
/user
/dbus.socket
]; then
1129 echo "Per-user instances are not supported. Skipping..."
1134 setup_nspawn_root
() {
1135 if [ -z "${initdir}" ]; then
1136 dfatal
"\$initdir not defined"
1140 rm -rf "$TESTDIR/unprivileged-nspawn-root"
1142 if [[ "$RUN_IN_UNPRIVILEGED_CONTAINER" = "yes" ]]; then
1143 ddebug
"cp -ar $initdir $TESTDIR/unprivileged-nspawn-root"
1144 cp -ar $initdir $TESTDIR/unprivileged-nspawn-root
1148 setup_basic_dirs
() {
1149 mkdir
-p $initdir/run
1150 mkdir
-p $initdir/etc
/systemd
/system
1151 mkdir
-p $initdir/var
/log
/journal
1153 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
1154 if [ -L "/$d" ]; then
1161 ln -sfn /run
"$initdir/var/run"
1162 ln -sfn /run
/lock
"$initdir/var/lock"
1165 mask_supporting_services
() {
1166 # mask some services that we do not want to run in these tests
1167 ln -fs /dev
/null
$initdir/etc
/systemd
/system
/systemd-hwdb-update.service
1168 ln -fs /dev
/null
$initdir/etc
/systemd
/system
/systemd-journal-catalog-update.service
1169 ln -fs /dev
/null
$initdir/etc
/systemd
/system
/systemd-networkd.service
1170 ln -fs /dev
/null
$initdir/etc
/systemd
/system
/systemd-networkd.socket
1171 ln -fs /dev
/null
$initdir/etc
/systemd
/system
/systemd-resolved.service
1176 local _so_regex
='([^ ]*/lib[^/]*/[^ ]*\.so[^ ]*)'
1179 LC_ALL
=C ldd
"$_bin" 2>/dev
/null |
while read _line
; do
1180 [[ $_line = 'not a dynamic executable' ]] && break
1182 if [[ $_line =~
$_so_regex ]]; then
1183 _file
=${BASH_REMATCH[1]}
1184 [[ -e ${initdir}/$_file ]] && continue
1185 inst_library
"$_file"
1189 if [[ $_line =~ not\ found
]]; then
1190 dfatal
"Missing a shared library required by $_bin."
1191 dfatal
"Run \"ldd $_bin\" to find out what it is."
1193 dfatal
"dracut cannot create an initrd."
1200 # make sure we don't get a stale LOOPDEV value from old times
1202 [[ -e $STATEFILE ]] && .
$STATEFILE
1204 if [[ ! -d "$TESTDIR" ]]; then
1205 if [[ -z "$TESTDIR" ]]; then
1206 TESTDIR
=$
(mktemp
--tmpdir=/var
/tmp
-d -t systemd-test.XXXXXX
)
1211 cat >$STATEFILE<<EOF
1219 initdir
=$TESTDIR/root
1224 ## @brief Converts numeric logging level to the first letter of level name.
1226 # @param lvl Numeric logging level in range from 1 to 6.
1227 # @retval 1 if @a lvl is out of range.
1228 # @retval 0 if @a lvl is correct.
1229 # @result Echoes first letter of level name.
1242 ## @brief Internal helper function for _do_dlog()
1244 # @param lvl Numeric logging level.
1245 # @param msg Message.
1246 # @retval 0 It's always returned, even if logging failed.
1248 # @note This function is not supposed to be called manually. Please use
1249 # dtrace(), ddebug(), or others instead which wrap this one.
1251 # This function calls _do_dlog() either with parameter msg, or if
1252 # none is given, it will read standard input and will use every line as
1256 # dwarn "This is a warning"
1257 # echo "This is a warning" | dwarn
1258 LOG_LEVEL
=${LOG_LEVEL:-4}
1261 [ -z "$LOG_LEVEL" ] && return 0
1262 [ $1 -le $LOG_LEVEL ] ||
return 0
1263 local lvl
="$1"; shift
1264 local lvlc
=$
(_lvl2char
"$lvl") ||
return 0
1266 if [ $# -ge 1 ]; then
1270 echo "$lvlc: " "$line"
1275 ## @brief Logs message at TRACE level (6)
1277 # @param msg Message.
1278 # @retval 0 It's always returned, even if logging failed.
1282 [ -n "$debug" ] && set -x ||
:
1285 ## @brief Logs message at DEBUG level (5)
1287 # @param msg Message.
1288 # @retval 0 It's always returned, even if logging failed.
1292 # [ -n "$debug" ] && set -x || :
1295 ## @brief Logs message at INFO level (4)
1297 # @param msg Message.
1298 # @retval 0 It's always returned, even if logging failed.
1302 [ -n "$debug" ] && set -x ||
:
1305 ## @brief Logs message at WARN level (3)
1307 # @param msg Message.
1308 # @retval 0 It's always returned, even if logging failed.
1312 [ -n "$debug" ] && set -x ||
:
1315 ## @brief Logs message at ERROR level (2)
1317 # @param msg Message.
1318 # @retval 0 It's always returned, even if logging failed.
1322 # [ -n "$debug" ] && set -x || :
1325 ## @brief Logs message at FATAL level (1)
1327 # @param msg Message.
1328 # @retval 0 It's always returned, even if logging failed.
1332 [ -n "$debug" ] && set -x ||
:
1336 # Generic substring function. If $2 is in $1, return 0.
1337 strstr
() { [ "${1#*$2*}" != "$1" ]; }
1339 # normalize_path <path>
1340 # Prints the normalized path, where it removes any duplicated
1341 # and trailing slashes.
1343 # $ normalize_path ///test/test//
1347 set -- "${1//+(\/)//}"
1352 # convert_abs_rel <from> <to>
1353 # Prints the relative path, when creating a symlink to <to> from <from>.
1355 # $ convert_abs_rel /usr/bin/test /bin/test-2
1357 # $ ln -s $(convert_abs_rel /usr/bin/test /bin/test-2) /usr/bin/test
1359 local __current __absolute __abssize __cursize __newpath
1360 local -i __i __level
1362 set -- "$(normalize_path "$1")" "$(normalize_path "$2")"
1364 # corner case #1 - self looping link
1365 [[ "$1" == "$2" ]] && { echo "${1##*/}"; return; }
1367 # corner case #2 - own dir link
1368 [[ "${1%/*}" == "$2" ]] && { echo "."; return; }
1370 IFS
="/" __current
=($1)
1371 IFS
="/" __absolute
=($2)
1373 __abssize
=${#__absolute[@]}
1374 __cursize
=${#__current[@]}
1376 while [[ ${__absolute[__level]} == ${__current[__level]} ]]
1379 if (( __level
> __abssize || __level
> __cursize
))
1385 for ((__i
= __level
; __i
< __cursize-1
; __i
++))
1387 if ((__i
> __level
))
1389 __newpath
=$__newpath"/"
1391 __newpath
=$__newpath".."
1394 for ((__i
= __level
; __i
< __abssize
; __i
++))
1396 if [[ -n $__newpath ]]
1398 __newpath
=$__newpath"/"
1400 __newpath
=$__newpath${__absolute[__i]}
1407 # Install a directory, keeping symlinks as on the original system.
1408 # Example: if /lib points to /lib64 on the host, "inst_dir /lib/file"
1409 # will create ${initdir}/lib64, ${initdir}/lib64/file,
1410 # and a symlink ${initdir}/lib -> lib64.
1412 [[ -e ${initdir}/"$1" ]] && return 0 # already there
1414 local _dir
="$1" _part
="${1%/*}" _file
1415 while [[ "$_part" != "${_part%/*}" ]] && ! [[ -e "${initdir}/${_part}" ]]; do
1420 # iterate over parent directories
1421 for _file
in $_dir; do
1422 [[ -e "${initdir}/$_file" ]] && continue
1423 if [[ -L $_file ]]; then
1424 inst_symlink
"$_file"
1427 mkdir
-m 0755 -p "${initdir}/$_file" ||
return 1
1428 [[ -e "$_file" ]] && chmod --reference="$_file" "${initdir}/$_file"
1429 chmod u
+w
"${initdir}/$_file"
1434 # $1 = file to copy to ramdisk
1435 # $2 (optional) Name for the file on the ramdisk
1436 # Location of the image dir is assumed to be $initdir
1437 # We never overwrite the target if it exists.
1439 [[ -f "$1" ]] ||
return 1
1440 strstr
"$1" "/" ||
return 1
1442 local _src
=$1 target
="${2:-$1}"
1443 if ! [[ -d ${initdir}/$target ]]; then
1444 [[ -e ${initdir}/$target ]] && return 0
1445 [[ -L ${initdir}/$target ]] && return 0
1446 [[ -d "${initdir}/${target%/*}" ]] || inst_dir "${target%/*}"
1448 # install checksum files also
1449 if [[ -e "${_src%/*}/.${_src##*/}.hmac" ]]; then
1450 inst
"${_src%/*}/.${_src##*/}.hmac" "${target%/*}/.${target##*/}.hmac"
1452 ddebug
"Installing $_src"
1453 cp --sparse=always
-pfL "$_src" "${initdir}/$target"
1456 # find symlinks linked to given library file
1458 # Function searches for symlinks by stripping version numbers appended to
1459 # library filename, checks if it points to the same target and finally
1460 # prints the list of symlinks to stdout.
1463 # rev_lib_symlinks libfoo.so.8.1
1464 # output: libfoo.so.8 libfoo.so
1465 # (Only if libfoo.so.8 and libfoo.so exists on host system.)
1466 rev_lib_symlinks
() {
1467 [[ ! $1 ]] && return 0
1469 local fn
="$1" orig
="$(readlink -f "$1")" links
=''
1471 [[ ${fn} =~ .
*\.so\..
* ]] ||
return 1
1473 until [[ ${fn##*.} == so
]]; do
1475 [[ -L ${fn} && $(readlink -f "${fn}") == ${orig} ]] && links+=" ${fn}"
1481 # Same as above, but specialized to handle dynamic libraries.
1482 # It handles making symlinks according to how the original library
1485 local _src="$1" _dest=${2:-$1} _lib _reallib _symlink
1486 strstr "$1" "/" || return 1
1487 [[ -e $initdir/$_dest ]] && return 0
1488 if [[ -L $_src ]]; then
1489 # install checksum files also
1490 if [[ -e "${_src%/*}/.
${_src##*/}.hmac
" ]]; then
1491 inst "${_src%/*}/.${_src##*/}.hmac" "${_dest%/*}/.${_dest##*/}.hmac
"
1493 _reallib=$(readlink -f "$_src")
1494 inst_simple "$_reallib" "$_reallib"
1495 inst_dir "${_dest%/*}"
1496 [[ -d "${_dest%/*}" ]] && _dest=$(readlink -f "${_dest%/*}")/${_dest##*/}
1497 ln -sfn $
(convert_abs_rel
"${_dest}" "${_reallib}") "${initdir}/${_dest}"
1499 inst_simple
"$_src" "$_dest"
1502 # Create additional symlinks. See rev_symlinks description.
1503 for _symlink
in $
(rev_lib_symlinks
$_src) $
(rev_lib_symlinks
$_reallib); do
1504 [[ -e $initdir/$_symlink ]] ||
{
1505 ddebug
"Creating extra symlink: $_symlink"
1506 inst_symlink
$_symlink
1511 # find a binary. If we were not passed the full path directly,
1512 # search in the usual places to find the binary.
1514 if [[ -z ${1##/*} ]]; then
1515 if [[ -x $1 ]] ||
{ strstr
"$1" ".so" && ldd
$1 &>/dev
/null
; }; then
1524 # Same as above, but specialized to install binary executables.
1525 # Install binary executable, and all shared library dependencies, if any.
1529 # In certain cases we might attempt to install a binary which is already
1530 # present in the test image, yet it's missing from the host system.
1531 # In such cases, let's check if the binary indeed exists in the image
1532 # before doing any other chcecks. If it does, immediately return with
1534 [[ $# -eq 1 && -e $initdir/$1 ]] && return 0
1536 _bin
=$
(find_binary
"$1") ||
return 1
1538 [[ -e $initdir/$_target ]] && return 0
1539 [[ -L $_bin ]] && inst_symlink
$_bin $_target && return 0
1541 local _so_regex
='([^ ]*/lib[^/]*/[^ ]*\.so[^ ]*)'
1543 LC_ALL
=C ldd
"$_bin" 2>/dev
/null |
while read _line
; do
1544 [[ $_line = 'not a dynamic executable' ]] && break
1546 if [[ $_line =~
$_so_regex ]]; then
1547 _file
=${BASH_REMATCH[1]}
1548 [[ -e ${initdir}/$_file ]] && continue
1549 inst_library
"$_file"
1553 if [[ $_line =~ not\ found
]]; then
1554 dfatal
"Missing a shared library required by $_bin."
1555 dfatal
"Run \"ldd $_bin\" to find out what it is."
1557 dfatal
"dracut cannot create an initrd."
1561 inst_simple
"$_bin" "$_target"
1564 # same as above, except for shell scripts.
1565 # If your shell script does not start with shebang, it is not a shell script.
1568 _bin
=$
(find_binary
"$1") ||
return 1
1570 local _line _shebang_regex
1571 read -r -n 80 _line
<"$_bin"
1572 # If debug is set, clean unprintable chars to prevent messing up the term
1573 [[ $debug ]] && _line
=$
(echo -n "$_line" |
tr -c -d '[:print:][:space:]')
1574 _shebang_regex
='(#! *)(/[^ ]+).*'
1575 [[ $_line =~
$_shebang_regex ]] ||
return 1
1576 inst
"${BASH_REMATCH[2]}" && inst_simple
"$_bin" "$@"
1579 # same as above, but specialized for symlinks
1581 local _src
=$1 _target
=${2:-$1} _realsrc
1582 strstr
"$1" "/" ||
return 1
1583 [[ -L $1 ]] ||
return 1
1584 [[ -L $initdir/$_target ]] && return 0
1585 _realsrc
=$
(readlink
-f "$_src")
1586 if ! [[ -e $initdir/$_realsrc ]]; then
1587 if [[ -d $_realsrc ]]; then
1588 inst_dir
"$_realsrc"
1593 [[ ! -e $initdir/${_target%/*} ]] && inst_dir
"${_target%/*}"
1594 [[ -d ${_target%/*} ]] && _target=$(readlink -f ${_target%/*})/${_target##*/}
1595 ln -sfn $
(convert_abs_rel
"${_target}" "${_realsrc}") "$initdir/$_target"
1598 # attempt to install any programs specified in a udev rule
1599 inst_rule_programs
() {
1602 if grep -qE 'PROGRAM==?"[^ "]+' "$1"; then
1603 for _prog
in $
(grep -E 'PROGRAM==?"[^ "]+' "$1" |
sed -r 's/.*PROGRAM==?"([^ "]+).*/\1/'); do
1604 if [ -x /lib
/udev
/$_prog ]; then
1605 _bin
=/lib
/udev
/$_prog
1607 _bin
=$
(find_binary
"$_prog") ||
{
1608 dinfo
"Skipping program $_prog using in udev rule $(basename $1) as it cannot be found"
1613 #dinfo "Installing $_bin due to it's use in the udev rule $(basename $1)"
1614 dracut_install
"$_bin"
1619 # udev rules always get installed in the same place, so
1620 # create a function to install them to make life simpler.
1622 local _target
=/etc
/udev
/rules.d _rule _found
1624 inst_dir
"/lib/udev/rules.d"
1626 for _rule
in "$@"; do
1627 if [ "${rule#/}" = "$rule" ]; then
1628 for r
in /lib
/udev
/rules.d
/etc
/udev
/rules.d
; do
1629 if [[ -f $r/$_rule ]]; then
1631 inst_simple
"$_found"
1632 inst_rule_programs
"$_found"
1636 for r
in '' .
/ $dracutbasedir/rules.d
/; do
1637 if [[ -f ${r}$_rule ]]; then
1639 inst_simple
"$_found" "$_target/${_found##*/}"
1640 inst_rule_programs
"$_found"
1643 [[ $_found ]] || dinfo
"Skipping udev rule: $_rule"
1648 # general purpose installation function
1649 # Same args as above.
1655 2) [[ ! $initdir && -d $2 ]] && export initdir
=$2
1656 [[ $initdir = $2 ]] && set $1;;
1657 3) [[ -z $initdir ]] && export initdir
=$2
1659 *) dfatal
"inst only takes 1 or 2 or 3 arguments"
1662 for _x
in inst_symlink inst_script inst_binary inst_simple
; do
1663 $_x "$@" && return 0
1668 # install any of listed files
1670 # If first argument is '-d' and second some destination path, first accessible
1671 # source is installed into this path, otherwise it will installed in the same
1672 # path as source. If none of listed files was installed, function return 1.
1673 # On first successful installation it returns with 0 status.
1677 # inst_any -d /bin/foo /bin/bar /bin/baz
1679 # Lets assume that /bin/baz exists, so it will be installed as /bin/foo in
1684 [[ $1 = '-d' ]] && to
="$2" && shift 2
1687 if [[ -e $f ]]; then
1688 [[ $to ]] && inst
"$f" "$to" && return 0
1689 inst
"$f" && return 0
1696 # dracut_install [-o ] <file> [<file> ... ]
1697 # Install <file> to the initramfs image
1698 # -o optionally install the <file> and don't fail, if it is not there
1701 if [[ $1 = '-o' ]]; then
1705 while (($# > 0)); do
1706 if ! inst
"$1" ; then
1707 if [[ $_optional = yes ]]; then
1708 dinfo
"Skipping program $1 as it cannot be found and is" \
1709 "flagged to be optional"
1711 dfatal
"Failed to install $1"
1719 # Install a single kernel module along with any firmware it may require.
1720 # $1 = full path to kernel module to install
1721 install_kmod_with_fw
() {
1722 # no need to go further if the module is already installed
1724 [[ -e "${initdir}/lib/modules/$KERNEL_VER/${1##*/lib/modules/$KERNEL_VER/}" ]] \
1727 [[ -e "$initdir/.kernelmodseen/${1##*/}" ]] && return 0
1729 if [[ $omit_drivers ]]; then
1730 local _kmod
=${1##*/}
1733 if [[ "$_kmod" =~
$omit_drivers ]]; then
1734 dinfo
"Omitting driver $_kmod"
1737 if [[ "${1##*/lib/modules/$KERNEL_VER/}" =~
$omit_drivers ]]; then
1738 dinfo
"Omitting driver $_kmod"
1743 [ -d "$initdir/.kernelmodseen" ] && \
1744 > "$initdir/.kernelmodseen/${1##*/}"
1746 inst_simple
"$1" "/lib/modules/$KERNEL_VER/${1##*/lib/modules/$KERNEL_VER/}" \
1749 local _modname
=${1##*/} _fwdir _found _fw
1750 _modname
=${_modname%.ko*}
1751 for _fw
in $
(modinfo
-k $KERNEL_VER -F firmware
$1 2>/dev
/null
); do
1753 for _fwdir
in $fw_dir; do
1754 if [[ -d $_fwdir && -f $_fwdir/$_fw ]]; then
1755 inst_simple
"$_fwdir/$_fw" "/lib/firmware/$_fw"
1759 if [[ $_found != yes ]]; then
1760 if ! grep -qe "\<${_modname//-/_}\>" /proc
/modules
; then
1761 dinfo
"Possible missing firmware \"${_fw}\" for kernel module" \
1762 "\"${_modname}.ko\""
1764 dwarn
"Possible missing firmware \"${_fw}\" for kernel module" \
1765 "\"${_modname}.ko\""
1772 # Do something with all the dependencies of a kernel module.
1773 # Note that kernel modules depend on themselves using the technique we use
1774 # $1 = function to call for each dependency we find
1775 # It will be passed the full path to the found kernel module
1776 # $2 = module to get dependencies for
1777 # rest of args = arguments to modprobe
1778 # _fderr specifies FD passed from surrounding scope
1779 for_each_kmod_dep
() {
1780 local _func
=$1 _kmod
=$2 _cmd _modpath _options _found
=0
1782 modprobe
"$@" --ignore-install --show-depends $_kmod 2>&${_fderr} |
(
1783 while read _cmd _modpath _options
; do
1784 [[ $_cmd = insmod
]] ||
continue
1785 $_func ${_modpath} ||
exit $?
1788 [[ $_found -eq 0 ]] && exit 1
1793 # filter kernel modules to install certain modules that meet specific
1795 # $1 = search only in subdirectory of /kernel/$1
1796 # $2 = function to call with module name to filter.
1797 # This function will be passed the full path to the module to test.
1798 # The behavior of this function can vary depending on whether $hostonly is set.
1799 # If it is, we will only look at modules that are already in memory.
1800 # If it is not, we will look at all kernel modules
1801 # This function returns the full filenames of modules that match $1
1802 filter_kernel_modules_by_path
() (
1803 local _modname _filtercmd
1804 if ! [[ $hostonly ]]; then
1805 _filtercmd
='find "$KERNEL_MODS/kernel/$1" "$KERNEL_MODS/extra"'
1806 _filtercmd
+=' "$KERNEL_MODS/weak-updates" -name "*.ko" -o -name "*.ko.gz"'
1807 _filtercmd
+=' -o -name "*.ko.xz"'
1808 _filtercmd
+=' 2>/dev/null'
1810 _filtercmd
='cut -d " " -f 1 </proc/modules|xargs modinfo -F filename '
1811 _filtercmd
+='-k $KERNEL_VER 2>/dev/null'
1813 for _modname
in $
(eval $_filtercmd); do
1815 *.ko
) "$2" "$_modname" && echo "$_modname";;
1816 *.ko.gz
) gzip -dc "$_modname" > $initdir/$$.ko
1817 $2 $initdir/$$.ko
&& echo "$_modname"
1818 rm -f $initdir/$$.ko
1820 *.ko.xz
) xz
-dc "$_modname" > $initdir/$$.ko
1821 $2 $initdir/$$.ko
&& echo "$_modname"
1822 rm -f $initdir/$$.ko
1827 find_kernel_modules_by_path
() (
1828 if ! [[ $hostonly ]]; then
1829 find "$KERNEL_MODS/kernel/$1" "$KERNEL_MODS/extra" "$KERNEL_MODS/weak-updates" \
1830 -name "*.ko" -o -name "*.ko.gz" -o -name "*.ko.xz" 2>/dev
/null
1832 cut
-d " " -f 1 </proc
/modules \
1833 |
xargs modinfo
-F filename
-k $KERNEL_VER 2>/dev
/null
1837 filter_kernel_modules
() {
1838 filter_kernel_modules_by_path drivers
"$1"
1841 find_kernel_modules
() {
1842 find_kernel_modules_by_path drivers
1845 # instmods [-c] <kernel module> [<kernel module> ... ]
1846 # instmods [-c] <kernel subsystem>
1847 # install kernel modules along with all their dependencies.
1848 # <kernel subsystem> can be e.g. "=block" or "=drivers/usb/storage"
1850 [[ $no_kernel = yes ]] && return
1851 # called [sub]functions inherit _fderr
1854 if [[ $1 = '-c' ]]; then
1859 function inst1mod
() {
1860 local _ret
=0 _mod
="$1"
1863 if [ -f $KERNEL_MODS/modules.
${_mod#=} ]; then
1864 ( [[ "$_mpargs" ]] && echo $_mpargs
1865 cat "${KERNEL_MODS}/modules.${_mod#=}" ) \
1868 ( [[ "$_mpargs" ]] && echo $_mpargs
1869 find "$KERNEL_MODS" -path "*/${_mod#=}/*" -type f
-printf '%f\n' ) \
1873 --*) _mpargs
+=" $_mod" ;;
1874 i2o_scsi
) return ;; # Do not load this diagnostic-only module
1877 # if we are already installed, skip this module and go on
1879 [[ -f "$initdir/.kernelmodseen/${_mod%.ko}.ko" ]] && return
1881 if [[ $omit_drivers ]] && [[ "$1" =~
$omit_drivers ]]; then
1882 dinfo
"Omitting driver ${_mod##$KERNEL_MODS}"
1885 # If we are building a host-specific initramfs and this
1886 # module is not already loaded, move on to the next one.
1887 [[ $hostonly ]] && ! grep -qe "\<${_mod//-/_}\>" /proc
/modules \
1888 && ! echo $add_drivers |
grep -qe "\<${_mod}\>" \
1891 # We use '-d' option in modprobe only if modules prefix path
1892 # differs from default '/'. This allows us to use Dracut with
1893 # old version of modprobe which doesn't have '-d' option.
1894 local _moddirname
=${KERNEL_MODS%%/lib/modules/*}
1895 [[ -n ${_moddirname} ]] && _moddirname
="-d ${_moddirname}/"
1897 # ok, load the module, all its dependencies, and any firmware
1899 for_each_kmod_dep install_kmod_with_fw
$_mod \
1900 --set-version $KERNEL_VER ${_moddirname} $_mpargs
1907 function instmods_1
() {
1909 if (($# == 0)); then # filenames from stdin
1911 inst1mod
"${_mod%.ko*}" ||
{
1912 if [ "$_check" = "yes" ]; then
1913 dfatal
"Failed to install $_mod"
1919 while (($# > 0)); do # filenames as arguments
1920 inst1mod
${1%.ko*} ||
{
1921 if [ "$_check" = "yes" ]; then
1922 dfatal
"Failed to install $1"
1931 local _ret _filter_not_found
='FATAL: Module .* not found.'
1933 # Capture all stderr from modprobe to _fderr. We could use {var}>...
1934 # redirections, but that would make dracut require bash4 at least.
1935 eval "( instmods_1 \"\$@\" ) ${_fderr}>&1" \
1936 |
while read line
; do [[ "$line" =~
$_filter_not_found ]] && echo $line ||
echo $line >&2 ;done | derror
1943 ln -fs ..
/usr
/bin
/systemctl
$initdir/bin
/
1944 ln -fs ..
/usr
/lib
/systemd
$initdir/lib
/
1945 inst_simple
"/usr/lib/systemd/system/haveged.service"
1949 if mountpoint
-q $1; then
1955 # can be overridden in specific test
1956 test_setup_cleanup
() {
1961 # (post-test) cleanup should always ignore failure and cleanup as much as possible
1964 _umount_dir
$initdir
1965 rm -vf "${IMAGESTATEDIR}/${IMAGE_NAME}.img"
1971 # can be overridden in specific test
1976 test_cleanup_again
() {
1977 [ -n "$TESTDIR" ] ||
return
1978 rm -rf "$TESTDIR/unprivileged-nspawn-root"
1979 _umount_dir
$initdir
1982 test_create_image
() {
1983 create_empty_image_rootdir
1985 # Create what will eventually be our root filesystem onto an overlay
1988 setup_basic_environment
1989 mask_supporting_services
1994 if [ ${TEST_REQUIRE_INSTALL_TESTS} -ne 0 ] && \
1995 type -P meson
>/dev
/null
&& \
1996 [[ "$(meson configure $BUILD_DIR | grep install-tests | awk '{ print $2 }')" != "true" ]]; then
1997 dfatal
"$BUILD_DIR needs to be built with -Dinstall-tests=true"
2001 image
="${TESTDIR}/${IMAGE_NAME}.img"
2002 public
="${IMAGESTATEDIR}/${IMAGE_NAME}.img"
2003 if [ -e "$image" ]; then
2004 echo "Reusing existing image $PWD/$image → $(realpath $image)"
2006 elif [ -e "$public" ]; then
2007 echo "Reusing existing cached image $PWD/$public → $(realpath $public)"
2008 ln -s "$(realpath $public)" "$image"
2020 if [ -z "$TEST_NO_QEMU" ]; then
2021 if run_qemu
"$1"; then
2022 check_result_qemu ||
{ echo "QEMU test failed"; return 1; }
2024 dwarn
"can't run QEMU, skipping"
2027 if [ -z "$TEST_NO_NSPAWN" ]; then
2029 if run_nspawn
"$initdir" "$1"; then
2030 check_result_nspawn
"$initdir" ||
{ echo "nspawn-root test failed"; return 1; }
2032 dwarn
"can't run systemd-nspawn, skipping"
2035 if [[ "$RUN_IN_UNPRIVILEGED_CONTAINER" = "yes" ]]; then
2036 dir
="$TESTDIR/unprivileged-nspawn-root"
2037 if NSPAWN_ARGUMENTS
="-U --private-network $NSPAWN_ARGUMENTS" run_nspawn
"$dir" "$1"; then
2038 check_result_nspawn
"$dir" ||
{ echo "unprivileged-nspawn-root test failed"; return 1; }
2040 dwarn
"can't run systemd-nspawn, skipping"
2048 if [[ $UID != "0" ]]; then
2049 echo "TEST: $TEST_DESCRIPTION [SKIPPED]: not root" >&2
2054 [[ $libdir ]] ||
for libdir
in /lib64
/lib
; do
2055 [[ -d $libdir ]] && libdirs
+=" $libdir" && break
2058 [[ $usrlibdir ]] ||
for usrlibdir
in /usr
/lib64
/usr
/lib
; do
2059 [[ -d $usrlibdir ]] && libdirs
+=" $usrlibdir" && break
2062 mkdir
-p "$STATEDIR"
2067 testname
="$(basename $PWD)"
2069 while (($# > 0)); do
2072 echo "${testname} RUN: $TEST_DESCRIPTION"
2075 if (( $ret == 0 )); then
2076 echo "${testname} RUN: $TEST_DESCRIPTION [OK]"
2078 echo "${testname} RUN: $TEST_DESCRIPTION [FAILED]"
2082 echo "${testname} SETUP: $TEST_DESCRIPTION"
2087 echo "${testname} CLEANUP: $TEST_DESCRIPTION"
2091 echo "${testname} CLEANUP AGAIN: $TEST_DESCRIPTION"
2096 echo -n "${testname}: $TEST_DESCRIPTION "
2101 ) </dev
/null
>"$TESTLOG" 2>&1 || ret
=$?
2103 if [ $ret -eq 0 ]; then