GPU Pass through to virtual host machine
Published on Oct 24, 2024 by Impaktor.
Table of Contents
Short write up on how to pass through GPU from Linux host to Windows guest VM. I’m running Arch Linux, with grub, on a Dell XPS laptop with Nvidia GPU. The virtualization is done through QEMU managed through virt-manager. Adjust below accordingly.
(My use case was to make use of a ML model that was packaged with conda, and even though I replicated the exact python version, and exact version of every package on linux, it failed to install, since there were package conflicts, while running the model on windows was just a matter of double-clicking a bat-file)
1. BIOS:
At start-up, enter BIOS menu, and look for option like intel vt-d
, vt-x
or amd-vi
or IOMMU
, make sure it is enabled.
2. GRUB
Assuming using GRUB as boot loader:
Find PCI ID for the video device (and/or audio) we want to pass through:
lspci -nn | grep -E "NVIDIA" 24:0000:01:00.0 3D controller [0302]: NVIDIA Corporation GA107M [GeForce RTX 3050 Ti Mobile] [10de:25a0] (rev a1)
We see the PCI ID
10de:25a0
Modify
/etc/default/grub
by adding eitherintel_iommu=on
oramd_iommu=on
, toGRUB_CMDLINE_LINUX
and then also:"amd_iommu=on iommu=pt vfio-pci=10de:25a0"
or is it(?):
"amd_iommu=on iommu=pt vfio-pci.ids=10de:25a0"
where we use video ID (and/or audio ID as comma separated without spaces)
Update grub & reboot:
sudo grub-mkconfig -o /boot/grub/grub.cf sudo reboot now
3. Isolate graphics card
Want to prevent linux from loading graphics card driver. Thus we need a second GPU for the linux, e.g. the integrated on on the Intel chip.
Use same PCI ID, and add to
/etc/modprobe.d/vfio.conf
to contain (again, comma separated video and audio PCI IDs if need be):options vfio-pci ids=10de:25a0 softdep nvidia pre: vfio-pci
last line might be optional, possibly for proprietary drives?
- Regenerate (make sure no errors in output):
Arch:
sudo mkinitcpio -p linux
Debian:
sudo update-initramfs -c -k $(uname -r)
4. virt-manager
- in “Overview” make sure:
- chipset: Q35
- Firmware: UEFI
- Add the device, using “Add hardware” button at bottom:
- PCI Host Device, find the device
- Also add one more for audio if needed
- From github.com/virtio-win download stable-virtio/virtio-win.iso.
- Select the ISO as CD-rom in virt-manager
- start the VM, and from the CD-rom, run the
- virtio-win-guest-tools application & reboot VM
- Check Device Manager if it detects the card
- From nvidia.com/Download/ install drivers (for me: GeForce RTX 3050 Ti Mobile) in the virtual windows
- Check I have Nvidia Control Panel, e.g. launch from start menu, and/or check I have a GPU under Task Manager
- possibly adjust resolution in settings->display