Posts Tagged ‘debian ramdisk synching rsync sync logfile flash flashd’

Putting /var in ram

March 13th, 2008 3 comments

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 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/
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/ {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 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.