Linux中的Ramdisk与Initrd
时间:2007-03-03 来源:wwling2001
Linux中的Ramdisk与Initrd
Ramdisk简介
先简单介绍一下ramdisk,Ramdisk是虚拟于RAM中的盘(Disk)。对于用户来说,可以把RAM disk与通常的硬盘分区(如/dev/hda1)同等对待来使用,例如:
redice # mkfs.ext2 /dev/ram0
mke2fs 1.38 (30-Jun-2005)
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
2048 inodes, 8192 blocks
409 blocks (4.99%) reserved for the super user
First data block=1
1 block group
8192 blocks per group, 8192 fragments per group
2048 inodes per group
Writing inode tables: done
Writing superblocks and filesystem accounting information: done
This filesystem will be automatically checked every 24 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
redice # mount /dev/ram0 /mnt/rd
redice # ls /mnt/rd
lost+found
redice # mount
/dev/hda2 on / type ext3
proc on /proc type proc (rw)
/dev/ram0 on /tmp/xxx type ext2 (rw)
当然,Ramdisk与硬盘分区有其不同的地方,例如RAM disk不适合作为长期保存文件的介质,掉电后Ramdisk的内容会随内存内容的消失而消失。Ramdisk的其中一个优势是它的读写速度高,可以被用 作需要高速读写的文件。但在2.6版本后,Ramdisk的这一作用开始被tmpfs(Virtual memory file system support)取代。
回到上面的例子,我们格式化了一个ramdisk(/dev/ram0)并且将其mount到/mnt/rd目录下,那么这个Ramdisk有多大呢?先看一下:
redice # df -h /dev/ram2
Filesystem 容量 已用 可用 已用% 挂载点
/dev/ram0 7.8M 1.0K 7.4M 1% /mnt/rd
从上面的信息看出,ramdisk有大约7.8M的可用空间。我们再试一下另外的文件系统,重新格式化成minix分区并挂接试一下:
redice # umount /mnt/rd
redice # mkfs.minix /dev/ram0
2752 inodes
8192 blocks
Firstdatazone=90 (90)
Zonesize=1024
Maxsize=268966912
redice # mount /dev/ram0 /mnt/rd
redice # df -h /dev/ram0
Filesystem 容量 已用 可用 已用% 挂载点
/dev/ram0 8.0M 1.0K 8.0M 1% /mnt/rd
现在看出来了,的确是8M(这同时说明,EXT2文件系统本身要占用一定的存储空间,相比之下minix文件系统要少些),这个空间是在编译核心时就确定下来了,在配置Ramdisk时,有一个叫 Default RAM disk size 的参数决定默认情况下Ramdisk的大小。可以通过核心命令行参数(ramdisk_size)来改变这个值,例如要设置Ramdisk的大小为16M,在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_size=16384
initrd /initrd
这样,Ramdisk的大小就变成16M了。这个参数是Ramdisk直接编译到核心时才能使用的,如果Ramdisk编译为模块,则应该使用模块参数来设置Ramdisk的大小:
redice # insmod rd rd_size=16384
编译到核心时,可以通过下面的一些核心命令行参数来配置Ramdisk:
当以模块的形式译时,模块支持以下几个加载参数:
|
initrd
上面已经提到,Ramdisk需要先格式化然后理能使用。那么,如果核心希望使用ramdisk该如何做呢?于是initrd产生了,initrd全称是 initial RAM disk ,它提供一种让核心可以简单使用Ramdisk的能力,简单的说,这些能力包括:
-
格式化一个 Ramdisk;
-
加载文件系统内容到Ramdisk;
-
将Ramdisk作为根文件系统;
我们可以将initrd形像的比作Norton Ghost备 份的硬盘分区,而Linux启动阶段的Ramdisk相当于一个未格式化的硬盘分区,核心可以直接将initrd的内容释放到一个未初始化的 Ramdisk里,这个过程与Ghost恢复一个分区的过程十分相似。于是,相应的内容被加载到相应的Ramdisk中,同时,这个Ramdisk也被格 式化成某种由initrd格式所表达的分区格式。
initrd与Ghost备份的分区有许多相似之处,例如,它有一定的大小,包含分区上的文件系统格式等。initrd支持的格式包括:
-
Ext2文件系统;
-
Romfs文件系统;
-
cramfs文件系统;
-
minix文件系统;
如果核心选择了Gzip支持(通常这是默认的,在init/do_mounts_rd.c中定义的BUILD_CRAMDISK宏)还可以使用Gzip压缩的initrd。相关的代码可以在核心源码 drivers/block/rd.c:identify_ramdisk_image 中找到。
制作initrd
制作initrd传统的作法是通过软盘(显然过时了,就不介绍了)、ramdisk或loop设备(/dev/loop)。通过ramdisk来制作的方法比较简单(以ext2文件系统为例):
redice # mkfs.ext2 /dev/ram0
redice # mount /dev/ram0 /mnt/rd
redice # cp _what_you_like_ /mnt/rd # 把需要的文件复制过去
redice # dd if=/dev/ram0 of=/tmp/initrd
redice # gzip -9 /tmp/initrd
这个过程也最能够解释initrd的本质,对于Linux来说,Ramdisk的一个块设备,而initrd是这个块设备上所有内容的“克隆”(由 命令dd来完成)而生成的文件。核心中加载initrd相关的代码则用于完成将相反的过程,即将这一个文件恢复到Ramdisk中去。
通过loop设备来制作initrd的过程:
redice # dd if=/dev/zero of=/tmp/initrd bs=1024 count=4096 # 制作一个4M的空白文件
redice # losetup /dev/loop0 /tmp/initrd # 映射到loop设备上;
redice # mkfs.ext2 /dev/loop0 # 创建文件系统;
redice # mount /dev/loop0 /mnt/rd
redice # cp _what_you_like_ /mnt/rd # 复制需要的文件;
redice # umount /mnt/rd
redice # losetup -d /dev/loop0
redice # gzip -9 /tmp/initrd
不过,现在已经有了一些更好的工具来完成这些工作,包括genromfs(uClinux里常用的工具),genext2fs,mkcramfs 等。这些工具提供了一些方便开发的新特性,例如,不需要上面烦索的过程,只要将文件复制到某个目录中,将其作为根目录,即可生成initrd;另一个重要 的改进是,这些工具都可以以普通用户的身份来生成initrd。
未完,待续…(补充有关如何加载|ARM中如何使用initrd作为根文件系统等)
链接和参考文档
文档
-
Linux文档中关于ramdisk的介绍,核心目录里 Documentation/ramdisk.txt;
-
Linux文档中关于initrd的介绍,核心目录 Documentation/initrd.txt;
-
Linux文档中关于tmpfs的介绍,核心目录 Documentation/filesystems/tmpfs.txt;
资源
How to use a Ramdisk for Linux

