What do you think? Discuss, post comments, or ask questions at the end of this article [More about me]

TRIM is an maintenance and optimisation operation for SSD drives that "that enables an operating system to inform a NAND flash solid-state drive (SSD) which data blocks it can erase because they are no longer in use. The use of TRIM can improve the performance of writing data to SSDs and contribute to longer SSD life." [techtarget.com].

On Arch-based distributions the util-linux package has a systemd service and timer which, when enabled, it uses fstrim to run TRIM operations on your primary drive weekly.

Things are a little more complicated if you drive is LUKS encrypted.  The Arch wiki has a great writeup on enabling (or allowing) periodic TRIM on a LUKS partition.  Note TRIM isn't allowed by default as it does have potential implications with regards to security etc. (which you can read about here).

We'll cover the following here:

Enabling fstrim.timer on Arch-based distros (like Arch, Manjaro, etc.)

Execute the following to enable and start the fstrim.timer which will execute TRIM weekly:

sudo systemctl enable fstrim.timer
sudo systemctl start fstrim.timer

Allowing TRIM operations on a LUKS partition

As mentioned previously, by default LUKS does not allow TRIM operations to passthrough to the encrypted drive.  If you want to execute periodic TRIM on a LUKS partition, first enable the fstrim.timer as above.

I assume here that you have a working root partition that is LUKS encrypted, and you're using a systemd based distro.  We'll need to modify our grub config and append two parameters:

  1. we need to append :allow-discards to the cryptdevice argument on the GRUB_CMDLINE_LINUX_DEFAULT line of grub;
  2. add the kernel parameter rd.luks.options=discard

Open /etc/default/grub in your favourite text editor (here we use vim):

sudo vim /etc/default/grub

And modify the GRUB_CMDLINE_LINUX_DEFAULT line as above.  It should end up looking similar to:

GRUB_CMDLINE_LINUX_DEFAULT="cryptdevice=UUID=d591882f-0cbc-4752-a971-6f68e910df09:luks-d591882f-0cbc-4752-a971-6f68e910df09:allow-discards root=/dev/mapper/luks-d591882f-0cbc-4752-a971-6f68e910df09 rd.luks.options=discard"

Note your line might have extra arguments like resume=..., resume_offset=..., security=apparmor etc.  This is okay, just add the :allow-discards and rd.luks.options=discard in the appropriate place.

Save your changes and update grub:

sudo update-grub

Now reboot and check that TRIM is allowed on your LUKS partitions.

Check fstrim is allowed on LUKS paritions

Check with lsblk

An easy way to check that TRIM operations are enabled for your LUKS partition is to use lsblk:

lsblk --discard

It should print out something like this:

[jay@jay-blade ~]$ lsblk --discard
NAME                                          DISC-ALN DISC-GRAN DISC-MAX DISC-ZERO
sda                                                  0      512B       2G         0
├─sda1                                               0      512B       2G         0
├─sda2                                               0      512B       2G         0
│ └─luks-d591882f-0cbc-4752-a971-6f68e910df09        0      512B       2G         0
├─sda3                                               0      512B       2G         0
└─sda4                                               0      512B       2G         0

Non-zero entries (like above) in the DISC-GRAN and DISC-MAX columns means TRIM is enabled/allowed.

References

  1. https://en.wikipedia.org/wiki/Trim_(computing)
  2. https://searchstorage.techtarget.com/definition/TRIM
  3. https://wiki.archlinux.org/index.php/Dm-crypt/Specialties#Discard/TRIM_support_for_solid_state_drives_(SSD)
  4. https://forum.manjaro.org/t/luks-full-disk-encryption-and-trim-problem/20291/3