本文共 6111 字,大约阅读时间需要 20 分钟。
The initial RAM disk (initrd) is an initial root file system that is mounted prior to when the real root file system is available. The initrd is bound to the kernel and loaded as part of the kernel boot procedure. The kernel then mounts this initrd as part of the two-stage boot process to load the modules to make the real file systems available and get at the real root file system.
The initrd contains a minimal set of directories and executables to achieve this, such as the insmod
tool to install kernel modules into the kernel.
In the case of desktop or server Linux systems, the initrd is a transient file system. Its lifetime is short, only serving as a bridge to the real root file system. In embedded systems with no mutable storage, the initrd is the permanent root file system. This article explores both of these contexts.
The initrd image contains the necessary executables and system files to support the second-stage boot of a Linux system.
Depending on which version of Linux you're running, the method for creating the initial RAM disk can vary. Prior to Fedora Core 3, the initrd is constructed using the loop device. The loop device is a device driver that allows you to mount a file as a block device and then interpret the file system it represents. The loop device may not be present in your kernel, but you can enable it through the kernel's configuration tool (make menuconfig
) by selecting Device Drivers > Block Devices > Loopback Device Support. You can inspect the loop device as follows (your initrd file name will vary):
# mkdir temp ; cd temp# cp /boot/initrd.img.gz .# gunzip initrd.img.gz# mount -t ext -o loop initrd.img /mnt/initrd# ls -la /mnt/initrd# |
You can now inspect the /mnt/initrd subdirectory for the contents of the initrd. Note that even if your initrd image file does not end with the .gz suffix, it's a compressed file, and you can add the .gz suffix to gunzip it.
Beginning with Fedora Core 3, the default initrd image is a compressed cpio archive file. Instead of mounting the file as a compressed image using the loop device, you can use a cpio archive. To inspect the contents of a cpio archive, use the following commands:
# mkdir temp ; cd temp# cp /boot/initrd-2.6.14.2.img initrd-2.6.14.2.img.gz# gunzip initrd-2.6.14.2.img.gz# cpio -i --make-directories < initrd-2.6.14.2.img# |
The result is a small root file system, as shown in Listing 3. The small, but necessary, set of applications are present in the ./bin directory, including nash
(not a shell, a script interpreter), insmod
for loading kernel modules, and lvm
(logical volume manager tools).
# ls -la#drwxr-xr-x 10 root root 4096 May 7 02:48 .drwxr-x--- 15 root root 4096 May 7 00:54 ..drwxr-xr-x 2 root root 4096 May 7 02:48 bindrwxr-xr-x 2 root root 4096 May 7 02:48 devdrwxr-xr-x 4 root root 4096 May 7 02:48 etc-rwxr-xr-x 1 root root 812 May 7 02:48 init-rw-r--r-- 1 root root 1723392 May 7 02:45 initrd-2.6.14.2.imgdrwxr-xr-x 2 root root 4096 May 7 02:48 libdrwxr-xr-x 2 root root 4096 May 7 02:48 loopfsdrwxr-xr-x 2 root root 4096 May 7 02:48 proclrwxrwxrwx 1 root root 3 May 7 02:48 sbin -> bindrwxr-xr-x 2 root root 4096 May 7 02:48 sysdrwxr-xr-x 2 root root 4096 May 7 02:48 sysroot# |
Of interest in Listing 3 is the init file at the root. This file, like the traditional Linux boot process, is invoked when the initrd image is decompressed into the RAM disk. We'll explore this later in the article.
Let's now go back to the beginning to formally understand how the initrd image is constructed in the first place. For a traditional Linux system, the initrd image is created during the Linux build process. Numerous tools, such as mkinitrd
, can be used to automatically build an initrd with the necessary libraries and modules for bridging to the real root file system. The mkinitrd
utility is actually a shell script, so you can see exactly how it achieves its result. There's also the YAIRD
(Yet Another Mkinitrd) utility, which permits customization of every aspect of the initrd construction.
Because there is no hard drive in many embedded systems based on Linux, the initrd also serves as the permanent root file system. Listing 4 shows how to create an initrd image. I'm using a standard Linux desktop so you can follow along without an embedded target. Other than cross-compilation, the concepts (as they apply to initrd construction) are the same for an embedded target.
#!/bin/bash# Housekeeping...rm -f /tmp/ramdisk.imgrm -f /tmp/ramdisk.img.gz# Ramdisk ConstantsRDSIZE=4000BLKSIZE=1024# Create an empty ramdisk imagedd if=/dev/zero of=/tmp/ramdisk.img bs=$BLKSIZE count=$RDSIZE# Make it an ext2 mountable file system/sbin/mke2fs -F -m 0 -b $BLKSIZE /tmp/ramdisk.img $RDSIZE# Mount it so that we can populatemount /tmp/ramdisk.img /mnt/initrd -t ext2 -o loop=/dev/loop0# Populate the filesystem (subdirectories)mkdir /mnt/initrd/binmkdir /mnt/initrd/sysmkdir /mnt/initrd/devmkdir /mnt/initrd/proc# Grab busybox and create the symbolic linkspushd /mnt/initrd/bincp /usr/local/src/busybox-1.1.1/busybox .ln -s busybox ashln -s busybox mountln -s busybox echoln -s busybox lsln -s busybox catln -s busybox psln -s busybox dmesgln -s busybox sysctlpopd# Grab the necessary dev filescp -a /dev/console /mnt/initrd/devcp -a /dev/ramdisk /mnt/initrd/devcp -a /dev/ram0 /mnt/initrd/devcp -a /dev/null /mnt/initrd/devcp -a /dev/tty1 /mnt/initrd/devcp -a /dev/tty2 /mnt/initrd/dev# Equate sbin with binpushd /mnt/initrdln -s bin sbinpopd# Create the init filecat >> /mnt/initrd/linuxrc << EOF#!/bin/ashechoecho "Simple initrd is active"echomount -t proc /proc /procmount -t sysfs none /sys/bin/ash --loginEOFchmod +x /mnt/initrd/linuxrc# Finish up...umount /mnt/initrdgzip -9 /tmp/ramdisk.imgcp /tmp/ramdisk.img.gz /boot/ramdisk.img.gz |
转载地址:http://gemvi.baihongyu.com/