1#!/bin/sh
2set -eu
3
4cache=${STUDIO_VM_CACHE_DIR:-$HOME/Library/Caches/clover-studio-vm-x86}
5mode=${1:-}
6qemu=$(command -v qemu-system-x86_64 || true)
7if [ -z "$qemu" ]; then
8 qemu=/opt/homebrew/bin/qemu-system-x86_64
9fi
10test -x "$qemu" || { echo 'qemu-system-x86_64 is unavailable' >&2; exit 1; }
11share=$(dirname "$(dirname "$qemu")")/share/qemu
12qemu_img=$(dirname "$qemu")/qemu-img
13mkdir -p "$cache"
14
15if [ ! -f "$cache/disk.qcow2" ]; then
16 "$qemu_img" create -f qcow2 "$cache/disk.qcow2" 96G
17fi
18if [ ! -f "$cache/vars.fd" ]; then
19 cp "$share/edk2-i386-vars.fd" "$cache/vars.fd"
20 chmod u+w "$cache/vars.fd"
21fi
22
23set --
24if [ "$mode" != installed ] && [ -f "$cache/nixos-minimal.iso" ]; then
25 set -- -drive "file=$cache/nixos-minimal.iso,media=cdrom,readonly=on"
26fi
27if [ "$mode" = installed ]; then
28 media=${STUDIO_VM_MEDIA_SOURCE:-/Volumes/media}
29 if ! mount | grep -F " on $media (" >/dev/null; then
30 echo "Mount the media share at $media before starting the VM" >&2
31 exit 1
32 fi
33 set -- "$@" -virtfs "local,path=$media,mount_tag=media,security_model=none,readonly=on"
34fi
35if [ "${STUDIO_VM_HEADLESS:-0}" = 1 ]; then
36 set -- "$@" -display none -serial "file:$cache/serial.log" -daemonize -pidfile "$cache/qemu.pid"
37else
38 set -- "$@" -display cocoa -serial mon:stdio
39fi
40
41set -- "$qemu" \
42 -machine q35 -accel tcg,tb-size=4096 -cpu max -smp 8 -m 32768 \
43 -drive "if=pflash,format=raw,readonly=on,file=$share/edk2-x86_64-code.fd" \
44 -drive "if=pflash,format=raw,file=$cache/vars.fd" \
45 -drive "if=virtio,format=qcow2,file=$cache/disk.qcow2" \
46 -netdev user,id=net0,ipv6=off,hostfwd=tcp:127.0.0.1:2222-:22 \
47 -device virtio-net-pci,netdev=net0 \
48 -device virtio-gpu-pci \
49 -qmp "unix:$cache/qmp.sock,server=on,wait=off" \
50 "$@"
51
52if command -v taskpolicy >/dev/null 2>&1; then
53 exec taskpolicy -c utility nice -n 10 "$@"
54fi
55exec nice -n 10 "$@"