kvm passthrough 尝试

本文详细记录了在Ubuntu环境下,使用KVM进行GPU直通的多种尝试与解决方案,包括安装配置过程、遇到的问题及解决方法,特别是针对显卡驱动和内核参数调整的深入探讨。

·为什么放弃使用VMwareESXi?

因为需要两台电脑,用其中一台对另一台进行直通的管理,这太麻烦了。

前期准备:

安装KVM

 #apt-get install qemu-kvm qemu virt-manager virt-viewer libvirt-bin python-libvirt bridge-utils


准备直通的显卡编号:

pci_0000_04_00_0

pci_0000_04_00_1

0000:04:00.0VGA compatible controller [0300]: NVIDIA Corporation GM204GL [Quadro M4000] [10de:13f1] (rev a1)

0000:04:00.1Audio device [0403]: NVIDIA Corporation GM204 High Definition Audio Controller [10de:0fbb] (rev a1)

方案1:PCIPass-through(libvirt)

来源:http://www.it165.net/admin/html/201506/5722.html

1.在BIOS中打开IntelVT-d(成功)

2.在Linux内核中方启用PCIPass-through(成功:sudogedit \etc\default\grub)

3.重启系统,使配置生效(成功)

4.使用lspci-nn命令找到待分配的PCI设备(成功)

5.使用virshnodedev-list命令找到设备的PCI编号(成功)

6.使用virshnodedev-dettach命令将设备从主机上移除(失败,系统直接卡住,或者终端卡住,多次实验均没有效果)

7.使用virt-manager将设备直接分配给一个启动了的虚拟机(试图跳过第六步直接执行词步,失败,KVM直接卡住)

NodeDevice (help keyword 'nodedev'):

nodedev-create create a device defined by an XML file on the node

nodedev-destroy destroy (stop) a device on the node

nodedev-detach detach node device from its device driver

nodedev-dumpxml node device details in XML

nodedev-list enumerate devices on this host

nodedev-reattach reattach node device to its device driver

nodedev-reset reset node device

方案2:PCIPass-through(qemu)

来源:http://blog.youkuaiyun.com/halcyonbaby/article/details/37776211

1.与方案1中1~5相同(成功)

2.

modprobe pci_stub

(成功)
3.
echo "10de 13f1" > /sys/bus/pci/drivers/pci-stub/new_id
(成功)
4.
echo 0000:04:00.0 > /sys/bus/pci/devices/0000:04:00.0/driver/unbind
(失败,系统直接卡住,多次试验结果均相同)
5.
echo 0000:04:00.0 > /sys/bus/pci/drivers/pci-stub/bind
(失败:bash:echo: write error: No such device )
方案3:PCIPass-through(VFIO)

来源:http://blog.youkuaiyun.com/halcyonbaby/article/details/37776211

1.安装kernelmodule(系统已经默认安装)

2.

sudo modprobe vfio
(成功)
3.

sudo modprobe vfio-pci
(成功)
4.

cd /sys/bus/pci/devices/0000:04:00.0/
(成功)
5.

readlink iommu_group
(成功:../../../../kernel/iommu_groups/11)
6.

ll iommu_group/devices
(成功:
total0

drwxr-xr-x2 root root 0 5月1610:22 ./

drwxr-xr-x3 root root 0 5月1610:06 ../

lrwxrwxrwx1 root root 0 5月1610:22 0000:04:00.0 ->../../../../devices/pci0000:00/0000:00:1c.4/0000:04:00.0/

lrwxrwxrwx1 root root 0 5月1610:22 0000:04:00.1 ->../../../../devices/pci0000:00/0000:00:1c.4/0000:04:00.1/ )

7.

echo 0000:04:00.0 > /sys/bus/pci/devices/0000:04:00.0/driver/unbind
(与方案2第四步相同,但是居然成功了)
8.

echo 10de 13f1 > /sys/bus/pci/drivers/vfio-pci/new_id
(失败,终端卡住)
9.将第八步改为echo"10de 13f1" > /sys/bus/pci/drivers/vfio-pci/new_id(失败,终端卡住)

