[ Content | View menu ]

Fun with Ramdisks

Yesterday I read a few posts on the Arch forums about which filesystem was best for pacman speed, and today I read a thread about mounting a ramdisk (ie: a block of RAM used like a harddrive). My brain-lightbulb came on, and I wondered how Pacman would perform if I loaded all the files into RAM in rc.local, and saved them to disk again in rc.local.shutdown. Of course, there is the risk that a total system lockup or loss of power would lose the files forever, and I’d probably have to reinstall everything to get Pacman working again, but I feel that is a risk I am willing to take for a slight performance upgrade.

My first challange was to create a ramdisk to work from. I decided that using ramfs was the best option, as I have 2GB of RAM and barely ever go over about 500MB or 600MB of usage. if you don’t have a lot of free RAM you may want to consider using tmpfs which dips into Swap when your RAM starts to run out. Anyway, I edited my /etc/fstab and came up with this:

#
# /etc/fstab: static file system information
#

none        /dev/pts     devpts  defaults              0 0
none        /dev/shm     tmpfs   defaults              0 0

#/dev/cdrom /mnt/cdrom   iso9660 ro,user,noauto,unhide 0 0
#/dev/dvd   /mnt/dvd     udf     ro,user,noauto,unhide 0 0
#/dev/fd0   /mnt/fd0     vfat    user,noauto           0 0

/dev/sda1   /mnt/media   jfs     defaults              0 0
/dev/sdb1   /boot        ext2    defaults              0 1
/dev/sdb2   swap         swap    defaults              0 0
/dev/sdb3   /            ext3    defaults              0 1
/dev/sdb4   /home        ext3    defaults              0 1

none        /tmp         ramfs   defaults              0 0
none        /mnt/ramdisk ramfs   defaults              0 0

I then moved the files out of /tmp to /tmp-backup, made the /mnt/ramdisk folder, and got it all working with ‘mount -a’. I then copied the files back in place and all was fine. I did have to restart X originally because I forgot to make /tmp writeable for anyone but root, and so nothing graphical could start up. I fixed that by going to vc/1 and chmodding it.

After getting a ramdisk to work from, I moved my attention to rc.local. I knew that it would probably be a good idea to backup the files I move into RAM to safeguard against the aforementioned problems, so I made a backup. I also didn’t want my computer taking longer to boot, so I backgrounded it all, the downside of this is that I can’t use pacman or abs immediately after turning my computer on as there is a delay of a few seconds. That’s not much of a problem though. I haven’t checked, but I imagine that the files will all be copied by the time X starts anyway. I ended up with this file:

#!/bin/dash
#
# /etc/rc.local: Local multi-user startup script.
#

echo 0 > /proc/sys/vm/swappiness
echo 1 > /proc/sys/kernel/sysrq

# Ramdisk Magic!
chmod 777 /tmp
touch /etc/ramdisk.sh

echo "#!/bin/dash" >> /etc/ramdisk.sh

echo "cd /var/ && tar cf abs.tar abs/" >> /etc/ramdisk.sh
echo "cd /var/cache/ && tar cf pacman.tar pacman/" >> /etc/ramdisk.sh
echo "cd /var/lib/ && tar cf pacman.tar pacman/" >> /etc/ramdisk.sh

echo "mkdir /mnt/ramdisk/var/" >> /etc/ramdisk.sh
echo "mkdir /mnt/ramdisk/var/cache/" >> /etc/ramdisk.sh
echo "mkdir /mnt/ramdisk/var/lib/" >> /etc/ramdisk.sh

echo "mv /var/abs /mnt/ramdisk/var" >> /etc/ramdisk.sh
echo "mv /var/cache/pacman /mnt/ramdisk/var/cache" >> /etc/ramdisk.sh
echo "mv /var/lib/pacman /mnt/ramdisk/var/lib" >> /etc/ramdisk.sh

echo "ln -s /mnt/ramdisk/var/abs /var/abs" >> /etc/ramdisk.sh
echo "ln -s /mnt/ramdisk/var/cache/pacman /var/cache/pacman" >> /etc/ramdisk.sh
echo "ln -s /mnt/ramdisk/var/lib/pacman /var/lib/pacman" >> /etc/ramdisk.sh
echo "ln -s /tmp /mnt/ramdisk/tmp" >> /etc/ramdisk.sh

echo "chmod 777 /mnt/ramdisk/tmp" >> /etc/ramdisk.sh
echo "rm /etc/ramdisk.sh" >> /etc/ramdisk.sh

chmod +x /etc/ramdisk.sh
/etc/ramdisk.sh &

As you can see, I don’t store /tmp in /mnt/ramdisk, I merely symlink it to there. That’s because I wanted to have /tmp available as soon as possible in case something needs to be written there. I tested the script, and it worked perfectly. Now I just had one more thing to do - write a script to save the files to disk at shutdown. I decided rc.local.shutdown was probably the best candidate for this, but I was unsure as to whether it was executed before or after the filesystems had been unmounted. I checked /etc/rc.shutdown and found that it was pretty much the first thing executed, so all would work. I then came up with this script:

#!/bin/dash
#
# /etc/rc.local.shutdown: Local shutdown script.
#

# Ramdisk Magic!

echo "Saving contents of ramdisk to harddrive"
rm /var/abs
rm /var/cache/pacman
rm /var/lib/pacman
mv /mnt/ramdisk/var/abs /var
mv /mnt/ramdisk/var/cache/pacman /var/cache
mv /mnt/ramdisk/var/lib/pacman /var/lib

I tested this script, and it also worked perfectly! Brilliant! I now have a lighting fast ‘disk’ storing pacman, abs, and tmp. This should have a nice and positive effect on system speed, if slight. What’s the point of using Linux if you’re not going to play with it? I eventually want to know how to mount the entire system in RAM, as Puppy does. I have an idea, but I’m far from sure.

Filed in Linux, under , , , , , on June 29, 2008

No Comments

Write comment - TrackBack - RSS Comments

Write comment