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()

Leave a Reply

Your email address will not be published. Required fields are marked *