Article Title: Use RamDisk in Linux. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
1. What is RamDisk?
As the name suggests, Ram: memory, Disk: Disk, RamDisk refers to using part of your memory space to simulate a hard Disk partition. However, this is not very useful today when hard disks are getting cheaper and cheaper, but now the memory is getting cheaper. For some situations where we want to make the access speed very high, you can try it.
2. Create a RamDisk
In fact, creating a RamDisk is very simple. Because RedHat Linux 6.0 supports RamDisk by default, you only need to format A RamDisk, and mount it to a directory.
1. Check all available ramdisks:
Ls? Al/dev/ram *
This will list your available ramdisks, which do not occupy memory until you format them.
2. Create a RamDisk:
Mke2fs/dev/ram0
Run the following command:
Mke2fs 1.14, 9-Jan-1999 for EXT2 FS 0.5b, 95/08/09
Linux ext2 filesystem format
Filesystem label =
1024 inodes, 4096 blocks
204 blocks (4.98%) reserved for the super user
First data block = 1
Block size = 1024 (log = 0)
Fragment size = 1024 (log = 0)
1 block group
8192 blocerp marketing director experiences ks per group, 8192 fragments per group
1024 inodes per group
If this operation fails, it may be because your kernel does not support ramdisk. In this case, you only need to reconfigure and compile the kernel. Set CONFIG_BLK_DEV_RAM to Enable during configuration.
3. mount it to use:
Mkdir/tmp/ramdisk0 create a directory
Mount/dev/ram0/tmp/ramdisk0 mount it to the directory you just created
In this way, we can use the newly created RamDisk --/tmp/ramdisk0
In addition, run the df command to check the RamDisk size:
> Df-k/dev/ram0
Filesystem 1k-blocks Used Available Use % Mounted on
/Dev/ram0 3963 13 3746 0%/tmp/ramdisk0
Note that this part of memory will be refreshed when the system restarts. Therefore, do not place any uncopied data in this region. If you have modified the Directory and need to keep the changes, you can back up the directory in some ways. That is to say, this directory can only be used to temporarily store data to speed up access.
3. Create an appropriate RamDisk
We have created a RamDisk but cannot specify the size. Now we will learn how to create a RamDisk with the proper size.
1. Use LILO Configuration:
Add a line to the lilo. conf file:
Ramdisk_size = 10000 (write ramdisk = 10000 if the old kernel version is used)
Then run/sbin/lilo to generate a new LILO.
In this way, after you restart the computer, the default ramdisk size will be 10 MB.
2. Use/etc/conf. modules to configure:
If your kernel compiles ramdisk as a module, you can determine the size of the ramdisk during loading. That is, you can modify the/etc/conf. modules option settings to add:
Options rd rd_size = 10000
Of course, you can also specify the parameter in the command line to implement ismod:
Insmodrd rd_size = 10000
When the above two methods are used to create an appropriate ramdisk, we also need to execute the following command to use it:
Mke2fs/dev/ram0
Mkdir/tmp/ramdisk0
Mount/dev/ram0/tmp/ramdisk0
When not in use, execute:
Umount/tmp/ramdisk0
If insmod is used to load the module, run the following command:
Rmmod rd
4. An instance using RamDisk
Next, we will describe the application through an instance that uses Ramdisk as a WEB server.
1. First move the WEB server to another place
Mv/home/httpd // home/httpd_real
Mkdir/home/httpd
Mkdir/home/httpd/cgi-bin
Mkdir/home/httpd/html
Mkdir/home/httpd/icons
2. Add the following command to/etc/rc. d/init. d/httpd. init:
/Sbin/mkfs-t ext2/dev/ram0
/Sbin/mkfs-t ext2/dev/ram1
/Sbin/mkfs-t ext2/dev/ram2
Mount/dev/ram0/home/httpd/cgi-bin
Mount/dev/ram1/home/httpd/icons
Mount/dev/ram2/home/httpd/html
Tar-C/home/httpd_real-c. | tar-C/home/httpd-x
In this case, you can update the httpd_real directory instead of the httpd directory when updating data. Otherwise, all updates will be lost when the system restarts. You 'd better set up a cro with a flight attendant to introduce the n process on the day of diversity, so that it can monitor whether httpd_real has changed and copy it to ramdisk as soon as there is a change.
5. Use RamDisk for/tmp directory
I also want to recommend a cool method. If you have too much memory, you can use part of it as the/tmp directory, which will greatly increase the execution speed of your system. In addition,/tmp will be deleted when the system is restarted.