Initramfs/指南
一些基于 Linux 的计算机系统系统需要一个intramfs才能正常启动。在本指南中,将说明 initramfs 的概念,以及如何正确地创建和管理 initramfs。
Initramfs 是什么
介绍
许多用户是没有必要关心initramfs系统的。他们的系统使用了简单的分区方案,而且没有奇奇怪怪的驱动程序或者设置(如加密的文件系统),因此 Linux 内核完全能够把控制权交给系统中的init可执行文件。但对于许多系统,initramfs 是强制性使用的。
Linux 启动过程
一旦 Linux 内核控制了系统(内核在由启动加载引导程序加载后获得控制权),它就会准备好内存结构和驱动程序。然后它将控制交给应用程序(通常是 init),其任务是进一步准备系统并确保在引导过程结束时,所有必要的服务正在运行且用户能够登录。该 init 应用程序通过启动 udev 守护程序来执行此操作,该守护程序将根据检测到的设备进一步加载和准备系统。启动 udev 时,将挂载尚未挂载的所有剩余文件系统,并启动其余服务。
对于那些所有必需的文件和工具驻留在同一个文件系统中的系统, init 应用程序可以完全控制进一步的引导过程。但当有多个文件系统被定义(或拥有更多的外来设备)时,情况可能变得更棘手些︰
- 当 /usr 分区位于单独的文件系统上时,除非 /usr 可用,否则无法使用存储在 /usr 中的文件的工具和驱动程序。如果需要这些工具来使 /usr 可用,那么我们就无法启动系统。
- 如果根文件系统被加密,那么 Linux 内核将无法找到 init 程序,导致系统无法启动。
这个问题的解决方案长期以来一直使用 initrd(初始根设备)。
初始根磁盘
initrd 是一个内存中的磁盘结构(ramdisk),其中包含必要的工具和脚本,用于在将控制权交给根文件系统上的 init 应用程序之前挂载所需的文件系统。 Linux 内核在此根磁盘上触发安装脚本(通常称为 linuxrc,但该名称不是必需的),此脚本的工作是准备系统、切换到真正的根文件系统,然后调用 init。
虽然使用initrd是必要的,但是它有一些缺点:
- 这是一个完整的块设备,他需要整个文件系统的开销,它有一个固定的大小。选择一个initrd是太小了,所需要的脚本不适用。让它过大的话,就会浪费内存。
- 因为它是一个真实的、 静态的设备,它消耗 Linux 内核中的缓存内存和易于使用的文件(如分页),这使得 initrd 有更大的内存消耗。
initramfs 的诞生解决了这些的问题。
initial ram 文件系统
Initramfs 初始 ram 文件系统基于 'tmpfs ' (大小灵活、 内存中的轻量级文件系统),但是他并不是一个单独的块设备 (所以没有缓存和所有额外的开销)。就像 initrd,它包含的工具和脚本在被称为真正的根文件系统上的二进制文件 init启动之前被挂载 。这些工具可以解密抽象层 (用于加密的文件系统),逻辑卷管理器,软件 raid,蓝牙驱动程序基于文件系统的装载机等。
The content of the initramfs is made by creating a cpio archive. cpio is an old (but proven) file archiver solution (and its resulting archive files are called cpio files). cpio is definitely comparable to the tar archiver. The choice of cpio here was because it was easier to implement (code-wise) and supported device files which tar did not support at the time.
All files, tools, libraries, configuration settings (if applicable), etc. are put into the cpio archive. This archive is then compressed using the gzip utility and stored alongside the Linux kernel. The boot loader will then offer it to the Linux kernel at boot time so the kernel knows an initramfs is needed.
Once detected, the Linux kernel will create a tmpfs file system, extract the contents of the archive on it, and then launch the init script located in the root of the tmpfs file system. This script then mounts the real root file system (after making sure it can mount it, for instance by loading additional modules, preparing an encryption abstraction layer, etc.) as well as vital other file systems (such as /usr and /var ).
Once the root file system and the other vital file systems are mounted, the init script from the initramfs will switch the root towards the real root file system and finally call the /sbin/init binary on that system to continue the boot process.
创建一个initramfs
Introduction and bootloader configuration
To create an initramfs, it is important to know what additional drivers, scripts, and tools will be needed to boot the system.
The root disk and root filesystem needs to be accessible
In practice, this usually means CONFIG_SATA_AHCI or CONFIG_BLK_DEV_NVME (for the disk), and CONFIG_EXT4_FS (the filesystem) should ideally be built-in [=y] to the kernel; otherwise having these as modules [=m] will require these modules to be loaded by the initramfs first so booting can continue.
If LVM is used, then LVM tools will be needed in the initramfs.
If software RAID is used, mdadm utilities will be needed, or DMRAID, etc... ZFS and BTRFS would also require consideration here.
Several automated tools exist that help users create initramfs (compressed cpio archives) for their system. Most commonly used are dracut and genkernel. Those who want total control can easily create personal, custom initramfs images as well manually.
Once created, the bootloader configuration will need to be adjusted to inform it an initramfs is to be used.
Usually this is done by re-generating the config file grub-mkconfig -o /boot/grub/grub.cfg
For instance, if the initramfs file is stored as /boot/initramfs-5.15.32-gentoo-r1.img, then afterwards, the GRUB configuration in /boot/grub/grub.cfg would look somewhat like the following:
title Gentoo Linux 5.15.32-r1
set root='hd1,gpt2'
linux /boot/vmlinuz-5.15.32-gentoo-r1-x86_64
initrd /boot/initramfs-5.15.32-gentoo-r1-x86_64.img
使用 genkernel
Gentoo's kernel building utility, Genkernel, can be used to generate an initramfs. genkernel expects that both the kernel and initramfs are built by it, although in some cases, an initramfs built by genkernel with the kernel built by another method may work (but is discouraged).
To use genkernel for generating an initramfs, it is recommended all necessary drivers and code that is needed to mount the / root and /usr file systems be included as built-in in the kernel (not as modules). If these are built-in, genkernel can be called with the --no-ramdisk-modules
option.
If modules are required in the initramfs, call genkernel as follows:
root #
genkernel --install --no-ramdisk-modules initramfs
Depending on the system, one or more of the following options may be needed:
Option | Description |
---|---|
--disklabel
|
Add support for LABEL= settings in /etc/fstab
|
--dmraid
|
Add support for fake hardware RAID. |
--firmware
|
Add in firmware code found on the system. |
--gpg
|
Add in GnuPG support. |
--iscsi
|
Add support for iSCSI. |
--luks
|
Add support for LUKS encryption containers. |
--lvm
|
Add support for LVM. |
--mdadm
|
Add support for software RAID. |
--multipath
|
Add support for multiple I/O access towards a SAN. |
--zfs
|
Add support for ZFS. |
When finished, the resulting initramfs file will be stored in /boot.
使用 dracut
The Dracut utility is created for the sole purpose of managing initramfs files.
To install the Dracut utility, run:
root #
emerge --ask sys-kernel/dracut
The next step is to configure dracut by editing /etc/dracut.conf.
Once configured, create an initramfs by calling dracut as follows:
root #
dracut
由此产生的图像支持基于配置的通用系统启动在/etc/dracut.conf。另外,也可以产生专门为你的系统中的initramfs(其中dracut从现有的系统尝试检测所需要的工具,如驱动器等)。如果模块和驱动内置于内核(而不是作为单独的模块或固件),你可以在 --no-kernel
添加选项:
root #
dracut --host-only --no-kernel
获取更多信息,请查看dracut和dracut.cmdline手册:
user $
man dracut
user $
man dracut.cmdline
参考
- Initramfs 官方 Gentoo Wiki.
- Custom Initramfs — the successor of initrd. It provides early userspace which can do things the kernel can't easily do by itself during the boot process.
- Early Userspace Mounting — how to build a custom minimal initramfs that checks the /usr filesystem and pre-mounts /usr. 另一个值得阅读的关于自定义 initramfs 的文章
外部资源
- The ramfs-rootfs-initramfs.txt file within the Linux kernel documentation.