Files
reims-vgpu/scripts/vmapple-snapshot/vmapple-snapshot.sh
T
Anees IqbalandClaude Opus 5 c7b8c0a577 ✨ One flat snapshot history across every guest OS line makes current mean whichever booted last, so snapshots become per-rail
Touches both boot scripts (`vm/boot-x86.sh`, `vm/boot-arm64.sh`) and the arm64
snapshot helper. No Rust, no device or backend code.

A rail is one guest OS line. Rails are siblings under `vm/disks/rails/` (x86) and
`vm/guest/rails/` (arm64), each owning its own immutable snapshot history, and
`rails/current` names the one a bare boot gets:

  <rails>/<rail>/snapshots/<label>/...
  <rails>/<rail>/snapshots/current -> <label>
  <rails>/current -> <rail>

Two coordinates select what boots — `--rail NAME` and `--snapshot LABEL`, each
with its own `current` — and neither repoints anything. Snapshots could not stay
flat once a second guest line existed: two lines share no history, so one
namespace makes `current` mean "whichever guest was captured last", which is how
a measurement gets attributed to the wrong OS. Captures land in the selected rail
and move only that rail's `current`; `rails/current` is never touched, so
capturing on one line cannot silently change what the next bare boot runs.

An unknown rail or a half-populated snapshot is an error in every boot class.
Falling through to the bootstrap path would boot the provisioned masters instead
— a different guest than the one asked for — and under capture would then repoint
`current` at it.

Capture is now `--capture`. A bare `--snapshot` still means capture, because that
is how every existing invocation in the docs and helper scripts spells it; with a
label it selects instead. `--list-rails` and `--list-snapshots` answer before
anything is built.

A snapshot may carry its own `OVMF_CODE.fd` (x86) and a rail its own `vm.json`
(arm64). A macOS release is installed under whatever OVMF build its installer
ran on, and the NVRAM in `OVMF_VARS.fd` only means anything to the code half that
wrote it — a split OVMF pair is a matched set. Likewise the ECID identifies the
machine a guest was personalized against, so a second provisioned guest is a
second vm.json rather than a second disk under the first one's identity. Both
override only when present; a single-guest tree behaves exactly as before.

An arm64 tree still holding `vm/guest/snapshots/` now refuses to boot and prints
the three-line move that fixes it. That history is someone's only copy of a
provisioned guest, and a boot script is the wrong thing to have rearranged it
silently.

Cleanups, each within one file rather than a shared one:

- boot-x86.sh: the kernel-panic report was written out twice — once in the poll
  loop, once after QEMU exits on the panic reboot that `-action reboot=shutdown`
  turns into an apparently clean exit. Same regex, excerpt, keep-dir, exit code,
  and `basename` recomputed four times. Now `serial_has_panic` +
  `report_panic_and_revert`, called from both.
- boot-arm64.sh: `--device NAME` and `--device=NAME` each carried a copy of the
  accepted set and the error string; they now share `set_gfx_device`, matching
  what boot-x86.sh already did.
- boot-arm64.sh: `cp -c … || cp …` at four call sites becomes one `clone_file`,
  again matching boot-x86.sh.

Verified on the x86 pathway, Linux host, rail `macos-13` (the guest the previous
flat `current` pointed at, imported as a rail so the default boot is unchanged):

- `--testing --device reims-vgpu-pci` boots and the guest answers SSH as macOS
  13.7.8 — the pre-change behavior of a bare boot, preserved.
- `--testing --device vmware-svga --rail macos-13` with TESTING_TIMEOUT=60 exits
  124 through capture-then-revert and leaves no clone in the run dir, so the
  testing loop still works with the panic refactor inside it.
- `--rail macos-11 --testing` reverts that rail and reaches the guest desktop.
- Refusals exercised against a scratch tree: unknown rail, unknown snapshot,
  half-populated snapshot, `--rail ../etc`, unset default rail, and the pre-rail
  migration notice.
