14.8. 在 RHEL 的镜像模式中集成第三方驱动程序


为确保驱动程序在更新之间保留,请在容器构建过程中直接包含您的第三方内核模块。这样,网络和安全驱动程序在镜像更新、升级和重启时仍然可用。

要使用默认 RHEL 内核中包含的文件系统、网络或安全模块,您可以使用 RHEL 工作流的镜像模式。与传统的软件包模式不同,其中使用 dnf 在实时系统上安装驱动程序,镜像模式会将文件系统视为不可变。

要包含您的自定义驱动程序,您可以使用多阶段构建。使用 rhel-bootc 镜像作为构建器阶段,安装第三方内核模块并编译您的源代码。这样可保证驱动程序与生成的可引导镜像中的内核兼容。

先决条件

  • 您可以将 .ko 文件嵌套在 RPM 中,以便在容器构建过程中自动使用 depmod 来处理依赖项映射。

流程

  1. 在构建主机上,编译驱动程序并构建 RPM 软件包。

    1. 验证 spec 并构建软件包:

      $ cat SPECS/hello.spec
    2. 构建驱动程序 RPM:

      $ rpmbuild -ba SPECS/hello.spec
  2. 创建 Containerfile 将驱动程序附加到操作系统镜像中。使用 rhel-bootc 基础镜像。以下是一个示例:

    # Copy the pre-built RPM into the build context
    COPY rpms/hello-<version>-<release>.el10.<arch>.rpm /tmp/

    或者,您可以从网络位置获取它。

    # Install the RPM.
    # The %post scripts in the RPM triggers 'depmod' automatically inside the image.
    RUN dnf install -y /tmp/hello-<version>-<release>.el10.<arch>.rpm && \
       	dnf clean all && \
        	rm /tmp/hello-<version>-<release>.el10.<arch>.rpm
  3. 构建并运行容器镜像。请参阅构建容器

验证

  1. 重启系统:

    $ sudo reboot
  2. 确认系统中的内核模块可用:

    $ sudo uname -r
    <kernel_version>
    
    $ sudo ls -l /lib/modules/<kernel_version>/extra/
    total 12
    -rwxr-xr-x. 1 root root 8512 Jan  1  1970 hello.ko
  3. 确保在完成镜像前计算模块依赖项,否则 modprobe 可能会在第一次引导时失败。

    $ sudo depmod -a <kernel-version>
  4. 确认您可以载入该模块:

    $ sudo modprobe /lib/modules/<kernel_version>/extra/hello.ko
    $ sudo lsmod | grep hello
       hello        12288  0
    $ sudo rmmod hello
    $ sudo dmesg
     ..snip..
    [   84.738633] hello: loading out-of-tree module taints kernel.
    [   84.738684] hello: module verification failed: signature and/or required key missing -tainting kernel.
    [   84.740548] Hello, world!
    [  138.206978] Goodbye!
Red Hat logoGithubredditYoutubeTwitter

学习

尝试、购买和销售

社区

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

关于红帽文档

Legal Notice

Theme

© 2026 Red Hat
返回顶部