{"id":345,"date":"2019-07-08T15:13:54","date_gmt":"2019-07-08T13:13:54","guid":{"rendered":"\/wordpress\/?p=345"},"modified":"2019-07-08T15:21:47","modified_gmt":"2019-07-08T13:21:47","slug":"takeaways-utilising-vfio-in-a-kvm-virtual-guest","status":"publish","type":"post","link":"\/wordpress\/linux\/takeaways-utilising-vfio-in-a-kvm-virtual-guest\/","title":{"rendered":"Takeaways utilising VFIO in a KVM virtual guest"},"content":{"rendered":"<p>Having a seamless experience with a host OS running a KVM-guest that gets full access to a GPU by PCI passthrough does come with some hurdles. These are some takeaways from setting it all up.<\/p>\n<p>VFIO is a modern way of passing through PCI-devices from the host to a virtual machine. IOMMU must be enabled for this to work. Both in BIOS and in the kernel. For adding the kernel flags I found it easiest to use <code>grubby<\/code> for this.<\/p>\n<p>Do note that <code>grubby<\/code> modifies the current <code>grub.cfg<\/code> which will be overwritten when <code>grub2-mkconfig<\/code> is run. That will occur when upgrading a kernel. So this will have to be re-run after a kernel upgrade.<\/p>\n<pre><code>grubby --args=\"intel_iommu=on\" --update-kernel=\/boot\/vmlinuz-$(uname -r)\r\n<\/code><\/pre>\n<h2>KVM guest<\/h2>\n<ul>\n<li style=\"list-style-type: none;\">\n<ul>\n<li>Install the KVM guest as usual but change bootmode to UEFI. This requires the package <code>edk2-ovmf<\/code> installed.<\/li>\n<li>Then remove every virtual device having to do with display and spice<\/li>\n<li>Change USB controller to accomodate usb3<\/li>\n<li>Change storage and NIC to use virtio drivers<\/li>\n<li>Add PCI hostdevice\n<ul>\n<li>Select the graphics card, usually 01:00.0 along with its HD audio device 01:00.1. Then by using an HDMI cable you get both display &amp; sound!<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h3>\/etc\/dracut.conf.d\/vfio.conf<\/h3>\n<pre><code>add_drivers+=\"vfio vfio_iommu_type1 vfio_pci\"\r\n<\/code><\/pre>\n<p>The GPU being passed through must be disabled from being used by the host OS.<\/p>\n<h3>\/etc\/modprobe.d\/vfio.conf<\/h3>\n<pre><code>options vfio-pci ids=10de:1401,10de:0fba\r\noptions vfio-pci disable_vga=1\r\noptions snd-hda-intel enable_msi=1\r\n<\/code><\/pre>\n<pre><code>grubby --args=\"rd.driver.pre=vfio-pci\" --update-kernel=\/boot\/vmlinuz-$(uname -r)\r\ngrubby --args=\"video=efifb:off\" --update-kernel=\/boot\/vmlinuz-$(uname -r)\r\ndracut -f --kver $(uname -r)\r\n<\/code><\/pre>\n<p>Without enabling MSI the sound can begin to stutter when there&#8217;s too much data going through the HDMI-cable. To verify if it&#8217;s enabled, check for a plus sign in the <code>lspci -v -s 01:00.1|grep MSI:<\/code> output.<\/p>\n<pre><code>\tCapabilities: [68] MSI: Enable+ Count=1\/1 Maskable- 64bit+<\/code><\/pre>\n<h2>Keyboard &amp; mouse<\/h2>\n<p>Found a software-KVM solution called Barrier (<a href=\"https:\/\/github.com\/debauchee\/barrier\">https:\/\/github.com\/debauchee\/barrier<\/a>) to be most viable.<\/p>\n<ul>\n<li style=\"list-style-type: none;\">\n<ul>\n<li>Installed as a client on the VM. It will autostart when having made a connection.<\/li>\n<li>Installed as a server on the host OS. Went for letting the window manager autostart it upon login.\n<ul>\n<li>Tick use relative mouse moves<\/li>\n<li>For some games lock-mode will have to be used (scroll-lock)<\/li>\n<li>Non-ascii characters can be troublesome. Set <code>meta = altgr<\/code> and <code>altgr = shift<\/code> in the screens section.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h2>USB-devices<\/h2>\n<p>As for handling USB-devices inside the VM there are some options. I went for <code>udev<\/code> and wrote a script to handle that. It&#8217;s available on <a href=\"https:\/\/github.com\/mstrahlert\/vfio-usb\">https:\/\/github.com\/mstrahlert\/vfio-usb<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Having a seamless experience with a host OS running a KVM-guest that gets full access to a GPU by PCI passthrough does come with some hurdles. These are some takeaways from setting it all up. VFIO is a modern way of passing through PCI-devices from the host to a virtual machine. IOMMU must be enabled <span class=\"ellipsis\">&hellip;<\/span> <span class=\"more-link-wrap\"><a href=\"\/wordpress\/linux\/takeaways-utilising-vfio-in-a-kvm-virtual-guest\/\" class=\"more-link\"><span>Read More &rarr;<\/span><\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[52,20],"tags":[21,50,30,55,53],"_links":{"self":[{"href":"https:\/\/wp.strahlert.net\/wordpress\/wp-json\/wp\/v2\/posts\/345"}],"collection":[{"href":"https:\/\/wp.strahlert.net\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wp.strahlert.net\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wp.strahlert.net\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/wp.strahlert.net\/wordpress\/wp-json\/wp\/v2\/comments?post=345"}],"version-history":[{"count":12,"href":"https:\/\/wp.strahlert.net\/wordpress\/wp-json\/wp\/v2\/posts\/345\/revisions"}],"predecessor-version":[{"id":357,"href":"https:\/\/wp.strahlert.net\/wordpress\/wp-json\/wp\/v2\/posts\/345\/revisions\/357"}],"wp:attachment":[{"href":"https:\/\/wp.strahlert.net\/wordpress\/wp-json\/wp\/v2\/media?parent=345"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wp.strahlert.net\/wordpress\/wp-json\/wp\/v2\/categories?post=345"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wp.strahlert.net\/wordpress\/wp-json\/wp\/v2\/tags?post=345"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}