Skip to contentSkip to conversation

rbd.cc: add --force option at 'rbd import'

PATCH In review Dimitris Bliablias 2 participants 4 messages Jun 18, 2014 – Aug 6, 2014

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 rbd.cc: add --force option at 'rbd import'

Extend the rbd utility with a new option named '--force'. This option will be used by the 'rbd import' command to allow overwriting an existing rbd image, something which is currently forbidden. If the image has snapshots, the command returns an error and nothing is imported. The force option will first remove the existing rbd image and then recreate it before importing the new data.

This patch also updates the tests affected by the modified functions, to reflect the latest changes. In addition, updates the rbd man page accordingly to record the new '--force' option.

---
 doc/man/8/rbd.rst                    |  12 +++-
 qa/workunits/rbd/import_export.sh    |   6 ++
 src/include/rbd/librbd.h             |   9 ++-
 src/include/rbd/librbd.hpp           |   7 +-
 src/librbd/internal.cc               |  61 +++++++++++++---
 src/librbd/internal.h                |   4 +-
 src/librbd/librbd.cc                 |  29 ++++----
 src/pybind/rbd.py                    |  11 +--
 src/rbd.cc                           |  27 +++++---
 src/rbd_fuse/rbd-fuse.c              |   2 +-
 src/test/bench/small_io_bench_rbd.cc |   2 +-
 src/test/cli/rbd/help.t              |   3 +-
 src/test/librbd/fsx.c                |   5 +-
 src/test/librbd/test_librbd.cc       | 131 +++++++++++++++++++++++++----------
 14 files changed, 227 insertions(+), 82 deletions(-)
doc/man/8/rbd.rst +11 -1
index 215aa92..95cd7d3 100644
@@ -123,6 +123,11 @@ Parameters
 
    Map the image read-only.  Equivalent to -o ro.
 
+.. option:: --f, --force
+
+   Overwrite an existing rbd image. This option is only used by the rbd import
+   command. See import section (below) for more details.
+
 
 Commands
 ========
@@ -176,11 +181,16 @@ Commands
 :command:`export` [*image-name*] [*dest-path*]
   Exports image to dest path (use - for stdout).
 
-:command:`import` [*path*] [*dest-image*]
+:command:`import` [*path*] [*dest-image*] [--force]
   Creates a new image and imports its data from path (use - for
   stdin).  The import operation will try to create sparse rbd images 
   if possible.  For import from stdin, the sparsification unit is
   the data block size of the destination image (1 << order).
+  Using the --force option will overwrite the destination image if it exists.
+  Overwriting images using a different format is not supported. If the image
+  has snapshots, import fails and nothing is imported. (Note: using --force
+  will first remove the original rbd image, and then re-create it to import
+  the data from path.
 
 :command:`export-diff` [*image-name*] [*dest-path*] [--from-snap *snapname*]
   Exports an incremental diff for an image to dest path (use - for stdout).  If
qa/workunits/rbd/import_export.sh +6 -0
index 29fcb6c..1b1b291 100755
@@ -54,6 +54,12 @@ rbd rm testimg
 cmp /tmp/img /tmp/img2
 cmp /tmp/img /tmp/img3
 
+# import using --force option
+rbd create testimg $RBD_CREATE_ARGS --size 10
+rbd import $RBD_CREATE_ARGS /tmp/img testimg && exit 1 || true   # should fail
+rbd import $RBD_CREATE_ARGS /tmp/img testimg --force
+rbd rm testimg
+
 rm /tmp/img /tmp/img2 /tmp/img3
 
 
src/include/rbd/librbd.h +6 -3
index a9a3318..9da6f5d 100644
@@ -25,6 +25,7 @@ extern "C" {
 #elif defined(__FreeBSD__)
 #include <sys/types.h>
 #endif
+#include <stdbool.h>
 #include <string.h>
 #include "../rados/librados.h"
 #include "features.h"
@@ -69,9 +70,10 @@ void rbd_version(int *major, int *minor, int *extra);
 
 /* images */
 int rbd_list(rados_ioctx_t io, char *names, size_t *size);
-int rbd_create(rados_ioctx_t io, const char *name, uint64_t size, int *order);
+int rbd_create(rados_ioctx_t io, const char *name, uint64_t size, int *order,
+	       bool force);
 int rbd_create2(rados_ioctx_t io, const char *name, uint64_t size,
-		uint64_t features, int *order);
+		uint64_t features, int *order, bool force);
 /**
  * create new rbd image
  *
@@ -87,11 +89,12 @@ int rbd_create2(rados_ioctx_t io, const char *name, uint64_t size,
  * @param order object/block size, as a power of two (object size == 1 << order)
  * @param stripe_unit stripe unit size, in bytes.
  * @param stripe_count number of objects to stripe over before looping
+ * @param force boolean, on true overwrite existing image
  * @return 0 on success, or negative error code
  */
 int rbd_create3(rados_ioctx_t io, const char *name, uint64_t size,
 		uint64_t features, int *order,
-		uint64_t stripe_unit, uint64_t stripe_count);
+		uint64_t stripe_unit, uint64_t stripe_count, bool force);
 int rbd_clone(rados_ioctx_t p_ioctx, const char *p_name,
 	      const char *p_snapname, rados_ioctx_t c_ioctx,
 	      const char *c_name, uint64_t features, int *c_order);
