]> git.ipfire.org Git - thirdparty/systemd.git/blob - test/units/testsuite-82.sh
core: stage /run/host/os-release with a symlink to avoid possible race condition
[thirdparty/systemd.git] / test / units / testsuite-82.sh
1 #!/usr/bin/env bash
2 # SPDX-License-Identifier: LGPL-2.1-or-later
3 set -ex
4 set -o pipefail
5
6 systemd-analyze log-level debug
7
8 export SYSTEMD_LOG_LEVEL=debug
9
10 if [ -f /run/testsuite82.touch3 ]; then
11 echo "This is the fourth boot!"
12 systemd-notify --status="Fourth Boot"
13
14 rm /run/testsuite82.touch3
15 mount
16 rmdir /original-root /run/nextroot
17
18 # Check that the fdstore entry still exists
19 test "$LISTEN_FDS" -eq 3
20 read -r x <&5
21 test "$x" = "oinkoink"
22
23 # Check that no service is still around
24 test "$(systemctl show -P ActiveState testsuite-82-survive.service)" != "active"
25 test "$(systemctl show -P ActiveState testsuite-82-nosurvive.service)" != "active"
26
27 # All succeeded, exit cleanly now
28
29 elif [ -f /run/testsuite82.touch2 ]; then
30 echo "This is the third boot!"
31 systemd-notify --status="Third Boot"
32
33 rm /run/testsuite82.touch2
34
35 # Check that the fdstore entry still exists
36 test "$LISTEN_FDS" -eq 2
37 read -r x <&4
38 test "$x" = "miaumiau"
39
40 # Upload another entry
41 T="/dev/shm/fdstore.$RANDOM"
42 echo "oinkoink" >"$T"
43 systemd-notify --fd=3 --pid=parent 3<"$T"
44 rm "$T"
45
46 # Check that no service is still around
47 test "$(systemctl show -P ActiveState testsuite-82-survive.service)" != "active"
48 test "$(systemctl show -P ActiveState testsuite-82-nosurvive.service)" != "active"
49
50 # Test that we really are in the new overlayfs root fs
51 read -r x </lower
52 test "$x" = "miep"
53 cmp /etc/os-release /run/systemd/propagate/.os-release-stage/os-release
54 grep -q MARKER=1 /etc/os-release
55
56 # Switch back to the original root, away from the overlayfs
57 mount --bind /original-root /run/nextroot
58 mount
59
60 # Now issue the soft reboot. We should be right back soon.
61 touch /run/testsuite82.touch3
62 systemctl --no-block soft-reboot
63
64 # Now block until the soft-boot killing spree kills us
65 exec sleep infinity
66
67 elif [ -f /run/testsuite82.touch ]; then
68 echo "This is the second boot!"
69 systemd-notify --status="Second Boot"
70
71 # Clean up what we created earlier
72 rm /run/testsuite82.touch
73
74 # Check that the fdstore entry still exists
75 test "$LISTEN_FDS" -eq 1
76 read -r x <&3
77 test "$x" = "wuffwuff"
78
79 # Upload another entry
80 T="/dev/shm/fdstore.$RANDOM"
81 echo "miaumiau" >"$T"
82 systemd-notify --fd=3 --pid=parent 3<"$T"
83 rm "$T"
84
85 # Check that no service survived, regardless of the configuration
86 test "$(systemctl show -P ActiveState testsuite-82-survive.service)" != "active"
87 test "$(systemctl show -P ActiveState testsuite-82-nosurvive.service)" != "active"
88
89 # This time we test the /run/nextroot/ root switching logic. (We synthesize a new rootfs from the old via overlayfs)
90 mkdir -p /run/nextroot /tmp/nextroot-lower /original-root
91 mount -t tmpfs tmpfs /tmp/nextroot-lower
92 echo miep >/tmp/nextroot-lower/lower
93
94 # Copy os-release away, so that we can manipulate it and check that it is updated in the propagate
95 # directory across soft reboots. Try to cover corner cases by truncating it.
96 mkdir -p /tmp/nextroot-lower/usr/lib
97 grep ID /etc/os-release >/tmp/nextroot-lower/usr/lib/os-release
98 echo MARKER=1 >>/tmp/nextroot-lower/usr/lib/os-release
99 cmp /etc/os-release /run/systemd/propagate/.os-release-stage/os-release
100 (! grep -q MARKER=1 /etc/os-release)
101
102 mount -t overlay nextroot /run/nextroot -o lowerdir=/tmp/nextroot-lower:/,ro
103
104 # Bind our current root into the target so that we later can return to it
105 mount --bind / /run/nextroot/original-root
106
107 # Now issue the soft reboot. We should be right back soon.
108 touch /run/testsuite82.touch2
109 systemctl --no-block soft-reboot
110
111 # Now block until the soft-boot killing spree kills us
112 exec sleep infinity
113 else
114 # This is the first boot
115 systemd-notify --status="First Boot"
116
117 # Let's upload an fd to the fdstore, so that we can verify fdstore passing works correctly
118 T="/dev/shm/fdstore.$RANDOM"
119 echo "wuffwuff" >"$T"
120 systemd-notify --fd=3 --pid=parent 3<"$T"
121 rm "$T"
122
123 # Create a script that can survive the soft reboot by ignoring SIGTERM (we
124 # do this instead of the argv[0][0] = '@' thing because that's so hard to
125 # do from a shell
126 T="/dev/shm/survive-$RANDOM.sh"
127 cat >$T <<EOF
128 #!/bin/bash
129 trap "" TERM
130 systemd-notify --ready
131 rm "$T"
132 exec sleep infinity
133 EOF
134 chmod +x "$T"
135 # This sets DefaultDependencies=no so that it remains running until the
136 # very end, and IgnoreOnIsolate=yes so that it isn't stopped via the
137 # "testsuite.target" isolation we do on next boot
138 systemd-run -p Type=notify -p DefaultDependencies=no -p IgnoreOnIsolate=yes --unit=testsuite-82-survive.service "$T"
139 systemd-run -p Type=exec -p DefaultDependencies=no -p IgnoreOnIsolate=yes --unit=testsuite-82-nosurvive.service sleep infinity
140
141 # Now issue the soft reboot. We should be right back soon.
142 touch /run/testsuite82.touch
143 systemctl --no-block soft-reboot
144
145 # Now block until the soft-boot killing spree kills us
146 exec sleep infinity
147 fi
148
149 systemd-analyze log-level info
150
151 touch /testok
152 systemctl --no-block poweroff