How to move files from a directory over to a subvol with btrfs?

Supposedly this should be easy, however I would like to try to use rsync, and this is a command I saw which seemed like it would simply move the files, then delete them, no excess space incurred.


[nixos@nixos:~]$ sudo rsync -aHAX --numeric-ids \
  --inplace \
  --remove-source-files \
  --info=progress2,stats1 --no-inc-recursive \
  /mnt/@/home/ \
  /mnt/home-check/
 16,067,546,673   1%  397.87MB/s    0:00:38 (xfr#200303, to-chk=2334593/2573858)rsync: [receiver] write failed on "/mnt/home-check/spiderunderurbed/.librewolf/ewl5dr1x.default/storage/default/https+++app.slack.com/cache/morgue/173/{a95076a0-7bcb-4db5-8d2e-f684f2ad36ad}.final": No space left on device (28)
rsync error: error in file IO (code 11) at receiver.c(381) [receiver=3.4.1]


That did not work, I tried using cp reflink for all the files, 600+ GBS worth of files, I ran into metadata exuastion, from what I hear you cant just turn a random directory with its contents into a subvol.

I have, like atleast 20gbs of free space on that partition in each attempt, where i either have to move files back or just immediately delete the contents of the subvol i was trying to copy/link it too.

Does anyone have any ideas or suggestions?

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.