The best way to enable parameters through sysfs

Since kernel 5.19, Btrfs has a new feature to automatically balance the filesystem:

Since Linux kernel 5.19 there is a sysfs knob to enable automatic block group reclaim. This is essentially the kernel automatically balancing individual block groups as they fall under a certain threshold.

By default completely empty block groups are reclaimed into free space automatically. Using the sysfs knob bg_reclaim_threshold, it is now it is possible to to set another threshold than 0. The full sysfs path is /sys/fs/btrfs/<FSID>/allocation/<PROFILE>/bg_reclaim_threshold.

  • FSID = filesystem UUID. Use btrfs filesystem show to list current filesystems.
  • PROFILE = DATA, METADATA or SYSTEM chunks.

Source: Balancing a Btrfs filesystem | Forza's Ramblings

I was wondering if Fedora could benefit from this feature and what is the best method to enable it at every startup.
Awaiting further improvements: [PATCH 0/6] btrfs: dynamic and periodic block_group reclaim - Boris Burkov

It seems that Meta uses it with some benefits:

This can be worked around by:

  • enabling automatic reclaim
  • periodically running balance

The latter is widely deployed via btrfsmaintenance
(GitHub - kdave/btrfsmaintenance: Scripts for btrfs maintenance tasks like periodic scrub, balance, trim or defrag on selected mountpoints or directories.) and the former is used at scale at Meta with good results.

Any opinions?

An example: echo 10 > /sys/fs/btrfs/c3c00bf0-73a6-4aca-91bb-b5e32e76a08c/allocation/data/bg_reclaim_threshold

Here, 10, means a threshold of 10%. The kernel will now consider block groups that fall below this amount of usage for automatic reclaiming.

1 Like

Not sure if it is the best way per se, but you can use systemd-tmpfiles to write a specific value to any file, including sysfs, on boot.[1][2]


  1. https://wiki.archlinux.org/title/Systemd#systemd-tmpfiles_-_temporary_files ↩︎

  2. https://wiki.archlinux.org/title/Gaming#Make_the_changes_permanent ↩︎

1 Like

Thank you for the response. I was also looking into “udev”, to start a script through a rule on “mount”.
Because if the partition is unmounted, the value resets.

NOTE: These settings are not persistent and will reset back to 0 when the filesystem is unmounted or system is rebooted.