天天看點

linux initramfs,Initramfs/指南

Other languages:

一些基于 Linux 的計算機系統系統需要一個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 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. 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 initramfstitle 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

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 the Dracut utility, run:

root #emerge --ask sys-kernel/dracut

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

參考

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.