Posts Tagged ‘ramdisk’

how to mount a ram hard disk in Linux

Monday, February 1st, 2010

In this post, i will show you how to create a very fast, 0 latency hard drive from the extra gigabyte or 2 on your system

1- You need to change the boot options to allow this…

Since i use debian lenny, my boot options look like this in /boot/grub/menu.lst

title        Debian GNU/Linux, kernel 2.6.26-2-686
root        (hd0,1)
kernel        /boot/vmlinuz-2.6.26-2-686 root=/dev/sda2 ro quiet
initrd        /boot/initrd.img-2.6.26-2-686

We need to append ramdisk_size=1572864 if we wanted a 1.5GB RAM drive, make sure you have 1.5GB EXTRA on your system, let’s say you must have a minimum of 2GB for the system to run using the 512MB you are leaving the system with

title        Debian 1.5GB Ramdisk, kernel 2.6.26-2-686
root        (hd0,1)
kernel        /boot/vmlinuz-2.6.26-2-686 root=/dev/sda2 ro quiet ramdisk_size=1572864
initrd        /boot/initrd.img-2.6.26-2-686

You are done for now, if you want to format and use it do this

/sbin/mkfs.ext2 /dev/ram0

Remember that you need to format as ext2 and not ext3, i have seen people format the hard drive as EXT3, why would you ever need Journaling on a RAM disk that is already volatile (Deleted when you reboot)

mkdir /ramdisk
mount /dev/ram0 /ramdisk

You can now get creative and format or load a disk image at boot time, in any case, the RAM is only allocated to the disk when you use it, but you really don’t need to know that to use your new RAM hard disk

NOTE: I chose to explain RAM disk because later on i will show you what advantages we can get from having a block level device, you can surely do this without modifying boot options by simply using tempfs or RAMFS that give you an instant ram disk, but it is not a block level device in that case

Testing Speed

So, you want to see how fast it is, but for that we need a data source that can push this thing to the maximum, the answer is /dev/zero that responds with a stream of zeros, so let us write a 1.3GB file to our new volatile hard disk

dd if=/dev/zero of=/ramdisk/pathtoimage.img bs=1M count=1300

On my computer, this took about 4.54006 seconds.