You're on the right track! Passing NVIDIA GPUs to guest VMs on Ubuntu 24.04 with a KVM host involves several steps. Here's a comprehensive guide covering the necessary configurations and considerations.
Understanding the Concepts
GPU Passthrough: Directly assigning a physical GPU to a guest VM, bypassing the host's virtualization layer for graphics processing. This provides near-native performance for the guest.
PCI Passthrough: The underlying mechanism for GPU passthrough, allowing a VM to directly access a PCI device (like a GPU).
IOMMU (Input/Output Memory Management Unit): A hardware feature that isolates PCI devices and allows for secure assignment to VMs.
VF (Virtual Function): A logical representation of a physical PCIe device, often used for GPU passthrough.
Prerequisites
Hardware:
NVIDIA GPUs: Ensure your host system has the NVIDIA GPUs you want to pass through.
IOMMU Support: Your CPU and motherboard must support IOMMU. Most modern Intel and AMD CPUs do.
PCIe Lanes: Sufficient PCIe lanes are required for each GPU.
KVM Enabled: KVM must be installed and configured on your Ubuntu host.
Software:
Ubuntu 24.04: Your host operating system.
QEMU/KVM: The virtualization software.
libvirt: A virtualization management API.
NVIDIA Drivers: You'll need the NVIDIA drivers installed on the host.
virt-manager (Optional but Recommended): A graphical tool for managing VMs.
Steps to Configure GPU Passthrough
1. Verify IOMMU Support and Enable It
Check IOMMU Status:
bash
dmesg | grep -i iommu
You should see output indicating that IOMMU is enabled. If not, proceed to the next step.
Enable IOMMU in BIOS/UEFI:
Restart your host system and enter the BIOS/UEFI settings. Look for options related to "Intel Virtualization Technology for Directed I/O (VT-d)" or "AMD IOMMU" and enable them.
Enable IOMMU in Kernel Boot Parameters:
Edit your GRUB configuration file:
bash
sudo nano /etc/default/grub
Find the line starting with GRUB_CMDLINE_LINUX_DEFAULT=. Add the following parameters to it (separated by spaces):
You should now see confirmation that IOMMU is active.
2. Identify the GPU and Its PCI IDs
List PCI Devices:
bash
lspci -nnv | grep NVIDIA
This command will list your NVIDIA GPUs and their associated PCI IDs (in the format Bus:Device.Function). Note down the Bus:Device.Function for the GPU you want to pass through. You'll need this information later.
3. Isolate the GPU for Passthrough
Blacklist the NVIDIA Driver: This prevents the host from loading the NVIDIA driver for the GPU.
Create a blacklist file:
bash
sudo nano /etc/modprobe.d/blacklist-nvidia.conf
Add the following lines, replacing [your_gpu_bus:device.function] with the actual PCI ID of your GPU:
code
blacklist nvidia
options nvidia_drm modeset=0
Save the file and update the initramfs:
bash
sudo update-initramfs -u
Reboot your host system.
4. Configure VFIO (Virtual Function I/O)
Install VFIO Modules:
bash
sudo apt update
sudo apt install vfio-pci
Configure VFIO for the GPU:
Create a configuration file for your GPU in /etc/vfio.d/:
bash
sudo nano /etc/vfio.d/nvidia.conf
Add the following content, replacing [your_gpu_bus:device.function] with the actual PCI ID of your GPU:
code
# Disable VFIO for the GPU
options vfio-pci ids=0x[your_gpu_bus:device.function]
Save the file.
Update Initramfs Again:
bash
sudo update-initramfs -u
Reboot your host system.
Verify VFIO is Working:
After rebooting, check if the NVIDIA driver is no longer loaded for the GPU:
bash
lspci -nnv | grep NVIDIA
The Kernel driver in use: should be empty or indicate vfio-pci.
5. Configure libvirt for GPU Passthrough
Create a New Virtual Machine (if you haven't already): You can use virt-manager or create a VM using virsh.
Edit the VM Configuration:
Using virt-manager, right-click on your VM and select "Open". Go to "Hardware" and click "Add Hardware". Select "PCI Host Device".
Select the GPU: In the "Device" dropdown, you should see your NVIDIA GPU listed. Select it.
(Optional) Add the Audio Device: If you want to pass through the GPU's audio capabilities, you might need to add the audio device as well.
(Optional) Add the Display Device: You can choose to pass through the GPU's display capabilities to the guest VM. This might be necessary for certain applications or for a more seamless experience.
Save the VM configuration.
6. Configure the Guest Operating System
Install NVIDIA Drivers in the Guest VM: Once the VM boots, install the appropriate NVIDIA drivers for the GPU.
Verify GPU Passthrough in the Guest: In the guest OS, you should be able to see and use the passed-through GPU.
Important Considerations and Troubleshooting
Kernel Version: Ensure you are using a relatively recent kernel version (5.x or later) for better VFIO support.
Secure Boot: Secure Boot might interfere with VFIO passthrough. You might need to disable it in your BIOS/UEFI settings or configure it to allow VFIO modules.
BIOS/UEFI Settings: Double-check your BIOS/UEFI settings for any settings related to virtualization, IOMMU, or PCIe configuration.
Driver Conflicts: Ensure there are no driver conflicts on the host system that might interfere with GPU passthrough.
Performance: While GPU passthrough offers near-native performance, there might be a slight overhead compared to a dedicated GPU for the host.
Multiple GPUs: You can pass through multiple GPUs to different VMs, but ensure you have enough PCIe lanes and that the GPUs are not interfering with each other.
VFIO Documentation: Refer to the official VFIO documentation for more detailed information and troubleshooting: https://vfio.org/
libvirt Documentation: Consult the libvirt documentation for managing VMs and hardware passthrough: https://libvirt.org/docs/
Using virsh (Command Line)
You can also configure GPU passthrough using the virsh command-line tool. Here's an example:
bash
sudo virsh edit <your_vm_name>
In the XML configuration, under the <devices> section, add the following:
xml
<pci>
<domain model='pcie'>
<host>0000:01:00.0</host> <!-- Replace with your GPU's PCI ID -->
<pass>]
</domain>
</pci>
This is a detailed guide, and the exact steps might vary slightly depending on your specific hardware and software configuration. Be patient and methodical, and consult the documentation if you encounter any issues.