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