PVE 配置显卡直通

配置 Device: Dell PowerEdge T630 CPU: Intel(R) Xeon(R) E5-2696 v4 x2 GPU 1: Matrox Electronics Systems Ltd. G200eR2 GPU 2: NVIDIA GeForce GTX 1060 3GB O

配置

Device: Dell PowerEdge T630
CPU: Intel(R) Xeon(R) E5-2696 v4 x2
GPU 1: Matrox Electronics Systems Ltd. G200eR2
GPU 2: NVIDIA GeForce GTX 1060 3GB
OS: Proxmox VE bookworm 8.3.1 x86_64

注意事项

  1. 硬件需支持并在 BIOS 中开启 Intel VT-D 或 AMD AMD-V
  2. BIOS 中开启 X2APIC 功能
  3. 进行任何硬件变更时请依据 手册 进行硬件配置
  4. 为了能够实时了解到 GPU 运行情况,建议连接显示器观察其响应,但如果出现主机无法识别/无法列出 PCIe 设备时请尝试拔掉显示器后重启
  5. 虚拟机存在直通设备时,建议关闭内存的 Ballooning 功能,来确保虚拟机内存不共享

PVE 直通显卡

开启 IOMMU 和硬件直通功能

编辑 /etc/default/grub,并修改 GRUB_CMDLINE_LINUX_DEFAULT 根据硬件平台添加如下选项

# Intel CPU
GRUB_CMDLINE_LINUX_DEFAULT="quiet intel_iommu=on iommu=pt initcall_blacklist=sysfb_init pcie_acs_override=downstream"

# AMD CPU
GRUB_CMDLINE_LINUX_DEFAULT="quiet amd_iommu=on iommu=pt initcall_blacklist=sysfb_init pcie_acs_override=downstream"

intel_iommu=on amd_iommu=on 用于开启 IOMMU 硬件单元驱动

iommu=pt 用于跳过地址映射来提高内核驱动设备性能

initcall_blacklist=sysfb_init 用于禁用 simplefb 设备加载
PVE 7.3 中使用 simplefb 替代了旧的 vesafbefifb
所以旧版中,Legacy BIOS 引导使用 video=vesafb:offUEFI 使用 video=efifb:off

pcie_acs_override=downstream 用于拆分 iommu groups,防止分组错误时直通一个板载端口导致整个设备被直通

更新 GRUB

PVE 官方手册中给出了 proxmox-boot-tool refresh 命令用于更新 GRUB,但有可能无法正常更新,此时请根据情况尝试 update-grub

加载内核模块

编辑 /etc/modules,添加如下内容

vfio
vfio_iommu_type1
vfio_pci

Linux Kernel 6.2 版本中取消了 vfio_virqfd,低于该版本需额外添加

重启 PVE

验证 IOMMU

dmesg | grep -e DMAR -e IOMMU,输出类似内容

[    0.000000] Warning: PCIe ACS overrides enabled; This may allow non-IOMMU protected peer-to-peer DMA
[    0.008734] ACPI: DMAR 0x000000007BAFE000 0000E8 (v01 DELL   PE_SC3   00000001 DELL 00000001)
[    0.008767] ACPI: Reserving DMAR table memory at [mem 0x7bafe000-0x7bafe0e7]
[    0.349828] DMAR: IOMMU enabled
[    0.822346] DMAR: Host address width 46
[    0.822348] DMAR: DRHD base: 0x000000fbffc000 flags: 0x0
[    0.822357] DMAR: dmar0: reg_base_addr fbffc000 ver 1:0 cap 8d2078c106f0466 ecap f020df
[    0.822360] DMAR: DRHD base: 0x000000c7ffc000 flags: 0x1
[    0.822365] DMAR: dmar1: reg_base_addr c7ffc000 ver 1:0 cap 8d2078c106f0466 ecap f020df
[    0.822367] DMAR: ATSR flags: 0x0
[    0.822370] DMAR: ATSR flags: 0x0
[    0.822372] DMAR-IR: IOAPIC id 10 under DRHD base  0xfbffc000 IOMMU 0
[    0.822374] DMAR-IR: IOAPIC id 8 under DRHD base  0xc7ffc000 IOMMU 1
[    0.822375] DMAR-IR: IOAPIC id 9 under DRHD base  0xc7ffc000 IOMMU 1
[    0.822376] DMAR-IR: HPET id 0 under DRHD base 0xc7ffc000
[    0.822378] DMAR-IR: Queued invalidation will be enabled to support x2apic and Intr-remapping.
[    0.822688] DMAR-IR: IRQ remapping was enabled on dmar0 but we are not in kdump mode
[    0.822995] DMAR-IR: IRQ remapping was enabled on dmar1 but we are not in kdump mode
[    0.823121] DMAR-IR: Enabled IRQ remapping in x2apic mode
[    1.331693] DMAR: No RMRR found
[    1.331696] DMAR: No SATC found
[    1.331698] DMAR: dmar0: Using Queued invalidation
[    1.331701] DMAR: dmar1: Using Queued invalidation
[    1.347932] DMAR: Intel(R) Virtualization Technology for Directed I/O

验证 IOMMU 中断重映射

dmesg | grep remapping,输出类似内容

# Intel
[    0.822378] DMAR-IR: Queued invalidation will be enabled to support x2apic and Intr-remapping.
[    0.823121] DMAR-IR: Enabled IRQ remapping in x2apic mode
# AMD
AMD-Vi: Interrupt remapping enabled

此时可以直通除显卡外的 PCIe 设备

