Skip to content

Commit

Permalink
btrfs-progs: add option for recursive subvol snapshots
Browse files Browse the repository at this point in the history
Adds an option -R to btrfs subvolume snapshot, corresponding to the flag
BTRFS_UTIL_CREATE_SNAPSHOT_RECURSIVE.

This is another resubmission of a missed patch of Omar's from 2018:
https://lore.kernel.org/all/e42cdc5d5287269faf4d09e8c9786d0b3adeb658.1516991902.git.osandov@fb.com/

Signed-off-by: Mark Harmstone <[email protected]>
Co-authored-by: Omar Sandoval <[email protected]>
  • Loading branch information
maharmstone and osandov committed Sep 9, 2024
1 parent c75b2f2 commit 3a0c784
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
8 changes: 7 additions & 1 deletion Documentation/btrfs-subvolume.rst
Original file line number Diff line number Diff line change
Expand Up @@ -252,17 +252,23 @@ show [options] <path>
-u|--uuid UUID
show details about subvolume with the given *UUID*, looked up in *path*

snapshot [-r] [-i <qgroupid>] <source> <dest>|[<dest>/]<name>
snapshot [-r|-R] [-i <qgroupid>] <source> <dest>|[<dest>/]<name>
Create a snapshot of the subvolume *source* with the
name *name* in the *dest* directory.

If only *dest* is given, the subvolume will be named the basename of *source*.
If *source* is not a subvolume, btrfs returns an error.

If you wish to recursively create a readonly snapshot, you can run
:command:`btrfs property set <path> ro true` on each subvolume after this command completes.

``Options``

-r
Make the new snapshot read only.
-R|--recursive
Recursively snapshot subvolumes beneath the source. This option cannot be
combined with -r.
-i <qgroupid>
Add the newly created subvolume to a qgroup. This option can be given multiple
times.
Expand Down
15 changes: 13 additions & 2 deletions cmds/subvolume.c
Original file line number Diff line number Diff line change
Expand Up @@ -616,7 +616,7 @@ static int cmd_subvolume_delete(const struct cmd_struct *cmd, int argc, char **a
static DEFINE_COMMAND_WITH_FLAGS(subvolume_delete, "delete", CMD_DRY_RUN);

static const char * const cmd_subvolume_snapshot_usage[] = {
"btrfs subvolume snapshot [-r] [-i <qgroupid>] <subvolume> { <subdir>/<name> | <subdir> }",
"btrfs subvolume snapshot [-r|-R] [-i <qgroupid>] <subvolume> { <subdir>/<name> | <subdir> }",
"",
"Create a snapshot of a <subvolume>. Call it <name> and place it in the <subdir>.",
"(<subvolume> will look like a new sub-directory, but is actually a btrfs subvolume",
Expand All @@ -625,6 +625,7 @@ static const char * const cmd_subvolume_snapshot_usage[] = {
"When only <subdir> is given, the subvolume will be named the basename of <subvolume>.",
"",
OPTLINE("-r", "make the new snapshot readonly"),
OPTLINE("-R|--recursive", "recursively snapshot subvolumes beneath the source; this option cannot be combined with -r"),
OPTLINE("-i <qgroupid>", "Add the new snapshot to a qgroup (a quota group). This option can be given multiple times."),
HELPINFO_INSERT_GLOBALS,
HELPINFO_INSERT_QUIET,
Expand All @@ -642,7 +643,7 @@ static int cmd_subvolume_snapshot(const struct cmd_struct *cmd, int argc, char *

optind = 0;
while (1) {
int c = getopt(argc, argv, "i:r");
int c = getopt(argc, argv, "i:rR");
if (c < 0)
break;

Expand All @@ -657,11 +658,21 @@ static int cmd_subvolume_snapshot(const struct cmd_struct *cmd, int argc, char *
case 'r':
flags |= BTRFS_UTIL_CREATE_SNAPSHOT_READ_ONLY;
break;
case 'R':
flags |= BTRFS_UTIL_CREATE_SNAPSHOT_RECURSIVE;
break;
default:
usage_unknown_option(cmd, argv);
}
}

if ((flags & BTRFS_UTIL_CREATE_SNAPSHOT_READ_ONLY) &&
(flags & BTRFS_UTIL_CREATE_SNAPSHOT_RECURSIVE)) {
error("-r and -R cannot be combined");
retval = 1;
goto out;
}

if (check_argc_exact(argc - optind, 2)) {
retval = 1;
goto out;
Expand Down

0 comments on commit 3a0c784

Please sign in to comment.