From b86563db7e232d27e93c0c13e96472551864f795 Mon Sep 17 00:00:00 2001 From: Niels de Vos Date: Thu, 28 Nov 2019 10:43:33 +0100 Subject: [PATCH] rbd: add Create4() for more advanced RBD Image creation This allows to set additional options while creating RBD images. Ceph-CSI will initially consume this to configure an optionally different pool for data. Signed-off-by: Niels de Vos --- rbd/rbd.go | 23 ++++++++++++++++++++ rbd/rbd_test.go | 56 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+) diff --git a/rbd/rbd.go b/rbd/rbd.go index 7af889f..c3d4300 100644 --- a/rbd/rbd.go +++ b/rbd/rbd.go @@ -261,6 +261,29 @@ func Create3(ioctx *rados.IOContext, name string, size uint64, features uint64, }, nil } +// int rbd_create4(rados_ioctx_t io, const char *name, uint64_t size, +// rbd_image_options_t opts); +func Create4(ioctx *rados.IOContext, name string, size uint64, rio *RbdImageOptions) (image *Image, err error) { + if rio == nil { + return nil, RBDError(C.EINVAL) + } + + c_name := C.CString(name) + defer C.free(unsafe.Pointer(c_name)) + + ret := C.rbd_create4(C.rados_ioctx_t(ioctx.Pointer()), c_name, + C.uint64_t(size), C.rbd_image_options_t(rio.options)) + + if ret < 0 { + return nil, RBDError(ret) + } + + return &Image{ + ioctx: ioctx, + name: name, + }, nil +} + // 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); diff --git a/rbd/rbd_test.go b/rbd/rbd_test.go index a803e17..874f292 100644 --- a/rbd/rbd_test.go +++ b/rbd/rbd_test.go @@ -114,6 +114,62 @@ func TestImageCreate3(t *testing.T) { conn.Shutdown() } +func TestCreateImageWithOptions(t *testing.T) { + conn, _ := rados.NewConn() + conn.ReadDefaultConfigFile() + conn.Connect() + + poolname := GetUUID() + err := conn.MakePool(poolname) + assert.NoError(t, err) + + ioctx, err := conn.OpenIOContext(poolname) + assert.NoError(t, err) + + // nil options, causes a panic if not handled correctly + name := GetUUID() + image, err := rbd.Create4(ioctx, name, 1<<22, nil) + assert.Error(t, err) + + options := rbd.NewRbdImageOptions() + + // empty/default options + name = GetUUID() + image, err = rbd.Create4(ioctx, name, 1<<22, options) + assert.NoError(t, err) + err = image.Remove() + assert.NoError(t, err) + + // create image with RbdImageOptionOrder + err = options.SetUint64(rbd.RbdImageOptionOrder, 22) + assert.NoError(t, err) + name = GetUUID() + image, err = rbd.Create4(ioctx, name, 1<<22, options) + assert.NoError(t, err) + err = image.Remove() + assert.NoError(t, err) + options.Clear() + + // create image with a different data pool + datapool := GetUUID() + err = conn.MakePool(datapool) + assert.NoError(t, err) + err = options.SetString(rbd.RbdImageOptionDataPool, datapool) + assert.NoError(t, err) + name = GetUUID() + image, err = rbd.Create4(ioctx, name, 1<<22, options) + assert.NoError(t, err) + err = image.Remove() + assert.NoError(t, err) + conn.DeletePool(datapool) + + // cleanup + options.Destroy() + ioctx.Destroy() + conn.DeletePool(poolname) + conn.Shutdown() +} + func TestGetImageNames(t *testing.T) { conn, _ := rados.NewConn() conn.ReadDefaultConfigFile()