10.尝试使用nano命令改变该文件内容(失败,终多端卡住)

11.尝试先chmod744/777再用echo改变文件内容(失败,终端卡住)

12.重启之后又试了一次(终端依然卡住)

13.尝试sh-c "echo 10de 13f1 > /sys/bus/pci/drivers/vfio-pci/new_id"(失败)

14.由于7.成功了,尝试转向方案2,依然失败

执行第七步之后/sys/bus/pci/devices/0000:04:00.0/下的文件有明显改变,可能会对之后的操作方案有影响

方案4:GPUPassthrough via vfio-pci with KVM on Ubuntu 15.04

1.Install Ubuntu, I'm using 15.04 Server. You should also update your kernel tov4.0.0.(我的:ubuntu14.04,内核版本4.4.0)

2.

sudo apt-get install qemu-kvm
(完成)
3.in/etc/default/grub:


GRUB_CMDLINE_LINUX_DEFAULT="nomodeset intel_iommu=on pcie_acs_override=downstream pci=assign-bussesigb.max_vfs=2"
(成功)
Notice:"pci=assign-busses igb.max_vfs=2" were for my i350 SR-IOV.You can ignore them if you do not have SR-IOV in mind.

补充:这里应该再

sudo update-gru
4.Inetc/modprobe.d/local.conf(new file): (成功)

optionsvfio-pciids=1002:ffffffff:ffffffff:ffffffff:00030000:ffff00ff,1002:ffffffff:ffffffff:ffffffff:00040300:ffffffff,10de:ffffffff:ffffffff:ffffffff:00030000:ffff00ff,10de:ffffffff:ffffffff:ffffffff:00040300:ffffffff
补充:我使用的命令是sudo vi和sudo gedit

5.Reboot(成功)

然而此方案后面语焉不详,所以暂时放弃了

方案5:重新尝试之前的方案

研究了一下显卡驱动的问题,发现我的显卡驱动没安好。

默默安好了显卡驱动。

尝试方案1:卡死在第6步。

尝试方案2:系统卡死在之前成功了的第3步。重启后又尝试了一下,第3/4步成功。最后一步卡死。

尝试方案3:第7步卡死

方案6:GPU Passthrough, VGA Passthrough in KVM

来源:https://blog.lofyer.org/pass-host-gpu-to-guest-via-qemu-ncursescurses/

1.Enable the mainboard VxT, iommu and alter the video device to Intel HD(完成)

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

Add follow parameters to grub.conf:

 intel_iommu=onpci-stub.ids=1002:6819,1002:aab0,vfio_iommu_type1.allow_unsafe_interrupts=1
(成功)
3.Addmodprobe.conf to /etc/modprobe.d/ with this content:(成功)

blacklistradeon
optionskvm ignore_msrs=1
optionskvm allow_unsafe_interrupts=1
optionskvm-amd npt=0
optionskvm_intel emulate_invalid_guest_state=0
optionsvfio_iommu_type1 allow_unsafe_interrupts=1
4.changethe following options in /etc/libvirt/qemu.conf:(成功)

#The user ID for QEMU processes run by the system instance.
user= "root"

#The group ID for QEMU processes run by the system instance.
group= "root"

......

#If clear_emulator_capabilities is enabled, libvirt will drop all
#privileged capabilities of the QEmu/KVM emulator. This is enabled by
#default.
#
#Warning: Disabling this option means that a compromised guest can
#exploit the privileges and possibly do damage to the host.
#
clear_emulator_capabilities= 0
5.File:vfio-bind: (成功)

    #!/bin/bash 
    modprobe vfio-pci 
    for var in "$@"; do 
            for dev in $(ls /sys/bus/pci/devices/$var/iommu_group/devices); do 
                    vendor=$(cat /sys/bus/pci/devices/$dev/vendor) 
                    device=$(cat /sys/bus/pci/devices/$dev/device) 
                    if [ -e /sys/bus/pci/devices/$dev/driver ]; then 
                            echo $dev > /sys/bus/pci/devices/$dev/driver/unbind 
                            fi 
                    echo $vendor $device > /sys/bus/pci/drivers/vfio-pci/new_id 
            done 
    done
