How to copy a btrfs filesystem
I have not found any ready-made solution as of today (2016-05-06), but solved the problem for my purposes, including Copy-on-Write handling. The steps to "clone" /source
to /target
are:
Get a list of subvolumes ordered by
ogen
:btrfs subvolume list -qu --sort ogen /source
. Sorting is probably enough to guarantee that snapshots or subvolumes which depend on previous ones are handled first. This is important for dealing with Copy-on-Write, because we need to have the base volumes transferred first.Make all subvolumes read-only using
btrfs property set -ts /source/some-volume ro true
.Now, for each subvolume from the list above, starting at the top, do the following:
If the volume does not have a parent UUID (displayed as
-
) or the parent UUID does not exist anymore in the list, run:btrfs send /source/some/volume | btrfs receive /target/some/
If the volume does have a parent UUID which still exists, we should have transferred it already because of
--sort ogen
and we can use that as a base to avoid data duplication. Hence, find the parent UUID's path in the list and run:btrfs send -p /source/parent/volume/ -c /source/parent/volume/ /source/some/volume/ | btrfs receive /target/some/
(btrfs would probably guess the-p
argument automatically, but I prefer to be explicit).After running one of the above commands make the target and source read-write again:
btrfs property set -ts /source/some/volume ro false; btrfs property set -ts /target/some/volume ro false
. This step can be skipped if the source has been previously read-only.
This should handle many cases. Caveats:
There might be some complications with respect to ordering when nesting subvolumes/snapshots.
The whole process is obviously more fun when scripted.
btrfs send
accepts multiple clone source (-c
) arguments. It may be advantageous to not only specify the parent's volume path, but also those of any ancestors or simply any previously sent volumes. It did not make any difference here, but it might — just a guess — help to avoid data duplication in some cases.I am unsure if any meta information on snapshots or subvolumes is lost along the way, but just about everything interesting else for most use cases should be preserved.
The whole process helped me transfer an 800 GB filesystem with 3.8 GB used (according to df
) to a 10 GB image with 3.8 GB used. Transferring without -p
and -c
would have used about 190 GB, so data duplication was indeed avoided.
I have created a python tool which can do this. I did this because I tried @Thomas Luzat's approach in both my own and @Johannes Ernst's implementation, and the used space doubled from 20GB to 40GB in the cloning procedure. I thought something more efficient was needed.
Consider this common file system history:
current ---------------------------------\
| | | |
snap4 snap3 snap2 snap1
With Thomas' algorithm, "current" would be cloned first, and all snapshots (being snapshots of former states of "current") would use "current" as clone source / parent. Obviously, it would be better to base snap3 on snap4, snap2 on snap3, etc.
And this is just the tip of the iceberg; finding the "best" clone sources (in terms of space savings) in a btrfs file system with a complex history is a non-trivial problem. I've come up with 3 other strategies to solve this problem, which seem to use space much more efficiently. One has actually resulted in clones size slightly below that of the source.
You can read the details on the github page if you're interested.
Option 1 - Dumb data copy then change UUID
Ensure that source partition is unmounted and will not be automounted.
Use either dd
(slow, dumb) or partclone.btrfs -b -s /dev/src -o /dev/target
Use btrfstune -u
to change UUID after copy and before mounting.
Data loss warning: Do NOT try to (auto)mount either original or copy until the UUID has changed
Option 2 - btrfs-clone
I have not personally tried btrfs-clone
, but it purports to clone an existing BTRFS file system to a new one, cloning each subvolume in order.