Skip to content

Instantly share code, notes, and snippets.

@mikroskeem
Last active February 28, 2024 14:22
Show Gist options
  • Save mikroskeem/fdbbbd35d7273aa77ba9ebc11e7b8e5d to your computer and use it in GitHub Desktop.
Save mikroskeem/fdbbbd35d7273aa77ba9ebc11e7b8e5d to your computer and use it in GitHub Desktop.
Intel GVT (vGPU) and QEMU example
#!/bin/bash -e
# https://www.kraxel.org/blog/2018/04/vgpu-display-support-finally-merged-upstream/
# You should set up your system as following:
# 1) Add 'i915.enable_gvt=1 intel_iommu=on iommu=pt' to boot arguments
# 1.1) If you get Windows guest working somehow, then add 'kvm.ignore_msrs=1' to avoid guest BSOD-ing
# (Windows does weird stuff).
# 2) `MODULES=(kvm kvmgt i915)` in /etc/mkinitcpio.conf
# 3) `echo -e "options i915 enable_gvt=1\nsoftdep i915 pre: kvmgt" > /etc/modprobe.d/20-kvmgt.conf` for sure
# 4) Make sure that `ulimit -l` returns `unlimited` (probably needs less, but better safe than sorry)
# Hint: Add 'yourusername soft memlock unlimited' and
# 'yourusername hard memlock unlimited' to /etc/security/limits.conf
# 5) Create new udev rule to access vfio as non-root:
# echo 'SUBSYSTEM=="vfio", OWNER="root", GROUP="kvm"' > /etc/udev/rules.d/10-vfio.rules
# Edit this to match to your device
MDEV_DEVICE="/sys/class/mdev_bus/0000:00:02.0/mdev_supported_types/i915-GVTg_V4_4"
# Creates vGPU
UUID="$(uuidgen)"
echo "${UUID}" | sudo tee "${MDEV_DEVICE}/create"
# Notes:
# - This requires quite new QEMU, something like 2.12.0+ (it's in testing repos for Arch Linux at the time of writing)
# - Also this example uses OVMF, OS must support UEFI. You might want to save a copy of OVMF_VARS.fd somewhere writable
# and remove `readonly` flag to configure EFI vars properly.
# - Windows 7 didn't like my 5th gen 5500 GT2 vGPU. Windows 10 complained about GPU having different ROM than boot time.
# Setting rombar=0 helped getting it that far. Couldn't copy vBIOS either because Linux complained about failing
# sanity check.
# - Kernel log gets spammed with gvt warnings of invalid addresses
# It doesn't bother me (as I do 'cat /proc/kmsg | grep -v "gvt"'), but there is no real solution for that.
# If you have specified '-video qxl', then don't open vfio-pci output before you've made sure that Intel vGPU is
# initialized properly.
# - Host kernel threw bunch of gvt warnings and caused lag in VM when QEMU mouse emulation was used.
# Binding USB mouse directly to VM helped to get rid of that issue.
# - If you are using NVIDIA Optimus like me (official NVIDIA way, iow disabling Intel, enabling modesetting and
# giving whole display for discrete GPU to deal with), then you have to disable it somehow temporarily.
# Otherwise you'll get no output from QEMU and/or crash Intel GPU completely (NVIDIA kernel module misbehaves and/or
# NVIDIA OpenGL stack is missing few features on Linux).
# Heck, NVIDIA OpenGL stack sucks anyway: https://i.imgur.com/smUCSLg.png (however it performs better
# than nouveau so... that's why I'm stuck on it.)
# I'm using https://github.com/mikroskeem/dot/blob/master/bin/bin/toggle-nvidia.sh for switching between GPUs without
# reboots.
# Probably doesn't work on non-Optimus setups either, I haven't confirmed it because of lacking hardware for testing.
qemu-system-x86_64 \
-enable-kvm \
-m 2G \
-nodefaults \
-M graphics=off \
-monitor stdio \
-soundhw ac97 \
-cpu host \
-drive if=pflash,format=raw,readonly,file=/usr/share/ovmf/x64/OVMF_CODE.fd \
-drive if=pflash,format=raw,readonly,file=/usr/share/ovmf/x64/OVMF_VARS.fd \
-smp cpus=2,cores=1,threads=1 \
-netdev user,id=vmnet0,net=192.168.76.0/24,dhcpstart=192.168.76.9 \
-display gtk,gl=on \
-device nec-usb-xhci \
-device e1000,netdev=vmnet0 \
-device vfio-pci,sysfsdev=/sys/bus/mdev/devices/"${UUID}",rombar=0,display=on \
-cdrom ~/Downloads/Fedora-Workstation-Live-x86_64-27-1.6.iso
# -vga qxl \
# -hda ~/win7.qcow2 \
# -cdrom ~/.local/share/libvirt/images/W7.X86X64.OEM.ESD.M6.NOV2016.iso
# Deletes vGPU
echo 1 | sudo tee "${MDEV_DEVICE}/devices/${UUID}/remove"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment