在bhyve中实现Intel GPU passthrough

备注

bhyve 对 Intel GPU passthrough 支持非常好,简单配置就能够实现Intel GPU直通给虚拟机使用,方便构建机器学习环境。远比 在bhyve中实现NVIDIA GPU passthrough 方便很多,可见Intel对开源的支持力度。

我在 在bhyve中实现NVIDIA GPU passthrough 实践中了解到对于 NVIDIA GPU 是需要对 bhyve 打补丁的,不过根据网上的资料,intel GPU似乎是原生支持,没有这么复杂。

当然,我没有独立的Intel显卡,但是我的组装电脑 纳斯NASSE C246 ITX主板 配置的 Intel Xeon E-2274G ,微处理器是 Cofee Lake (也就是俗称的第八代),内置了 Intel UHD Graphics 630 。虽然图形性能非常弱,用于 Machine Learning 也非常拉胯。但是,只要能验证方案可行也是有价值的。

根据intel OpenVINO System Requirements 资料,是支持 6th - 14th generation Intel® Core™ processors 以及 Intel® UHD Graphics

我的构想:

安装 vm-bhyve

备注

使用 vm-bhyve 作为管理器来完成部署

  • 安装 vm-bhyve

安装 vm-bhyve
pkg install vm-bhyve bhyve-firmware
  • 创建 vm-bhyve 使用的存储:

创建虚拟机存储数据集
zfs create zdata/vms

#zfs set recordsize=64K zdata/vms

zfs create zdata/vms/.config
zfs create zdata/vms/.img
zfs create zdata/vms/.iso
zfs create zdata/vms/.templates
  • /etc/rc.conf 中设置虚拟化支持:

配置 /etc/rc.conf 支持虚拟化
# needed for virtualization support
vm_enable="YES"
vm_dir="zfs:zdata/vms"
  • /boot/loader.conf 添加:

/boot/loader.conf
# needed for virtualization support 
vmm_load="YES"
  • 初始化:

初始化
vm init

PCI passthru

  • 检查 PCI 设备:

检查可以passthrough的设备
 vm passthru
  • 输出显示:

输出显示 Intel UHD Graphics P630
DEVICE     BHYVE ID     READY        DESCRIPTION
hostb0     0/0/0        No           8th Gen Core 4-core Workstation Processor Host Bridge/DRAM Registers [Coffee Lake S]
...
vgapci0    0/2/0        No           CoffeeLake-S GT2 [UHD Graphics P630]
...
  • 配置 /boot/loader.conf 屏蔽掉需要passthru的Intel UHD Graphics P630 0/2/0 :

屏蔽掉 Intel UHD Graphics P630 0/2/0 (另一个 1/0/0Nvidia Tesla P10 GPU运算卡 )
pptdevs="0/2/0 1/0/0"
  • 重启系统屏蔽Host主机使用 Intel UHD Graphics P630

配置

  • 配置 /zdata/vms/.templates/x-vm.conf (修订使用 0/2/0 作为 passthru ,以及使用 tap1 作为网络接口, VNC使用5901端口):

模版配置 /zdata/vms/.templates/x-vm.conf
loader="uefi"
cpu=4
memory=16G
wired_memory="yes"
network0_type="virtio-net"
network0_switch="igc0bridge"
network0_device="tap0"
disk0_name="disk0"
disk0_dev="sparse-zvol"
disk0_type="virtio-blk"
disk0_size="50G"
passthru0="0/2/0"
graphics="yes"
graphics_listen="0.0.0.0"
graphics_port="5901"
  • 创建虚拟机 idev :

创建虚拟机 idev
vm create -t x-vm -s 60G idev
  • 安装ubuntu:

安装虚拟机
vm install idev ubuntu-24.04.2-live-server-amd64.iso

非常顺利没有出现 在bhyve中实现NVIDIA GPU passthrough 一旦passthru NVIDIA GPU就卡住bhyve虚拟机启动的问题。

重启安装好的 idev 虚拟机,进入虚拟机内部检查 lspci 输出可以看到 Intel UHD Graphics P630 GPU正确passthru给了虚拟机使用:

虚拟机内部 lspci 检查输出
...
00:06.0 VGA compatible controller: Intel Corporation CoffeeLake-S GT2 [UHD Graphics P630]
00:07.0 VGA compatible controller: Device fb5d:40fb
...

安装