]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
rbd: permit removal of snapshots by snap-id
authorJason Dillaman <dillaman@redhat.com>
Mon, 25 Jun 2018 21:04:21 +0000 (17:04 -0400)
committerJason Dillaman <dillaman@redhat.com>
Tue, 24 Jul 2018 12:52:33 +0000 (08:52 -0400)
Signed-off-by: Jason Dillaman <dillaman@redhat.com>
src/test/cli/rbd/help.t
src/tools/rbd/action/Snap.cc

index 2304409dcfde7965e7d99938c483beb6fd0b5db9..e7d6aff7b621e7aba76a5108224f4f9a45f3e14f 100644 (file)
   
   rbd help snap remove
   usage: rbd snap remove [--pool <pool>] [--namespace <namespace>] 
-                         [--image <image>] [--snap <snap>] [--no-progress] 
-                         [--image-id <image-id>] [--force] 
+                         [--image <image>] [--snap <snap>] 
+                         [--image-id <image-id>] [--snap-id <snap-id>] 
+                         [--no-progress] [--force] 
                          <snap-spec> 
   
   Delete a snapshot.
     --namespace arg      namespace name
     --image arg          image name
     --snap arg           snapshot name
-    --no-progress        disable progress output
     --image-id arg       image id
+    --snap-id arg        snapshot id
+    --no-progress        disable progress output
     --force              flatten children and unprotect snapshot if needed.
   
   rbd help snap rename
index 3e49e1ef8b212cd6e57f074002fad3d128b353a7..df337b66b918dbbf7ef7cc669b427a64fe5b3255 100644 (file)
@@ -376,8 +376,9 @@ int execute_create(const po::variables_map &vm,
 void get_remove_arguments(po::options_description *positional,
                           po::options_description *options) {
   at::add_snap_spec_options(positional, options, at::ARGUMENT_MODIFIER_NONE);
-  at::add_no_progress_option(options);
   at::add_image_id_option(options);
+  at::add_snap_id_option(options);
+  at::add_no_progress_option(options);
 
   options->add_options()
     ("force", po::bool_switch(), "flatten children and unprotect snapshot if needed.");
@@ -391,24 +392,39 @@ int execute_remove(const po::variables_map &vm,
   std::string image_name;
   std::string snap_name;
   std::string image_id;
+  uint64_t snap_id = CEPH_NOSNAP;
   bool force = vm["force"].as<bool>();
+  bool no_progress = vm[at::NO_PROGRESS].as<bool>();
 
   if (vm.count(at::IMAGE_ID)) {
     image_id = vm[at::IMAGE_ID].as<std::string>();
   }
+  if (vm.count(at::SNAPSHOT_ID)) {
+    snap_id = vm[at::SNAPSHOT_ID].as<uint64_t>();
+  }
 
   int r = utils::get_pool_image_snapshot_names(
     vm, at::ARGUMENT_MODIFIER_NONE, &arg_index, &pool_name, &namespace_name,
     &image_name, &snap_name, image_id.empty(),
-    utils::SNAPSHOT_PRESENCE_REQUIRED, utils::SPEC_VALIDATION_NONE);
+    (snap_id == CEPH_NOSNAP ? utils::SNAPSHOT_PRESENCE_REQUIRED :
+                              utils::SNAPSHOT_PRESENCE_PERMITTED),
+    utils::SPEC_VALIDATION_NONE);
   if (r < 0) {
     return r;
   }
 
   if (!image_id.empty() && !image_name.empty()) {
-    std::cerr << "rbd: trying to access image using both name and id. "
+    std::cerr << "rbd: trying to access image using both name and id."
               << std::endl;
     return -EINVAL;
+  } else if (!snap_name.empty() && snap_id != CEPH_NOSNAP) {
+    std::cerr << "rbd: trying to access snapshot using both name and id."
+              << std::endl;
+    return -EINVAL;
+  } else if ((force || no_progress) && snap_id != CEPH_NOSNAP) {
+    std::cerr << "rbd: force and no-progress options not permitted when "
+              << "removing by id." << std::endl;
+    return -EINVAL;
   }
 
   librados::Rados rados;
@@ -429,11 +445,18 @@ int execute_remove(const po::variables_map &vm,
     return r;
   }
 
-  r = do_remove_snap(image, snap_name.c_str(), force, vm[at::NO_PROGRESS].as<bool>());
+  if (!snap_name.empty()) {
+    r = do_remove_snap(image, snap_name.c_str(), force, no_progress);
+  } else {
+    r = image.snap_remove_by_id(snap_id);
+  }
+
   if (r < 0) {
     if (r == -EBUSY) {
-      std::cerr << "rbd: snapshot '" << snap_name << "' "
-                << "is protected from removal." << std::endl;
+      std::cerr << "rbd: snapshot "
+                << (snap_name.empty() ? std::string("id ") + stringify(snap_id) :
+                                        std::string("'") + snap_name + "'")
+                << " is protected from removal." << std::endl;
     } else {
       std::cerr << "rbd: failed to remove snapshot: " << cpp_strerror(r)
                 << std::endl;