Passthrough of NVIDIA GPUs to guest VMs using KVM on Ubuntu 24.04 involves several steps to ensure the hardware is properly isolated and presented to the VM. Here's a general guide to help you pass a dedicated NVIDIA GPU to a VM:
🧰 Prerequisites
Host OS : Ubuntu 24.04 (already installed)
Virtualization : KVM + QEMU
Hardware :
VT-d (for Intel) or AMD-Vi (for AMD) enabled in BIOS
Multiple NVIDIA GPUs (ideally each passed individually to different VMs)
🧾 Steps Overview
Enable IOMMU (VT-d or AMD-Vi)
Enable VFIO (GPU passthrough driver)
Identify GPU(s) and bind them to VFIO
Create VM with virtual hardware (using virt-manager or virsh)
Pass GPU to VM
Install GPU drivers in the VM
🔧 Step-by-Step Guide
1. ✅ Enable IOMMU in GRUB
Edit /etc/default/grub:
bash Copy code
sudo nano /etc/default/grub
Find the line:
bash Copy code
GRUB_CMDLINE_LINUX_DEFAULT="quiet splash"
Modify it by adding the appropriate IOMMU option:
For Intel :
bash Copy code
GRUB_CMDLINE_LINUX_DEFAULT="quiet splash intel_iommu=on iommu=pt"
For AMD :
bash Copy code
GRUB_CMDLINE_LINUX_DEFAULT="quiet splash amd_iommu=on iommu=pt"
Then update GRUB:
bash Copy code
sudo update-grub
sudo reboot
2. 📨 Install Required Packages
bash Copy code
sudo apt update
sudo apt install qemu-kvm libvirt-daemon-system libvirt-clients virt-manager ovmf
3. 📦 Enable VFIO (for PCI passthrough)
Optional: blacklist NVIDIA drivers on host if you are passing them through.
bash Copy code
echo "blacklist nouveau" | sudo tee -a /etc/modprobe.d/blacklist.conf
echo "blacklist nvidia" | sudo tee -a /etc/modprobe.d/blacklist.conf
Create a VFIO config file:
bash Copy code
sudo nano /etc/modprobe.d/vfio.conf
Add:
bash Copy code
options vfio-pci ids=XXXX:YYYY,XXXX:ZZZZ
Replace XXXX:YYYY and others with VendorID:DeviceID pairs (from lspci -nn), like 10de:1db6.
4. 🔍 Identify GPU + IOMMU Group
List all GPUs:
bash Copy code
lspci -nn | grep -i nvidia
Check IOMMU groups:
bash Copy code
for d in /sys/kernel/iommu_groups/*/devices/*; do
echo -e "\nIOMMU Group ${d%%/*}:";
lspci -nnks "${d##*/}"
done
Make sure GPU and its audio part are in the same group and exclusive.
5. 🌐 Bind GPU to VFIO on Boot
Install vfio-pci on boot:
Create script:
bash Copy code
sudo nano /etc/initramfs-tools/scripts/init-top/vfio
Add:
bash Copy code
#!/bin/sh
PREREQ=""
prereqs() { echo "$PREREQ"; }
case $1 in
prereqs) exit 0 ;;
esac
modprobe vfio-pci
Make it executable:
bash Copy code
sudo chmod +x /etc/initramfs-tools/scripts/init-top/vfio
sudo update-initramfs -u
sudo reboot
6. 🧠 Verify GPU is bound to VFIO
After reboot:
bash Copy code
lspci -nnk -d 10de:1db6 # Replace with your GPU's ID
It should say:
text Copy code
Kernel driver in use: vfio-pci
7. 🖥 Create VM Using virt-manager or virsh
Launch with:
bash Copy code
virt-manager
Use UEFI (OVMF) firmware
Choose machine type : q35
Add GPU via Add Hardware > PCI Host Device
Add both:
GPU
Audio device (often also 10de but different device ID)
8. 🎮 Install Guest OS + NVIDIA Drivers
Then in your Guest VM :
Install NVIDIA drivers normally (from .run file or repos)
If Windows guest, install official Windows NVIDIA drivers
✅ Result
Your guest VM should now have direct access to the NVIDIA GPU and can use it for rendering, CUDA processing, etc., bypassing the host OS .
🔚 Optional Extras
Prevent host from using GPU: Don’t start graphical desktop on that GPU.
Use Looking Glass for GUI access if it's a gaming/workstation VM.
Use virtio input/audio/display if shuttling between VMs.
❓ Want to passthrough multiple GPUs to different VMs?
Just repeat the binding and passthrough steps for each GPU. Each VM can own a full GPU. Be sure each GPU is in a separate IOMMU group .
Would you like a sample virsh XML configuration or help automating this setup with Ansible or scripts?