如何使用 snapd 的 lxd/lxc 容器在 centos/rhel/ol8 上启用 gpu passthrough?

问题描述

我在 CentOS 上部署 LXC 的指南是安装 snapd 的 lxd https://www.cyberciti.biz/faq/set-up-use-lxd-on-centos-rhel-8-x/

SnapD 是一种服务,它允许安装基于 debian/ubuntu 的软件包,逻辑是 lxd 在该平台上是最新的。

嗯。如果启用 gpu 直通更容易,我愿意安装替代版本。

最终,我正在尝试构建一个容器环境,我可以在其中运行具有 gpu 支持的最新版本的 python 和 jupyter。

我有一些关于如何启用 GPU 直通的指南。

https://theorangeone.net/posts/lxc-nvidia-gpu-passthrough/
https://www.reddit.com/r/Proxmox/comments/glog5j/lxc_gpu_passthrough/

我在我的 ol8 主机上添加了以下内核模块

/etc/modules-load.d/vfio-pci.conf
    # Nvidia modules
    nvidia
    nvidia_uvm

#noticed snapd has a modules file I can't edit  

/var/lib/snapd/snap/core18/1988/etc/modules-load.d/modules.conf
            

然后修改grub

nano /etc/default/grub 
    #https://access.redhat.com/documentation/en-us/red_hat_virtualization/4.1/html/installation_guide/appe-configuring_a_hypervisor_host_for_pci_passthrough
    GRUB_CMDLINE_LINUX
    #iommu=on amd_iommu=on
    iommu=pt amd_iommu=pt
            
grub2-mkconfig -o /boot/grub2/grub.cfg

然后添加udev规则

    nano /etc/udev/rules.d/70-nvidia.rules
    KERNEL=="nvidia",RUN+="/bin/bash -c '/usr/bin/nvidia-smi -L && /bin/chmod 666 /dev/nvidia*'"
    KERNEL=="nvidia_uvm",RUN+="/bin/bash -c '/usr/bin/nvidia-modprobe -c0 -u && /bin/chmod 0666 /dev/nvidia-uvm*'"

#reboot

然后将 gpu 添加到 lxc.conf

ls -l /dev/nvidia*

# Allow cgroup access
lxc.cgroup.devices.allow: c 195:* rwm
lxc.cgroup.devices.allow: c 243:* rwm

nano /var/snap/lxd/common/lxd/logs/nvidia-test/lxc.conf
        

# Pass through device files
lxc.mount.entry: /dev/nvidia0 dev/nvidia0 none bind,optional,create=file
lxc.mount.entry: /dev/nvidiactl dev/nvidiactl none bind,create=file
lxc.mount.entry: /dev/nvidia-uvm dev/nvidia-uvm none bind,create=file
lxc.mount.entry: /dev/nvidia-modeset dev/nvidia-modeset none ind,create=file
lxc.mount.entry: /dev/nvidia-uvm-tools dev/nvidia-uvm-tools none bind,create=file

在 lxc 容器内我开始(ol8)

#installed nvidia-driver that comes with nvidia-smi
    nvidia-driver-cuda-3:460.32.03-1.el8.x86_64
    
#installed cuda
    cuda-11-2-11.2.2-1.x86_64

当我去运行 nvidia-smi

[root@nvidia-test ~]# 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.

因为我无法编辑 snapd 模块文件,我想手动复制 nvidia 内核模块文件并将它们插入(使用 modprobe --show-depends 确定)

[root@nvidia-test ~]# insmod nvidia.ko.xz NVreg_DynamicPowerManagement=0x02
insmod: ERROR: could not insert module nvidia.ko.xz: Function not implemented

我的容器内的一些诊断信息

[root@nvidia-test ~]# find /sys | grep dmar
find: '/sys/kernel/debug': Permission denied
find: '/sys/fs/pstore': Permission denied
find: '/sys/fs/fuse/connections/59': Permission denied
[root@nvidia-test ~]# lspci | grep -i nvidia
05:00.0 VGA compatible controller: NVIDIA Corporation GP107GL [Quadro P1000] (rev a1)
05:00.1 Audio device: NVIDIA Corporation GP107GL High Definition Audio Controller (rev a1)

那么……我还有什么应该做的吗?我应该删除 snapd lxd 并使用 OL8 提供的默认 lxc 吗?

解决方法

您可以通过创建 LXD gpu 设备使用 GPU Passthrough 到 LXD 容器。此 gpu 设备将共同完成所有必要任务,将 GPU 公开给容器,包括您在上面明确进行的配置。

这里是包含所有额外参数的文档(例如,如果有多个GPU,您如何区分), https://linuxcontainers.org/lxd/docs/master/instances#type-gpu

在最简单的形式中,您可以对现有容器运行以下命令以添加默认 GPU(到容器)。

lxc config device add mycontainer mynvidia gpu

当您在 NVidia 容器中添加 GPU 时,您还需要将相应的 NVidia 运行时添加到容器中(使其与主机上的内核版本匹配!)。在容器中,我们不需要(也不能)添加内核驱动程序,但我们需要添加运行时(库、实用程序和其他软件)。 LXD 负责处理此问题,并为您下载相应版本的 NVidia 容器运行时,并将其附加到容器中。这是一个完整的示例,它在启用 NVidia 运行时创建容器,然后将 NVidia GPU 设备添加到该容器。

$ lxc launch ubuntu: mycontainer -c nvidia.runtime=true -c nvidia.driver.capabilities=all
Creating mycontainer
Starting mycontainer
$ lxc config device add mycontainer mynvidia gpu
Device mynvidia added to mycontainer
$ lxc shell mycontainer
root@mycontainer:~# nvidia-smi 
Mon Mar 15 13:37:24 2021       
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 450.102.04   Driver Version: 450.102.04   CUDA Version: 11.0     |
|-------------------------------+----------------------+----------------------+
...
$ 

如果您经常创建此类 GPU 容器,则可以使用 GPU 配置创建 LXD 配置文件。然后,如果您想要一个 GPU 容器,您可以使用 nvidia 配置文件启动容器,或者您可以应用 nvidia 配置文件到现有容器,从而使它们成为 GPU容器!

$ cat mynvidiaLXDprofile.txt
config:
  nvidia.driver.capabilities: all
  nvidia.runtime: "true"
description: ""
devices:
  mygpu:
    type: gpu
name: nvidia
used_by: []
$ lxc profile create nvidia
Profile nvidia created
$ lxc profile edit nvidia < mynvidiaLXDprofile.txt
$ lxc launch ubuntu:20.04 mycontainer --profile default --profile nvidia
Creating mycontainer
Starting mycontainer
$ 

我们一直在使用 LXD 的 snap 包进行上述所有说明。

,

找到答案

#https://ubuntu.com/blog/nvidia-cuda-inside-a-lxd-container

lxc config device add cuda gpu gpu

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...