Linux RAM Disk: Creating A Filesystem In RAM

Posted on in Categories , last updated 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

     ServerAdmin [email protected]
     DocumentRoot /ramcache
     #ErrorLog /var/logs/httpd/images.example.com_error.log
     #CustomLog /var/logs/httpd/images.example.com_access.log combined

Reload httpd:
# service httpd reload

Now all hits to 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

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/
# echo '/path/to/' >> /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.

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin and a trainer for the Linux operating system/Unix shell scripting. He has worked with global clients and in various industries, including IT, education, defense and space research, and the nonprofit sector. Follow him on Twitter, Facebook, Google+.

31 comment

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

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

    linux-jgbr:/home/dpanasiuk #

      1. if you mounted the ram like this mount /dev/ram1 /mnt/ram1 then right click on folder /mnt/ram1 and select unmount. that simple, on puppy linux anyway. Contents of ram will still be there until remount or shutdown.

  2. 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!

  3. @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.

  4. @Vivek Gite

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

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


  5. 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.

  6. @ 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


  7. # mkfs -q /dev/ram1 8192

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

    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

    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

  8. 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

  9. 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 :)

  10. “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.

  11. 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.

    1. there are several ways;
      one is make a new file system; mke2fs -t ext2 /dev/ram1 or ext3, fat32 or ?
      another is to use dd if=/fs.img of=/dev/ram1
      you could also write ones or zeros to it……

  12. 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 ( 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.

  13. add to kernel boot option in grub; ramdisk_size=409600 # for 400mb ramdisk;

    kernel /vmlinuz ramdisk_size=409600 #or how much you want

    this states the max amount of ram for ramdisk but doesn’t actually use up ram until you write to the ramdisk! Actually i run a 6gb ramdisk.

    after boot in terminal enter;

    mke2fs -t ext2 /dev/ram1 409600

    or ‘mkfs’ this formats a ramdisk ‘ram1’ with ext2 filesystem and fixes the max size (make smaller but not bigger, although if you have enough ram you can make several, ram2, ram3 etc)
    next in term mount the ramdisk ‘ram1′

    mount /dev/ram1 /mnt/ram1
    mount /dev/ram1 /mnt/dvd #i have no dvd so it is available. or /mnt/’your choice’

    you can edit /etc/fstab to automount and specify fs at boot up.
    I hope someone finds this useful. I use it on puppy linux fatdog64 and the totally amazing Lighthouse64 distros.

  14. Folks –
    Before accepting this as a solution to performance or other problem you face, please be sure to benchmark the “with” and “without” this method.

    Borys’ hdparm benchmark _is_ insightful (if perhaps a bit too concise that some folks didn’t understand it).

    If your aim is to improve I/O performance, a RAM disk will usually NOT provide the answer you seek because the Linux kernel itself contains a “RAM disk of sorts” by implementing file caching in RAM.

    So for example: You have a webserver with images which is hosted on a slow volume.

    With a RAM disk: If you access the file once, it’s “slow” but then the file is cached by the kernel so all further reads are faster.

    With RAM disk: The first time you access the file, it _seems_ faster. but this is misleading because really this is your “second” read attempt and it’s already cached by the kernel. The first read occurred when you copied the file from disk to RAM. This explains why the RAM filesystem is almost the same speed multiple reads to (cached) disk.

    You could achieve fast disk caching by having an INIT script which simply does a “cat /some/file > /dev/null” and therefore the file ends up in cache. This trick is actually quite useful for things like proxies or file mounts hosted on slow remote NAS devices. Once you have “read” the files in some kind of preloader script, you spare your first visitor any penalty for first access.

    The invention of fast disk caching has eliminated the performance need for RAM disks (which used to be so common in the 80’s and early 90’s, and made obsolete with utilities like DOS “smartdrv” or OS like Linux and *BSD).

    That said this is a fun trick to try out. And you might very well have a need for files which “disappear” when the OS is powered off. You can also use this with ssh-keygen if you wanted to output the keys somewhere without touching the local filesystem (an obscure application to be sure, but one I was investigating).

Leave a Comment