1. Install Qemu-KVM 1.1 Enable the virtualization setting in the BIOS first.
1.2 Check KVM installation environment.
$ sudo apt-get update
$ sudo apt-get install cpu-checker
$ sudo modprobe kvm_intel
$ sudo modprobe kvm
1.3 If everything is okay, you can install the qemu-kvm packages.
$ sudo apt-get install qemu-kvm libvirt-bin ubuntu-vm-builder bridge-utils
$ sudo adduser `id -un` libvirtd
$ sudo chmod 777 /var/run/libvirt/libvirt-sock
$ sudo chown root:libvirtd /dev/kvm
$ virsh -c qemu:///system list
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.
$ sudo apt-get install virt-manager
2. Enable IOMMU support
$ sudo vi /etc/default/grub
$ sudo update-grub
$ sudo reboot
You can verify the IOMMU support by type in
$ dmesg | grep -e DMAR -e IOMMU
If you are using an Intel machine. For AMD, check here.
3. Modify KVM kernel I skipped this step.
4. Verify the PCI that you need to attach to the virtual machine.
Suppose we are interested in passing through this device
20:00.0 VGA compatible controller: Advanced Micro Devices [AMD] nee ATI Caicos [Radeon HD 6450]
which is a PCI-Express Graphics Card. This PCI device 20:00.0 will be the example device in the following steps.
Note down the device ID 1002:6779 .
$ lspci -n
20:00.0 0300: 1002:6779
5. Unbind and bind5.1 Load the PCI stub module.
$ sudo modprobe pci_stub
# echo "1002 6779" > /sys/bus/pci/drivers/pci-stub/new_id
# echo 0000:20:00.0 > /sys/bus/pci/devices/0000\:20\:00.0/driver/unbind
echo 0000:20:00.0 > /sys/bus/pci/drivers/pci-stub/bind
6. Assign device
Add extra parameters as below after the usual kvm command.
$ kvm ... -device pci-assign,host=20:00.0
If you see errors, check the following section.
7. Necessary permission modifications According to my experience, you might encounter an error saying:
Device 'pci-assign' could not be initialized.
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).
The command that fixed it was:
# echo 1 > /sys/module/kvm/parameters/allow_unsafe_assigned_interrupts
But I did try something else before the command above, so I suppose that if this command doesn't work, try the following:
# vi /etc/libvirt/qemu.conf
Uncomment and set
clear_emulator_capabilites = 0.
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 under root.
# kvm ... -device pci-assign,host=20:00.0
In the guest OS, run lspci, and check if you can see the assigned pci device there.