boot-overlay.sh

2.0 KB · raw

1#!/usr/bin/env bash2# Boot a THROWAWAY copy-on-write overlay of the golden image, run commands over3# the serial console, then discard the overlay. The golden image is never4# mutated, so every boot starts from the exact same snapshot.5#6# Usage: boot-overlay.sh <golden.qcow2> [command ...]7#   With no command, runs a nina smoke test (help / hello / status).8# Tunables via env: MEM_MB (2048), SMP (2), BOOT_TIMEOUT (600)9set -euo pipefail1011GOLDEN="${1:?usage: boot-overlay.sh <golden.qcow2> [command...]}"12shift || true13MEM_MB="${MEM_MB:-2048}"14SMP="${SMP:-2}"15BOOT_TIMEOUT="${BOOT_TIMEOUT:-600}"16[ -f "$GOLDEN" ] || { echo "missing golden: $GOLDEN" >&2; exit 1; }1718if [ "$#" -gt 0 ]; then CMD="$*"; else19  CMD='/root/nina help; echo ---; /root/nina hello; echo ---; /root/nina --version 2>/dev/null || true'20fi2122work="$(mktemp -d)"23trap 'rm -rf "$work"' EXIT24overlay="$work/overlay.qcow2"25qemu-img create -f qcow2 -b "$(cd "$(dirname "$GOLDEN")" && pwd)/$(basename "$GOLDEN")" -F qcow2 "$overlay" >/dev/null26echo "throwaway overlay: $overlay  (backing: $GOLDEN)"2728export BO_OVERLAY="$overlay" BO_MEM="$MEM_MB" BO_SMP="$SMP" \29       BO_TIMEOUT="$BOOT_TIMEOUT" BO_CMD="$CMD"3031expect -c '32  set timeout $env(BO_TIMEOUT)33  set overlay $env(BO_OVERLAY)34  set mem     $env(BO_MEM)35  set smp     $env(BO_SMP)36  set cmd     $env(BO_CMD)3738  spawn qemu-system-x86_64 \39    -machine q35 -accel tcg -cpu max -smp $smp -m $mem \40    -drive file=$overlay,if=virtio,format=qcow2 \41    -netdev user,id=n0 -device virtio-net-pci,netdev=n0 \42    -nographic4344  expect {45    timeout { puts "\n!! no autologin on serial"; exit 2 }46    "automatic login" {}47  }48  sleep 649  send "echo NINA_SMOKE_BEGIN\r"50  expect { timeout { puts "\n!! shell not ready"; exit 3 } "NINA_SMOKE_BEGIN" {} }51  send "$cmd\r"52  send "echo NINA_SMOKE_END_RC=\$?\r"53  expect {54    timeout { puts "\n!! command timed out"; exit 3 }55    -re {NINA_SMOKE_END_RC=[0-9]+} {}56  }57  send "poweroff\r"58  set timeout 12059  expect eof60'6162echo "=== overlay discarded; golden image unchanged ==="