Luigit
repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by admin

nushell/modules/btrfs.nu

Raw
# Btrfs Maintenance Script
# This script checks if your system is Btrfs and attempts to fix "Full" states
# by running a balance on chunks > 75% full.
export def maintenance [
	mount_point: path = '/' # mount point to check, `/` by default
] {
	print "🔍 Checking filesystem type..."
	let fs_type = df | where Mounted == $mount_point | get Filesystem.0

	if $fs_type != "btrfs" {
			print $"❌ Error: Not a Btrfs filesystem ($fs_type). Aborting."
			exit 1
	}

	print "✅ Filesystem is Btrfs."

	print "\n📊 Current Btrfs Usage:"
	^btrfs filesystem usage $mount_point

	print "\n🛠  Starting Btrfs Balance..."

	try {
			^sudo btrfs balance start -dusage=75 -musage=75 $mount_point
			print "✅ Balance command sent successfully."
	} catch { |err |
			print $"❌ Failed to start balance: $err"
			print "Ensure you have sudo privileges."
			exit 1
	}

	print "\n⏳ Waiting for balance to complete or show status..."
	^sudo btrfs status $mount_point

	if ($in | str contains "Balance") {
			print "\n⚠  Balance is still running. You can check progress with:"
			print $"  btrfs balance status $MOUNT_POINT"
	}

	print "\n📈 Checking storage after balance:"
	^btrfs filesystem usage $mount_point

	print "\n✅ Maintenance Complete."
}