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 # make sure the initdir is not mounted to avoid concurrent access
241 if [[ ! "$KERNEL_BIN" ]]; then
242 if [[ "$LOOKS_LIKE_ARCH" ]]; then
243 KERNEL_BIN
=/boot
/vmlinuz-linux
245 [ "$ARCH" ] || ARCH
=$
(uname
-m)
248 KERNEL_BIN
=/boot
/vmlinux-
$KERNEL_VER
252 KERNEL_BIN
=/boot
/vmlinuz-
$KERNEL_VER
258 default_fedora_initrd
=/boot
/initramfs-
${KERNEL_VER}.img
259 default_debian_initrd
=/boot
/initrd.img-
${KERNEL_VER}
260 default_arch_initrd
=/boot
/initramfs-linux-fallback.img
261 default_suse_initrd
=/boot
/initrd-
${KERNEL_VER}
262 if [[ ! "$INITRD" ]]; then
263 if [[ -e "$default_fedora_initrd" ]]; then
264 INITRD
="$default_fedora_initrd"
265 elif [[ "$LOOKS_LIKE_DEBIAN" && -e "$default_debian_initrd" ]]; then
266 INITRD
="$default_debian_initrd"
267 elif [[ "$LOOKS_LIKE_ARCH" && -e "$default_arch_initrd" ]]; then
268 INITRD
="$default_arch_initrd"
269 elif [[ "$LOOKS_LIKE_SUSE" && -e "$default_suse_initrd" ]]; then
270 INITRD
="$default_suse_initrd"
274 # If QEMU_SMP was not explicitly set, try to determine the value 'dynamically'
275 # i.e. use the number of online CPUs on the host machine. If the nproc utility
276 # is not installed or there's some other error when calling it, fall back
277 # to the original value (QEMU_SMP=1).
278 if ! [ "$QEMU_SMP" ]; then
279 if ! QEMU_SMP
=$
(nproc
); then
280 dwarn
"nproc utility is not installed, falling back to QEMU_SMP=1"
285 find_qemu_bin ||
return 1
287 # Umount initdir to avoid concurrent access to the filesystem
291 if [[ "$UNIFIED_CGROUP_HIERARCHY" = "yes" ]]; then
292 _cgroup_args
="systemd.unified_cgroup_hierarchy=yes"
293 elif [[ "$UNIFIED_CGROUP_HIERARCHY" = "no" ]]; then
294 _cgroup_args
="systemd.unified_cgroup_hierarchy=no systemd.legacy_systemd_cgroup_controller=yes"
295 elif [[ "$UNIFIED_CGROUP_HIERARCHY" = "hybrid" ]]; then
296 _cgroup_args
="systemd.unified_cgroup_hierarchy=no systemd.legacy_systemd_cgroup_controller=no"
297 elif [[ "$UNIFIED_CGROUP_HIERARCHY" != "default" ]]; then
298 dfatal
"Unknown UNIFIED_CGROUP_HIERARCHY. Got $UNIFIED_CGROUP_HIERARCHY, expected [yes|no|hybrid|default]"
302 if [[ "$LOOKS_LIKE_SUSE" ]]; then
303 PARAMS
+="rd.hostonly=0"
307 if [[ ! "$INTERACTIVE_DEBUG" ]]; then
308 _end
="systemd.wants=end.service"
313 KERNEL_APPEND
="$PARAMS \
323 SYSTEMD_UNIT_PATH=/usr/lib/systemd/tests/testdata/testsuite-$1.units:/usr/lib/systemd/tests/testdata/units: \
324 systemd.unit=testsuite.target \
325 systemd.wants=testsuite-$1.service ${_end} \
329 QEMU_OPTIONS
="-smp $QEMU_SMP \
333 -kernel $KERNEL_BIN \
334 -drive format=raw,cache=unsafe,file=${IMAGESTATEDIR}/${IMAGE_NAME}.img \
338 if [[ "$INITRD" && "$SKIP_INITRD" != "yes" ]]; then
339 QEMU_OPTIONS
="$QEMU_OPTIONS -initrd $INITRD"
342 # Let's use KVM if possible
343 if [[ -c /dev
/kvm
&& $QEMU_KVM == "yes" ]]; then
344 QEMU_OPTIONS
="$QEMU_OPTIONS -machine accel=kvm -enable-kvm -cpu host"
347 if [[ "$QEMU_TIMEOUT" != "infinity" ]]; then
348 QEMU_BIN
="timeout --foreground $QEMU_TIMEOUT $QEMU_BIN"
350 (set -x; $QEMU_BIN $QEMU_OPTIONS -append "$KERNEL_APPEND")
352 if [ "$rc" = 124 ] && [ "$QEMU_TIMEOUT" != "infinity" ]; then
353 derror
"test timed out after $QEMU_TIMEOUT s"
356 [ "$rc" != 0 ] && derror
"QEMU failed with exit code $rc"
361 # Return 0 if nspawn did run (then you must check the result state/logs for actual
362 # success), or 1 if nspawn is not available.
364 [[ -d /run
/systemd
/system
]] ||
return 1
368 --kill-signal=SIGKILL
370 --setenv=SYSTEMD_UNIT_PATH
=/usr
/lib
/systemd
/tests
/testdata
/testsuite-
$2.units
:/usr
/lib
/systemd
/tests
/testdata
/units
:
373 systemd.unit
=testsuite.target
374 systemd.wants
=testsuite-
$2.service
377 if [[ ! "$INTERACTIVE_DEBUG" ]]; then
378 _nspawn_cmd
+=( systemd.wants
=end.service
)
382 if [[ "$NSPAWN_TIMEOUT" != "infinity" ]]; then
383 _nspawn_pre
=(timeout
--foreground $NSPAWN_TIMEOUT)
388 if [[ "$UNIFIED_CGROUP_HIERARCHY" = "hybrid" ]]; then
389 dwarn
"nspawn doesn't support SYSTEMD_NSPAWN_UNIFIED_HIERARCHY=hybrid, skipping"
391 elif [[ "$UNIFIED_CGROUP_HIERARCHY" = "yes" ||
"$UNIFIED_CGROUP_HIERARCHY" = "no" ]]; then
392 _nspawn_pre
=("${nspawn_pre[@]}" env SYSTEMD_NSPAWN_UNIFIED_HIERARCHY
=$UNIFIED_CGROUP_HIERARCHY)
393 elif [[ "$UNIFIED_CGROUP_HIERARCHY" = "default" ]]; then
394 _nspawn_pre
=("${nspawn_pre[@]}" env
--unset=UNIFIED_CGROUP_HIERARCHY
--unset=SYSTEMD_NSPAWN_UNIFIED_HIERARCHY
)
396 dfatal
"Unknown UNIFIED_CGROUP_HIERARCHY. Got $UNIFIED_CGROUP_HIERARCHY, expected [yes|no|hybrid|default]"
400 (set -x; "${_nspawn_pre[@]}" "$SYSTEMD_NSPAWN" $NSPAWN_ARGUMENTS "${_nspawn_cmd[@]}")
402 if [ "$rc" = 124 ] && [ "$NSPAWN_TIMEOUT" != "infinity" ]; then
403 derror
"test timed out after $NSPAWN_TIMEOUT s"
406 [ "$rc" != 0 ] && derror
"nspawn failed with exit code $rc"
411 setup_basic_environment
() {
412 # create the basic filesystem layout
416 install_missing_libraries
433 has_user_dbus_socket
&& install_user_dbus
437 generate_module_dependencies
438 if [[ "$IS_BUILT_WITH_ASAN" = "yes" ]]; then
444 # don't forget KERNEL_APPEND='... selinux=1 ...'
445 if [[ "$SETUP_SELINUX" != "yes" ]]; then
446 ddebug
"Don't setup SELinux"
449 ddebug
"Setup SELinux"
450 local _conf_dir
=/etc
/selinux
451 local _fixfiles_tools
="bash uname cat sort uniq awk grep egrep head expr find rm secon setfiles"
453 rm -rf $initdir/$_conf_dir
454 if ! cp -ar $_conf_dir $initdir/$_conf_dir; then
455 dfatal
"Failed to copy $_conf_dir"
459 touch $initdir/.autorelabel
460 mkdir
-p $initdir/usr
/lib
/systemd
/tests
/testdata
/units
/basic.target.wants
461 ln -sf ..
/autorelabel.service
$initdir/usr
/lib
/systemd
/tests
/testdata
/units
/basic.target.wants
/
463 dracut_install
$_fixfiles_tools
464 dracut_install fixfiles
465 dracut_install sestatus
469 if ! type -p valgrind
; then
470 dfatal
"Failed to install valgrind"
474 local _valgrind_bins
=$
(strace
-e execve valgrind
/bin
/true
2>&1 >/dev
/null | perl
-lne 'print $1 if /^execve\("([^"]+)"/')
475 dracut_install
$_valgrind_bins
477 local _valgrind_libs
=$
(LD_DEBUG
=files valgrind
/bin
/true
2>&1 >/dev
/null | perl
-lne 'print $1 if m{calling init: (/.*vgpreload_.*)}')
478 dracut_install
$_valgrind_libs
480 local _valgrind_dbg_and_supp
=$
(
481 strace
-e open valgrind
/bin
/true
2>&1 >/dev
/null |
482 perl
-lne 'if (my ($fname) = /^open\("([^"]+).*= (?!-)\d+/) { print $fname if $fname =~ /debug|\.supp$/ }'
484 dracut_install
$_valgrind_dbg_and_supp
487 create_valgrind_wrapper
() {
488 local _valgrind_wrapper
=$initdir/$ROOTLIBDIR/systemd-under-valgrind
489 ddebug
"Create $_valgrind_wrapper"
490 cat >$_valgrind_wrapper <<EOF
493 mount -t proc proc /proc
494 exec valgrind --leak-check=full --log-file=/valgrind.out $ROOTLIBDIR/systemd "\$@"
496 chmod 0755 $_valgrind_wrapper
499 create_asan_wrapper
() {
500 local _asan_wrapper
=$initdir/$ROOTLIBDIR/systemd-under-asan
501 local _asan_rt_pattern
502 ddebug
"Create $_asan_wrapper"
504 case "$ASAN_COMPILER" in
506 _asan_rt_pattern
="*libasan*"
509 _asan_rt_pattern
="libclang_rt.asan-*"
510 # Install llvm-symbolizer to generate useful reports
511 # See: https://clang.llvm.org/docs/AddressSanitizer.html#symbolizing-the-reports
512 dracut_install
"llvm-symbolizer"
515 dfail
"Unsupported compiler: $ASAN_COMPILER"
519 cat >$_asan_wrapper <<EOF
524 DEFAULT_ASAN_OPTIONS=${ASAN_OPTIONS:-strict_string_checks=1:detect_stack_use_after_return=1:check_initialization_order=1:strict_init_order=1}
525 DEFAULT_UBSAN_OPTIONS=${UBSAN_OPTIONS:-print_stacktrace=1:print_summary=1:halt_on_error=1}
526 DEFAULT_ENVIRONMENT="ASAN_OPTIONS=\$DEFAULT_ASAN_OPTIONS UBSAN_OPTIONS=\$DEFAULT_UBSAN_OPTIONS"
528 # As right now bash is the PID 1, we can't expect PATH to have a sane value.
529 # Let's make one to prevent unexpected "<bin> not found" issues in the future
530 export PATH="/sbin:/bin:/usr/sbin:/usr/bin"
532 mount -t proc proc /proc
533 mount -t sysfs sysfs /sys
534 mount -o remount,rw /
536 PATH_TO_ASAN=\$(find / -name '$_asan_rt_pattern' | sed 1q)
537 if [[ "\$PATH_TO_ASAN" ]]; then
538 # A lot of services (most notably dbus) won't start without preloading libasan
539 # See https://github.com/systemd/systemd/issues/5004
540 DEFAULT_ENVIRONMENT="\$DEFAULT_ENVIRONMENT LD_PRELOAD=\$PATH_TO_ASAN"
541 # Let's add the ASan DSO's path to the dynamic linker's cache. This is pretty
542 # unnecessary for gcc & libasan, however, for clang this is crucial, as its
543 # runtime ASan DSO is in a non-standard (library) path.
544 echo \${PATH_TO_ASAN%/*} > /etc/ld.so.conf.d/asan-path-override.conf
547 echo DefaultEnvironment=\$DEFAULT_ENVIRONMENT >>/etc/systemd/system.conf
548 echo DefaultTimeoutStartSec=180s >>/etc/systemd/system.conf
549 echo DefaultStandardOutput=journal+console >>/etc/systemd/system.conf
551 # ASAN and syscall filters aren't compatible with each other.
552 find / -name '*.service' -type f | xargs sed -i 's/^\\(MemoryDeny\\|SystemCall\\)/#\\1/'
554 # The redirection of ASAN reports to a file prevents them from ending up in /dev/null.
555 # But, apparently, sometimes it doesn't work: https://github.com/google/sanitizers/issues/886.
556 JOURNALD_CONF_DIR=/etc/systemd/system/systemd-journald.service.d
557 mkdir -p "\$JOURNALD_CONF_DIR"
558 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"
560 # Sometimes UBSan sends its reports to stderr regardless of what is specified in log_path
561 # Let's try to catch them by redirecting stderr (and stdout just in case) to a file
562 # See https://github.com/systemd/systemd/pull/12524#issuecomment-491108821
563 printf "[Service]\nStandardOutput=file:/systemd-journald.out\n" >"\$JOURNALD_CONF_DIR/out.conf"
565 # 90s isn't enough for some services to finish when literally everything is run
566 # under ASan+UBSan in containers, which, in turn, are run in VMs.
567 # Let's limit which environments such services should be executed in.
568 mkdir -p /etc/systemd/system/systemd-hwdb-update.service.d
569 printf "[Unit]\nConditionVirtualization=container\n\n[Service]\nTimeoutSec=180s\n" >/etc/systemd/system/systemd-hwdb-update.service.d/env-override.conf
571 # Let's override another hard-coded timeout that kicks in too early
572 mkdir -p /etc/systemd/system/systemd-journal-flush.service.d
573 printf "[Service]\nTimeoutSec=180s\n" >/etc/systemd/system/systemd-journal-flush.service.d/timeout.conf
575 # The 'mount' utility doesn't behave well under libasan, causing unexpected
576 # fails during boot and subsequent test results check:
577 # bash-5.0# mount -o remount,rw -v /
578 # mount: /dev/sda1 mounted on /.
581 # Let's workaround this by clearing the previously set LD_PRELOAD env variable,
582 # so the libasan library is not loaded for this particular service
584 local _dropin_dir="/etc/systemd/system/\$1.service.d"
585 mkdir -p "\$_dropin_dir"
586 printf "[Service]\nUnsetEnvironment=LD_PRELOAD\n" >"\$_dropin_dir/unset_ld_preload.conf"
589 unset_ld_preload systemd-remount-fs
590 unset_ld_preload testsuite-
592 export ASAN_OPTIONS=\$DEFAULT_ASAN_OPTIONS:log_path=/systemd.asan.log UBSAN_OPTIONS=\$DEFAULT_UBSAN_OPTIONS
593 exec $ROOTLIBDIR/systemd "\$@"
596 chmod 0755 $_asan_wrapper
599 create_strace_wrapper
() {
600 local _strace_wrapper
=$initdir/$ROOTLIBDIR/systemd-under-strace
601 ddebug
"Create $_strace_wrapper"
602 cat >$_strace_wrapper <<EOF
605 exec strace -D -o /strace.out $ROOTLIBDIR/systemd "\$@"
607 chmod 0755 $_strace_wrapper
611 dracut_install
/sbin
/fsck
*
612 dracut_install
-o /bin
/fsck
*
614 # fskc.reiserfs calls reiserfsck. so, install it
615 dracut_install
-o reiserfsck
619 instmods dm_crypt
=crypto
621 if [[ "$LOOKS_LIKE_DEBIAN" ]]; then
622 # dmsetup installs 55-dm and 60-persistent-storage-dm on Debian/Ubuntu
623 # and since buster/bionic 95-dm-notify.rules
624 # see https://gitlab.com/debian-lvm/lvm2/blob/master/debian/patches/udev.patch
625 inst_rules
55-dm.rules
60-persistent-storage-dm.rules
95-dm-notify.rules
627 inst_rules
10-dm.rules
13-dm-disk.rules
95-dm-notify.rules
632 # install compiled files
633 local _ninja_bin
=$
(type -P ninja ||
type -P ninja-build
)
634 if [[ -z "$_ninja_bin" ]]; then
635 dfatal
"ninja was not found"
638 (set -x; DESTDIR
=$initdir "$_ninja_bin" -C $BUILD_DIR install)
639 # remove unneeded documentation
640 rm -fr $initdir/usr
/share
/{man
,doc
}
641 # we strip binaries since debug symbols increase binaries size a lot
642 # and it could fill the available space
645 [[ "$LOOKS_LIKE_SUSE" ]] && setup_suse
647 # enable debug logging in PID1
648 echo LogLevel
=debug
>> $initdir/etc
/systemd
/system.conf
649 # store coredumps in journal
650 echo Storage
=journal
>> $initdir/etc
/systemd
/coredump.conf
655 local rpath
=$
(objdump
-p "$_bin" 2>/dev
/null |
awk "/R(UN)?PATH/ { print \"$initdir\" \$2 }" |
paste -sd :)
657 if [ -z "$rpath" ] ; then
664 install_missing_libraries
() {
665 # install possible missing libraries
666 for i
in $initdir{,/usr
}/{sbin
,bin
}/* $initdir{,/usr
}/lib
/systemd
/{,tests
/{,manual
/,unsafe
/}}*; do
667 LD_LIBRARY_PATH
="${LD_LIBRARY_PATH:+$LD_LIBRARY_PATH:}$(get_ldpath $i)" inst_libs
$i
672 if [ -n "${LOOPDEV}" ]; then
673 ddebug
"losetup -d $LOOPDEV"
674 losetup
-d "${LOOPDEV}"
678 trap cleanup_loopdev EXIT
680 create_empty_image
() {
681 if [ -z "$IMAGE_NAME" ]; then
682 echo "create_empty_image: \$IMAGE_NAME not set"
687 if [[ "$STRIP_BINARIES" = "no" ]]; then
691 image
="${TESTDIR}/${IMAGE_NAME}.img"
692 public
="$IMAGESTATEDIR/${IMAGE_NAME}.img"
693 echo "Setting up $public (${_size} MB)"
694 rm -f "$image" "$public"
696 # Create the blank file to use as a root filesystem
697 truncate
-s "${_size}M" "$image"
698 ln -vs "$(realpath $image)" "$public"
700 LOOPDEV
=$
(losetup
--show -P -f "$public")
701 [ -b "$LOOPDEV" ] ||
return 1
702 sfdisk
"$LOOPDEV" <<EOF
709 local _label
="-L systemd.${name}"
710 # mkfs.reiserfs doesn't know -L. so, use --label instead
711 [[ "$FSTYPE" == "reiserfs" ]] && _label
="--label systemd.${name}"
712 mkfs
-t "${FSTYPE}" ${_label} "${LOOPDEV}p1" -q; ret
=$?
713 if [ $ret -ne 0 ] ; then
714 dfatal
"Failed to mkfs -t ${FSTYPE}"
720 if [ -z "${LOOPDEV}" ]; then
721 image
="${IMAGESTATEDIR}/${IMAGE_NAME}.img"
722 LOOPDEV
=$
(losetup
--show -P -f "$image")
723 [ -b "$LOOPDEV" ] ||
return 1
728 if ! mountpoint
-q $initdir; then
730 mount
${LOOPDEV}p1
$initdir
731 TEST_SETUP_CLEANUP_ROOTDIR
=1
736 # only umount if create_empty_image_rootdir() was called to mount it
737 [[ -z $TEST_SETUP_CLEANUP_ROOTDIR ]] || _umount_dir
$initdir
741 # unmount the loopback device from all places. Otherwise we risk file
743 image
="${IMAGESTATEDIR}/${IMAGE_NAME}.img"
744 for device
in $
(losetup
-l |
awk '$6=="'"$image"'" {print $1}'); do
745 ddebug
"Unmounting all uses of $device"
746 mount |
awk '/^'"${device}"'p/{print $1}' |
xargs --no-run-if-empty umount
-v
750 create_empty_image_rootdir
() {
755 check_asan_reports
() {
759 if [[ "$IS_BUILT_WITH_ASAN" = "yes" ]]; then
761 if [[ -e "$root/systemd.asan.log.1" ]]; then
762 cat "$root/systemd.asan.log.1"
766 journald_report
=$
(find "$root" -name "systemd-journald.*san.log*" -exec cat {} \
;)
767 if [[ ! -z "$journald_report" ]]; then
768 printf "%s\n" "$journald_report"
769 cat "$root/systemd-journald.out" ||
:
774 "$JOURNALCTL" -D "$root/var/log/journal" | perl
-alne '
776 %services_to_ignore = (
777 "dbus-daemon" => undef,
780 print $2 if /\s(\S*)\[(\d+)\]:\s*SUMMARY:\s+\w+Sanitizer/ && !exists $services_to_ignore{$1}'
782 if [[ ! -z "$pids" ]]; then
785 "$JOURNALCTL" -D "$root/var/log/journal" _PID
=$pid --no-pager
794 if [ -n "${ARTIFACT_DIRECTORY}" ]; then
795 dest
="${ARTIFACT_DIRECTORY}/${testname}.journal"
797 dest
="$TESTDIR/system.journal"
801 /usr
/lib
/systemd
/systemd-journal-remote \
803 --getter="journalctl -o export -D $j"
807 # we want to print this sometime later, so save this in a variable
808 JOURNAL_LIST
="$(ls -l $dest*)"
811 check_result_nspawn
() {
813 local journald_report
=""
815 [[ -e $1/testok
]] && ret
=0
816 [[ -f $1/failed
]] && cp -a $1/failed
$TESTDIR
817 save_journal
$1/var
/log
/journal
818 [[ -f $TESTDIR/failed
]] && cat $TESTDIR/failed
820 test -s $TESTDIR/failed
&& ret
=$
(($ret+1))
821 [ -n "$TIMED_OUT" ] && ret
=$
(($ret+1))
822 check_asan_reports
"$1" || ret
=$
(($ret+1))
827 # can be overridden in specific test
828 check_result_qemu
() {
831 [[ -e $initdir/testok
]] && ret
=0
832 [[ -f $initdir/failed
]] && cp -a $initdir/failed
$TESTDIR
833 save_journal
$initdir/var
/log
/journal
834 check_asan_reports
"$initdir" || ret
=$
(($ret+1))
836 [[ -f $TESTDIR/failed
]] && cat $TESTDIR/failed
838 test -s $TESTDIR/failed
&& ret
=$
(($ret+1))
839 [ -n "$TIMED_OUT" ] && ret
=$
(($ret+1))
844 if [[ "$STRIP_BINARIES" = "no" ]]; then
845 ddebug
"Don't strip binaries"
848 ddebug
"Strip binaries"
849 find "$initdir" -executable -not -path '*/lib/modules/*.ko' -type f | \
850 xargs strip
--strip-unneeded |
& \
851 grep -vi 'file format not recognized' | \
856 mkdir
-p $initdir/etc
/rc.d
857 cat >$initdir/etc
/rc.d
/rc.
local <<EOF
861 chmod 0755 $initdir/etc
/rc.d
/rc.
local
865 ddebug
"install any Execs from the service files"
867 export PKG_CONFIG_PATH
=$BUILD_DIR/src
/core
/
868 systemdsystemunitdir
=$
(pkg-config
--variable=systemdsystemunitdir systemd
)
869 systemduserunitdir
=$
(pkg-config
--variable=systemduserunitdir systemd
)
870 sed -r -n 's|^Exec[a-zA-Z]*=[@+!-]*([^ ]+).*|\1|gp' $initdir/{$systemdsystemunitdir,$systemduserunitdir}/*.service \
871 |
sort -u |
while read i
; do
872 # some {rc,halt}.local scripts and programs are okay to not exist, the rest should
873 # also, plymouth is pulled in by rescue.service, but even there the exit code
874 # is ignored; as it's not present on some distros, don't fail if it doesn't exist
875 dinfo
"Attempting to install $i"
876 inst
$i ||
[ "${i%.local}" != "$i" ] ||
[ "${i%systemd-update-done}" != "$i" ] ||
[ "/bin/plymouth" == "$i" ]
881 generate_module_dependencies
() {
882 if [[ -d $initdir/lib
/modules
/$KERNEL_VER ]] && \
883 ! depmod
-a -b "$initdir" $KERNEL_VER; then
884 dfatal
"\"depmod -a $KERNEL_VER\" failed."
889 install_depmod_files
() {
890 inst
/lib
/modules
/$KERNEL_VER/modules.order
891 inst
/lib
/modules
/$KERNEL_VER/modules.
builtin
895 # install plymouth, if found... else remove plymouth service files
896 # if [ -x /usr/libexec/plymouth/plymouth-populate-initrd ]; then
897 # PLYMOUTH_POPULATE_SOURCE_FUNCTIONS="$TEST_BASE_DIR/test-functions" \
898 # /usr/libexec/plymouth/plymouth-populate-initrd -t $initdir
899 # dracut_install plymouth plymouthd
901 rm -f $initdir/{usr
/lib
,etc
}/systemd
/system
/plymouth
* $initdir/{usr
/lib
,etc
}/systemd
/system
/*/plymouth
*
905 install_ld_so_conf
() {
906 cp -a /etc
/ld.so.conf
* $initdir/etc
907 ldconfig
-r "$initdir"
911 # create unprivileged user for user manager tests
912 mkdir
-p $initdir/etc
/sysusers.d
913 cat >$initdir/etc
/sysusers.d
/testuser.conf
<<EOF
914 u testuser 4711 "Test User" /home/testuser
917 mkdir
-p $initdir/home
/testuser
-m 0700
918 chown
4711:4711 $initdir/home
/testuser
921 install_config_files
() {
922 inst
/etc
/sysconfig
/init ||
:
928 inst
/etc
/nsswitch.conf
929 inst
/etc
/pam.conf ||
:
932 # we want an empty environment
933 > $initdir/etc
/environment
934 > $initdir/etc
/machine-id
937 echo systemd-testsuite
> $initdir/etc
/hostname
939 # let's set up just one image with the traditional verbose output
940 if [ ${IMAGE_NAME} != "basic" ]; then
941 mkdir
-p $initdir/etc
/systemd
/system.conf.d
942 echo -e '[Manager]\nStatusUnitFormat=name' >$initdir/etc
/systemd
/system.conf.d
/status.conf
946 install_basic_tools
() {
947 dracut_install
"${BASICTOOLS[@]}"
948 dracut_install
-o sushell
949 # in Debian ldconfig is just a shell script wrapper around ldconfig.real
950 dracut_install
-o ldconfig.real
953 install_debug_tools
() {
954 dracut_install
"${DEBUGTOOLS[@]}"
956 if [[ $INTERACTIVE_DEBUG ]]; then
957 # Set default TERM from vt220 to linux, so at least basic key shortcuts work
958 local _getty_override
="$initdir/etc/systemd/system/serial-getty@.service.d"
959 mkdir
-p "$_getty_override"
960 echo -e "[Service]\nEnvironment=TERM=linux" > "$_getty_override/default-TERM.conf"
962 cat > "$initdir/etc/motd" << EOF
963 To adjust the terminal size use:
973 # install libnss_files for login
974 NSS_LIBS
=$
(LD_DEBUG
=files getent passwd
2>&1 >/dev
/null |
sed -n '/calling init: .*libnss_/ {s!^.* /!/!; p}')
975 dracut_install
$NSS_LIBS
979 inst
$ROOTLIBDIR/system
/dbus.socket
981 # Newer Fedora versions use dbus-broker by default. Let's install it if it's available.
982 if [ -f $ROOTLIBDIR/system
/dbus-broker.service
]; then
983 inst
$ROOTLIBDIR/system
/dbus-broker.service
984 inst_symlink
/etc
/systemd
/system
/dbus.service
985 inst
/usr
/bin
/dbus-broker
986 inst
/usr
/bin
/dbus-broker-launch
987 elif [ -f $ROOTLIBDIR/system
/dbus-daemon.service
]; then
988 # Fedora rawhide replaced dbus.service with dbus-daemon.service
989 inst
$ROOTLIBDIR/system
/dbus-daemon.service
991 inst_symlink
/etc
/systemd
/system
/dbus.service
993 inst
$ROOTLIBDIR/system
/dbus.service
997 /etc
/dbus-1
/usr
/share
/dbus-1
-xtype f \
998 |
while read file; do
1002 # setup policy for Type=dbus test
1003 mkdir
-p $initdir/etc
/dbus-
1/system.d
1004 cat > $initdir/etc
/dbus-
1/system.d
/systemd.
test.ExecStopPost.conf
<<EOF
1005 <?xml version="1.0"?>
1006 <!DOCTYPE busconfig PUBLIC "-//freedesktop//DTD D-BUS Bus Configuration 1.0//EN"
1007 "http://www.freedesktop.org/standards/dbus/1.0/busconfig.dtd">
1009 <policy user="root">
1010 <allow own="systemd.test.ExecStopPost"/>
1016 install_user_dbus
() {
1017 inst
$ROOTLIBDIR/user
/dbus.socket
1018 inst_symlink
/usr
/lib
/systemd
/user
/sockets.target.wants
/dbus.socket || inst_symlink
/etc
/systemd
/user
/sockets.target.wants
/dbus.socket
1020 # Append the After= dependency on dbus in case it isn't already set up
1021 mkdir
-p "$initdir/etc/systemd/system/user@.service.d/"
1022 cat <<EOF >"$initdir/etc/systemd/system/user@.service.d/dbus.conf"
1027 # Newer Fedora versions use dbus-broker by default. Let's install it if it's available.
1028 if [ -f $ROOTLIBDIR/user
/dbus-broker.service
]; then
1029 inst
$ROOTLIBDIR/user
/dbus-broker.service
1030 inst_symlink
/etc
/systemd
/user
/dbus.service
1031 elif [ -f $ROOTLIBDIR/system
/dbus-daemon.service
]; then
1032 # Fedora rawhide replaced dbus.service with dbus-daemon.service
1033 inst
$ROOTLIBDIR/user
/dbus-daemon.service
1035 inst_symlink
/etc
/systemd
/user
/dbus.service
1037 inst
$ROOTLIBDIR/user
/dbus.service
1043 if [[ "$LOOKS_LIKE_DEBIAN" ]] && type -p dpkg-architecture
&>/dev
/null
; then
1044 find "/lib/$(dpkg-architecture -qDEB_HOST_MULTIARCH)/security" -xtype f
1046 find /lib
*/security
-xtype f
1048 find /etc
/pam.d
/etc
/security
-xtype f
1049 ) |
while read file; do
1053 # pam_unix depends on unix_chkpwd.
1054 # see http://www.linux-pam.org/Linux-PAM-html/sag-pam_unix.html
1055 dracut_install
-o unix_chkpwd
1057 [[ "$LOOKS_LIKE_DEBIAN" ]] &&
1058 cp /etc
/pam.d
/systemd-user
$initdir/etc
/pam.d
/
1060 # set empty root password for easy debugging
1061 sed -i 's/^root:x:/root::/' $initdir/etc
/passwd
1065 # The first three paths may be deprecated.
1066 # It seems now the last two paths are used by many distributions.
1068 /usr
/lib
/kbd
/keymaps
/include
/* \
1069 /usr
/lib
/kbd
/keymaps
/i386
/include
/* \
1070 /usr
/lib
/kbd
/keymaps
/i386
/qwerty
/us.
* \
1071 /usr
/lib
/kbd
/keymaps
/legacy
/include
/* \
1072 /usr
/lib
/kbd
/keymaps
/legacy
/i386
/qwerty
/us.
*; do
1073 [[ -f $i ]] ||
continue
1077 # When it takes any argument, then install more keymaps.
1078 if [[ -n $1 ]]; then
1080 /usr
/lib
/kbd
/keymaps
/i386
/*/* \
1081 /usr
/lib
/kbd
/keymaps
/legacy
/i386
/*/*; do
1082 [[ -f $i ]] ||
continue
1088 install_zoneinfo
() {
1089 inst_any
/usr
/share
/zoneinfo
/Asia
/Seoul
1090 inst_any
/usr
/share
/zoneinfo
/Asia
/Vladivostok
1091 inst_any
/usr
/share
/zoneinfo
/Australia
/Sydney
1092 inst_any
/usr
/share
/zoneinfo
/Europe
/Berlin
1093 inst_any
/usr
/share
/zoneinfo
/Europe
/Kiev
1094 inst_any
/usr
/share
/zoneinfo
/Pacific
/Auckland
1095 inst_any
/usr
/share
/zoneinfo
/Pacific
/Honolulu
1096 inst_any
/usr
/share
/zoneinfo
/CET
1097 inst_any
/usr
/share
/zoneinfo
/EET
1098 inst_any
/usr
/share
/zoneinfo
/UTC
1103 /usr
/lib
/kbd
/consolefonts
/eurlatgr
* \
1104 /usr
/lib
/kbd
/consolefonts
/latarcyrheb-sun16
*; do
1105 [[ -f $i ]] ||
continue
1110 install_terminfo
() {
1111 for _terminfodir
in /lib
/terminfo
/etc
/terminfo
/usr
/share
/terminfo
; do
1112 [ -f ${_terminfodir}/l
/linux
] && break
1114 dracut_install
-o ${_terminfodir}/l
/linux
1117 has_user_dbus_socket
() {
1118 if [ -f /usr
/lib
/systemd
/user
/dbus.socket
] ||
[ -f /etc
/systemd
/user
/dbus.socket
]; then
1121 echo "Per-user instances are not supported. Skipping..."
1126 setup_nspawn_root
() {
1127 if [ -z "${initdir}" ]; then
1128 dfatal
"\$initdir not defined"
1132 rm -rf "$TESTDIR/unprivileged-nspawn-root"
1134 if [[ "$RUN_IN_UNPRIVILEGED_CONTAINER" = "yes" ]]; then
1135 ddebug
"cp -ar $initdir $TESTDIR/unprivileged-nspawn-root"
1136 cp -ar $initdir $TESTDIR/unprivileged-nspawn-root
1140 setup_basic_dirs
() {
1141 mkdir
-p $initdir/run
1142 mkdir
-p $initdir/etc
/systemd
/system
1143 mkdir
-p $initdir/var
/log
/journal
1145 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
1146 if [ -L "/$d" ]; then
1153 ln -sfn /run
"$initdir/var/run"
1154 ln -sfn /run
/lock
"$initdir/var/lock"
1157 mask_supporting_services
() {
1158 # mask some services that we do not want to run in these tests
1159 ln -fs /dev
/null
$initdir/etc
/systemd
/system
/systemd-hwdb-update.service
1160 ln -fs /dev
/null
$initdir/etc
/systemd
/system
/systemd-journal-catalog-update.service
1161 ln -fs /dev
/null
$initdir/etc
/systemd
/system
/systemd-networkd.service
1162 ln -fs /dev
/null
$initdir/etc
/systemd
/system
/systemd-networkd.socket
1163 ln -fs /dev
/null
$initdir/etc
/systemd
/system
/systemd-resolved.service
1168 local _so_regex
='([^ ]*/lib[^/]*/[^ ]*\.so[^ ]*)'
1171 LC_ALL
=C ldd
"$_bin" 2>/dev
/null |
while read _line
; do
1172 [[ $_line = 'not a dynamic executable' ]] && break
1174 if [[ $_line =~
$_so_regex ]]; then
1175 _file
=${BASH_REMATCH[1]}
1176 [[ -e ${initdir}/$_file ]] && continue
1177 inst_library
"$_file"
1181 if [[ $_line =~ not\ found
]]; then
1182 dfatal
"Missing a shared library required by $_bin."
1183 dfatal
"Run \"ldd $_bin\" to find out what it is."
1185 dfatal
"dracut cannot create an initrd."
1192 # make sure we don't get a stale LOOPDEV value from old times
1194 [[ -e $STATEFILE ]] && .
$STATEFILE
1196 if [[ ! -d "$TESTDIR" ]]; then
1197 if [[ -z "$TESTDIR" ]]; then
1198 TESTDIR
=$
(mktemp
--tmpdir=/var
/tmp
-d -t systemd-test.XXXXXX
)
1203 cat >$STATEFILE<<EOF
1211 initdir
=$TESTDIR/root
1216 ## @brief Converts numeric logging level to the first letter of level name.
1218 # @param lvl Numeric logging level in range from 1 to 6.
1219 # @retval 1 if @a lvl is out of range.
1220 # @retval 0 if @a lvl is correct.
1221 # @result Echoes first letter of level name.
1234 ## @brief Internal helper function for _do_dlog()
1236 # @param lvl Numeric logging level.
1237 # @param msg Message.
1238 # @retval 0 It's always returned, even if logging failed.
1240 # @note This function is not supposed to be called manually. Please use
1241 # dtrace(), ddebug(), or others instead which wrap this one.
1243 # This function calls _do_dlog() either with parameter msg, or if
1244 # none is given, it will read standard input and will use every line as
1248 # dwarn "This is a warning"
1249 # echo "This is a warning" | dwarn
1250 LOG_LEVEL
=${LOG_LEVEL:-4}
1253 [ -z "$LOG_LEVEL" ] && return 0
1254 [ $1 -le $LOG_LEVEL ] ||
return 0
1255 local lvl
="$1"; shift
1256 local lvlc
=$
(_lvl2char
"$lvl") ||
return 0
1258 if [ $# -ge 1 ]; then
1262 echo "$lvlc: " "$line"
1267 ## @brief Logs message at TRACE level (6)
1269 # @param msg Message.
1270 # @retval 0 It's always returned, even if logging failed.
1274 [ -n "$debug" ] && set -x ||
:
1277 ## @brief Logs message at DEBUG level (5)
1279 # @param msg Message.
1280 # @retval 0 It's always returned, even if logging failed.
1284 # [ -n "$debug" ] && set -x || :
1287 ## @brief Logs message at INFO level (4)
1289 # @param msg Message.
1290 # @retval 0 It's always returned, even if logging failed.
1294 [ -n "$debug" ] && set -x ||
:
1297 ## @brief Logs message at WARN level (3)
1299 # @param msg Message.
1300 # @retval 0 It's always returned, even if logging failed.
1304 [ -n "$debug" ] && set -x ||
:
1307 ## @brief Logs message at ERROR level (2)
1309 # @param msg Message.
1310 # @retval 0 It's always returned, even if logging failed.
1314 # [ -n "$debug" ] && set -x || :
1317 ## @brief Logs message at FATAL level (1)
1319 # @param msg Message.
1320 # @retval 0 It's always returned, even if logging failed.
1324 [ -n "$debug" ] && set -x ||
:
1328 # Generic substring function. If $2 is in $1, return 0.
1329 strstr
() { [ "${1#*$2*}" != "$1" ]; }
1331 # normalize_path <path>
1332 # Prints the normalized path, where it removes any duplicated
1333 # and trailing slashes.
1335 # $ normalize_path ///test/test//
1339 set -- "${1//+(\/)//}"
1344 # convert_abs_rel <from> <to>
1345 # Prints the relative path, when creating a symlink to <to> from <from>.
1347 # $ convert_abs_rel /usr/bin/test /bin/test-2
1349 # $ ln -s $(convert_abs_rel /usr/bin/test /bin/test-2) /usr/bin/test
1351 local __current __absolute __abssize __cursize __newpath
1352 local -i __i __level
1354 set -- "$(normalize_path "$1")" "$(normalize_path "$2")"
1356 # corner case #1 - self looping link
1357 [[ "$1" == "$2" ]] && { echo "${1##*/}"; return; }
1359 # corner case #2 - own dir link
1360 [[ "${1%/*}" == "$2" ]] && { echo "."; return; }
1362 IFS
="/" __current
=($1)
1363 IFS
="/" __absolute
=($2)
1365 __abssize
=${#__absolute[@]}
1366 __cursize
=${#__current[@]}
1368 while [[ ${__absolute[__level]} == ${__current[__level]} ]]
1371 if (( __level
> __abssize || __level
> __cursize
))
1377 for ((__i
= __level
; __i
< __cursize-1
; __i
++))
1379 if ((__i
> __level
))
1381 __newpath
=$__newpath"/"
1383 __newpath
=$__newpath".."
1386 for ((__i
= __level
; __i
< __abssize
; __i
++))
1388 if [[ -n $__newpath ]]
1390 __newpath
=$__newpath"/"
1392 __newpath
=$__newpath${__absolute[__i]}
1399 # Install a directory, keeping symlinks as on the original system.
1400 # Example: if /lib points to /lib64 on the host, "inst_dir /lib/file"
1401 # will create ${initdir}/lib64, ${initdir}/lib64/file,
1402 # and a symlink ${initdir}/lib -> lib64.
1404 [[ -e ${initdir}/"$1" ]] && return 0 # already there
1406 local _dir
="$1" _part
="${1%/*}" _file
1407 while [[ "$_part" != "${_part%/*}" ]] && ! [[ -e "${initdir}/${_part}" ]]; do
1412 # iterate over parent directories
1413 for _file
in $_dir; do
1414 [[ -e "${initdir}/$_file" ]] && continue
1415 if [[ -L $_file ]]; then
1416 inst_symlink
"$_file"
1419 mkdir
-m 0755 -p "${initdir}/$_file" ||
return 1
1420 [[ -e "$_file" ]] && chmod --reference="$_file" "${initdir}/$_file"
1421 chmod u
+w
"${initdir}/$_file"
1426 # $1 = file to copy to ramdisk
1427 # $2 (optional) Name for the file on the ramdisk
1428 # Location of the image dir is assumed to be $initdir
1429 # We never overwrite the target if it exists.
1431 [[ -f "$1" ]] ||
return 1
1432 strstr
"$1" "/" ||
return 1
1434 local _src
=$1 target
="${2:-$1}"
1435 if ! [[ -d ${initdir}/$target ]]; then
1436 [[ -e ${initdir}/$target ]] && return 0
1437 [[ -L ${initdir}/$target ]] && return 0
1438 [[ -d "${initdir}/${target%/*}" ]] || inst_dir "${target%/*}"
1440 # install checksum files also
1441 if [[ -e "${_src%/*}/.${_src##*/}.hmac" ]]; then
1442 inst
"${_src%/*}/.${_src##*/}.hmac" "${target%/*}/.${target##*/}.hmac"
1444 ddebug
"Installing $_src"
1445 cp --sparse=always
-pfL "$_src" "${initdir}/$target"
1448 # find symlinks linked to given library file
1450 # Function searches for symlinks by stripping version numbers appended to
1451 # library filename, checks if it points to the same target and finally
1452 # prints the list of symlinks to stdout.
1455 # rev_lib_symlinks libfoo.so.8.1
1456 # output: libfoo.so.8 libfoo.so
1457 # (Only if libfoo.so.8 and libfoo.so exists on host system.)
1458 rev_lib_symlinks
() {
1459 [[ ! $1 ]] && return 0
1461 local fn
="$1" orig
="$(readlink -f "$1")" links
=''
1463 [[ ${fn} =~ .
*\.so\..
* ]] ||
return 1
1465 until [[ ${fn##*.} == so
]]; do
1467 [[ -L ${fn} && $(readlink -f "${fn}") == ${orig} ]] && links+=" ${fn}"
1473 # Same as above, but specialized to handle dynamic libraries.
1474 # It handles making symlinks according to how the original library
1477 local _src="$1" _dest=${2:-$1} _lib _reallib _symlink
1478 strstr "$1" "/" || return 1
1479 [[ -e $initdir/$_dest ]] && return 0
1480 if [[ -L $_src ]]; then
1481 # install checksum files also
1482 if [[ -e "${_src%/*}/.
${_src##*/}.hmac
" ]]; then
1483 inst "${_src%/*}/.${_src##*/}.hmac" "${_dest%/*}/.${_dest##*/}.hmac
"
1485 _reallib=$(readlink -f "$_src")
1486 inst_simple "$_reallib" "$_reallib"
1487 inst_dir "${_dest%/*}"
1488 [[ -d "${_dest%/*}" ]] && _dest=$(readlink -f "${_dest%/*}")/${_dest##*/}
1489 ln -sfn $
(convert_abs_rel
"${_dest}" "${_reallib}") "${initdir}/${_dest}"
1491 inst_simple
"$_src" "$_dest"
1494 # Create additional symlinks. See rev_symlinks description.
1495 for _symlink
in $
(rev_lib_symlinks
$_src) $
(rev_lib_symlinks
$_reallib); do
1496 [[ -e $initdir/$_symlink ]] ||
{
1497 ddebug
"Creating extra symlink: $_symlink"
1498 inst_symlink
$_symlink
1503 # find a binary. If we were not passed the full path directly,
1504 # search in the usual places to find the binary.
1506 if [[ -z ${1##/*} ]]; then
1507 if [[ -x $1 ]] ||
{ strstr
"$1" ".so" && ldd
$1 &>/dev
/null
; }; then
1516 # Same as above, but specialized to install binary executables.
1517 # Install binary executable, and all shared library dependencies, if any.
1521 # In certain cases we might attempt to install a binary which is already
1522 # present in the test image, yet it's missing from the host system.
1523 # In such cases, let's check if the binary indeed exists in the image
1524 # before doing any other chcecks. If it does, immediately return with
1526 [[ $# -eq 1 && -e $initdir/$1 ]] && return 0
1528 _bin
=$
(find_binary
"$1") ||
return 1
1530 [[ -e $initdir/$_target ]] && return 0
1531 [[ -L $_bin ]] && inst_symlink
$_bin $_target && return 0
1533 local _so_regex
='([^ ]*/lib[^/]*/[^ ]*\.so[^ ]*)'
1535 LC_ALL
=C ldd
"$_bin" 2>/dev
/null |
while read _line
; do
1536 [[ $_line = 'not a dynamic executable' ]] && break
1538 if [[ $_line =~
$_so_regex ]]; then
1539 _file
=${BASH_REMATCH[1]}
1540 [[ -e ${initdir}/$_file ]] && continue
1541 inst_library
"$_file"
1545 if [[ $_line =~ not\ found
]]; then
1546 dfatal
"Missing a shared library required by $_bin."
1547 dfatal
"Run \"ldd $_bin\" to find out what it is."
1549 dfatal
"dracut cannot create an initrd."
1553 inst_simple
"$_bin" "$_target"
1556 # same as above, except for shell scripts.
1557 # If your shell script does not start with shebang, it is not a shell script.
1560 _bin
=$
(find_binary
"$1") ||
return 1
1562 local _line _shebang_regex
1563 read -r -n 80 _line
<"$_bin"
1564 # If debug is set, clean unprintable chars to prevent messing up the term
1565 [[ $debug ]] && _line
=$
(echo -n "$_line" |
tr -c -d '[:print:][:space:]')
1566 _shebang_regex
='(#! *)(/[^ ]+).*'
1567 [[ $_line =~
$_shebang_regex ]] ||
return 1
1568 inst
"${BASH_REMATCH[2]}" && inst_simple
"$_bin" "$@"
1571 # same as above, but specialized for symlinks
1573 local _src
=$1 _target
=${2:-$1} _realsrc
1574 strstr
"$1" "/" ||
return 1
1575 [[ -L $1 ]] ||
return 1
1576 [[ -L $initdir/$_target ]] && return 0
1577 _realsrc
=$
(readlink
-f "$_src")
1578 if ! [[ -e $initdir/$_realsrc ]]; then
1579 if [[ -d $_realsrc ]]; then
1580 inst_dir
"$_realsrc"
1585 [[ ! -e $initdir/${_target%/*} ]] && inst_dir
"${_target%/*}"
1586 [[ -d ${_target%/*} ]] && _target=$(readlink -f ${_target%/*})/${_target##*/}
1587 ln -sfn $
(convert_abs_rel
"${_target}" "${_realsrc}") "$initdir/$_target"
1590 # attempt to install any programs specified in a udev rule
1591 inst_rule_programs
() {
1594 if grep -qE 'PROGRAM==?"[^ "]+' "$1"; then
1595 for _prog
in $
(grep -E 'PROGRAM==?"[^ "]+' "$1" |
sed -r 's/.*PROGRAM==?"([^ "]+).*/\1/'); do
1596 if [ -x /lib
/udev
/$_prog ]; then
1597 _bin
=/lib
/udev
/$_prog
1599 _bin
=$
(find_binary
"$_prog") ||
{
1600 dinfo
"Skipping program $_prog using in udev rule $(basename $1) as it cannot be found"
1605 #dinfo "Installing $_bin due to it's use in the udev rule $(basename $1)"
1606 dracut_install
"$_bin"
1611 # udev rules always get installed in the same place, so
1612 # create a function to install them to make life simpler.
1614 local _target
=/etc
/udev
/rules.d _rule _found
1616 inst_dir
"/lib/udev/rules.d"
1618 for _rule
in "$@"; do
1619 if [ "${rule#/}" = "$rule" ]; then
1620 for r
in /lib
/udev
/rules.d
/etc
/udev
/rules.d
; do
1621 if [[ -f $r/$_rule ]]; then
1623 inst_simple
"$_found"
1624 inst_rule_programs
"$_found"
1628 for r
in '' .
/ $dracutbasedir/rules.d
/; do
1629 if [[ -f ${r}$_rule ]]; then
1631 inst_simple
"$_found" "$_target/${_found##*/}"
1632 inst_rule_programs
"$_found"
1635 [[ $_found ]] || dinfo
"Skipping udev rule: $_rule"
1640 # general purpose installation function
1641 # Same args as above.
1647 2) [[ ! $initdir && -d $2 ]] && export initdir
=$2
1648 [[ $initdir = $2 ]] && set $1;;
1649 3) [[ -z $initdir ]] && export initdir
=$2
1651 *) dfatal
"inst only takes 1 or 2 or 3 arguments"
1654 for _x
in inst_symlink inst_script inst_binary inst_simple
; do
1655 $_x "$@" && return 0
1660 # install any of listed files
1662 # If first argument is '-d' and second some destination path, first accessible
1663 # source is installed into this path, otherwise it will installed in the same
1664 # path as source. If none of listed files was installed, function return 1.
1665 # On first successful installation it returns with 0 status.
1669 # inst_any -d /bin/foo /bin/bar /bin/baz
1671 # Lets assume that /bin/baz exists, so it will be installed as /bin/foo in
1676 [[ $1 = '-d' ]] && to
="$2" && shift 2
1679 if [[ -e $f ]]; then
1680 [[ $to ]] && inst
"$f" "$to" && return 0
1681 inst
"$f" && return 0
1688 # dracut_install [-o ] <file> [<file> ... ]
1689 # Install <file> to the initramfs image
1690 # -o optionally install the <file> and don't fail, if it is not there
1693 if [[ $1 = '-o' ]]; then
1697 while (($# > 0)); do
1698 if ! inst
"$1" ; then
1699 if [[ $_optional = yes ]]; then
1700 dinfo
"Skipping program $1 as it cannot be found and is" \
1701 "flagged to be optional"
1703 dfatal
"Failed to install $1"
1711 # Install a single kernel module along with any firmware it may require.
1712 # $1 = full path to kernel module to install
1713 install_kmod_with_fw
() {
1714 # no need to go further if the module is already installed
1716 [[ -e "${initdir}/lib/modules/$KERNEL_VER/${1##*/lib/modules/$KERNEL_VER/}" ]] \
1719 [[ -e "$initdir/.kernelmodseen/${1##*/}" ]] && return 0
1721 if [[ $omit_drivers ]]; then
1722 local _kmod
=${1##*/}
1725 if [[ "$_kmod" =~
$omit_drivers ]]; then
1726 dinfo
"Omitting driver $_kmod"
1729 if [[ "${1##*/lib/modules/$KERNEL_VER/}" =~
$omit_drivers ]]; then
1730 dinfo
"Omitting driver $_kmod"
1735 [ -d "$initdir/.kernelmodseen" ] && \
1736 > "$initdir/.kernelmodseen/${1##*/}"
1738 inst_simple
"$1" "/lib/modules/$KERNEL_VER/${1##*/lib/modules/$KERNEL_VER/}" \
1741 local _modname
=${1##*/} _fwdir _found _fw
1742 _modname
=${_modname%.ko*}
1743 for _fw
in $
(modinfo
-k $KERNEL_VER -F firmware
$1 2>/dev
/null
); do
1745 for _fwdir
in $fw_dir; do
1746 if [[ -d $_fwdir && -f $_fwdir/$_fw ]]; then
1747 inst_simple
"$_fwdir/$_fw" "/lib/firmware/$_fw"
1751 if [[ $_found != yes ]]; then
1752 if ! grep -qe "\<${_modname//-/_}\>" /proc
/modules
; then
1753 dinfo
"Possible missing firmware \"${_fw}\" for kernel module" \
1754 "\"${_modname}.ko\""
1756 dwarn
"Possible missing firmware \"${_fw}\" for kernel module" \
1757 "\"${_modname}.ko\""
1764 # Do something with all the dependencies of a kernel module.
1765 # Note that kernel modules depend on themselves using the technique we use
1766 # $1 = function to call for each dependency we find
1767 # It will be passed the full path to the found kernel module
1768 # $2 = module to get dependencies for
1769 # rest of args = arguments to modprobe
1770 # _fderr specifies FD passed from surrounding scope
1771 for_each_kmod_dep
() {
1772 local _func
=$1 _kmod
=$2 _cmd _modpath _options _found
=0
1774 modprobe
"$@" --ignore-install --show-depends $_kmod 2>&${_fderr} |
(
1775 while read _cmd _modpath _options
; do
1776 [[ $_cmd = insmod
]] ||
continue
1777 $_func ${_modpath} ||
exit $?
1780 [[ $_found -eq 0 ]] && exit 1
1785 # filter kernel modules to install certain modules that meet specific
1787 # $1 = search only in subdirectory of /kernel/$1
1788 # $2 = function to call with module name to filter.
1789 # This function will be passed the full path to the module to test.
1790 # The behavior of this function can vary depending on whether $hostonly is set.
1791 # If it is, we will only look at modules that are already in memory.
1792 # If it is not, we will look at all kernel modules
1793 # This function returns the full filenames of modules that match $1
1794 filter_kernel_modules_by_path
() (
1795 local _modname _filtercmd
1796 if ! [[ $hostonly ]]; then
1797 _filtercmd
='find "$KERNEL_MODS/kernel/$1" "$KERNEL_MODS/extra"'
1798 _filtercmd
+=' "$KERNEL_MODS/weak-updates" -name "*.ko" -o -name "*.ko.gz"'
1799 _filtercmd
+=' -o -name "*.ko.xz"'
1800 _filtercmd
+=' 2>/dev/null'
1802 _filtercmd
='cut -d " " -f 1 </proc/modules|xargs modinfo -F filename '
1803 _filtercmd
+='-k $KERNEL_VER 2>/dev/null'
1805 for _modname
in $
(eval $_filtercmd); do
1807 *.ko
) "$2" "$_modname" && echo "$_modname";;
1808 *.ko.gz
) gzip -dc "$_modname" > $initdir/$$.ko
1809 $2 $initdir/$$.ko
&& echo "$_modname"
1810 rm -f $initdir/$$.ko
1812 *.ko.xz
) xz
-dc "$_modname" > $initdir/$$.ko
1813 $2 $initdir/$$.ko
&& echo "$_modname"
1814 rm -f $initdir/$$.ko
1819 find_kernel_modules_by_path
() (
1820 if ! [[ $hostonly ]]; then
1821 find "$KERNEL_MODS/kernel/$1" "$KERNEL_MODS/extra" "$KERNEL_MODS/weak-updates" \
1822 -name "*.ko" -o -name "*.ko.gz" -o -name "*.ko.xz" 2>/dev
/null
1824 cut
-d " " -f 1 </proc
/modules \
1825 |
xargs modinfo
-F filename
-k $KERNEL_VER 2>/dev
/null
1829 filter_kernel_modules
() {
1830 filter_kernel_modules_by_path drivers
"$1"
1833 find_kernel_modules
() {
1834 find_kernel_modules_by_path drivers
1837 # instmods [-c] <kernel module> [<kernel module> ... ]
1838 # instmods [-c] <kernel subsystem>
1839 # install kernel modules along with all their dependencies.
1840 # <kernel subsystem> can be e.g. "=block" or "=drivers/usb/storage"
1842 [[ $no_kernel = yes ]] && return
1843 # called [sub]functions inherit _fderr
1846 if [[ $1 = '-c' ]]; then
1851 function inst1mod
() {
1852 local _ret
=0 _mod
="$1"
1855 if [ -f $KERNEL_MODS/modules.
${_mod#=} ]; then
1856 ( [[ "$_mpargs" ]] && echo $_mpargs
1857 cat "${KERNEL_MODS}/modules.${_mod#=}" ) \
1860 ( [[ "$_mpargs" ]] && echo $_mpargs
1861 find "$KERNEL_MODS" -path "*/${_mod#=}/*" -type f
-printf '%f\n' ) \
1865 --*) _mpargs
+=" $_mod" ;;
1866 i2o_scsi
) return ;; # Do not load this diagnostic-only module
1869 # if we are already installed, skip this module and go on
1871 [[ -f "$initdir/.kernelmodseen/${_mod%.ko}.ko" ]] && return
1873 if [[ $omit_drivers ]] && [[ "$1" =~
$omit_drivers ]]; then
1874 dinfo
"Omitting driver ${_mod##$KERNEL_MODS}"
1877 # If we are building a host-specific initramfs and this
1878 # module is not already loaded, move on to the next one.
1879 [[ $hostonly ]] && ! grep -qe "\<${_mod//-/_}\>" /proc
/modules \
1880 && ! echo $add_drivers |
grep -qe "\<${_mod}\>" \
1883 # We use '-d' option in modprobe only if modules prefix path
1884 # differs from default '/'. This allows us to use Dracut with
1885 # old version of modprobe which doesn't have '-d' option.
1886 local _moddirname
=${KERNEL_MODS%%/lib/modules/*}
1887 [[ -n ${_moddirname} ]] && _moddirname
="-d ${_moddirname}/"
1889 # ok, load the module, all its dependencies, and any firmware
1891 for_each_kmod_dep install_kmod_with_fw
$_mod \
1892 --set-version $KERNEL_VER ${_moddirname} $_mpargs
1899 function instmods_1
() {
1901 if (($# == 0)); then # filenames from stdin
1903 inst1mod
"${_mod%.ko*}" ||
{
1904 if [ "$_check" = "yes" ]; then
1905 dfatal
"Failed to install $_mod"
1911 while (($# > 0)); do # filenames as arguments
1912 inst1mod
${1%.ko*} ||
{
1913 if [ "$_check" = "yes" ]; then
1914 dfatal
"Failed to install $1"
1923 local _ret _filter_not_found
='FATAL: Module .* not found.'
1925 # Capture all stderr from modprobe to _fderr. We could use {var}>...
1926 # redirections, but that would make dracut require bash4 at least.
1927 eval "( instmods_1 \"\$@\" ) ${_fderr}>&1" \
1928 |
while read line
; do [[ "$line" =~
$_filter_not_found ]] && echo $line ||
echo $line >&2 ;done | derror
1935 ln -fs ..
/usr
/bin
/systemctl
$initdir/bin
/
1936 ln -fs ..
/usr
/lib
/systemd
$initdir/lib
/
1937 inst_simple
"/usr/lib/systemd/system/haveged.service"
1941 if mountpoint
-q $1; then
1947 # can be overridden in specific test
1948 test_setup_cleanup
() {
1953 # (post-test) cleanup should always ignore failure and cleanup as much as possible
1956 _umount_dir
$initdir
1957 rm -vf "${IMAGESTATEDIR}/${IMAGE_NAME}.img"
1963 # can be overridden in specific test
1968 test_cleanup_again
() {
1969 [ -n "$TESTDIR" ] ||
return
1970 rm -rf "$TESTDIR/unprivileged-nspawn-root"
1971 _umount_dir
$initdir
1974 test_create_image
() {
1975 create_empty_image_rootdir
1977 # Create what will eventually be our root filesystem onto an overlay
1980 setup_basic_environment
1981 mask_supporting_services
1986 if [ ${TEST_REQUIRE_INSTALL_TESTS} -ne 0 ] && \
1987 type -P meson
>/dev
/null
&& \
1988 [[ "$(meson configure $BUILD_DIR | grep install-tests | awk '{ print $2 }')" != "true" ]]; then
1989 dfatal
"Needs to be built with -Dinstall-tests=true"
1993 image
="${TESTDIR}/${IMAGE_NAME}.img"
1994 public
="${IMAGESTATEDIR}/${IMAGE_NAME}.img"
1995 if [ -e "$image" ]; then
1996 echo "Reusing existing image $PWD/$image → $(realpath $image)"
1998 elif [ -e "$public" ]; then
1999 echo "Reusing existing cached image $PWD/$public → $(realpath $public)"
2000 ln -s "$(realpath $public)" "$image"
2011 rm -f "$initdir"/{testok
,failed
,skipped
}
2013 if [ -z "$TEST_NO_QEMU" ]; then
2014 if run_qemu
"$1"; then
2015 check_result_qemu ||
{ echo "QEMU test failed"; return 1; }
2017 dwarn
"can't run QEMU, skipping"
2020 if [ -z "$TEST_NO_NSPAWN" ]; then
2022 if run_nspawn
"$initdir" "$1"; then
2023 check_result_nspawn
"$initdir" ||
{ echo "nspawn-root test failed"; return 1; }
2025 dwarn
"can't run systemd-nspawn, skipping"
2028 if [[ "$RUN_IN_UNPRIVILEGED_CONTAINER" = "yes" ]]; then
2029 dir
="$TESTDIR/unprivileged-nspawn-root"
2030 if NSPAWN_ARGUMENTS
="-U --private-network $NSPAWN_ARGUMENTS" run_nspawn
"$dir" "$1"; then
2031 check_result_nspawn
"$dir" ||
{ echo "unprivileged-nspawn-root test failed"; return 1; }
2033 dwarn
"can't run systemd-nspawn, skipping"
2041 if [[ $UID != "0" ]]; then
2042 echo "TEST: $TEST_DESCRIPTION [SKIPPED]: not root" >&2
2047 [[ $libdir ]] ||
for libdir
in /lib64
/lib
; do
2048 [[ -d $libdir ]] && libdirs
+=" $libdir" && break
2051 [[ $usrlibdir ]] ||
for usrlibdir
in /usr
/lib64
/usr
/lib
; do
2052 [[ -d $usrlibdir ]] && libdirs
+=" $usrlibdir" && break
2055 mkdir
-p "$STATEDIR"
2060 testname
="$(basename $PWD)"
2062 while (($# > 0)); do
2065 echo "${testname} RUN: $TEST_DESCRIPTION"
2068 if (( $ret == 0 )); then
2069 echo "${testname} RUN: $TEST_DESCRIPTION [OK]"
2071 echo "${testname} RUN: $TEST_DESCRIPTION [FAILED]"
2075 echo "${testname} SETUP: $TEST_DESCRIPTION"
2080 echo "${testname} CLEANUP: $TEST_DESCRIPTION"
2084 echo "${testname} CLEANUP AGAIN: $TEST_DESCRIPTION"
2089 echo -n "${testname}: $TEST_DESCRIPTION "
2094 ) </dev
/null
>"$TESTLOG" 2>&1 || ret
=$?
2096 if [ $ret -eq 0 ]; then