Skip to content

Commit

Permalink
rbd: add CreateWithOptions() for more advanced RBD Image creation
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
nixpanic committed Nov 29, 2019
1 parent dee5772 commit f6b91ab
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 0 deletions.
21 changes: 21 additions & 0 deletions rbd/rbd.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,27 @@ func Create(ioctx *rados.IOContext, name string, size uint64, order int,
}, nil
}

func CreateWithOptions(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);
Expand Down
56 changes: 56 additions & 0 deletions rbd/rbd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,62 @@ func TestCreateImage(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.CreateWithOptions(ioctx, name, 1<<22, nil)
assert.Error(t, err)

options := rbd.NewRbdImageOptions()

// empty/default options
name = GetUUID()
image, err = rbd.CreateWithOptions(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.CreateWithOptions(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.CreateWithOptions(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()
Expand Down

0 comments on commit f6b91ab

Please sign in to comment.