For it to work you need several adjustments, every layer needs to support TRIM, have the support enabled and pass the information down to the next, till it reaches the physical layer.
Lets start at the highest layer in the stack, the filesystem:
Edit /etc/fstab to include the discard mount option where needed (keep in mind that the FS needs to support it, ext4 does):
/dev/mapper/lvm_crypt /data ext4 defaults,discard 0 2
Next comes the logical volume manager – which is optional and should by default be setup correctly already. Anyway check to be sure that you got the following line in /etc/lvm/lvm.conf:
issue_discards = 1
Last but not least we need to tell the encryption layer to issue the trim commands too. For this to work you need to edit /etc/crypttab to include the discard option:
lvm_crypt UUID=a-uuid-or-device-path none luks,discard
After having changed the crypttab (and lvm.conf) it is neccessary to update the initrd image for the changes to take effect (at next boot):
sudo update-initramfs -k all -c
Reboot and test if it’s working:
sudo fstrim /data