GPU passthrough allows a virtual machine (VM) to have direct access to a physical GPU on the host machine. This technique provides the VM with enhanced computational power, bypassing the virtualization overhead.
Enabling GPU passthrough on a KVM (Kernel-based Virtual Machine) VPS allows a virtual machine to directly access a GPU, enabling hardware acceleration for workloads like gaming, machine learning, or 3D rendering. Here’s a step-by-step guide on how to enable GPU passthrough:
Before enabling GPU passthrough, ensure the following:
Hardware Support: Ensure your CPU and motherboard support Intel VT-d or AMD IOMMU, as well as a dedicated GPU.
Host OS with KVM Support: Install a Linux distribution that supports KVM, such as Ubuntu, Debian, or CentOS.
Separate GPU for Host and VM: GPU pass-through requires that the GPU in the physical host can be used exclusively, usually requiring a dedicated GPU.
Root Access: Full administrative privileges on the host server.
apt-get update apt-get install qemu-kvm qemu bridge-utils libvirt-daemon-system libvirt-clients virtinst virt-manager
IOMMU is a common name for Intel VT-d and AMD-Vi. VT-d stands for Intel Virtualization Technology for Directed I/O, not to be confused with VT-x Intel Virtualization Technology. VT-x allows one hardware platform to act as multiple "virtual" platforms, while VT-d improves system security and reliability, and improves the performance of I/O devices in a virtualized environment.
Edit your bootloader configuration file (GRUB in most cases) to enable IOMMU:
Editing the GRUB configuration:
sudo nano /etc/default/grub
Add after GRUB_CMDLINE_LINUX_DEFAULT and GRUB_CMDLINE_LINUX.
#For Intel CPUs intel_iommu=on #For AMD CPUs amd_iommu=on
Execute after modification:
sudo update-grub sudo reboot
Refer to the previous step to add the GPU PCI ID to the file.
Editing the GRUB configuration, after GRUB_CMDLINE_LINUX:
vfio-pci.ids=10de:1cb1,10de:0fb9
After restarting, run:
lspci -k | grep -A 5 VGA
The kernel driver in use status of the PU card changes to: vfio-pci, indicating that the modification is successful. This means that the GPU card passthrough setting is complete, which can be completed by adding specific hardware through VNC when creating a VM.
Let's view the GPU card resource location of the host resources.
lspci | grep -i vga
You can check if the system has some special third-party software installed, such as video editors, graphic processing tools, firewall security software and so on. For example, AVG Firewall may have compatibility problems with some video card drivers, resulting in a lower version of OpenGL.
Installing the graphical interface:
apt-get install xfce4 xfce4-goodies dbus-x11 xfonts-base
Change the vncserver connection password:
apt install tightvncserver vncserver vi .vnc/xstartup #!/bin/sh xrdb $HOME/.Xresources startxfce4 &
Restart VNC service:
vncserver -kill :1 vncserver :1
Choose Browse Local ISO,
Add CPU and RAM,
Add disk,
Add the GPU card after installed.
Add hardware, find the graphics card location, you need to add 00 and 01
Boot up the VM and check if the GPU is recognized. You may need to install NVIDIA drivers within the VM if using an NVIDIA GPU.
1. Error "Device is in use by the host": Ensure the GPU is not being used by the host's display server (e.g., Xorg or Wayland).
2. IOMMU Grouping Issues: Use tools like pci-stub or patch your kernel to break problematic groups.
3. Performance Bottlenecks: Ensure the VM has sufficient CPU cores, RAM, and storage bandwidth.
GPU passthrough on KVM VPS offers an efficient way to unlock the full potential of a GPU for virtualized workloads. While the process requires some technical setup, the performance gains are worth the effort for demanding applications.
By following this guide, you can enable GPU passthrough and start leveraging high-performance computing on your VPS.