Linux initial RAM disk (initrd) Overview

Source: Internet
Author: User

The Linux initial RAM disk (initrd) is a temporary root file system mounted during system boot. It is used to support the two-phase boot process. The initrd file contains various executable programs and drivers that can be used to mount the actual root file system, unmount the initrd RAM disk, and release the memory. In many embedded Linux systems, initrd is the final root file system. This article will explore the initial RAM disk of Linux 2.6, including how to create and use it in the Linux kernel.

 

What is an initial RAM disk?

Initial RAM disk (initrd)Is an initial root file system attached to the system before the actual root file system is available. Initrd is bound with the kernel and loaded as part of the kernel boot process. The kernel then loads the initrd file as part of its two-phase boot process, so that you can later use the real file system and mount the actual root file system.

Initrd contains the directory required to achieve this goal and the minimal set of executable programs, such asinsmodTool.

Initrd is a temporary file system in a desktop or Server Linux system. It has a short life cycle and can only serve as a bridge to the real file system. Initrd is a permanent root file system in an embedded system without storage devices. This article will explore these two situations.



Back to Top

Initrd profiling

The initrd image contains the necessary executable programs and system files required to support the two-phase Linux boot process.

The method for creating an initial RAM disk may vary depending on the Linux version we run. Before Fedora Core 3, initrd usedLoop Device.Loop DeviceIs a device driver that can be used to mount files to the system as a block device, and then you can view the content in the file system. You may not have a loop device in your kernel, but you can use the Kernel configuration tool (make menuconfig) SelectDevice Drivers> Block devices> loopback device support. You can view the content of the loop device as follows (the name of the initrd file may be slightly different ):

Listing 1. view the content of initrd (applicable to versions earlier than FC3)

# 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#

Now we can view the content in the/mnt/initrd subdirectory, which represents the content of the initrd file. Note that even if your initrd image file is not. at the end of GZ, it may also be a compressed file. You can add this file. GZ suffix, and then use gunzip to decompress it.

Starting from Fedora Core 3, the default initrd image is changed to a compressed cpio archive file. Instead of using the loop device to mount initrd as a compressed image, we can use it as a cpio archive file. To view the contents of the cpio archive file, run the following command:

Listing 2. view the content of initrd (applicable to FC3 and later versions)

# 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

A small root file system is generated, as shown in listing 3. There is a small but essential set of applications in the./bin directory, includingnash(Not a shell, is a script interpreter ),insmod(Used to load the kernel module) andlvm(Logical volume management tool ).

Listing 3. Default Linux initrd directory structure

# 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#

What is interesting in listing 3 is that the init file is in the root directory. Similar to the traditional Linux boot process, this file is also called when extracting the initrd image to a RAM disk. We will explore this issue later in this article.



Back to Top

Tool used to create initrd

Cpio command

UsecpioCommand, we can operate on the cpio file. Cpio is a file format. It uses a file header to concatenate a group of files. Cpio file formats can use ASCII and binary files. To ensure portability, we can use ASCII format. To reduce the file size, we can use the binary version.

Let's go back to the very beginning to see how the initrd image was initially built. For traditional Linux systems, the initrd image is created during the Linux build process. There are many tools, suchmkinitrd, Can be used to use the necessary libraries and modules to automatically build initrd, thus serving as a bridge to the real root file system.mkinitrdThe tool is actually a shell script, so we can see how it achieves this result. There is anotherYAIRD(Yet another mkinitrd) tool, which can be customized for various aspects of the initrd build process.



Back to Top

Manually build a custom initial RAM disk

Because many Linux-based embedded systems do not have hard disks, initrd will also be used as a permanent root file system for such systems. Listing 4 shows how to create an initrd image file. I use a standard Linux desktop, so that you can follow the steps below even if you do not have an embedded platform. In addition to cross-compilation, other concepts (also applicable to initrd construction) are the same for embedded platforms.

Listing 4. Create a custom initrd tool (mkird)

#! /Bin/bash # housekeeping... rm-F/tmp/ramdisk. imgrm-F/tmp/ramdisk.img.gz # ramdisk constantsrdsize = 4000 blksize = 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/modules/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 example # 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

Initrd Linux release

Minimax is an open-source project designed to become a Linux release encapsulated in initrd. It is 32 MB in size. To be as small as possible, it uses busybox and uclibc. In addition to being very small, it also uses the 2.6 Linux kernel and provides many useful tools.

To create initrd, we first created an empty file, which uses/dev/zero(A zero-composition code stream) as the input, and write it into the ramdisk. IMG file. The size of the generated file is 4 MB (4000 1 K blocks ). Then usemke2fsCommand to create an ext2 (second extended) File System on this empty file. Now this file is changed to an ext2 file system. We use the loop device to mount this file to/mnt/initrd. On this mount point, we now have a directory which is presented in the form of an ext2 file system. We can assemble our own initrd file. The following script provides this function.

The next step is to create the subdirectories required to form the root file system:/bin,/sys,/dev, And/proc. Only the required directories are listed here (for example, there is no database), but there are many functions.