src/include/rbd/librbd.hpp +4 -3
index 7107f58..a17afc0 100644
@@ -77,12 +77,13 @@ public:
   int open_read_only(IoCtx& io_ctx, Image& image, const char *name,
 		     const char *snapname);
   int list(IoCtx& io_ctx, std::vector<std::string>& names);
-  int create(IoCtx& io_ctx, const char *name, uint64_t size, int *order);
+  int create(IoCtx& io_ctx, const char *name, uint64_t size, int *order,
+	     bool force);
   int create2(IoCtx& io_ctx, const char *name, uint64_t size,
-	      uint64_t features, int *order);
+	      uint64_t features, int *order, bool force);
   int create3(IoCtx& io_ctx, const char *name, uint64_t size,
 	      uint64_t features, int *order,
-	      uint64_t stripe_unit, uint64_t stripe_count);
+	      uint64_t stripe_unit, uint64_t stripe_count, bool force);
   int clone(IoCtx& p_ioctx, const char *p_name, const char *p_snapname,
 	       IoCtx& c_ioctx, const char *c_name, uint64_t features,
 	       int *c_order);
src/librbd/internal.cc +53 -8

105 lines not on this page · raw message

src/librbd/internal.h +2 -2
index 6fb1af4..e79b90b 100644
@@ -81,10 +81,10 @@ namespace librbd {
   int list_children(ImageCtx *ictx,
 		    std::set<pair<std::string, std::string> > & names);
   int create(librados::IoCtx& io_ctx, const char *imgname, uint64_t size,
-	     int *order);
+	     int *order, bool force);
   int create(librados::IoCtx& io_ctx, const char *imgname, uint64_t size,
 	     bool old_format, uint64_t features, int *order,
-	     uint64_t stripe_unit, uint64_t stripe_count);
+	     uint64_t stripe_unit, uint64_t stripe_count, bool force);
   int clone(IoCtx& p_ioctx, const char *p_name, const char *p_snap_name,
 	    IoCtx& c_ioctx, const char *c_name,
 	    uint64_t features, int *c_order,
src/librbd/librbd.cc +17 -12
index b265343..7d2feda 100644
@@ -113,23 +113,25 @@ namespace librbd {
     return 0;
   }
 
-  int RBD::create(IoCtx& io_ctx, const char *name, uint64_t size, int *order)
+  int RBD::create(IoCtx& io_ctx, const char *name, uint64_t size, int *order,
+		  bool force)
   {
-    return librbd::create(io_ctx, name, size, order);
+    return librbd::create(io_ctx, name, size, order, force);
   }
 
   int RBD::create2(IoCtx& io_ctx, const char *name, uint64_t size,
-		   uint64_t features, int *order)
+		   uint64_t features, int *order, bool force)
   {
-    return librbd::create(io_ctx, name, size, false, features, order, 0, 0);
+    return librbd::create(io_ctx, name, size, false, features, order, 0, 0,
+			  force);
   }
 
   int RBD::create3(IoCtx& io_ctx, const char *name, uint64_t size,
 		   uint64_t features, int *order, uint64_t stripe_unit,
-		   uint64_t stripe_count)
+		   uint64_t stripe_count, bool force)
   {
     return librbd::create(io_ctx, name, size, false, features, order,
-			  stripe_unit, stripe_count);
+			  stripe_unit, stripe_count, force);
   }
 
   int RBD::clone(IoCtx& p_ioctx, const char *p_name, const char *p_snap_name,
@@ -562,31 +564,34 @@ extern "C" int rbd_list(rados_ioctx_t p, char *names, size_t *size)
   return (int)expected_size;
 }
 