- `serial_has_panic` / `report_panic_and_revert` driven against a synthetic
  serial log: clean log detects nothing, panicking log reproduces the excerpt,
  keep-file and message the two copies produced.
- `clone_file` fallback exercised (GNU cp has no `-c`, so this host takes it).
- Both `--device` spellings on both scripts accept and reject as before.
- `bash -n` on both boot scripts and the snapshot helper.

NOT verified: rails macos-12, macos-14, macos-15 and macos-26 are imported and
resolve but were never booted. Nothing on the arm64 pathway ran — no Apple host —
so its rail resolution, the per-rail vm.json, `clone_file` on APFS and the
migration refusal were exercised only as far as a Linux shell reaches. `--capture`
was not run on either pathway; no clean-shutdown capture happened. shellcheck is
not installed on this host.

Separately measured and NOT addressed here: rail macos-11 under
`--device reims-vgpu-pci` boots, binds and presents zero-copy, but the composited
frame is empty (`present_black_retain`, alpha-only) and the screen is black. Big
Sur bring-up on the product device is its own problem, not an artifact of the
import — the same snapshot renders under vmware-svga.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-09 04:51:21 +03:00

126 lines
5.1 KiB
Bash
Executable File

#!/usr/bin/env bash
#
# scripts/vmapple-snapshot/vmapple-snapshot.sh
#
# Manage the vmapple guest's IMMUTABLE snapshot history. Snapshots are per-rail
# — a rail is one guest OS line — and live under
# vm/guest/rails/<rail>/snapshots/<label>/{disk.img,aux.img.trimmed} with a
# `current` symlink inside each rail. Snapshots are APFS clones (instant, COW)
# and read-only; they are NEVER overwritten. vm/boot-arm64.sh reverts to the
# selected rail's `current` on every boot and captures new snapshots via
# `--capture`; this tool covers the rest.
#
# Every command operates on ONE rail: --rail NAME, else $RAIL, else whatever
# vm/guest/rails/current names. Nothing here repoints `rails/current` — the
# default rail is a deliberate choice, not something a snapshot edit should move.
#
# rails list all rails (marks the default)
# list list the rail's snapshots (marks current)
# current print the rail's current snapshot label
# rollback <label> repoint the rail's `current` (no data touched)
# create [label] clone the at-rest guest bundle into a NEW snapshot in
# the rail and make it current (guest must be shut down)
#
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
REPO_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)"
GUEST_DIR="${GUEST_DIR:-$REPO_ROOT/vm/guest}"
RAILS_DIR="${RAILS_DIR:-$GUEST_DIR/rails}"
RAIL_LABEL="${RAIL:-}"
die() { echo "vmapple-snapshot: $*" >&2; exit 1; }
# `--rail NAME` may appear anywhere; strip it before the subcommand is read.
ARGS=()
while [ "$#" -gt 0 ]; do
case "$1" in
--rail) shift; RAIL_LABEL="${1:-}"; [ -n "$RAIL_LABEL" ] || die "--rail needs a name"; shift ;;
--rail=*) RAIL_LABEL="${1#--rail=}"; shift ;;
*) ARGS+=("$1"); shift ;;
esac
done
set -- ${ARGS[@]+"${ARGS[@]}"}
list_rail_labels() {
find "$RAILS_DIR" -mindepth 1 -maxdepth 1 -type d 2>/dev/null \
| while IFS= read -r d; do basename "$d"; done | sort
}
cmd_rails() {
local d; d="$(readlink "$RAILS_DIR/current" 2>/dev/null || echo "")"
d="$(basename "${d:-}" 2>/dev/null || echo "")"
list_rail_labels | while IFS= read -r name; do
local mark=" "; [ "$name" = "$d" ] && mark="* "
printf '%s%s\n' "$mark" "$name"
done
}
# `rails` is answerable without resolving one, so it runs before the resolve below.
if [ "${1:-list}" = "rails" ]; then cmd_rails; exit 0; fi
# --- Resolve the rail ------------------------------------------------------------
if [ -z "$RAIL_LABEL" ]; then
RAIL_LABEL="$(readlink "$RAILS_DIR/current" 2>/dev/null || true)"
[ -n "$RAIL_LABEL" ] || die \
"no default rail: $RAILS_DIR/current is unset (pass --rail NAME, or: ln -sfn <rail> $RAILS_DIR/current)
available: $(list_rail_labels | tr '\n' ' ')"
RAIL_LABEL="$(basename "$RAIL_LABEL")"
fi
case "$RAIL_LABEL" in
*/*|.|..) die "--rail takes a plain label, not a path: '$RAIL_LABEL'" ;;
esac
RAIL_DIR="$RAILS_DIR/$RAIL_LABEL"
[ -d "$RAIL_DIR" ] || die "no rail '$RAIL_LABEL' at $RAIL_DIR
available: $(list_rail_labels | tr '\n' ' ')"
SNAPSHOTS_DIR="$RAIL_DIR/snapshots"
CURRENT="$SNAPSHOTS_DIR/current"
cur_label() { readlink "$CURRENT" 2>/dev/null || echo ""; }
cmd_list() {
[ -d "$SNAPSHOTS_DIR" ] || die "rail '$RAIL_LABEL' has no snapshots dir at $SNAPSHOTS_DIR"
local c; c="$(cur_label)"
for d in "$SNAPSHOTS_DIR"/*/; do
[ -d "$d" ] || continue
local name; name="$(basename "$d")"
[ "$name" = "current" ] && continue # skip the `current` symlink
local mark=" "; [ "$name" = "$c" ] && mark="* "
printf '%s%s\n' "$mark" "$name"
done
}
cmd_current() { local c; c="$(cur_label)"; [ -n "$c" ] && echo "$c" || die "rail '$RAIL_LABEL' has no current snapshot"; }
cmd_rollback() {
local label="${1:-}"; [ -n "$label" ] || die "usage: rollback <label>"
[ -d "$SNAPSHOTS_DIR/$label" ] || die "rail '$RAIL_LABEL' has no such snapshot: $label (see: list)"
ln -sfn "$label" "$CURRENT"
echo "vmapple-snapshot: rail '$RAIL_LABEL' current -> $label"
}
cmd_create() {
[ -f "$GUEST_DIR/disk.img" ] && [ -f "$GUEST_DIR/aux.img.trimmed" ] \
|| die "no at-rest bundle at $GUEST_DIR (disk.img + aux.img.trimmed)"
if pgrep -f 'qemu-system-aarch6[4].*vmapple' >/dev/null 2>&1; then
die "guest is running — shut it down first (scripts/vmapple-shutdown) for a clean snapshot"
fi
local label="${1:-$(date +%Y-%m-%d-%H%M%S)-manual}"
local dir="$SNAPSHOTS_DIR/$label"
[ -e "$dir" ] && die "snapshot already exists in rail '$RAIL_LABEL': $label"
mkdir -p "$dir"
cp -c "$GUEST_DIR/disk.img" "$dir/disk.img" 2>/dev/null || cp "$GUEST_DIR/disk.img" "$dir/disk.img"
cp -c "$GUEST_DIR/aux.img.trimmed" "$dir/aux.img.trimmed" 2>/dev/null || cp "$GUEST_DIR/aux.img.trimmed" "$dir/aux.img.trimmed"
chmod 444 "$dir/disk.img" "$dir/aux.img.trimmed"
ln -sfn "$label" "$CURRENT"
echo "vmapple-snapshot: rail '$RAIL_LABEL' created + current -> $label"
}
case "${1:-list}" in
list) cmd_list ;;
current) cmd_current ;;
rollback) shift; cmd_rollback "$@" ;;
create) shift; cmd_create "$@" ;;
-h|--help) sed -n '2,24p' "$0" ;;
*) die "unknown command: ${1:-} (rails | list | current | rollback <label> | create [label])" ;;
esac