Replacement of ext2 File System

Although ext2 is a common Linux File System Format, there are some alternatives to reduce the size of initrd image files and mounted file systems. Examples of such file systems include romfs (ROM file system), cramfs (compressed ROM file system), and squashfs (highly compressed read-only file system ). If we need to temporarily write data to the file system, ext2 can implement this function well. E2compr is an extension of the ext2 file system driver and supports online compression.

In order to use the root file system, we use busybox. This tool is a single image that contains many tools that can be found on Linux systems (such as ash, awk, sed, and insmod ). Busybox has the advantage of packaging many tools into a file and sharing their common elements, which can greatly reduce the size of the image file. This is ideal for embedded systems. Copy the busybox image from your source directory to the/bin directory under your root directory. Then, many symbolic links are created, all pointing to the busybox tool. Busybox determines which tool is called and executes the functions of the tool. We have created several links in this directory to support the init script (each command is a link pointing to busybox .)

The next step is to create several special device files. I copied these files directly from my current/dev subdirectory, which uses-aOption (archive) to retain their attributes.

The penultimate step is to generate the linuxrc file. After the RAM disk is mounted to the kernel, it looksinitFile. If noinitFile, the kernel will call the linuxrc file as its own startup script. In this file, we implement basic environment settings, such as mounting the/proc file system. In addition to/proc, I mounted the/sys file system and printed a message to the terminal. Finally, we callash(Clone of a Bourne shell) to interact with the root file system. Use the linuxrc FilechmodCommand to be executable.

Finally, our root file system is complete. We unmount it, and then usegzipCompress it. The generated file (ramdisk.img.gz) is copied to the/boot subdirectory, so that it can be loaded through GNU grub.

To build an initial RAM disk, we can simply callmkirdIn this way, the image file is automatically created and copied to the/boot directory.



Back to Top

Test the customized initial RAM disk

Initrd support in Linux Kernel

For the Linux kernel, to support the initial RAM disk, the kernel must useCONFIG_BLK_DEV_RAMAndCONFIG_BLK_DEV_INITRD.

The new initrd image is now in the/boot directory, so the next step is to test it using the default kernel. Now we can restart the Linux system. When the grub interface appears, press the C key to start the command line tool in grub. Now we can interact with grub to define the kernel to be loaded and the initrd image file.kernelCommand allows us to specify the kernel file,initrdCommand can be used to specify the initrd image file. After defining these parameters, you can usebootCommand to guide the kernel, as shown in listing 5.

Listing 5. Using grub to manually boot the kernel and initrd

    GNU GRUB  version 0.95  (638K lower / 97216K upper memory)[ Minimal BASH-like line editing is supported. For the first word, TAB  lists possible command completions. Anywhere else TAB lists the possible  completions of a device/filename. ESC at any time exits.]grub> kernel /bzImage-2.6.1   [Linux-bzImage, setup=0x1400, size=0x29672e]grub> initrd /ramdisk.img.gz   [Linux-initrd @ 0x5f2a000, 0xb5108 bytes]grub> bootUncompressing Linux... OK, booting the kernel.

After the kernel is started, it checks whether the initrd image file is available (more details will be given later), loads it, and mounts it to the root file system. In Listing 6, we can see the final appearance of the Linux Startup Process. After startup, ash shell can be used to input commands. In this example, we will browse the content of the root file system and view the content in the virtual proc file system. We also demonstrate how to use the touch command to create files in the file system. Note that the first process created islinuxrc(Usuallyinit).

Listing 6. Using a simple initrd to boot the Linux Kernel

...md: Autodetecting RAID arraysmd: autorunmd: ... autorun DONE.RAMDISK: Compressed image found at block 0VFS: Mounted root (ext2 file system).Freeing unused kernel memory: 208k freed/ $ lsbin         etc       linuxrc       proc        sysdev         lib       lost+found    sbin/ $ cat /proc/1/cmdline/bin/ash/linuxrc/ $ cd bin/bin $ lsash      cat      echo     mount    sysctlbusybox  dmesg    ls       ps/bin $ touch zfile/bin $ lsash      cat      echo     mount    sysctlbusybox  dmesg    ls       ps       zfile



Back to Top

Use an initial RAM disk to boot the system

Now we have learned how to build and use a custom initial RAM disk. This section describes how the kernel recognizes initrd and mounts it as the root file system. We will introduce several main functions in the startup chain and explain what operations are being performed.

The boot loader, such as grub, defines the kernel to be loaded and copies the kernel image and related initrd to the memory. We can find many of these features in the./init subdirectory in the Linux kernel source code directory.

After the kernel and initrd images are decompressed and copied to the memory, the kernel will be called. It will perform different initialization operations, and eventually you will find yourselfinit/main.c:init()(Subdir/file: function) function. This function executes a large number of subsystem initialization operations. A Pairinit/do_mounts.c:prepare_namespace()This function is used to prepare the namespace (mount the dev file system, raid or MD, device, and final initrd ). Loading initrd is done by callinginit/do_mounts_initrd.c:initrd_load().

