2023-05-03 15:05:12 +02:00
#!/usr/bin/env bash
# SPDX-License-Identifier: LGPL-2.1-or-later
set -ex
set -o pipefail
systemd-analyze log-level debug
export SYSTEMD_LOG_LEVEL = debug
if [ -f /run/testsuite82.touch3 ] ; then
echo "This is the fourth boot!"
systemd-notify --status= "Fourth Boot"
rm /run/testsuite82.touch3
mount
rmdir /original-root /run/nextroot
# Check that the fdstore entry still exists
test " $LISTEN_FDS " -eq 3
read -r x <& 5
test " $x " = "oinkoink"
2023-07-22 23:35:40 +01:00
# Check that no service is still around
test " $( systemctl show -P ActiveState testsuite-82-survive.service) " != "active"
2023-05-03 15:05:12 +02:00
test " $( systemctl show -P ActiveState testsuite-82-nosurvive.service) " != "active"
# All succeeded, exit cleanly now
elif [ -f /run/testsuite82.touch2 ] ; then
echo "This is the third boot!"
systemd-notify --status= "Third Boot"
rm /run/testsuite82.touch2
# Check that the fdstore entry still exists
test " $LISTEN_FDS " -eq 2
read -r x <& 4
test " $x " = "miaumiau"
# Upload another entry
T = " /dev/shm/fdstore. $RANDOM "
echo "oinkoink" >" $T "
systemd-notify --fd= 3 --pid= parent 3<" $T "
rm " $T "
2023-07-22 23:35:40 +01:00
# Check that no service is still around
test " $( systemctl show -P ActiveState testsuite-82-survive.service) " != "active"
2023-05-03 15:05:12 +02:00
test " $( systemctl show -P ActiveState testsuite-82-nosurvive.service) " != "active"
# Test that we really are in the new overlayfs root fs
read -r x </lower
test " $x " = "miep"
2023-08-13 22:29:25 +01:00
cmp /etc/os-release /run/systemd/propagate/.os-release-stage/os-release
2023-07-18 15:44:27 +01:00
grep -q MARKER = 1 /etc/os-release
2023-05-03 15:05:12 +02:00
# Switch back to the original root, away from the overlayfs
mount --bind /original-root /run/nextroot
mount
# Now issue the soft reboot. We should be right back soon.
touch /run/testsuite82.touch3
systemctl --no-block soft-reboot
# Now block until the soft-boot killing spree kills us
exec sleep infinity
elif [ -f /run/testsuite82.touch ] ; then
echo "This is the second boot!"
systemd-notify --status= "Second Boot"
# Clean up what we created earlier
rm /run/testsuite82.touch
# Check that the fdstore entry still exists
test " $LISTEN_FDS " -eq 1
read -r x <& 3
test " $x " = "wuffwuff"
2023-08-30 19:51:13 +01:00
# Check that we got a PrepareForShutdownWithMetadata signal with the right type
test " $( jq .payload.data[ 1] .type.data </run/testsuite82.signal) " = "\"soft-reboot\""
2023-05-03 15:05:12 +02:00
# Upload another entry
T = " /dev/shm/fdstore. $RANDOM "
echo "miaumiau" >" $T "
systemd-notify --fd= 3 --pid= parent 3<" $T "
rm " $T "
2023-07-22 23:35:40 +01:00
# Check that no service survived, regardless of the configuration
test " $( systemctl show -P ActiveState testsuite-82-survive.service) " != "active"
2023-05-03 15:05:12 +02:00
test " $( systemctl show -P ActiveState testsuite-82-nosurvive.service) " != "active"
# This time we test the /run/nextroot/ root switching logic. (We synthesize a new rootfs from the old via overlayfs)
mkdir -p /run/nextroot /tmp/nextroot-lower /original-root
mount -t tmpfs tmpfs /tmp/nextroot-lower
echo miep >/tmp/nextroot-lower/lower
2023-07-18 15:44:27 +01:00
# Copy os-release away, so that we can manipulate it and check that it is updated in the propagate
2023-08-11 14:42:02 +01:00
# directory across soft reboots. Try to cover corner cases by truncating it.
2023-07-18 15:44:27 +01:00
mkdir -p /tmp/nextroot-lower/usr/lib
2023-08-11 14:42:02 +01:00
grep ID /etc/os-release >/tmp/nextroot-lower/usr/lib/os-release
2023-07-18 15:44:27 +01:00
echo MARKER = 1 >>/tmp/nextroot-lower/usr/lib/os-release
2023-08-13 22:29:25 +01:00
cmp /etc/os-release /run/systemd/propagate/.os-release-stage/os-release
2023-07-18 15:44:27 +01:00
( ! grep -q MARKER = 1 /etc/os-release)
mount -t overlay nextroot /run/nextroot -o lowerdir = /tmp/nextroot-lower:/,ro
2023-05-03 15:05:12 +02:00
# Bind our current root into the target so that we later can return to it
mount --bind / /run/nextroot/original-root
# Now issue the soft reboot. We should be right back soon.
touch /run/testsuite82.touch2
systemctl --no-block soft-reboot
# Now block until the soft-boot killing spree kills us
exec sleep infinity
else
# This is the first boot
systemd-notify --status= "First Boot"
2023-06-15 20:37:55 +02:00
# Let's upload an fd to the fdstore, so that we can verify fdstore passing works correctly
2023-05-03 15:05:12 +02:00
T = " /dev/shm/fdstore. $RANDOM "
echo "wuffwuff" >" $T "
systemd-notify --fd= 3 --pid= parent 3<" $T "
rm " $T "
# Create a script that can survive the soft reboot by ignoring SIGTERM (we
# do this instead of the argv[0][0] = '@' thing because that's so hard to
# do from a shell
T = " /dev/shm/survive- $RANDOM .sh "
cat >$T <<EOF
#!/bin/bash
trap "" TERM
systemd-notify --ready
rm " $T "
exec sleep infinity
EOF
chmod +x " $T "
2023-07-22 23:27:15 +01:00
# This sets DefaultDependencies=no so that it remains running until the
# very end, and IgnoreOnIsolate=yes so that it isn't stopped via the
# "testsuite.target" isolation we do on next boot
systemd-run -p Type = notify -p DefaultDependencies = no -p IgnoreOnIsolate = yes --unit= testsuite-82-survive.service " $T "
systemd-run -p Type = exec -p DefaultDependencies = no -p IgnoreOnIsolate = yes --unit= testsuite-82-nosurvive.service sleep infinity
2023-05-03 15:05:12 +02:00
2023-08-30 19:51:13 +01:00
# Check that we can set up an inhibitor, and that busctl monitor sees the
# PrepareForShutdownWithMetadata signal and that it says 'soft-reboot'.
systemd-run --unit busctl.service --property StandardOutput = file:/run/testsuite82.signal \
busctl monitor --json= pretty --match 'sender=org.freedesktop.login1,path=/org/freedesktop/login1,interface=org.freedesktop.login1.Manager,member=PrepareForShutdownWithMetadata,type=signal'
systemd-run --unit inhibit.service \
systemd-inhibit --what= shutdown --who= test --why= test --mode= delay \
sleep infinity
2023-05-03 15:05:12 +02:00
# Now issue the soft reboot. We should be right back soon.
touch /run/testsuite82.touch
2023-08-30 19:51:13 +01:00
systemctl --no-block --check-inhibitors= yes soft-reboot
2023-05-03 15:05:12 +02:00
# Now block until the soft-boot killing spree kills us
exec sleep infinity
fi
systemd-analyze log-level info
2023-07-12 15:49:55 +02:00
touch /testok
2023-05-03 15:05:12 +02:00
systemctl --no-block poweroff