phoebus_os/installer/tests/vm/rollback-guest.sh
Phoebus 744059b6a2 fix(vm): provision Gentoo release key; truthful rollback gate
Live image ships gnupg plus the Gentoo release key, fingerprint-checked
at build time against the published release-key fingerprints, so the
fail-closed DIGESTS verification cannot be silently skipped.

Rollback harness: authenticate sudo (unique end markers, verified
timestamp, abort instead of hanging), run both guest phases as root,
use explicit --legacy for snapshot/break/undo, fix the verify SYS path,
emit the real systemd state, and preserve the phase-1 transcript.
2026-09-25 19:49:50 -07:00

73 lines
3 KiB
Bash
Executable file

#!/bin/bash
# Guest-side rollback acceptance for the disposable VM.
#
# Fetched and run by `serial-driver.py rollback` inside an already-installed
# Phoebus system. It establishes a known-good package/root state, creates a
# recovery point through the supported `sys` interface, introduces a
# controlled, reversible mutation, runs the real `sys undo` path and powers
# off. The host then boots the disk detached and verifies the restored state.
#
# `sys` is installed into the target by the installer; fall back to the host
# copy only if it is somehow absent (see docs/HANDOFF.md). This exercises the
# real installed command path, not a mock.
set -uo pipefail
# The snapshot/break/undo steps mutate the system and must run as root. The
# driver authenticates sudo before invoking this script; fail fast rather
# than recording vacuous "success" markers as an unprivileged user.
if [ "$(id -u)" != "0" ]; then
echo "===ROLLBACK-NEEDS-ROOT id=$(id -u)==="
exit 1
fi
SERVE="${SERVE_URL:-http://10.0.2.2:8000}"
if [[ -n "${SYS_BIN:-}" ]]; then
SYS="$SYS_BIN"
elif [[ -x /usr/local/bin/sys ]]; then
SYS=/usr/local/bin/sys
else
SYS=/tmp/sys
fi
USER_NAME="${USERNAME:-phoebus}"
WORK=/tmp/phoebus-rollback
mkdir -p "$WORK"
pkg_fingerprint() {
# The package database, not a marker file: a rollback must restore it.
find /var/db/pkg -mindepth 2 -maxdepth 2 -type d 2>/dev/null \
| sort | sha256sum | awk '{print $1}'
}
# 1. Establish and record a known-good root/package state. The /home marker
# proves user data survives (it is not part of the snapshotted subvolumes).
KERNEL="$(uname -r)"
HOME_MARKER="rollback-marker-$$-$KERNEL"
printf '%s\n' "$HOME_MARKER" > "/home/$USER_NAME/rollback-marker.txt"
PKGS_BEFORE="$(pkg_fingerprint)"
echo "===ROLLBACK-KNOWN-GOOD pkgs=$PKGS_BEFORE kernel=$KERNEL home=$HOME_MARKER==="
# 2. Create the recovery point through the supported interface. Explicit
# `--legacy` selects deterministic handling (no model); without it the
# request would enter the AI workflow and stop with no provider.
"$SYS" -y snapshot "rollback acceptance" --legacy > "$WORK/snapshot.out" 2>&1
SNAP_ID="$(sed -n 's/^protective snapshot: //p' "$WORK/snapshot.out" | tail -1)"
echo "===ROLLBACK-SNAPSHOT id=$SNAP_ID==="
# 3. Controlled, reversible update failure. A small package install is a real
# update: it changes both the root subvolume and /var/db/pkg, and a snapshot
# can restore it. A root marker proves the root generation too.
printf 'broken by rollback acceptance\n' > /etc/phoebus/rollback-broken.txt 2>/dev/null || true
"$SYS" -y install app-editors/nano --legacy --portage > "$WORK/break.out" 2>&1
echo "===ROLLBACK-BROKEN pkgs=$(pkg_fingerprint)==="
# 4. Invoke the real sys undo path.
"$SYS" -y undo --legacy > "$WORK/undo.out" 2>&1
RC=$?
echo "===ROLLBACK-UNDO rc=$RC==="
"$SYS" rollback status --legacy > "$WORK/status.out" 2>&1 || true
sed -n '1,40p' "$WORK/status.out"
echo "===ROLLBACK-DONE==="
sync
sudo -n poweroff -f || poweroff -f
echo "===POWEROFF==="