#!/bin/sh

# sudo tunctl -t tap0 -g netdev
# qemu-img create -f qcow2 nb-disk0.qcow2 10G

NBISO="${HOME}/Downloads/NetBSD-10.99.14-amd64.iso"
NBISO="${HOME}/Downloads/NetBSD-10.1_STABLE-amd64.iso"

HERE="$(dirname "${0}")"

boot=c
cdrom=
name=NetBSD

case "${1}" in
install)
  boot=d
  cdrom="${NBISO}"
  name="${name} Installer"
  ;;
esac


qemu-system-x86_64 \
  -name "${name}" \
  -m 1G \
  \
  -enable-kvm \
  -nodefaults \
  -readconfig "${HERE}/pve-q35-4.0.cfg" \
  \
  -machine type=q35,vmport=off \
  -smbios type=1,uuid=2e6f0d62-3938-4ad3-b634-97cec8ceeff9 \
  \
  -global ICH9-LPC.acpi-pci-hotplug-with-bridge-support=off \
  -global ICH9-LPC.disable_s3=1 \
  -global ICH9-LPC.disable_s4=1 \
  \
  -smp 2,sockets=1,cores=2,maxcpus=2 \
  -cpu host,+kvm_pv_eoi,+kvm_pv_unhalt \
  \
  -object iothread,id=iothread-virtio0 \
  \
  -device vmgenid,guid=61fafb51-ac1e-41f0-ba2a-453d1b6c7990 \
  \
  -device VGA,id=vga,bus=pcie.0,addr=0x1 \
  \
  -object rng-random,filename=/dev/urandom,id=rng0 \
  -device virtio-rng-pci,rng=rng0,max-bytes=1024,period=1000,bus=pci.1,addr=0x1d \
  \
  -drive ${cdrom:+file="${cdrom}",}if=none,id=drive-ide2,media=cdrom,aio=io_uring \
  -device ide-cd,bus=ide.1,unit=0,drive=drive-ide2,id=ide2,bootindex=200 \
  \
  -drive file="${HERE}/nb-disk0.qcow2",if=none,id=drive-virtio0,aio=native,discard=on,cache=none,detect-zeroes=unmap \
  -device virtio-blk-pci,drive=drive-virtio0,id=virtio0,bus=pci.0,addr=0xa,iothread=iothread-virtio0,bootindex=100 \
  \
  -netdev type=tap,id=net0,ifname=tap0,script=no,downscript=no,vhost=off \
  -device virtio-net-pci,mac=00:b1:ac:c0:ff:ee,netdev=net0,bus=pci.0,addr=0x12,id=net0,rx_queue_size=1024,tx_queue_size=256,bootindex=300 \
  \
  -boot "${boot}" \
  -display sdl \
  ;

# -vga virtio \
# -device virtio-vga,xres=1280,yres=800 \
# -display sdl,zoom-to-fit=on \
