[HowTo] Exclude or ignore Btrfs from fstrim service when using Kernel 6.2+

There is news about Kernel 6.2 for Btrfs:

The filesystem BTRFS enables discard=async on some modern SSDs by default. This is why it does not need to use fstrim.

See the changelog:

discard=async by default for devices that support it

The issue:

If You have multiple different filesystems Ext4, XFS and Btrfs …, disabling systemD service fstrim is not a good idea for all filesystems on multiple SSDs on your same computer.
If You need to fstrim other filesystems e.g. EXT4 and disable fstrim for Btrfs only.

Currently fstrim has no option to ignore any selected filesystem e.g. Btrfs, but there is a trick to exclude Btrfs from fstrim. :nerd_face:

How to exclude or ignore Btrfs from fstrim service

  1. Edit fstrim.serivce
$ sudo systemctl edit --full fstrim.service 
  1. Change the line:

Change

ExecStart=/usr/bin/fstrim --listed-in /etc/fstab:/proc/self/mountinfo --verbose --quiet-unsupported

to

ExecStart=/usr/bin/sh -c "/bin/grep -v btrfs /proc/self/mountinfo > /tmp/noBtrfs.mountinfo && /usr/bin/fstrim --listed-in /tmp/noBtrfs.mountinfo --verbose --quiet-unsupported"
4 Likes

Why not use the file directly as an argument to the grep? :thinking:
Plus i would suggest to use a drop-in to override the original instead…

Below is a little bit enhanced version of what you did but as a drop-in:

  • /etc/systemd/system/fstrim.service.d/noBtrfs.conf
    [Service]
    ExecStartPre=/bin/sh -c "/bin/grep -v btrfs /etc/fstab > /tmp/noBtrfs.fstab"
    ExecStartPre=/bin/sh -c "/bin/grep -v btrfs /proc/self/mountinfo > /tmp/noBtrfs.mountinfo"
    ExecStart=
    ExecStart=/sbin/fstrim/fstrim --listed-in /tmp/noBtrfs.fstab:/tmp/noBtrfs.mountinfo --verbose --quiet-unsupported
    ExecStartPost=/bin/rm -f /tmp/noBtrfs.fstab /tmp/noBtrfs.mountinfo
    
    • The ExecStartPre’s get executed in sequence.
    • The ExecStart only gets executed if the ExecStartPre’s ran without error.
    • The ExecStartPost is for cleanup of the temporary files after the main task.
    • The empty ExecStart is to clear the setting from the original unit so we can override it, else you end-up with an additional ExecStart line instead…

By the way according to fstrim(8) section --listed-in:

  • It’s much easier to add X-fstrim.notrim to the mount options in fstab of the BTRFS volumes you use…

Which would totally remove the need to do all the above… :wink:
:point_up:

1 Like

I like your suggestion. But you do not need to add the first line for fstab, because mountinfo is sufficient and more flexible than fstab, it can detect when connecting any other external SSD via USB.

Yes, I did not know that before. Thanks for the suggestion. But the default config of fstrim.services uses mountinfo which collects all mount-points.

Update

Today, fstrim added a new option -t,--types to filter out by filesystems

I literally used the same command as the default service uses, just changed the files it uses, eg. double-collon separated and in same order :wink:
You are free to comment-out the first line ofcourse, which would result in only the mountinfo tobe used :wink:
(Commenting is done using a # at line start)

1 Like

Thanks and advice, I was looking for similar information.