run-vm.sh 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #!/usr/bin/env bash
  2. set -euo pipefail
  3. ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
  4. VM_DIR="${VM_DIR:-$HOME/.HHD/PCLPoC-VM}"
  5. ISO="${ISO:-$HOME/.HHD/en-us_windows_10_enterprise_ltsc_2021_x64_dvd_d289cf96.iso}"
  6. PHASE="${1:-install}"
  7. RUN="$ROOT/run"
  8. mkdir -p "$RUN"
  9. for f in "$VM_DIR/disk.qcow2" "$VM_DIR/toolkit.img"; do
  10. if [[ ! -f "$f" ]]; then
  11. echo "missing: $f" >&2
  12. exit 1
  13. fi
  14. done
  15. if [[ "$PHASE" == "install" && ! -f "$ISO" ]]; then
  16. echo "missing ISO: $ISO" >&2
  17. exit 1
  18. fi
  19. if pgrep -f "qemu-system.*PCLPoC-2.99.0.1" >/dev/null 2>&1; then
  20. echo "PCLPoC VM is already running" >&2
  21. exit 1
  22. fi
  23. rm -f "$RUN/qmp.sock" "$RUN/qemu.pid"
  24. args=(
  25. -name PCLPoC-2.99.0.1
  26. -machine q35,accel=kvm
  27. -cpu host
  28. -smp 4,sockets=1,cores=4,threads=1
  29. -m 4096
  30. -rtc base=localtime,driftfix=slew
  31. -vga std
  32. -vnc 127.0.0.1:2
  33. -qmp "unix:$RUN/qmp.sock,server=on,wait=off"
  34. -pidfile "$RUN/qemu.pid"
  35. -drive "file=$VM_DIR/disk.qcow2,if=none,id=disk0,format=qcow2,cache=writeback,discard=unmap"
  36. -device ahci,id=ahci0
  37. -device "ide-hd,drive=disk0,bus=ahci0.0,bootindex=2"
  38. -drive "file=$VM_DIR/toolkit.img,if=none,id=usb0,format=raw,cache=writeback"
  39. -device qemu-xhci,id=xhci
  40. -device "usb-storage,drive=usb0,bus=xhci.0,removable=on,serial=PCLTOOL"
  41. )
  42. if [[ "$PHASE" == "install" ]]; then
  43. args+=(
  44. -boot order=d,menu=off
  45. -drive "file=$ISO,if=none,id=cd0,format=raw,media=cdrom,readonly=on"
  46. -device "ide-cd,drive=cd0,bus=ahci0.1,bootindex=1"
  47. )
  48. else
  49. args+=(-boot order=c,menu=off)
  50. fi
  51. qemu-system-x86_64 "${args[@]}" -daemonize
  52. echo "VM started: phase=$PHASE"
  53. echo "pid: $(cat "$RUN/qemu.pid" 2>/dev/null || echo unknown)"
  54. echo "QMP: $RUN/qmp.sock"
  55. echo "VNC: 127.0.0.1:5902 (localhost only)"