Skip to main content

max / mountaineer

2.5 KB · 75 lines History Blame Raw
1 # tools/qemu/common.sh — sourced helpers for the qemu wrapper scripts.
2 # Centralizes paths, firmware locations, and the QEMU invocation pattern
3 # so the per-task scripts (vm-up, vm-build, vm-test-install) stay terse.
4
5 set -eu
6
7 ROOT="$(cd "$(dirname "$0")/../.." && pwd)"
8 VMS="$ROOT/.vms"
9 ISO_DIR="$VMS/iso"
10 IMG_DIR="$VMS/images"
11 DISK_DIR="$VMS/disks"
12 NVRAM_DIR="$VMS/nvram"
13 KEYS_DIR="$VMS/keys"
14
15 # Alpine release pinning.
16 # ALPINE_VERSION is the full point version; the major.minor part selects
17 # the release branch directory, the full version names the file. Current
18 # as of 2026-05: 3.23.4 in main+community.
19 ALPINE_VERSION="${ALPINE_VERSION:-3.23.4}"
20 ALPINE_BRANCH="${ALPINE_VERSION%.*}"
21 ALPINE_ISO_URL="${ALPINE_ISO_URL:-https://dl-cdn.alpinelinux.org/alpine/v${ALPINE_BRANCH}/releases/x86_64/alpine-virt-${ALPINE_VERSION}-x86_64.iso}"
22 ALPINE_ISO="$ISO_DIR/$(basename "$ALPINE_ISO_URL")"
23 ALPINE_CLOUD_URL="${ALPINE_CLOUD_URL:-https://dl-cdn.alpinelinux.org/alpine/v${ALPINE_BRANCH}/releases/cloud/generic_alpine-${ALPINE_VERSION}-x86_64-uefi-cloudinit-r0.qcow2}"
24
25 OVMF_CODE="/opt/homebrew/share/qemu/edk2-x86_64-code.fd"
26 OVMF_VARS_TEMPLATE="/opt/homebrew/share/qemu/edk2-i386-vars.fd"
27
28 # Apple Silicon: no HVF for x86_64. Use TCG with as much help as we can give it.
29 # QEMU 11+: pass accel via a separate -accel flag, not as a -machine property
30 # (thread=multi is an -accel property, not a machine property).
31 ACCEL_ARGS="-accel tcg,thread=multi"
32 SMP="${SMP:-4}"
33 MEM="${MEM:-4G}"
34
35 # Forwarded SSH ports.
36 BUILDER_SSH_PORT="${BUILDER_SSH_PORT:-2222}"
37 TARGET_SSH_PORT="${TARGET_SSH_PORT:-2223}"
38
39 # Common QEMU invocation. Callers add: -drive, -cdrom, -netdev forwards,
40 # -name, AND a -serial flag (we don't pick one here because daemonized
41 # and foreground modes want different serial routing).
42 qemu_base_args() {
43 local name="$1"
44 local nvram="$NVRAM_DIR/${name}_VARS.fd"
45 if [ ! -f "$nvram" ]; then
46 cp "$OVMF_VARS_TEMPLATE" "$nvram"
47 fi
48 cat <<EOF
49 -name $name
50 -machine type=q35
51 $ACCEL_ARGS
52 -cpu max
53 -smp $SMP
54 -m $MEM
55 -drive if=pflash,format=raw,readonly=on,file=$OVMF_CODE
56 -drive if=pflash,format=raw,file=$nvram
57 -rtc base=utc
58 -device virtio-rng-pci
59 -vga virtio
60 -display none
61 EOF
62 }
63
64 ensure_alpine_iso() {
65 if [ ! -f "$ALPINE_ISO" ]; then
66 echo "==> downloading Alpine $ALPINE_VERSION ISO ..."
67 mkdir -p "$ISO_DIR"
68 curl -fL --progress-bar -o "$ALPINE_ISO" "$ALPINE_ISO_URL"
69 fi
70 }
71
72 ensure_dirs() {
73 mkdir -p "$ISO_DIR" "$IMG_DIR" "$DISK_DIR" "$NVRAM_DIR" "$KEYS_DIR"
74 }
75