#!/usr/bin/env bash # shellcheck shell=bash # # Boot a disposable UEFI VM with a scratch disk for installer testing. # # run-vm.sh --iso /path/to/livegui.iso --disk-size 32G --workdir /tmp/phoebus-vm # # The VM disk is a qcow2 file inside the work directory. No host disk is ever # touched. Delete the work directory to discard the test. set -euo pipefail ISO="" DISK_SIZE="32G" DISK_NAME="phoebus-test" WORKDIR="${TMPDIR:-/tmp}/phoebus-vm" MEM="4096" CPUS="4" BOOT="d" DISPLAY_OPT="gtk" GRAPHICS="auto" EXTRA=() usage() { cat <<'EOF' Usage: run-vm.sh --iso PATH [options] Options: --iso PATH Live ISO to boot (required) --disk-size SIZE Scratch disk size (default 32G) --disk-name NAME Scratch disk file name inside the workdir (default phoebus-test; the file is NAME.qcow2, so each interactive or automated run can use a unique disk) --workdir DIR Directory for the scratch disk (default /tmp/phoebus-vm) --mem MiB RAM in MiB (default 4096) --cpus N vCPUs (default 4) --display OPT QEMU display (default gtk; use none for headless) --graphics MODE auto (default), gl, or 2d; auto uses aligned 2D input --disk-boot Boot from the scratch disk instead of the ISO --help Show this help Requires qemu-system-x86_64 and OVMF (edk2) firmware. EOF } while (($#)); do case "$1" in --iso) shift; ISO="${1:-}" ;; --disk-size) shift; DISK_SIZE="${1:-}" ;; --disk-name) shift; DISK_NAME="${1:-}" ;; --workdir) shift; WORKDIR="${1:-}" ;; --mem) shift; MEM="${1:-}" ;; --cpus) shift; CPUS="${1:-}" ;; --display) shift; DISPLAY_OPT="${1:-}" ;; --graphics) shift; GRAPHICS="${1:-}" ;; --disk-boot) BOOT="c" ;; --help|-h) usage; exit 0 ;; *) echo "unknown option: $1" >&2; usage; exit 2 ;; esac shift done [[ -n "$ISO" ]] || { echo "error: --iso is required" >&2; usage; exit 2; } [[ "$GRAPHICS" == auto || "$GRAPHICS" == gl || "$GRAPHICS" == 2d ]] || { echo "error: --graphics must be auto, gl, or 2d" >&2; exit 2; } [[ -r "$ISO" ]] || { echo "error: cannot read ISO: $ISO" >&2; exit 2; } command -v qemu-system-x86_64 >/dev/null 2>&1 || { echo "error: qemu-system-x86_64 not found" >&2; exit 1; } # Locate OVMF firmware. OVMF_CODE="" OVMF_VARS_SRC="" for cand in \ /usr/share/OVMF/OVMF_CODE.fd \ /usr/share/OVMF/OVMF_CODE_4M.fd \ /usr/share/edk2/x64/OVMF_CODE.fd \ /usr/share/edk2-ovmf/x64/OVMF_CODE.fd; do if [[ -r "$cand" ]]; then OVMF_CODE="$cand"; fi done for cand in \ /usr/share/OVMF/OVMF_VARS.fd \ /usr/share/OVMF/OVMF_VARS_4M.fd \ /usr/share/edk2/x64/OVMF_VARS.fd \ /usr/share/edk2-ovmf/x64/OVMF_VARS.fd; do if [[ -r "$cand" ]]; then OVMF_VARS_SRC="$cand"; fi done [[ -n "$OVMF_CODE" ]] || { echo "error: OVMF firmware not found" >&2; exit 1; } mkdir -p "$WORKDIR" DISK="$WORKDIR/${DISK_NAME}.qcow2" VARS="$WORKDIR/OVMF_VARS.fd" if [[ ! -f "$DISK" ]]; then echo "creating scratch disk $DISK ($DISK_SIZE)" qemu-img create -f qcow2 "$DISK" "$DISK_SIZE" >/dev/null fi VIDEO_DEVICE="virtio-vga" if [[ "$DISPLAY_OPT" == gtk* && "$DISPLAY_OPT" != *zoom-to-fit=* ]]; then DISPLAY_OPT="${DISPLAY_OPT},zoom-to-fit=off" fi if [[ "$GRAPHICS" == gl && "$DISPLAY_OPT" == gtk* ]]; then render_node=false for node in /dev/dri/renderD*; do if [[ -r "$node" && -w "$node" ]]; then render_node=true; break; fi done gl_probe=false if [[ "$render_node" == true ]] && command -v timeout >/dev/null 2>&1; then probe_rc=0 timeout 2s qemu-system-x86_64 -machine q35 -nodefaults \ -device virtio-vga-gl -display egl-headless,gl=on \ -S -monitor none -serial none >/dev/null 2>&1 || probe_rc=$? if [[ "$probe_rc" == 0 || "$probe_rc" == 124 ]]; then gl_probe=true; fi fi if [[ "$render_node" == true && "$gl_probe" == true ]] \ && qemu-system-x86_64 -device help | grep 'name "virtio-vga-gl"' >/dev/null; then VIDEO_DEVICE="virtio-vga-gl" DISPLAY_OPT="${DISPLAY_OPT},gl=on" else echo "error: VirGL requires a writable DRM render node and working host EGL/VirGL" >&2 exit 1 fi elif [[ "$GRAPHICS" == gl ]]; then echo "error: --graphics gl requires a GTK display" >&2 exit 2 fi echo "Graphics: $VIDEO_DEVICE ($DISPLAY_OPT)" if [[ ! -f "$VARS" ]]; then cp "$OVMF_VARS_SRC" "$VARS" fi # KVM needs a writable /dev/kvm; without it `-cpu host` fails under TCG, so # fall back to `-cpu max` and warn. TCG is roughly an order of magnitude # slower than KVM: the live desktop still works but the installer stages # (especially package compilation) take much longer. ACCEL=() CPU="host" if [[ -w /dev/kvm ]]; then ACCEL=(-enable-kvm); else CPU="max" echo "warning: /dev/kvm is not writable; using TCG emulation (-cpu max)." >&2 echo "warning: expect roughly 5-10x slower execution than with KVM." >&2 fi echo "ISO: $ISO" echo "Disk: $DISK" echo "Workdir: $WORKDIR" echo "Firmware:$OVMF_CODE" echo echo "Inside the VM run: /tmp/phoebus-installer --plan" echo "Remember: the scratch disk is disposable." # QEMU option values deliberately contain comma-separated properties. # shellcheck disable=SC2054 QEMU=(qemu-system-x86_64 -machine q35 "${ACCEL[@]}" -m "$MEM" -smp "$CPUS" -cpu "$CPU" -display "$DISPLAY_OPT" -drive if=pflash,format=raw,readonly=on,file="$OVMF_CODE" -drive if=pflash,format=raw,file="$VARS" -drive file="$DISK",if=virtio,format=qcow2 -netdev user,id=n0 -device virtio-net-pci,netdev=n0 -device "$VIDEO_DEVICE" -device qemu-xhci -device usb-tablet -chardev socket,id=qga,path="$WORKDIR/qga.sock",server=on,wait=off -device virtio-serial-pci -device virtserialport,chardev=qga,name=org.qemu.guest_agent.0 -boot "order=$BOOT" -cdrom "$ISO" ) exec "${QEMU[@]}" "${EXTRA[@]}"