#!/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/"
BTRFS_DATA_USAGE="5 10 20 30 40 50 60 70 80 85"
BTRFS_METADATA_USAGE="5 10 20 30 40 50 60 70 80"
ERROR_CODE=0

for MOUNT_POINT in ${MOUNT_POINTS} ; do
	for USAGE in ${BTRFS_DATA_USAGE} ; do 
		echo "Balancing block groups on a btrfs filesystem, mount point: ${MOUNT_POINT}, blocks: data, usage: ${USAGE}%."
		echo btrfs balance start -v -dusage=${USAGE} ${MOUNT_POINT}
		btrfs balance start -v -dusage=${USAGE} ${MOUNT_POINT}
		ERROR_CODE=$?
		echo "Exit code: ${ERROR_CODE}"
	done
done

for MOUNT_POINT in ${MOUNT_POINTS} ; do
	for USAGE in ${BTRFS_METADATA_USAGE} ; do 
		echo "Balancing block groups on a btrfs filesystem, mount point: ${MOUNT_POINT}, blocks: metadata, usage: ${USAGE}%."
		echo btrfs balance start -v -musage=${USAGE} ${MOUNT_POINT}
		btrfs balance start -v -musage=${USAGE} ${MOUNT_POINT}
		ERROR_CODE=$?
		echo "Exit code: ${ERROR_CODE}"
	done
done

exit 0

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