oh boy, fun. i’ve dealt w/ this before but it’s been a hot minute. let’s see if we can’t get you moving in the right direction.
a handful of tools can be brought to bear on these sorts of issues:
## get an allocation summary of your fs+devices:
sudo btrfs filesystem usage -T /path/to/some-dir
## `balance` has (at least) two uses:
## 1. balance extents across multi-device volumes
## 2. compact meta/sys/data to free up space, which may give you
## enough meta headroom to get it done; where nn is the %used
## of (data) blocks to target for compaction. the admin docs
## suggest starting at a low value like nn=10 and gradually
## incrementing until achieving the necessary headroom.
sudo btrfs -v balance start -dusage=nn /path/to/volume
## `balance status` will show progress at-a-glance
sudo btrfs balance status /path/to/volume
i haven’t done this on my filesystems for a while, and the nix store gets a ton of churn, so let’s see what happens.
❯ sudo btrfs fi us -T /nix/store
Overall:
Device size: 170.00GiB
Device allocated: 169.00GiB
Device unallocated: 1.00GiB
Device missing: 0.00B
Device slack: 5.00GiB
Used: 76.08GiB
Free (estimated): 85.24GiB (min: 84.74GiB)
Free (statfs, df): 85.24GiB
Data ratio: 1.00
Metadata ratio: 2.00
Global reserve: 226.06MiB (used: 0.00B)
Multiple profiles: no
Data Metadata System
Id Path single DUP DUP Unallocated Total Slack
-- --------- --------- -------- -------- ----------- --------- -------
1 /dev/sda5 156.98GiB 12.00GiB 16.00MiB 1.00GiB 170.00GiB 5.00GiB
-- --------- --------- -------- -------- ----------- --------- -------
Total 156.98GiB 6.00GiB 8.00MiB 1.00GiB 170.00GiB 5.00GiB
Used 72.74GiB 1.67GiB 48.00KiB
## `balance` took only about 90s on a ~170GiB volume with a shitload of small files on it
❯ sudo btrfs -v balance start -dusage=33 /nix
Dumping filters: flags 0x1, state 0x0, force is off
DATA (flags 0x2): balancing, usage=33
Done, had to relocate 54 out of 165 chunks
❯ sudo btrfs fi us -T /nix/store
Overall:
Device size: 170.00GiB
Device allocated: 115.00GiB
Device unallocated: 55.00GiB
Device missing: 0.00B
Device slack: 5.00GiB
Used: 76.05GiB
Free (estimated): 85.24GiB (min: 57.74GiB)
Free (statfs, df): 85.24GiB
Data ratio: 1.00
Metadata ratio: 2.00
Global reserve: 226.06MiB (used: 0.00B)
Multiple profiles: no
Data Metadata System
Id Path single DUP DUP Unallocated Total Slack
-- --------- --------- -------- -------- ----------- --------- -------
1 /dev/sda5 102.98GiB 12.00GiB 16.00MiB 55.00GiB 170.00GiB 5.00GiB
-- --------- --------- -------- -------- ----------- --------- -------
Total 102.98GiB 6.00GiB 8.00MiB 55.00GiB 170.00GiB 5.00GiB
Used 72.74GiB 1.65GiB 48.00KiB
note the difference in Device Unallocated afterward.
you may also be able to convert from a dir->subvol by creating a read-write snapshot of the parent volume, removing what you don’t want to keep in the subvol/snapshot, and then mounting the snapshot’s subvolume on the stub of the old directory name by using the subvol=name option in your /etc/fstab.
1. btrfs-balance
add.
i was able to try the “create a read/write snapshot of the parent fs” approach w/ btrfs earlier this evening, after realizing I never put /home on my machine on its own subvolume. it went smoothly and didn’t explode the filesystem’s metadata utilization. i laid out the subvolume and snapshot hierarchy in a way that was inspired by NetApp filers w/ similar features from back in the day.
the basic gist (in pseudocode) was:
mkdir -p /.vol/.snap
btrfs subvolume snapshot / /.vol/@home
cd /.vol/@home
rm -r !./home/
mv ./home/* ./
rmdir ./home
rm -r /home/*
mount -o subvol=.vol/@home /dev/<btrfs-rootdev> /home
then adjust /etc/fstab or nixos filesystem mount options as needed to mimic the subvolume mount command shown above.
HTH.