winvm.sh 5.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #!/bin/bash
  2. # winvm.sh <command> - a Windows 11 VM for references only real Windows gives (live WE on Media Foundation, D3D11
  3. # through WARP: there is no GPU in the VM). Needs /dev/kvm in the container (docker-compose.yml) and the evaluation ISO
  4. # (free, 90 days) in $WINVM/iso, see README.md.
  5. # install creates the disk and installs Windows unattended (autounattend.xml), ~15 min
  6. # start boots the installed VM (WINVM_RESTRICT=off: with internet, only for the evaluation's activation,
  7. # slmgr /ato, which an offline VM can't do, the license then reports "expired")
  8. # stop | kill shuts Windows down (ACPI) | kills QEMU
  9. # shot <png> screenshot; any other qmp.py command works the same way (keys, type, shots, raw)
  10. # Headless: the display is only reachable through QMP/the VNC unix socket in $WINVM/run, never the desktop's display.
  11. # No audio device. The network can't reach anything (restrict=on) except QEMU's SMB share of $WINVM/share, which
  12. # Windows sees as \\10.0.2.4\qemu
  13. WINVM=${WINVM:-$HOME/.local/share/winvm}
  14. RUN=$WINVM/run
  15. HERE=$(cd "$(dirname "$0")" && pwd)
  16. ISO=${WINVM_ISO:-$(ls $WINVM/iso/*.iso 2>/dev/null | head -1)}
  17. mkdir -p $RUN $WINVM/share $WINVM/tpm
  18. ulimit -c 0
  19. tpm () {
  20. pgrep -f "swtpm socket.*$WINVM/tpm" >/dev/null && return
  21. swtpm socket --tpmstate dir=$WINVM/tpm --ctrl type=unixio,path=$RUN/tpm.sock --tpm2 --daemon \
  22. --log file=$RUN/swtpm.log
  23. sleep 0.5
  24. }
  25. qemu () {
  26. tpm
  27. [ -f $WINVM/vars.fd ] || cp /usr/share/edk2/ovmf/OVMF_VARS.fd $WINVM/vars.fd
  28. qemu-system-x86_64 -name winvm -machine q35 -accel kvm -cpu host -smp ${WINVM_CPUS:-8} -m ${WINVM_MEM:-8G} \
  29. -drive if=pflash,format=raw,readonly=on,file=/usr/share/edk2/ovmf/OVMF_CODE.fd \
  30. -drive if=pflash,format=raw,file=$WINVM/vars.fd \
  31. -chardev socket,id=tpm,path=$RUN/tpm.sock -tpmdev emulator,id=tpm0,chardev=tpm -device tpm-tis,tpmdev=tpm0 \
  32. -device ahci,id=ahci -drive id=disk,file=$WINVM/disk.qcow2,if=none,format=qcow2,cache=unsafe \
  33. -device ide-hd,drive=disk,bus=ahci.0 \
  34. -nic user,model=e1000e,restrict=${WINVM_RESTRICT:-on},smb=$WINVM/share \
  35. -vga std -device qemu-xhci -device usb-tablet -display none -vnc unix:$RUN/vnc.sock \
  36. -qmp unix:$RUN/qmp.sock,server=on,wait=off -rtc base=localtime \
  37. -daemonize -pidfile $RUN/qemu.pid "$@"
  38. }
  39. case $1 in
  40. install)
  41. [ -f "$ISO" ] || { echo "no ISO in $WINVM/iso"; exit 1; }
  42. [ -f $WINVM/disk.qcow2 ] && { echo "$WINVM/disk.qcow2 exists, remove it to reinstall"; exit 1; }
  43. qemu-img create -f qcow2 $WINVM/disk.qcow2 ${WINVM_DISK:-80G} >/dev/null
  44. rm -f $WINVM/vars.fd $WINVM/tpm/*
  45. mkdir -p $RUN/unattend && cp $HERE/autounattend.xml $HERE/firstlogon.ps1 $RUN/unattend/
  46. xorriso -as mkisofs -quiet -J -r -V UNATTEND -o $RUN/unattend.iso $RUN/unattend
  47. qemu -drive file="$ISO",media=cdrom,if=none,id=cd0,readonly=on -device ide-cd,drive=cd0,bus=ahci.1,bootindex=0 \
  48. -drive file=$RUN/unattend.iso,media=cdrom,if=none,id=cd1,readonly=on -device ide-cd,drive=cd1,bus=ahci.2
  49. for i in $(seq 20); do python3 $HERE/qmp.py $RUN/qmp.sock keys ret 2>/dev/null; sleep 0.5; done
  50. echo "installing, watch with: $0 shot /tmp/x.png"
  51. ;;
  52. start)
  53. [ -f $RUN/qemu.pid ] && kill -0 $(cat $RUN/qemu.pid) 2>/dev/null && { echo running; exit 0; }
  54. qemu
  55. ;;
  56. stop)
  57. python3 $HERE/qmp.py $RUN/qmp.sock raw '{"execute":"system_powerdown"}'
  58. ;;
  59. kill)
  60. [ -f $RUN/qemu.pid ] && kill $(cat $RUN/qemu.pid)
  61. pkill -f "swtpm socket.*$WINVM/tpm"
  62. ;;
  63. bootstrap)
  64. # once after install: types the agent installer into Win+R, from then on "run" works
  65. cp $HERE/agent.ps1 $HERE/install_agent.ps1 $WINVM/share/
  66. python3 $HERE/qmp.py $RUN/qmp.sock keys meta_l-r
  67. sleep 2
  68. python3 $HERE/qmp.py $RUN/qmp.sock type 'powershell -ExecutionPolicy Bypass -File \\10.0.2.4\qemu\install_agent.ps1'
  69. python3 $HERE/qmp.py $RUN/qmp.sock keys ret
  70. ;;
  71. launch)
  72. # launch <exe> [args] [workdir]: starts a program on the VM's desktop through a one-off scheduled task, so it
  73. # doesn't inherit the agent's output handles (the agent would wait for it to exit)
  74. EXE=$2 ARGS=${3:-} DIR=${4:-}
  75. [ -n "$DIR" ] && DIR="'$DIR'" || DIR="(Split-Path '$EXE')"
  76. exec bash "$HERE/winvm.sh" run -c "\$a = New-ScheduledTaskAction -Execute '$EXE' -Argument '$ARGS' -WorkingDirectory $DIR; Register-ScheduledTask -TaskName winvm-launch -Action \$a -Force | Out-Null; Start-ScheduledTask winvm-launch" 60
  77. ;;
  78. run)
  79. # run <script.ps1 | -c 'command'> [timeout s]: runs it in the VM through the agent, prints its output
  80. NAME=job$$_$(date +%s%N)
  81. mkdir -p $WINVM/share/cmd $WINVM/share/out
  82. if [ "$2" = -c ]; then printf '%s\r\n' "$3" > $WINVM/share/cmd/$NAME.tmp; T=${4:-600}; else cp "$2" $WINVM/share/cmd/$NAME.tmp; T=${3:-600}; fi
  83. mv $WINVM/share/cmd/$NAME.tmp $WINVM/share/cmd/$NAME.ps1
  84. timeout $T bash -c "until [ -f $WINVM/share/out/$NAME.done ]; do sleep 0.5; done" || echo "timed out" >&2
  85. cat $WINVM/share/out/$NAME.txt 2>/dev/null | iconv -f UTF-16LE -t UTF-8 2>/dev/null | tr -d '\r\357\273\277'
  86. rm -f $WINVM/share/out/$NAME.*
  87. ;;
  88. *)
  89. python3 $HERE/qmp.py $RUN/qmp.sock "$@"
  90. ;;
  91. esac