TRIM on LVM on LUKS on SSD, revisited

A few years ago I wrote about enabling trim on an SSD that was running with LVM on top of LUKS. Since then things have changed slightly, a few times.

With Fedora 24 you no longer need to edit the /etc/crypttab file and rebuild your initramfs. Now systemd supports a kernel boot argument rd.luks.options=discard which is the only thing you should need to do to enable trim on your LUKS device.

Edit /etc/default/grub and add the rd.luks.options=discard argument to the end of GRUB_CMDLINE_LINUX, e.g.:
GRUB_TIMEOUT=5
GRUB_DISTRIBUTOR="$(sed 's, release .*$,,g' /etc/system-release)"
GRUB_DEFAULT=saved
GRUB_DISABLE_SUBMENU=true
GRUB_TERMINAL_OUTPUT="console"
GRUB_CMDLINE_LINUX="rd.luks.uuid=luks-de023401-ccec-4455-832bf-e5ac477743dc rd.luks.uuid=luks-a6d344739a-ad221-4345-6608-e45f16a8645e rhgb quiet rd.luks.options=discard"
GRUB_DISABLE_RECOVERY="true"

Next, rebuild your grub config file:
sudo grub2-mkconfig -o /boot/grub2/grub.cfg

If you’re using LVM, the setting is the same as the previous post. Edit the /etc/lvm/lvm.conf file and enabled the issue_discards option:
issue_discards = 1

If using LVM you will need to rebuild your initramfs so that the updated lvm.conf is in there.
sudo dracut -f

Reboot and try fstrim:
sudo fstrim -v /

Now also thanks to systemd, you can just enable the fstrim timer (cron) to do this automatically:
sudo systemctl enable fstrim.timer

23 thoughts on “TRIM on LVM on LUKS on SSD, revisited

  1. Would you mind updating this post to be a complete set of instructions? It’s a little unclear if “the LVM settings in the other post are still the same” means that you need to just edit lvm.conf and that’s all, or if you still need to muck with fstab.conf as well.

  2. OK, so I tried to follow these instructions with the Fedora 24 Beta and I found that I *did* need to make the crypttab change and force the initrd to regenerate before it would work, still. (However, adding the grub command-line meant that I didn’t need to have “discard” listed in /etc/fstab)

  3. Yeah I can do that. The same, means follow the lvm bit from the other post which should be to allow discards in lvm.conf and rebuild initramfs.

  4. Did you try the combination of just changing the inittab and adding kernel boot option?

    Edit: I just re-checked on my machine, and I did not need to edit /etc/crypttab – all I needed to do was add the kernel boot line. So not sure why you’re getting different behaviour.

  5. Hi all,

    I think the difference between some people needing to edit crypttab and others not boils down to some people having a line for their luks volumes in their crypttab and others not.

    On a fresh F24 install with luks /etc/crypttab is empty, no editing needed. On a machine with a much older upgraded install /etc/crypttab is not empty, and will end up in the initrd (I believe) and I guess that rd.luks.options only sets the default options for luks volumes not listed in crypttab.

    Regards,

    Hans

  6. Thanks for the HOWTO. I did have to edit crypttab though on a fresh F24 install. It did not work for me without it. So it must be some other reason than in-place upgrades for it.

  7. OK, thanks for letting me know. Were you using LVM? I still have to set it on LVM but haven’t recently on systems with just an encrypted partition.

  8. I had to edit crypttab.

    I’m wondering if it happens if you setup your partitions manually (I still have everything on LVM).

    Anyway – thanks for the howto! 🙂

  9. Thanks for this guide; It was very helpful. I problem Stephen was having may be due to his active grub configuration being in another location. That would be likely if he was using UEFI boot. I used `sudo grub2-mkconfig -o /boot/efi/EFI/fedora/grub.cfg` instead of `sudo grub2-mkconfig -o /boot/grub2/grub.cfg` and it worked as expected.

  10. Instructions work, but I had to include “luks,allow-discards” to crypttab.

    This was a F25 clean install with encryption enabled at install

  11. Thanks, interesting. I have a Fedora 25 install and I only had to add “discard” – I’m not sure what’s going on there, but happy it’s working for you.

  12. Can confirm that
    * adding ‘rd.luks.options=discard’ to /etc/default/grub
    * running sudo grub2-mkconfig -o /boot/grub2/grub.cfg
    * rebooting

    did not solve it, but doing the following after reboot

    * add ‘luks,allow-discards’ too all encrypted volumes in /etc/crypttab
    * running sudo dracut –force
    * rebooting

    did the trick on a clean Fedora 25 install with LUKS (no LVM). Thanks for the guides!

  13. I have followed the instructions, however do encounter the following problem. My configuration is LVM with ext4 LUKS encryption on Fedora 25. Once I issue the fstrim -av command I get the following amount of data trimmed:

    /home: 100.7 GiB (108144668672 bytes) trimmed
    /boot: 826 MiB (866119680 bytes) trimmed
    /: 43.4 GiB (46640447488 bytes) trimmed

    Once I issue it again right away, it gets almost no trimmed data as expected:

    /home: 509.2 MiB (533905408 bytes) trimmed
    /boot: 0 B (0 bytes) trimmed
    /: 0 B (0 bytes) trimmed

    Howeverm once I reboot, without using my ssd for copying huge files, my complete partition gets trimmed again:

    /home: 100.7 GiB (108144668672 bytes) trimmed
    /boot: 826 MiB (866119680 bytes) trimmed
    /: 43.4 GiB (46640447488 bytes) trimmed

    I think this is not normal and would indicate the trim is somehow forgotten after reboot?

    As far as I recall Fedora 24 was behaving as expected and not doing a trim for the complete ssd again after reboot.

    Does anyone know of this issue and how to fix it?

    Thanks.

  14. I’m not sure that this is a bug – the device has been powered off/reset so I assume the drive doesn’t track what it’s trimmed already between power offs.

  15. Thanks for the post
    Just wanted to confirm this. If I am not using LUKS, only LVM, I should follow the instructions from “If you’re using LVM, the setting is the same as the previous post…”, RIGHT?
    Thank you in advance.

  16. If you’re only using LVM then you can leave off the crypttab bit. You’ll just need to edit lvm.conf and re-build your initramfs.

    -c

Leave a Reply

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