In the Zentyal project we automatize tests with ANSTE. This tool is able to deploy complex scenarios with several virtual machines installed during the process. As you can imagine, the creation of lots of different images is a heavy task. To speed it up we make use of RAM disks, a feature offered by Linux and other operative systems to create filesystems directly over RAM, that are tens of times faster that physical disks.

Our testing machine, a quad-core computer, was blessed since it’s very first day with 16GB of RAM to be able to deploy a very broad spectrum of scenarios with virtual machines, routers, networks and so, the type of complex scenarios in which Zentyal servers are involved every day. We dedicated 12 of its 16GB to RAM disk, for that we used tmpfs, a file system that can be directly mounted in any Linux in any moment, you only need to specify the size during mount time, the module reserves the space and includes its own file system, avoiding the need to format the virtual disk. All the configuration needed to use it, is to add this line to /etc/fstab:

tmpfs           /tmp    tmpfs   size=12G,nr_inodes=120k 0       0

But we had a problem with this system, from time to time we had to reboot the machine because the RAM disk seemed to be full, although it wasn’t.

After some time we bought a second testing machine, with very similar specs, only to increase the availability of this valuable resource so our developers can use the free time of the new machine to test in complex scenarios while programming new features or to quickly mimic the cases involved in reported issues.

In this machine we decided to test raw RAM disks. The main differences between these disks and the ones created with tmpfs is that they don’t include any filesystem, (so you need to format them before mount) and that they don’t check if there is enough space in RAM for them, so if you are not aware, you can take all the space (by the kernel) blocking the machine.

The first thing you need to do to use this technology is to add a parameter to the kernel that specifies what is the size of the disks. In Ubuntu Server, or in any other Linux system with grub 2.0 you can do that by editing /etc/default/grub adding the option ramdisk_size=12000000 to the variable GRUB_CMDLINE_LINUX:

GRUB_CMDLINE_LINUX="ramdisk_size=12000000"

Then you need to format the disk and mount it during boot time, for that we have implemented a simple upstart task (e.g. /etc/init/mounttmp.conf):

description     "Mount tmp filesystems on boot"

start on startup

expect daemon

task

script
    mke2fs -m 0 /dev/ram0
    mount /dev/ram0 /tmp
    chmod a+rwxt /tmp
end script

As you can see, we format the virtual disk with ext2 file system. We chose this option because it’s a very well known one, but especially because it doesn’t have journaling, a very cool feature to avoid disasters when you have precious data in your physical disks, but totally useless when you are caching files in temporal storage.

So far we haven’t had any problem with RAM disks and both options have very similar performance, so we have finally discarded tmpfs and configured our first testing server to use RAM disk also.

Although we use it for testing, this is only an example of what can be done with temporal RAM disks. You can really apply this solution to speed up almost any application that makes heavy use of cached data stored in physical filesystems without any single change in the implementation, and, as a last note, there are also very interesting uses of RAM disks with union filesystemsbut that is another story.

A post by Jaime Soriano