How to Enable GPU Passthrough on KVM with AlmaLinux
Categories:
Introduction
GPU passthrough allows a physical GPU to be directly assigned to a virtual machine (VM) in a KVM (Kernel-based Virtual Machine) environment. This feature is crucial for high-performance tasks such as gaming, 3D rendering, video editing, and machine learning, as it enables the VM to utilize the full power of the GPU. AlmaLinux, a stable and robust enterprise-grade Linux distribution, provides a reliable platform for setting up GPU passthrough.
In this guide, we will explain how to configure GPU passthrough on KVM with AlmaLinux. By the end of this tutorial, you will have a VM capable of leveraging your GPU’s full potential.
What is GPU Passthrough?
GPU passthrough is a virtualization feature that dedicates a host machine’s physical GPU to a guest VM, enabling near-native performance. It is commonly used in scenarios where high-performance graphics or compute power is required, such as:
- Gaming on VMs: Running modern games in a virtualized environment.
- Machine Learning: Utilizing GPU acceleration for training and inference.
- 3D Rendering: Running graphics-intensive applications within a VM.
GPU passthrough requires hardware virtualization support (Intel VT-d or AMD IOMMU), a compatible GPU, and proper configuration of the host system.
Prerequisites
Before starting, ensure the following requirements are met:
Hardware Support:
- A CPU with hardware virtualization support (Intel VT-x/VT-d or AMD-V/IOMMU).
- A GPU that supports passthrough (NVIDIA or AMD).
Host System:
- AlmaLinux 8 or newer installed.
- KVM, QEMU, and libvirt configured and operational.
Permissions:
- Administrative privileges to configure virtualization and hardware.
BIOS/UEFI Configuration:
- Enable virtualization extensions (Intel VT-d or AMD IOMMU) in BIOS/UEFI.
Additional Tools:
virt-manager
for GUI management of VMs.pciutils
for identifying hardware devices.
Step-by-Step Guide to Configure GPU Passthrough on KVM with AlmaLinux
Step 1: Enable IOMMU in BIOS/UEFI
- Restart your system and access the BIOS/UEFI settings.
- Locate the virtualization options and enable Intel VT-d or AMD IOMMU.
- Save the changes and reboot into AlmaLinux.
Step 2: Enable IOMMU on AlmaLinux
Edit the GRUB configuration file:
sudo nano /etc/default/grub
Add the following parameters to the
GRUB_CMDLINE_LINUX
line:- For Intel:
intel_iommu=on iommu=pt
- For AMD:
amd_iommu=on iommu=pt
- For Intel:
Update GRUB and reboot:
sudo grub2-mkconfig -o /boot/grub2/grub.cfg sudo reboot
Step 3: Verify IOMMU is Enabled
After rebooting, verify that IOMMU is enabled:
dmesg | grep -e DMAR -e IOMMU
You should see lines indicating that IOMMU is enabled.
Step 4: Identify the GPU and Bind it to the VFIO Driver
List all PCI devices and identify your GPU:
lspci -nn
Look for entries related to your GPU (e.g., NVIDIA or AMD).
Note the GPU’s PCI ID (e.g.,
0000:01:00.0
for the GPU and0000:01:00.1
for the audio device).Bind the GPU to the VFIO driver:
- Create a configuration file:
sudo nano /etc/modprobe.d/vfio.conf
- Add the following line, replacing
<PCI-ID>
with your GPU’s ID:options vfio-pci ids=<GPU-ID>,<Audio-ID>
- Create a configuration file:
Update the initramfs and reboot:
sudo dracut -f --kver $(uname -r) sudo reboot
Step 5: Verify GPU Binding
After rebooting, verify that the GPU is bound to the VFIO driver:
lspci -nnk -d <GPU-ID>
The output should show vfio-pci
as the driver in use.
Step 6: Create a Virtual Machine with GPU Passthrough
Open
virt-manager
and create a new VM or edit an existing one.Configure the VM settings:
- CPU: Set the CPU mode to “host-passthrough” for better performance.
- GPU:
- Go to the Add Hardware section.
- Select PCI Host Device and add your GPU and its associated audio device.
- Display: Disable SPICE or VNC and set the display to
None
.
Install the operating system on the VM (e.g., Windows 10 or Linux).
Step 7: Install GPU Drivers in the VM
- Boot into the guest operating system.
- Install the appropriate GPU drivers (NVIDIA or AMD).
- Reboot the VM to apply the changes.
Step 8: Test GPU Passthrough
Run a graphics-intensive application or benchmark tool in the VM to confirm that GPU passthrough is working as expected.
Troubleshooting Tips
GPU Not Detected in VM:
- Verify that the GPU is correctly bound to the VFIO driver.
- Check the VM’s XML configuration to ensure the GPU is assigned.
IOMMU Errors:
- Ensure that virtualization extensions are enabled in the BIOS/UEFI.
- Verify that IOMMU is enabled in the GRUB configuration.
Host System Crashes or Freezes:
- Check for hardware compatibility issues.
- Ensure that the GPU is not being used by the host (e.g., use an integrated GPU for the host).
Performance Issues:
- Use a dedicated GPU for the VM and an integrated GPU for the host.
- Ensure that the CPU is in “host-passthrough” mode for optimal performance.
Best Practices for GPU Passthrough on KVM
- Use Compatible Hardware: Verify that your GPU supports virtualization and is not restricted by the manufacturer (e.g., some NVIDIA consumer GPUs have limitations for passthrough).
- Backup Configurations: Keep a backup of your VM’s XML configuration and GRUB settings for easy recovery.
- Allocate Sufficient Resources: Ensure the VM has enough CPU cores, memory, and disk space for optimal performance.
- Update Drivers: Regularly update GPU drivers in the guest OS for compatibility and performance improvements.
Conclusion
GPU passthrough on KVM with AlmaLinux unlocks the full potential of your hardware, enabling high-performance applications in a virtualized environment. By following the steps outlined in this guide, you can configure GPU passthrough for your VMs, providing near-native performance for tasks like gaming, rendering, and machine learning.
Whether you’re setting up a powerful gaming VM or a high-performance computing environment, AlmaLinux and KVM offer a reliable platform for GPU passthrough. With proper configuration and hardware, you can achieve excellent results tailored to your needs.