Resize btrfs filesystem to the minimum size in a single step

btrfsfilesystems

How do I resize a btrfs filesystem to the minimum possible size in a single step?

I only want to dd the smallest amount of data possible, and want to use dd since btrfs send is progressively slower the more snapshots there are.

btrfs filesystem resize has a max argument, but no min argument.

If I try to resize more than the free space available, I get a message like:

ERROR: unable to resize '/media/backup-alt': No space left on device

I've been progressively resizing downwards in steps of decreasing size (eg passing arguments -128G -64G, -32G, …) but this is a time consuming convergence on a solution.

Is there a way to shrink to minimum in a single step?

Best Answer

The best solution I've come across so far is to get the minimum free space (using -b for bytes):

sudo btrfs filesystem usage -b /mountpoint
   Free (estimated):              71890542592 (min: 71890542592)

And then resize by the negative of the min amount:

sudo btrfs filesystem resize -71890542592 /mountpoint

Alternatively, if there is a big difference between the min free and the unallocated, you may choose to use (unallocated * 0.9) since resizing by the exact unallocated bytes seems to fail.

You can then repeatedly shrink by small amounts until the resize fails:

while sudo btrfs filesystem resize -200M /mountpoint; do true; done

This is not exactly a single step, but at least mostly automated. The loop by itself could be a single step, but it will probably take longer doing small incremental resizes rather than initially shrinking by a large chunk.