Forwarding a video card in KVM from under ubuntu
- From the sandbox
- Tutorial
Prehistory
About two years ago, I decided to switch completely to Linux, but the need for a Vend did not fall, so I always kept 2 operating systems. About a month ago, I tried to install VirtualBox and install Windows on it, in general I liked working with 2 operating systems, but there was one problem, I only program computer graphics and I needed support for OpenGL 3.3 and higher, unfortunately VirtualBox did not provide me with such opportunities. Googling came across such a thing as Xen, tried for a long time to do something on it, as a result, nothing came of it, most likely due to the main video card - the nVidia GeForce 580 GTX, the hypervisor simply did not want to launch even ubuntu, not to mention Windows. I started looking for other hypervisors, and came across KVM, I had to tinker with it, but in the end it worked.
Configuration
So we need:
- A processor with virtualization support (in Intel it is VT-d, in AMD it is AMD-Vi). Important: the chipset must also support this technology.
- Motherboard with I / O Memory Management Unit (IOMMU)
- Two video cards, for a guest OS it is advisable to use an AMD video card, because there should be no problems with her. NVidia graphics cards do not work.
My configuration:
- AMD Phenom II x4 processor
- Asus M5A99X EVO R2.0 motherboard
- Video card for Ubuntu: NVidia GeForce 580 GTX
- Video card for guest OS: AMD Radeon HD 5800
Training
First you need to reboot into the BIOS and enable IOMMU. To check if IOMMU works for AMD:
dmesg | grep -iE "(IOMMU|AMD-Vi)"
or for Intel:
dmesg | grep -iE "(IOMMU|VT-d)"
Now you need to enable iommu in grub. Open / etc / default / grub, look for GRUB_CMDLINE_LINUX, bring it to the view:
GRUB_CMDLINE_LINUX="max_loop=64 iommu=pt iommu=1 amd_iommu=fullflush"
Thus, we increase the number of LOOP devices, enable IOMMU support for AMD. If everything became flattened when loading the operating system (usually this is due to 2 video cards), then add nomodeset to the previous line, we get:
GRUB_CMDLINE_LINUX="nomodeset max_loop=64 iommu=pt iommu=1 amd_iommu=fullflush"
Also, to reduce glitches, you can prevent drivers from downloading from AMD (if you have both video cards from AMD , then you should not do this) by adding them to the blacklist. Open /etc/modprobe.d/blacklist.conf and add the following:
blacklist fglrx
blacklist radeon
Now you need to prepare the video card for forwarding by disconnecting it from the driver on the host and connecting it to pci-stub. To select our card, enter lspci and look for our card:
02:00.0 VGA compatible controller: Advanced Micro Devices [AMD] nee ATI Cypress PRO [Radeon HD 5850]
02:00.1 Audio device: Advanced Micro Devices [AMD] nee ATI Cypress HDMI Audio [Radeon HD 5800 Series]
in my case, it is 02: 00.0 and 02: 00.1. 02:00 a.m. is a sound chip. The video card is visible as video and audio, so they should be transferred together.
Then, we need vendor id and device id. You can see by running lspci with the -n switch.
02:00.0 0300: 1002:6899
02:00.1 0403: 1002:aa50
Now we will connect our devices to pci-stub. Video card:
echo "1002 6899" > /sys/bus/pci/drivers/pci-stub/new_id
echo "0000:02:00.0" > /sys/bus/pci/devices/0000\:02\:00.0/driver/unbind
echo "0000:02:00.0" > /sys/bus/pci/drivers/pci-stub/bind
please note that there is no colon between VendorID and DeviceID.
Sound card:
echo "1002 aa50" > /sys/bus/pci/drivers/pci-stub/new_id
echo "0000:02:00.1" > /sys/bus/pci/devices/0000\:02\:00.1/driver/unbind
echo "0000:02:00.1" > /sys/bus/pci/drivers/pci-stub/bind
for convenience, you can write a script and add it to autorun.
Show script
#!/bin/bash
hostgr="0000:02:00.0"
hostau="0000:02:00.1"
grid="1002 6899"
auid="1002 aa50"
#
echo $grid > "/sys/bus/pci/drivers/pci-stub/new_id"
echo $hostgr > "/sys/bus/pci/devices/$hostgr/driver/unbind"
echo $hostgr > "/sys/bus/pci/drivers/pci-stub/bind"
echo $auid > "/sys/bus/pci/drivers/pci-stub/new_id"
echo $hostau > "/sys/bus/pci/devices/$hostau/driver/unbind"
echo $hostau > "/sys/bus/pci/drivers/pci-stub/bind"
Installation
First you need to install several packages:
sudo apt-get install qemu-kvm libvirt-bin ubuntu-vm-builder bridge-utils
this is an installation on a server without a gui, for convenient work with kvm we will also install virt-manager:
sudo apt-get install virt-manager
After that, the menu item "Virtual Machine Manager" should appear on the menu.
Next, we will transfer the allow_unsafe_assigned_interrupts = option to the kvm module 1. We create the file in /etc/modprobe.d/kvm.conf with the contents and restart the module.
options kvm allow_unsafe_assigned_interrupts=1
Next, run virt-manager, a window will appear:

Right-click on localhost and select New or click on the monitor on the left in the panel. Next, follow the instructions to create a virtual machine. After installing the system, you can forward the devices we need. Go to the View-> Details menu. The following window should appear:

Click on the Add hardware button, select the PCI Host Device, and look for our video card. The same thing should be done with the sound chip from the video card. In the same way, you can forward USB hosts so that they work, you should specify in the Controller USB in the Model: USB2 field.
When starting up on the computer’s devices, our adapter should appear: The

next thing we need to do is download the driver. Download from the manufacturer’s website and install it in the system. When I tried to install the driver, the system frantically went to BSOD, I solved this problem by installing a newer Windows. You can also try installing the driver through the device manager, but for me it just simply hung.
To complete it, turn off the virtual machine and reboot the computer, if this is not done, when you restart the system may go to BSOD.
At the moment, when restarting a guest with a forwarded video card, kvm hangs the entire system, so if you turn off the virtual machine, you will also have to restart the entire computer.
Total
Now, after starting, the virtual machine’s console will turn off after loading the video card driver, in the case of Windows 7, the picture “Starting Windows” remains there, but the image itself should already be output to the forwarded video card. Although the image will not be displayed on the konos, nevertheless it will transmit data from input devices and it will be possible to control the guest from the keyboard and mouse as a normal virtual machine.
We cling the second monitor to the forwarded video card, and enjoy the work done.
Finally, a screenshot of the system performance assessment:

Sources
help.ubuntu.com/community/KVM www.linux.org.ru/wiki/en/ Forwarding a video card to a virtual
machine