#!/usr/bin/env bash # SPDX-License-Identifier: LGPL-2.1-or-later set -ex set -o pipefail # shellcheck source=test/units/util.sh . "$(dirname "$0")"/util.sh # Because this test tests soft-reboot, we have to get rid of the symlink we put at # /run/nextroot to allow rebooting into the previous snapshot if the test fails for # the duration of the test. However, let's make sure we put the symlink back in place # if the test fails. if [[ -L /run/nextroot ]]; then at_error() { mountpoint -q /run/nextroot && umount -R /run/nextroot rm -rf /run/nextroot ln -sf /snapshot /run/nextroot } trap at_error ERR rm -f /run/nextroot fi trigger_uevent() { local rule=/run/udev/rules.d/99-softreboot.rules if [[ ! -f "$rule" ]]; then mkdir -p /run/udev/rules.d cat >"$rule" <"$T" systemd-notify --fd=3 --pid=parent 3<"$T" rm "$T" # Check that the surviving services are still around test "$(systemctl show -P ActiveState TEST-82-SOFTREBOOT-survive.service)" = "active" test "$(systemctl show -P ActiveState TEST-82-SOFTREBOOT-survive-argv.service)" = "active" test "$(systemctl show -P ActiveState TEST-82-SOFTREBOOT-nosurvive-sigterm.service)" != "active" test "$(systemctl show -P ActiveState TEST-82-SOFTREBOOT-nosurvive.service)" != "active" # Test that we really are in the new overlayfs root fs read -r x "$T" systemd-notify --fd=3 --pid=parent 3<"$T" rm "$T" # Check that the surviving services are still around test "$(systemctl show -P ActiveState TEST-82-SOFTREBOOT-survive.service)" = "active" test "$(systemctl show -P ActiveState TEST-82-SOFTREBOOT-survive-argv.service)" = "active" test "$(systemctl show -P ActiveState TEST-82-SOFTREBOOT-nosurvive-sigterm.service)" != "active" test "$(systemctl show -P ActiveState TEST-82-SOFTREBOOT-nosurvive.service)" != "active" # Regression test for #41789: the lingering user enabled on the first boot must # have its user@.service started again after the soft reboot. Before the fix it # was GC'd at logind startup and never restarted. Disable lingering again here, # before the nextroot switch below (the third boot runs on a minimal overlay # rootfs that does not have this user). terminate-user is best-effort: it can # race the asynchronous disable-linger/UserStopDelaySec teardown. linger_uid=$(id -u testuser) timeout 30 bash -c "until systemctl is-active --quiet user@${linger_uid}.service; do sleep 1; done" loginctl disable-linger testuser loginctl terminate-user testuser || true # 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 # Copy os-release away, so that we can manipulate it and check that it is updated in the propagate # directory across soft reboots. Try to cover corner cases by truncating it. mkdir -p /tmp/nextroot-lower/etc grep ID /etc/os-release >/tmp/nextroot-lower/etc/os-release echo MARKER=1 >>/tmp/nextroot-lower/etc/os-release cmp /etc/os-release /run/systemd/propagate/.os-release-stage/os-release (! grep -q MARKER=1 /etc/os-release) mount -t overlay nextroot /run/nextroot -o lowerdir=/tmp/nextroot-lower:/,ro # Bind our current root into the target so that we later can return to it mount --bind / /run/nextroot/original-root # Restart the unit that is not supposed to survive systemd-run --collect --service-type=exec --unit=TEST-82-SOFTREBOOT-nosurvive.service sleep infinity # Now ensure there are no naming clashes and a bunch of transient units all succeed for _ in $(seq 1 25); do systemd-run --wait true done check_device_property 1 trigger_uevent # Now issue the soft reboot. We should be right back soon. Given /run/nextroot exists, we should # automatically do a softreboot instead of normal reboot. touch /run/TEST-82-SOFTREBOOT.touch2 systemctl --no-block 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" test "$(busctl -j get-property org.freedesktop.systemd1 /org/freedesktop/systemd1 org.freedesktop.systemd1.Manager SoftRebootsCount | jq -r '.data')" -eq 0 # Let's upload an fd to the fdstore, so that we can verify fdstore passing works correctly T="/dev/shm/fdstore.$RANDOM" echo "wuffwuff" >"$T" systemd-notify --fd=3 --pid=parent 3<"$T" rm "$T" survive_sigterm="/dev/shm/survive-sigterm-$RANDOM.sh" cat >"$survive_sigterm" <"$survive_argv" <