Initramfs/Guide/zh-cn

越来越多的系统需要一个intramfs才能正常启动. 在本指南中，将说明 initramfs的概念，以及如何正确地创建和管理initramfs.

介绍
许多用户，是没有必要关心initramfs系统的. 他们的系统使用简单的分区，并使用架构，没有奇奇怪怪的驱动程序或设置（如加密的文件系统），因此 Linux 内核是完全能够把控制权交给他们系统的可执行文件. 但对于许多系统，initramfs 是强制性使用的.

理解initramfs 是什么（为什么需要它），这需要你了解Linux引导过程，甚至的很深入的接触.

Linux 启动过程
一旦Linux内核拥有超过系统（它由启动引导程序被加载后获得）的控制，它准备的内存结构和驱动程序. 它然后将控制移交给应用程序（通常是）的任务是进一步准备系统，并确保在引导过程结束时，所有必要的服务正在运行，并且用户能够登录. 该应用程序不会通过启动，在其他服务中，并根据检测到的设备上的系统，进一步加载守护. 当启动，尚未挂载的所有的文件系统安装和服务的其余部分将被启动.

为系统所有必需的文件和工具驻留在同一个文件系统， 应用程序可以完全控制进一步的引导过程. 但当有多个文件系统被定义（或拥有更多的外来设备）时，这可能变得更棘手些︰


 * 当 分区是一个单独的文件系统分区、 工具和驱动程序有文件存储在 ，他们不能被使用除非 分区是可用的. 如果需要这些工具来使 分区可用，然后我们就不能启动系统了.


 * 如果根文件系统被加密，那么Linux内核将无法找到 引导应用，导致系统无法启动.

针对此问题的解决方案，就是使用一个的initrd（初始根设备）.

初始根磁盘
initrd是一个内存磁盘结构（ramdisk）的文件，其中包含了必要的工具和挂载必要文件系统分区的脚本，在控制权移交给/系统分区下 程序 “前”被启动. Linux内核接下来会触发设置脚本（通常称为，但这个名字可能不一样）而且他在根分区里面. 它让系统切换到真正的根文件系统，然后调用.

虽然使用initrd是必要的，但是它有一些缺点：


 * 这是一个完整的块设备，他需要整个文件系统的开销，它有一个固定的大小. 选择一个initrd是太小了，所需要的脚本不适用. 让它过大的话，就会浪费内存.


 * Because it is a real, static device it consumes cache memory in the Linux kernel and is prone to the memory and file management methods in use (such as paging), this makes initrd greater in memory consumption.

To resolve these (for some perhaps hardly called) problems, the initramfs was created.

initial ram 文件系统
An initramfs is an initial ram file system based on tmpfs (a size-flexible, in-memory lightweight file system), which also did not use a separate block device (so no caching was done and all overhead mentioned earlier disappears). Just like the initrd, it contains the tools and scripts needed to mount the file systems before the binary on the real root file system is called. These tools can be decryption abstraction layers (for encrypted file systems), logical volume managers, software raid, bluetooth driver based file system loaders, etc.

The content of the initramfs is made by creating a cpio archive. is an old (but proven) file archiver solution (and its resulting archive files are called cpio files). cpio is definitely comparable to the archiver. The choice of here was because it was easier to implement (code-wise) and supported (back then) device files which  could not.

All files, tools, libraries, configuration settings (if applicable), etc. are put into the cpio archive. This archive is then compressed using the 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 launches the script located in the root of the tmpfs file system. This script will then mount 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 and  ).

Once the root file system and the other vital file systems are mounted, the script from the initramfs will switch the root towards the real root file system and finally call the  binary on that system to continue the boot process.

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. For instance, if LVM is used, then LVM tools will be needed in the initramfs. Likewise, if software RAID is used, utilities will be needed, etc.

Some tools exist that help users create initramfs' (compressed archives) for their system. But for those who want total control can easily create personal, custom initramfs' as well.

Once created, the bootloader configuration will need adjusted to inform it an initramfs is to be used. For instance, if the initramfs file is stored as, then the configuration in could look like the following:

使用 genkernel
Gentoo's kernel building utility,, can be used to generate an initramfs, even if was not used to configure and build the kernel.

To use for generating an initramfs, it is recommended all necessary drivers and code that is needed to mount the  and  file systems be included in the kernel (not as modules). Then, call as follows:

Depending on the system, one or more of the following options may be needed:

When finished, the resulting initramfs file will be stored in.

使用 dracut
The utility is created for the sole purpose of managing initramfs files. It uses a highly modular approach on what support is to be included and what is not to be included.

To install, make special care to include support for the correct value(s) in the DRACUT_MODULES variable. This variable can be set in to include support for system specific setups:

It is advisable to set (or unset) the modules needed. After configuring the DRACUT_MODULES variable in, to install the Dracut utility.

The next step is to configure by editing. In the configuration file, which is well commented, in order to add support for needed modules.

Once configured, create an initramfs by calling as follows:

The resulting image supports generic system boots based on the configuration in. It is also possible to generate an initramfs specifically tailored to your system (which tries to detect the needed tools, drivers, etc. from the existing system). If the modules and drivers are built into the kernel (not as separate modules and references to the firmware), then the  option can be added:

For more information, check out the dracut and dracut.cmdline manual pages:

参考

 * Initramfs on the official Gentoo Wiki.
 * Dracut on the official Gentoo Wiki.

外部资源

 * The ramfs-rootfs-initramfs.txt file within the Linux kernel documentation.