Adding trim support in LMDE/Ubuntu/Debian

Some time ago I bought a ssd drive and I wanted to preserve the drive
performance, therefore I tried to add the discard option in the fstab file,
however it turns out that it doesn’t really work on the newest filesystem (Btrfs)
which is the default filesystem for LMDE 14, I followed this guide, and when I tested it, didn’t work at all. Therefore I made a little script that runs
the ftrim command, then I added it to the crontab.

#!/usr/bin/env python
import pygtk
pygtk.require('2.0')
import pynotify
import sys
import subprocess
import logging
import time
log_filename = '/var/log/trim_logs/PyTrim.log'
logging.basicConfig(filename=log_filename,level=logging.INFO)
dt = time.strftime(" %Y-%m-%d %H:%M:%S")

if __name__ == '__main__':
if not pynotify.init("Basics"):
sys.exit(1)

proc=subprocess.Popen('sudo fstrim -v / ', shell=True, stdout=subprocess.PIPE, )
output=proc.communicate()
output = str(output)[2:-10]
n = pynotify.Notification("PyTrim", output)
logging.info(output+dt)

if not n.show():
print "Failed to send notification"
sys.exit(1)

Running it from the command line ie “sudo f_trim” works quite well however in order to
run the ftirm command without superuser privileges you need to make a little modification
on the sudoers file, like this.

cd /etc/
sudo visudo -f sudoers

Those commands will open nano whith is the default editor for sudoers file and you will see
something like this:
sudoers
you need to change “yourusername” you login name then save it, Crtl+X and Yes to overwrite the old file
and enter to save it. If you don’t know how to use nano just google for a quick nano tutorial.
Next step is to add your script on to crontab, you if you want to look for a crontab tutorial
this is definitely a good resource, however the man pages and info files are your friends :D.
The following is a screen shot of how it looks on my system.

crontab

So you need to edit the crontab file with your favourite editor and add line like the last one you can see on the screen shot.
Since the script pipes the result to the notifications area it’s really important to add the line “env DISPLAY=:0” otherwise the notification won’t show up on the screen. Obviously the path in the crontab file must be changed accordingly to the path where you are keeping the script.
The last thing is to create a directory where you can save the logs. If you want to use /var/log/trim_logs/ as selected in line 10 you need to first create that directory and change it’s owner ship to yourself; one way to go is something like this:

cd /var/log
sudo mkdir trim_logs
sudo chown yourusername trim_logs

You can still choose another directory and modify line 10.
One last thing, once you save the script remember to make it executable
by running otherwise it won’t run.

chmod +x yourfilename

I hope this little post can help someone else. Feel free to post any questions.