6.执行./vfio-bind0000:04:00.0 0000:04:00.1 (失败,终端卡住)

(注:这里我先用了chmod+x才执行了该文件)

总结一下这个方案,不过是之前的方案三换汤不换药。

方案7:修改虚拟机的xml文件

来源:http://www.server110.com/kvm/201402/5547.html

sudovirt-install --name=one --ram 2048 --vcpus=4 --diskpath=/root/one.img,size=40 --accelerate --cdrom/home/ubuntu/Downloads/111/cn_windows_7_ultimate_with_sp1_x64_dvd_u_677408.iso
(成功)

virsh edit On<span style="font-size:12px;"><span style="font-family:'Droid Sans Fallback', sans-serif;">e</span></span>
在xml文件内添加:

    <hostdev mode='subsystem' type='pci' managed='yes'> 
      <source> 
      <address domain='0x0000' bus='0x04' slot='0x00' function='0x0'/> 
      <address domain='0x0000' bus='0x04' slot='0x00' function='0x1'/> 
      </source> 
    </hostdev> 
(成功)

Failed.Try again? [y,n,f,?]:

error:(domain_definition):69: Opening and ending tag mismatch: hostdev line68 and devices

</devices>(已解决)

Failed.Try again? [y,n,f,?]:

error:XML error: Missing <source> element in hostdev device(已解决)

Domain One XML configuration edited.(成功)

virsh undefine One
virsh define One.xml
error:Failed to open file 'One.xml': No such file or directory

方案8:首先编译kernel

来源:http://www.linux-kvm.org/page/How_to_assign_devices_with_VT-d_in_KVM

……正在折腾中,待更新。


方案9:询问百度知道

来源:https://zhidao.baidu.com/question/1823842477872037748.html

在百度知道里面花了点财富值提问了一下我的问题,得到的答案是:

这个现象的原因一般有三个:1、硬件超温过热。2、系统某些基层文件出问题。3、内存松动。解决办法:1、清尘,检查散热风扇。2、重装系统。3、重新插拔内存并擦拭金手指部分。

然而这个电脑是个新电脑,不存在灰尘过多和内存松动的问题。(虽然如此,我还是把显卡和内存条都插拔并且清理了一遍)

重装系统也尝试了。

最终这三个方法都没有解决我的问题。

方案10: 检查显卡驱动

这个方案得到了成功。

具体步骤见在ubuntu 14.04下的KVM虚拟机显卡直通
--------------------- 
作者:湛蓝之息 
来源:优快云 
原文:https://blog.youkuaiyun.com/Zhanxix/article/details/72399768 
版权声明:本文为博主原创文章,转载请附上博文链接!