initrd_load()Function calledinit/do_mounts_rd.c:rd_load_image()By callinginit/do_mounts_rd.c:identify_ramdisk_image()To determine which RAM disk to attach. This function checks the magic number of the image file to determine whether it is in minux, etc2, romfs, cramfs, or GZIP format. Wheninitrd_load_imagePreviously, it also calledinit/do_mounts_rd:crd_load(). This function allocates space for the RAM disk, computes the cyclic redundancy check code (CRC), decompress the RAM disk image, and loads it into the memory. Now we have this initrd image in a suitable block device.

Use oneinit/do_mounts.c:mount_root()Call to mount the block device to the root file system. It creates the root device and callsinit/do_mounts.c:mount_block_root(). Callinit/do_mounts.c:do_mount_root()The latter will callfs/namespace.c:sys_mount()To mount the root file system, and thenchdirTo this file system. This is the familiar message we see in Listing 6.VFS: Mounted root (ext2 file system)..

Finally, returninitFunction, and callinit/main.c:run_init_process. This causes the callexecveTo start the INIT process (in this example/linuxrc). Linuxrc can be an executable program or a script (provided that it has a script interpreter available ).

The Calling hierarchy of these functions is shown in listing 7. Although not all functions involved in copying and attaching an initial RAM disk are listed here, this is enough to provide a rough framework of the overall process.

Listing 7. Hierarchy of the main functions used during initrd loading and mounting

init/main.c:init  init/do_mounts.c:prepare_namespace    init/do_mounts_initrd.c:initrd_load      init/do_mounts_rd.c:rd_load_image        init/do_mounts_rd.c:identify_ramdisk_image        init/do_mounts_rd.c:crd_load          lib/inflate.c:gunzip    init/do_mounts.c:mount_root      init/do_mounts.c:mount_block_root         init/do_mounts.c:do_mount_root           fs/namespace.c:sys_mount  init/main.c:run_init_process    execve



Back to Top

Diskless boot

Similar to embedded boot, a local disk (floppy disk or CD-ROM) is not required for both the boot kernel and the ramdisk root file system. DHCP (Dynamic Host Configuration Protocol) can be used to determine network parameters, such as IP addresses and subnet masks. Trivial File Transfer Protocol can be used to transmit kernel images and initial ramdisk images to local devices. After the transfer is complete, you can boot the Linux kernel and mount initrd, which is similar to the local image boot process.



Back to Top

Compress initrd

When building an embedded system, we may want to make the initrd image file as small as possible. There are some tips to consider. First, use busybox (as shown in this article ). Busybox can compress several Mb tools into several hundred kb.

In this example, the busybox image is statically linked, so it does not need other libraries. However, if we need a standard C library (we may need a customized binary Library), we have other options besides the huge glibc. The first small library is uclibc, which is a standard C library prepared for systems with very strict spatial requirements. Another library suitable for space-intensive environments is dietlib. Remember that we need to use these libraries to re-compile the binary files we want to re-compile in the embedded system, so this requires additional work (but this is very worthwhile ).



Back to Top

Conclusion

The initial RAM disk was initially designed to use a temporary root file system as a bridge between the kernel and the final root file system. Initrd is also useful for non-persistent root file systems that are loaded into the RAM disk in an embedded system.

References

Learning

  • For more information, see the original article on the developerworks global site.
  • "Linux boot process insider" (developerworks, May 2006) explores the process from Linux's initial Bootstrap to starting the first user space application.
  • In "Boot Linux from FireWire devices" (developerworks, July 2004), we can learn how to start Linux on various devices on various platforms (using initrd.
  • The cpio file format is simple and concise. Therefore, it is no surprise that the fedora team chooses to use it as the initrd format.
  • The mkinitrd tool is ideal for creating initrd image files. In addition to creating an initrd image, you can determine which modules are to be loaded for your system and add all these modules to the image.
  • The loop device is a very useful driver that can mount the image file as a file system.
  • Network boot and exotic root howto not only introduces the process of Linux booting from the network, but also introduces content such as floppy boot, CD-ROM boot and embedded environment.
  • In the developerworks Linux area, you can find more resources for Linux developers.
  • Stay tuned to developerworks technical events and network broadcasts.

Obtain products and technologies

  • The cpio file format (which can now be used as an initrd image format of Fedora Core) has a long history and can be used on many UNIX systems.
  • Ash shell is a clone of the Bourne shell (most of which are compatible). Although it is small, it works completely. It is ideal for use as a script interpreter on Embedded Systems with very strict spatial requirements.
  • Busybox is a good way to reduce your memory requirements for the next embedded Linux project.
  • To further reduce the size of the initrd file, consider using the glibc alternative library, such as uclibc or dietlib. If you like to use C ++, you can try the alpha version of The uclibc ++ library.
  • Minimax is a Linux release that is completely encapsulated in the initrd image file!
  • Order a free SEK for Linux with two DVDs, including the latest trial software for IBM for Linux, including DB2, Lotus, rational, Tivoli, and websphere.
  • Use IBM trial software in your next development project, which can be downloaded directly from developerworks.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.