A RAM disk is a portion of RAM which is being used as if it were a disk drive. RAM disks have fixed sizes, and act like regular disk partitions. Their access time is much faster for a RAM disk than for a real, physical disk. However, any data stored on a RAM disk is lost when the system is shut down or powered off. RAM disks can be a great place to store temporary data which needs to be accessed frequently.
For RAM disk to be accessed, the support for the same should be compiled with the linux kernel. As far as i remember Kernel 2.4.* and 2.6.* have RAM disk support compiled in it.
RedHat/Fedora creates 16 ram disks by default. Although they are not usable by default. The size of these ram disks is 4096K (4 MB).
To list available RAM disks use the command – ls -lh /dev/ram*.
To know default size of RAM disk use – dmesg | grep RAMDISK
As it can be seen, using the default configuration only 4 MB RAMDISK can be created. To create larger RAMDISKS you need to configure the kernel parameters so that larger RAMDISKS can be created. How ?? By simply passing the size of the RAMDISK as a parameter during booting of the kernel.
The kernel option for RAMDISK size is: ramdisk_size=<size in kb>.
All you have to do is type this at the boot prompt during booting. Or if you are using some boot loader like lilo or grub, you need to change the boot command in either lilo.conf or grub.conf. For example in grub.conf you would be having something like
# grub.conf generated by anaconda
# Note that you do not have to rerun grub after making changes to this file
# NOTICE: You do not have a /boot partition. This means that
# all kernel and initrd paths are relative to /, eg.
# root (hd0,4)
# kernel /boot/vmlinuz-version ro root=/dev/hda5
# initrd /boot/initrd-version.img
title Fedora Core (2.6.11-1.1369_FC4)
kernel /boot/vmlinuz-2.6.11-1.1369_FC4 ro root=LABEL=/1 ramdisk_size=512000 quiet
title Windoze XP
The above configuration file would create ramdisks of size 512MB. There is an upper limit to the max size of RAMDISK that can be created and it is also controlled by the amount of available RAM. So if you have 512MB of RAM, you would not be able to create RAMDISKS of more than 512MB. But depending on the kernel configuration, if you have a 4GB RAM system, you may not be able to create RAMDISK of more than 1GB maybe. You will have to check this limit either with the kernel documents or by trying out different sizes at boot prompt.
Once the RAMDISK size has been configured, all that needs to be done is to create a file system on the RAMDISK and then mount the RAMDISK as a file system directory in linux.
To create an ext2fs file system, the following command is used :
mke2fs -m 0 /dev/ram0
The -m 0 option keeps mke2fs from reserving any space on the file system for the root user. This makes the complete ramdisk space available to any regular user.
To mount the ramdisk :
mount /dev/ram0 /mnt/RAMDISK
Run mount to check whether the RAMDISK has been mounted.
Now the RAMDISK can be used as a regular directory. You can read, write, delete and modify files on the RAMDISK as if you are working on a regular hard disk. Linux would handle it as if it were handling a regular directory on the disk. The difference between RAMDISK and normal DISK would be invisible to a regular user.
The only problem is that if the system is either rebooted or crashes. In that case, all data in the RAMDISK is lost. You will need to recreate the RAMDISK and redo all that has been done.
The benefit of using RAMDISk is that reads and writes ot the RAMDISK would be extremely fast, since everything is in RAM and not on the DISK. To prevent loss of data, it is advisable to keep a backup of data in RAMDISK on the local HDD.
The commands used to create the RAMDISK and loading of data in RAMDISK can be automated by issueing the command in the rc.local or some other startup script.
More details of effectively creating and using RAMDISKS can be obtained from the kernel documentation.