<think>我们遇到了一个libvirt错误:在不支持的配置中,对于aarch64架构的QEMU虚拟机,在aarch64主机上使用'host-passthrough' CPU模式不被虚拟机监控程序(hypervisor)支持。 问题分析: - 错误信息明确指出,在aarch64架构的主机上,为aarch64架构的QEMU虚拟机配置CPU模式为'host-passthrough'不被支持。 - 'host-passthrough'模式通常用于将主机的CPU特性完全暴露给虚拟机,以实现最佳性能,但这也可能导致兼容性问题,特别是当主机和虚拟机架构相同时。 解决方法: 1. 尝试更改CPU模式:根据错误信息,'host-passthrough'不被支持,我们可以尝试使用其他CPU模式,例如'host-model'或'custom'(并指定一个合适的CPU模型)。 2. 检查虚拟机监控程序(Hypervisor)的支持情况:确保你的Hypervisor(例如QEMU/KVM)版本支持在aarch64上使用'host-passthrough'。如果不支持,则需要升级或使用替代方案。 3. 检查libvirt的版本:确保libvirt版本足够新,以支持所需的特性。 4. 如果确实需要'host-passthrough'模式,可能需要考虑更换到支持该模式的平台(如x86_64)或者等待后续版本的支持。 具体步骤(以更改为'host-model'为例): - 编辑虚拟机XML配置文件,将CPU模式从'host-passthrough'改为'host-model'。 - 或者使用virt-manager图形界面修改虚拟机的CPU设置。 修改CPU模式的XML配置示例: 原配置可能是这样的: ```xml <cpu mode='host-passthrough'/> ``` 改为: ```xml <cpu mode='host-model'/> ``` 注意:修改配置后需要重新定义虚拟机(使用`virsh define`命令)并重启虚拟机。 但是,请注意,'host-model'模式可能会根据主机CPU自动选择一个最接近的CPU模型,而不完全穿透所有特性,因此性能可能不如'host-passthrough',但兼容性更好。 如果问题仍然存在,可能需要进一步检查Hypervisor和libvirt的日志(如/var/log/libvirt/qemu/虚拟机名称.log)以获取更多信息。 §§相关问题§§ 1. 如何检查当前主机上QEMU/KVM支持的CPU模式? 2. 在aarch64架构上,除了'host-passthrough',还有哪些可用的CPU模式? 3. 如何升级libvirt和QEMU以支持更多的CPU特性? 4. 使用'host-model'模式与'host-passthrough'模式在性能上有多大差异? </think>### 问题分析 `libvirtError: unsupported configuration: CPU mode 'host-passthrough' for aarch64 qemu domain on aarch64 host is not supported by hypervisor` 错误表明: 1. **架构限制**:在 ARM64 (aarch64) 主机上运行 ARM64 虚拟机时,QEMU/KVM 不支持 `host-passthrough` CPU 穿透模式。 2. **根本原因**: - `host-passthrough` 会将主机 CPU 指令集完全暴露给虚拟机,但 ARM 架构的 CPU 特性穿透存在兼容性问题。 - 当前 Hypervisor(通常是 QEMU/KVM)或 libvirt 版本未实现该功能。 ### 解决方法 #### ✅ 方案 1:改用兼容的 CPU 模式 编辑虚拟机 XML 配置(`virsh edit <VM_NAME>`),替换 CPU 模式: ```xml <!-- 原配置(错误) --> <cpu mode='host-passthrough'/> <!-- 修改为(二选一) --> <cpu mode='host-model'/> <!-- 自动匹配主机兼容模型 --> 或 <cpu mode='custom' match='exact'> <model fallback='allow'>cortex-a72</model> <!-- 指定具体 ARM CPU 型号 --> </cpu> ``` > **推荐型号**:根据主机 CPU 选择 `cortex-a53`, `cortex-a72`, `neoverse-n1` 等(通过 `lscpu` 查看主机型号)。 #### ✅ 方案 2:升级软件堆栈 ```bash # 升级 QEMU/KVM 和 libvirt(以 Debian/Ubuntu 为例) sudo apt update && sudo apt install qemu-system-arm libvirt-daemon-system # 重启服务 sudo systemctl restart libvirtd ``` > 需验证新版本是否支持该特性(ARM 平台支持进度较慢)。 #### ✅ 方案 3:绕过限制(风险较高) 在 XML 中手动添加 CPU 特性(需严格匹配主机): ```xml <cpu mode='custom' match='exact'> <model fallback='forbid'>host</model> <!-- 通过 `cat /proc/cpuinfo` 获取特性列表 --> <feature policy='require' name='pmu'/> <feature policy='require' name='fp'/> </cpu> ``` > ️ 此操作可能导致虚拟机启动失败,仅建议测试环境使用。 ### 验证是否修复 1. 保存配置:`virsh define <XML_FILE>` 2. 启动虚拟机:`virsh start <VM_NAME>` 3. 检查日志:`journalctl -u libvirtd | grep <VM_NAME>`
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值