From 34734dd2b4355ce5d149362e8e611e5d4bec92b6 Mon Sep 17 00:00:00 2001 From: Mykola Golub Date: Sun, 10 Nov 2019 10:17:44 +0000 Subject: [PATCH] rbd: add 'mirror image snapshot' command Signed-off-by: Mykola Golub --- src/test/cli/rbd/help.t | 17 ++++++++ src/tools/rbd/action/MirrorImage.cc | 65 ++++++++++++++++++++++++++++- 2 files changed, 80 insertions(+), 2 deletions(-) diff --git a/src/test/cli/rbd/help.t b/src/test/cli/rbd/help.t index d63edc7c0b660..7262ffa88f73e 100644 --- a/src/test/cli/rbd/help.t +++ b/src/test/cli/rbd/help.t @@ -87,6 +87,7 @@ mirroring. mirror image resync Force resync to primary image for RBD mirroring. + mirror image snapshot Create RBD mirroring image snapshot. mirror image status Show RBD mirroring status for an image. mirror pool demote Demote all primary images in the pool. mirror pool disable Disable RBD mirroring by default within a @@ -1579,6 +1580,22 @@ Force resync to primary image for RBD mirroring. + Positional arguments + image specification + (example: [/[/]]) + + Optional arguments + -p [ --pool ] arg pool name + --namespace arg namespace name + --image arg image name + + rbd help mirror image snapshot + usage: rbd mirror image snapshot [--pool ] [--namespace ] + [--image ] + + + Create RBD mirroring image snapshot. + Positional arguments image specification (example: [/[/]]) diff --git a/src/tools/rbd/action/MirrorImage.cc b/src/tools/rbd/action/MirrorImage.cc index bd77742750db5..6d3698b56863c 100644 --- a/src/tools/rbd/action/MirrorImage.cc +++ b/src/tools/rbd/action/MirrorImage.cc @@ -33,11 +33,11 @@ namespace po = boost::program_options; namespace { -int validate_mirroring_enabled(librbd::Image& image) { +int validate_mirroring_enabled(librbd::Image &image, bool snapshot = false) { librbd::mirror_image_info_t mirror_image; int r = image.mirror_image_get_info(&mirror_image, sizeof(mirror_image)); if (r < 0) { - std::cerr << "rbd: failed to retrieve mirror mode: " + std::cerr << "rbd: failed to retrieve mirror info: " << cpp_strerror(r) << std::endl; return r; } @@ -46,6 +46,23 @@ int validate_mirroring_enabled(librbd::Image& image) { std::cerr << "rbd: mirroring not enabled on the image" << std::endl; return -EINVAL; } + + if (snapshot) { + librbd::mirror_image_mode_t mode; + r = image.mirror_image_get_mode(&mode); + if (r < 0) { + std::cerr << "rbd: failed to retrieve mirror mode: " + << cpp_strerror(r) << std::endl; + return r; + } + + if (mode != RBD_MIRROR_IMAGE_MODE_SNAPSHOT) { + std::cerr << "rbd: snapshot based mirroring not enabled on the image" + << std::endl; + return -EINVAL; + } + } + return 0; } @@ -397,6 +414,46 @@ int execute_status(const po::variables_map &vm, return 0; } +int execute_snapshot(const po::variables_map &vm, + const std::vector &ceph_global_init_args) { + size_t arg_index = 0; + std::string pool_name; + std::string namespace_name; + std::string image_name; + int r = utils::get_pool_image_snapshot_names( + vm, at::ARGUMENT_MODIFIER_NONE, &arg_index, &pool_name, &namespace_name, + &image_name, nullptr, true, utils::SNAPSHOT_PRESENCE_NONE, + utils::SPEC_VALIDATION_NONE); + if (r < 0) { + return r; + } + + librados::Rados rados; + librados::IoCtx io_ctx; + librbd::Image image; + r = utils::init_and_open_image(pool_name, namespace_name, image_name, "", "", + false, &rados, &io_ctx, &image); + if (r < 0) { + return r; + } + + r = validate_mirroring_enabled(image, true); + if (r < 0) { + return r; + } + + uint64_t snap_id; + r = image.mirror_image_create_snapshot(&snap_id); + if (r < 0) { + std::cerr << "rbd: error creating snapshot: " << cpp_strerror(r) + << std::endl; + return r; + } + + std::cout << "Snapshot ID: " << snap_id << std::endl; + return 0; +} + Shell::Action action_enable( {"mirror", "image", "enable"}, {}, "Enable RBD mirroring for an image.", "", @@ -421,6 +478,10 @@ Shell::Action action_status( {"mirror", "image", "status"}, {}, "Show RBD mirroring status for an image.", "", &get_status_arguments, &execute_status); +Shell::Action action_snapshot( + {"mirror", "image", "snapshot"}, {}, + "Create RBD mirroring image snapshot.", "", + &get_arguments, &execute_snapshot); } // namespace mirror_image } // namespace action -- 2.39.5