I just noticed this days that, according to smartctl, the hard disk on my notebook has reached 365 days on Power_On_Hours variable. As the notebook itself has almost 2 years of age (I bought it in March of 2008), it is almost a double birthday-combo :) .

However, at this age, it is obvious that the hard disk of the notebook was not prepared to endure such challenges nicely. Both of its speed (5400 RPMs), durability, capacity, and so on were targeted on a more light use.

On the other hand, the system has 3GB of RAM, which is more than enough for most of my tasks. So after some thinking, I’ve been using some small script to automatically improve the system performance by offloading the most I/O-consuming stuff to RAM for the past few months.

How does it works? Simple. I have a small script which grabs content of a disk directory which I expect to receive heavy use (for example, /usr/lib/firefox, or the rpm BUILD/ directories which receive lots of I/O when building packages, and so on), and converts them into a RAM disk. This way, when anything inside such directory is accessed, it requires absolutely no hard disk I/O, the seek times are non-existent as well, and the throughput is incredible.

For example, if I build mozilla-thunderbird by using real hard disk BUILD directory, it takes almost 2 hours to build. If I use ramdisk for just the BUILD directory, the time required for such task drops down to about 30 minutes. The firefox startup time (both cold and hot – e.g., the first execution and consecutive ones) have the same time of about 1 second; and so on.

So, without further words, this is the script I am using:

#!/bin/bash
#
# This script remounts a directory in tmpfs (ramdisk) to speed it up
#

DIR=$1
SIZE=$2

if [ ! "$UID" = "0" ]; then
    # this script must run by root. Let's try sudo'ing to root..
    exec sudo $0 $*
fi

if [ ! -d $DIR ]; then
    echo "Usage: $0 <full path to a directory>"
    exit 1
fi

if [ "a$SIZE" = "a" ]; then
    OPTIONS=""
else
    OPTIONS="-o size=$SIZE"
fi

# first, copy everything somewhere to reuse it later
TMP=`mktemp`
tar cpf $TMP $DIR

# remount dir as ramdisk
mount -t tmpfs $OPTIONS $DIR $DIR

# unpack everything back
(cd / && tar xpf $TMP)
rm -f $TMP

To use it, just save it under a name like toram and use it on the directory you want to move to ramdisk. The second optional parameter is the size of the ramdisk to use – as by default tmpfs mounts itself with 50% of available ram, sometimes you may want to use more or less memory.

Just some examples on how to use it:

# toram `pwd`/BUILD (will remount current BUILD directory in RAM)
# toram /usr/lib/firefox-3.6 (will remount firefox directory in ram)

I hope this could be useful to someone :) .

8 Responses to “Improving system speed, disk life and everything else in one small command”

  1. [...] here: Eugeni: Improving system speed, disk life and everything else in one small command Tags: [...]

  2. amazing

    what would be a script to launch firefox and to automaticaly mount to ramdisk and when closing FF then dismount ram ?

    and if i want 2 apps with directory in ramdisk, does it work ?

  3. It works with as many directories as you want :) .

    As for firefox, you could create a startup script which will basically: 1. check if the ramdisk is already mounted (e.g., if the directory is in /proc/mounts: grep -q /usr/lib/firefox /proc/mounts && echo “it is mounted” – should do the trick ) 2. if it is mounted, just run firefox 3. if it is not mounted, mount it, run firefox, and wait for its process to end. After that, just umount that directory

  4. sudo doesn’t like utf8 and password as “sp5&k%4.f”

    su likes it very much

  5. i tried your solution with my little knowledge

    i replaced “sudo” by “su -root” then my root password is allowed

    toram and umount works well

    i launch firefox then i don’t noticed any improvement

    i use toram /usr/lib/firefox-3.6 35M

    is it the good syntax ?

    yes firefox is in /usr/lib/firefox-3.6/

    perhaps all my ram is in use then the ram disk is in the swap ?

    ksysguard points that my ram size is 2 GB and 600kB swap is in use before using toram and FF then i assume /usr/lib/firefox-3.6/ ram disk is in the swap isn’t it.

  6. For firefox, the speed improvements depend a lot on your hardware configuration. If you have a desktop machine with a SATA disk, the startup time could decrease for example from 3 seconds to 2 or so; it is much more noticeable on slower notebook disks. On my machine it made a huge difference.

    Regarding firefox, its another issue is that it loads a LOT of data from your profile on startup. So if you have lots of cache or haven’t cleared your private data recently, it slows down the startup by several orders of magnitude. Just to exemplify, on my machine a 6-months-old profile took almost 30 seconds to startup. After cleaning the visited sites, downloads, cache, and so on, it dropped back to about 5 seconds.

  7. This script looks very interesting. I was wondering what the size variable is measured in. bytes ,kb or mb?

    Cheers, Donald.

  8. The size variable could be anything that tmpfs accepts. For example, 1024, 1024k, 1024m, 2G; or for example 50%, 75%, to use this percentage of your total RAM for the tmpfs partition.

Leave a Reply

(required)

(required)

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

© 2012 Eugeni's blog Suffusion theme by Sayontan Sinha