How to Enable GPU Passthrough on KVM VPS

Enabling GPU passthrough on a KVM VPS can dramatically enhance the performance of GPU-intensive tasks like machine learning, video rendering, and gaming. By dedicating a GPU to a virtual machine, users can achieve near-native performance. This guide walks you through the process step-by-step.

What Is GPU Passthrough?

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.

Typical GPU Passthrough

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:

Prerequisites

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.

Step-by-Step Guide - Enable GPU Passthrough on KVM VPS

Step 1: Installing KVM requires software packages

apt-get update
apt-get install qemu-kvm qemu bridge-utils libvirt-daemon-system libvirt-clients virtinst virt-manager

Step 2: Enable IOMMU in BIOS/UEFI

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.

Edit grub file
#For Intel CPUs
intel_iommu=on

#For AMD CPUs
amd_iommu=on

Execute after modification:

sudo update-grub
sudo reboot

Step 3: Binding GPU to VFIO-PCI driver

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
view gpu resource

Step 4: Install VNC

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

Step 5: Create a new VPS

Create a new VPS

Choose Browse Local ISO,

Choose Browse Local ISO

Add CPU and RAM,

add cpu and ram

Add disk,

add disk

Add the GPU card after installed.

add gpu

Add hardware, find the graphics card location, you need to add 00 and 01

add gpu

Step 6: Start the VM and Install GPU Drivers on the VM

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.

Check resource in task manager

Common Issues and Solutions

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.

Conclusion

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.