]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
rbd: add 'mirror image snapshot' command
authorMykola Golub <mgolub@suse.com>
Sun, 10 Nov 2019 10:17:44 +0000 (10:17 +0000)
committerMykola Golub <mgolub@suse.com>
Tue, 10 Dec 2019 15:45:30 +0000 (15:45 +0000)
Signed-off-by: Mykola Golub <mgolub@suse.com>
src/test/cli/rbd/help.t
src/tools/rbd/action/MirrorImage.cc

index d63edc7c0b6607689b636c6e5c10a914c5f28c74..7262ffa88f73ed4707779f6789b7f8a9e564983e 100644 (file)
@@ -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
   
   Force resync to primary image for RBD mirroring.
   
+  Positional arguments
+    <image-spec>         image specification
+                         (example: [<pool-name>/[<namespace>/]]<image-name>)
+  
+  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 <pool>] [--namespace <namespace>] 
+                                   [--image <image>] 
+                                   <image-spec> 
+  
+  Create RBD mirroring image snapshot.
+  
   Positional arguments
     <image-spec>         image specification
                          (example: [<pool-name>/[<namespace>/]]<image-name>)
index bd77742750db55e9736c4efc0e79bebe5f15aaf4..6d3698b56863ccfb7b1c183094d5a35b1ede1f57 100644 (file)
@@ -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<std::string> &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