]> git.ipfire.org Git - thirdparty/systemd.git/blob - test/TEST-08-INITRD/test.sh
man/systemd.mount: tmpfs automatically gains After=swap.target dep
[thirdparty/systemd.git] / test / TEST-08-INITRD / test.sh
1 #!/usr/bin/env bash
2 # SPDX-License-Identifier: LGPL-2.1-or-later
3 set -e
4
5 TEST_DESCRIPTION="Test various scenarios involving transition from/to initrd"
6 TEST_NO_NSPAWN=1
7
8 # shellcheck source=test/test-functions
9 . "${TEST_BASE_DIR:?}/test-functions"
10
11 test_append_files() {
12 local workspace="${1:?}"
13 local sd_initrd file dir
14
15 # Create a shutdown initrd
16 #
17 # This should provide coverage for shutdown initrd related issues, see:
18 # - https://github.com/systemd/systemd/issues/28645
19 # - https://github.com/systemd/systemd/pull/28648
20 # - https://github.com/systemd/systemd/pull/28793
21 #
22 # This is a bit messier than I originally anticipated, as installing our own libraries
23 # is handled implicitly by install_systemd() which I don't want to use here, since
24 # I need only the systemd-shutdown binary
25 sd_initrd="$workspace/shutdown-initrd"
26 mkdir -p "$sd_initrd/etc" "$sd_initrd/usr"
27 initdir="$sd_initrd" image_install bash /usr/lib/os-release
28 ln -srf "$sd_initrd/usr/lib/os-release" "$sd_initrd/etc/initrd-release"
29 initdir="$sd_initrd" inst_binary "$workspace/usr/lib/systemd/systemd-shutdown" "/usr/lib/systemd/systemd-shutdown"
30 initdir="$sd_initrd" inst_libs "$sd_initrd/usr/lib/systemd/systemd-shutdown"
31 # We need to deal with libsystemd stuff explicitly, as we don't call install_systemd() here
32 while read -r file; do
33 initdir="$sd_initrd" inst_library "$file" "${file##"$workspace"}"
34 initdir="$sd_initrd" inst_libs "$file"
35 done < <(find "$workspace/usr/" -name "libsystemd*.so*")
36 # Call systemd-shutdown indirectly, so we can show a message that we can check for
37 # later to make sure the shutdown initrd was actually executed
38 cat >"$sd_initrd/shutdown" <<\EOF
39 #!/usr/bin/bash -eu
40 echo "Hello from shutdown initrd"
41 exec /usr/lib/systemd/systemd-shutdown "$@"
42 EOF
43 chmod +x "$sd_initrd/shutdown"
44 }
45
46 check_result_qemu_hook() {
47 local console_log="${TESTDIR:?}/console.log"
48
49 if [[ ! -e "$console_log" ]]; then
50 dfatal "Missing console log - this shouldn't happen"
51 return 1
52 fi
53
54 # The console log should not contain messages like:
55 # [ 6.245000] systemd-shutdown[1]: Failed to move /run/initramfs to /: Invalid argument
56 # [ 6.245955] systemd-shutdown[1]: Failed to switch root to "/run/initramfs": Invalid argument
57 if grep -qE "systemd-shutdown.+: Failed to move /run/initramfs" "$console_log" ||
58 grep -qE "systemd-shutdown.+: Failed to switch root" "$console_log"; then
59 derror "sd-shutdown failed to switch root in shutdown initrd"
60 return 1
61 fi
62
63 # Check if the shutdown initrd was executed at all
64 if ! grep -qE "^Hello from shutdown initrd\s*$" "$console_log"; then
65 derror "Missing 'hello' message from shutdown initrd"
66 return 1
67 fi
68
69 return 0
70 }
71
72 # Setup a one shot service in initrd that creates a dummy bind mount under /run
73 # to check if the mount persists though the initrd transition. The "check" part
74 # is in the respective testsuite-08.sh script.
75 #
76 # See: https://github.com/systemd/systemd/issues/28452
77 run_qemu_hook() {
78 local extra="${TESTDIR:?}/initrd.extra"
79
80 mkdir -m 755 "$extra"
81 mkdir -m 755 "$extra/etc" "$extra/etc/systemd" "$extra/etc/systemd/system" "$extra/etc/systemd/system/initrd.target.wants"
82
83 cat >"$extra/etc/systemd/system/initrd-run-mount.service" <<EOF
84 [Unit]
85 Description=Create a mount in /run that should survive the transition from initrd
86
87 [Service]
88 Type=oneshot
89 RemainAfterExit=yes
90 ExecStart=mkdir /run/initrd-mount-source /run/initrd-mount-target
91 ExecStart=mount -v --bind /run/initrd-mount-source /run/initrd-mount-target
92 ExecStart=cp -v /etc/initrd-release /run/initrd-mount-target/hello-world
93 EOF
94 ln -svrf "$extra/etc/systemd/system/initrd-run-mount.service" "$extra/etc/systemd/system/initrd.target.wants/initrd-run-mount.service"
95
96 (cd "$extra" && find . | cpio -o -H newc -R root:root > "$extra.cpio")
97
98 INITRD_EXTRA="$extra.cpio"
99 }
100
101 do_test "$@"