Linux INITRD detailed __linux

Source: Internet
Author: User

Author: EasyLife http://www.mike.org.cn/blog/index.php?load=read&id=635

In the Linux operating system, there is a special feature-Initialize the memory disk INITRD (INITial Ram disk) technology, and the kernel supports compressed file system images. With these two features, we can start the Linux system from a small initialization memory disk and mount part of the system memory as the root file system.

RAMDisk is to allocate part of memory as a partition and use it as a hard disk. For programs that are constantly used by the system, placing them in RAMDisk will speed up the operation of the computer, such as large data network servers, diskless workstations, and so on. In order to be able to use RAMDisk, we have to compile the kernel in the block device in the RAMDisk support selected, it also has two options below, one is to set the RAMDisk size, the default is 4096k, and the other is to set the default number. If you want to use INITRD, you have to choose the support. It can be directly compiled into the kernel, or can be compiled into modules, when needed to load. We have to use it when we start it, so we have to compile it directly into the kernel.

The following is the 2.6 kernel selection path for the module:

Linux Kernel Configuration
-> Device Drivers
->block devices
->ram Block Device Support
->default Number of RAM disks (set RAMDisk, default is 16)
->default RAM disk Size (Kbytes) (set RAMDisk size, default is 4096k)


Linux Kernel Configuration
->general Setup
->inital RAM filesystem and RAM disk (INITRAMFS/INITRD) support

If the support for RAMDisk has been compiled into the kernel, we can use it. First, create the directory RAM under the/MNT directory, run the Mkdir/mnt/ram, then create the file system for/DEV/RAM0, run MKE2FS/DEV/RAM0, and finally Mount/dev/ram, run Mount/dev/ram0/mnt/ram, You can manipulate it like a normal hard disk. It is noteworthy that when creating the file system, the screen output 1024 inodes, 4096 blocks, that is, RAMDisk size is 4m=4096 block, but after we mount, with the command Df–k/dev/ram view, show that the RAMDisk size is only 3963K, because the file system itself takes up some space. (This space is determined by the default RAM disk size (Kbytes) when compiling the core)

We can change the size of the RAMDisk according to need. If we want to increase the default 4M to 8 m, when RAMDisk is directly compiled into the kernel, you can add ramdisk=8192 to the Grub configuration file grub.conf, and after you run grub, the RAMDisk size becomes 8M After you restart the computer.

For example, to set the size of the RAMDisk to 8M, you can use 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

So the size of the RAMDisk becomes 16M. This parameter is RAMDisk directly to the core to use, if RAMDisk compiled into a module, you should use the module parameters to set the size of the RAMDisk:

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

Options Rd rd_size=8192,

b, in the Load Rd module is followed by a description, that is, Insmod Rd rd_size=8192.

# Insmod Rd rd_size=8192

When compiling to the core, you can configure RAMDisk with some of the following core command-line parameters:

The size of the Ramdisk_size-ramdisk (Kbytes);
RAMDisk-the same effect as ramdisk_size;
Ramdisk_blocksize-ramdisk block Size, default is 1024;

When translating as a module, the module supports the following load parameters:

Rd_size-ramdisk_size or RAMDisk parameters with the above;
Rd_blocksize-ramdisk_blocksize with the above;

Or the start is as the starting line parameter ramdisk=8192;

Creating INITRD RAMDisk Images

As mentioned above, RAMDisk needs to be formatted before it can be used. So what if the core wants to use RAMDisk? So INITRD produced, INITRD full name is initial RAM disk, it provides a core can be simple to use ramdisk ability, in short, these capabilities include:

Format a Ramdisk;
Loading file system contents to RAMDisk;
RAMDisk as the root file system;

We can compare the INITRD image to the Norton Ghost Backup hard disk partition, while the Linux boot phase RAMDisk is equivalent to an unformatted hard disk partition, the core can release INITRD content directly into an uninitialized RAMDisk, This process is very similar to the process of recovering a partition ghost. As a result, the corresponding content is loaded into the corresponding RAMDisk, and the RAMDisk is also formatted as a partition format expressed in INITRD format.

There are many similarities between INITRD and ghost backup partitions, for example, it has a certain size, contains the file system format on the partition, and so on. INITRD supported formats include: EXT2 file system, Romfs file system, Cramfs file system, Minix file system, if gzip support is selected for core (usually this is the default, INIT/DO_MOUNTS_RD.C defined in Build_ Cramdisk macros) can also use gzip-compressed INITRD. The relevant code can be found in the core source drivers/block/rd.c:identify_ramdisk_image.

Make INITRD
 
There are two main formats for INITRD: The traditional RAMDisk and Cpio format (the advantage of this format is that kernel primitives do not require additional file system support)

The traditional practice of making initrd is through floppy disks (apparently outdated, not introduced), RAMDisk or loop devices (/dev/loop). The method made by RAMDisk is relatively simple (take the ext2 file system as an example):

Through RAMDisk

# MKFS.EXT2/DEV/RAM0
# MOUNT/DEV/RAM0/MNT/RD
# CP _what_you_like_/MNT/RD # Copy the required files to the past
# dd If=/dev/ram0 OF=/TMP/INITRD
# gzip-9/TMP/INITRD

This process also best explains the nature of the INITRD, and for Linux, a RAMDisk block device, and INITRD is the file generated by the "clone" of all content on the block, which is done by the command DD. The code associated with loading INITRD in the core is used to complete the reverse process, which restores the file to the RAMDisk.

The process of making initrd through the loop device:

DD If=/dev/zero of=/tmp/initrd bs=1024 count=4096 # Make a 4M blank file
LOSETUP/DEV/LOOP0/TMP/INITRD # Mapped to the loop device;
MKFS.EXT2/DEV/LOOP0 # Create file system;
Mount/dev/loop0/mnt/rd
CP _what_you_like_/MNT/RD # copy required files;
Umount/mnt/rd
Losetup-d/dev/loop0
Gzip-9/TMP/INITRD

The process of making initrd through Cpio:

Cd/path/to # to the directory of files that need to be copied
Find. |cpio-o-H newc |gzip-c >. /initrd.gz

However, there are some better tools to do this, including GENROMFS (uclinux common tools), Genext2fs,mkcramfs, MKINITRD, etc. These tools provide new features that are easy to develop, such as a process that does not require a bug on the top, as long as the file is copied into a directory and used as the root directory to generate INITRD; Another important improvement is that these tools can generate INITRD as a normal user.

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.