Nothing works to allow periodic TRIM on btrfs LUKS partition

Installed EOS on an NVME drive formatted in btrfs and encrypted with LUKS (all using Calamares). Found recently that my drive started hanging up my system at times and the write speed in benchmarks dropped from 2gb/s to 100mb/s. From some googling I found it could be caused by TRIM not being used often enough, and sudo fstrim -v / surely didn’t work because of permissions.
Ran sudo cryptsetup --allow-discards --persistent refresh $partition-name and it allowed fstrim to run, but gave me a warning that it won’t be applied persistently as the partition is LUKS1. Started searching for ways to allow it persistently and people were using grub parameters like rd.luks.options=discard, rd.luks.allow-discards, cryptdevice=/dev/sdaX:root:allow-discards. None of these did anything, even when combined (I didn’t forget to update grub). Same goes for discard option in /etc/crypttab. What is the problem here?

I don’t use luks but i am on btrfs and seems to be working out of the box.

[ricklinux@rick-ms7c37 ~]$ systemctl status fstrim.timer
● fstrim.timer - Discard unused filesystem blocks once a week
     Loaded: loaded (/usr/lib/systemd/system/fstrim.timer; enabled; preset: disabled)
     Active: active (waiting) since Sun 2024-07-14 15:52:12 EDT; 39min ago
 Invocation: 1985cf551a2e4aaaae920b476c4cb447
    Trigger: Mon 2024-07-15 00:39:28 EDT; 8h left
   Triggers: ● fstrim.service
       Docs: man:fstrim

Jul 14 15:52:12 rick-ms7c37 systemd[1]: Started Discard unused filesystem blocks once a week.

Edit: Wish i could help on luks but just don’t have much knowledge there.

That should all work fine with just putting discard into /etc/crypttab.

Here is how the entry looks on my laptop:

root   UUID=8de4c6d0-f1e6-4d1c-88a0-849c2e8a15fb    /crypto_keyfile.bin        discard,no-read-workqueue,no-write-workqueue

root” is the name of the luks device as shown in /dev/mapper/
The UUID is derived the command blkid.

I do not have rd.luks.options or rd.luks.allow-discards on the kernel commandline.

Mine looked like luks-xxxx UUID=xxxx /crypto_keyfile.bin luks,discard, fstrim still gave me the same error that was resolved by the cryptsetup command. Is there an extra step I might be missing?

Please share more details:

  • content of /etc/cryptsetup
  • ls -l /dev/mapper
  • output of blkid command

Cryptsetup is empty.

total 0
crw------- 1 root root 10, 236 Jul 15 12:10 control
lrwxrwxrwx 1 root root       7 Jul 15 12:10 luks-e655b8b3-774d-4a05-a5fa-7793d67f213a -> ../dm-0
/dev/nvme0n1p1: UUID="e655b8b3-774d-4a05-a5fa-7793d67f213a" TYPE="crypto_LUKS" PARTUUID="a2db9ee9-aa8c-4a6f-9108-6c074a87879e"
/dev/nvme0n1p2: UUID="F870-EEAD" BLOCK_SIZE="512" TYPE="vfat" PARTUUID="09523d7c-b212-4384-a2f6-e0b006c6c9e2"
/dev/mapper/luks-e655b8b3-774d-4a05-a5fa-7793d67f213a: UUID="6d2beae5-8dd0-4357-8b9a-693a2af64bcd" UUID_SUB="86a77592-c673-4811-9f9e-99649b16342e" BLOCK_SIZE="4096" TYPE="btrfs"
/dev/sda1: LABEL="Data" UUID="901463d6-9781-498b-a143-8df49810ed94" BLOCK_SIZE="512" TYPE="xfs" PARTUUID="6f202a2b-a5bf-447b-a077-c1d9daa96196"

Sorry, I mean /etc/crypttab

What is this “xxxx” about? is this the true UUID or are you trying to hide something? There is no need to hide a UUID. It is just an unique identifier for a drive or partition. It is not a secret. it is important to see the UUID. If you put “xxxx” everywhere there is nothing to review for me.

I put it like that because it’s easier to read (names are quite long) and assumed they shouldn’t have anything to do with the problem I’m having, but if they do, I’ve put them back now.

The crypttab entry is luks-e655b8b3-774d-4a05-a5fa-7793d67f213a UUID=e655b8b3-774d-4a05-a5fa-7793d67f213a /crypto_keyfile.bin luks,discard

blkid reports UUID="6d2beae5-8dd0-4357-8b9a-693a2af64bcd"

But in your cryttab is UUID=e655b8b3-774d-4a05-a5fa-7793d67f213a

That is a mismatch from my point of view. Try to change the cryptab UUID to the value reported by blkid.

Which UUID do you have on your kernel cmdline? rd.luks.uuid ? From my point of view that should also be the UUID shown by blkid.

PS
I do not know what blkid means with UUID_SUB. That might be something btrfs specific.

PPS
I am not sure, but you probably need to update your initramfs if you change crypttab.

If you are using dracut with systemd hook, just add only a parameter rd.luks.options=discard in Grub cmdline and remove two parameters rd.luks.allow-discards, :allow-discards, then update Grub and reboot. This has worked for me in the past.

mkinitcpio without systemd hook does not work with rd.luks.options=discard, AFAIK.

Ok, this was the step that I missed. Left crypttab as is for now, ran sudo dracut-rebuild and fstrim worked with no issues instantly after rebooting. Solved.

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.