#!/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 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" # The controlled break must genuinely mutate the system: a package that is # already installed would make the whole acceptance vacuous (snapshot and # restore of an identical tree). GNU hello is tiny and never in the base set. BREAK_PKG="app-misc/hello" BREAK_CAT="app-misc" BREAK_NAME="hello" if ls -d "/var/db/pkg/$BREAK_CAT/$BREAK_NAME-"* >/dev/null 2>&1; then echo "===ROLLBACK-BREAK-PRESENT $BREAK_PKG already installed; refusing a vacuous run===" exit 1 fi 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===" if [[ -z "$SNAP_ID" ]]; then echo "--- snapshot.out ---" sed -n '1,20p' "$WORK/snapshot.out" exit 1 fi # 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. The break # must change the package database; otherwise the restore comparison below # would pass without proving anything. The marker is mandatory: `sys undo` # restores the snapshotted root on the next boot, so the marker must exist # before undo (asserted here) and be absent after the detached reboot # (asserted by rollback-verify.sh and the host driver, not live: the live # root still shows the marker until the reboot activates the restored # generation). mkdir -p /etc/phoebus printf 'broken by rollback acceptance\n' > /etc/phoebus/rollback-broken.txt if [[ ! -f /etc/phoebus/rollback-broken.txt ]]; then echo "===ROLLBACK-MARKER-MISSING failed to create /etc/phoebus/rollback-broken.txt===" exit 1 fi echo "===ROLLBACK-MARKER present===" "$SYS" -y install "$BREAK_PKG" --legacy --portage > "$WORK/break.out" 2>&1 PKGS_BROKEN="$(pkg_fingerprint)" echo "===ROLLBACK-BROKEN pkgs=$PKGS_BROKEN===" if [[ "$PKGS_BROKEN" == "$PKGS_BEFORE" ]]; then echo "--- break.out ---" sed -n '1,30p' "$WORK/break.out" echo "===ROLLBACK-BREAK-NOOP the break did not change the package database===" exit 1 fi # 4. Invoke the real sys undo path, targeting our own snapshot id so the # restore point is exact (not "newest", which a protective snapshot taken # by the break step would win). Restoring our snapshot must remove both # the installed package and the root marker. "$SYS" -y undo "$SNAP_ID" --legacy > "$WORK/undo.out" 2>&1 RC=$? echo "===ROLLBACK-UNDO rc=$RC id=$SNAP_ID===" "$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==="