Linux SSD TRIM Setup
If you are installing an SSD in Linux, you must setup TRIM in order to keep the drive performing at peak performance. Also, I believe fstrim only works on ext4, someone correct me if I am wrong.
You can install Linux just fine right out of the box, you just need to make sure AHCI is enabled in the BIOS. If your BIOS does not have an option to set it, then chances are it will detect it automatically.
Once you have your SSD installed and your OS loaded onto it, you just just need to make sure the TRIM function is enabled. The program that does this is called “fstrim”.
According to the MAN page;
“fstrim is used on a mounted filesystem to discard (or “trim”) blocks which are not in use by the filesystem. This is useful for solid- state drives (SSDs) and thinly-provisioned storage.”
So basically, fstrim goes around picking up the garbage left behind by the file system so that when it is time to write to a block again, you don’t have to take out the trash every time. This can obviously speed things up as the drive gets more and more use.
The problem is that by default, fstrim is usually installed but there is no automated way to trigger it.
You can manually do by typing the following command:
sudo fstrim -v /
and should return the following:
joao@D630:/etc$ sudo fstrim -v /
/: 825077760 bytes were trimmed
In my system, the root “/” folder is the SSD, but you can change the path to wherever the SSD is mounted. For example, if your home directory was where the SSD is stored, then you command would look like this;
sudo fstrim -v /home
So, it’s great that we can do this manually but this should be automated, no?
There are many tutorials out there but I have decided to share how I did it and why I did it. Because there are people with laptops and some with desktops and most solutions are either or but mine I think satisfies either or both and it is fairly easy to set up.
Let’s look at some of the scenarios, if you have a laptop then we can write a simple script to run fstrim at boot but if you have a desktop, you may not reboot very often so this solution is not very good for desktops. If you run a desktop, you could just run a CRON job periodically and this may satisfy a laptop user as well. The best combination is both! I believe that fstrim should run more often to clean smaller amounts because, let’s say you ran your laptop for a few days, and fstrim only ran at boot, then the next boot time would be increased because it would have to wait for fstrim to do its work.
So, we are going to add a script to four places:
/etc/rc.local = This will run at every boot no matter what.
/etc/rc0.d/ = This will run at shutdown only.
/etc/rc6.d/ = This will run on restart only.
/etc/cron.hourly/ = This will run every hour the system is running.
We will edit the rc.local directly and add the command there. We will then create a script and add it to /etc/init.d/ and create links to rc0.d, rc6.d and cron.hourly.
Here is my reasoning for doing this. Running a cron job every hour should not tax the system for normal use, this will keep the fstrim load in check. When you shut down, rc0.d will run fstrim during a shutdown and this should have cleaned most of the file system. When the system starts, rc.local will catch any little thing left with little to no delay in booting. If instead of a shut down your reboot the machine, the same process will occur except that rc6.d will execute instead of rc0.d. Now, in the event of weird things happening to your system and a clean shutdown or restart was not done, rc.local will catch it all, but might add more time to your boot. So, once this is all set in place, it should be completely automated.
Fire up your favorite terminal and edit the rc.local file.
(note: I am running Elementary OS, so scratch is my editor. replace with your favorite editor, Gedit etc.. In Elementary OS the actual command for scratch is “scratch-text-editor”, I just created a symbolic link in /usr/bin to “scratch” to shorten the command)
sudo scratch /etc/rc.local
Now, add the following right above the “exit 0”
#Run “fstrim” at boot
fstrim -v / >>$LOG
echo “Time: $(date)” >>$LOG
Now, every time you start or reboot, fstrim will run!
Notice, I added a log command, you can always see the log at /var/log/trim.log
Next, create a folder in your Home directory called “Scripts” and inside there create an empty file called fstrim.sh
now edit that file and copy the following;
#Run “fstrim” command
fstrim -v / >>$LOG
echo “Time: $(date)” >>$LOG
Save and close the file. Open a terminal and navigate to the Scripts directory. Next, we need to make the file executable:
chmod +x fstrim.sh
Next, copy the file to /etc/init.d/ using the following command.
sudo cp fstrim.sh /etc/init.d/fstrim.sh
Next, change the permissions on the file with the following command:
sudo chmod 755 /etc/init.d/fstrim.sh
Next, we make the three symbolic links and will be finished.
sudo ln -s /etc/init.d/fstrim.sh /etc/rc0.d/K30fstrim
sudo ln -s /etc/init.d/fstrim.sh /etc/rc6.d/K30fstrim
sudo ln -s /etc/init.d/fstrim.sh /etc/cron.hourly/fstrim
Restart your machine and it should work, you can verify by looking in /var/log/trim.log