SSD TRIM Ubuntu Linux 12.10

On Ubuntu Linux 12.10 TRIM on filesystem level is not enabled by default (for a good reason!) but also batched discard is not enabled by default.

The German ubuntuusers wiki suggests running a script every day or week. Since I decided that I only wanted this to run if my load is low, because queued TRIM will only be avaliable in SATA 3.1 and running TRIM can block the system.

I decided to run TRIM daily since that would make it run shorter.

So I wrote this little script (Python this time, no other dependencies but the little script mentioned above and a file /var/opt/trim which needs to be existing and non-empty), which will be executed every 3 hours (using cron).

#!/usr/bin/python

import os, time

TRESH = 1.0
RUN_FILE = "/var/opt/trim"

load_1, load_5, load_15 = os.getloadavg()
last_run_file=open(RUN_FILE,'r')
last_run=int(last_run_file.read())
last_run_file.close()
now=int(time.time())

#print last_run

if now - last_run > 86400 and load_5 < TRESH:
        run=1
        try:
                run=os.system('/opt/trim.sh')
                last_run_file=open(RUN_FILE,'w')
                last_run_file.write(str(now))
                last_run_file.close()
        except Exception, e:
                import traceback
                print traceback.format_exc()

Moving to a new machine/setup with Ubuntu Linux (or Debian)

Today, as I got my new harddrive (which is a Samsung 830 SSD), I decided to re-install my copy of Ubuntu (for whatever reason). Copying my home folder, some configfiles and so on is an easy task, but what about all these little programs, that were installed with some manual Installer (like QPilot, VMware, …) because they don’t provide a APT-Installer? I do not do any bookkeeping about those, so I wrote a little script, that checks populates a list of files on my root filesystem and checks them, if they belong to some debian package.

It took a while on my old spinning harddrive, probably there is a more efficient way, instead of calling dlocate for every single file.

My little script:

#!/bin/bash
FILELIST=/tmp/findonapt_filelist.lst
FRESHLIST=0

populatelist() {
    echo "Creating fileslist"
    eval "/usr/bin/find / -xdev -type f > $FILELIST 2>/dev/null"
}

if [ ! -f $FILELIST ]
    then
        populatelist &
        FRESHLIST=1
        sleep 5;
fi

exec 0<$FILELIST
value=0
while read LINE

do
    if [[ $LINE != *var\/cache* ]]
        then 
            if [ $(dlocate -S $LINE | wc -l) == 0 ]
                then
                    value=`expr $value + 1`;
                    echo $LINE;
            fi

    fi
done

if [ $FRESHLIST == 1]
    then
        wait
fi

echo "****$value Non-APT Files found";