| 1 | #!/bin/sh |
| 2 | set -eu |
| 3 | |
| 4 | cache=${STUDIO_VM_CACHE_DIR:-$HOME/Library/Caches/clover-studio-vm-x86} |
| 5 | mode=${1:-} |
| 6 | qemu=$(command -v qemu-system-x86_64 || true) |
| 7 | if [ -z "$qemu" ]; then |
| 8 | qemu=/opt/homebrew/bin/qemu-system-x86_64 |
| 9 | fi |
| 10 | test -x "$qemu" || { echo 'qemu-system-x86_64 is unavailable' >&2; exit 1; } |
| 11 | share=$(dirname "$(dirname "$qemu")")/share/qemu |
| 12 | qemu_img=$(dirname "$qemu")/qemu-img |
| 13 | mkdir -p "$cache" |
| 14 | |
| 15 | if [ ! -f "$cache/disk.qcow2" ]; then |
| 16 | "$qemu_img" create -f qcow2 "$cache/disk.qcow2" 96G |
| 17 | fi |
| 18 | if [ ! -f "$cache/vars.fd" ]; then |
| 19 | cp "$share/edk2-i386-vars.fd" "$cache/vars.fd" |
| 20 | chmod u+w "$cache/vars.fd" |
| 21 | fi |
| 22 | |
| 23 | set -- |
| 24 | if [ "$mode" != installed ] && [ -f "$cache/nixos-minimal.iso" ]; then |
| 25 | set -- -drive "file=$cache/nixos-minimal.iso,media=cdrom,readonly=on" |
| 26 | fi |
| 27 | if [ "$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" |
| 34 | fi |
| 35 | if [ "${STUDIO_VM_HEADLESS:-0}" = 1 ]; then |
| 36 | set -- "$@" -display none -serial "file:$cache/serial.log" -daemonize -pidfile "$cache/qemu.pid" |
| 37 | else |
| 38 | set -- "$@" -display cocoa -serial mon:stdio |
| 39 | fi |
| 40 | |
| 41 | set -- "$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 | |
| 52 | if command -v taskpolicy >/dev/null 2>&1; then |
| 53 | exec taskpolicy -c utility nice -n 10 "$@" |
| 54 | fi |
| 55 | exec nice -n 10 "$@" |