11 KiB
Single GPU passthrough with QEMU and VFIO
![Windows 10 1709](/Screenshots/Windows 10 QEMU single GPU info.png)Windows 10 1709
Table of Contents
- What this does
- What you need
- Submit your own configuration
- My system
- vBIOS
- Configure
- Known problems
- TODO
Last Update
2018/10/20
What this does
In one command it kills X, frees the GPU from drivers and console, detaches the GPU from the host, starts the VM with the GPU, waits until the VM is off, reattaches the GPU to the host and starts lightdm.
What you need
- An IOMMU enabled motherboard. Check your motherboard manual for an option in BIOS to enable IOMMU.
- CPU support for AMD-v/VT-x and AMD-Vi/VT-d (AMD/Intel).
- Virtualization support enabled on BIOS. Check your motherboard manual if you need help.
- One GPU that supports UEFI and its BIOS. All GPUs from 2012 and later should support this, some may have issues. If the GPU does not support UEFI you may be able to make it work, but you won't see anything in the screen until the drivers inside Windows kick in.
- QEMU, OVMF UEFI and VIRTIO drivers for Windows. If you need to install, refer to the Install guide
- [Optional] An hard drive only for Windows.
- [Recommended] Another computer to login remotely with
ssh
for convenience, at least until you have everything working.
Submit your own configuration
Create a pull merge with [a file explaining how you got it working](Hardware configurations/README.md).
My system
[Hardware]
CPU: AMD Ryzen 5 2600
Motherboard: Gigabyte AB350M-Gaming 3 rev1.1
Motherboard BIOS: F23d
RAM: 16GB
GPU: Gigabyte Nvidia GeForce GTX 770
GPU model: GV-N770OC-2GD
GPU BIOS: 80.04.C3.00.0F
GPU codename: GK104
[Software]
Linux Distro: ArchLinux
Linux Kernel: 4.17.14
Nvidia divers: 396.51-1
QEMU version: 2.12.1-1
OVMF version: r24021
[Guests]
Windows 10 Pro 1709 x64
MacOS High Sierra 10.13.3
vBIOS
I experienced some weird things when doing this on the display, like a corruption of the image, it may be my GPU. If you encounter anything, a reboot solved my problems.
Method 1 - Linux
It did not work for me, the ROM is 59KiB and it should be around 162KiB. It may work for you.
- Execute
scripts/iommu.sh
to get the BUS ID for the GPU. Looks like0000:06:00.0
. - Edit
scripts/extract-vbios-linux.sh
to your convenience. Changevideobusid=
. [Optional] Change also the location where the vBIOS will be saveVBIOS=
. - Make the script executable with
chmod +x scripts/extract-vbios-linux.sh
. - Link the service to systemd:
ln -s scripts/qemu@.service /usr/lib/systemd/system/
. - Execute the systemd unit with
sudo systemctl start qemu@extract-vbios-linux.service
. You can also do it overssh
. The screen will turn dark for a while. The extracted ROM will be in the root directory/root/vBIOS.rom
From here you are alone, I don't know how to proceed. Maybe you need to edit, maybe don't.
Method 2 - nvflash in Linux
- Download nvflash https://www.techpowerup.com/download/nvidia-nvflash/. Do not install from AUR; the package it's broken.
- Unzip it as
/root/nvflash_linux
with# unzip nvflash_5.414.0_linux.zip -d /root/
. - Execute
scripts/iommu.sh
to get the BUS ID for the GPU. Looks like0000:06:00.0
. - Edit
scripts/extract-vbios-nvflash.sh
. Change the variablesvideobusid
with your GPU BUS ID;NVFLASH
if you changed the location of the executable; andVBIOS
if you want the ROM in other path. - Link the service to systemd:
ln -s scripts/qemu@.service /usr/lib/systemd/system/
. - Execute the systemd unit with
sudo systemctl start qemu@extract-vbios-nvflash.service
. You can also do it overssh
. The screen will turn dark for a while. The extracted ROM will be in the root directory/root/vBIOS.rom
- Edit the vBIOS
Method 3 - Windows
Source. You can download the bios from techpowerup.com; if you do so, skip to edit the vBIOS.
- Download and install GPU-Z.
- [Open GPU-Z and backup the GPU BIOS](/Screenshots/GPU-Z vBIOS.png). Right next to the
Bios Version
; in my case80.04.C3.00.0F
, there is an icon for backup. A file namedGK104.rom
will be created [Your file name may vary]. - Edit the vBIOS
Edit the VBIOS
- Open the vBIOS ROM (
vBIOS.rom
) in the HEX editor. - ![After a bunch of
00
, there is a55
orU
in HEX, delete everything before the55
](/Screenshots/Hex vBIOS.png), and save. I strongly recommend not to overwrite the original ROM. ![Note that series ofFF
values have been reported on a GTX 1060](/Screenshots/Hex vBIOS 1060.png).
Configure
- Clone this repository
$ git clone https://gitlab.com/YuriAlek/vfio.git
- [Optional] Download virtio drivers. If you do not, modify
scripts/windows.sh
.
$ wget -o virtio-win.iso "https://fedorapeople.org/groups/virt/virtio-win/direct-downloads/stable-virtio/virtio-win.iso"
- Get the iommu groups needed for the VM (GPU, GPU audio and USB controller). They look like
06:00.0
$ chmod +x scripts/iommu.sh
$ scripts/iommu.sh
-------------------------
# GPU
IOMMU group 13
06:00.0 VGA compatible controller [0300]: NVIDIA Corporation GK104 [GeForce GTX 770] [10de:1184] (rev a1)
06:00.1 Audio device [0403]: NVIDIA Corporation GK104 HDMI Audio Controller [10de:0e0a] (rev a1)
# USB 3.0 Controller
IOMMU group 16
07:00.3 USB controller [0c03]: Advanced Micro Devices, Inc. [AMD] USB 3.0 Host controller [1022:145f]
# SATA Controller
IOMMU group 18
08:00.2 SATA controller [0106]: Advanced Micro Devices, Inc. [AMD] FCH SATA Controller [AHCI mode] [1022:7901] (rev 51)
- [Optional] Create the image for the VM. Only if not using a physical hard drive. You can edit the path, the size and the format. Check
man qemu-img
for more information.
$ qemu-img create -f raw /path/to/image/windows.raw 60G
-
Edit the config in
scripts/config.sh
to convenience. If you use systemd to start the VM you have to editEnvironmentFile
inqemu@.service
to point to your config file. Variables you may have to edit: -
PCI devices.
IOMMU_GPU
;IOMMU_USB
. -
User.
-
Location of HDD/IMG, ISO, vBIOS and OVMF image.
-
The Desktop Environment, Display Manager, Window Manager, etc.
lightdm
/i3
. -
QEMU options like RAM and CPU cores.
-
Kernel modules.
-
Other things like add a command to kill PulseAudio
pulseaudio -k
and another, at the end of the script, to start it againpulseaudio --start
. -
The network options
-device virtio-net-pci,netdev=n1 -netdev user,id=n1
. -
Swap virtio for sata as the HDD interface.
-
Start the VM
# scripts/windows-install.sh
-
When installing Windows, in the section
Where do you want to install Windows?
there will be no hard drives to install to; to fix it: -
Load driver
-
Browse
-
CD Drive (E:) virtio-win-0.1.1
-
vioscsi
-
w10
-
amd64
-
ok
-
Load driver
Red Hat VirtIO SCSI pass-through controller (E:\vioscsi\w10\amd64\vioscsi.inf)
-
Next
-
Select the
Unallocated Space
-
Proceed as normal.
-
Let Windows find the drivers for the GPU (if Windows has network) or download the updated ones from NVIDIA.
-
Once installed Windows, run the VM with:
# scripts/windows.sh
For the sake of convenience
# ln -s scripts/qemu@.service /usr/lib/systemd/system/
# ln -s scripts/qemu-mac@.service /usr/lib/systemd/system/
$ alias windows="sudo systemctl start qemu@windows.service"
$ alias macos="sudo systemctl start qemu-mac@macos-hs.service"
To start the Windows VM
$ windows
To start the MacOS VM
$ macos
For MacOS
Check this amazing guide for creating the MacOS install image and Clover.
Known problems
Race condition
There is something somewhere that makes it crash. That's why there is so many sleep
MacOS does not like USB hubs, therefore anything connected to a hub will be ignored by MacOS
Sometimes works, sometimes does not
Sometimes the GPU will not have correct drivers inside Windows, even when yesterday was working just fine; Windows may install them.
Sometimes the QEMU command will just fail and the command continues and start X again.
Sometimes the QEMU command does not exit after shutting down the VM.
Windows version
Windows 10 Pro 1709 works for me, but 1803 does not (may be the UEFI). I have heard that the 1803 version comes with a Spectre patch and the performance is bad. The Spectre patch can be disabled.
TODO
- Unbind GPU without
virsh
. - Run QEMU as user.
- Try if is necessary to edit
/etc/mkinitcpio.conf
. No need to load the kernel modules at boot. - Extract the vBIOS in Linux.
- Install guide.
- Fix the race condition.
- Network. I am too lazy, check this. I will add it to the scripts at some point.
- Update macos script.
- IOMMU guide.
- Audio.
- Troubleshooting guide.
- How to edit the
windows.sh
script. - Create scripts for install and use (Without DVD images).
- Improve the script with multiple options for HDD/IMG; network; PCI devices, etc.
- ACS Patch (Does not work for me).
- CPU pinning and RAM HugePages.
- Not kill X,
shifter
&xpra
may be the solution. Also uswsusp (userspace software suspend). Source