#!/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 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. "$SYS" -y snapshot "rollback acceptance" > "$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 > "$WORK/break.out" 2>&1 echo "===ROLLBACK-BROKEN pkgs=$(pkg_fingerprint)===" # 4. Invoke the real sys undo path. "$SYS" -y undo > "$WORK/undo.out" 2>&1 RC=$? echo "===ROLLBACK-UNDO rc=$RC===" "$SYS" rollback status > "$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==="