This is Gentoo's testing wiki. It is a non-operational environment and its textual content is outdated.

Please visit our production wiki at https://wiki.gentoo.org

Initramfs/指南

From Gentoo Wiki (test)
Jump to:navigation Jump to:search
This page is a translated version of the page Initramfs/Guide and the translation is 63% complete.
Other languages:

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

Initramfs 是什么

介绍

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

理解initramfs 是什么(为什么需要它),这需要你了解Linux引导过程,甚至的很深入的接触。

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 (back then) device files which tar could not.

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

Several tools exist that help users create initramfs (compressed cpio archives) for their system. Those who want total control can easily create personal, custom initramfs images 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 /boot/initramfs-3.2.2-gentoo-r5, then the configuration in /boot/grub/grub.conf could look like the following:

FILE grub.confExample entry in grub.conf for booting with an initramfs
title Gentoo Linux 3.2.2-r5
root (hd0,0)
kernel /boot/kernel-3.2.2-gentoo-r5
initrd /boot/initramfs-3.2.2-gentoo-r5

使用 genkernel

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

To use genkernel for generating an initramfs, it is recommended all necessary drivers and code that is needed to mount the / and /usr file systems be included in the kernel (not as modules). Then, 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

Warning
At the time of writing, dracut is not marked stable yet, so it may need unmasked before continuing.

The dracut 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 dracut, make special care to include support for the correct value(s) in the DRACUT_MODULES variable. This variable can be set in /etc/portage/make.conf to include support for system specific setups:

FILE make.confPreparing to install Dracut
'"`UNIQ--pre-00000001-QINU`"'

It is advisable to set (or unset) the modules needed. After configuring the DRACUT_MODULES variable in /etc/portage/make.conf, emerge dracut to install the Dracut utility.

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

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

参考

外部资源