Wednesday, August 23, 2006

RAM disk in linux

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)
root (hd0,4)
kernel /boot/vmlinuz-2.6.11-1.1369_FC4 ro root=LABEL=/1 ramdisk_size=512000 quiet
initrd /boot/initrd-2.6.11-1.1369_FC4.img
title Windoze XP
rootnoverify (hd0,1)
chainloader +1

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 :

mkdir /mnt/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.


Anonymous said...

How can i know the space left on a ramdisk, it doesn't appear with df ?

Jayant Kumar said...

once you mount the RAMDISK as a normal drive, all your commands run as they run on a normal drive.

So after mounting ramdisk using mount command, a "df -h" should show you the space used and left on this drive.

Anonymous said...

Is it possble to determine where in the physical memory is the RAM disk located?

sathiya said...

I have 2 GB RAM, i am unable to create more than 512 MB RAM DISK...

If it is not possible to create, what should i do to make that possible....

Jayant Kumar said...

you need to pass the ramdisk_size=<size in kb> option in the kernel to change the size of the ramdisk.

Let me know the kernel version if this does not work "uname -r"

Jaxx said...

I have the same problem
my kernel version is (suse 10.3):

I want to add that I have a verry large sql database that I want to copy onto the ramdisk during boot.. it will be used only for reading data so no problem if it gets lost during reboot.. the database is about 5 gigs and sofar I only have 2 gigs of ram so another question: is it even possible to install 8gigs for suse and use 6 gigs for a ramdisk ?

South Indian said...

Hope you are reading My post.
yesterday I did a iso install of Ubuntu from harddisk.
what I wanted to know is,If a bootable Linux Cd is made(editing iso removing some less used packages for eg) ,say a size of 350MB-400MB.I have 2 GB RAM.

So,If I use grub(of a hdd installed distro) to boot this iso to RAMDISK and later ,boot from the RAM_DISK:
^^^Is this possible.

Thanks and Nice Blog

Arun KT said...

Hi Jayanth,

Thank you for the information. It is very useful.