From 30c6067a7cb382022ae598119d766c495d8f2a2c Mon Sep 17 00:00:00 2001 From: Aminda Suomalainen Date: Wed, 1 Apr 2026 13:19:32 +0300 Subject: [PATCH] btrfs-enable-dynamic-periodic-reclaim: initial commit --- .../btrfs-enable-dynamic-periodic-reclaim | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100755 bash/usr-local-bin/btrfs-enable-dynamic-periodic-reclaim diff --git a/bash/usr-local-bin/btrfs-enable-dynamic-periodic-reclaim b/bash/usr-local-bin/btrfs-enable-dynamic-periodic-reclaim new file mode 100755 index 0000000..aaf0342 --- /dev/null +++ b/bash/usr-local-bin/btrfs-enable-dynamic-periodic-reclaim @@ -0,0 +1,30 @@ +#!/usr/bin/env bash + +# Enable dynamic and periodic reclaim for currently mounted BTRFS filesystems +# and try to keep them enabled on boot through systemd-tmpfiles. +# WARNING: will not persist over remount + +# Ensure we are root +if [ "$(id -u)" != "0" ]; then + echo "This script obviously requires root" + exit 1 +fi + +# If the previous file was removed within the for loop, it would erase +# configuration for other than the last filesystem. +if [ -f "/etc/tmpfiles.d/btrfs-enable-dynamic-periodic-reclaim.conf" ]; then + rm /etc/tmpfiles.d/btrfs-enable-dynamic-periodic-reclaim.conf +fi + +# Loop through filesystems +for filesystem in /sys/fs/btrfs/*; do + # Exclude /sys/fs/btrfs/btrfs/features + if [ -d "${filesystem}/allocation/data/" ]; then + echo 1 > ${filesystem}/allocation/data/dynamic_reclaim + echo 1 > ${filesystem}/allocation/data/periodic_reclaim + # Adapted from https://wiki.archlinux.org/title/Gaming#Make_the_changes_permanent + # to have these options set on boot instead of having to rerun this script automatically + echo "w ${filesystem}/allocation/data/dynamic_reclaim - - - - 1" >> /etc/tmpfiles.d/btrfs-enable-dynamic-periodic-reclaim.conf + echo "w ${filesystem}/allocation/data/periodic_reclaim - - - - 1" >> /etc/tmpfiles.d/btrfs-enable-dynamic-periodic-reclaim.conf + fi +done