rbd: add Sparsify implementing rbd_sparsify

The sparsify function can be used to make an image sparse by replacing
runs of zeros with holes. This change adds a wrapper for the basic
rbd_sparsify function and accompanying tests.

Signed-off-by: John Mulligan <jmulligan@redhat.com>
This commit is contained in:
John Mulligan 2020-08-20 14:40:49 -04:00 committed by John Mulligan
parent 176a163c3c
commit dbf20aea0a
2 changed files with 60 additions and 0 deletions

View File

@ -101,3 +101,17 @@ func (image *Image) GetModifyTimestamp() (Timespec, error) {
return Timespec(ts.CStructToTimespec(ts.CTimespecPtr(&cts))), nil
}
// Sparsify makes an image sparse by deallocating runs of zeros.
// The sparseSize value will be used to find runs of zeros and must be
// a power of two no less than 4096 and no larger than the image size.
//
// Implements:
// int rbd_sparsify(rbd_image_t image, size_t sparse_size);
func (image *Image) Sparsify(sparseSize uint) error {
if err := image.validate(imageIsOpen); err != nil {
return err
}
return getError(C.rbd_sparsify(image.image, C.size_t(sparseSize)))
}

View File

@ -77,3 +77,49 @@ func TestClosedImageNautilus(t *testing.T) {
_, err = image.GetModifyTimestamp()
assert.Error(t, err)
}
func TestSparsify(t *testing.T) {
conn := radosConnect(t)
defer conn.Shutdown()
poolname := GetUUID()
err := conn.MakePool(poolname)
require.NoError(t, err)
defer conn.DeletePool(poolname)
ioctx, err := conn.OpenIOContext(poolname)
require.NoError(t, err)
defer ioctx.Destroy()
name := GetUUID()
err = quickCreate(ioctx, name, testImageSize, testImageOrder)
require.NoError(t, err)
defer func() { assert.NoError(t, RemoveImage(ioctx, name)) }()
t.Run("valid", func(t *testing.T) {
img, err := OpenImage(ioctx, name, NoSnapshot)
assert.NoError(t, err)
defer func() { assert.NoError(t, img.Close()) }()
err = img.Sparsify(4096)
assert.NoError(t, err)
})
t.Run("invalidValue", func(t *testing.T) {
img, err := OpenImage(ioctx, name, NoSnapshot)
assert.NoError(t, err)
defer func() { assert.NoError(t, img.Close()) }()
err = img.Sparsify(1024)
assert.Error(t, err)
})
t.Run("closedImage", func(t *testing.T) {
img, err := OpenImage(ioctx, name, NoSnapshot)
assert.NoError(t, err)
assert.NoError(t, img.Close())
err = img.Sparsify(1024)
assert.Error(t, err)
})
}