Linux RAM Disk: Creating A Filesystem In RAM

by on March 5, 2010 · 23 comments· last updated at March 5, 2010

Software RAM disks use the normal RAM in main memory as if it were a partition on a hard drive rather than actually accessing the data bus normally used for secondary storage such as hard disk. How do I create and store a web cache on a RAM disk to improve the speed of loading pages under Linux operating systems?

You can create the ram disk as follows (8192 = 8M, no need to format the ramdisk as a journaling file system) :
# mkfs -q /dev/ram1 8192
# mkdir -p /ramcache
# mount /dev/ram1 /ramcache
# df -H | grep ramcache

Sample outputs:

/dev/ram1              8.2M   1.1M   6.7M  15% /ramcache

Next you copy images or caching objects to /ramcache
# cp /var/www/html/images/*.jpg /ramcache
Now you can edit Apache or squid reverse proxy to use /ramcache to map to images.example.com:

 
<VirtualHost 1.2.3.4:80>
     ServerAdmin admin@example.com
     ServerName images.example.com
     DocumentRoot /ramcache
     #ErrorLog /var/logs/httpd/images.example.com_error.log
     #CustomLog /var/logs/httpd/images.example.com_access.log combined
</VirtualHost>
 

Reload httpd:
# service httpd reload

Now all hits to images.example.com will be served from the ram. This can improve the speed of loading pages or images. However, if server rebooted all data will be lost. So you may want to write /etc/init.d/ script to copy back files to /ramcache. Create a script called initramcache.sh:

