Linux Kernel details Linux initrd

Source: Internet
Author: User

In Linux, there is a special feature-initializing the memory disk initrd (initial RAM disk) technology, and the kernel supports compressed file system images. With these two functions, we can enable the Linux system to initialize the memory disk from an early stage and mount part of the system memory as the root file system.

Ramdisk allocates a portion of the memory as a partition and uses it as a hard disk. For programs that are constantly used during system operation, placing them on ramdisk will speed up computer operations, such as network servers with large data volumes and diskless workstations. In order to be able to use ramdisk, We must select ramdisk support in block device during kernel compilation. There are two options below. One is to set the ramdisk size. The default value is 4096 K; the other is to set the default number. If you want to use initrd, You have to select it. It can be directly compiled into the kernel, compiled into modules, and loaded as needed. Since we use it at startup, We must compile it directly into the kernel.

The following is the path of the 2.6 kernel to the module:

Linux Kernel configuration
-> Device Drivers
-> Block devices
-> Ram block Device Support
-> Default number of RAM disks (set the number of ramdisks. The default value is 16)
-> Default RAM disk size (Kbytes) (set the ramdisk size. The default value is 4096 K)

Linux Kernel configuration
-> General setup
-> In‑ram filesystem and RAM disk (initramfs/initrd) Support

If ramdisk support has been compiled into the kernel, we can use it. First, create the directory RAM under the/mnt directory, run mkdir/mnt/Ram, then create a file system for/dev/ram0, and run mke2fs/dev/ram0; finally, Mount/dev/Ram and run Mount/dev/ram0/mnt/Ram to operate it like a general hard disk. It is worth noting that, when creating a file system, 1024 inodes and 4096 blocks are output on the screen, that is, the ramdisk size is 4 MB = 4096 blocks. However, after mounting, run DF-K
When viewing/dev/Ram, it is displayed that the ramdisk size is only 3963 kb, because the file system occupies some space. (This space is determined by default RAM disk size (Kbytes) during core compilation)

We can change the ramdisk size as needed. For example, we want to increase the default size of 4 m to 8 M. When ramdisk is directly compiled into the kernel, the grub configuration file can be used. add ramdisk = 8192 to the conf file. After grub is run, restart the computer and the ramdisk size changes to 8 Mb.

For example, to set the ramdisk size to 8 m, you can use the following in GRUB:

# Grub. conf-
Default = 0
Timeout = 10
Splashimage = (hd0, 0)/GRUB/splash.xpm.gz
Title redice Linux
Root (hd0, 0)
Kernel/vmlinuz Ro root = label =/HDC = ide-SCSI ramdisk = 8192
Initrd/initrd

In this way, the ramdisk size becomes 16 Mb. This parameter can be used only when ramdisk is directly compiled to the core. If ramdisk is compiled as a module, you should use the module parameter to set the ramdisk size:

A. Add a line to the module load configuration file/etc/modules. conf:

Options rd rd_size = 8192,

B. Add a description after loading the RD module, that is, insmod RD rd_size = 8192.

# Insmod RD rd_size = 8192

When compiling to the core, you can use the following core command line parameters to configure ramdisk:

Ramdisk_size-ramdisk size (Kbytes );
Ramdisk-the role of ramdisk_size is the same as that of ramdisk_size;
Ramdisk_blocksize-ramdisk block size. The default value is 1024;

When translated as a module, the module supports the following loading parameters:

Rd_size-ramdisk_size or ramdisk parameter above;
Rd_blocksize-same as ramdisk_blocksize;
 
Or ramdisk = 8192 as the startup line parameter;

Create an initrd ramdisk Image

As mentioned above, ramdisk needs to be formatted before it can be used. So what if the core wants to use ramdisk? As a result, initrd came into being. initrd stands for initial RAM disk, which provides the ability of the core to simply use ramdisk. In short, these capabilities include:

Format a ramdisk;
Load file system content to ramdisk;
Use ramdisk as the root file system;

We can compare the initrd image to the hard disk partition backed up by Norton Ghost, while the ramdisk in the Linux Startup phase is equivalent to an unformatted hard disk partition, the core can directly release initrd content to an uninitialized ramdisk, which is very similar to the process of restoring a partition on the ghost. Therefore, the corresponding content is loaded into the corresponding ramdisk. At the same time, this ramdisk is also formatted into a partition format expressed by the initrd format.

Initrd and ghost backup partitions have many similarities. For example, they have a certain size, including the file system format on the partition. The formats supported by initrd include: ext2 file system, romfs file system, cramfs file system, minix file system, and gzip support if the core is selected (this is usually the default, the build_cramdisk macro defined in init/do_mounts_rd.c) can also use gzip compressed initrd. The related code can be found in drivers/block/RD. C: identify_ramdisk_image.

Make initrd
 
Initrd has two main formats: the traditional ramdisk and cpio formats (the advantage of this format is that the kernel native does not need additional File System Support)

The traditional method of creating initrd is to use a floppy disk (obviously outdated, not introduced), ramdisk, or loop device (/dev/loop ). Ramdisk is a simple method (taking the ext2 file system as an example ):

Ramdisk

# Mkfs. ext2/dev/ram0
# Mount/dev/ram0/mnt/RD
# Cp _ what_you_like _/mnt/RD # copy the desired file
# Dd If =/dev/ram0 of =/tmp/initrd
# Gzip-9/tmp/initrd

This process can also best explain the nature of initrd. For Linux, ramdisk is a block device, and initrd is a "clone" of all the content on this block device (completed by the command dd) and the generated file. The initrd-related code loaded in the core is used to complete the opposite process, restoring the file to ramdisk.

The process of creating initrd using the loop device:

Dd If =/dev/Zero of =/tmp/initrd BS = 1024 COUNT = 4096 # create a 4 m blank File
Losetup/dev/loop0/tmp/initrd # ing to the loop device;
Mkfs. ext2/dev/loop0 # create a file system;
Mount/dev/loop0/mnt/RD
CP _ what_you_like _/mnt/RD # copy the desired file;
Umount/mnt/RD
Losetup-D/dev/loop0
Gzip-9/tmp/initrd

The process of creating initrd through cpio:

CD/path/to # directory of the file to be copied
Find. | cpio-o-h NEWC | gzip-C> ../initrd.gz

However, there are already some better tools to do this, including genromfs (commonly used tools in uClinux), genext2fs, mkcramfs, and mkinitrd. These tools provide some new features for convenient development. For example, if you do not need to copy the file to a directory and use it as the root directory, you can generate initrd; another important improvement is that these tools can generate initrd as normal users.

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.