From 0e6aead68455a1043aed04e0557dc14af2cd6f60 Mon Sep 17 00:00:00 2001 From: Josef Bacik Date: Tue, 1 Nov 2011 14:26:20 -0400 Subject: [PATCH 17/35] Btrfs-progs: add an option for specifying the root to restore If the normal fs tree is hosed and the user has multiple subvolumes it's handy to be able to specify just one of the subvolumes to restore. It's also handy if a user only wants to restore say /home instead of his entire disk. Thanks, Signed-off-by: Josef Bacik --- restore.c | 32 ++++++++++++++++++++++++++++---- 1 files changed, 28 insertions(+), 4 deletions(-) diff --git a/restore.c b/restore.c index 250c9d3..38c4ded 100644 --- a/restore.c +++ b/restore.c @@ -760,13 +760,14 @@ int main(int argc, char **argv) char dir_name[128]; u64 tree_location = 0; u64 fs_location = 0; + u64 root_objectid = 0; int len; int ret; int opt; int super_mirror = 0; int find_dir = 0; - while ((opt = getopt(argc, argv, "sviot:u:df:")) != -1) { + while ((opt = getopt(argc, argv, "sviot:u:df:r:")) != -1) { switch (opt) { case 's': get_snaps = 1; @@ -809,6 +810,14 @@ int main(int argc, char **argv) case 'd': find_dir = 1; break; + case 'r': + errno = 0; + root_objectid = (u64)strtoll(optarg, NULL, 10); + if (errno != 0) { + fprintf(stderr, "Root objectid not valid\n"); + exit(1); + } + break; default: usage(); exit(1); @@ -842,8 +851,6 @@ int main(int argc, char **argv) } } - printf("Root objectid is %Lu\n", root->objectid); - memset(path_name, 0, 4096); strncpy(dir_name, argv[optind + 1], 128); @@ -856,6 +863,23 @@ int main(int argc, char **argv) dir_name[len - 1] = '\0'; } + if (root_objectid != 0) { + struct btrfs_root *orig_root = root; + + key.objectid = root_objectid; + key.type = BTRFS_ROOT_ITEM_KEY; + key.offset = (u64)-1; + root = btrfs_read_fs_root(orig_root->fs_info, &key); + if (IS_ERR(root)) { + fprintf(stderr, "Error reading root\n"); + root = orig_root; + ret = 1; + goto out; + } + key.type = 0; + key.offset = 0; + } + if (find_dir) { ret = find_first_dir(root, &key.objectid); if (ret) @@ -864,7 +888,7 @@ int main(int argc, char **argv) key.objectid = BTRFS_FIRST_FREE_OBJECTID; } - ret = search_dir(root->fs_info->fs_root, &key, dir_name); + ret = search_dir(root, &key, dir_name); out: close_ctree(root); -- 1.7.6.233.gd79bc