#!/bin/sh
mkfs -t ext2 -q /dev/ram1 8192
[ ! -d /ramcache ] && mkdir -p /ramcache
mount /dev/ram1 /ramcache
/bin/cp /var/www/html/images/*.jpg /ramcache

Call it from /etc/rc.local or create softlink in /etc/rc3.d/
# chmod +x /path/to/initramcache.sh
# echo '/path/to/initramcache.sh' >> /etc/rc.local

A Note About tmpfs

tmpfs is supported by the Linux kernel from version 2.4+. tmpfs (also known as shmfs) is a little different from the Linux ramdisk. It allocate memory dynamically and by allowing less-used pages to be moved onto swap space. ramfs, in contrast, does not make use of swap which can be an advantage or disadvantage in many cases. See how to use tmpfs under Linux.



You should follow me on twitter here or grab rss feed to keep track of new changes.

Featured Articles:

{ 23 comments… read them below or add one }

1 jaysunn March 5, 2010 at 1:09 pm

This is very interesting. I am going to show this to one of my colleuges in regards to this benifiting us.

Great Tutorial.

jaysunn

Reply

2 Dariusz March 5, 2010 at 1:22 pm

/dev/ram1 seem not exist on Suse based systems:

linux-jgbr:/home/dpanasiuk # ls /dev/ra*
/dev/random
linux-jgbr:/home/dpanasiuk # cat /etc/issue
Welcome to openSUSE 11.2 “Emerald” – Kernel \r (\l).

linux-jgbr:/home/dpanasiuk #

Reply

3 Vivek Gite March 5, 2010 at 1:38 pm

@Dariusz
Try

mknod /dev/ram1 b 1 1
chown root:disk /dev/ram1
chmod 0660 /dev/ram1
mkfs -t ext3 -q /dev/ram1 8192
mkdir -p /ramcache
mount /dev/ram1 /ramcache
df -H

Let me know…

Reply

4 Sharad December 7, 2011 at 6:22 am

How to unmount RAM ??

Reply

5 pp April 1, 2012 at 5:56 pm

It works in SuSE 11.4

Reply

6 Swen Schroder March 5, 2010 at 1:41 pm

Could you please compare the speed with serving the files from disk? I believe this is completely useless – as the RAM disk will only steal memory which would otherwise be used as file system cache, which will be quiet as efficient as serving the files directly from RAM!

Reply

7 tiptop March 5, 2010 at 1:47 pm

@Swen, Why not run ab against such a setup? I guess you trust kernel too much.

On a related note, you can use ramdisk to improve privacy too.

Reply

8 Diggity March 5, 2010 at 2:00 pm

why use a journaled filesystem for the ramdisk?

Reply

9 Vivek Gite March 5, 2010 at 2:04 pm

@Diggity, ignore -t ext3 but you are right on spot.

@ Swen, let face it disk i/o going to be slow as compare to your ram. Not all projects are same, so it may or may not produce results at the end of day. YMMV.

Reply

10 Dariusz March 5, 2010 at 2:13 pm

@Vivek Gite

Your instructions for Suse (and I suppose for any other Linux which doesn’t have /dev/ram1 created by default) worked.

dpanasiuk@linux-jgbr:~> uname -a
Linux linux-jgbr 2.6.31.12-0.1-default #1 SMP 2010-01-27 08:20:11 +0100 i686 i686 i386 GNU/Linux
dpanasiuk@linux-jgbr:~> df -H | grep ram
/dev/ram1 8.2M 1.1M 6.7M 15% /ramcache
dpanasiuk@linux-jgbr:~>

cheers

Reply

11 Ravi Chandra March 6, 2010 at 6:12 am

Super post.

Reply

12 Boris March 6, 2010 at 11:21 am

On Debian, I tryed to do this :
mkfs -q /dev/ram1 97468
(arround 96M).
I guess this alert :
# mkfs -q /dev/ram1 97468
mkfs.ext2: Filesystem larger than apparent device size.
Proceed anyway? (y,n)

I don’t know if it’s really dangerous.

Reply

13 Vivek Gite March 6, 2010 at 1:24 pm

Pass ramdisk_size=N option to linux kernel. You need to edit grub.conf and pass that option. Once done reboot the system. This command is added to the kernel line in grub. N must be in K size. See
http://www.cyberciti.biz/files/linux-kernel/Documentation/ramdisk.txt

Reply

14 Tapas Mallick March 6, 2010 at 2:16 pm

Nice Post as usual :)
if we unmount the ram disk, will it still reserve RAM or release it for system’s normal use ?

Reply

15 Vivek Gite March 6, 2010 at 2:29 pm

@ Tapas,

Short answer – No it will not release it.

Long answer – once memory has been allocated to the ramdisk, it can not be freed until you reboot the system. It will get flagged so that the Linux kernel will not try to reuse the memory again. Therefore, you cannot reclaim the RAM after you are done with using the ramdisk. If you remount the ramdisk, your data will still be there.

cp /path/something.txt /ramdisk
umount  /ramdisk
mount /dev/ram1  /ramdisk
ls /ramdisk/something.txt

HTH

Reply

16 Amitabh Patnaik March 11, 2010 at 4:13 pm

This rocks! Thanks for sharing : )

Reply

17 Borys Łącki March 22, 2010 at 5:04 pm

# mkfs -q /dev/ram1 8192

# hdparm -tT /dev/{hda,ram1}

/dev/hda:
Timing cached reads: 1512 MB in 2.02 seconds = 750.01 MB/sec
Timing buffered disk reads: 154 MB in 3.03 seconds = 50.76 MB/sec

/dev/ram1:
Timing cached reads: 1350 MB in 2.00 seconds = 674.96 MB/sec
Timing buffered disk reads: 16 MB in 0.03 seconds = 561.60 MB/sec

Reply

18 chester April 16, 2010 at 4:31 am

to use memory as storage and still allow the kernel to reuse the memory, you can use ramfs or the newer tmpfs

just mount a directory
mount -t tmpfs tmpfs /dir

if you don’t fill up the space in the tmpfs, the kernel gets to use the free RAM for buffers and cache. If you delete a file, the kernel will reclaim the ram, and if you umount the directory, all data is lost

Reply

19 Steffen Schaumburg July 26, 2010 at 1:18 am

The /dev/ram method didn’t work for me, even after running mknod. I probably deselected a necessary kernel option. But the tmpfs method is working great so far, thanks :)

Reply

20 Jon Doe September 16, 2010 at 9:50 pm

“How do I create and store a web cache on a RAM disk to improve the speed of loading pages under Linux operating systems?”

Linux is a kernel, commonly used together with the GNU operating system instead of Hurd. In that case the OS is GNU with Linux or GNU/Linux.

Reply

21 Robert Grubba August 30, 2011 at 9:43 am

Thanks for Your article, it sounds really neat and I will try & test performance boost from Your solution soon.

I’m thinking of putting my entire wordpress directory on ramdisk (and in the future also database files). For the wordpress files part it seems quite easy to do – entire directory would be on ramdisk with rsync syncing it with backup folder on harddisk (lets say in 30 minutes cycles).
Or maybe there is a chance of setting apache to write to one location (harddisk) and serve files from another one (ramdisk which would be rsync’ed from harddrive in that case)?

But how to manage syncing of database files form ram to local filesystem? As far as I remember I shouldn’t rsync db files when database server is running (or maybe mysqlhotcopy would do the job?)… how to cope with unexpected power-downs – I can create shutdown script syncing database after shutting down database server, but it will not manage any restarts from hardware/power failures in that way.

Any advice would be welcome.

Reply

22 Marco July 5, 2012 at 8:03 pm

How can I erase the filesystem in /dev/ram1?

Reply

23 Guest April 6, 2013 at 11:10 am

Ramdisk/tmpfs is kind of last resort. It’s almost never needed. Linux kernel uses all the free RAM for file cache by default, so all the frequently accessed files remain cached in RAM. Short-lived temporary files may stay in cache and not hit the disk at all. Linux kernel is smart. :)

There’re just a few cases when ramdisk/tmpfs is much faster than real filesystem (and most of them can be reported as a bug). Usually there’re better alternatives.

For example to reduce number of disk write attempts you can increase vm.dirty_* sysctls (http://www.kernel.org/doc/Documentation/sysctl/vm.txt). If you’re trying to reduce number of HDD spinups for a laptop, then vm.laptop_mode and laptop-mode-tools package would give you the best solution. If your software uses a lot of slow fsync’s that you need to avoid, then `eatmydata` wrapper is just for you.

Ramdisk takes your RAM from other apps even when not used, and often it’s not easy to keep it in sync with your local data. The only way to make sure that you need it is to benchmark it, not with some general benchmarking tool, but with your particular test case.

Reply

Leave a Comment

You can use these HTML tags and attributes for your code and commands: <strong> <em> <ol> <li> <u> <ul> <kbd> <blockquote> <pre> <a href="" title="">

Tagged as: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,

Previous Faq:

Next Faq: