飞牛OS系统:如何开启虚拟机IOMMU直通功能

发布于 2025年03月25日

IOMMU是现代CPU提供的一种硬件功能,它能够将物理设备直接映射到虚拟机,称为**设备直通**。在飞牛NAS系统中,使用KVM/QEMU虚拟化技术,IOMMU直通功能可以将PCI设备(如GPU、网卡等)直接分配给虚拟机,显著提升性能和兼容性。

本文将详细讲解如何在飞牛NAS系统中开启IOMMU,并配置虚拟机直通功能。

步骤一:确认硬件支持

在开启IOMMU前,需确认CPU和主板硬件支持:

1. Intel CPU:需支持 VT-d(Virtualization Technology for Directed I/O)。
2. AMD CPU:需支持 AMD-Vi。
3. BIOS设置:确保BIOS已启用相关选项(如 Intel VT-d 或 AMD IOMMU)。

通过命令验证硬件支持:

dmesg | grep -e "DMAR" -e "IOMMU"  # Intel
dmesg | grep -e "AMD-Vi"          # AMD

如果输出中包含 DMARAMD-Vi,说明硬件支持已启用。

root@NASOS:~# dmesg | grep -e "DMAR" -e "IOMMU"
[    0.009549] ACPI: DMAR 0x00000000427DB000 000088 (v01 INTEL  EDK2     00000002      01000013)
[    0.009576] ACPI: Reserving DMAR table memory at [mem 0x427db000-0x427db087]
[    0.015392] DMAR: IOMMU enabled
[    0.039041] DMAR: Host address width 39
[    0.039042] DMAR: DRHD base: 0x000000fed90000 flags: 0x0
[    0.039045] DMAR: dmar0: reg_base_addr fed90000 ver 4:0 cap 1c0000c40660462 ecap 29a00f0505e
[    0.039046] DMAR: DRHD base: 0x000000fed91000 flags: 0x1
[    0.039050] DMAR: dmar1: reg_base_addr fed91000 ver 5:0 cap d2008c40660462 ecap f050da
[    0.039051] DMAR: RMRR base: 0x0000004c000000 end: 0x000000503fffff
[    0.039053] DMAR-IR: IOAPIC id 2 under DRHD base  0xfed91000 IOMMU 1
[    0.039054] DMAR-IR: HPET id 0 under DRHD base 0xfed91000
[    0.039055] DMAR-IR: Queued invalidation will be enabled to support x2apic and Intr-remapping.
[    0.040588] DMAR-IR: Enabled IRQ remapping in x2apic mode
[    0.247454] pci 0000:00:02.0: DMAR: Skip IOMMU disabling for graphics
[    0.307589] DMAR: No ATSR found
[    0.307590] DMAR: No SATC found
[    0.307591] DMAR: IOMMU feature fl1gp_support inconsistent
[    0.307592] DMAR: IOMMU feature pgsel_inv inconsistent
[    0.307592] DMAR: IOMMU feature nwfs inconsistent
[    0.307593] DMAR: IOMMU feature dit inconsistent
[    0.307593] DMAR: IOMMU feature sc_support inconsistent
[    0.307594] DMAR: IOMMU feature dev_iotlb_support inconsistent
[    0.307594] DMAR: dmar0: Using Queued invalidation
[    0.307597] DMAR: dmar1: Using Queued invalidation
[    0.308172] DMAR: Intel(R) Virtualization Technology for Directed I/O

步骤二:启用IOMMU内核功能

1. 编辑GRUB配置文件

sudo nano /etc/default/grub

2. 修改内核启动参数

- Intel CPU

GRUB_CMDLINE_LINUX_DEFAULT="quiet i915.force_probe=7d55 intel_iommu=on iommu=pt"  //输入对应的intel_iommu=on iommu=pt

- AMD CPU

GRUB_CMDLINE_LINUX_DEFAULT="quiet splash amd_iommu=on iommu=pt"

参数说明:

intel_iommu=on 或 amd_iommu=on:启用IOMMU。
iommu=pt:启用直通模式(Pass-Through),优化性能。

3. 更新GRUB并重启

sudo update-grub && sudo reboot

步骤三:验证IOMMU是否启用

重启后检查内核日志:

dmesg | grep -i "IOMMU"

输出类似:

