Linux memory disk Initialization Technology

Source: Internet
Author: User

Linux memory disk Initialization Technology


Share final edit
Lifelens

Linux memory initialization Technology (initrd) is used to support two-phase system boot process. It is a temporary root file system mounted during system startup: here, the root file system refers to the root file system ). Initrd contains many executable programs and drivers, and allows the root file system of the temporary memory disk to be detached. After the memory is released, the real root file system is mounted. Initrd is the final root file system in many embedded Linux file systems. This article mainly describes the initrd Technology of linux2.6 kernel, including the creation and use of the kernel.

1 What is memory disk initialization?
Initrd has a higher Mount priority than the real root file system. It is attached to the kernel and is loaded as part of the kernel startup process ). Then, as the first part of the two-phase boot process, the kernel Mount (Mount) initrd is used to obtain and load a truly valid file system.
For this purpose, initrd contains the minimum directory and programs, such as insmod, to install the kernel module into the kernel.
For desktop or Server Linux, initrd is a temporary file system with a short life cycle. It serves only as a bridge to reach the real root file system. But for an embedded system without storage devices, it is a permanent root file system. This article involves both aspects.

2. In-depth analysis of initrd
Initrd contains necessary programs and system files for supporting the second-stage process of system startup. The method for creating the initialization Memory changes with the system version you are using. After coreora core3, initrd is established by the loop device. What is a send-back device? It is a device driver that allows you to mount a file as a block device and describe its file system. Maybe the loop device does not exist in your kernel, but you can open it through the Kernel configuration tool (make menuconfig. Path: Device Drivers-Block devices-loopback device
Support. The following is the check command:
# 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, you can view the content of initrd by viewing the/mnt/initrd subdirectory. In other words, your initrdimage file does not use .gz as the suffix, but you can also enable gunzip to open it by adding the suffix.
Starting from fedora core3, the default initrd image is a compressed gpio archive file. In addition to mounting files, you can also use cpio to archive files to mount them into a compressed image using the return device. You can run the following command to check the content of the cpio archive file:
# 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 below:

# Ls-la
#
Drwxr-XR-x 10 Root 4096 May 7.
Drwxr-x --- 15 Root 4096 May 7 ..
Drwxr-XR-x 2 root Root 4096 May 7 Bin
Drwxr-XR-x 2 root Root 4096 May 7 Dev
Drwxr-XR-x 4 Root 4096 May 7 etc
-Rwxr-XR-x 1 Root 812 May 7 init
-RW-r -- 1 Root 1723392 May 7 initrd-2.6.14.2.img
Drwxr-XR-x 2 root Root 4096 May 7 lib
Drwxr-XR-x 2 root Root 4096 May 7 loopfs
Drwxr-XR-x 2 root Root 4096 May 7 proc
Lrwxrwxrwx 1 Root 3 May 7 sbin-> Bin
Drwxr-XR-x 2 root Root 4096 May 7 sys
Drwxr-XR-x 2 root Root 4096 May 7 sysroot
#

Some small, but it is necessary for a program combination in. the/bin directory contains Nash (not a shell, but a script interpretation tool), insmod for loading kernel modules, and LVM.
The initialization file in the root directory is relatively interesting in the directory shown above. These files are generated when the initrd image is decompressed to ram, just as in the traditional Linux Startup Process. We will continue to discuss this issue later.

3. Create an initrd tool.
Now let's go back to the first discussion: how is an initrd image created? In traditional Linux systems, initrd is created during Linux build. Many tools such as mkinitrd can be used to automatically build an initrd for transitioning to a real root file system through necessary libraries and modules. In fact, the mkinitrd tool is a script file, so we can clearly see how this process is implemented. Similarly, the yaird (yet another mkinitrd) tool also allows us to customize the phase at which each initrd is built.

4. Create a custom memory disk for initialization.
Because many Linux-based embedded systems do not have hard drives, initrd can also be used as a permanent root file system. Next I will show you how to create an initrd image. I am using a standard Linux desktop system, so you can follow it even if there is no Embedded Target device. In addition to cross-compilation, the construction process of Embedded Target files is the same.

#! /Bin/bash

# Housekeeping...
Rm-F/tmp/ramdisk. img
Rm-F/tmp/ramdisk.img.gz

# Ramdisk Constants
Rdsize = 4000
Blksize = 1024

# Create an empty ramdisk Image
Dd 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 populate
Mount/tmp/ramdisk. img/mnt/initrd-T ext2-o loop =/dev/loop0

# Populate the filesystem (subdirectories)
Mkdir/mnt/initrd/bin
Mkdir/mnt/initrd/sys
Mkdir/mnt/initrd/dev
Mkdir/mnt/initrd/proc

# Grab busybox and create the Symbolic Links
Pushd/mnt/initrd/bin
CP/usr/local/src/busybox-1.1.1/busybox.
Ln-s busybox Ash
Ln-s busybox Mount
Ln-s busybox echo
Ln-s busybox ls
Ln-s busybox cat
Ln-s busybox PS
Ln-s busybox dmesg
Ln-s busybox sysctl
Popd

# Grab the necessary Dev files
CP-A/dev/console/mnt/initrd/dev
CP-A/dev/ramdisk/mnt/initrd/dev
CP-A/dev/ram0/mnt/initrd/dev
CP-A/dev/null/mnt/initrd/dev
CP-A/dev/tty1/mnt/initrd/dev
CP-A/dev/tty2/mnt/initrd/dev

# Equate sbin with Bin
Pushd/mnt/initrd
Ln-s bin sbin
Popd

# Create the init file
Cat>/mnt/initrd/linuxrc <EOF
#! /Bin/ash
Echo
Echo "simple initrd is active"
Echo
Mount-T proc/proc
Mount-T sysfs NONE/sys
/Bin/ash -- Login
EOF

Chmod + x/mnt/initrd/linuxrc

# Finish up...
Umount/mnt/initrd
Gzip-9/tmp/ramdisk. img
CP/tmp/ramdisk.img.gz/boot/ramdisk.img.gz

To create initrd, you must first create an empty file and use/dev/zero (0 bytes stream) as the ramdisk. IMG input. The size of the obtained file is about 4 MB (consisting of 4000 1 K blocks ). Next, use the mke2fs command to create an ext2 file system that uses this empty file. Now, this file is an ext2 file system. OK. Next, mount the file to/mnt/initrd as a loop device. Now, you have a directory representing the ext2 file system at the Mount point, and store your initrd. Most other script statements are used to implement this function.
The next step is to create some required subdirectories for generating your root file system:/bin,/sys,/dev, And/Pro. Only a few directories are needed, for example, no/lib. However, they already include most of the functions.

If you want your root file system to play a greater role, use busybox. This tool is an image containing many independent tools that you can find in Linux (Ash, A, and so on wk, sed, insmod ). The advantage of busybox is that it brings them together and shares public parts, which greatly reduces the size of the image. This is ideal for embedded systems. Copy the bustbox image from its source directory to your/bin directory. In this way, many symbolic links pointing to the busybox toolset will be created, busybox can determine which tool will be used and automatically reference it. A small set of links created under the/bin directory will be used to support startup scripts.
The next step is to create a small number of special device files. I copied them directly from my/dev folder. Don't forget to add the-A option to keep their original attributes.
The last step is to generate the linuxrc file. After the kernel is mounted with a memory disk, it will search for and execute the relevant startup files. If not, the kernel will use the linuxrc file as its startup script. You 'd better make some basic settings for the environment variables in this file, such as mounting the/proc file system. In addition to/proc, I also mounted the/sys file system to send messages to the terminal. Finally, I call Ash and use it to interact with the root file system. Remember to use chmod to change the attributes of the linuxrc file to executable.
Finally, your root file system is OK. Now, the upload file is not mounted. Use gzipto compress the Upload File and copy ramdisk.img.gz to the/boot directory, so that it can be called by grub.
To create your initial ramdisk, you only need to call mkird and the image will be automatically created and copied to the/boot directory.

5. Test the custom ramdisk initialization.
Your new initrd image is in the/boot directory. Therefore, the next step is to test it with your default kernel. OK. Now you can restart your Linux system. When the grub boot screen appears, press the C key to open the grub command line tool. Now, you can use grub to determine to start a dedicated kernel and initrd image. The kernel command allows you to customize the Kernel File, while the initrd command allows you to specify a specialized initrd image file. After they are all specified, run the startup command to start the kernel, as shown below:

GNU grub version 0.95 (638 K lower/97216 K 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, set = 0x1400, size = 0x29672e]

Grub> initrd/ramdisk.img.gz
[Linux-initrd @ 0x5f2a000, 0xb5108 bytes]

Grub> boot

Uncompressing Linux... OK, booting the kernel.

After the kernel is started, it starts to check whether the initrd image is available. If the answer is OK, load and mount it as the root file system. The following is the end of this special startup process:

...
MD: autodetecting raid Arrays
MD: Autorun
MD:... Autorun done.
Ramdisk: compressed image found at Block 0
VFS: mounted root (ext2 File System ).
Freeing unused kernel memory: 208 K freed
/$ Ls
Bin etc linuxrc proc sys
Dev lib lost + found sbin
/$ CAT/proc/1/cmdline
/Bin/ash/linuxrc
/$ CD Bin
/Bin $ ls
Ash cat echo Mount sysctl
Busybox dmesg ls PS
/Bin $ touch zfile
/Bin $ ls
Ash cat echo Mount sysctl
Busybox dmesg ls PS zfile

After startup, you can use Ash to enter the command mode. In this example, I explored the root file system and demonstrated to you that you can write to this file system by creating a new file. Note that the first step is to create linuxrc.

6. Start by initializing the memory disk
Now we have seen how to build and use a custom memory disk to initialize. This section is used for introduction, how does the kernel identify initrd and mount it as its root file system. I will introduce some main functions in the boot chain and explain the events.
Boot Loader like grub will usually confirm the kernel to be loaded and copy the kernel image to any associated initrd to the memory. You can find it in your Linux kernel source program directory. find the implementation of these functions in the/init subdirectory.
After the kernel and initrd image are decompressed and copied to the memory, the kernel is called. At this time, you start various initialization processes. In the end, you will find yourself in init/Main. C: Init () (subdir/file: function ). This function implements initialization of many subsystems. Here, you need to call init/do_mounts.c: prepare_namespace () to prepare the namespace (mount the dev file system, raid, or MD, devices, and the final initrd ). Call init/do_mounts_initrd.c: initrd_load () to load initrd.
Initrd_load () calls init/do_mounts_rd.c: rd_load_image () to determine whether to load the memory disk image by calling init/do_mounts_rd.c: identify_ramdisk_image. The subsequent function checks the kernel number to determine whether the file is in minux, etc2, romfs, cramfs, or GZIP format. After initrd_load_image is returned, init/do_mounts_rd: crd_load () it is also called. This function is used to allocate space to the memory disk, perform verification calculation, extract, and load the memory disk image to the memory. Now, you have an initrd image suitable for mounting on the block device.
Now, call init/do_mounts.c: mount_root () to mount the device as the root device. OK. The root device is created. The next function is init/do_mounts.c: mount_block_root (). This function also calls fs/namespace. c: sys_mount () to mount the real root file system and perform the chdir operation on it.
Finally, the system returns to the startup function and calls init/Main. C: run_init_process. The result of the call is that the initialization process starts (here it is through/linuxrc ). Linuxrc can be an executable program or a script (as long as the script interpreter can interpret it normally ).
The hierarchy of function calls can be seen from the following table. Not all functions related to copying, attaching, and initializing memory disks are listed. Here, we will only give a rough review of the overall basic process:
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

7 applications without disk startup
As with the startup of many embedded systems, local disks (soft drives or optical drives) are not necessary for starting the kernel and memory disk root file systems. DHCP tools can be used to confirm network parameters, such as IP rejection and subnet mask. In addition, TFTP can be used to transmit kernel images and initial memory disk images to local devices. Once the transmission is complete, the Linux kernel can be started and mounted with initrd, which is the same as the local image startup process.

8. Make your initrd as small as possible
When you are building an embedded system, you always want the initrd image to be as small as possible. Well, here are some tips. The first step is to use busybox. As mentioned above, busybox contains many large tools, usually measured in MB, but it has to control its volume within the range of several hundred kb.
In this example, the busybox image uses a static link, so no library file is required. However, if you need to get the standard C library file to meet your binary program, you have other better options besides the large glibc library. First, the small-sized uclibc library is specifically used for space-constrained, and the Standard C library's shrinking version. Dietlib is another library applicable to the environment with spatial restrictions. Remember, you need to recompile your binary program with these libraries in your embedded system. Although using them will bring about some additional work, it is worth it.

9 Summary
The initial purpose of the memory disk Initialization Technology was to allow the kernel to transition to the final root file system through a temporary root file system. Initrd is also useful for Embedded Linux systems: it can be used to mount a non-persistent root file system to a memory disk.

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.