如何在 Linux 上使用 Dracut 构建 initramfs
在上一篇文章中,我们讨论了使用标准、简单的工具(如 gzip、dd 和 cpio)或使用专用脚本(如 lsinitramfs、lsinitrd 和 unmkinitramfs)来侦听和提取 initramfs 映像的内容。在本教程中,我们学习如何使用 dracut 在 Linux 上(重新)构建 initramfs。
在本教程中您将学习:
什么是 dracut
如何列出 dracut 模块
如何使用 dracut 构建 initramfs
如何为特定内核版本构建 initramfs
如何为所有内核构建 initramfs
如何构建主机定制的 initramfs
如何列出 initram 中包含的文件并获取其内容
如何在 initramfs 中包含额外的文件
如何更改initramfs压缩方式
如何使用dracut配置文件
使用的软件要求和约定
介绍德拉克特
Dracut 是一个用于构建 initramfs cpio 档案的工具。它起源于并主要用于 Fedora 和 Red Hat 系列的其他发行版,但也可以用于 Gentoo 和 Archlinux 等社区发行版。应用程序功能按模块组织。为了列出系统上可用的所有 dracut 模块,我们只需使用 --list-modules
选项调用应用程序,该选项将返回类似于以下内容的输出:
$ dracut --list-modules
bash
systemd
systemd-network-management
warpclock
fips
systemd-ac-power
systemd-ask-password
systemd-coredump
systemd-hostnamed
systemd-initrd
systemd-journald
systemd-ldconfig
systemd-modules-load
[...]
所有 dracut 模块都位于 /usr/lib/dracut/modules.d
目录中。在此目录中,所有模块都表示为子目录,并包含一系列脚本。每个模块都提供特定的功能。例如,plymouth
模块 (/usr/lib/dracut/modules.d/50plymouth
) 提供对启动动画的支持(它需要“plymouth”包才能启动)进行安装)。
Dracut 基本用法
在最基本的用法中,我们可以在没有任何选项或参数的情况下调用 dracut。当以这种方式使用时,程序会尝试使用以下名称模式为系统当前正在使用的内核生成 initramfs:
/boot/initramfs-<kernel-version>
通过使用带有 -r
选项(--kernel-release
的缩写)的 uname
实用程序,可以轻松获取内核版本。因此,我们可以猜测运行以下命令的 initramfs 将使用的名称:
$ echo "/boot/initramfs-$(uname -r)"
如果与 dracut 生成的 initramfs 名称相同的 initramfs 已存在,则应用程序将拒绝覆盖它,并显示类似于以下内容的错误:
dracut: Will not override existing initramfs (/boot/initramfs-5.14.14-300.fc35.x86_64.img) without --force
正如消息本身所建议的,为了覆盖现有的 initramfs,我们必须使用 --force
选项运行 dracut。
为特定内核版本构建 initramfs
为特定内核版本构建 initramfs 的最简单方法是使用 --kver
选项调用 dracut,并提供应该构建 initramfs 的内核版本作为参数:
$ sudo dracut --kver 5.14.14-300.fc35.x86_64
initramfs 将使用我们上面看到的命名模式构建在 /boot
目录中。如果我们想在自定义位置中为特定内核版本构建 initramfs,我们可以使用应创建映像的路径作为 first 参数来调用 dracut,并传递内核版本为第二个。举个例子,要在当前工作目录中显式生成以当前运行的内核命名的 initramfs 映像,我们将运行:
$ sudo dracut . 5.14.14-300.fc35.x86_64
上面的命令将在启动目录中生成一个 initramfs,名为 initramfs.img
。
为所有现有内核构建 initramfs
有时我们可能想要为操作系统上的所有现有内核构建或重新构建 initramfs。 Dracut 提供了一种非常简单的方法来完成此任务。我们所要做的就是运行应用程序并使用 --regenerate-all
选项。 initramfs 存档将在默认目录中创建。如前所述,如果特定内核的 initramfs 已经存在,我们还需要传递 --force
选项:
$ sudo dracut --regenerate-all --force
创建主机定制的 initramfs
通常,当我们使用 dracut 生成 initramfs 时,会创建通用主机配置。 initramfs 内部包含了启动通用机器所需的所有内容,以确保最大可能的兼容性。如果我们只想将特定机器实际需要的内容放入 initramfs 中,则可以使用 -H
选项(--hostonly
的缩写)运行 dracut。要为当前运行的内核重建主机定制的 initramfs,我们将执行以下简单命令:
$ sudo dracut -H --force
列出 initram 中包含的文件并获取其内容
在之前的教程中,我们了解了如何在 Linux 上检查 initramfs 的内容。简而言之,在 Fedora 上以及通常在 Red Hat 系列的发行版上执行操作并使用 dracut 的推荐方法是使用 lsinitrd 脚本,传递以下路径:我们想要将 initramfs 作为参数进行检查。例如,要检查 /boot/initramfs-5.14.14-300.fc35.x86_64.img initramfs 的内容,我们将运行:
$ sudo lsinitrd /boot/initramfs-5.14.14-300.fc35.x86_64.img
上面的命令除其他外,还生成 initramfs 中包含的文件列表。要检查文件的内容,我们可以使用 lsinitrd 的 -f 选项并将 initramfs 内的文件路径作为参数传递。举个例子,要读取 initramfs 中包含的 etc/crypttab
文件的内容,我们将运行:
$ sudo lsinitrd /boot/initramfs-5.14.14-300.fc35.x86_64.img -f etc/crypttab
使用 –include 选项在 initramfs 中包含额外文件
有时我们可能想在 initramfs 中包含额外的文件。基本上我们可以使用两个选项来执行此操作:--include
和--install
;让我们看看它们是如何工作的。
使用 –include 包含文件
--include
选项采用两个参数,按顺序:
要包含在 initramfs 中的文件的路径(来源)
文件在 initramfs 中应具有的路径(目标)
假设我们要为当前运行的内核重建 initramfs,并且希望将 /custom-content.conf
文件作为 /etc/custom-content.conf
包含在其中它的。我们会运行:
$ sudo dracut --include /custom-content.conf /etc/custom-content.conf --force
使用 --include
选项我们还可以将目录的内容包含在 initramfs 中。假设我们有 /foo
目录,并且希望将其内容包含在 initramfs 内的 /
目录下。我们会运行:
$ sudo dracut --include /foo / --force
只有目录的内容会被复制到 initramfs 中,而不是目录本身。
使用 –install 选项安装文件
--install
选项还可用于将文件包含在 initramfs 中。与 --include
的主要区别在于文件安装在 initramfs 内,位置与系统中的位置相同。使用该选项时,我们提供应作为参数包含的文件列表;应在引号中指定多个文件,并以空格分隔。例如,要为当前运行的内核重新生成 initramfs 并安装 /custom-content.conf
和 /custom-content0.conf
文件,我们将运行:
$ sudo dracut --install "/custom-content.conf /custom-content0.conf" --force
要包含的文件必须存在于源文件系统上,否则将生成错误。如果我们不确定文件是否存在,我们可以使用--install-Optional
来代替:仅当文件存在时才会包含文件。
控制 initramfs 压缩
默认情况下,包含 initramfs 文件的 cpio 存档使用 gzip
进行压缩。但是,我们可以决定使用替代压缩方法或根本不压缩。我们可以使用适当的命令行选项配置此参数。它们以用于压缩的算法命名。其中:
–no-compress(Initramfs 不压缩)
–gzip
–bzip2
–lzma
–xz
–lzo
–lz4
德拉卡特配置文件
到目前为止,我们了解了如何使用 dracut 命令行选项配置 initramfs 生成的许多方面。作为替代方案,我们可以使用其配置文件来修改 dracut 行为。在讨论我们可以设置的一些配置之前,了解配置文件的处理方式非常重要。按优先顺序:
运行时选项
/etc/dracut.conf.d 中以“.conf”结尾的配置文件
/usr/lib/dracut/dracut.conf.d 中以“.conf”结尾的配置文件
/etc/dracut.conf 中的配置
命令行选项始终覆盖配置文件中的说明。主要的 dracut 配置文件是 /etc/dracut.conf
,但可以使用以 .conf
后缀结尾的专用配置文件进行模块化安装,这些文件可以放置在/usr/lib/dracut/dracut.conf.d
(通常是“供应商”配置)和 /etc/dracut.conf.d
目录。后一个目录中的配置文件替换前一个目录中的同名配置文件。带有 .conf
后缀的文件按字母数字顺序处理。这些文件中存在的指令覆盖/etc/dracut.conf
文件中指定的指令。有关可在配置文件中使用的指令的完整列表,请通过运行以下命令查阅手册页:
$ man dracut.conf
这里我们仅举几个例子:
Configuration | Explanation |
---|---|
dracutmodules+=”<dracut modules>” | Space separated list of the modules which should be used for the initramfs |
add_dracutmodules+=”<dracut modules>” | Space separated list of additional modules which should be used for the initramfs |
install_items+= “<file>[ <file> …]” | Space separated list of files which should be installed in the initramfs |
compress=”{cat|bzip2|lzma|xz|gzip|lzo|lz4|zstd|<compressor [args …]>}” | Specify the compression method |
hostonly=”{yes|no}” | Specify whether only what is needed for the host should be included in the initramfs |
结论
在本教程中,我们讨论了 dracut,该工具用于在许多 Linux 发行版上构建 initramfs,主要是那些属于 Red Hat 系列的发行版。我们了解了 dracut 基础知识、如何为特定内核版本和系统上安装的所有内核构建 initramfs、如何构建主机定制的 initramfs、如何列出 initramfs 内的文件以及如何包含额外文件。我们还了解了如何更改 initramfs 压缩方法,最后了解了如何使用 dracut 配置文件。