Home > Debian > Putting /var in ram

Putting /var in ram

Well there you have it. I wanted to save some more power on my debian server. So I installed a flash disk and copied my debian install from hd to it.

After that I made my hd spin down automatically by using hdparm.

At the moment flash-memory is limited to ~10.000 write/erase cycles. By spreading the writes to disk the manufacturers try to avoid this problem. With a 24/7 server writing regularly to log files this might become a problem on the long run. That’s why I decided to put /var completely into memory by using a ramdisk. Note that you should not do/use this when you are running enterprise critical applications where a system crash might result in serious data loss.

First things first. First create a directory where we will persist our /var directory to in case of shutdown/reboot. I created a directory var-bak for this.

mkdir /var-bak

I then copied the /var directory to this /var-bak directory with cp -a.
After that I removed the /var-bak/run directory which should not be stored.

cp -a /var/* /var-bak/
rm -rf /var-bak/run

Ok, so now we have a copy of our contents in /var. Let’s mount the ramdisk. And copy the contents of /var-bak back to the ramdisk.

mount -t ramfs ramfs /var
cp -a /var-bak/* /var/

Ok there we have it. It’s all set up now. Using this method might cause some trouble. Try a df command to see what I mean.

router:~# df -h /var
Filesystem            Size  Used Avail Use% Mounted on
ramfs                    0     0     0   -  /var

That’s right it lists 0 as available space. Programs which perform space checks might report an ‘insufficient diskpace’ error. I had to fix the mysql init script to ignore this.

To be able to copy and synch the disks automatically on startup/shutdown/reboot I created a init script.
I called it ramdisk.sh and placed it in the /etc/init.d directory.

RSync makes sure the /var-bak directory keeps correctly synched with the /var directory. Also it makes sure the run directory is ignored during the synch process.

#! /bin/sh
# /etc/init.d/ramdisk.sh
case "$1" in
    echo "Copying files to Ramdisk"
    cp -a /var-bak/* /var/
    echo [`date +"%Y-%m-%d %H:%M"`] Ramdisk Synched from HD >> /var/log/ramdisk_sync.log
    echo "Synching files to Harddisk"
    echo [`date +"%Y-%m-%d %H:%M"`] Ramdisk Synched to HD >> /var/log/ramdisk_sync.log
    rsync -av --delete --exclude=run/ --exclude=run/** /var/ /var-bak/
    echo "Synching logfiles to Harddisk"
    echo [`date +"%Y-%m-%d %H:%M"`] Ramdisk Synched to HD >> /var/log/ramdisk_sync.log
    rsync -av --delete --exclude=run/ --exclude=run/** /var/ /var-bak/
    echo "Usage: /etc/init.d/ramdisk.sh {start|stop|sync}"
    exit 1
exit 0

After that I wanted it to be started as early as possible. So I placed it as early in the process as possible.
Maybe this still needs some more tweaking but this works ok for me at this point. You can use the sync command to manually sync the ramdisk to disk.

update-rc.d ramdisk.sh defaults 00 99

That’s it. You could optionally run a cron job to synch the ramdisk every once in a while if you like. It will at least save quite some write cycles.

  1. November 3rd, 2009 at 22:01 | #1

    Thank you very much for your article!
    Very helpful!
    As addition can only say that you also should put a symlink in /etc/rcS.d right after mountall.sh (in my case it has S35, so name it for example S36ramdisk.sh), and also put it in /etc/fstab, together with /tmp, which is also making a bit if a write cycles.
    Here are the lines:

    tmpfs /tmp tmpfs defaults,noatime,mode=1777 0 0
    tmpfs /var tmpfs defaults,noatime,mode=1777 0 0

    tmpfs is dynamic, while ramfs is static, so if you are not tight on ram you should use tmpfs for /var to not to freeze your system.
    Hope that helps.

  2. gcb
    December 1st, 2012 at 05:00 | #2

    you have some encoding issues on the source…

    also a patch so debian distros wont complain:
    # Provides: ramdiskvar
    # Default-Start: 2 3 4 5
    # Default-Stop: 0 1 6
    # Short-Description: ramdisk var permancency handler
    # Description: see http://famvdploeg.com/blog/2008/03/putting-var-in-ram/

    just add anywhere i think.


  3. December 1st, 2012 at 13:54 | #3


    Tnx for the heads up. Maybe I should also note that it would be a good idea to boot into rescue-mode/single-user mode when setting this whole thing up. I might need to give this a bit of an update to make it more ‘up-to-date’.

    By the way: what encoding issues are you referring to?

  1. October 15th, 2010 at 18:21 | #1
  2. June 2nd, 2022 at 02:29 | #2

Time limit is exhausted. Please reload CAPTCHA.