root@NASOS:~# dmesg | grep -i "IOMMU"
[    0.000000] Command line: BOOT_IMAGE=/boot/vmlinuz-6.6.38-trim root=UUID=03bc50e4-cf78-4bb8-bed6-cd5a93e37913 ro modprobe.blacklist=pcspkr quiet i915.force_probe=7d55 intel_iommu=on iommu=pt
[    0.015347] Kernel command line: BOOT_IMAGE=/boot/vmlinuz-6.6.38-trim root=UUID=03bc50e4-cf78-4bb8-bed6-cd5a93e37913 ro modprobe.blacklist=pcspkr quiet i915.force_probe=7d55 intel_iommu=on iommu=pt
[    0.015392] DMAR: IOMMU enabled   //enabled说明已经是开启成功
[    0.039053] DMAR-IR: IOAPIC id 2 under DRHD base  0xfed91000 IOMMU 1
[    0.247454] pci 0000:00:02.0: DMAR: Skip IOMMU disabling for graphics
[    0.268622] iommu: Default domain type: Passthrough (set via kernel command line)
[    0.307591] DMAR: IOMMU feature fl1gp_support inconsistent
[    0.307592] DMAR: IOMMU feature pgsel_inv inconsistent
[    0.307592] DMAR: IOMMU feature nwfs inconsistent
[    0.307593] DMAR: IOMMU feature dit inconsistent
[    0.307593] DMAR: IOMMU feature sc_support inconsistent
[    0.307594] DMAR: IOMMU feature dev_iotlb_support inconsistent
[    0.307797] pci 0000:00:02.0: Adding to iommu group 0
[    0.307834] pci 0000:00:00.0: Adding to iommu group 1
[    0.307843] pci 0000:00:06.0: Adding to iommu group 2
[    0.307851] pci 0000:00:06.2: Adding to iommu group 3
[    0.307862] pci 0000:00:0d.0: Adding to iommu group 4
[    0.307875] pci 0000:00:14.0: Adding to iommu group 5
[    0.307881] pci 0000:00:14.2: Adding to iommu group 5
[    0.307891] pci 0000:00:16.0: Adding to iommu group 6
[    0.307899] pci 0000:00:17.0: Adding to iommu group 7
[    0.307910] pci 0000:00:1c.0: Adding to iommu group 8
[    0.307919] pci 0000:00:1c.5: Adding to iommu group 9
[    0.307928] pci 0000:00:1c.6: Adding to iommu group 10
[    0.307941] pci 0000:00:1c.7: Adding to iommu group 11
[    0.307950] pci 0000:00:1d.0: Adding to iommu group 12
[    0.307968] pci 0000:00:1f.0: Adding to iommu group 13
[    0.307976] pci 0000:00:1f.3: Adding to iommu group 13
[    0.307986] pci 0000:00:1f.4: Adding to iommu group 13
[    0.307993] pci 0000:00:1f.5: Adding to iommu group 13
[    0.308002] pci 0000:01:00.0: Adding to iommu group 14
[    0.308010] pci 0000:02:00.0: Adding to iommu group 15
[    0.308023] pci 0000:03:00.0: Adding to iommu group 16
[    0.308032] pci 0000:04:00.0: Adding to iommu group 17
[    0.308041] pci 0000:05:00.0: Adding to iommu group 18
[    0.308051] pci 0000:06:00.0: Adding to iommu group 19
[    0.308065] pci 0000:07:00.0: Adding to iommu group 20

如果有设备分配到独立IOMMU组,说明IOMMU已正确启用。

最后:回到飞牛系统中查看虚拟机硬件设置中查看是否开启成功

常见问题与解决方案

1. 硬件不支持 IOMMU

检查BIOS是否启用 VT-dAMD-Vi

若硬件不支持,无法启用IOMMU。

2. 虚拟机无法启动

确认设备已正确解绑定并绑定到 vfio-pci

确保虚拟机配置的设备地址无误。

3. 显卡直通失败

尝试在 GRUB 参数中增加 iommu=pt

检查宿主机是否卸载了显卡驱动。

通过以上步骤,您可以在飞牛NAS系统中成功启用IOMMU,并实现虚拟机设备直通功能。此功能特别适用于需要高性能计算的场景(如GPU直通给虚拟机运行图形密集型应用)。如果在操作过程中遇到问题,欢迎留言探讨。