GPU Passthrough, VGA Passthrough in KVM

By | 2013年5月23日

To inspire you, I’ve got a video from someone else. Better mutt the volume by the way.
Arch Linux KVM Crysis HD Gpu Passthrough
Or you can download it to see.
Download the video in HD

Here’s the links I refer to:
http://thread.gmane.org/gmane.comp.emulators.kvm.devel/71981
https://bbs.archlinux.org/viewtopic.php?id=162768
https://docs.google.com/document/d/1ef_nfl652L0HLn_wGvnpgjsBJd9LZzaV_-rIcEEoK8Y/edit?pli=1
http://www.linux-kvm.org/page/VGA_device_assignment
http://www.linux-kvm.org/page/How_to_assign_devices_with_VT-d_in_KVM

Result:
VGAPassthrough: success in host F19, guest Windows7
GPUPassthrough: success in Fedora-Rawhide

HOST:
CPU: Core i5 3470
GPU: ATI HD Radeon 7850
OS: Fedora-Rawhide
QEMU: qemu-1.5.1
kvm-vgapassthrough
So, here’s the steps

0. Enable the mainboard VxT, iommu and alter the video device to Intel HD

1. See what we have got now.

We have output below

You can see the pci bus and vendor.

2. Modify the kernel parameter, morprobe.d and libvirt.conf

Add follow parameters to grub.conf

NOTE: If you have got an AMD cpu, please replace “interl_iommu=on” with “iommu=pt iommu=1”
Add modprobe.conf to /etc/modprobe.d/ with this content:

change the following options in /etc/libvirt/qemu.conf:

Reboot.

3. Using scripts below

Version 1: VFIO-Passthrough

File: vfio-bind

Bind the device

Start VM

Version 2: PCI-Passthrough

Bind device

Start VM