-extern "C" int rbd_create(rados_ioctx_t p, const char *name, uint64_t size, int *order)
+extern "C" int rbd_create(rados_ioctx_t p, const char *name, uint64_t size,
+			  int *order, bool force)
 {
   librados::IoCtx io_ctx;
   librados::IoCtx::from_rados_ioctx_t(p, io_ctx);
-  return librbd::create(io_ctx, name, size, order);
+  return librbd::create(io_ctx, name, size, order, force);
 }
 
 extern "C" int rbd_create2(rados_ioctx_t p, const char *name,
 			   uint64_t size, uint64_t features,
-			   int *order)
+			   int *order, bool force)
 {
   librados::IoCtx io_ctx;
   librados::IoCtx::from_rados_ioctx_t(p, io_ctx);
-  return librbd::create(io_ctx, name, size, false, features, order, 0, 0);
+  return librbd::create(io_ctx, name, size, false, features, order, 0, 0,
+			force);
 }
 
 extern "C" int rbd_create3(rados_ioctx_t p, const char *name,
 			   uint64_t size, uint64_t features,
 			   int *order,
-			   uint64_t stripe_unit, uint64_t stripe_count)
+			   uint64_t stripe_unit, uint64_t stripe_count,
+			   bool force)
 {
   librados::IoCtx io_ctx;
   librados::IoCtx::from_rados_ioctx_t(p, io_ctx);
   return librbd::create(io_ctx, name, size, false, features, order,
-			stripe_unit, stripe_count);
+			stripe_unit, stripe_count, force);
 }
 
 extern "C" int rbd_clone(rados_ioctx_t p_ioctx, const char *p_name,
src/pybind/rbd.py +7 -4
index a3512ad..be88e47 100644
@@ -157,7 +157,7 @@ class RBD(object):
         return (major.value, minor.value, extra.value)
 
     def create(self, ioctx, name, size, order=None, old_format=True,
-               features=0, stripe_unit=0, stripe_count=0):
+               features=0, stripe_unit=0, stripe_count=0, force=False):
         """
         Create an rbd image.
 
@@ -179,6 +179,9 @@ class RBD(object):
         :type stripe_unit: int
         :param stripe_count: objects to stripe over before looping
         :type stripe_count: int
+        :param force: when True overwrite an existing image; currently this
+                      option is only available when importing an rbd image
+        :type force: bool
         :raises: :class:`ImageExists`
         :raises: :class:`TypeError`
         :raises: :class:`InvalidArgument`
@@ -194,7 +197,7 @@ class RBD(object):
                                       ' masks or non-default striping')
             ret = self.librbd.rbd_create(ioctx.io, c_char_p(name),
                                          c_uint64(size),
-                                         byref(c_int(order)))
+                                         byref(c_int(order)), force)
         else:
             if not hasattr(self.librbd, 'rbd_create2'):
                 raise FunctionNotSupported('installed version of librbd does'
@@ -209,12 +212,12 @@ class RBD(object):
                                               c_uint64(features),
                                               byref(c_int(order)),
                                               c_uint64(stripe_unit),
-                                              c_uint64(stripe_count))
+                                              c_uint64(stripe_count), force)
             else:
                 ret = self.librbd.rbd_create2(ioctx.io, c_char_p(name),
                                               c_uint64(size),
                                               c_uint64(features),
-                                              byref(c_int(order)))
+                                              byref(c_int(order)), force)
         if ret < 0:
             raise make_ex(ret, 'error creating image')
 
src/rbd.cc +19 -8
index d6658e3..df46c19 100644
@@ -89,7 +89,7 @@ void usage()
 "  rm <image-name>                             delete an image\n"
 "  export <image-name> <path>                  export image to file\n"
 "                                              \"-\" for stdout\n"
-"  import <path> <image-name>                  import image from file\n"
+"  import <path> <image-name> [--force]        import image from file\n"
 "                                              (dest defaults\n"
 "                                               as the filename part of file)\n"
 "                                              \"-\" for stdin\n"
@@ -150,6 +150,7 @@ void usage()
 "  --no-progress                      do not show progress for long-running commands\n"
 "  -o, --options <map-options>        options to use when mapping an image\n"
 "  --read-only                        set device readonly when mapping image\n"
+"  -f, --force                        overwrite an existing image when importing\n"
 "  --allow-shrink                     allow shrinking of an image when resizing\n";
 }
 
@@ -408,7 +409,7 @@ static int do_list(librbd::RBD &rbd, librados::IoCtx& io_ctx, bool lflag,
 static int do_create(librbd::RBD &rbd, librados::IoCtx& io_ctx,
 		     const char *imgname, uint64_t size, int *order,
 		     int format, uint64_t features,
-		     uint64_t stripe_unit, uint64_t stripe_count)
+		     uint64_t stripe_unit, uint64_t stripe_count, bool force)
 {
   int r;
 
@@ -420,7 +421,7 @@ static int do_create(librbd::RBD &rbd, librados::IoCtx& io_ctx,
 	   << std::endl;
       return -EINVAL;
     }
-    r = rbd.create(io_ctx, imgname, size, order);
+    r = rbd.create(io_ctx, imgname, size, order, force);
   } else {
     if (features == 0) {
       features = RBD_FEATURE_LAYERING;
@@ -430,7 +431,7 @@ static int do_create(librbd::RBD &rbd, librados::IoCtx& io_ctx,
       features |= RBD_FEATURE_STRIPINGV2;
     }
     r = rbd.create3(io_ctx, imgname, size, features, order,
-		    stripe_unit, stripe_count);
+		    stripe_unit, stripe_count, force);
   }
   if (r < 0)
     return r;
@@ -1290,7 +1291,7 @@ done_img:
 
 static int do_import(librbd::RBD &rbd, librados::IoCtx& io_ctx,
 		     const char *imgname, int *order, const char *path,
-		     int format, uint64_t features, uint64_t size)
+		     int format, uint64_t features, uint64_t size, bool force)
 {
   int fd, r;
   struct stat stat_buf;
@@ -1346,7 +1347,8 @@ static int do_import(librbd::RBD &rbd, librados::IoCtx& io_ctx,
       size = (uint64_t) bdev_size;
     }
   }
-  r = do_create(rbd, io_ctx, imgname, size, order, format, features, 0, 0);
+  r = do_create(rbd, io_ctx, imgname, size, order, format, features, 0, 0,
+		force);
   if (r < 0) {
     cerr << "rbd: image creation failed" << std::endl;
     goto done;
@@ -1972,6 +1974,7 @@ int main(int argc, const char **argv)
   int order = 0;
   bool format_specified = false, output_format_specified = false;
   int format = 1;
+  bool force = false;
   uint64_t features = RBD_FEATURE_LAYERING;
   const char *imgname = NULL, *snapname = NULL, *destname = NULL,
     *dest_poolname = NULL, *dest_snapname = NULL, *path = NULL,
@@ -2065,6 +2068,8 @@ int main(int argc, const char **argv)
       progress = false;
     } else if (ceph_argparse_flag(args, i , "--allow-shrink", (char *)NULL)) {
       resize_allow_shrink = true;
+    } else if (ceph_argparse_flag(args, i, "-f", "--force", (char *)NULL)) {
+      force = true;
     } else if (ceph_argparse_witharg(args, i, &val, "--format", (char *) NULL)) {
       std::string err;
       long long ret = strict_strtoll(val.c_str(), 10, &err);
@@ -2182,6 +2187,12 @@ if (!set_conf_param(v, p1, p2, p3)) { \
     }
   }
 
+  if (force && opt_cmd != OPT_IMPORT) {
+    cerr << "rbd: --force can only be used when"
+	 << " importing an image" << std::endl;
+    return EXIT_FAILURE;
+  }
+
   if (format_specified && opt_cmd != OPT_IMPORT && opt_cmd != OPT_CREATE) {
     cerr << "rbd: image format can only be set when "
 	 << "creating or importing an image" << std::endl;
@@ -2429,7 +2440,7 @@ if (!set_conf_param(v, p1, p2, p3)) { \
       return EINVAL;
     }
     r = do_create(rbd, io_ctx, imgname, size, &order, format, features,
-		  stripe_unit, stripe_count);
+		  stripe_unit, stripe_count, force);
     if (r < 0) {
       cerr << "rbd: create error: " << cpp_strerror(-r) << std::endl;
       return -r;
@@ -2655,7 +2666,7 @@ if (!set_conf_param(v, p1, p2, p3)) { \
       return EINVAL;
     }
     r = do_import(rbd, dest_io_ctx, destname, &order, path,
-		  format, features, size);
+		  format, features, size, force);
     if (r < 0) {
       cerr << "rbd: import failed: " << cpp_strerror(-r) << std::endl;
       return -r;
src/rbd_fuse/rbd-fuse.c +1 -1
index 6fc84f2..3499977 100644
@@ -499,7 +499,7 @@ rbdfs_create(const char *path, mode_t mode, struct fuse_file_info *fi)
 	int r;
 	int order = imageorder;
 
-	r = rbd_create2(ioctx, path+1, imagesize, imagefeatures, &order);
+	r = rbd_create2(ioctx, path+1, imagesize, imagefeatures, &order, false);
 	return r;
 }
 
src/test/bench/small_io_bench_rbd.cc +1 -1
index ba7071e..a94d14b 100644
@@ -137,7 +137,7 @@ int main(int argc, char **argv)
     uint64_t image_size = ((uint64_t)vm["image-size"].as<unsigned>()) << 20;
     for (set<string>::const_iterator i = image_names.begin();
 	 i != image_names.end(); ++i) {
-      r = rbd.create(ioctx, i->c_str(), image_size, &order);
+      r = rbd.create(ioctx, i->c_str(), image_size, &order, false);
       if (r < 0) {
 	cerr << "error creating image " << *i << " r=" << r << std::endl;
 	return -r;
src/test/cli/rbd/help.t +2 -1
index 3818ee3..38f1f1b 100644
@@ -16,7 +16,7 @@
     rm <image-name>                             delete an image
     export <image-name> <path>                  export image to file
                                                 "-" for stdout
-    import <path> <image-name>                  import image from file
+    import <path> <image-name> [--force]        import image from file
                                                 (dest defaults
                                                  as the filename part of file)
                                                 "-" for stdin
@@ -77,4 +77,5 @@
     --no-progress                      do not show progress for long-running commands
     -o, --options <map-options>        options to use when mapping an image
     --read-only                        set device readonly when mapping image
+    -f, --force                        overwrite an existing image when importing
     --allow-shrink                     allow shrinking of an image when resizing
src/test/librbd/fsx.c +3 -2
index a811a12..e1a823f 100644
@@ -1010,9 +1010,10 @@ create_image()
 		goto failed_krbd;
 	}
 	if (clone_calls) {
-		r = rbd_create2(ioctx, iname, 0, RBD_FEATURE_LAYERING, &order);
+		r = rbd_create2(ioctx, iname, 0, RBD_FEATURE_LAYERING, &order,
+				false);
 	} else {
-		r = rbd_create(ioctx, iname, 0, &order);
+		r = rbd_create(ioctx, iname, 0, &order, false);
 	}
 	if (r < 0) {
 		simple_err("Error creating image", r);
src/test/librbd/test_librbd.cc +95 -36

326 lines not on this page · raw message

Signature · 1 line
1.9.1
#1 lore raw
Quoted 6 lines · Dimitris Bliablias

Extend the rbd utility with a new option named '--force'. This option will be used by the 'rbd import' command to allow overwriting an existing rbd image, something which is currently forbidden. If the image has snapshots, the command returns an error and nothing is imported. The force option will first remove the existing rbd image and then recreate it before importing the new data.

It makes sense to add a --force option to rbd import.

Quoted 3 lines

This patch also updates the tests affected by the modified functions, to reflect the latest changes. In addition, updates the rbd man page accordingly to record the new '--force' option.

Excellent, it's great to see tests, all bindings, and the man page updated in the right place.

Quoted 82 lines
---
  doc/man/8/rbd.rst                    |  12 +++-
  qa/workunits/rbd/import_export.sh    |   6 ++
  src/include/rbd/librbd.h             |   9 ++-
  src/include/rbd/librbd.hpp           |   7 +-
  src/librbd/internal.cc               |  61 +++++++++++++---
  src/librbd/internal.h                |   4 +-
  src/librbd/librbd.cc                 |  29 ++++----
  src/pybind/rbd.py                    |  11 +--
  src/rbd.cc                           |  27 +++++---
  src/rbd_fuse/rbd-fuse.c              |   2 +-
  src/test/bench/small_io_bench_rbd.cc |   2 +-
  src/test/cli/rbd/help.t              |   3 +-
  src/test/librbd/fsx.c                |   5 +-
  src/test/librbd/test_librbd.cc       | 131 +++++++++++++++++++++++++----------
  14 files changed, 227 insertions(+), 82 deletions(-)
doc/man/8/rbd.rst +11 -1
index 215aa92..95cd7d3 100644
@@ -123,6 +123,11 @@ Parameters
 
     Map the image read-only.  Equivalent to -o ro.
 
+.. option:: --f, --force
+
+   Overwrite an existing rbd image. This option is only used by the rbd import
+   command. See import section (below) for more details.
+
 
  Commands
  ========
@@ -176,11 +181,16 @@ Commands
  :command:`export` [*image-name*] [*dest-path*]
    Exports image to dest path (use - for stdout).
 
-:command:`import` [*path*] [*dest-image*]
+:command:`import` [*path*] [*dest-image*] [--force]
    Creates a new image and imports its data from path (use - for
    stdin).  The import operation will try to create sparse rbd images
    if possible.  For import from stdin, the sparsification unit is
    the data block size of the destination image (1 << order).
+  Using the --force option will overwrite the destination image if it exists.
+  Overwriting images using a different format is not supported. If the image
+  has snapshots, import fails and nothing is imported. (Note: using --force
+  will first remove the original rbd image, and then re-create it to import
+  the data from path.
 
  :command:`export-diff` [*image-name*] [*dest-path*] [--from-snap *snapname*]
    Exports an incremental diff for an image to dest path (use - for stdout).  If
qa/workunits/rbd/import_export.sh +6 -0
index 29fcb6c..1b1b291 100755
@@ -54,6 +54,12 @@ rbd rm testimg
  cmp /tmp/img /tmp/img2
  cmp /tmp/img /tmp/img3
 
+# import using --force option
+rbd create testimg $RBD_CREATE_ARGS --size 10
+rbd import $RBD_CREATE_ARGS /tmp/img testimg && exit 1 || true   # should fail
+rbd import $RBD_CREATE_ARGS /tmp/img testimg --force
+rbd rm testimg
+
  rm /tmp/img /tmp/img2 /tmp/img3
 
 
src/include/rbd/librbd.h +1 -0
index a9a3318..9da6f5d 100644
@@ -25,6 +25,7 @@ extern "C" {
  #elif defined(__FreeBSD__)
  #include <sys/types.h>
  #endif
+#include <stdbool.h>
  #include <string.h>
  #include "../rados/librados.h"
  #include "features.h"

src/include/rbd/librbd.h void rbd_version(int *major, int *minor, int *extra); · line 76

Quoted 9 lines
src/include/rbd/librbd.h +3 -2
@@ -69,9 +70,10 @@ void rbd_version(int *major, int *minor, int *extra);
 
  /* images */
  int rbd_list(rados_ioctx_t io, char *names, size_t *size);
-int rbd_create(rados_ioctx_t io, const char *name, uint64_t size, int *order);
+int rbd_create(rados_ioctx_t io, const char *name, uint64_t size, int *order,
+	       bool force);
  int rbd_create2(rados_ioctx_t io, const char *name, uint64_t size,
-		uint64_t features, int *order);
+		uint64_t features, int *order, bool force);

However, this is changing the ABI, which breaks existing users. Why not implement this in the command line tool instead of as part of librbd? Trying to create(), and then removing and recreating if EEXIST is returned should work just as well.

Josh

#2 lore raw

PATCH 1/1 rbd.cc: add --force option at 'rbd import'

Extend the rbd utility with a new option named '--force'. This option will be used by the 'rbd import' command to allow overwriting an existing rbd image, something which is currently forbidden. If the image has snapshots, the command returns an error and nothing is imported. The force option will first remove the existing rbd image and then recreate it before importing the new data.

This patch also updates the tests affected by the modified functions, to reflect the latest changes. In addition, updates the rbd man page accordingly to record the new '--force' option.

---
Hello,

I've refactored the patch as you suggested, but I had to also drop the '-f' option and keep only the '--force' one. This was made due to the fact that the '-f' option is currently used along with the '--foreground' option that specifies whether the Ceph daemons should run in the foreground or not. The 'parse_argv' function of the 'src/common/config.h' file is called before the argument parsing of the rbd utility command line options and consequently it consumes the '-f' argument whenever it is supplied. Not sure if this is the intended behavior for the '-f' option.

Thanks,
Dimitris

 doc/man/8/rbd.rst                 | 12 +++++++-
 qa/workunits/rbd/import_export.sh |  6 ++++
 src/rbd.cc                        | 65 +++++++++++++++++++++++++++++++++++++--
 src/test/cli/rbd/help.t           |  3 +-
 4 files changed, 81 insertions(+), 5 deletions(-)
doc/man/8/rbd.rst +11 -1
index 5424547..46d7f21 100644
@@ -123,6 +123,11 @@ Parameters
 
    Map the image read-only.  Equivalent to -o ro.
 
+.. option:: --force
+
+   Overwrite an existing rbd image. This option is only used by the rbd import
+   command. See import section (below) for more details.
+
 
 Commands
 ========
@@ -176,11 +181,16 @@ Commands
 :command:`export` [*image-name*] [*dest-path*]
   Exports image to dest path (use - for stdout).
 
-:command:`import` [*path*] [*dest-image*]
+:command:`import` [*path*] [*dest-image*] [--force]
   Creates a new image and imports its data from path (use - for
   stdin).  The import operation will try to create sparse rbd images 
   if possible.  For import from stdin, the sparsification unit is
   the data block size of the destination image (1 << order).
+  Using the --force option will overwrite the destination image if it exists.
+  Overwriting images using a different format is not supported. If the image
+  has snapshots, import fails and nothing is imported. (Note: using --force
+  will first remove the original rbd image, and then re-create it to import
+  the data from path).
 
 :command:`export-diff` [*image-name*] [*dest-path*] [--from-snap *snapname*]
   Exports an incremental diff for an image to dest path (use - for stdout).  If
qa/workunits/rbd/import_export.sh +6 -0
index 29fcb6c..1b1b291 100755
@@ -54,6 +54,12 @@ rbd rm testimg
 cmp /tmp/img /tmp/img2
 cmp /tmp/img /tmp/img3
 
+# import using --force option
+rbd create testimg $RBD_CREATE_ARGS --size 10
+rbd import $RBD_CREATE_ARGS /tmp/img testimg && exit 1 || true   # should fail
+rbd import $RBD_CREATE_ARGS /tmp/img testimg --force
+rbd rm testimg
+
 rm /tmp/img /tmp/img2 /tmp/img3
 
 
src/rbd.cc +62 -3

125 lines not on this page · raw message

src/test/cli/rbd/help.t +2 -1
index 3818ee3..1529efd 100644
@@ -16,7 +16,7 @@
     rm <image-name>                             delete an image
     export <image-name> <path>                  export image to file
                                                 "-" for stdout
-    import <path> <image-name>                  import image from file
+    import <path> <image-name> [--force]        import image from file
                                                 (dest defaults
                                                  as the filename part of file)
                                                 "-" for stdin
@@ -77,4 +77,5 @@
     --no-progress                      do not show progress for long-running commands
     -o, --options <map-options>        options to use when mapping an image
     --read-only                        set device readonly when mapping image
+    --force                            overwrite an existing image when importing
     --allow-shrink                     allow shrinking of an image when resizing
Signature · 1 line
1.9.1
#3 lore raw
Quoted 15 lines · Dimitris Bliablias

Extend the rbd utility with a new option named '--force'. This option will be used by the 'rbd import' command to allow overwriting an existing rbd image, something which is currently forbidden. If the image has snapshots, the command returns an error and nothing is imported. The force option will first remove the existing rbd image and then recreate it before importing the new data.

This patch also updates the tests affected by the modified functions, to reflect the latest changes. In addition, updates the rbd man page accordingly to record the new '--force' option.

---
Hello,

Quoted 9 lines

I've refactored the patch as you suggested, but I had to also drop the '-f' option and keep only the '--force' one. This was made due to the fact that the '-f' option is currently used along with the '--foreground' option that specifies whether the Ceph daemons should run in the foreground or not. The 'parse_argv' function of the 'src/common/config.h' file is called before the argument parsing of the rbd utility command line options and consequently it consumes the '-f' argument whenever it is supplied. Not sure if this is the intended behavior for the '-f' option.

This is a side-effect of the common args parsing infrastructure shared by daemons (where this option makes sense) and command line tools. Since this is triggered by global_init() which takes a daemon/utility flag, it wouldn't be too hard to pass that down to the config code and ignore daemon-only options like this for command line tools.

Quoted 107 lines

Thanks,
Dimitris

  doc/man/8/rbd.rst                 | 12 +++++++-
  qa/workunits/rbd/import_export.sh |  6 ++++
  src/rbd.cc                        | 65 +++++++++++++++++++++++++++++++++++++--
  src/test/cli/rbd/help.t           |  3 +-
  4 files changed, 81 insertions(+), 5 deletions(-)
doc/man/8/rbd.rst +11 -1
index 5424547..46d7f21 100644
@@ -123,6 +123,11 @@ Parameters
 
     Map the image read-only.  Equivalent to -o ro.
 
+.. option:: --force
+
+   Overwrite an existing rbd image. This option is only used by the rbd import
+   command. See import section (below) for more details.
+
 
  Commands
  ========
@@ -176,11 +181,16 @@ Commands
  :command:`export` [*image-name*] [*dest-path*]
    Exports image to dest path (use - for stdout).
 
-:command:`import` [*path*] [*dest-image*]
+:command:`import` [*path*] [*dest-image*] [--force]
    Creates a new image and imports its data from path (use - for
    stdin).  The import operation will try to create sparse rbd images
    if possible.  For import from stdin, the sparsification unit is
    the data block size of the destination image (1 << order).
+  Using the --force option will overwrite the destination image if it exists.
+  Overwriting images using a different format is not supported. If the image
+  has snapshots, import fails and nothing is imported. (Note: using --force
+  will first remove the original rbd image, and then re-create it to import
+  the data from path).
 
  :command:`export-diff` [*image-name*] [*dest-path*] [--from-snap *snapname*]
    Exports an incremental diff for an image to dest path (use - for stdout).  If
qa/workunits/rbd/import_export.sh +6 -0
index 29fcb6c..1b1b291 100755
@@ -54,6 +54,12 @@ rbd rm testimg
  cmp /tmp/img /tmp/img2
  cmp /tmp/img /tmp/img3
 
+# import using --force option
+rbd create testimg $RBD_CREATE_ARGS --size 10
+rbd import $RBD_CREATE_ARGS /tmp/img testimg && exit 1 || true   # should fail
+rbd import $RBD_CREATE_ARGS /tmp/img testimg --force
+rbd rm testimg
+
  rm /tmp/img /tmp/img2 /tmp/img3
 
 
src/rbd.cc +5 -2
index c068ed6..a0cf544 100644
@@ -28,6 +28,7 @@
 
  #include "include/compat.h"
  #include "common/blkdev.h"
+#include "librbd/internal.h"
 
  #include <boost/scoped_ptr.hpp>
  #include <errno.h>
@@ -89,7 +90,7 @@ void usage()
  "  rm <image-name>                             delete an image\n"
  "  export <image-name> <path>                  export image to file\n"
  "                                              \"-\" for stdout\n"
-"  import <path> <image-name>                  import image from file\n"
+"  import <path> <image-name> [--force]        import image from file\n"
  "                                              (dest defaults\n"
  "                                               as the filename part of file)\n"
  "                                              \"-\" for stdin\n"
@@ -150,6 +151,7 @@ void usage()
  "  --no-progress                      do not show progress for long-running commands\n"
  "  -o, --options <map-options>        options to use when mapping an image\n"
  "  --read-only                        set device readonly when mapping image\n"
+"  --force                            overwrite an existing image when importing\n"
  "  --allow-shrink                     allow shrinking of an image when resizing\n";
  }
 
@@ -1290,7 +1292,7 @@ done_img:
 
  static int do_import(librbd::RBD &rbd, librados::IoCtx& io_ctx,
  		     const char *imgname, int *order, const char *path,
-		     int format, uint64_t features, uint64_t size)
+		     int format, uint64_t features, uint64_t size, bool force)
  {
    int fd, r;
    struct stat stat_buf;
@@ -1310,6 +1312,7 @@ static int do_import(librbd::RBD &rbd, librados::IoCtx& io_ctx,
    ssize_t readlen;		// amount received from one read
    size_t blklen = 0;		// amount accumulated from reads to fill blk
    librbd::Image image;
+  bool old_format;
 
    bool from_stdin = !strcmp(path, "-");
    if (from_stdin) {

src/rbd.cc static int do_import(librbd::RBD &rbd, librados::IoCtx& io_ctx, · line 1353

Quoted 5 lines
src/rbd.cc +2 -0
@@ -1346,6 +1349,53 @@ static int do_import(librbd::RBD &rbd, librados::IoCtx& io_ctx,
        size = (uint64_t) bdev_size;
      }
    }
+  // check if the rbd image already exists, in either format
+  r = librbd::detect_format(io_ctx, imgname, &old_format, NULL);

I'd rather avoid using librbd-internal functions (like detect_format()). Trying to open the image image would accomplish the same thing.

src/rbd.cc static int do_import(librbd::RBD &rbd, librados::IoCtx& io_ctx, · line 1357

Quoted 4 lines
src/rbd.cc +4 -0
+  if (r != -ENOENT) {
+    if (force) {
+      // using the force option when importing to an rbd image is only allowed
+      // for images of the same format

I'm not sure why this restriction would be useful. What's the rationale? To me the format is just another property of the image.

Quoted 11 lines
src/rbd.cc +11 -0
+      bool format_match =
+	old_format ? (format == 1) : (format == 2);
+      if (format_match) {
+        // force option supplied, remove the original image
+        r = do_delete(rbd, io_ctx, imgname);
+	if (r < 0) {
+	  if (r == -ENOTEMPTY) {
+	    cerr << "rbd: image has snapshots - these must be deleted"
+		 << " with 'rbd snap purge' before the image can be removed."
+		 << std::endl;
+	  } else if (r == -EBUSY) {

src/rbd.cc static int do_import(librbd::RBD &rbd, librados::IoCtx& io_ctx, · line 1378

Quoted 10 lines
src/rbd.cc +10 -0
+	    cerr << "rbd: error: image still has watchers"
+		 << std::endl
+		 << "This means the image is still open or the client using "
+		 << "it crashed. Try again after closing/unmapping it or "
+		 << "waiting 30s for the crashed client to timeout."
+		 << std::endl;
+	  } else {
+	    cerr << "rbd: error removing original image: " << cpp_strerror(-r)
+		 << std::endl;
+	  }

This section of do_delete() and print various error messages can go into a helper, called from here and main() for 'rbd rm'.

The rest looks good.

Thanks!
Josh

Quoted 82 lines
src/rbd.cc +30 -1
+	goto done;
+	}
+      } else {
+	cerr << "rbd: image " << imgname << " already exists in format "
+	     << format << std::endl
+	     << "Option '--force' is only valid for images of the same format."
+	     << std::endl;
+	r = -EEXIST;
+	goto done;
+      }
+    } else {
+      cerr << "rbd: image " << imgname << " already exists"
+	   << std::endl
+	   << "If you really know what you are doing, supply the"
+	   << " '--force' option to overwrite this image."
+	   << std::endl;
+      r = -EEXIST;
+      goto done;
+    }
+  }
    r = do_create(rbd, io_ctx, imgname, size, order, format, features, 0, 0);
    if (r < 0) {
      cerr << "rbd: image creation failed" << std::endl;
@@ -1972,6 +2022,7 @@ int main(int argc, const char **argv)
    int order = 0;
    bool format_specified = false, output_format_specified = false;
    int format = 1;
+  bool force = false;
    uint64_t features = RBD_FEATURE_LAYERING;
    const char *imgname = NULL, *snapname = NULL, *destname = NULL,
      *dest_poolname = NULL, *dest_snapname = NULL, *path = NULL,
@@ -1997,6 +2048,8 @@ int main(int argc, const char **argv)
      } else if (ceph_argparse_flag(args, i, "-h", "--help", (char*)NULL)) {
        usage();
        return 0;
+    } else if (ceph_argparse_flag(args, i, "--force", (char*)NULL)) {
+      force = true;
      } else if (ceph_argparse_flag(args, i, "--new-format", (char*)NULL)) {
        format = 2;
        format_specified = true;
@@ -2190,6 +2243,12 @@ if (!set_conf_param(v, p1, p2, p3)) { \
      }
    }
 
+  if (force && opt_cmd != OPT_IMPORT) {
+    cerr << "rbd: --force can only be used when"
+	 << " importing an image" << std::endl;
+    return EXIT_FAILURE;
+  }
+
    if (format_specified && opt_cmd != OPT_IMPORT && opt_cmd != OPT_CREATE) {
      cerr << "rbd: image format can only be set when "
  	 << "creating or importing an image" << std::endl;
@@ -2663,7 +2722,7 @@ if (!set_conf_param(v, p1, p2, p3)) { \
        return EINVAL;
      }
      r = do_import(rbd, dest_io_ctx, destname, &order, path,
-		  format, features, size);
+		  format, features, size, force);
      if (r < 0) {
        cerr << "rbd: import failed: " << cpp_strerror(-r) << std::endl;
        return -r;
src/test/cli/rbd/help.t +2 -1
index 3818ee3..1529efd 100644
@@ -16,7 +16,7 @@
      rm <image-name>                             delete an image
      export <image-name> <path>                  export image to file
                                                  "-" for stdout
-    import <path> <image-name>                  import image from file
+    import <path> <image-name> [--force]        import image from file
                                                  (dest defaults
                                                   as the filename part of file)
                                                  "-" for stdin
@@ -77,4 +77,5 @@
      --no-progress                      do not show progress for long-running commands
      -o, --options <map-options>        options to use when mapping an image
      --read-only                        set device readonly when mapping image
+    --force                            overwrite an existing image when importing
      --allow-shrink                     allow shrinking of an image when resizing