脚本实现

f=DirectConnection.py;rm -f $f;wget  https://gitee.com/liumou_site/pvet/raw/master/model/eth/$f;python3 $f

网卡直通不适用于单网卡环境

手动实现

lscpu | grep -i virtualization

执行检查CPU是否支持虚拟化,如果有输出说明支持,如下

root@jmk:~# lscpu | grep -i virtualization
Virtualization:                     AMD-V
root@jmk:~# 

如果没有输出,则需要先开启该功能

开启intel_iommu

执行下面的命令

sed -i "s@GRUB_CMDLINE_LINUX_DEFAULT=.*@GRUB_CMDLINE_LINUX_DEFAULT=\"quiet intel_iommu=on\"@g" /etc/default/grub

然后检查

grep GRUB_CMDLINE_LINUX_DEFAULT /etc/default/grub

如果输出和我的一样就行

root@tb3:~# grep GRUB_CMDLINE_LINUX_DEFAULT /etc/default/grub
GRUB_CMDLINE_LINUX_DEFAULT="quiet intel_iommu=on"
root@tb3:~# 

更新grub

update-grub
root@tb3:~# update-grub
Generating grub configuration file ...
Found linux image: /boot/vmlinuz-6.2.16-3-pve
Found initrd image: /boot/initrd.img-6.2.16-3-pve
Found memtest86+ 64bit EFI image: /boot/memtest86+x64.efi
Adding boot menu entry for UEFI Firmware Settings ...
done
root@tb3:~# 

加载模块

echo -e "vfio\nvfio_iommu_type1\nvfio_pci\nvfio_virqfd" > /etc/modules

结束之后,执行重启命令

reboot

检查

重启完成之后,执行下面的命令

dmesg | grep -e DMAR -e IOMMU
root@tb3:~# dmesg | grep -e DMAR -e IOMMU
[    0.008985] ACPI: DMAR 0x000000007548D000 000088 (v02 INTEL  EDK2     00000002      01000013)
[    0.009020] ACPI: Reserving DMAR table memory at [mem 0x7548d000-0x7548d087]
[    0.036336] DMAR: IOMMU enabled
[    0.088899] DMAR: Host address width 39
[    0.088899] DMAR: DRHD base: 0x000000fed90000 flags: 0x0
[    0.088905] DMAR: dmar0: reg_base_addr fed90000 ver 4:0 cap 1c0000c40660462 ecap 29a00f0505e
[    0.088907] DMAR: DRHD base: 0x000000fed91000 flags: 0x1
[    0.088911] DMAR: dmar1: reg_base_addr fed91000 ver 5:0 cap d2008c40660462 ecap f050da
[    0.088913] DMAR: RMRR base: 0x0000007c000000 end: 0x000000803fffff
[    0.088916] DMAR-IR: IOAPIC id 2 under DRHD base  0xfed91000 IOMMU 1
[    0.088917] DMAR-IR: HPET id 0 under DRHD base 0xfed91000
[    0.088918] DMAR-IR: Queued invalidation will be enabled to support x2apic and Intr-remapping.
[    0.090598] DMAR-IR: Enabled IRQ remapping in x2apic mode
[    0.346660] pci 0000:00:02.0: DMAR: Skip IOMMU disabling for graphics
[    0.452450] DMAR: No ATSR found
[    0.452451] DMAR: No SATC found
[    0.452452] DMAR: IOMMU feature fl1gp_support inconsistent
[    0.452453] DMAR: IOMMU feature pgsel_inv inconsistent
[    0.452454] DMAR: IOMMU feature nwfs inconsistent
[    0.452454] DMAR: IOMMU feature dit inconsistent
[    0.452455] DMAR: IOMMU feature sc_support inconsistent
[    0.452456] DMAR: IOMMU feature dev_iotlb_support inconsistent
[    0.452457] DMAR: dmar0: Using Queued invalidation
[    0.452459] DMAR: dmar1: Using Queued invalidation
[    0.454391] DMAR: Intel(R) Virtualization Technology for Directed I/O
root@tb3:~# 

有输出说明成功了

更新initramfs

update-initramfs -u -k all
root@tb3:~# update-initramfs -u -k all
update-initramfs: Generating /boot/initrd.img-6.2.16-3-pve
Running hook script 'zz-proxmox-boot'..
Re-executing '/etc/kernel/postinst.d/zz-proxmox-boot' in new private mount namespace..
No /etc/kernel/proxmox-boot-uuids found, skipping ESP sync.
root@tb3:~# 

注意事项

不能设置虚拟网卡绑定的设备

root@jmk:~# cat /etc/network/interfaces
auto lo
iface lo inet loopback

iface enp1s0 inet manual

auto vmbr0
iface vmbr0 inet static
        address 172.20.3.100/24
        gateway 172.20.3.1
        bridge-ports enp1s0
        bridge-stp off
        bridge-fd 0

iface enp2s0 inet manual

iface wlp3s0 inet manual

bridge-ports enp1s0这里出现的网卡名称不能用于直通,否则无法访问后台

获取PCIE ID

执行下面的命令查询

root@jmk:~# dmesg | grep -i enp2s0
[    2.356636] r8169 0000:02:00.0 enp2s0: renamed from eth1
root@jmk:~# 

请将enp2s0改成你的网卡名称,例如: eth0

root@jmk:~# dmesg | grep -i enp2s0
[    2.356636] r8169 0000:02:00.0 enp2s0: renamed from eth1
root@jmk:~# 

其中的0000:02:00.0就是属于enp2s0网卡设备的ID

添加直通

image-1712448550305

点击添加pci设备,然后找到id一致的那个设备即可