dracut
dracut 會為內核創建初始映像,用於預載訪問根文件系統所需的塊設備模塊(如 IDE、SCSI 或 RAID)。安裝 linux包 時,可以在 mkinitcpio 和 dracut 之間進行選擇。Fedora、RHEL、Gentoo 和 Debian 等系統都使用 dracut,而Arch 默認使用mkinitcpio。
你可以在此查看 dracut 的完整項目文檔。
安裝 dracut包,你也可以安裝 dracut-gitAUR 使用最新的開發版本。
dracut 的用法非常簡單,而且通常就算在非標準環境下也不需要用戶進行配置(如在 LUKS 上配置 LVM)。
使用如下命令為當前運行的內核生成 initramfs:
# dracut --hostonly --no-hostonly-cmdline --add-confdir no-network /boot/initramfs-linux.img
為了永久啟用僅主機(hostonly)模式以便無需在命令行中進行指定,你可以在 dracut 的配置中添加:
/etc/dracut.conf.d/hostonly.conf
hostonly="yes" hostonly_cmdline="no"
dracut-gitAUR 已默認啟用僅主機模式。
# dracut -f --regenerate-all
使用如下命令生成後備 initramfs:
# dracut /boot/initramfs-linux-fallback.img
/boot/initramfs-linux.img 指代輸出的鏡像文件。如果你使用了其它內核,請考慮更改文件名。例如,對於 linux-lts包 內核,輸出文件應使用 /boot/initramfs-linux-lts.img。不過,只要引導加載程序的配置使用了相同的文件名,就可以隨意命名這些文件。
--force 選項會覆寫現有映像文件。
--kver 選項用於指定要使用的內核。該選項的輸入需要匹配 /usr/lib/modules 目錄下文件夾之一的名稱。
其它選項可參考 dracut(8)。
要注意的是,initial ramdisk 階段有兩種執行各種任務的不同方式:
- 基於 Shell(bash/busybox/dash)的 initial ramdisk:該方式會啟動一個初始化腳本,然後掃描 initial ramdisk 的文件系統來查找要執行的 dracut 腳本。
- 基於 systemd(默認)的 initial ramdisk:systemd 在 initial ramdisk 階段時就已啟動。具體要執行的任務由標準的 systemd 單元文件指定,相關信息可參考 systemd 啟動流程。
這兩種方式的主要區別在於 systemd dracut 模塊的存在與否。詳細信息請參考 #dracut 模塊。
dracut 可通過直接傳入命令行參數進行配置(參考 dracut(8) § OPTIONS)。如果你希望執行 dracut 命令時始終帶上特定參數,可以在 /etc/dracut.conf.d/ 目錄下的 .conf 文件中進行指定。例如:
/etc/dracut.conf.d/myflags.conf
hostonly="yes" compress="lz4" add_drivers+=" i915 " omit_dracutmodules+=" systemd network "
更多配置選項可參考 dracut.conf(5)。各個選項的完整說明可參考 dracut(8)。在下文中會對部分常用選項進行說明。
dracut 使用模塊化流程構建 initramfs(參考 dracut.modules(7))。dracut 的所有內置模塊位於 /lib/dracut/modules.d,可通過 dracut --list-modules 命令列出。更多模塊可通過其它軟體包提供(如 dracut-sshd-gitAUR)。遺憾的是,dracut 的內置模塊缺乏文檔說明,儘管它們的名稱通常不言自明。
部分模塊默認被激活/禁用,可通過 --add/--omit 選項分別激活或禁用,也可以在配置文件中使用 add_dracutmodules+="" 或 omit_dracutmodules+="" 持久化更改:
/etc/dracut.conf.d/myflags.conf
# ... add_dracutmodules+=" dracut modules to activate " omit_dracutmodules+=" dracut modules to deactivate " # ...
dracut 模塊文檔請參考上游文檔。
大多數 dracut 模塊都依賴於其它 dracut 模塊。例如,藍牙 dracut 模塊就依賴於 dbus dracut 模塊。
要使用 systemd 通過 systemd-cryptenroll 調用 TPM2 解鎖 luks2 加密卷的功能,需要安裝 tpm2-tools包 並啟用 tpm2-tss dracut 模塊。
可以通過 --force_drivers 命令行選項或 force_drivers+="" 配置項啟用 dracut 早期加載功能(在 initramfs 階段通過 modprobe 加載)。例如:
/etc/dracut.conf.d/myflags.conf
# ... force_drivers+=" nvidia nvidia_modeset nvidia_uvm nvidia_drm " # ...
內核命令行選項可放置在 /etc/dracut.conf.d/ 的 .conf 文件內,並通過 kernel_cmdline= 選項進行配置。Dracut 會自動讀取配置,然後創建並寫入到 initramfs /etc/cmdline.d/ 目錄下的 01-default.conf 文件中。舉個例子,你的內核命令行選項文件內容可能如下:
/etc/dracut.conf.d/cmdline.conf
kernel_cmdline="rd.luks.uuid=luks-f6c738f3-ee64-4633-b6b0-eceddb1bb010 rd.lvm.lv=arch/root rd.lvm.lv=arch/swap root=/dev/arch/root rootfstype=ext4 rootflags=rw,relatime"
不需要為 dracut 指定根塊設備。參考 dracut.cmdline(7):
- 內核使用的根設備從來都是在啟動配置文件的內核命令行選項中指定的。
不過,提前設置一些參數可能會很有用,而且還可以啟用一些其它功能,如提示輸入額外命令行參數。所有選項請參見 dracut.cmdline(7)。下面是一些配置選項示例:
- 從交換分區恢復:
resume=UUID=80895b78-7312-45bc-afe5-58eb4b579422 - 提示輸入額外內核命令行參數:{ic|1=rd.cmdline=ask}}
- 在設定了
quiet的前提下輸出更多信息:rd.info
dracut 可以通過 --uefi 命令行參數或 uefi="yes" 配置項生成統一內核映像。
你可以查看生成的映像的信息,並輸出到單頁上:
# lsinitrd /path/to/initramfs_or_uefi_image | less
該命令會列出生成映像時傳入到 dracut 的參數、包含的 dracut 模塊以及包含的所有文件。
要減少壓縮生成映像所消耗的時間,可以更換使用的壓縮軟體。
只需添加下列任意一行(不能多選)到 dracut 配置文件中:
compress="cat" compress="gzip" compress="bzip2" compress="lzma" compress="xz" compress="lzo" compress="lz4" compress="zstd"
默認使用的是 gzip包。選擇 compress="cat" 將不會壓縮 initramfs。
你也可以使用非官方支持的壓縮軟體:
compress="program"
有些方法可以優化啟動和生成 initramfs 的性能:
- 理解並配置最快的壓縮方式。如果內核模塊已經被壓縮過,可能在生成 initramfs 時就不需要再次進行壓縮。
- 理解在 initramfs 中添加 systemd 可能造成的影響。如果它會降低性能,就將其移除;如果會提升性能,就納入進來。
- 在使用寫時複製文件系統時,考慮使用 dracut-cpio。具體適用性請參考
--enhanced-cpio選項。
- 減少 initramfs 中嵌入的內核模塊和 dracut 模塊的數量。例如:如果安裝了 nfs-utils包,但不依賴其進行啟動,就需要顯式移除 nfs dracut 模塊,否則在默認配置下生成的 initramfs 會啟用網絡啟動 - 詳細信息請參考 https://github.com/dracut-ng/dracut-ng/pull/297 。
- 考慮使用 busybox包 dracut 模塊替換掉 bash。
- 考慮使用
hostonly和hostonly_mode=strict。
linux包 和 dracut-gitAUR 包自帶了用於在內核更新後自動生成新 initramfs 映像的 pacman 鉤子。
-
dracut-ukifyAUR 軟體包是使用 systemd-ukify包 生成統一內核映像的現代方法。與以下方法不同,你可以對整個內核映像簽名,包括 initramfs。需要在 dracut 配置中使用
uefi_secureboot_cert和uefi_secureboot_key選項(dracut.conf(5))。 - 另外,如果你希望 initramfs 映像也是 EFI 可執行文件(即
esp/EFI/Linux/linux-kernel-machine_id-build_id.efi),也可以使用 dracut-uefi-hookAUR。在該目錄下的 EFI 二進制文件會被 systemd-boot 自動檢測到,因此不需要在/boot/loader/loader.conf中額外創建條目。
你還需要通過卸載 mkinitcpio包 或使用以下命令來阻止 mkinitcpio 創建和移除 initramfs 映像:
# ln -sf /dev/null /etc/pacman.d/hooks/90-mkinitcpio-install.hook # ln -sf /dev/null /etc/pacman.d/hooks/60-mkinitcpio-remove.hook
如果檢測到了藍牙鍵盤,dracut 會自動啟用藍牙模塊。但 dracut 需要處於 hostonly 模式才能自動發現藍牙鍵盤。
The limine-dracut-supportAUR package utilizes limine-entry-tool with dracut and pacman hooks to automate the installation and removal of kernels and boot entries in the Limine boot loader. See Limine#Boot entry automation for more information.
如果從休眠中恢復無效,你可能需要配置 dracut 以包含 resume 模塊。添加一個配置文件:
/etc/dracut.conf.d/resume-from-hibernate.conf
add_dracutmodules+=" resume "
如果適用於你的系統,你可能也需要看下從加密交換分區恢復指南以及 dracut 特定指南。
如果內核無法自動發現並掛載 LVM / 軟 RAID / LUKS 塊設備,你可以加上以下內核命令行選項重新生成 initramfs:
rd.auto rd.lvm=1 rd.dm=1 rd.md=1 rd.luks=1
If you have issues booting or very long shutdown processes while the system waits for brltty, add the following to the dracut configuration line:
omit_dracutmodules+=" brltty "
Alternatively, uninstall brltty包 if it is not needed.
Cannot use whirlpool hash for keyslot encryption. Keyslot open failed. No usable keyslot is available.
A failure to boot with a message similar to the above typically will only require the user to include the crypt module via add_dracutmodules.