2020-01-07 13:30:27 +00:00
|
|
|
package rbd
|
|
|
|
|
|
|
|
// #cgo LDFLAGS: -lrbd
|
|
|
|
// /* force XSI-complaint strerror_r() */
|
|
|
|
// #define _POSIX_C_SOURCE 200112L
|
|
|
|
// #undef _GNU_SOURCE
|
|
|
|
// #include <errno.h>
|
|
|
|
// #include <stdlib.h>
|
|
|
|
// #include <rados/librados.h>
|
|
|
|
// #include <rbd/librbd.h>
|
|
|
|
import "C"
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"io"
|
|
|
|
"time"
|
|
|
|
"unsafe"
|
|
|
|
|
2020-08-11 14:42:21 +00:00
|
|
|
"github.com/ceph/go-ceph/internal/cutil"
|
2020-06-17 09:37:06 +00:00
|
|
|
"github.com/ceph/go-ceph/internal/retry"
|
2020-08-11 14:42:21 +00:00
|
|
|
ts "github.com/ceph/go-ceph/internal/timespec"
|
2020-01-07 13:30:27 +00:00
|
|
|
"github.com/ceph/go-ceph/rados"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2020-05-08 14:07:31 +00:00
|
|
|
// Image.Seek() constants:
|
2020-01-07 13:30:27 +00:00
|
|
|
|
2020-05-08 14:07:31 +00:00
|
|
|
// SeekSet is used with Seek to absolutely position the file.
|
2020-01-07 13:30:27 +00:00
|
|
|
SeekSet = int(C.SEEK_SET)
|
2020-05-08 14:07:31 +00:00
|
|
|
// SeekCur is used with Seek to position the file relatively to the current
|
|
|
|
// position.
|
2020-01-07 13:30:27 +00:00
|
|
|
SeekCur = int(C.SEEK_CUR)
|
2020-05-08 14:07:31 +00:00
|
|
|
// SeekEnd is used with Seek to position the file relatively to the end.
|
2020-01-07 13:30:27 +00:00
|
|
|
SeekEnd = int(C.SEEK_END)
|
|
|
|
)
|
|
|
|
|
|
|
|
// bits for Image.validate() and Snapshot.validate()
|
|
|
|
const (
|
|
|
|
imageNeedsName uint32 = 1 << iota
|
|
|
|
imageNeedsIOContext
|
|
|
|
imageIsOpen
|
2020-06-17 09:37:06 +00:00
|
|
|
imageIsNotOpen
|
2020-01-07 13:30:27 +00:00
|
|
|
snapshotNeedsName
|
|
|
|
|
|
|
|
// NoSnapshot indicates that no snapshot name is in use (see OpenImage)
|
|
|
|
NoSnapshot = ""
|
|
|
|
)
|
|
|
|
|
2020-08-11 14:42:21 +00:00
|
|
|
// Timespec is a public type for the internal C 'struct timespec'
|
|
|
|
type Timespec ts.Timespec
|
|
|
|
|
2021-10-18 20:26:15 +00:00
|
|
|
// revive:disable:var-naming old-yet-exported public api
|
|
|
|
|
2020-05-08 14:07:31 +00:00
|
|
|
// ImageInfo represents the status information for an image.
|
2020-01-07 13:30:27 +00:00
|
|
|
type ImageInfo struct {
|
|
|
|
Size uint64
|
|
|
|
Obj_size uint64
|
|
|
|
Num_objs uint64
|
|
|
|
Order int
|
|
|
|
Block_name_prefix string
|
|
|
|
}
|
|
|
|
|
2021-10-18 20:26:15 +00:00
|
|
|
// revive:enable:var-naming
|
|
|
|
|
2020-05-08 14:07:31 +00:00
|
|
|
// SnapInfo represents the status information for a snapshot.
|
2020-01-07 13:30:27 +00:00
|
|
|
type SnapInfo struct {
|
|
|
|
Id uint64
|
|
|
|
Size uint64
|
|
|
|
Name string
|
|
|
|
}
|
|
|
|
|
2020-05-08 14:07:31 +00:00
|
|
|
// Locker provides info about a client that is locking an image.
|
2020-01-07 13:30:27 +00:00
|
|
|
type Locker struct {
|
|
|
|
Client string
|
|
|
|
Cookie string
|
|
|
|
Addr string
|
|
|
|
}
|
|
|
|
|
2020-05-08 14:07:31 +00:00
|
|
|
// Image is a handle for an RBD image.
|
2020-01-07 13:30:27 +00:00
|
|
|
type Image struct {
|
|
|
|
io.Reader
|
|
|
|
io.Writer
|
|
|
|
io.Seeker
|
|
|
|
io.ReaderAt
|
|
|
|
io.WriterAt
|
|
|
|
name string
|
|
|
|
offset int64
|
|
|
|
ioctx *rados.IOContext
|
|
|
|
image C.rbd_image_t
|
|
|
|
}
|
|
|
|
|
|
|
|
// TrashInfo contains information about trashed RBDs.
|
|
|
|
type TrashInfo struct {
|
|
|
|
Id string // Id string, required to remove / restore trashed RBDs.
|
|
|
|
Name string // Original name of trashed RBD.
|
|
|
|
DeletionTime time.Time // Date / time at which the RBD was moved to the trash.
|
|
|
|
DefermentEndTime time.Time // Date / time after which the trashed RBD may be permanently deleted.
|
|
|
|
}
|
|
|
|
|
2020-05-08 14:07:31 +00:00
|
|
|
// cephIoctx returns a ceph rados_ioctx_t given a go-ceph rados IOContext.
|
|
|
|
func cephIoctx(radosIoctx *rados.IOContext) C.rados_ioctx_t {
|
2021-02-10 03:38:18 +00:00
|
|
|
p := radosIoctx.Pointer()
|
|
|
|
if p == nil {
|
|
|
|
panic("invalid IOContext pointer")
|
|
|
|
}
|
|
|
|
return C.rados_ioctx_t(p)
|
2020-05-08 14:07:31 +00:00
|
|
|
}
|
|
|
|
|
2020-01-07 13:30:27 +00:00
|
|
|
// test if a bit is set in the given value
|
|
|
|
func hasBit(value, bit uint32) bool {
|
|
|
|
return (value & bit) == bit
|
|
|
|
}
|
|
|
|
|
|
|
|
// validate the attributes listed in the req bitmask, and return an error in
|
|
|
|
// case the attribute is not set
|
|
|
|
func (image *Image) validate(req uint32) error {
|
|
|
|
if hasBit(req, imageNeedsName) && image.name == "" {
|
|
|
|
return ErrNoName
|
|
|
|
} else if hasBit(req, imageNeedsIOContext) && image.ioctx == nil {
|
|
|
|
return ErrNoIOContext
|
|
|
|
} else if hasBit(req, imageIsOpen) && image.image == nil {
|
|
|
|
return ErrImageNotOpen
|
2020-06-17 09:37:06 +00:00
|
|
|
} else if hasBit(req, imageIsNotOpen) && image.image != nil {
|
|
|
|
return ErrImageIsOpen
|
2020-01-07 13:30:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Version returns the major, minor, and patch level of the librbd library.
|
|
|
|
func Version() (int, int, int) {
|
2021-10-18 20:26:15 +00:00
|
|
|
var cMajor, cMinor, cPatch C.int
|
|
|
|
C.rbd_version(&cMajor, &cMinor, &cPatch)
|
|
|
|
return int(cMajor), int(cMinor), int(cPatch)
|
2020-01-07 13:30:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetImage gets a reference to a previously created rbd image.
|
|
|
|
func GetImage(ioctx *rados.IOContext, name string) *Image {
|
|
|
|
return &Image{
|
|
|
|
ioctx: ioctx,
|
|
|
|
name: name,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a new rbd image.
|
|
|
|
//
|
|
|
|
// Implements:
|
2023-04-26 07:05:57 +00:00
|
|
|
//
|
|
|
|
// int rbd_create(rados_ioctx_t io, const char *name, uint64_t size, int *order);
|
2020-01-07 13:30:27 +00:00
|
|
|
//
|
|
|
|
// Also implements (for backward compatibility):
|
2023-04-26 07:05:57 +00:00
|
|
|
//
|
|
|
|
// int rbd_create2(rados_ioctx_t io, const char *name, uint64_t size,
|
|
|
|
// uint64_t features, int *order);
|
|
|
|
// int rbd_create3(rados_ioctx_t io, const char *name, uint64_t size,
|
|
|
|
// uint64_t features, int *order,
|
|
|
|
// uint64_t stripe_unit, uint64_t stripe_count);
|
2020-01-07 13:30:27 +00:00
|
|
|
func Create(ioctx *rados.IOContext, name string, size uint64, order int,
|
|
|
|
args ...uint64) (image *Image, err error) {
|
|
|
|
var ret C.int
|
|
|
|
|
|
|
|
switch len(args) {
|
|
|
|
case 3:
|
|
|
|
return Create3(ioctx, name, size, args[0], order, args[1],
|
|
|
|
args[2])
|
|
|
|
case 1:
|
|
|
|
return Create2(ioctx, name, size, args[0], order)
|
|
|
|
case 0:
|
2021-10-18 20:26:15 +00:00
|
|
|
cOrder := C.int(order)
|
|
|
|
cName := C.CString(name)
|
2020-01-07 13:30:27 +00:00
|
|
|
|
2021-10-18 20:26:15 +00:00
|
|
|
defer C.free(unsafe.Pointer(cName))
|
2020-01-07 13:30:27 +00:00
|
|
|
|
2020-05-08 14:07:31 +00:00
|
|
|
ret = C.rbd_create(cephIoctx(ioctx),
|
2021-10-18 20:26:15 +00:00
|
|
|
cName, C.uint64_t(size), &cOrder)
|
2020-01-07 13:30:27 +00:00
|
|
|
default:
|
|
|
|
return nil, errors.New("Wrong number of argument")
|
|
|
|
}
|
|
|
|
|
|
|
|
if ret < 0 {
|
2020-08-11 14:42:21 +00:00
|
|
|
return nil, rbdError(ret)
|
2020-01-07 13:30:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &Image{
|
|
|
|
ioctx: ioctx,
|
|
|
|
name: name,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create2 creates a new rbd image using provided features.
|
|
|
|
//
|
|
|
|
// Implements:
|
2023-04-26 07:05:57 +00:00
|
|
|
//
|
|
|
|
// int rbd_create2(rados_ioctx_t io, const char *name, uint64_t size,
|
|
|
|
// uint64_t features, int *order);
|
2020-01-07 13:30:27 +00:00
|
|
|
func Create2(ioctx *rados.IOContext, name string, size uint64, features uint64,
|
|
|
|
order int) (image *Image, err error) {
|
|
|
|
var ret C.int
|
|
|
|
|
2021-10-18 20:26:15 +00:00
|
|
|
cOrder := C.int(order)
|
|
|
|
cName := C.CString(name)
|
2020-01-07 13:30:27 +00:00
|
|
|
|
2021-10-18 20:26:15 +00:00
|
|
|
defer C.free(unsafe.Pointer(cName))
|
2020-01-07 13:30:27 +00:00
|
|
|
|
2021-10-18 20:26:15 +00:00
|
|
|
ret = C.rbd_create2(cephIoctx(ioctx), cName,
|
|
|
|
C.uint64_t(size), C.uint64_t(features), &cOrder)
|
2020-01-07 13:30:27 +00:00
|
|
|
if ret < 0 {
|
2020-08-11 14:42:21 +00:00
|
|
|
return nil, rbdError(ret)
|
2020-01-07 13:30:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &Image{
|
|
|
|
ioctx: ioctx,
|
|
|
|
name: name,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create3 creates a new rbd image using provided features and stripe
|
|
|
|
// parameters.
|
|
|
|
//
|
|
|
|
// Implements:
|
2023-04-26 07:05:57 +00:00
|
|
|
//
|
|
|
|
// int rbd_create3(rados_ioctx_t io, const char *name, uint64_t size,
|
|
|
|
// uint64_t features, int *order,
|
|
|
|
// uint64_t stripe_unit, uint64_t stripe_count);
|
2020-01-07 13:30:27 +00:00
|
|
|
func Create3(ioctx *rados.IOContext, name string, size uint64, features uint64,
|
2021-10-18 20:26:15 +00:00
|
|
|
order int, stripeUnit uint64, stripeCount uint64) (image *Image, err error) {
|
2020-01-07 13:30:27 +00:00
|
|
|
var ret C.int
|
|
|
|
|
2021-10-18 20:26:15 +00:00
|
|
|
cOrder := C.int(order)
|
|
|
|
cName := C.CString(name)
|
2020-01-07 13:30:27 +00:00
|
|
|
|
2021-10-18 20:26:15 +00:00
|
|
|
defer C.free(unsafe.Pointer(cName))
|
2020-01-07 13:30:27 +00:00
|
|
|
|
2021-10-18 20:26:15 +00:00
|
|
|
ret = C.rbd_create3(cephIoctx(ioctx), cName,
|
|
|
|
C.uint64_t(size), C.uint64_t(features), &cOrder,
|
|
|
|
C.uint64_t(stripeUnit), C.uint64_t(stripeCount))
|
2020-01-07 13:30:27 +00:00
|
|
|
if ret < 0 {
|
2020-08-11 14:42:21 +00:00
|
|
|
return nil, rbdError(ret)
|
2020-01-07 13:30:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &Image{
|
|
|
|
ioctx: ioctx,
|
|
|
|
name: name,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clone a new rbd image from a snapshot.
|
|
|
|
//
|
|
|
|
// Implements:
|
2023-04-26 07:05:57 +00:00
|
|
|
//
|
|
|
|
// 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);
|
2021-10-18 20:26:15 +00:00
|
|
|
func (image *Image) Clone(snapname string, cIoctx *rados.IOContext, cName string, features uint64, order int) (*Image, error) {
|
2020-01-07 13:30:27 +00:00
|
|
|
if err := image.validate(imageNeedsIOContext); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-10-18 20:26:15 +00:00
|
|
|
cOrder := C.int(order)
|
|
|
|
cParentName := C.CString(image.name)
|
|
|
|
cParentSnapName := C.CString(snapname)
|
|
|
|
cCloneName := C.CString(cName)
|
2020-01-07 13:30:27 +00:00
|
|
|
|
2021-10-18 20:26:15 +00:00
|
|
|
defer C.free(unsafe.Pointer(cParentName))
|
|
|
|
defer C.free(unsafe.Pointer(cParentSnapName))
|
|
|
|
defer C.free(unsafe.Pointer(cCloneName))
|
2020-01-07 13:30:27 +00:00
|
|
|
|
2021-10-18 20:26:15 +00:00
|
|
|
ret := C.rbd_clone(
|
|
|
|
cephIoctx(image.ioctx),
|
|
|
|
cParentName,
|
|
|
|
cParentSnapName,
|
|
|
|
cephIoctx(cIoctx),
|
|
|
|
cCloneName,
|
|
|
|
C.uint64_t(features),
|
|
|
|
&cOrder)
|
2020-01-07 13:30:27 +00:00
|
|
|
if ret < 0 {
|
2020-08-11 14:42:21 +00:00
|
|
|
return nil, rbdError(ret)
|
2020-01-07 13:30:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &Image{
|
2021-10-18 20:26:15 +00:00
|
|
|
ioctx: cIoctx,
|
|
|
|
name: cName,
|
2020-01-07 13:30:27 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove the specified rbd image.
|
|
|
|
//
|
|
|
|
// Implements:
|
2023-04-26 07:05:57 +00:00
|
|
|
//
|
|
|
|
// int rbd_remove(rados_ioctx_t io, const char *name);
|
2020-01-07 13:30:27 +00:00
|
|
|
func (image *Image) Remove() error {
|
2020-06-17 09:37:06 +00:00
|
|
|
if err := image.validate(imageNeedsIOContext | imageNeedsName | imageIsNotOpen); err != nil {
|
2020-01-07 13:30:27 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return RemoveImage(image.ioctx, image.name)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Trash will move an image into the RBD trash, where it will be protected (i.e., salvageable) for
|
|
|
|
// at least the specified delay.
|
|
|
|
func (image *Image) Trash(delay time.Duration) error {
|
|
|
|
if err := image.validate(imageNeedsIOContext | imageNeedsName); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-10-18 20:26:15 +00:00
|
|
|
cName := C.CString(image.name)
|
|
|
|
defer C.free(unsafe.Pointer(cName))
|
2020-01-07 13:30:27 +00:00
|
|
|
|
2021-10-18 20:26:15 +00:00
|
|
|
return getError(C.rbd_trash_move(cephIoctx(image.ioctx), cName,
|
2020-01-07 13:30:27 +00:00
|
|
|
C.uint64_t(delay.Seconds())))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Rename an rbd image.
|
|
|
|
//
|
|
|
|
// Implements:
|
2023-04-26 07:05:57 +00:00
|
|
|
//
|
|
|
|
// int rbd_rename(rados_ioctx_t src_io_ctx, const char *srcname, const char *destname);
|
2020-01-07 13:30:27 +00:00
|
|
|
func (image *Image) Rename(destname string) error {
|
|
|
|
if err := image.validate(imageNeedsIOContext | imageNeedsName); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-10-18 20:26:15 +00:00
|
|
|
cSrcName := C.CString(image.name)
|
|
|
|
cDestName := C.CString(destname)
|
2020-01-07 13:30:27 +00:00
|
|
|
|
2021-10-18 20:26:15 +00:00
|
|
|
defer C.free(unsafe.Pointer(cSrcName))
|
|
|
|
defer C.free(unsafe.Pointer(cDestName))
|
2020-01-07 13:30:27 +00:00
|
|
|
|
2020-08-11 14:42:21 +00:00
|
|
|
err := rbdError(C.rbd_rename(cephIoctx(image.ioctx),
|
2021-10-18 20:26:15 +00:00
|
|
|
cSrcName, cDestName))
|
2020-01-07 13:30:27 +00:00
|
|
|
if err == 0 {
|
|
|
|
image.name = destname
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Open the rbd image.
|
|
|
|
//
|
2021-10-18 20:26:15 +00:00
|
|
|
// Deprecated: use OpenImage and OpenImageReadOnly instead
|
2020-01-07 13:30:27 +00:00
|
|
|
func (image *Image) Open(args ...interface{}) error {
|
|
|
|
if err := image.validate(imageNeedsIOContext | imageNeedsName); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
snapName string
|
|
|
|
readOnly bool
|
|
|
|
)
|
|
|
|
for _, arg := range args {
|
|
|
|
switch t := arg.(type) {
|
|
|
|
case string:
|
|
|
|
snapName = t
|
|
|
|
case bool:
|
|
|
|
readOnly = t
|
|
|
|
default:
|
|
|
|
return errors.New("Unexpected argument")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
tmp *Image
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
if readOnly {
|
|
|
|
tmp, err = OpenImageReadOnly(image.ioctx, image.name, snapName)
|
|
|
|
} else {
|
|
|
|
tmp, err = OpenImage(image.ioctx, image.name, snapName)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
image.image = tmp.image
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close an open rbd image.
|
|
|
|
//
|
|
|
|
// Implements:
|
2023-04-26 07:05:57 +00:00
|
|
|
//
|
|
|
|
// int rbd_close(rbd_image_t image);
|
2020-01-07 13:30:27 +00:00
|
|
|
func (image *Image) Close() error {
|
|
|
|
if err := image.validate(imageIsOpen); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if ret := C.rbd_close(image.image); ret != 0 {
|
2020-08-11 14:42:21 +00:00
|
|
|
return rbdError(ret)
|
2020-01-07 13:30:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
image.image = nil
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Resize an rbd image.
|
|
|
|
//
|
|
|
|
// Implements:
|
2023-04-26 07:05:57 +00:00
|
|
|
//
|
|
|
|
// int rbd_resize(rbd_image_t image, uint64_t size);
|
2020-01-07 13:30:27 +00:00
|
|
|
func (image *Image) Resize(size uint64) error {
|
|
|
|
if err := image.validate(imageIsOpen); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return getError(C.rbd_resize(image.image, C.uint64_t(size)))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stat an rbd image.
|
|
|
|
//
|
|
|
|
// Implements:
|
2023-04-26 07:05:57 +00:00
|
|
|
//
|
|
|
|
// int rbd_stat(rbd_image_t image, rbd_image_info_t *info, size_t infosize);
|
2020-01-07 13:30:27 +00:00
|
|
|
func (image *Image) Stat() (info *ImageInfo, err error) {
|
|
|
|
if err := image.validate(imageIsOpen); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-10-18 20:26:15 +00:00
|
|
|
var cStat C.rbd_image_info_t
|
2020-01-07 13:30:27 +00:00
|
|
|
|
2021-10-18 20:26:15 +00:00
|
|
|
if ret := C.rbd_stat(image.image, &cStat, C.size_t(unsafe.Sizeof(info))); ret < 0 {
|
2020-08-11 14:42:21 +00:00
|
|
|
return info, rbdError(ret)
|
2020-01-07 13:30:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &ImageInfo{
|
2021-10-18 20:26:15 +00:00
|
|
|
Size: uint64(cStat.size),
|
|
|
|
Obj_size: uint64(cStat.obj_size),
|
|
|
|
Num_objs: uint64(cStat.num_objs),
|
|
|
|
Order: int(cStat.order),
|
|
|
|
Block_name_prefix: C.GoString((*C.char)(&cStat.block_name_prefix[0]))}, nil
|
2020-01-07 13:30:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// IsOldFormat returns true if the rbd image uses the old format.
|
|
|
|
//
|
|
|
|
// Implements:
|
2023-04-26 07:05:57 +00:00
|
|
|
//
|
|
|
|
// int rbd_get_old_format(rbd_image_t image, uint8_t *old);
|
2021-10-18 20:26:15 +00:00
|
|
|
func (image *Image) IsOldFormat() (bool, error) {
|
2020-01-07 13:30:27 +00:00
|
|
|
if err := image.validate(imageIsOpen); err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
2021-10-18 20:26:15 +00:00
|
|
|
var cOldFormat C.uint8_t
|
2020-01-07 13:30:27 +00:00
|
|
|
ret := C.rbd_get_old_format(image.image,
|
2021-10-18 20:26:15 +00:00
|
|
|
&cOldFormat)
|
2020-01-07 13:30:27 +00:00
|
|
|
if ret < 0 {
|
2020-08-11 14:42:21 +00:00
|
|
|
return false, rbdError(ret)
|
2020-01-07 13:30:27 +00:00
|
|
|
}
|
|
|
|
|
2021-10-18 20:26:15 +00:00
|
|
|
return cOldFormat != 0, nil
|
2020-01-07 13:30:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetSize returns the size of the rbd image.
|
|
|
|
//
|
|
|
|
// Implements:
|
2023-04-26 07:05:57 +00:00
|
|
|
//
|
|
|
|
// int rbd_size(rbd_image_t image, uint64_t *size);
|
2020-01-07 13:30:27 +00:00
|
|
|
func (image *Image) GetSize() (size uint64, err error) {
|
|
|
|
if err := image.validate(imageIsOpen); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if ret := C.rbd_get_size(image.image, (*C.uint64_t)(&size)); ret < 0 {
|
2020-08-11 14:42:21 +00:00
|
|
|
return 0, rbdError(ret)
|
2020-01-07 13:30:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return size, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetStripeUnit returns the stripe-unit value for the rbd image.
|
|
|
|
//
|
|
|
|
// Implements:
|
2023-04-26 07:05:57 +00:00
|
|
|
//
|
|
|
|
// int rbd_get_stripe_unit(rbd_image_t image, uint64_t *stripe_unit);
|
2021-10-18 20:26:15 +00:00
|
|
|
func (image *Image) GetStripeUnit() (uint64, error) {
|
2020-01-07 13:30:27 +00:00
|
|
|
if err := image.validate(imageIsOpen); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
2021-10-18 20:26:15 +00:00
|
|
|
var stripeUnit uint64
|
|
|
|
if ret := C.rbd_get_stripe_unit(image.image, (*C.uint64_t)(&stripeUnit)); ret < 0 {
|
2020-08-11 14:42:21 +00:00
|
|
|
return 0, rbdError(ret)
|
2020-01-07 13:30:27 +00:00
|
|
|
}
|
|
|
|
|
2021-10-18 20:26:15 +00:00
|
|
|
return stripeUnit, nil
|
2020-01-07 13:30:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetStripeCount returns the stripe-count value for the rbd image.
|
|
|
|
//
|
|
|
|
// Implements:
|
2023-04-26 07:05:57 +00:00
|
|
|
//
|
|
|
|
// int rbd_get_stripe_count(rbd_image_t image, uint64_t *stripe_count);
|
2021-10-18 20:26:15 +00:00
|
|
|
func (image *Image) GetStripeCount() (uint64, error) {
|
2020-01-07 13:30:27 +00:00
|
|
|
if err := image.validate(imageIsOpen); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
2021-10-18 20:26:15 +00:00
|
|
|
var stripeCount uint64
|
|
|
|
if ret := C.rbd_get_stripe_count(image.image, (*C.uint64_t)(&stripeCount)); ret < 0 {
|
2020-08-11 14:42:21 +00:00
|
|
|
return 0, rbdError(ret)
|
2020-01-07 13:30:27 +00:00
|
|
|
}
|
|
|
|
|
2021-10-18 20:26:15 +00:00
|
|
|
return stripeCount, nil
|
2020-01-07 13:30:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetOverlap returns the overlapping bytes between the rbd image and its
|
|
|
|
// parent.
|
|
|
|
//
|
|
|
|
// Implements:
|
2023-04-26 07:05:57 +00:00
|
|
|
//
|
|
|
|
// int rbd_get_overlap(rbd_image_t image, uint64_t *overlap);
|
2020-01-07 13:30:27 +00:00
|
|
|
func (image *Image) GetOverlap() (overlap uint64, err error) {
|
|
|
|
if err := image.validate(imageIsOpen); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if ret := C.rbd_get_overlap(image.image, (*C.uint64_t)(&overlap)); ret < 0 {
|
2020-08-11 14:42:21 +00:00
|
|
|
return overlap, rbdError(ret)
|
2020-01-07 13:30:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return overlap, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Copy one rbd image to another.
|
|
|
|
//
|
|
|
|
// Implements:
|
2023-04-26 07:05:57 +00:00
|
|
|
//
|
|
|
|
// int rbd_copy(rbd_image_t image, rados_ioctx_t dest_io_ctx, const char *destname);
|
2020-01-07 13:30:27 +00:00
|
|
|
func (image *Image) Copy(ioctx *rados.IOContext, destname string) error {
|
|
|
|
if err := image.validate(imageIsOpen); err != nil {
|
|
|
|
return err
|
|
|
|
} else if ioctx == nil {
|
|
|
|
return ErrNoIOContext
|
|
|
|
} else if len(destname) == 0 {
|
|
|
|
return ErrNoName
|
|
|
|
}
|
|
|
|
|
2021-10-18 20:26:15 +00:00
|
|
|
cDestName := C.CString(destname)
|
|
|
|
defer C.free(unsafe.Pointer(cDestName))
|
2020-01-07 13:30:27 +00:00
|
|
|
|
|
|
|
return getError(C.rbd_copy(image.image,
|
2021-10-18 20:26:15 +00:00
|
|
|
cephIoctx(ioctx), cDestName))
|
2020-01-07 13:30:27 +00:00
|
|
|
}
|
|
|
|
|
2020-05-08 14:07:31 +00:00
|
|
|
// Copy2 copies one rbd image to another, using an image handle.
|
2020-01-07 13:30:27 +00:00
|
|
|
//
|
|
|
|
// Implements:
|
2023-04-26 07:05:57 +00:00
|
|
|
//
|
|
|
|
// int rbd_copy2(rbd_image_t src, rbd_image_t dest);
|
2020-01-07 13:30:27 +00:00
|
|
|
func (image *Image) Copy2(dest *Image) error {
|
|
|
|
if err := image.validate(imageIsOpen); err != nil {
|
|
|
|
return err
|
|
|
|
} else if err := dest.validate(imageIsOpen); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return getError(C.rbd_copy2(image.image, dest.image))
|
|
|
|
}
|
|
|
|
|
2021-06-09 04:54:52 +00:00
|
|
|
// DeepCopy an rbd image to a new image with specific options.
|
|
|
|
//
|
|
|
|
// Implements:
|
2023-04-26 07:05:57 +00:00
|
|
|
//
|
|
|
|
// int rbd_deep_copy(rbd_image_t src, rados_ioctx_t dest_io_ctx,
|
|
|
|
// const char *destname, rbd_image_options_t dest_opts);
|
2021-06-09 04:54:52 +00:00
|
|
|
func (image *Image) DeepCopy(ioctx *rados.IOContext, destname string, rio *ImageOptions) error {
|
|
|
|
if err := image.validate(imageIsOpen); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if ioctx == nil {
|
|
|
|
return ErrNoIOContext
|
|
|
|
}
|
|
|
|
if destname == "" {
|
|
|
|
return ErrNoName
|
|
|
|
}
|
|
|
|
if rio == nil {
|
|
|
|
return rbdError(C.EINVAL)
|
|
|
|
}
|
|
|
|
|
|
|
|
cDestname := C.CString(destname)
|
|
|
|
defer C.free(unsafe.Pointer(cDestname))
|
|
|
|
|
|
|
|
ret := C.rbd_deep_copy(image.image, cephIoctx(ioctx), cDestname,
|
|
|
|
C.rbd_image_options_t(rio.options))
|
|
|
|
return getError(ret)
|
|
|
|
}
|
|
|
|
|
2020-01-07 13:30:27 +00:00
|
|
|
// Flatten removes snapshot references from the image.
|
|
|
|
//
|
|
|
|
// Implements:
|
2023-04-26 07:05:57 +00:00
|
|
|
//
|
|
|
|
// int rbd_flatten(rbd_image_t image);
|
2020-01-07 13:30:27 +00:00
|
|
|
func (image *Image) Flatten() error {
|
|
|
|
if err := image.validate(imageIsOpen); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return getError(C.rbd_flatten(image.image))
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListLockers returns a list of clients that have locks on the image.
|
|
|
|
//
|
|
|
|
// Impelemnts:
|
2023-04-26 07:05:57 +00:00
|
|
|
//
|
|
|
|
// ssize_t rbd_list_lockers(rbd_image_t image, int *exclusive,
|
|
|
|
// char *tag, size_t *tag_len,
|
|
|
|
// char *clients, size_t *clients_len,
|
|
|
|
// char *cookies, size_t *cookies_len,
|
|
|
|
// char *addrs, size_t *addrs_len);
|
2020-01-07 13:30:27 +00:00
|
|
|
func (image *Image) ListLockers() (tag string, lockers []Locker, err error) {
|
|
|
|
if err := image.validate(imageIsOpen); err != nil {
|
|
|
|
return "", nil, err
|
|
|
|
}
|
|
|
|
|
2021-10-18 20:26:15 +00:00
|
|
|
var cExclusive C.int
|
|
|
|
var cTagLen, cClientsLen, cCookiesLen, cAddrsLen C.size_t
|
|
|
|
var cLockerCount C.ssize_t
|
2020-01-07 13:30:27 +00:00
|
|
|
|
2021-10-18 20:26:15 +00:00
|
|
|
C.rbd_list_lockers(image.image, &cExclusive,
|
|
|
|
nil, (*C.size_t)(&cTagLen),
|
|
|
|
nil, (*C.size_t)(&cClientsLen),
|
|
|
|
nil, (*C.size_t)(&cCookiesLen),
|
|
|
|
nil, (*C.size_t)(&cAddrsLen))
|
2020-01-07 13:30:27 +00:00
|
|
|
|
|
|
|
// no locker held on rbd image when either c_clients_len,
|
|
|
|
// c_cookies_len or c_addrs_len is *0*, so just quickly returned
|
2021-10-18 20:26:15 +00:00
|
|
|
if int(cClientsLen) == 0 || int(cCookiesLen) == 0 ||
|
|
|
|
int(cAddrsLen) == 0 {
|
2020-01-07 13:30:27 +00:00
|
|
|
lockers = make([]Locker, 0)
|
|
|
|
return "", lockers, nil
|
|
|
|
}
|
|
|
|
|
2021-10-18 20:26:15 +00:00
|
|
|
tagBuf := make([]byte, cTagLen)
|
|
|
|
clientsBuf := make([]byte, cClientsLen)
|
|
|
|
cookiesBuf := make([]byte, cCookiesLen)
|
|
|
|
addrsBuf := make([]byte, cAddrsLen)
|
2020-01-07 13:30:27 +00:00
|
|
|
|
2021-10-18 20:26:15 +00:00
|
|
|
cLockerCount = C.rbd_list_lockers(image.image, &cExclusive,
|
|
|
|
(*C.char)(unsafe.Pointer(&tagBuf[0])), (*C.size_t)(&cTagLen),
|
|
|
|
(*C.char)(unsafe.Pointer(&clientsBuf[0])), (*C.size_t)(&cClientsLen),
|
|
|
|
(*C.char)(unsafe.Pointer(&cookiesBuf[0])), (*C.size_t)(&cCookiesLen),
|
|
|
|
(*C.char)(unsafe.Pointer(&addrsBuf[0])), (*C.size_t)(&cAddrsLen))
|
2020-01-07 13:30:27 +00:00
|
|
|
|
|
|
|
// rbd_list_lockers returns negative value for errors
|
|
|
|
// and *0* means no locker held on rbd image.
|
|
|
|
// but *0* is unexpected here because first rbd_list_lockers already
|
|
|
|
// dealt with no locker case
|
2021-10-18 20:26:15 +00:00
|
|
|
if int(cLockerCount) <= 0 {
|
|
|
|
return "", nil, rbdError(cLockerCount)
|
2020-01-07 13:30:27 +00:00
|
|
|
}
|
|
|
|
|
2021-10-18 20:26:15 +00:00
|
|
|
clients := cutil.SplitSparseBuffer(clientsBuf)
|
|
|
|
cookies := cutil.SplitSparseBuffer(cookiesBuf)
|
|
|
|
addrs := cutil.SplitSparseBuffer(addrsBuf)
|
2020-01-07 13:30:27 +00:00
|
|
|
|
2021-10-18 20:26:15 +00:00
|
|
|
lockers = make([]Locker, cLockerCount)
|
|
|
|
for i := 0; i < int(cLockerCount); i++ {
|
2020-01-07 13:30:27 +00:00
|
|
|
lockers[i] = Locker{Client: clients[i],
|
|
|
|
Cookie: cookies[i],
|
|
|
|
Addr: addrs[i]}
|
|
|
|
}
|
|
|
|
|
2021-10-18 20:26:15 +00:00
|
|
|
return string(tagBuf), lockers, nil
|
2020-01-07 13:30:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// LockExclusive acquires an exclusive lock on the rbd image.
|
|
|
|
//
|
|
|
|
// Implements:
|
2023-04-26 07:05:57 +00:00
|
|
|
//
|
|
|
|
// int rbd_lock_exclusive(rbd_image_t image, const char *cookie);
|
2020-01-07 13:30:27 +00:00
|
|
|
func (image *Image) LockExclusive(cookie string) error {
|
|
|
|
if err := image.validate(imageIsOpen); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-10-18 20:26:15 +00:00
|
|
|
cCookie := C.CString(cookie)
|
|
|
|
defer C.free(unsafe.Pointer(cCookie))
|
2020-01-07 13:30:27 +00:00
|
|
|
|
2021-10-18 20:26:15 +00:00
|
|
|
return getError(C.rbd_lock_exclusive(image.image, cCookie))
|
2020-01-07 13:30:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// LockShared acquires a shared lock on the rbd image.
|
|
|
|
//
|
|
|
|
// Implements:
|
2023-04-26 07:05:57 +00:00
|
|
|
//
|
|
|
|
// int rbd_lock_shared(rbd_image_t image, const char *cookie, const char *tag);
|
2020-01-07 13:30:27 +00:00
|
|
|
func (image *Image) LockShared(cookie string, tag string) error {
|
|
|
|
if err := image.validate(imageIsOpen); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-10-18 20:26:15 +00:00
|
|
|
cCookie := C.CString(cookie)
|
|
|
|
cTag := C.CString(tag)
|
|
|
|
defer C.free(unsafe.Pointer(cCookie))
|
|
|
|
defer C.free(unsafe.Pointer(cTag))
|
2020-01-07 13:30:27 +00:00
|
|
|
|
2021-10-18 20:26:15 +00:00
|
|
|
return getError(C.rbd_lock_shared(image.image, cCookie, cTag))
|
2020-01-07 13:30:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Unlock releases a lock on the image.
|
|
|
|
//
|
|
|
|
// Implements:
|
2023-04-26 07:05:57 +00:00
|
|
|
//
|
|
|
|
// int rbd_lock_shared(rbd_image_t image, const char *cookie, const char *tag);
|
2020-01-07 13:30:27 +00:00
|
|
|
func (image *Image) Unlock(cookie string) error {
|
|
|
|
if err := image.validate(imageIsOpen); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-10-18 20:26:15 +00:00
|
|
|
cCookie := C.CString(cookie)
|
|
|
|
defer C.free(unsafe.Pointer(cCookie))
|
2020-01-07 13:30:27 +00:00
|
|
|
|
2021-10-18 20:26:15 +00:00
|
|
|
return getError(C.rbd_unlock(image.image, cCookie))
|
2020-01-07 13:30:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// BreakLock forces the release of a lock held by another client.
|
|
|
|
//
|
|
|
|
// Implements:
|
2023-04-26 07:05:57 +00:00
|
|
|
//
|
|
|
|
// int rbd_break_lock(rbd_image_t image, const char *client, const char *cookie);
|
2020-01-07 13:30:27 +00:00
|
|
|
func (image *Image) BreakLock(client string, cookie string) error {
|
|
|
|
if err := image.validate(imageIsOpen); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-10-18 20:26:15 +00:00
|
|
|
cClient := C.CString(client)
|
|
|
|
cCookie := C.CString(cookie)
|
|
|
|
defer C.free(unsafe.Pointer(cClient))
|
|
|
|
defer C.free(unsafe.Pointer(cCookie))
|
2020-01-07 13:30:27 +00:00
|
|
|
|
2021-10-18 20:26:15 +00:00
|
|
|
return getError(C.rbd_break_lock(image.image, cClient, cCookie))
|
2020-01-07 13:30:27 +00:00
|
|
|
}
|
|
|
|
|
2022-02-21 20:13:09 +00:00
|
|
|
// Read data from the image. The length of the read is determined by the length
|
|
|
|
// of the buffer slice. The position of the read is determined by an internal
|
|
|
|
// offset which is not safe in concurrent code. Prefer ReadAt when possible.
|
|
|
|
//
|
|
|
|
// Implements:
|
2023-04-26 07:05:57 +00:00
|
|
|
//
|
|
|
|
// ssize_t rbd_read(rbd_image_t image, uint64_t ofs, size_t len,
|
|
|
|
// char *buf);
|
2020-01-07 13:30:27 +00:00
|
|
|
func (image *Image) Read(data []byte) (int, error) {
|
|
|
|
if err := image.validate(imageIsOpen); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(data) == 0 {
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
ret := int(C.rbd_read(
|
|
|
|
image.image,
|
|
|
|
(C.uint64_t)(image.offset),
|
|
|
|
(C.size_t)(len(data)),
|
|
|
|
(*C.char)(unsafe.Pointer(&data[0]))))
|
|
|
|
|
|
|
|
if ret < 0 {
|
2020-08-11 14:42:21 +00:00
|
|
|
return 0, rbdError(ret)
|
2020-01-07 13:30:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
image.offset += int64(ret)
|
|
|
|
if ret < len(data) {
|
|
|
|
return ret, io.EOF
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret, nil
|
|
|
|
}
|
|
|
|
|
2022-02-21 20:13:09 +00:00
|
|
|
// Write data to an image. The length of the write is determined by the length of
|
|
|
|
// the buffer slice. The position of the write is determined by an internal
|
|
|
|
// offset which is not safe in concurrent code. Prefer WriteAt when possible.
|
|
|
|
//
|
|
|
|
// Implements:
|
2023-04-26 07:05:57 +00:00
|
|
|
//
|
|
|
|
// ssize_t rbd_write(rbd_image_t image, uint64_t ofs, size_t len,
|
|
|
|
// const char *buf);
|
2020-01-07 13:30:27 +00:00
|
|
|
func (image *Image) Write(data []byte) (n int, err error) {
|
|
|
|
if err := image.validate(imageIsOpen); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
ret := int(C.rbd_write(image.image, C.uint64_t(image.offset),
|
|
|
|
C.size_t(len(data)), (*C.char)(unsafe.Pointer(&data[0]))))
|
|
|
|
|
|
|
|
if ret >= 0 {
|
|
|
|
image.offset += int64(ret)
|
|
|
|
}
|
|
|
|
|
|
|
|
if ret != len(data) {
|
2020-08-11 14:42:21 +00:00
|
|
|
err = rbdError(-C.EPERM)
|
2020-01-07 13:30:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret, err
|
|
|
|
}
|
|
|
|
|
2020-05-08 14:07:31 +00:00
|
|
|
// Seek updates the internal file position for the current image.
|
2020-01-07 13:30:27 +00:00
|
|
|
func (image *Image) Seek(offset int64, whence int) (int64, error) {
|
|
|
|
switch whence {
|
|
|
|
case SeekSet:
|
|
|
|
image.offset = offset
|
|
|
|
case SeekCur:
|
|
|
|
image.offset += offset
|
|
|
|
case SeekEnd:
|
|
|
|
stats, err := image.Stat()
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
image.offset = int64(stats.Size) - offset
|
|
|
|
default:
|
|
|
|
return 0, errors.New("Wrong value for whence")
|
|
|
|
}
|
|
|
|
return image.offset, nil
|
|
|
|
}
|
|
|
|
|
2020-05-08 14:07:31 +00:00
|
|
|
// Discard the supplied range from the image. The supplied range will be read
|
|
|
|
// as zeros once Discard returns. The discarded range will no longer take up
|
|
|
|
// space.
|
|
|
|
//
|
|
|
|
// Implements:
|
2023-04-26 07:05:57 +00:00
|
|
|
//
|
|
|
|
// int rbd_discard(rbd_image_t image, uint64_t ofs, uint64_t len);
|
2020-01-07 13:30:27 +00:00
|
|
|
func (image *Image) Discard(ofs uint64, length uint64) (int, error) {
|
|
|
|
if err := image.validate(imageIsOpen); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
ret := C.rbd_discard(image.image, C.uint64_t(ofs), C.uint64_t(length))
|
|
|
|
if ret < 0 {
|
2020-08-11 14:42:21 +00:00
|
|
|
return 0, rbdError(ret)
|
2020-01-07 13:30:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return int(ret), nil
|
|
|
|
}
|
|
|
|
|
2020-05-08 14:07:31 +00:00
|
|
|
// ReadAt copies data from the image into the supplied buffer.
|
2020-01-07 13:30:27 +00:00
|
|
|
func (image *Image) ReadAt(data []byte, off int64) (int, error) {
|
|
|
|
if err := image.validate(imageIsOpen); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(data) == 0 {
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
ret := int(C.rbd_read(
|
|
|
|
image.image,
|
|
|
|
(C.uint64_t)(off),
|
|
|
|
(C.size_t)(len(data)),
|
|
|
|
(*C.char)(unsafe.Pointer(&data[0]))))
|
|
|
|
|
|
|
|
if ret < 0 {
|
2020-08-11 14:42:21 +00:00
|
|
|
return 0, rbdError(ret)
|
2020-01-07 13:30:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ret < len(data) {
|
|
|
|
return ret, io.EOF
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret, nil
|
|
|
|
}
|
|
|
|
|
2020-05-08 14:07:31 +00:00
|
|
|
// WriteAt copies data from the supplied buffer to the image.
|
2020-01-07 13:30:27 +00:00
|
|
|
func (image *Image) WriteAt(data []byte, off int64) (n int, err error) {
|
|
|
|
if err := image.validate(imageIsOpen); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(data) == 0 {
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
ret := int(C.rbd_write(image.image, C.uint64_t(off),
|
|
|
|
C.size_t(len(data)), (*C.char)(unsafe.Pointer(&data[0]))))
|
|
|
|
|
|
|
|
if ret != len(data) {
|
2020-08-11 14:42:21 +00:00
|
|
|
err = rbdError(-C.EPERM)
|
2020-01-07 13:30:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret, err
|
|
|
|
}
|
|
|
|
|
2020-12-09 05:46:45 +00:00
|
|
|
// WriteSame repeats writing data from starting point ofs until n bytes have
|
|
|
|
// been written.
|
|
|
|
//
|
|
|
|
// Implements:
|
2023-04-26 07:05:57 +00:00
|
|
|
//
|
|
|
|
// ssize_t rbd_writesame(rbd_image_t image, uint64_t ofs, size_t len,
|
|
|
|
// const char *buf, size_t data_len, int op_flags);
|
2020-12-09 05:46:45 +00:00
|
|
|
func (image *Image) WriteSame(ofs, n uint64, data []byte, flags rados.OpFlags) (int64, error) {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
if err = image.validate(imageIsOpen); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(data) == 0 {
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
ret := C.rbd_writesame(image.image,
|
|
|
|
C.uint64_t(ofs),
|
2021-06-09 04:54:52 +00:00
|
|
|
C.size_t(n),
|
2020-12-09 05:46:45 +00:00
|
|
|
(*C.char)(unsafe.Pointer(&data[0])),
|
|
|
|
C.size_t(len(data)),
|
|
|
|
C.int(flags))
|
|
|
|
if ret < 0 {
|
|
|
|
err = getError(C.int(ret))
|
|
|
|
}
|
|
|
|
|
|
|
|
return int64(ret), err
|
|
|
|
}
|
|
|
|
|
2020-05-08 14:07:31 +00:00
|
|
|
// Flush all cached writes to storage.
|
|
|
|
//
|
|
|
|
// Implements:
|
2023-04-26 07:05:57 +00:00
|
|
|
//
|
|
|
|
// int rbd_flush(rbd_image_t image);
|
2020-01-07 13:30:27 +00:00
|
|
|
func (image *Image) Flush() error {
|
|
|
|
if err := image.validate(imageIsOpen); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return getError(C.rbd_flush(image.image))
|
|
|
|
}
|
|
|
|
|
2020-05-08 14:07:31 +00:00
|
|
|
// GetSnapshotNames returns more than just the names of snapshots
|
|
|
|
// associated with the rbd image.
|
|
|
|
//
|
|
|
|
// Implements:
|
2023-04-26 07:05:57 +00:00
|
|
|
//
|
|
|
|
// int rbd_snap_list(rbd_image_t image, rbd_snap_info_t *snaps, int *max_snaps);
|
2020-01-07 13:30:27 +00:00
|
|
|
func (image *Image) GetSnapshotNames() (snaps []SnapInfo, err error) {
|
|
|
|
if err := image.validate(imageIsOpen); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-10-18 20:26:15 +00:00
|
|
|
var cMaxSnaps C.int
|
2020-01-07 13:30:27 +00:00
|
|
|
|
2021-10-18 20:26:15 +00:00
|
|
|
ret := C.rbd_snap_list(image.image, nil, &cMaxSnaps)
|
2020-01-07 13:30:27 +00:00
|
|
|
|
2021-10-18 20:26:15 +00:00
|
|
|
cSnaps := make([]C.rbd_snap_info_t, cMaxSnaps)
|
|
|
|
snaps = make([]SnapInfo, cMaxSnaps)
|
2020-01-07 13:30:27 +00:00
|
|
|
|
|
|
|
ret = C.rbd_snap_list(image.image,
|
2021-10-18 20:26:15 +00:00
|
|
|
&cSnaps[0], &cMaxSnaps)
|
2020-01-07 13:30:27 +00:00
|
|
|
if ret < 0 {
|
2020-08-11 14:42:21 +00:00
|
|
|
return nil, rbdError(ret)
|
2020-01-07 13:30:27 +00:00
|
|
|
}
|
|
|
|
|
2021-10-18 20:26:15 +00:00
|
|
|
for i, s := range cSnaps {
|
2020-01-07 13:30:27 +00:00
|
|
|
snaps[i] = SnapInfo{Id: uint64(s.id),
|
|
|
|
Size: uint64(s.size),
|
|
|
|
Name: C.GoString(s.name)}
|
|
|
|
}
|
|
|
|
|
2021-10-18 20:26:15 +00:00
|
|
|
C.rbd_snap_list_end(&cSnaps[0])
|
2020-01-07 13:30:27 +00:00
|
|
|
return snaps[:len(snaps)-1], nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetId returns the internal image ID string.
|
|
|
|
//
|
|
|
|
// Implements:
|
2023-04-26 07:05:57 +00:00
|
|
|
//
|
|
|
|
// int rbd_get_id(rbd_image_t image, char *id, size_t id_len);
|
2020-01-07 13:30:27 +00:00
|
|
|
func (image *Image) GetId() (string, error) {
|
|
|
|
if err := image.validate(imageIsOpen); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2020-06-17 09:37:06 +00:00
|
|
|
var (
|
|
|
|
err error
|
|
|
|
buf []byte
|
|
|
|
)
|
|
|
|
retry.WithSizes(1, 8192, func(size int) retry.Hint {
|
|
|
|
buf = make([]byte, size)
|
2020-01-07 13:30:27 +00:00
|
|
|
ret := C.rbd_get_id(
|
|
|
|
image.image,
|
|
|
|
(*C.char)(unsafe.Pointer(&buf[0])),
|
2020-06-17 09:37:06 +00:00
|
|
|
C.size_t(size))
|
|
|
|
err = getErrorIfNegative(ret)
|
|
|
|
return retry.DoubleSize.If(err == errRange)
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
2020-01-07 13:30:27 +00:00
|
|
|
}
|
2020-06-17 09:37:06 +00:00
|
|
|
id := C.GoString((*C.char)(unsafe.Pointer(&buf[0])))
|
|
|
|
return id, nil
|
|
|
|
|
2020-01-07 13:30:27 +00:00
|
|
|
}
|
|
|
|
|
2021-12-15 05:26:34 +00:00
|
|
|
// GetName returns the image name.
|
|
|
|
func (image *Image) GetName() string {
|
|
|
|
return image.name
|
|
|
|
}
|
|
|
|
|
2021-06-09 04:54:52 +00:00
|
|
|
// SetSnapshot updates the rbd image (not the Snapshot) such that the snapshot
|
|
|
|
// is the source of readable data.
|
|
|
|
//
|
|
|
|
// Implements:
|
2023-04-26 07:05:57 +00:00
|
|
|
//
|
|
|
|
// int rbd_snap_set(rbd_image_t image, const char *snapname);
|
2021-06-09 04:54:52 +00:00
|
|
|
func (image *Image) SetSnapshot(snapname string) error {
|
|
|
|
if err := image.validate(imageIsOpen); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-10-18 20:26:15 +00:00
|
|
|
cSnapName := C.CString(snapname)
|
|
|
|
defer C.free(unsafe.Pointer(cSnapName))
|
2021-06-09 04:54:52 +00:00
|
|
|
|
2021-10-18 20:26:15 +00:00
|
|
|
return getError(C.rbd_snap_set(image.image, cSnapName))
|
2021-06-09 04:54:52 +00:00
|
|
|
}
|
|
|
|
|
2020-01-07 13:30:27 +00:00
|
|
|
// GetTrashList returns a slice of TrashInfo structs, containing information about all RBD images
|
|
|
|
// currently residing in the trash.
|
|
|
|
func GetTrashList(ioctx *rados.IOContext) ([]TrashInfo, error) {
|
2020-06-17 09:37:06 +00:00
|
|
|
var (
|
|
|
|
err error
|
|
|
|
count C.size_t
|
|
|
|
entries []C.rbd_trash_image_info_t
|
|
|
|
)
|
2023-01-09 11:52:23 +00:00
|
|
|
retry.WithSizes(32, 10240, func(size int) retry.Hint {
|
2020-06-17 09:37:06 +00:00
|
|
|
count = C.size_t(size)
|
|
|
|
entries = make([]C.rbd_trash_image_info_t, count)
|
|
|
|
ret := C.rbd_trash_list(cephIoctx(ioctx), &entries[0], &count)
|
|
|
|
err = getErrorIfNegative(ret)
|
|
|
|
return retry.Size(int(count)).If(err == errRange)
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2020-01-07 13:30:27 +00:00
|
|
|
}
|
2020-06-17 09:37:06 +00:00
|
|
|
// Free rbd_trash_image_info_t pointers
|
|
|
|
defer C.rbd_trash_list_cleanup(&entries[0], count)
|
2020-01-07 13:30:27 +00:00
|
|
|
|
2020-06-17 09:37:06 +00:00
|
|
|
trashList := make([]TrashInfo, count)
|
|
|
|
for i, ti := range entries[:count] {
|
2020-01-07 13:30:27 +00:00
|
|
|
trashList[i] = TrashInfo{
|
|
|
|
Id: C.GoString(ti.id),
|
|
|
|
Name: C.GoString(ti.name),
|
|
|
|
DeletionTime: time.Unix(int64(ti.deletion_time), 0),
|
|
|
|
DefermentEndTime: time.Unix(int64(ti.deferment_end_time), 0),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return trashList, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// TrashRemove permanently deletes the trashed RBD with the specified id.
|
|
|
|
func TrashRemove(ioctx *rados.IOContext, id string, force bool) error {
|
2021-10-18 20:26:15 +00:00
|
|
|
cid := C.CString(id)
|
|
|
|
defer C.free(unsafe.Pointer(cid))
|
2020-01-07 13:30:27 +00:00
|
|
|
|
2021-10-18 20:26:15 +00:00
|
|
|
return getError(C.rbd_trash_remove(cephIoctx(ioctx), cid, C.bool(force)))
|
2020-01-07 13:30:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TrashRestore restores the trashed RBD with the specified id back to the pool from whence it
|
|
|
|
// came, with the specified new name.
|
|
|
|
func TrashRestore(ioctx *rados.IOContext, id, name string) error {
|
2021-10-18 20:26:15 +00:00
|
|
|
cid := C.CString(id)
|
|
|
|
cName := C.CString(name)
|
|
|
|
defer C.free(unsafe.Pointer(cid))
|
|
|
|
defer C.free(unsafe.Pointer(cName))
|
2020-01-07 13:30:27 +00:00
|
|
|
|
2021-10-18 20:26:15 +00:00
|
|
|
return getError(C.rbd_trash_restore(cephIoctx(ioctx), cid, cName))
|
2020-01-07 13:30:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// OpenImage will open an existing rbd image by name and snapshot name,
|
|
|
|
// returning a new opened image. Pass the NoSnapshot sentinel value as the
|
|
|
|
// snapName to explicitly indicate that no snapshot name is being provided.
|
|
|
|
//
|
|
|
|
// Implements:
|
2023-04-26 07:05:57 +00:00
|
|
|
//
|
|
|
|
// int rbd_open(rados_ioctx_t io, const char *name,
|
|
|
|
// rbd_image_t *image, const char *snap_name);
|
2020-01-07 13:30:27 +00:00
|
|
|
func OpenImage(ioctx *rados.IOContext, name, snapName string) (*Image, error) {
|
2020-06-17 09:37:06 +00:00
|
|
|
if ioctx == nil {
|
|
|
|
return nil, ErrNoIOContext
|
|
|
|
}
|
|
|
|
if name == "" {
|
|
|
|
return nil, ErrNoName
|
|
|
|
}
|
|
|
|
|
2020-01-07 13:30:27 +00:00
|
|
|
cName := C.CString(name)
|
|
|
|
defer C.free(unsafe.Pointer(cName))
|
|
|
|
|
|
|
|
var cSnapName *C.char
|
|
|
|
if snapName != NoSnapshot {
|
|
|
|
cSnapName = C.CString(snapName)
|
|
|
|
defer C.free(unsafe.Pointer(cSnapName))
|
|
|
|
}
|
|
|
|
|
|
|
|
var cImage C.rbd_image_t
|
|
|
|
ret := C.rbd_open(
|
2020-05-08 14:07:31 +00:00
|
|
|
cephIoctx(ioctx),
|
2020-01-07 13:30:27 +00:00
|
|
|
cName,
|
|
|
|
&cImage,
|
|
|
|
cSnapName)
|
|
|
|
|
|
|
|
if ret != 0 {
|
|
|
|
return nil, getError(ret)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &Image{
|
|
|
|
ioctx: ioctx,
|
|
|
|
name: name,
|
|
|
|
image: cImage,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// OpenImageReadOnly will open an existing rbd image by name and snapshot name,
|
|
|
|
// returning a new opened-for-read image. Pass the NoSnapshot sentinel value
|
|
|
|
// as the snapName to explicitly indicate that no snapshot name is being
|
|
|
|
// provided.
|
|
|
|
//
|
|
|
|
// Implements:
|
2023-04-26 07:05:57 +00:00
|
|
|
//
|
|
|
|
// int rbd_open_read_only(rados_ioctx_t io, const char *name,
|
|
|
|
// rbd_image_t *image, const char *snap_name);
|
2020-01-07 13:30:27 +00:00
|
|
|
func OpenImageReadOnly(ioctx *rados.IOContext, name, snapName string) (*Image, error) {
|
2020-06-17 09:37:06 +00:00
|
|
|
if ioctx == nil {
|
|
|
|
return nil, ErrNoIOContext
|
|
|
|
}
|
|
|
|
if name == "" {
|
|
|
|
return nil, ErrNoName
|
|
|
|
}
|
|
|
|
|
2020-01-07 13:30:27 +00:00
|
|
|
cName := C.CString(name)
|
|
|
|
defer C.free(unsafe.Pointer(cName))
|
|
|
|
|
|
|
|
var cSnapName *C.char
|
|
|
|
if snapName != NoSnapshot {
|
|
|
|
cSnapName = C.CString(snapName)
|
|
|
|
defer C.free(unsafe.Pointer(cSnapName))
|
|
|
|
}
|
|
|
|
|
|
|
|
var cImage C.rbd_image_t
|
|
|
|
ret := C.rbd_open_read_only(
|
2020-05-08 14:07:31 +00:00
|
|
|
cephIoctx(ioctx),
|
2020-01-07 13:30:27 +00:00
|
|
|
cName,
|
|
|
|
&cImage,
|
|
|
|
cSnapName)
|
|
|
|
|
|
|
|
if ret != 0 {
|
|
|
|
return nil, getError(ret)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &Image{
|
|
|
|
ioctx: ioctx,
|
|
|
|
name: name,
|
|
|
|
image: cImage,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// OpenImageById will open an existing rbd image by ID and snapshot name,
|
|
|
|
// returning a new opened image. Pass the NoSnapshot sentinel value as the
|
|
|
|
// snapName to explicitly indicate that no snapshot name is being provided.
|
|
|
|
// Error handling will fail & segfault unless compiled with a version of ceph
|
|
|
|
// that fixes https://tracker.ceph.com/issues/43178
|
|
|
|
//
|
|
|
|
// Implements:
|
2023-04-26 07:05:57 +00:00
|
|
|
//
|
|
|
|
// int rbd_open_by_id(rados_ioctx_t io, const char *id,
|
|
|
|
// rbd_image_t *image, const char *snap_name);
|
2020-01-07 13:30:27 +00:00
|
|
|
func OpenImageById(ioctx *rados.IOContext, id, snapName string) (*Image, error) {
|
2020-06-17 09:37:06 +00:00
|
|
|
if ioctx == nil {
|
|
|
|
return nil, ErrNoIOContext
|
|
|
|
}
|
|
|
|
if id == "" {
|
|
|
|
return nil, ErrNoName
|
|
|
|
}
|
|
|
|
|
2020-01-07 13:30:27 +00:00
|
|
|
cid := C.CString(id)
|
|
|
|
defer C.free(unsafe.Pointer(cid))
|
|
|
|
|
|
|
|
var cSnapName *C.char
|
|
|
|
if snapName != NoSnapshot {
|
|
|
|
cSnapName = C.CString(snapName)
|
|
|
|
defer C.free(unsafe.Pointer(cSnapName))
|
|
|
|
}
|
|
|
|
|
|
|
|
var cImage C.rbd_image_t
|
|
|
|
ret := C.rbd_open_by_id(
|
2020-05-08 14:07:31 +00:00
|
|
|
cephIoctx(ioctx),
|
2020-01-07 13:30:27 +00:00
|
|
|
cid,
|
|
|
|
&cImage,
|
|
|
|
cSnapName)
|
|
|
|
|
|
|
|
if ret != 0 {
|
|
|
|
return nil, getError(ret)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &Image{
|
|
|
|
ioctx: ioctx,
|
|
|
|
image: cImage,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// OpenImageByIdReadOnly will open an existing rbd image by ID and snapshot
|
|
|
|
// name, returning a new opened-for-read image. Pass the NoSnapshot sentinel
|
|
|
|
// value as the snapName to explicitly indicate that no snapshot name is being
|
|
|
|
// provided.
|
|
|
|
// Error handling will fail & segfault unless compiled with a version of ceph
|
|
|
|
// that fixes https://tracker.ceph.com/issues/43178
|
|
|
|
//
|
|
|
|
// Implements:
|
2023-04-26 07:05:57 +00:00
|
|
|
//
|
|
|
|
// int rbd_open_by_id_read_only(rados_ioctx_t io, const char *id,
|
|
|
|
// rbd_image_t *image, const char *snap_name);
|
2020-01-07 13:30:27 +00:00
|
|
|
func OpenImageByIdReadOnly(ioctx *rados.IOContext, id, snapName string) (*Image, error) {
|
2020-06-17 09:37:06 +00:00
|
|
|
if ioctx == nil {
|
|
|
|
return nil, ErrNoIOContext
|
|
|
|
}
|
|
|
|
if id == "" {
|
|
|
|
return nil, ErrNoName
|
|
|
|
}
|
|
|
|
|
2020-01-07 13:30:27 +00:00
|
|
|
cid := C.CString(id)
|
|
|
|
defer C.free(unsafe.Pointer(cid))
|
|
|
|
|
|
|
|
var cSnapName *C.char
|
|
|
|
if snapName != NoSnapshot {
|
|
|
|
cSnapName = C.CString(snapName)
|
|
|
|
defer C.free(unsafe.Pointer(cSnapName))
|
|
|
|
}
|
|
|
|
|
|
|
|
var cImage C.rbd_image_t
|
|
|
|
ret := C.rbd_open_by_id_read_only(
|
2020-05-08 14:07:31 +00:00
|
|
|
cephIoctx(ioctx),
|
2020-01-07 13:30:27 +00:00
|
|
|
cid,
|
|
|
|
&cImage,
|
|
|
|
cSnapName)
|
|
|
|
|
|
|
|
if ret != 0 {
|
|
|
|
return nil, getError(ret)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &Image{
|
|
|
|
ioctx: ioctx,
|
|
|
|
image: cImage,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateImage creates a new rbd image using provided image options.
|
|
|
|
//
|
|
|
|
// Implements:
|
2023-04-26 07:05:57 +00:00
|
|
|
//
|
|
|
|
// int rbd_create4(rados_ioctx_t io, const char *name, uint64_t size,
|
|
|
|
// rbd_image_options_t opts);
|
2020-05-08 14:07:31 +00:00
|
|
|
func CreateImage(ioctx *rados.IOContext, name string, size uint64, rio *ImageOptions) error {
|
2020-06-17 09:37:06 +00:00
|
|
|
if ioctx == nil {
|
|
|
|
return ErrNoIOContext
|
|
|
|
}
|
|
|
|
if name == "" {
|
|
|
|
return ErrNoName
|
|
|
|
}
|
2020-01-07 13:30:27 +00:00
|
|
|
if rio == nil {
|
2020-08-11 14:42:21 +00:00
|
|
|
return rbdError(C.EINVAL)
|
2020-01-07 13:30:27 +00:00
|
|
|
}
|
|
|
|
|
2021-10-18 20:26:15 +00:00
|
|
|
cName := C.CString(name)
|
|
|
|
defer C.free(unsafe.Pointer(cName))
|
2020-01-07 13:30:27 +00:00
|
|
|
|
2021-10-18 20:26:15 +00:00
|
|
|
ret := C.rbd_create4(cephIoctx(ioctx), cName,
|
2020-01-07 13:30:27 +00:00
|
|
|
C.uint64_t(size), C.rbd_image_options_t(rio.options))
|
|
|
|
return getError(ret)
|
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveImage removes the specified rbd image.
|
|
|
|
//
|
|
|
|
// Implements:
|
2023-04-26 07:05:57 +00:00
|
|
|
//
|
|
|
|
// int rbd_remove(rados_ioctx_t io, const char *name);
|
2020-01-07 13:30:27 +00:00
|
|
|
func RemoveImage(ioctx *rados.IOContext, name string) error {
|
2020-06-17 09:37:06 +00:00
|
|
|
if ioctx == nil {
|
|
|
|
return ErrNoIOContext
|
|
|
|
}
|
|
|
|
if name == "" {
|
|
|
|
return ErrNoName
|
|
|
|
}
|
|
|
|
|
2021-10-18 20:26:15 +00:00
|
|
|
cName := C.CString(name)
|
|
|
|
defer C.free(unsafe.Pointer(cName))
|
|
|
|
return getError(C.rbd_remove(cephIoctx(ioctx), cName))
|
2020-01-07 13:30:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// CloneImage creates a clone of the image from the named snapshot in the
|
|
|
|
// provided io-context with the given name and image options.
|
|
|
|
//
|
|
|
|
// Implements:
|
2023-04-26 07:05:57 +00:00
|
|
|
//
|
|
|
|
// int rbd_clone3(rados_ioctx_t p_ioctx, const char *p_name,
|
|
|
|
// const char *p_snapname, rados_ioctx_t c_ioctx,
|
|
|
|
// const char *c_name, rbd_image_options_t c_opts);
|
2020-01-07 13:30:27 +00:00
|
|
|
func CloneImage(ioctx *rados.IOContext, parentName, snapName string,
|
2020-05-08 14:07:31 +00:00
|
|
|
destctx *rados.IOContext, name string, rio *ImageOptions) error {
|
2020-01-07 13:30:27 +00:00
|
|
|
|
|
|
|
if rio == nil {
|
2020-08-11 14:42:21 +00:00
|
|
|
return rbdError(C.EINVAL)
|
2020-01-07 13:30:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cParentName := C.CString(parentName)
|
|
|
|
defer C.free(unsafe.Pointer(cParentName))
|
|
|
|
cParentSnapName := C.CString(snapName)
|
|
|
|
defer C.free(unsafe.Pointer(cParentSnapName))
|
|
|
|
cCloneName := C.CString(name)
|
|
|
|
defer C.free(unsafe.Pointer(cCloneName))
|
|
|
|
|
|
|
|
ret := C.rbd_clone3(
|
2020-05-08 14:07:31 +00:00
|
|
|
cephIoctx(ioctx),
|
2020-01-07 13:30:27 +00:00
|
|
|
cParentName,
|
|
|
|
cParentSnapName,
|
2020-05-08 14:07:31 +00:00
|
|
|
cephIoctx(destctx),
|
2020-01-07 13:30:27 +00:00
|
|
|
cCloneName,
|
|
|
|
C.rbd_image_options_t(rio.options))
|
|
|
|
return getError(ret)
|
|
|
|
}
|
|
|
|
|
|
|
|
// CloneFromImage creates a clone of the image from the named snapshot in the
|
|
|
|
// provided io-context with the given name and image options.
|
|
|
|
// This function is a convenience wrapper around CloneImage to support cloning
|
|
|
|
// from an existing Image.
|
|
|
|
func CloneFromImage(parent *Image, snapName string,
|
2020-05-08 14:07:31 +00:00
|
|
|
destctx *rados.IOContext, name string, rio *ImageOptions) error {
|
2020-01-07 13:30:27 +00:00
|
|
|
|
|
|
|
if err := parent.validate(imageNeedsIOContext); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return CloneImage(parent.ioctx, parent.name, snapName, destctx, name, rio)
|
|
|
|
}
|