Ramdisks

From Linux 101, The beginner's guide to all things Linux.

Jump to: navigation, search

Well ramdisks are not really commonly used by beginners, but this is a cool topic to discuss and let's you see a little bit of the power of Linux.

If you use a bootable CD-rom based distribution of linux, such as knoppix, then you may be using these (though it is an option you have to tell it to use).

The basic idea to a ramdisk: take a chunk of memory and treat it as if it were a harddisk or some other permanent storage device. You may be wondering why this could be useful, so let's go back to the knoppix CD as an example.

You are running knoppix on a 1 CD-ROM computer that has plenty of memory. You want to boot into Knoppix Linux, then put in another CD to pull off some data or something else important. You can't just boot the knoppix CD, hit the eject button, and then put in the other CD. As soon as you try to run a new command from the knoppix CD, it will attempt to look for the knoppix CD and then not find it. At this point it would be critically useful if you could load the entire knoppix CD into memory, then eject the CD, and put in your personal CD.

Well, thanks to ramdisks, we have this potential.

Ramdisks could be used in other capacities to speed up operations, depending on how you set up your solution to a problem and how it is bottlenecked by hard drive read-writes or not. Some distributions also use ramdisks for starting up the computer.

[edit] Making Ramdisks

First, to even use ramdisks you need the correct kernel configuration:

  • RAMdisk support

Then, you can follow these commands:

# dd if=/dev/zero of=/dev/ram bs=1k count=2048
# mke2fs -vm0 /dev/ram 2048
# mount -t ext2 /dev/ram /mnt/ramdisk
# cp -av preparedfiles /mnt/ramdisk
# umount /mnt/ramdisk
# dd if=/dev/ram bs=1k count=2048 | gzip -v9 ramdisk.gz

Here is the process:

  1. The dd command is used to flood the new 2 Megabyte (1k blocksize * 2048 blocks = 2MB) with zeros. This is a size-saving idea. (Remember: before the RAM is actually used, it could have been used by another process and have bits set to 1 throughout it.)
  2. Next you create a filesystem onto this memory with mke2fs. This example uses ext2, the basic, common filesystem of Linux. If you were to use a journaled filesystem (ext3, reiserfs) then you would need much more memory, so ext2 is a wise choice.
  3. The mount command tells the system to now place this filesystem into the / root filesystem so now we can write to it.
  4. The cp command copies our files that we have already prepared into this filesystem.
  5. The umount locks out the filesystem from further writes.
  6. The dd command here lets us read the raw data from RAM (the 0's and 1's) and then pipe it into gzip and compress it into a file.

This process is sort of a cool spoof. We pretend a piece of memory is a block device like a hard drive or floppy. We can then create a filesystem on it and write files. Then later we can pull the raw data from the memory and write it into a file.

[edit] Reading ramdisks

Kernel configuration option needed:

  • Loopback device support

Next we (may) want to read from the newly created ramdisk.gz file. So, we want to read from a file that contains the raw filesystem (ext2) in this case data. We've done this before, with loopback devices only we used the CD-ROM iso9660 filesystem format instead of ext2. Well, we can use the same process to read the filesystem.

# gunzip ramdisk.gz
# mount -o loop ramdisk /mnt/ramdisk

Ramdisks are a very cool utility to allow you more potential with your computer hardware. At the cost of memory, we can create new filesytems and the system can use them.

Personal tools