This post describes how to pass through the host operating system, in order to expose a real piece of hardware device to a KVM virtual machine.
1. Install Qemu-KVM[1]
1.1 Enable the virtualization settings in BIOS.
1.2 Check KVM installation environment.
1 2 3 4 5 | |
1.3 If everything is okay, you can install the qemu-kvm packages.
1 2 3 4 5 | |
1.4 If the command below shows you an empty list of virtual machines, the installation is successful. You may now install the virt-manager. which is the GUI tool for KVM.
1
| |
2. Enable IOMMU support
1
| |
and add intel_iommu=on in GRUB_CMDLINE_LINUX_DEFAULT parameter.
Regenerate the grub list:
1
| |
After reboot, you can verify the IOMMU support by type in
1
| |
If you are using an Intel machine. For AMD, check here.
3. Modify KVM kernel [2]
I skipped this step.
4. Verify the PCI that you need to attach to the virtual machine.
In the host os, type:
1
| |
Suppose we are interested in passing through this device:
1
| |
which is a PCI-Express Graphics Card. This PCI device 20:00.0 will be the example device in the following steps.
1 2 3 4 | |
Note down the device ID 1002:6779 .
5. Unbind and bind
5.1 Load the PCI stub module.
1
| |
5.2 Unbind the device from the host kernel, and bind it to pci stub. (need root access)
1 2 3 | |
6. Assign device
Add extra parameters as below after the usual kvm command.
1
| |
If you see errors, check the following section.
7. Necessary permission modifications [3]
According to my experience, you might encounter an error saying:
1
| |
Before you try any solution, first make sure that modules such as kvm, kvm_intel, pci_stub are loaded. Also make sure that intel_iommu=on is in your grub configuration file (even if you see IOMMU from dmesg, it doesn’t ensure that INTEL_IOMMU is enabled). Then if the error is still there, try the following command:
1
| |
But I did try something else before the command above, so I suppose that if this command doesn’t work, try the following:
1
| |
Uncomment and set user and group to root.
Set clear_emulator_capabilites = 0.
Set relaxed_acs_check = 1.
Besides, if you are using a RPM-based system such as Fedora, you also need to set SELinux to permissive.
Then run kvm again as root.
1
| |
8. Done.
In the guest OS, run lspci, and check if you can see the assigned pci device there.
References
[1] https://help.ubuntu.com/community/KVM/Installation
[2] http://www.linux-kvm.org/page/How_to_assign_devices_with_VT-d_in_KVM
[3] http://comments.gmane.org/gmane.comp.emulators.libvirt.user/2945