Skip to main content

max / mountaineer

3.4 KB · 101 lines History Blame Raw
1 #!/bin/sh
2 # vm-test-install.sh — boot the just-built Mountaineer ISO in a fresh
3 # disposable target VM and drive the installer non-interactively.
4 #
5 # Usage:
6 # tools/qemu/vm-test-install.sh [<config-yaml>]
7 #
8 # <config-yaml> defaults to tools/qemu/test-install.yaml — the installer's
9 # --config schema for unattended install. The target VM is destroyed and
10 # recreated each run, so this is safely re-runnable.
11
12 . "$(dirname "$0")/common.sh"
13 ensure_dirs
14
15 ISO=$(ls -t "$ROOT/out"/*.iso 2>/dev/null | head -1 || true)
16 if [ -z "$ISO" ] || [ ! -f "$ISO" ]; then
17 echo "error: no ISO found in $ROOT/out/. Run tools/qemu/vm-build.sh first." >&2
18 exit 1
19 fi
20
21 DAEMON=0
22 CONFIG=""
23 for arg in "$@"; do
24 case "$arg" in
25 --daemonize) DAEMON=1 ;;
26 *) CONFIG="$arg" ;;
27 esac
28 done
29 [ -n "$CONFIG" ] || CONFIG="$ROOT/tools/qemu/test-install.yaml"
30 if [ ! -f "$CONFIG" ]; then
31 echo "error: install config $CONFIG not found" >&2
32 exit 1
33 fi
34
35 TARGET_DISK="$DISK_DIR/target.qcow2"
36 TARGET_SIZE="${TARGET_SIZE:-20G}"
37
38 # Fresh disk every run.
39 rm -f "$TARGET_DISK"
40 qemu-img create -f qcow2 "$TARGET_DISK" "$TARGET_SIZE"
41
42 # Stage the config into a small fat32 image that the live ISO mounts.
43 # The installer looks for /media/cdrom/install.yaml first, then
44 # /mnt/config/install.yaml, then prompts interactively.
45 CFG_IMG="$DISK_DIR/install-config.img"
46 rm -f "$CFG_IMG"
47 truncate -s 8M "$CFG_IMG"
48 HAVE_CFG_IMG=0
49 if command -v mkfs.fat >/dev/null 2>&1 || command -v mkfs.vfat >/dev/null 2>&1; then
50 if command -v mkfs.fat >/dev/null 2>&1; then
51 mkfs.fat -n MTN-CFG "$CFG_IMG" >/dev/null 2>&1 && HAVE_CFG_IMG=1
52 else
53 mkfs.vfat -n MTN-CFG "$CFG_IMG" >/dev/null 2>&1 && HAVE_CFG_IMG=1
54 fi
55 fi
56 if [ "$HAVE_CFG_IMG" = 1 ] && command -v mcopy >/dev/null 2>&1; then
57 mcopy -i "$CFG_IMG" "$CONFIG" ::/install.yaml
58 else
59 HAVE_CFG_IMG=0
60 echo "note: dosfstools/mtools not installed; install config will not be auto-attached."
61 echo " brew install dosfstools mtools # for fully unattended runs"
62 echo " meanwhile: scp $CONFIG over after boot and run mountaineer-install --config <path>"
63 fi
64
65 LOG="$VMS/logs/target-install-serial.log"
66 MON="$VMS/sockets/target-install-mon.sock"
67 rm -f "$MON"
68
69 echo "==> booting target VM with ISO $ISO and fresh disk $TARGET_DISK"
70
71 if [ "$DAEMON" = 1 ]; then
72 echo " serial log: $LOG"
73 echo " ssh: ssh -p $TARGET_SSH_PORT -i $KEYS_DIR/builder_ed25519 root@127.0.0.1"
74 echo " stop: printf 'quit\\n' | nc -U $MON"
75 # shellcheck disable=SC2046
76 qemu-system-x86_64 \
77 $(qemu_base_args target) \
78 -serial file:"$LOG" \
79 -monitor unix:"$MON",server,nowait \
80 -daemonize \
81 -drive file="$TARGET_DISK",if=virtio,format=qcow2 \
82 -drive file="$CFG_IMG",if=virtio,format=raw,readonly=on \
83 -cdrom "$ISO" \
84 -boot d \
85 -netdev user,id=net0,hostfwd=tcp:127.0.0.1:$TARGET_SSH_PORT-:22 \
86 -device virtio-net-pci,netdev=net0
87 else
88 echo " serial console on this terminal; quit qemu with Ctrl-A then X"
89 echo ""
90 # shellcheck disable=SC2046
91 exec qemu-system-x86_64 \
92 $(qemu_base_args target) \
93 -serial mon:stdio \
94 -drive file="$TARGET_DISK",if=virtio,format=qcow2 \
95 -drive file="$CFG_IMG",if=virtio,format=raw,readonly=on \
96 -cdrom "$ISO" \
97 -boot d \
98 -netdev user,id=net0,hostfwd=tcp:127.0.0.1:$TARGET_SSH_PORT-:22 \
99 -device virtio-net-pci,netdev=net0
100 fi
101