From 2cfce45f8d93f8ad5236fad21425f69841476663 Mon Sep 17 00:00:00 2001 From: Jason Dillaman Date: Wed, 11 Sep 2019 21:17:52 -0400 Subject: [PATCH] rbd: display and configure local mirroring site name The 'rbd mirror pool info' and 'rbd mirror pool enable' commands have been updated to display and optionally set the site name. Signed-off-by: Jason Dillaman --- src/test/cli/rbd/help.t | 2 + src/tools/rbd/action/MirrorPool.cc | 100 +++++++++++++++++++++++------ 2 files changed, 81 insertions(+), 21 deletions(-) diff --git a/src/test/cli/rbd/help.t b/src/test/cli/rbd/help.t index 4f1f266b22628..be724d4907800 100644 --- a/src/test/cli/rbd/help.t +++ b/src/test/cli/rbd/help.t @@ -1631,6 +1631,7 @@ rbd help mirror pool enable usage: rbd mirror pool enable [--pool ] [--namespace ] + [--site-name ] Enable RBD mirroring by default within a pool. @@ -1643,6 +1644,7 @@ Optional arguments -p [ --pool ] arg pool name --namespace arg namespace name + --site-name arg local site name rbd help mirror pool info usage: rbd mirror pool info [--pool ] [--namespace ] diff --git a/src/tools/rbd/action/MirrorPool.cc b/src/tools/rbd/action/MirrorPool.cc index ed6605da1896d..45a510fd405b7 100644 --- a/src/tools/rbd/action/MirrorPool.cc +++ b/src/tools/rbd/action/MirrorPool.cc @@ -39,9 +39,30 @@ namespace at = argument_types; namespace po = boost::program_options; static const std::string ALL_NAME("all"); +static const std::string SITE_NAME("site-name"); namespace { +void add_site_name_optional(po::options_description *options) { + options->add_options() + (SITE_NAME.c_str(), po::value(), "local site name"); +} + +int set_site_name(librados::Rados& rados, const std::string& site_name) { + librbd::RBD rbd; + int r = rbd.mirror_site_name_set(rados, site_name); + if (r == -EOPNOTSUPP) { + std::cerr << "rbd: cluster does not support site names" << std::endl; + return r; + } else if (r < 0) { + std::cerr << "rbd: failed to set site name" << cpp_strerror(r) + << std::endl; + return r; + } + + return 0; +} + int validate_mirroring_enabled(librados::IoCtx& io_ctx) { librbd::RBD rbd; rbd_mirror_mode_t mirror_mode; @@ -868,23 +889,15 @@ void get_enable_arguments(po::options_description *positional, at::add_pool_options(positional, options, true); positional->add_options() ("mode", "mirror mode [image or pool]"); + add_site_name_optional(options); } -int execute_enable_disable(const std::string &pool_name, - const std::string &namespace_name, +int execute_enable_disable(librados::IoCtx& io_ctx, rbd_mirror_mode_t next_mirror_mode, - const std::string &mode) { - librados::Rados rados; - librados::IoCtx io_ctx; - rbd_mirror_mode_t current_mirror_mode; - - int r = utils::init(pool_name, namespace_name, &rados, &io_ctx); - if (r < 0) { - return r; - } - + const std::string &mode, bool ignore_no_update) { librbd::RBD rbd; - r = rbd.mirror_mode_get(io_ctx, ¤t_mirror_mode); + rbd_mirror_mode_t current_mirror_mode; + int r = rbd.mirror_mode_get(io_ctx, ¤t_mirror_mode); if (r < 0) { std::cerr << "rbd: failed to retrieve mirror mode: " << cpp_strerror(r) << std::endl; @@ -892,11 +905,13 @@ int execute_enable_disable(const std::string &pool_name, } if (current_mirror_mode == next_mirror_mode) { - if (mode == "disabled") { - std::cout << "mirroring is already " << mode << std::endl; - } else { - std::cout << "mirroring is already configured for " - << mode << " mode" << std::endl; + if (!ignore_no_update) { + if (mode == "disabled") { + std::cout << "rbd: mirroring is already " << mode << std::endl; + } else { + std::cout << "rbd: mirroring is already configured for " + << mode << " mode" << std::endl; + } } return 0; } else if (next_mirror_mode == RBD_MIRROR_MODE_IMAGE && @@ -927,8 +942,16 @@ int execute_disable(const po::variables_map &vm, return r; } - return execute_enable_disable(pool_name, namespace_name, - RBD_MIRROR_MODE_DISABLED, "disabled"); + librados::Rados rados; + librados::IoCtx io_ctx; + + r = utils::init(pool_name, namespace_name, &rados, &io_ctx); + if (r < 0) { + return r; + } + + return execute_enable_disable(io_ctx, RBD_MIRROR_MODE_DISABLED, "disabled", + false); } int execute_enable(const po::variables_map &vm, @@ -953,7 +976,30 @@ int execute_enable(const po::variables_map &vm, return -EINVAL; } - return execute_enable_disable(pool_name, namespace_name, mirror_mode, mode); + librados::Rados rados; + librados::IoCtx io_ctx; + + r = utils::init(pool_name, namespace_name, &rados, &io_ctx); + if (r < 0) { + return r; + } + + bool updated = false; + if (vm.count(SITE_NAME)) { + librbd::RBD rbd; + + auto site_name = vm[SITE_NAME].as(); + std::string original_site_name; + r = rbd.mirror_site_name_get(rados, &original_site_name); + updated = (r >= 0 && site_name != original_site_name); + + r = set_site_name(rados, site_name); + if (r < 0) { + return r; + } + } + + return execute_enable_disable(io_ctx, mirror_mode, mode, updated); } void get_info_arguments(po::options_description *positional, @@ -995,6 +1041,12 @@ int execute_info(const po::variables_map &vm, return r; } + std::string site_name; + r = rbd.mirror_site_name_get(rados, &site_name); + if (r < 0 && r != -EOPNOTSUPP) { + return r; + } + std::vector mirror_peers; if (namespace_name.empty()) { r = rbd.mirror_peer_list(io_ctx, &mirror_peers); @@ -1027,6 +1079,12 @@ int execute_info(const po::variables_map &vm, } if (mirror_mode != RBD_MIRROR_MODE_DISABLED && namespace_name.empty()) { + if (formatter != nullptr) { + formatter->dump_string("site_name", site_name); + } else { + std::cout << "Site Name: " << site_name << std::endl; + } + r = format_mirror_peers(io_ctx, formatter, mirror_peers, vm[ALL_NAME].as()); if (r < 0) { -- 2.39.5