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)"2728expect -c '29  set timeout [lindex $argv 3]30  set overlay [lindex $argv 0]31  set mem     [lindex $argv 1]32  set smp     [lindex $argv 2]33  set cmd     [lindex $argv 4]3435  spawn qemu-system-x86_64 \36    -machine q35 -accel tcg -cpu max -smp $smp -m $mem \37    -drive file=$overlay,if=virtio,format=qcow2 \38    -netdev user,id=n0 -device virtio-net-pci,netdev=n0 \39    -nographic4041  expect {42    timeout { puts "\n!! no login on serial"; exit 2 }43    -re {\[root@test-primary[^\]]*\]#} {}44    -re {login:} { send "root\r"; exp_continue }45  }46  send "\r"47  expect -re {\[root@test-primary[^\]]*\]#}4849  send "echo NINA_SMOKE_BEGIN\r"50  expect "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' "$overlay" "$MEM_MB" "$SMP" "$BOOT_TIMEOUT" "$CMD"6162echo "=== overlay discarded; golden image unchanged ==="