23 thoughts on “GPU Passthrough, VGA Passthrough in KVM

  1. Jackson

    does spice work with VM using vfio-pci passthrough and pci-stub passthrough?

    Reply
    1. lofyer Post author

      Spice does not work when the videocard was passed to the vm except spice keyboard and mouse channel. You should use vnc in the VM instead.

      Reply
    1. lofyer Post author

      No.
      I used Fedora Rawhide. Since he said that the patch might have been applied by the time I did this experiment.
      The patches mentioned in that thread was to fix the “reboot issue”, but I skipped this by using dev-eject in the guest before shutting down.

      Reply
      1. liyong

        第二个显卡可以实现透传了,但是第一个显卡不能透传,显示器启动没有任何画面,并且停止虚拟机也不能回到linux系统的桌面,请问,如何实现第一个显卡的透传

        Reply
        1. lofyer Post author

          第一个显卡你意思是你的工作系统用的吗?这个也可以透传,但是传进去后你只能通过ssh上去debug了。

          Reply
          1. ly

            pc配置i7 6700,16g内存,两张显卡1060显卡,用的vfio方式,目前的症状是工作用第一张1060显卡,第二张显卡可以成功透传,一旦虚拟机启动,虽然可以成功,但是工作的画面就卡死了,因为显卡是一摸一样的,所以PCI ID号是一样的,但是我如果用CPU集成显卡启动两个虚拟机就没有问题,前提是打了i915vga补丁,前几天打了补丁发现没问题,后来把系统删除重新开始做就发现实现不了,郁闷!还有我用virsh nodedev-detach 解绑第二张显卡,结果发现我工作的第一张显卡也不解绑且不工作了,我在4.x的内核是这样,但是在3.15内核可以任意解绑PCI设备,不知道什么原因

          2. lofyer Post author

            你可以使用subsystem id而非pci id;在关机或重启之前,请把虚拟机里的显卡卸载,可手动可用小程序。

          3. ly

            问题是,为什么3.x的内核可以直接解绑pci显卡,4.x的内核就不行呢,一解绑屏幕就死了

  2. shweng

    楼主,
    有问题请教一下,我也实现了穿透了第二块显卡(ATI HD7750)到虚拟机里,用的是第一种方式VFIO。目前显示器联接到HD7750的DVI口可以进到虚拟机桌面,穿透的显卡可用,QEMU窗口是黑的,没有显示输出。我想让虚拟机桌面显示到QEMU窗口或远程桌面工具上而不是直接输出到显卡输出端口。看到你的视频好像虚拟机图像是在Host的窗口中显示的,能否告知怎样详细配置,是直接QEMU窗口显示,还是通过远程桌面工具如VNC Viewer,Spicy连接的?

    Reply
    1. lofyer Post author

      当时用的方式是虚机里运行TurboVNC或者TigerVNC的server,然后外面再用client连接。

      Reply
      1. Tony

        是这种方式吗?
        qemu-system-x86_64 ….. -vnc :45

        然后用vnc viewer IP:45的方式吗?

        Reply
        1. lofyer Post author

          成功后你用VNC连接qemu的VNC确实是没输出的,你在显卡的接口上接一个显示器应该会看到桌面。

          Reply
  3. Geneva

    Interesting content. You should use social websites to increase traffic and make your site go viral.
    There are tools which automate this time consuming process.Visitors can flood your
    website in no time, just search in google for:
    Rixisosa’s Social Automation

    Reply
  4. mq

    你好!我也在尝试用vfio,pci-stub passthrough pci vga。我想请问一下:
    1 第一次运行qemu,显卡是不是没有输出,必须要装上驱动才可以?
    2 你试过用vfio+ovmf吗?

    Reply
    1. lofyer Post author

      1. 透传成功之前显卡一直没有输出,都是输出到qemu的monitor,成功后,需要安装显卡驱动,才会输出到物理显卡。
      2. 没有试过 ^_^

      Reply
  5. Tony

    您好,我用了您的Vfio的方法进行passthough,命令是 qemu-system-x86_64 -enable-kvm -M q35 -m 2G -cpu host,kvm=off -smp 6,sockets=1,cores=6,threads=1 -boot d -hda /root/kvm_demo_3.18/win7_64_nvidia_vfio_vnc.img -cdrom /root/kvm_demo/cn_windows_7_ultimate_with_sp1_x64_dvd_u_677408.iso -drive file=/root/kvm_demo/invida_k2000_driver.iso,media=cdrom -device vfio-pci,host=04:00.0,x-vga=on -device vfio-pci,host=04:00.1 -vnc :45 。
    我安装好系统后,装上显卡的驱动,禁用掉qemu提供的标准显卡。然后shutdown guest 系统,重新输入上面的命令,然后用vnc viewer连接,发现是黑屏,上面出现“Guest has not initialized the display(yet)”。 我的显卡(丽台k2000)是“04:00.0”,“04:00.1”是这块显卡上集成的声卡。 我在host上的另一个terminal中输入“dmesg | grep 04:00.0”,输出“pci 0000:04:00.0: can’t claim BAR 6 [mem 0xfff80000-0xffffffff pref]: no compatible bridge window
    pci 0000:04:00.0: BAR 6: assigned [mem 0xa9080000-0xa90fffff pref]
    pci 0000:04:00.0: Signaling PME through PCIe PME interrupt
    pci-stub 0000:04:00.0: claimed by stub
    vfio-pci 0000:04:00.0: irq 117 for MSI/MSI-X”。不知道是如何 passthough显示不出来,请教一下。

    Reply
    1. 董小姐

      我的问题是我透传,dp连接到物理显卡已经可以看到画面,可是去设备管理器看不到显卡,装驱动就蓝屏。我是董小姐 焦急等待回复!

      Reply
  6. 董小姐

    我的问题是我把dp头插在独显上,已经透传了画面,可是我去设备管理器查看就找不到显卡,装驱动就蓝屏

    Reply

发表评论

电子邮件地址不会被公开。