]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
pybind/rbd: add allow_shrink=True as a parameter to def resize(self, size) method 23605/head
authorzhengyin <zhengyin@cmss.chinamobile.com>
Thu, 16 Aug 2018 11:31:43 +0000 (19:31 +0800)
committerzhengyin <zhengyin@cmss.chinamobile.com>
Thu, 23 Aug 2018 06:35:21 +0000 (14:35 +0800)
resize(size, allow_shrink) has allow_shrink param, if allow_shrink=false
and old_size > new size, it will raise error.

Signed-off-by: Zheng Yin <zhengyin@cmss.chinamobile.com>
src/pybind/rbd/rbd.pyx
src/test/pybind/test_rbd.py

index fa406ca80825b77dc6bc815b2385324d7bc65902..f471175804845633d3e62e092ab6167163a83e70 100644 (file)
@@ -302,7 +302,8 @@ cdef extern from "rbd/librbd.h" nogil:
     int rbd_open_by_id_read_only(rados_ioctx_t io, const char *image_id,
                                  rbd_image_t *image, const char *snap_name)
     int rbd_close(rbd_image_t image)
-    int rbd_resize(rbd_image_t image, uint64_t size)
+    int rbd_resize2(rbd_image_t image, uint64_t size, bint allow_shrink,
+                    librbd_progress_fn_t cb, void *cbdata)
     int rbd_stat(rbd_image_t image, rbd_image_info_t *info, size_t infosize)
     int rbd_get_old_format(rbd_image_t image, uint8_t *old)
     int rbd_get_size(rbd_image_t image, uint64_t *size)
@@ -2083,18 +2084,28 @@ cdef class Image(object):
     def __repr__(self):
         return "rbd.Image(ioctx, %r)" % self.name
 
-    def resize(self, size):
+    def resize(self, size, allow_shrink=True):
         """
-        Change the size of the image.
+        Change the size of the image, allow shrink.
 
         :param size: the new size of the image
         :type size: int
+        :param allow_shrink: permit shrinking
+        :type allow_shrink: bool
         """
-        cdef uint64_t _size = size
+        old_size = self.size()
+        if old_size == size:
+            return
+        if not allow_shrink and old_size > size:
+            raise InvalidArgument("error allow_shrink is False but old_size > new_size")
+        cdef:
+            uint64_t _size = size
+            bint _allow_shrink = allow_shrink
+            librbd_progress_fn_t prog_cb = &no_op_progress_callback
         with nogil:
-            ret = rbd_resize(self.image, _size)
+            ret = rbd_resize2(self.image, _size, _allow_shrink, prog_cb, NULL)
         if ret < 0:
-            raise make_ex(ret, 'error resizing image %s' % (self.name,))
+            raise make_ex(ret, 'error resizing image %s' % self.name)
 
     def stat(self):
         """
index 38cd58bfd3a2f0cd301e6e15adcceee2d9c7e2fa..1ebb5fdf48806ef2d090f3af0c27aa1efe6e69ed 100644 (file)
@@ -444,6 +444,13 @@ class TestImage(object):
         info = self.image.stat()
         check_stat(info, new_size, IMG_ORDER)
 
+    def test_resize_allow_shrink_False(self):
+        new_size = IMG_SIZE * 2
+        self.image.resize(new_size)
+        info = self.image.stat()
+        check_stat(info, new_size, IMG_ORDER)
+        assert_raises(InvalidArgument, self.image.resize, IMG_SIZE, False)
+
     def test_size(self):
         eq(IMG_SIZE, self.image.size())
         self.image.create_snap('snap1')