#!/bin/bash
#
# sys-filesystems-balance
#
# Balance block groups on a btrfs filesystem
#
# (c)2018 Marek Uher <marek@uher.info>
#

#MOUNT_POINTS="/__file_domains/@system/ /__file_domains/@boot/ /__file_domains/@data/"
MOUNT_POINTS="`findmnt -lo target -t btrfs | egrep -v "(TARGET|snapshots|rollbacks|__file_domains)" | sort`"
ERROR_CODE=0

for MOUNT_POINT in ${MOUNT_POINTS} ; do
	echo "Defragmenting a btrfs filesystem, mount point: ${MOUNT_POINT}"
	echo btrfs filesystem defragment -r ${MOUNT_POINT}
	btrfs filesystem defragment -r ${MOUNT_POINT}
	ERROR_CODE=$?
	echo "Exit code: ${ERROR_CODE}"
done

exit 0

# /* End of file: sys-filesystems-balance */ 
