LUKS – Resolving Discard/TRIM Conflicts in Kernel Parameters

cryptsetupdm-cryptkernel parametersluks

I'm confused between the various ways that LUKS/dmcrypt/cryptsetup discard /TRIM operations can be enabled via the Linux kernel command line.

  1. The dracut manpage:

    rd.luks.allow-discards

    Allow using of discards (TRIM) requests on all LUKS partitions.

  2. The systemd-cryptsetup-generator manpage

    luks.options=, rd.luks.options=

    … If only a list of options, without an UUID, is specified, they apply to any UUIDs not specified elsewhere, and without an entry in /etc/crypttab. …

    The argument rd.luks.options=discard is recommended here.

  3. The Arch wiki section on LUKS and SSDs shows a third colon-seprated field:

    cryptdevice=/dev/sdaX:root:allow-discards

Questions:

  1. What is the difference between discard and allow-discards? Is the former mandatory and the second optional?
  2. Will luks.options= or rd.luks.options= apply given cryptdevice=/dev/sda2 (eg not a UUID)? What if cryptdevice= is given a UUID, does that count as "specified elsewhere"?
  3. Will luks.options= or rd.luks.options= overwrite / append / prepend if cryptsetup= already gives options?
  4. Is there any disadvantage to using rd.luks.allow-discards which seems to be simplest if TRIM is wanted everywhere?

Best Answer

It depends a little on the distribution you are using and what components are included by dracut in the initramfs.

For example, the cryptdevice= option is interpreted by the encrypt hook. Thus, it's only relevant for initramfs images that include this hook.

The disadvantage of rd.luks.allow-discards and rd.luks.allow-discards= is that it simply doesn't work. The dracut.cmdline(7) description of these options is incorrect. I tested it under Fedora 26 where it doesn't work and there is even a bug report for Fedora 19 where this deviation between documented and actual behavior was discussed and it was closed as wont-fix.

The luks.options= and rd.luks.options= are more generic as you basically can place any valid crypttab option in there, e.g. discard. Since they are interpreted by systemd-cryptsetup-generator which doesn't care about cryptdevice= you can't expect a useful interaction between these options.

Note that luks.options= only has an effect for devices that aren't listed in the initramfs image's etc/crypttab file.

Thus, to enable dm-crypt pass-though SSD trim support (a.k.a. discard) for dm-crypted devices opened during boot you have 2 options:

  1. add rd.luks.options=discard to the kernel command line and make sure that the initramfs image doesn't include a etc/crypttab
  2. add the discard option to the relevant entries in /etc/crypttab and make sure that the current version is included in the initramfs image.

You can use lsinitrd /path/to/initramfs etc/crypttab for checking the initramfs image, dracut -v -f /path/to/initramfs-image for regenerating the image after changes to /etc and dmsetup table to see whether the crypted device was actually opened with the discard option (the relevant entries should include the string allow_discards then).

Related Question