Skip to content

Instantly share code, notes, and snippets.

@pojntfx
Last active November 4, 2023 12:34
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pojntfx/b860e123e649504bcd298aa6e92c4043 to your computer and use it in GitHub Desktop.
Save pojntfx/b860e123e649504bcd298aa6e92c4043 to your computer and use it in GitHub Desktop.
GPU-accelerated, UEFI-, Secure Boot, TPM 2.0, audio- and webcam-enabled QEMU using Virgil 3D or Spice and QXL
qemu-system-x86_64 -enable-kvm -m 4096 -smp $(nproc) -cpu host -device ac97 -audiodev alsa,id=snd0,out.buffer-length=500000,out.period-length=726 -usb -device usb-tablet -device virtio-keyboard-pci -net nic -net user -cdrom distro.iso
# For Virgil 3D GPU acceleration (fast but Linux-only):
-device virtio-vga-gl
# For QXL GPU acceleration (slower but more compatible):
-device qxl
# For GTK display:
-display gtk,gl=on,grab-on-hover=on
# For SDL display:
-display sdl,gl=on
# For Spice display:
-display spice-app,gl=on
# For remote Spice access:
-spice port=5900,addr=127.0.0.1,password=asdf -display vnc=:0
# For Spice clipboard sharing (not supported with Virgil 3D GPU acceleration)
-chardev spicevmc,id=ch1,name=vdagent -device virtio-serial-pci -device virtserialport,chardev=ch1,id=ch1,name=com.redhat.spice.0
# For a hard disk, run `qemu-img create -f qcow2 disk.qcow2 20G` to create a disk, `qemu-img create -f raw disk.qcow2 20G` if the size is reported incorrectly or `qemu-img resize disk.qcow2 +20G` to expand an existing disk and append:
-hda disk.qcow2
# You can also specify multiple drives:
-drive file=disk.qcow2,index=0,media=disk -drive file=distro.iso,index=1,media=cdrom -drive file=virtio.iso,index=2,media=cdrom
# For webcam passthrough (or to passthrough any USB device), run with sudo and append (change hostbus=2,hostaddr=4 to match your webcam's USB address/the USB address of the device you want to pass through; you can find it using `lsusb`):
-device usb-ehci,id=ehci -device usb-host,hostbus=2,hostaddr=4
# For UEFI, use (on Fedora):
-bios /usr/share/edk2/ovmf/OVMF_CODE.fd # On Debian, use /usr/share/ovmf/OVMF.fd instead, on popOS, Ubuntu etc use /usr/share/OVMF/OVMF_CODE.fd
# If the guest OS is ChromeOS, append the following for mouse cursor support:
-show-cursor
# To add a second disk, create another one like described above and append the following; this should enable i.e. ChromeOS installation:
-drive file=disk2.qcow2,if=virtio,index=1
# Port-forwarding (so that you can reach services running in the VM from the host) is also possible:
-net user,hostfwd=tcp::50000-:50000,hostfwd=tcp::50001-:50001,hostfwd=tcp::6443-:6443
# To enable nested virtualization, stop the VM and run the following on the host system:
sudo modprobe -r kvm_intel
sudo modprobe kvm_intel nested=1
# If you want the VM to have Secure Boot and TPM 2.0 (i.e. to run Windows 11), stop the VM and run the following on the host system:
cp /usr/share/edk2/ovmf/OVMF_VARS.secboot.fd .
mkdir -p "${PWD}/.tpm"
swtpm socket --tpmstate dir=/"${PWD}/.tpm" --ctrl type=unixio,path="${PWD}/.tpm/swtpm-sock" --tpm2 -d
# Also append the following to the `qemu-system-x86_64` command:
-machine q35,smm=on -global driver=cfi.pflash01,property=secure,value=on -drive if=pflash,format=raw,unit=0,file=/usr/share/edk2/ovmf/OVMF_CODE.secboot.fd,readonly=on -drive if=pflash,format=raw,unit=1,file=OVMF_VARS.secboot.fd -chardev socket,id=chrtpm,path="${PWD}/.tpm/swtpm-sock" -tpmdev emulator,id=tpm0,chardev=chrtpm -device tpm-tis,tpmdev=tpm0
# If you are emulating Windows and you want to use Spice and QXL for graphics (Virgil 3D is not supported on Windows yet), stop the VM and run the following on the host system:
curl -L -o virtio.iso https://fedorapeople.org/groups/virt/virtio-win/direct-downloads/archive-virtio/virtio-win-0.1.208-1/virtio-win-0.1.208.iso # Use https://fedorapeople.org/groups/virt/virtio-win/direct-downloads/archive-virtio/virtio-win-0.1.173-1/virtio-win-0.1.173.iso if your guest will be Windows 7
# Also append the following to the `qemu-system-x86_64` command:
-drive file=virtio.iso,media=cdrom
# After installing Windows, press Windows + R, enter `D:\virtio-win-guest-tools.exe` (or manually open it using the explorer; for proper graphics support (and Windows 7, where this is only option), also manually install the drivers one by one through the Device Manager by right-clicking on i.e. the "Standard VGA Graphics Card" and selecting the `qxl` (or `qxldod` on Windows 8+) directory from the mounted drivers) and reboot
# You may also use https://github.com/pal1000/mesa-dist-win (extract, run `systemwidedeploy.cmd` and select "Core desktop OpenGL drivers") for proper OpenGL support (after installation, use `GPU Caps Viewer` to confirm that OpenGL 3.1 or higher is supported)
# For audio in Windows, install the Realtek AC97 driver
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment