#!/usr/bin/env bash # Boots a Das OS ISO in QEMU (KVM when available). # # Usage: scripts/run-qemu.sh [--bios] [--disk] [--serial] [path/to/image.iso] # --bios boot with legacy BIOS instead of UEFI # --disk attach out/qemu/disk.qcow2 (20 GB, created on first use) to test installing # --serial no window: the serial console in this terminal (server ISO), Ctrl-A X quits # Without a path the newest desktop ISO in out/iso is used (server ISO with --serial). set -euo pipefail root=$(cd "$(dirname "$0")/.." && pwd) state=$root/out/qemu firmware=uefi disk=false serial=false iso="" die() { echo "run-qemu: $*" >&2 exit 1 } for arg in "$@"; do case $arg in --bios) firmware=bios ;; --disk) disk=true ;; --serial) serial=true ;; -h | --help) sed -n '2,9s/^# \{0,1\}//p' "$0" exit 0 ;; -*) die "unknown option: $arg" ;; *) iso=$arg ;; esac done if [[ -z $iso ]]; then pattern="dasos-[0-9]*.iso" [[ $serial == true ]] && pattern="dasos-server-*.iso" iso=$(ls -t "$root"/out/iso/$pattern 2>/dev/null | head -n 1) || true fi [[ -n $iso && -f $iso ]] || die "no ISO found, run 'make iso' first" args=( -machine q35 -smp 4 -m 4096 -device virtio-net-pci,netdev=net0 -netdev user,id=net0 -cdrom "$iso" -boot order=d ) if [[ $serial == true ]]; then # The guest's ttyS0 and the QEMU monitor share this terminal args+=(-nographic -serial mon:stdio) else args+=(-device virtio-vga -display gtk,zoom-to-fit=on -device qemu-xhci -device usb-tablet) fi if [[ -w /dev/kvm ]]; then args+=(-accel kvm -cpu host) else echo "run-qemu: /dev/kvm is not accessible, falling back to slow emulation" >&2 args+=(-accel tcg -cpu max) fi mkdir -p "$state" if [[ $firmware == uefi ]]; then code="" # Debian/Ubuntu/Mint, then Arch, then Fedora file names for candidate in /usr/share/OVMF/OVMF_CODE_4M.fd /usr/share/edk2/x64/OVMF_CODE.4m.fd /usr/share/edk2/ovmf/OVMF_CODE.fd; do [[ -f $candidate ]] && code=$candidate && break done [[ -n $code ]] || die "UEFI firmware (OVMF) not found, install the ovmf package or use --bios" vars_template=$(dirname "$code")/$(basename "$code" | sed 's/CODE/VARS/') [[ -f $state/OVMF_VARS.fd ]] || cp "$vars_template" "$state/OVMF_VARS.fd" args+=( -drive "if=pflash,format=raw,readonly=on,file=$code" -drive "if=pflash,format=raw,file=$state/OVMF_VARS.fd" ) fi if [[ $disk == true ]]; then [[ -f $state/disk.qcow2 ]] || qemu-img create -f qcow2 "$state/disk.qcow2" 20G args+=(-drive "file=$state/disk.qcow2,if=virtio,format=qcow2") fi exec qemu-system-x86_64 "${args[@]}"