From: Ricardo Dias Date: Fri, 24 Mar 2017 13:50:52 +0000 (+0000) Subject: rbd: added image-id optional to Info command X-Git-Tag: v12.0.2~106^2~2 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=3138bdfba37cc9c05f52f251edc4a53fb9e24d9d;p=ceph.git rbd: added image-id optional to Info command Signed-off-by: Ricardo Dias --- diff --git a/src/test/cli/rbd/help.t b/src/test/cli/rbd/help.t index 0eb9fa935f79..8d4d5f59c3c4 100644 --- a/src/test/cli/rbd/help.t +++ b/src/test/cli/rbd/help.t @@ -660,7 +660,7 @@ rbd help info usage: rbd info [--pool ] [--image ] [--snap ] - [--format ] [--pretty-format] + [--image-id ] [--format ] [--pretty-format] Show information about image size, striping, etc. @@ -673,6 +673,7 @@ -p [ --pool ] arg pool name --image arg image name --snap arg snapshot name + --image-id arg image id --format arg output format [plain, json, or xml] --pretty-format pretty formatting (json and xml) diff --git a/src/tools/rbd/action/Info.cc b/src/tools/rbd/action/Info.cc index 59ce6c9aefec..3bfdeeb7d67a 100644 --- a/src/tools/rbd/action/Info.cc +++ b/src/tools/rbd/action/Info.cc @@ -65,7 +65,8 @@ static void format_flags(Formatter *f, uint64_t flags) } static int do_show_info(librados::IoCtx &io_ctx, librbd::Image& image, - const char *imgname, const char *snapname, Formatter *f) + const std::string &imgname, const std::string &imgid, + const std::string &snapname, Formatter *f) { librbd::image_info_t info; std::string parent_pool, parent_name, parent_snapname; @@ -111,8 +112,8 @@ static int do_show_info(librados::IoCtx &io_ctx, librbd::Image& image, return r; } - if (snapname) { - r = image.snap_is_protected(snapname, &snap_protected); + if (!snapname.empty()) { + r = image.snap_is_protected(snapname.c_str(), &snap_protected); if (r < 0) return r; } @@ -142,7 +143,11 @@ static int do_show_info(librados::IoCtx &io_ctx, librbd::Image& image, if (f) { f->open_object_section("image"); - f->dump_string("name", imgname); + if (!imgname.empty()) { + f->dump_string("name", imgname); + } else { + f->dump_string("id", imgid); + } f->dump_unsigned("size", info.size); f->dump_unsigned("objects", info.num_objs); f->dump_int("order", info.order); @@ -153,7 +158,7 @@ static int do_show_info(librados::IoCtx &io_ctx, librbd::Image& image, f->dump_string("block_name_prefix", prefix); f->dump_int("format", (old_format ? 1 : 2)); } else { - std::cout << "rbd image '" << imgname << "':\n" + std::cout << "rbd image '" << (imgname.empty() ? imgid : imgname) << "':\n" << "\tsize " << prettybyte_t(info.size) << " in " << info.num_objs << " objects" << std::endl @@ -184,7 +189,7 @@ static int do_show_info(librados::IoCtx &io_ctx, librbd::Image& image, } // snapshot info, if present - if (snapname) { + if (!snapname.empty()) { if (f) { f->dump_string("protected", snap_protected ? "true" : "false"); } else { @@ -272,6 +277,7 @@ void get_arguments(po::options_description *positional, po::options_description *options) { at::add_image_or_snap_spec_options(positional, options, at::ARGUMENT_MODIFIER_NONE); + at::add_image_id_option(options); at::add_format_options(options); } @@ -280,10 +286,34 @@ int execute(const po::variables_map &vm) { std::string pool_name; std::string image_name; std::string snap_name; - int r = utils::get_pool_image_snapshot_names( - vm, at::ARGUMENT_MODIFIER_NONE, &arg_index, &pool_name, &image_name, - &snap_name, utils::SNAPSHOT_PRESENCE_PERMITTED, - utils::SPEC_VALIDATION_NONE); + std::string image_id; + + if (vm.count(at::IMAGE_ID)) { + image_id = vm[at::IMAGE_ID].as(); + } + + bool has_image_spec = utils::check_if_image_spec_present( + vm, at::ARGUMENT_MODIFIER_NONE, arg_index); + + if (!image_id.empty() && has_image_spec) { + std::cerr << "rbd: trying to access image using both name and id. " + << std::endl; + return -EINVAL; + } + + int r; + if (image_id.empty()) { + r = utils::get_pool_image_snapshot_names(vm, at::ARGUMENT_MODIFIER_NONE, + &arg_index, &pool_name, + &image_name, &snap_name, + utils::SNAPSHOT_PRESENCE_PERMITTED, + utils::SPEC_VALIDATION_NONE); + } else { + r = utils::get_pool_snapshot_names(vm, at::ARGUMENT_MODIFIER_NONE, + &arg_index, &pool_name, &snap_name, + utils::SNAPSHOT_PRESENCE_PERMITTED, + utils::SPEC_VALIDATION_NONE); + } if (r < 0) { return r; } @@ -297,14 +327,13 @@ int execute(const po::variables_map &vm) { librados::Rados rados; librados::IoCtx io_ctx; librbd::Image image; - r = utils::init_and_open_image(pool_name, image_name, snap_name, true, - &rados, &io_ctx, &image); + r = utils::init_and_open_image(pool_name, image_name, image_id, snap_name, + true, &rados, &io_ctx, &image); if (r < 0) { return r; } - r = do_show_info(io_ctx, image, image_name.c_str(), - snap_name.empty() ? nullptr : snap_name.c_str(), + r = do_show_info(io_ctx, image, image_name, image_id, snap_name, formatter.get()); if (r < 0) { std::cerr << "rbd: info: " << cpp_strerror(r) << std::endl;