如果没有开启,请验证 BIOS 中是否启用 X2APIC 功能

开启 X2APIC 后仍无法启用中断重映射,尝试在 /etc/modprobe.d/iommu_unsafe_interrupts.conf 中添加以下内容并重启

options vfio_iommu_type1 allow_unsafe_interrupts=1

vfio_iommu_type1 启用该模块来实现设备隔离、DMA 映射及中断处理

allow_unsafe_interrupts 允许不安全的 IOMMU 中断重映射

显卡直通

屏蔽驱动

编辑 /etc/modprobe.d/pve-blacklist.conf 添加以下内容用于屏蔽对应 GPU 平台驱动,防止主机驱动 GPU

# Nvidia
blacklist nvidiafb
blacklist nouveau
blacklist nvidia

# AMD
blacklist amdgpu
blacklist radeon

# Intel UHD
blacklist snd_hda_codec_hdmi
blacklist snd_hda_intel
blacklist snd_hda_codec
blacklist snd_hda_core

忽略显卡警告(NVIDIA)

NVIDIA GPU 在 Windows 中可能会在使用类似 GeForce Experience Passmark Performance Test SiSoftware Sandra 应用时使出警告

编辑 /etc/modprobe.d/kvm.conf,添加如下内容

options kvm ignore_msrs=1 report_ignored_msrs=0

ignore_msrs=1 忽略异常

report_ignore_msrs=0 禁止报告异常

配置 VFIO,分离显示和音频输出

输入 lspci -nn | grep VGA 得到如下输出

09:00.0 VGA compatible controller [0300]: Matrox Electronics Systems Ltd. G200eR2 [102b:0534] (rev 01)
83:00.0 VGA compatible controller [0300]: NVIDIA Corporation GP104 [GeForce GTX 1060 3GB] [10de:1b84] (rev a1)

显然 83:00.0 为 GPU 设备,执行 lspci -n -s 83:00,得到如下输出

83:00.0 0300: 10de:1b84 (rev a1)
83:00.1 0403: 10de:10f0 (rev a1)

根据以上两次输出对比可以得到设备 ID 为 10de:1b84,则板载音频设备 ID 为 10de:10f0

将两个 ID 写入 /etc/modprobe.d/vfio.conf

options vfio-pci ids=10de:1b84,10de:10f0

vfio-pci 用于将指定设备绑定到 VFIO 框架,由 vfio-pci 驱动,以便用户态进程可以直接访问这些设备

更新内核

update-initramfs -u

重启

验证 vfio-pci

执行 lspci -nnk 找到显卡设备,应为类似如下内容

83:00.0 VGA compatible controller [0300]: NVIDIA Corporation GP104 [GeForce GTX 1060 3GB] [10de:1b84] (rev a1)
    Subsystem: GALAX GP104 [GeForce GTX 1060 3GB] [1b4c:11de]
    Kernel driver in use: vfio-pci
    Kernel modules: nvidiafb, nouveau
83:00.1 Audio device [0403]: NVIDIA Corporation GP104 High Definition Audio Controller [10de:10f0] (rev a1)
    Subsystem: GALAX GP104 High Definition Audio Controller [1b4c:11de]
    Kernel driver in use: vfio-pci
    Kernel modules: snd_hda_intel

其中 Kernel driver in use 值为 vfio-pci 即可

添加显卡到虚拟机

登录 PVE WebUI,进入虚拟机 Hardware 配置,Add => PCI Device

选择 Raw Device,在 Device 中选择 GPU,开启 All Functions ROM-Bar PCI-Express

点击 Add

解决 Linux 虚拟机中驱动签名验证问题

如果在 Linux 中使用 nvidia-smi 提示以下内容

NVIDIA-SMI has failed because it couldn't communicate with the NVIDIA driver. Make sure that the latest NVIDIA driver is installed and running.

并且 systemd-modules-load nvidia-persistenced.service 无法启动,执行 systemctl status systemd-modules-load.service journalctl -xeu nvidia-persistenced.service 提示以下内容

# systemd-modules-load.service
modprobe: ERROR: could not insert 'nvidia': Invalid argument
modprobe: ERROR: could not insert 'nvidia_current_drm': Key was rejected by service
modprobe: ERROR: ../libkmod/libkmod-module.c:1047 command_do() Error running install command 'modprobe nvidia ; modprobe -i nvidia-current-modeset ' for module nvidia_modeset: retcode 1
modprobe: ERROR: could not insert 'nvidia_modeset': Invalid argument
modprobe: ERROR: could not insert 'nvidia_current_drm': Key was rejected by service
Error running install command 'modprobe nvidia-modeset ; modprobe -i nvidia-current-drm ' for module nvidia_drm: retcode 1
Failed to insert module 'nvidia_drm': Invalid argument

# nvidia-persistenced.service
Started (737)
Failed to query NVIDIA devices. Please ensure that the NVIDIA device files (/dev/nvidia*) exist, and that user 101 has read and write permissions for those files.
Shutdown (737)
nvidia-persistenced failed to initialize. Check syslog for more details.
nvidia-persistenced.service: Control process exited, code=exited, status=1/FAILURE

该问题大概率由 驱动签名无法验证 导致,此时请尝试在 虚拟机 BIOS 中关闭安全启动

如果出于特殊需求或环境导致无法关闭,需注册 DKMS 密钥,注册流程比较复杂且涉及 EFI 分区改动,本文不作详解

LICENSED UNDER CC BY-NC-SA 4.0
Comment