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.
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: mkdir /tmp/ramdisk0 # create a filesystem: mke2fs /dev/ram0 # mount the ramdisk: mount /dev/ram0 /tmp/ramdisk0
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.
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/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 blocks per group, 8192 fragments per group
1024 inodes per group
Running df -k /dev/ram0
tells you how much of that you can really use (The filesystem takes also some space):
>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.
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/hda map=/boot/map install=/boot/boot.b prompt timeout=50 image=/boot/vmlinuz label=linux root=/dev/hda2 read-only ramdisk_size=10000
Actually, I got a little over 9 megs of usable space as the filesystem takes also a little space.
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=10000
or as a command line parameter to ismod:
insmod rd rd_size=10000
Here is an example which shows how to use the module:
umount /tmp/ramdisk0
. rmmod rd
insmod rd rd_size=20000
mke2fs /dev/ram0
mount /dev/ram0 /tmp/ramdisk0
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_real mkdir /home/httpd mkdir /home/httpd/cgi-bin mkdir /home/httpd/html mkdir /home/httpd/icons
Then, 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):
### 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.