1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
6 #include "tools/rbd/ArgumentTypes.h"
7 #include "tools/rbd/Shell.h"
8 #include "tools/rbd/Utils.h"
9 #include "include/rbd_types.h"
10 #include "cls/rbd/cls_rbd_types.h"
11 #include "common/errno.h"
12 #include "common/Formatter.h"
18 namespace at
= argument_types
;
19 namespace po
= boost::program_options
;
21 int execute_create(const po::variables_map
&vm
) {
24 std::string group_name
;
25 std::string pool_name
;
27 int r
= utils::get_pool_group_names(vm
, at::ARGUMENT_MODIFIER_NONE
,
28 &arg_index
, &pool_name
, &group_name
);
33 librados::Rados rados
;
34 librados::IoCtx io_ctx
;
36 r
= utils::init(pool_name
, &rados
, &io_ctx
);
41 r
= rbd
.group_create(io_ctx
, group_name
.c_str());
43 std::cerr
<< "rbd: create error: " << cpp_strerror(r
) << std::endl
;
50 int execute_list(const po::variables_map
&vm
) {
53 std::string pool_name
= utils::get_pool_name(vm
, &arg_index
);
55 at::Format::Formatter formatter
;
56 int r
= utils::get_formatter(vm
, &formatter
);
60 Formatter
*f
= formatter
.get();
62 librados::Rados rados
;
63 librados::IoCtx io_ctx
;
64 r
= utils::init(pool_name
, &rados
, &io_ctx
);
70 std::vector
<std::string
> names
;
71 r
= rbd
.group_list(io_ctx
, &names
);
76 f
->open_array_section("consistency_groups");
77 for (auto i
: names
) {
79 f
->dump_string("name", i
);
81 std::cout
<< i
<< std::endl
;
91 int execute_remove(const po::variables_map
&vm
) {
94 std::string group_name
;
95 std::string pool_name
;
97 int r
= utils::get_pool_group_names(vm
, at::ARGUMENT_MODIFIER_NONE
,
98 &arg_index
, &pool_name
, &group_name
);
103 librados::Rados rados
;
104 librados::IoCtx io_ctx
;
106 r
= utils::init(pool_name
, &rados
, &io_ctx
);
112 r
= rbd
.group_remove(io_ctx
, group_name
.c_str());
114 std::cerr
<< "rbd: remove error: " << cpp_strerror(r
) << std::endl
;
121 int execute_add(const po::variables_map
&vm
) {
122 size_t arg_index
= 0;
124 std::string group_name
;
125 std::string group_pool_name
;
127 int r
= utils::get_special_pool_group_names(vm
, &arg_index
,
131 std::cerr
<< "rbd: image add error: " << cpp_strerror(r
) << std::endl
;
135 std::string image_name
;
136 std::string image_pool_name
;
138 r
= utils::get_special_pool_image_names(vm
, &arg_index
,
143 std::cerr
<< "rbd: image add error: " << cpp_strerror(r
) << std::endl
;
147 librados::Rados rados
;
149 librados::IoCtx cg_io_ctx
;
150 r
= utils::init(group_pool_name
, &rados
, &cg_io_ctx
);
155 librados::IoCtx image_io_ctx
;
156 r
= utils::init(image_pool_name
, &rados
, &image_io_ctx
);
162 r
= rbd
.group_image_add(cg_io_ctx
, group_name
.c_str(),
163 image_io_ctx
, image_name
.c_str());
165 std::cerr
<< "rbd: add image error: " << cpp_strerror(r
) << std::endl
;
172 int execute_remove_image(const po::variables_map
&vm
) {
173 size_t arg_index
= 0;
175 std::string group_name
;
176 std::string group_pool_name
;
178 int r
= utils::get_special_pool_group_names(vm
, &arg_index
,
182 std::cerr
<< "rbd: image remove error: " << cpp_strerror(r
) << std::endl
;
186 std::string image_name
;
187 std::string image_pool_name
;
188 std::string image_id
;
190 if (vm
.count(at::IMAGE_ID
)) {
191 image_id
= vm
[at::IMAGE_ID
].as
<std::string
>();
194 bool has_image_spec
= utils::check_if_image_spec_present(
195 vm
, at::ARGUMENT_MODIFIER_NONE
, arg_index
);
197 if (!image_id
.empty() && has_image_spec
) {
198 std::cerr
<< "rbd: trying to access image using both name and id. "
203 if (image_id
.empty()) {
204 r
= utils::get_special_pool_image_names(vm
, &arg_index
, &image_pool_name
,
207 image_pool_name
= utils::get_pool_name(vm
, &arg_index
);
211 std::cerr
<< "rbd: image remove error: " << cpp_strerror(r
) << std::endl
;
215 librados::Rados rados
;
217 librados::IoCtx cg_io_ctx
;
218 r
= utils::init(group_pool_name
, &rados
, &cg_io_ctx
);
223 librados::IoCtx image_io_ctx
;
224 r
= utils::init(image_pool_name
, &rados
, &image_io_ctx
);
230 if (image_id
.empty()) {
231 r
= rbd
.group_image_remove(cg_io_ctx
, group_name
.c_str(),
232 image_io_ctx
, image_name
.c_str());
234 r
= rbd
.group_image_remove_by_id(cg_io_ctx
, group_name
.c_str(),
235 image_io_ctx
, image_id
.c_str());
238 std::cerr
<< "rbd: remove image error: " << cpp_strerror(r
) << std::endl
;
245 int execute_list_images(const po::variables_map
&vm
) {
246 size_t arg_index
= 0;
247 std::string group_name
;
248 std::string pool_name
;
250 int r
= utils::get_pool_group_names(vm
, at::ARGUMENT_MODIFIER_NONE
,
251 &arg_index
, &pool_name
, &group_name
);
256 if (group_name
.empty()) {
258 << "consistency group name was not specified" << std::endl
;
262 at::Format::Formatter formatter
;
263 r
= utils::get_formatter(vm
, &formatter
);
267 Formatter
*f
= formatter
.get();
269 librados::Rados rados
;
270 librados::IoCtx io_ctx
;
271 r
= utils::init(pool_name
, &rados
, &io_ctx
);
277 std::vector
<librbd::group_image_status_t
> images
;
279 r
= rbd
.group_image_list(io_ctx
, group_name
.c_str(), &images
);
288 f
->open_array_section("consistency_groups");
290 for (auto i
: images
) {
291 std::string image_name
= i
.name
;
292 int64_t pool_id
= i
.pool
;
294 std::string state_string
;
295 if (cls::rbd::GROUP_IMAGE_LINK_STATE_INCOMPLETE
== state
) {
296 state_string
= "incomplete";
299 f
->dump_string("image name", image_name
);
300 f
->dump_int("pool id", pool_id
);
301 f
->dump_int("state", state
);
303 std::cout
<< pool_id
<< "." << image_name
<< " " << state_string
<< std::endl
;
314 void get_create_arguments(po::options_description
*positional
,
315 po::options_description
*options
) {
316 at::add_group_spec_options(positional
, options
, at::ARGUMENT_MODIFIER_NONE
);
319 void get_remove_arguments(po::options_description
*positional
,
320 po::options_description
*options
) {
321 at::add_group_spec_options(positional
, options
, at::ARGUMENT_MODIFIER_NONE
);
324 void get_list_arguments(po::options_description
*positional
,
325 po::options_description
*options
) {
326 add_pool_option(options
, at::ARGUMENT_MODIFIER_NONE
);
327 at::add_format_options(options
);
330 void get_add_arguments(po::options_description
*positional
,
331 po::options_description
*options
) {
332 positional
->add_options()
333 (at::GROUP_SPEC
.c_str(),
334 "group specification\n"
335 "(example: [<pool-name>/]<group-name>)");
337 at::add_special_pool_option(options
, "group");
338 at::add_group_option(options
, at::ARGUMENT_MODIFIER_NONE
);
340 positional
->add_options()
341 (at::IMAGE_SPEC
.c_str(),
342 "image specification\n"
343 "(example: [<pool-name>/]<image-name>)");
345 at::add_special_pool_option(options
, "image");
346 at::add_image_option(options
, at::ARGUMENT_MODIFIER_NONE
);
348 at::add_pool_option(options
, at::ARGUMENT_MODIFIER_NONE
,
349 " unless overridden");
352 void get_remove_image_arguments(po::options_description
*positional
,
353 po::options_description
*options
) {
354 positional
->add_options()
355 (at::GROUP_SPEC
.c_str(),
356 "group specification\n"
357 "(example: [<pool-name>/]<group-name>)");
359 at::add_special_pool_option(options
, "group");
360 at::add_group_option(options
, at::ARGUMENT_MODIFIER_NONE
);
362 positional
->add_options()
363 (at::IMAGE_SPEC
.c_str(),
364 "image specification\n"
365 "(example: [<pool-name>/]<image-name>)");
367 at::add_special_pool_option(options
, "image");
368 at::add_image_option(options
, at::ARGUMENT_MODIFIER_NONE
);
370 at::add_pool_option(options
, at::ARGUMENT_MODIFIER_NONE
,
371 " unless overridden");
372 at::add_image_id_option(options
);
375 void get_list_images_arguments(po::options_description
*positional
,
376 po::options_description
*options
) {
377 at::add_format_options(options
);
378 at::add_group_spec_options(positional
, options
, at::ARGUMENT_MODIFIER_NONE
);
381 Shell::Action
action_create(
382 {"group", "create"}, {}, "Create a consistency group.",
383 "", &get_create_arguments
, &execute_create
);
384 Shell::Action
action_remove(
385 {"group", "remove"}, {"group", "rm"}, "Delete a consistency group.",
386 "", &get_remove_arguments
, &execute_remove
);
387 Shell::Action
action_list(
388 {"group", "list"}, {"group", "ls"}, "List rbd consistency groups.",
389 "", &get_list_arguments
, &execute_list
);
390 Shell::Action
action_add(
391 {"group", "image", "add"}, {}, "Add an image to a consistency group.",
392 "", &get_add_arguments
, &execute_add
);
393 Shell::Action
action_remove_image(
394 {"group", "image", "remove"}, {}, "Remove an image from a consistency group.",
395 "", &get_remove_image_arguments
, &execute_remove_image
);
396 Shell::Action
action_list_images(
397 {"group", "image", "list"}, {}, "List images in a consistency group.",
398 "", &get_list_images_arguments
, &execute_list_images
);
400 } // namespace action