A new method for implementing embedded Linux-ramdisk

Source: Internet
Author: User
Introduction 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 features, we can enable Linux to initialize memory disks from an early stage.


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 use part of the system memory as the root file system.
And does not use swap partitions (if you do not run X
Windows), that is, to completely embed the Linux system into the memory, without relying on any other hard disk. Currently, the PC memory is at least 128 MB, while the root file system only uses
30 m, so not only will the performance of the entire machine be reduced, but it will be greatly improved.

Because the system does not work on the hard disk, the system eliminates the problem caused by the mechanical drive. Because the system runs in the memory, the root file system and operations are completely in the CPU/Ram environment, system Performance at speed
And reliability is very good; it will not damage the file system because of the illegal shutdown, because each time we start, we extract the compressed file system to the memory disk and mount it as the root file system.

1. Hardware requirements

For such a system, hardware does not need to be specially designed, but is only implemented through components on a common PC. It is worth mentioning that the system memory size should be at least 64 MB. Because 30 m is used
Ramdisk is used, and the remaining 30 mb is used as the system to ensure normal operation of the system. The current computer memory is generally 128 MB, and this condition can be met. The only special one is
Flash disk, which is equivalent to a hard disk with an IDE interface, with a size of 20 mb. It is mainly used to start Lilo and store the root file system compressed package.

2 ramdisk usage

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 running, placing them on ramdisk will speed up computer operations, such as large
Network servers with data volumes and diskless workstations. To be able to use ramdisk, we need
The ramdisk in device can be selected. There are two options below. One is to set the ramdisk size, which is 4096 kb by default, and the other is supported by initrd. It can
It can be directly compiled into the kernel, or compiled into modules and loaded as needed. Since we use it at startup, We must compile it directly into the kernel.

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/Ram; mount/dev/Ram and run Mount/dev/RAM/mnt/Ram to operate it like a general hard disk. It is worth noting that when creating a file system, 1024 is output on the screen

Inodes, 4096 blocks, that is, when the ramdisk size is 4 m = 4096 blocks, but after we mount the block, run the DF-k/dev/Ram command to view it, it is displayed that the ramdisk size is only 3963 kb, because the file system occupies some space.

We can change the ramdisk location size as needed. If we want to increase the default 4 m to 10 m, when ramdisk is directly compiled into the kernel, you can configure the file in lilo
Add a line to Lilo. conf: append = "ramdis_size = 10000". After lilo is run, after the computer is restarted, The ramdisk size changes to 10 MB,
Or ramdis_size = 10000 as the startup line parameter; When ramdisk is compiled as a load-able module, You need to load the configuration file in the module
Add one line to/etc/modules. conf: Options Rd
Rd_size = 10000, or add a description after loading the RD module, that is, insmod RD rd_size = 10000.

3. Implementation Process

3.1 create a compressed file system

Our implementation process depends on the existing Linux system. First, start a general Linux system. In this system, create a suitable File System Based on your needs. For example
For Apache network servers, you only need to leave the commands, scripts, configuration files, and library functions required to complete system startup and basic maintenance, plus the ones required to implement the Apache server. Details

Create a directory/minlinux in Linux, where we create a file system. All static link libraries, man pages, and Info
Pages), header files, kernel source code is completely useless for system operation, so they are not needed. Some basic tools for system maintenance in the directory bin, such as LS, MV, grep,
Chown, chmod, chgrp, LN, RM, etc. In sbin, commands are usually required during system startup, such as Bash, e2fsck, mke2fs, fdisk,
Insmod, rmmod, depmod, modprobe, lsmod, shutdown, reboot, login, init, Getty,
Mount, umount, and so on; put Apache Application HTTP and some other special tools under usr/bin. Then, based on the dynamic link library required by these executable files
Lib directory. Of course, the configuration files under the etc directory and the device files under Dev must all be available. They all correspond to executable files, because when many executable files are executed
Open the device and run it according to the configuration file. There is a special directory where proc should be set. In the kernel compilation option file system selection, we choose to support the file system proc, then run it in the system
There is a lot of content below it, which is generated by constantly tracking the status of the system kernel and running processes in real time, but does not occupy any disk space, but resides in the memory. In some cases
System settings, many tools get information from here, such as dmesg, PS, top, etc.

After the file system is created, the size should be around 20 mb.

Next, copy the file system to ramdisk to prepare for generating the ramdisk image file. We changed the system's ramdisk to 30 m. After restarting the computer, we performed the following operations:

Dd If =/dev/Zero of =/dev/Ram BS = 1 k count = 30000 adjust ramdisk to zero, so that a higher compression ratio is followed;

Mke2fs-M0/dev/Ram 30000 create a 30 m ext2 File System on ramdisk;

Mount/dev/RAM/mnt/Ram mount the formatted ramdisk to the directory/mnt/Ram;

