准备工作

开启IOMMU和硬件直通功能

输入以下指令

nano /etc/default/grub

修改行GRUB_CMDLINE_LINUX_DEFAULT="quiet"

使其为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"

更新grud

proxmox-boot-tool refresh

出现以下输出代表更新成功

root@pve:~# proxmox-boot-tool refresh
Running hook script 'proxmox-auto-removal'..
Running hook script 'zz-proxmox-boot'..
Re-executing '/etc/kernel/postinst.d/zz-proxmox-boot' in new private mount namespace..
No /etc/kernel/cmdline found - falling back to /proc/cmdline

新增模块

nano /etc/modules

添加以下字段

vfio
vfio_iommu_type1
vfio_pci
vfio_virqfd

重启系统

reboot

验证是否成功启用IOMMU

输入 dmesg | grep -e DMAR -e IOMMU,并有以下类似内容

root@pve:~# dmesg | grep -e DMAR -e IOMMU
[    0.011412] ACPI: DMAR 0x0000000076D145C0 0000DC (v01 ALASKA A M I    00000001 INTL 20091013)
[    0.011442] ACPI: Reserving DMAR table memory at [mem 0x76d145c0-0x76d1469b]
[    0.143932] DMAR: IOMMU enabled
[    0.395450] DMAR: Host address width 46
[    0.395452] DMAR: DRHD base: 0x000000fbffd000 flags: 0x0
[    0.395463] DMAR: dmar0: reg_base_addr fbffd000 ver 1:0 cap d2008c10ef0466 ecap f0205b
[    0.395466] DMAR: DRHD base: 0x000000fbffc000 flags: 0x1
[    0.395471] DMAR: dmar1: reg_base_addr fbffc000 ver 1:0 cap d2078c106f0466 ecap f020df
[    0.395474] DMAR: RMRR base: 0x000000773e0000 end: 0x000000773f0fff
[    0.395476] DMAR: ATSR flags: 0x0
[    0.395478] DMAR: RHSA base: 0x000000fbffc000 proximity domain: 0x0
[    0.395481] DMAR-IR: IOAPIC id 1 under DRHD base  0xfbffc000 IOMMU 1
[    0.395483] DMAR-IR: IOAPIC id 2 under DRHD base  0xfbffc000 IOMMU 1
[    0.395484] DMAR-IR: HPET id 0 under DRHD base 0xfbffc000
[    0.395486] DMAR-IR: x2apic is disabled because BIOS sets x2apic opt out bit.
[    0.395487] DMAR-IR: Use 'intremap=no_x2apic_optout' to override the BIOS setting.
[    0.396157] DMAR-IR: Enabled IRQ remapping in xapic mode
[   11.580820] DMAR: No SATC found
[   11.580822] DMAR: IOMMU feature sc_support inconsistent
[   11.580824] DMAR: IOMMU feature dev_iotlb_support inconsistent
[   11.580825] DMAR: dmar0: Using Queued invalidation
[   11.580833] DMAR: dmar1: Using Queued invalidation
[   11.585131] DMAR: Intel(R) Virtualization Technology for Directed I/O

验证是否 启用IOMMU中断重映射

输入 dmesg | grep remapping 并输出 类似 以下内容即为成功。

到目前为止可以 直通 除显卡外PCI-E设备

# intel
[    0.175675] DMAR-IR: Queued invalidation will be enabled to support x2apic and Intr-remapping.
[    0.177198] DMAR-IR: Enabled IRQ remapping in x2apic mode
# amd
AMD-Vi: Interrupt remapping enabled

允许不安全IOMMU中断重映射

上面“验证是否 启用IOMMU中断重映射”这部通过的话不用运行这一步

编辑 /etc/modprobe.d/iommu_unsafe_interrupts.conf ,添加以下内容。

options vfio_iommu_type1 allow_unsafe_interrupts=1

显卡直通

屏蔽驱动

编辑 /etc/modprobe.d/pve-blacklist.conf ,添加以下内容

# 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

配置VFIO

主要用于 显示输出音频输出 分开

查看显卡ID
输入lspci -nn | grep VGA ,得到类似以下内容

root@pve:~# lspci -nn | grep VGA
03:00.0 VGA compatible controller [0300]: Advanced Micro Devices, Inc. [AMD/ATI] Ellesmere [Radeon RX 470/480/570/570X/580/580X/590] [1002:67df] (rev e7)

获取 设备ID音频ID ,注意我这 显卡编号03:00.0

输入lspci -n -s 03:00,输出以下结果

root@pve:~# lspci -n -s 03:00
03:00.0 0300: 1002:67df (rev e7)
03:00.1 0403: 1002:aaf0

设备ID1002:67df) 和 音频ID1002:aaf0) 写入到 /etc/modprobe.d/vfio.conf

options vfio-pci ids=1002:67df,1002:aaf0

更新内核

update-initramfs -u

重启系统

检查vfio-pci 是否应用成功

输入 lspci -nnk 并找到 显卡编号 那一行,如果得到类似以下结果,为成功

root@pve:~# lspci -nnk
03:00.0 VGA compatible controller [0300]: Advanced Micro Devices, Inc. [AMD/ATI] Ellesmere [Radeon RX 470/480/570/570X/580/580X/590] [1002:67df] (rev e7)
        Subsystem: Advanced Micro Devices, Inc. [AMD/ATI] Ellesmere [Radeon RX 470/480/570/570X/580/580X/590] [1002:0b31]
        Kernel driver in use: vfio-pci
        Kernel modules: amdgpu
03:00.1 Audio device [0403]: Advanced Micro Devices, Inc. [AMD/ATI] Ellesmere HDMI Audio [Radeon RX 470/480 / 570/580/590] [1002:aaf0]
        Subsystem: Advanced Micro Devices, Inc. [AMD/ATI] Ellesmere HDMI Audio [Radeon RX 470/480 / 570/580/590] [1002:aaf0]
        Kernel driver in use: vfio-pci
        Kernel modules: snd_hda_intel

结果中有 Kernel driver in use: vfio-pci 即为成功

添加显卡到虚拟机

显卡 选择 即可

选择 虚拟机 , 并点击 硬件

点击 添加 , 然后点击 PCI设备

选择 原始设备 ,在 设备 中选择需要直通的显卡

启用 所有功能主GPUROM-BarPCI-Express

点击添加

然后输入以下指令添加配置nano /etc/pve/qemu-server/100.conf100这个是你虚拟机的编号

按需修改

hostpci0: 0000:03:00,pcie=1,x-vga=on

这里就完成了,启动虚拟机就好了!

Enjoy!

Last modification:September 9th, 2024 at 12:37 pm
如果觉得我的文章对你有用,请随意赞赏