516c87d2ae | 3 years ago | |
---|---|---|
README.md | 3 years ago |
README.md
Enable IOMMU
Set the kernel paramater depending on your CPU.
For GRUB user, edit grub configuration.
/etc/default/grub |
---|
GRUB_CMDLINE_LINUX_DEFAULT="... intel_iommu=on iommu=pt ..." |
OR |
GRUB_CMDLINE_LINUX_DEFAULT="... amd_iommu=on iommu=pt ..." |
Generate grub.cfg
grub-mkconfig -o /boot/grub/grub.cfg
Reboot your system for the changes to take effect.
Verify IOMMU
If you don't see any output when running following command, IOMMU is not functioning.
dmesg | grep 'IOMMU enabled'
Install required tools
Gentoo Linux
emerge -av qemu virt-manager libvirt ebtables dnsmasq
Arch Linux
pacman -S qemu libvirt edk2-ovmf virt-manager dnsmasq ebtables
Fedora
dnf install @virtualization
Enable required services
SystemD
systemctl enable --now libvirtd
OpenRC
rc-update add libvirtd default
rc-service libvirtd start
Sometimes, you might need to start default network manually.
virsh net-start default
virsh net-autostart default
Setup Guest OS
NOTE: You should replace win10 with your VM's name where applicable
Download virtio driver.
Launch virt-manager and create a new virtual machine. Select Customize before install on Final Step.
In Overview section, set Chipset to Q35, and Firmware to UEFI
In CPUs section, set CPU model to host-passthrough, and CPU Topology to whatever fits your system.
For SATA disk of VM, set Disk Bus to virtio.
In NIC section, set Device Model to virtio
Add Hardware > CDROM: virtio-win.iso
Now, Begin Installation. Windows can't detect the virtio disk, so you need to Load Driver and select virtio-iso/amd64/win10 when prompted.
After successful installation of Windows, install virtio drivers from virtio CDROM.
Attaching PCI devices
Remove Channel Spice, Display Spice, Video XQL, Sound ich* and other unnecessary devices.
Now, click on Add Hardware, select PCI Devices and add the PCI Host devices for your GPU's VGA and HDMI Audio.
vBIOS Patching
While most of the GPU can be passed with stock vBIOS, some GPU requires vBIOS to be patched in order to work correctly, specially NVIDIA Pascal Series GPU.
In order to patch vBIOS, you need to first dump the GPU vBIOS from your system.
If you have Windows installed, you can use GPU-Z to dump vBIOS.
To dump vBIOS on Linux, you can use following command (replace PCI id with yours):
I didn't manage to get this to work on Arch Linux but works on Gentoo. So, it might not work depending on your distribution.
In which case, you can try using live cd.
echo 1 > /sys/bus/pci/devices/0000:01:00.0/rom
cat /sys/bus/pci/devices/0000:01:00.0/rom > path/to/dump/vbios.rom
echo 0 > /sys/bus/pci/devices/0000:01:00.0/rom
To patch vBIOS, you need to use Hex Editor (eg., Okteta)
For NVIDIA GPU (esp. Pascal Series), using hex editor, search string “VIDEO”, and remove everything before HEX value 55.
For other GPU, I have no idea.
To use patched vBIOS, edit VM's configuration to include patched vBIOS inside hostdev block of VGA
virsh edit win10 |
---|
|
Libvirt Hooks
Libvirt hooks automate the process of running specific tasks during VM state change.
More info at: PassthroughPost
Create Libvirt Hook
mkdir /etc/libvirt/hooks
touch /etc/libvirt/hooks/qemu
chmod +x /etc/libvirt/hooks/qemu
/etc/libvirt/hooks/qemu |
---|
|
Create Start Script
mkdir -p /etc/libvirt/hooks/qemu.d/win10/prepare/begin
touch /etc/libvirt/hooks/qemu.d/win10/prepare/begin/start.sh
chmod +x /etc/libvirt/hooks/qemu.d/win10/prepare/begin/start.sh
/etc/libvirt/hooks/qemu.d/win10/prepare/begin/start.sh |
---|
|
Create Stop Script
mkdir -p /etc/libvirt/hooks/qemu.d/win10/release/end
touch /etc/libvirt/hooks/qemu.d/win10/release/end/stop.sh
chmod +x /etc/libvirt/hooks/qemu.d/win10/release/end/stop.sh
/etc/libvirt/hooks/qemu.d/win10/release/end/stop.sh |
---|
|
Keyboard/Mouse Passthrough
Modify libvirt configuration of your VM. Change first line to:
virsh edit win10 |
---|
|
Find your keyboard and mouse devices in /dev/input/by-id. You'd generally use the devices ending with event-kbd and event-mouse. And the devices in your configuration right before closing </domain>
tag.
Replace MOUSE_NAME and KEYBOARD_NAME with your device id.
virsh edit win10 |
---|
|
You need to include these devices in your qemu config.
/etc/libvirt/qemu.conf |
---|
|
Also, switch from PS/2 devices to virtio devices. Add the devices inside <devices>
block
virsh edit win10 |
---|
|
Audio Passthrough
VM's audio can be routed to the host. You need Pulseaudio.
Modify the libvirt configuration of your VM.
virsh edit win10 |
---|
|
Video card driver virtualisation detection
Video Card drivers refuse to run in Virtual Machine, so you need to spoof Hyper-V Vendor ID.
virsh edit win10 |
---|
|
NVIDIA guest drivers also require hiding the KVM CPU leaf:
virsh edit win10 |
---|
|
User Groups
You should add your user to input, kvm, and libvirt groups to be able to run VM without root.
usermod -aG kvm,input,libvirt username
See Also
Single GPU Passthrough by joeknock90
Single GPU Passthrough by YuriAlek
ArchLinux PCI Passthrough
Gentoo GPU Passthrough