Abstract:
This article shows how to use RAM as a virtual harddisk.
Introduction to RamDisk
This is a brief article about how to setup a RamDisk on a RedHat 6.0 system. It should be very similar for other Linux distributions.What is a RamDisk? A RamDisk is a portion of memory that you allocate to use as a partition. Or, in other words, you are taking memory, pretending to treat it as a hard drive, and you are saving your files to it. Why would you want to use a RamDisk? Well, if you know that certain files you have are constantly going to be used, putting the files into memory will increase the performance of your computer since your memory is faster than your hard drive. Things like web servers with lots of data can be sped up this way. Or, if you are insane, and you have a PII 550 Mhz computer with 1 gig of memory and an old 500 meg hard drive, you can use it just to increase your hard drive space. Then again, if you want an almost diskless machine, it might not be that crazy afterall.
Here are some more resources to help you.
- http://metalab.unc.edu/LDP/HOWTO/Kernel-HOWTO.html
- http://metalab.unc.edu/LDP/HOWTO/mini/LILO.html
- /usr/src/linux/Documentation/ramdisk.txt
How to use RamDisk
Well, it is very easy to use a ramdisk. First of all, the default installation of RedHat 6.0 comes with ramdisk support. All you have to do is format a ramdisk and then mount it to a directory. To find out all the ramdisks you have available, do a "ls -al /dev/ram*". This gives you the preset ramdisks available to your liking. These ramdisks don't actually grab memory until you use them somehow (like formatting them). Here is a very simple example of how to use a ramdisk.# create a mount point:Those three commands will make a directory for the ramdisk , format the ramdisk (create a filesystem), and mount the ramdisk to the directory "/tmp/ramdisk0". Now you can treat that directory as a pretend partition! Go ahead and use it like any other directory or as any other partition.
mkdir /tmp/ramdisk0
# create a filesystem:
mke2fs /dev/ram0
# mount the ramdisk:
mount /dev/ram0 /tmp/ramdisk0
If the formatting of the ramdisk faild then you might have no support for ramdisk compiled into the Kernel. The Kernel configuration option for ramdisk is CONFIG_BLK_DEV_RAM .
The default size of the ramdisk is 4Mb=4096 blocks. You saw what ramdisk size you got while you were running mke2fs. mke2fs /dev/ram0 should have produced a message like this:
mke2fs 1.14, 9-Jan-1999 for EXT2 FS 0.5b, 95/08/09Running df -k /dev/ram0 tells you how much of that you can really use (The filesystem takes also some space):
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 blocks per group, 8192 fragments per group
1024 inodes per group
>df -k /dev/ram0
Filesystem 1k-blocks Used Available Use% Mounted on
/dev/ram0 3963 13 3746 0% /tmp/ramdisk0
What are some catches? Well, when the computer reboots, it gets wiped. Don't put any data there that isn't copied somewhere else. If you make changes to that directory, and you need to keep the changes, figure out some way to back them up.
Changing the size of the ramdisks
To use a ram disk you either need to have ramdisk support compiled into the Kernel or you need to compile it as loadable module. The Kernel configuration option is CONFIG_BLK_DEV_RAM . Compiling the ramdisk a loadable module has the advantage that you can decide at load time what the size of your ramdisks should be.
Okay, first the hard way. Add this line to your lilo.conf file:
ramdisk_size=10000 (or ramdisk=10000 for old kernels)
and it will make the default ramdisks 10 megs after you type the "lilo" command and reboot the computer. Here is an example of my /etc/lilo.conf file.
boot=/dev/hdaActually, I got a little over 9 megs of usable space as the filesystem takes also a little space.
map=/boot/map
install=/boot/boot.b
prompt
timeout=50
image=/boot/vmlinuz
label=linux
root=/dev/hda2
read-only
ramdisk_size=10000
When you compile ramdisk support as loadable module then you can decide at load time what the size should be. This is done either with an option line in the /etc/conf.modules file:
options rd rd_size=10000or as a command line parameter to ismod:
insmod rd rd_size=10000Here is an example which shows how to use the module:
- Unmount the ramdisk mounted in the previous chapter, umount /tmp/ramdisk0 .
- Unload the module (it was automatically loaded in the previous chapter), rmmod rd
- Load the ramdisk module and set the size to 20Mb, insmod rd rd_size=20000
- create a file system, mke2fs /dev/ram0
- mount the ramdisk, mount /dev/ram0 /tmp/ramdisk0
Example of how to use a RamDisk for a webserver.
Okay, here is an example of how to use 3 ramdisks for a webserver. Let us say you are 99% confident that your default installation of Apache for RedHat 6.0 won't use more than 9 megs for its cgi-scripts, html, and icons. Here is how to install one.First, issue this command to move the real copy of the document root directory of your webserver to a different place. Also, make the directories to mount the ramdisks .
mv /home/httpd/ /home/httpd_realThen, add these commands to the start procedure in your /etc/rc.d/init.d/httpd.init (or where ever the httpd gets started on your system):
mkdir /home/httpd
mkdir /home/httpd/cgi-bin
mkdir /home/httpd/html
mkdir /home/httpd/icons
### Make the ramdisk partitions
/sbin/mkfs -t ext2 /dev/ram0
/sbin/mkfs -t ext2 /dev/ram1
/sbin/mkfs -t ext2 /dev/ram2
### Mount the ramdisks to their appropriate places
mount /dev/ram0 /home/httpd/cgi-bin
mount /dev/ram1 /home/httpd/icons
mount /dev/ram2 /home/httpd/html
### Copying real directory to ramdisks (the
### data on the ramdisks is lost after a reboot)
tar -C /home/httpd_real -c . | tar -C /home/httpd -x
### After this you can start the web-server.
Comments
- Please remember one thing, BACKUP YOUR DATA if you change it and you need it. When the computer reboots, any changes are lost.
A cron job should do it. Have it check every 10 minutes and see if any files have changed and backup any changes. Another thing you could do is make your changes to the real directory, and then copy over the changes to the ramdisks. That is much safer. - A cool use of this would be to have a computer with 1 gig of memory and then use 256 megs for "/tmp". If you have lots of processes that use "/tmp", it should help speed up your system. Also, anything in /tmp would get lost when the computer reboots, which can be a good thing.
- Linux uses all the memory that is not in use by programs as a unified disk-cache but my experience is that ramdisks give you despite that still some speed increase.