pci passthrough with qemu

本文详细介绍了如何在Linux KVM环境下,通过硬件支持iommu实现设备passthrough的方法,包括硬件检查、内核配置、错误分析及解决策略等关键步骤。同时,还探讨了使用vfio进行passthrough的不同之处和具体操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

http://pic.dhe.ibm.com/infocenter/lnxinfo/v3r0m0/index.jsp?topic=%2Fliaat%2Fliaatbppassthrougtask.htm


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


1. 确定硬件是否支持iommu

这是一个硬件的功能,intel架构上叫vt-d。

从BIOS上可以看出来, 不同版本放的位置不一样。

我的是在 Security->Virtualization->VT-d


2. 配置kernel,支持iommu

在intel机器上有这么个选项,

INTEL_IOMMU

  │   Location:                                                                                                                            │  
  │     -> Device Drivers                                                                                                                  │  
  │ (2)   -> IOMMU Hardware Support (IOMMU_SUPPORT [=y]) 


3. 重启,看看是否enable了。

在intel上,运行

dmesg | grep -e DMAR -e IOMMU


4. 在host上unbind设备

加载pci_stub模块, modprobe pci_stub

选中某个设备, lspci -s 03:00.0 -n, 03:00.0 0280: 8086:0085 (rev 34)

bind/unbind

  • echo "8086 0085" > /sys/bus/pci/drivers/pci-stub/new_id
  • echo 0000:03:00.0 > /sys/bus/pci/devices/0000:01:00.0/driver/unbind
  • echo 0000:03:00.0 > /sys/bus/pci/drivers/pci-stub/bind

5. 把设备给guest

./i386-softmmu/qemu-system-i386 -enable-kvm \

-drive file=../../kvm/ubuntu.qcow2 -boot dc -m 512  \

-monitor stdio \

-netdev user,id=usernet -device rtl8139,netdev=usernet \

-device pci-assign,host=0000:03:00.0


不过偶没有成功,报错了。。。


6. 错误分析,解决

按照上面的办法没成功,我看了一下dmesg,有这么一句。

kvm_iommu_map_guest: No interrupt remapping support, disallowing device assignment. Re-enble with "allow_unsafe_assigned_interrupts=1" module option.


稍微调查了一下,发现和这个选项有关,config IRQ_REMAP。 可惜我enable不了,因为这个必须在x86_64上做。

没办法,那我只好在装载kvm模块的时候加上这个参数的设置了。


加上这个参数后,果然就好了。赞。



接下来我用了vfio来做passthrough。步骤略有不同

1. 安装相关的kernel module

sudo modprobe vfio

sudo modprobe vfio-pci


这样就产生了 /dev/vfio/vfio 设备


2. 按照文档要求,添加设备到一个group

按照现在的要求,需要讲某一个iommu_group下的所有设备都添加到一个vfio group.

cd /sys/bus/pci/devices/0000:0d:00.0/

readlink iommu_group  这样可以显示该设备对应iommu_group的名字

ll iommu_group/devices 这样可以显示和该设备处于一个iommu_group的设备。


echo 0000:0d:00.0 > /sys/bus/pci/devices/0000:0d:00.0/driver/unbind

echo 1180 e823 > /sys/bus/pci/drivers/vfio-pci/new_id

echo 0000:03:00.0 > /sys/bus/pci/devices/0000:03:00.0/driver/unbind

echo 8086 0085 > /sys/bus/pci/drivers/vfio-pci/new_id


这样就生成了 /dev/vfio/6  (数字由iommu_group号决定)


3. 启动 guest

../git/qemu-latest/i386-softmmu/qemu-system-i386 -enable-kvm -drive file=ubuntu.qcow2 -boot dc -m 512  -monitor stdio -netdev user,id=usernet -device vfio-pci,host=0000:03:00.0


如果是在power机器上,则写成

-device "spapr-pci-vfio-host-bridge,id=CXGB3,iommu=4,index=6"


4. fix

默认这么启动会有个错误,当然这是在我的机器上。

vfio_iommu_type1_open: No interrupt remapping support.  Use the module param "allow_unsafe_interrupts" to enable VFIO IOMMU support on this platform


意思是我这个机器不支持remap, 那我就把这个参数直接设成了1.  就好了。





### Proxmox VE 7.4 Passthrough Configuration Guide Passthrough technology allows hardware devices to be directly accessed by virtual machines, providing near-native performance and compatibility. For configuring passthrough on Proxmox VE 7.4, several steps need careful attention. #### Preparation Before Configuration Before starting the configuration process, ensure that Intel VT-d or AMD IOMMU is enabled in BIOS settings as these features are essential for device passthrough functionality[^4]. Additionally, verify whether your CPU supports such technologies through official documentation or manufacturer specifications. #### Modifying Kernel Parameters Edit `/etc/default/grub` file using a text editor with root privileges: ```bash nano /etc/default/grub ``` Add `intel_iommu=on` or `amd_iommu=on`, depending upon the processor type, into the line beginning with `GRUB_CMDLINE_LINUX_DEFAULT`. This modification ensures proper initialization of IOMMU during boot-up which facilitates safe direct memory access between guest VMs and physical devices without compromising system stability. After editing this parameter, update grub configurations via command: ```bash update-grub ``` Reboot the server after completing above changes so they can take effect immediately. #### Configuring PCI Device Assignment Identify target devices intended for assignment from within host machine by executing following commands sequentially: ```bash lspci -nn | grep -i network # List all Network Interface Cards (NICs) lspci -v # Display detailed information about each installed card ``` Once identified, note down their Bus ID numbers carefully since those will serve as identifiers when assigning them later on. Proceed towards creating QEMU/KVM options inside respective Virtual Machine's configuration files located at path `/etc/pve/qemu-server/<vmid>.conf`. For example, adding below lines assigns an Ethernet controller connected over PCIe slot having bus address '0000:01:00.0': ```plaintext hostpci0: 0000:01:00.0,type=legacy ``` Save modifications made hereafter restarting affected guests should reflect newly assigned resources accordingly. #### Verifying Successful Setup Upon completion of setup procedures outlined previously, validate successful integration either visually inspecting WebUI under Hardware tab associated with specific Guest OS instance or running diagnostic utilities like `dmesg |grep vfio` post-login session establishment thereupon. --related questions-- 1. What precautions must one consider before enabling IOMMU support? 2. How does setting up SR-IOV differ from traditional PCI-passthrough methods? 3. Can multiple GPUs be simultaneously passed-through different VMs residing same PVE node? 4. Is it possible to hot-plug/unplug pass-throughed peripherals while VM remains powered-on state?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值