From 4784d6202bc670e3c74a426f5f7acda2eedd8fe7 Mon Sep 17 00:00:00 2001 From: Mykola Golub Date: Thu, 15 Mar 2018 18:40:59 +0200 Subject: [PATCH] rbd: return image id in info command output And as a side effect, previously when rbd info was called with --image-id, the image id was shown instead of the image name, so the user was not able to see the image name from info output. Now it will show the image name. Signed-off-by: Mykola Golub --- qa/workunits/rbd/cli_generic.sh | 2 +- .../cli-integration/rbd/formatted-output.t | 30 +++++++++++++++++++ src/tools/rbd/action/Info.cc | 28 +++++++++++------ 3 files changed, 50 insertions(+), 10 deletions(-) diff --git a/qa/workunits/rbd/cli_generic.sh b/qa/workunits/rbd/cli_generic.sh index 3a4bdb39ebb..edda28ec114 100755 --- a/qa/workunits/rbd/cli_generic.sh +++ b/qa/workunits/rbd/cli_generic.sh @@ -419,7 +419,7 @@ test_trash() { rbd trash mv test2 ID=`rbd trash ls | cut -d ' ' -f 1` - rbd info --image-id $ID | grep "rbd image '$ID'" + rbd info --image-id $ID | grep "rbd image 'test2'" rbd trash restore $ID rbd ls | grep test2 diff --git a/src/test/cli-integration/rbd/formatted-output.t b/src/test/cli-integration/rbd/formatted-output.t index eae9d4ea01f..2e73dcd7a6c 100644 --- a/src/test/cli-integration/rbd/formatted-output.t +++ b/src/test/cli-integration/rbd/formatted-output.t @@ -64,6 +64,7 @@ For now, use a more inclusive regex. { "block_name_prefix": "rb.0.*", (glob) "format": 1, + "id": "", "name": "foo", "object_size": 4194304, "objects": 256, @@ -75,6 +76,7 @@ whenever it is run. grep -v to ignore it, but still work on other distros. $ rbd info foo --format xml | xml_pp 2>&1 | grep -v '^new version at /usr/bin/xml_pp' foo + 1073741824 256 22 @@ -93,6 +95,7 @@ whenever it is run. grep -v to ignore it, but still work on other distros. { "block_name_prefix": "rb.0.*", (glob) "format": 1, + "id": "", "name": "foo", "object_size": 4194304, "objects": 256, @@ -103,6 +106,7 @@ whenever it is run. grep -v to ignore it, but still work on other distros. $ rbd info foo@snap --format xml | xml_pp 2>&1 | grep -v '^new version at /usr/bin/xml_pp' foo + 1073741824 256 22 @@ -115,6 +119,7 @@ whenever it is run. grep -v to ignore it, but still work on other distros. rbd image 'bar': \tsize 1024 MB in 256 objects (esc) \torder 22 (4096 kB objects) (esc) + \tid:* (glob) [^^]+ (re) \tformat: 2 (esc) \tfeatures: layering, exclusive-lock, object-map, fast-diff, deep-flatten (esc) @@ -134,6 +139,7 @@ whenever it is run. grep -v to ignore it, but still work on other distros. ], "flags": [], "format": 2, + "id": "*", (glob) "name": "bar", "object_size": 4194304, "objects": 256, @@ -144,6 +150,7 @@ whenever it is run. grep -v to ignore it, but still work on other distros. $ rbd info bar --format xml | xml_pp 2>&1 | grep -v '^new version at /usr/bin/xml_pp' bar + * (glob) 1073741824 256 22 @@ -165,6 +172,7 @@ whenever it is run. grep -v to ignore it, but still work on other distros. rbd image 'bar': \tsize 512 MB in 128 objects (esc) \torder 22 (4096 kB objects) (esc) + \tid:* (glob) [^^]+ (re) \tformat: 2 (esc) \tfeatures: layering, exclusive-lock, object-map, fast-diff, deep-flatten (esc) @@ -185,6 +193,7 @@ whenever it is run. grep -v to ignore it, but still work on other distros. ], "flags": [], "format": 2, + "id": "*", (glob) "name": "bar", "object_size": 4194304, "objects": 128, @@ -196,6 +205,7 @@ whenever it is run. grep -v to ignore it, but still work on other distros. $ rbd info bar@snap --format xml | xml_pp 2>&1 | grep -v '^new version at /usr/bin/xml_pp' bar + * (glob) 536870912 128 22 @@ -218,6 +228,7 @@ whenever it is run. grep -v to ignore it, but still work on other distros. rbd image 'bar': \tsize 1024 MB in 256 objects (esc) \torder 22 (4096 kB objects) (esc) + \tid:* (glob) [^^]+ (re) \tformat: 2 (esc) \tfeatures: layering, exclusive-lock, object-map, fast-diff, deep-flatten (esc) @@ -238,6 +249,7 @@ whenever it is run. grep -v to ignore it, but still work on other distros. ], "flags": [], "format": 2, + "id": "*", (glob) "name": "bar", "object_size": 4194304, "objects": 256, @@ -249,6 +261,7 @@ whenever it is run. grep -v to ignore it, but still work on other distros. $ rbd info bar@snap2 --format xml | xml_pp 2>&1 | grep -v '^new version at /usr/bin/xml_pp' bar + * (glob) 1073741824 256 22 @@ -271,6 +284,7 @@ whenever it is run. grep -v to ignore it, but still work on other distros. rbd image 'baz': \tsize 2048 MB in 512 objects (esc) \torder 22 (4096 kB objects) (esc) + \tid:* (glob) [^^]+ (re) \tformat: 2 (esc) \tfeatures: layering (esc) @@ -286,6 +300,7 @@ whenever it is run. grep -v to ignore it, but still work on other distros. ], "flags": [], "format": 2, + "id": "*", (glob) "name": "baz", "object_size": 4194304, "objects": 512, @@ -296,6 +311,7 @@ whenever it is run. grep -v to ignore it, but still work on other distros. $ rbd info baz --format xml | xml_pp 2>&1 | grep -v '^new version at /usr/bin/xml_pp' baz + * (glob) 2147483648 512 22 @@ -319,6 +335,7 @@ whenever it is run. grep -v to ignore it, but still work on other distros. { "block_name_prefix": "rb.0.*", (glob) "format": 1, + "id": "", "name": "quux", "object_size": 4194304, "objects": 1, @@ -328,6 +345,7 @@ whenever it is run. grep -v to ignore it, but still work on other distros. $ rbd info quux --format xml | xml_pp 2>&1 | grep -v '^new version at /usr/bin/xml_pp' quux + 1048576 1 22 @@ -339,6 +357,7 @@ whenever it is run. grep -v to ignore it, but still work on other distros. rbd image 'child': \tsize 512 MB in 128 objects (esc) \torder 22 (4096 kB objects) (esc) + \tid:* (glob) [^^]+ (re) \tformat: 2 (esc) \tfeatures: layering, exclusive-lock, object-map, fast-diff (esc) @@ -357,6 +376,7 @@ whenever it is run. grep -v to ignore it, but still work on other distros. ], "flags": [], "format": 2, + "id": "*", (glob) "name": "child", "object_size": 4194304, "objects": 128, @@ -367,6 +387,7 @@ whenever it is run. grep -v to ignore it, but still work on other distros. $ rbd info rbd_other/child --format xml | xml_pp 2>&1 | grep -v '^new version at /usr/bin/xml_pp' child + * (glob) 536870912 128 22 @@ -387,6 +408,7 @@ whenever it is run. grep -v to ignore it, but still work on other distros. rbd image 'child': \tsize 512 MB in 128 objects (esc) \torder 22 (4096 kB objects) (esc) + \tid:* (glob) [^^]+ (re) \tformat: 2 (esc) \tfeatures: layering, exclusive-lock, object-map, fast-diff (esc) @@ -408,6 +430,7 @@ whenever it is run. grep -v to ignore it, but still work on other distros. ], "flags": [], "format": 2, + "id": "*", (glob) "name": "child", "object_size": 4194304, "objects": 128, @@ -425,6 +448,7 @@ whenever it is run. grep -v to ignore it, but still work on other distros. $ rbd info rbd_other/child@snap --format xml | xml_pp 2>&1 | grep -v '^new version at /usr/bin/xml_pp' child + * (glob) 536870912 128 22 @@ -452,6 +476,7 @@ whenever it is run. grep -v to ignore it, but still work on other distros. rbd image 'deep-flatten-child': \tsize 512 MB in 128 objects (esc) \torder 22 (4096 kB objects) (esc) + \tid:* (glob) [^^]+ (re) \tformat: 2 (esc) \tfeatures: layering, exclusive-lock, object-map, fast-diff, deep-flatten (esc) @@ -471,6 +496,7 @@ whenever it is run. grep -v to ignore it, but still work on other distros. ], "flags": [], "format": 2, + "id": "*", (glob) "name": "deep-flatten-child", "object_size": 4194304, "objects": 128, @@ -481,6 +507,7 @@ whenever it is run. grep -v to ignore it, but still work on other distros. $ rbd info rbd_other/deep-flatten-child --format xml | xml_pp 2>&1 | grep -v '^new version at /usr/bin/xml_pp' deep-flatten-child + * (glob) 536870912 128 22 @@ -502,6 +529,7 @@ whenever it is run. grep -v to ignore it, but still work on other distros. rbd image 'deep-flatten-child': \tsize 512 MB in 128 objects (esc) \torder 22 (4096 kB objects) (esc) + \tid:* (glob) [^^]+ (re) \tformat: 2 (esc) \tfeatures: layering, exclusive-lock, object-map, fast-diff, deep-flatten (esc) @@ -522,6 +550,7 @@ whenever it is run. grep -v to ignore it, but still work on other distros. ], "flags": [], "format": 2, + "id": "*", (glob) "name": "deep-flatten-child", "object_size": 4194304, "objects": 128, @@ -533,6 +562,7 @@ whenever it is run. grep -v to ignore it, but still work on other distros. $ rbd info rbd_other/deep-flatten-child@snap --format xml | xml_pp 2>&1 | grep -v '^new version at /usr/bin/xml_pp' deep-flatten-child + * (glob) 536870912 128 22 diff --git a/src/tools/rbd/action/Info.cc b/src/tools/rbd/action/Info.cc index 5a4f4c392aa..6927a303b05 100644 --- a/src/tools/rbd/action/Info.cc +++ b/src/tools/rbd/action/Info.cc @@ -75,7 +75,6 @@ static void format_flags(Formatter *f, uint64_t flags) } static int do_show_info(librados::IoCtx &io_ctx, librbd::Image& image, - const std::string &imgname, const std::string &imgid, const std::string &snapname, Formatter *f) { librbd::image_info_t info; @@ -85,6 +84,11 @@ static int do_show_info(librados::IoCtx &io_ctx, librbd::Image& image, librbd::mirror_image_info_t mirror_image; int r; + std::string imgname; + r = image.get_name(&imgname); + if (r < 0) + return r; + r = image.stat(info, sizeof(info)); if (r < 0) return r; @@ -93,6 +97,13 @@ static int do_show_info(librados::IoCtx &io_ctx, librbd::Image& image, if (r < 0) return r; + std::string imgid; + if (!old_format) { + r = image.get_id(&imgid); + if (r < 0) + return r; + } + std::string data_pool; if (!old_format) { int64_t data_pool_id = image.get_data_pool_id(); @@ -180,11 +191,8 @@ static int do_show_info(librados::IoCtx &io_ctx, librbd::Image& image, if (f) { f->open_object_section("image"); - if (!imgname.empty()) { - f->dump_string("name", imgname); - } else { - f->dump_string("id", imgid); - } + f->dump_string("name", imgname); + f->dump_string("id", imgid); f->dump_unsigned("size", info.size); f->dump_unsigned("objects", info.num_objs); f->dump_int("order", info.order); @@ -195,13 +203,16 @@ 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.empty() ? imgid : imgname) << "':\n" + std::cout << "rbd image '" << imgname << "':\n" << "\tsize " << prettybyte_t(info.size) << " in " << info.num_objs << " objects" << std::endl << "\torder " << info.order << " (" << prettybyte_t(info.obj_size) << " objects)" << std::endl; + if (!imgid.empty()) { + std::cout << "\tid: " << imgid << std::endl; + } if (!data_pool.empty()) { std::cout << "\tdata_pool: " << data_pool << std::endl; } @@ -396,8 +407,7 @@ int execute(const po::variables_map &vm, return r; } - r = do_show_info(io_ctx, image, image_name, image_id, snap_name, - formatter.get()); + r = do_show_info(io_ctx, image, snap_name, formatter.get()); if (r < 0) { std::cerr << "rbd: info: " << cpp_strerror(r) << std::endl; return r; -- 2.47.3