diff options
Diffstat (limited to 'cmds-subvolume.c')
-rw-r--r-- | cmds-subvolume.c | 284 |
1 files changed, 255 insertions, 29 deletions
diff --git a/cmds-subvolume.c b/cmds-subvolume.c index ac39f7bf..63c5990f 100644 --- a/cmds-subvolume.c +++ b/cmds-subvolume.c @@ -24,6 +24,7 @@ #include <libgen.h> #include <limits.h> #include <getopt.h> +#include <uuid/uuid.h> #include "kerncompat.h" #include "ioctl.h" @@ -32,6 +33,7 @@ #include "ctree.h" #include "commands.h" #include "btrfs-list.h" +#include "utils.h" static const char * const subvolume_cmd_group_usage[] = { "btrfs subvolume <command> <args>", @@ -271,21 +273,32 @@ out: return ret; } +/* + * Naming of options: + * - uppercase for filters and sort options + * - lowercase for enabling specific items in the output + */ static const char * const cmd_subvol_list_usage[] = { - "btrfs subvolume list [-apurts] [-g [+|-]value] [-c [+|-]value] " + "btrfs subvolume list [-agopurts] [-G [+|-]value] [-C [+|-]value] " "[--sort=gen,ogen,rootid,path] <path>", "List subvolumes (and snapshots)", "", "-p print parent ID", - "-a print all the subvolumes in the filesystem.", + "-a print all the subvolumes in the filesystem and", + " distinguish absolute and relative path with respect", + " to the given <path>", + "-c print the ogeneration of the subvolume", + "-g print the generation of the subvolume", + "-o print only subvolumes bellow specified path", "-u print the uuid of subvolumes (and snapshots)", + "-q print the parent uuid of the snapshots", "-t print the result as a table", "-s list snapshots only in the filesystem", "-r list readonly subvolumes (including snapshots)", - "-g [+|-]value", + "-G [+|-]value", " filter the subvolumes by generation", " (+value: >= value; -value: <= value; value: = value)", - "-c [+|-]value", + "-C [+|-]value", " filter the subvolumes by ogeneration", " (+value: >= value; -value: <= value; value: = value)", "--sort=gen,ogen,rootid,path", @@ -300,13 +313,14 @@ static int cmd_subvol_list(int argc, char **argv) struct btrfs_list_filter_set *filter_set; struct btrfs_list_comparer_set *comparer_set; u64 flags = 0; - int fd; + int fd = -1; u64 top_id; - int ret; + int ret = -1, uerr = 0; int c; char *subvol; int is_tab_result = 0; int is_list_all = 0; + int is_only_in_path = 0; struct option long_options[] = { {"sort", 1, NULL, 'S'}, {0, 0, 0, 0} @@ -318,7 +332,7 @@ static int cmd_subvol_list(int argc, char **argv) optind = 1; while(1) { c = getopt_long(argc, argv, - "apsurg:c:t", long_options, NULL); + "acgopqsurG:C:t", long_options, NULL); if (c < 0) break; @@ -329,6 +343,15 @@ static int cmd_subvol_list(int argc, char **argv) case 'a': is_list_all = 1; break; + case 'c': + btrfs_list_setup_print_column(BTRFS_LIST_OGENERATION); + break; + case 'g': + btrfs_list_setup_print_column(BTRFS_LIST_GENERATION); + break; + case 'o': + is_only_in_path = 1; + break; case 't': is_tab_result = 1; break; @@ -338,39 +361,49 @@ static int cmd_subvol_list(int argc, char **argv) 0); btrfs_list_setup_print_column(BTRFS_LIST_OGENERATION); btrfs_list_setup_print_column(BTRFS_LIST_OTIME); - + break; case 'u': btrfs_list_setup_print_column(BTRFS_LIST_UUID); break; + case 'q': + btrfs_list_setup_print_column(BTRFS_LIST_PUUID); + break; case 'r': flags |= BTRFS_ROOT_SUBVOL_RDONLY; break; - case 'g': + case 'G': btrfs_list_setup_print_column(BTRFS_LIST_GENERATION); ret = btrfs_list_parse_filter_string(optarg, &filter_set, BTRFS_LIST_FILTER_GEN); - if (ret) - usage(cmd_subvol_list_usage); + if (ret) { + uerr = 1; + goto out; + } break; - case 'c': + case 'C': btrfs_list_setup_print_column(BTRFS_LIST_OGENERATION); ret = btrfs_list_parse_filter_string(optarg, &filter_set, BTRFS_LIST_FILTER_CGEN); - if (ret) - usage(cmd_subvol_list_usage); + if (ret) { + uerr = 1; + goto out; + } break; case 'S': ret = btrfs_list_parse_sort_string(optarg, &comparer_set); - if (ret) - usage(cmd_subvol_list_usage); + if (ret) { + uerr = 1; + goto out; + } break; default: - usage(cmd_subvol_list_usage); + uerr = 1; + goto out; } } @@ -378,38 +411,66 @@ static int cmd_subvol_list(int argc, char **argv) btrfs_list_setup_filter(&filter_set, BTRFS_LIST_FILTER_FLAGS, flags); - if (check_argc_exact(argc - optind, 1)) - usage(cmd_subvol_list_usage); + if (check_argc_exact(argc - optind, 1)) { + uerr = 1; + goto out; + } subvol = argv[optind]; ret = test_issubvolume(subvol); if (ret < 0) { fprintf(stderr, "ERROR: error accessing '%s'\n", subvol); - return 12; + goto out; } if (!ret) { fprintf(stderr, "ERROR: '%s' is not a subvolume\n", subvol); - return 13; + ret = -1; + goto out; } fd = open_file_or_dir(subvol); if (fd < 0) { + ret = -1; fprintf(stderr, "ERROR: can't access '%s'\n", subvol); - return 12; + goto out; } top_id = btrfs_list_get_path_rootid(fd); - if (!is_list_all) + + if (is_list_all) + btrfs_list_setup_filter(&filter_set, + BTRFS_LIST_FILTER_FULL_PATH, + top_id); + else if (is_only_in_path) btrfs_list_setup_filter(&filter_set, BTRFS_LIST_FILTER_TOPID_EQUAL, top_id); - ret = btrfs_list_subvols(fd, filter_set, comparer_set, - is_tab_result); - if (ret) - return 19; - return 0; + /* by default we shall print the following columns*/ + btrfs_list_setup_print_column(BTRFS_LIST_OBJECTID); + btrfs_list_setup_print_column(BTRFS_LIST_GENERATION); + btrfs_list_setup_print_column(BTRFS_LIST_TOP_LEVEL); + btrfs_list_setup_print_column(BTRFS_LIST_PATH); + + if (is_tab_result) + ret = btrfs_list_subvols_print(fd, filter_set, comparer_set, + BTRFS_LIST_LAYOUT_TABLE, + !is_list_all && !is_only_in_path, NULL); + else + ret = btrfs_list_subvols_print(fd, filter_set, comparer_set, + BTRFS_LIST_LAYOUT_DEFAULT, + !is_list_all && !is_only_in_path, NULL); + +out: + if (filter_set) + btrfs_list_free_filter_set(filter_set); + if (comparer_set) + btrfs_list_free_comparer_set(comparer_set); + if (uerr) + usage(cmd_subvol_list_usage); + + return ret; } static const char * const cmd_snapshot_usage[] = { @@ -612,7 +673,17 @@ static int cmd_subvol_get_default(int argc, char **argv) btrfs_list_setup_filter(&filter_set, BTRFS_LIST_FILTER_ROOTID, default_id); - ret = btrfs_list_subvols(fd, filter_set, NULL, 0); + /* by default we shall print the following columns*/ + btrfs_list_setup_print_column(BTRFS_LIST_OBJECTID); + btrfs_list_setup_print_column(BTRFS_LIST_GENERATION); + btrfs_list_setup_print_column(BTRFS_LIST_TOP_LEVEL); + btrfs_list_setup_print_column(BTRFS_LIST_PATH); + + ret = btrfs_list_subvols_print(fd, filter_set, NULL, + BTRFS_LIST_LAYOUT_DEFAULT, 1, NULL); + + if (filter_set) + btrfs_list_free_filter_set(filter_set); if (ret) return 19; return 0; @@ -699,6 +770,160 @@ static int cmd_find_new(int argc, char **argv) return 0; } +static const char * const cmd_subvol_show_usage[] = { + "btrfs subvolume show <subvol-path>", + "Show more information of the subvolume", + NULL +}; + +static int cmd_subvol_show(int argc, char **argv) +{ + struct root_info get_ri; + struct btrfs_list_filter_set *filter_set; + char tstr[256]; + char uuidparse[37]; + char *fullpath = NULL, *svpath = NULL, *mnt = NULL; + char raw_prefix[] = "\t\t\t\t"; + u64 sv_id, mntid; + int fd = -1, mntfd = -1; + int ret = -1; + + if (check_argc_exact(argc, 2)) + usage(cmd_subvol_show_usage); + + fullpath = realpath(argv[1], 0); + if (!fullpath) { + fprintf(stderr, "ERROR: finding real path for '%s', %s\n", + argv[1], strerror(errno)); + goto out; + } + + ret = test_issubvolume(fullpath); + if (ret < 0) { + fprintf(stderr, "ERROR: error accessing '%s'\n", fullpath); + goto out; + } + if (!ret) { + fprintf(stderr, "ERROR: '%s' is not a subvolume\n", fullpath); + ret = -1; + goto out; + } + + ret = find_mount_root(fullpath, &mnt); + if (ret < 0) { + fprintf(stderr, "ERROR: find_mount_root failed on %s: " + "%s\n", fullpath, strerror(-ret)); + goto out; + } + ret = -1; + svpath = get_subvol_name(mnt, fullpath); + + fd = open_file_or_dir(fullpath); + if (fd < 0) { + fprintf(stderr, "ERROR: can't access '%s'\n", fullpath); + goto out; + } + + sv_id = btrfs_list_get_path_rootid(fd); + if (sv_id < 0) { + fprintf(stderr, "ERROR: can't get rootid for '%s'\n", + fullpath); + goto out; + } + + mntfd = open_file_or_dir(mnt); + if (mntfd < 0) { + fprintf(stderr, "ERROR: can't access '%s'\n", mnt); + goto out; + } + + mntid = btrfs_list_get_path_rootid(mntfd); + if (mntid < 0) { + fprintf(stderr, "ERROR: can't get rootid for '%s'\n", mnt); + goto out; + } + + if (sv_id == BTRFS_FS_TREE_OBJECTID) { + printf("%s is btrfs root\n", fullpath); + goto out; + } + + memset(&get_ri, 0, sizeof(get_ri)); + get_ri.root_id = sv_id; + + if (btrfs_get_subvol(mntfd, &get_ri)) { + fprintf(stderr, "ERROR: can't find '%s'\n", + svpath); + goto out; + } + + ret = 0; + /* print the info */ + printf("%s\n", fullpath); + printf("\tName: \t\t\t%s\n", get_ri.name); + + if (uuid_is_null(get_ri.uuid)) + strcpy(uuidparse, "-"); + else + uuid_unparse(get_ri.uuid, uuidparse); + printf("\tuuid: \t\t\t%s\n", uuidparse); + + if (uuid_is_null(get_ri.puuid)) + strcpy(uuidparse, "-"); + else + uuid_unparse(get_ri.puuid, uuidparse); + printf("\tParent uuid: \t\t%s\n", uuidparse); + + if (get_ri.otime) + strftime(tstr, 256, "%Y-%m-%d %X", + localtime(&get_ri.otime)); + else + strcpy(tstr, "-"); + printf("\tCreation time: \t\t%s\n", tstr); + + printf("\tObject ID: \t\t%llu\n", get_ri.root_id); + printf("\tGeneration (Gen): \t%llu\n", get_ri.gen); + printf("\tGen at creation: \t%llu\n", get_ri.ogen); + printf("\tParent: \t\t%llu\n", get_ri.ref_tree); + printf("\tTop Level: \t\t%llu\n", get_ri.top_id); + + if (get_ri.flags & BTRFS_ROOT_SUBVOL_RDONLY) + printf("\tFlags: \t\t\treadonly\n"); + else + printf("\tFlags: \t\t\t-\n"); + + /* print the snapshots of the given subvol if any*/ + printf("\tSnapshot(s):\n"); + filter_set = btrfs_list_alloc_filter_set(); + btrfs_list_setup_filter(&filter_set, BTRFS_LIST_FILTER_BY_PARENT, + (u64)get_ri.uuid); + btrfs_list_setup_print_column(BTRFS_LIST_PATH); + btrfs_list_subvols_print(fd, filter_set, NULL, BTRFS_LIST_LAYOUT_RAW, + 1, raw_prefix); + + /* clean up */ + if (get_ri.path) + free(get_ri.path); + if (get_ri.name) + free(get_ri.name); + if (get_ri.full_path) + free(get_ri.full_path); + if (filter_set) + btrfs_list_free_filter_set(filter_set); + +out: + if (mntfd >= 0) + close(mntfd); + if (fd >= 0) + close(fd); + if (mnt) + free(mnt); + if (fullpath) + free(fullpath); + + return ret; +} + const struct cmd_group subvolume_cmd_group = { subvolume_cmd_group_usage, NULL, { { "create", cmd_subvol_create, cmd_subvol_create_usage, NULL, 0 }, @@ -710,6 +935,7 @@ const struct cmd_group subvolume_cmd_group = { { "set-default", cmd_subvol_set_default, cmd_subvol_set_default_usage, NULL, 0 }, { "find-new", cmd_find_new, cmd_find_new_usage, NULL, 0 }, + { "show", cmd_subvol_show, cmd_subvol_show_usage, NULL, 0 }, { 0, 0, 0, 0, 0 } } }; |