CP-Av/minlinux/*/mnt/Ram copies the file structure to ramdisk.

Then we modify the files in the/mnt/RAM/etc directory. The main file is fstab, which is responsible for transferring information about the file system to be mounted to the startup process during system startup.
Use ramdisk as the root file system and do not need to swap partitions. Therefore, this file is configured

/Dev/RAM/ext2 defaults 1 1

None/proc defaults 0 0

You can. In general, swap partitions must be activated when the system is started, that is, the Swapon-a command is in the startup script, but we do not need to swap partitions, so we need to remove this item, otherwise, the system prints an error message and stops the startup.

Finally, we need to copy the ramdisk image and compress it. The main steps are as follows:

Run DF. Note that the value of/dev/Ram in the 1024-blocks column is 25600 on my machine;

Uninstall/dev/Ram, run CD/root to switch to the root directory, and run umount/dev/Ram;

Write ramdisk as an image file and run dd If =/dev/Ram of = ram30.img BS = 1 k count = 25600.

Compress and upload a compressed image file ram30.img.gz in the/rootdirectory,

Run gzip-9 V ram30.img

3.2 Create an initrd ramdisk Image

First, create an ext2 File System in/dev/ram0 that is suitable for the size. The method is the same as above, but the size is only 2048 K, because initrd ramdisk is used to guide (bootstrap) 30 m ramdisk, and mount it to/mnt/Ram.

Then, create the directories and files required for the image in/dev/ram0. Create the directory bin, Dev, etc, Lib, MNT, And the executable script file linuxrc. The content of linuxrc is:

#! /Bin/bash

Mount-o-RO/dev/hda1 // MNT # mount the flash disk to/MNT in read-only mode

Zcat/mnt/boot/ram30.img.gz>/dev/Ram # decompress the root file system image to ram

Umount/dev/hda1 # detach a flash disk


#! /Bin/bash

Mount-o-RO/dev/hda1 // boot # mount the/dev/hda1/disk to/boot in read-only mode

Zcat ram30.img.gz>/dev/Ram # extract the root file system image to ram

Umount/dev/hda1 # detach a flash disk

In bin, the commands used in linuxrc are used. Lib is the dynamic link library required for these commands. In etc, the configuration file lD. So. conf is used to find the required dynamic link during command running.
Library path, run the command ldconfig-R
/Mnt/Ram generates the file lD. So. cach, which establishes a correspondence between the command and the dynamic link library. The basic terminal device and the device used in linuxrc under Dev:
Console, Ram, null, Ty, tty1, tty2, and hda1; mnt is the mount point of hda1.

Finally, create the compressed initrd ramdisk image.

Run DF to check the number of bytes in/dev/ram0, 1684 K on my computer;

Convert the current directory to/root and uninstall/dev/ram0 umount/dev/ram0;

Copy/dev/ram0 to image file dd If =/dev/ram0 of = initrd. img bs = 1 k count = 1684;

Upload the compressed image file initrd.img.gz and run gzip-9 V initrd. IMG.

3.3 start

To start the system, you must use a flash disk and use LILO to write the system boot information to the primary boot area of the flash disk.

Use the flash disk as the first master disk hda, and use the hard disk with a common Linux disk as the first slave disk HDB and start from it. Create an ext2 File System on the flash drive and mount it
/Mnt/flash directory. Create a boot directory on the flash disk, copy the compressed file system and initrd image to the boot directory, and copy the Linux kernel and boot Area
Record boot. B, boot area ing map copy to it. Create the Lilo configuration file ramlilo. conf in a common Linux system. The configuration file is as follows:

Boot =/dev/hda

Map =/mnt/flash/boot/Map

Install =/mnt/flash/boot. B


Timeout = 50
Image =/mnt/flash/boot/vmlinuz

Append = "ramdisk_size = 30000"

Label = embedded

Root =/dev/Ram

Initrd =/mnt/flash/boot/initrd.img.gz

Run the command lilo-C ramlilo. conf to write the startup information to the Master Boot zone MBR of flash.

Restart the computer and run Mount after login. We can see the following two items:

/Dev/Ram on/ext2 (RW)

None on/proc type proc (RW)

This shows that only ramdisk is mounted and created successfully.

4 Conclusion

In the embedded Linux system we have created, various operations are performed, which are fast and stable. The system does not enter the manual maintenance interface due to damage to the root file system. Use it
HTTP network servers, network monitors, broadband communication device managers, and other machines that require continuous operation for a long time all have high application value.

Dd If =/dev/Zero of =/dev/Ram BS = 1 k count = 2048

Mke2fs-vm0/dev/Ram 2048

Create an ext2 File System of 2 Mb on ramdisk;





Dd If =/dev/Ram BS = 1 k count = 2048 | gzip-V9>/tmp/ram_image.gz


1 K-block
Available in use % mount point

26571532 5003036 20196948 20%/

124427 45584
72419 39%/boot

0 257312 0%/dev/SHM

10154020 176964 9452936 2%

6198404 1558524 4325008 27%/mnt/LFS

/Dev/ram1 2011 1671 340 84%/mnt/Ram

Copy search

Copy search
Related Article

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.