From 3f6125a599e48366a3b3fe63cc5530a126587f0e Mon Sep 17 00:00:00 2001 From: zhengyin Date: Thu, 16 Aug 2018 19:31:43 +0800 Subject: [PATCH] pybind/rbd: add allow_shrink=True as a parameter to def resize(self, size) method 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 --- src/pybind/rbd/rbd.pyx | 23 +++++++++++++++++------ src/test/pybind/test_rbd.py | 7 +++++++ 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/src/pybind/rbd/rbd.pyx b/src/pybind/rbd/rbd.pyx index fa406ca80825b..f471175804845 100644 --- a/src/pybind/rbd/rbd.pyx +++ b/src/pybind/rbd/rbd.pyx @@ -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): """ diff --git a/src/test/pybind/test_rbd.py b/src/test/pybind/test_rbd.py index 38cd58bfd3a2f..1ebb5fdf48806 100644 --- a/src/test/pybind/test_rbd.py +++ b/src/test/pybind/test_rbd.py @@ -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') -- 2.39.5