rbd: add support to create clone in different pool

added support to create image in different pool.
if the snapshot/rbd image exists in one pool we
can create a clone the clone of the rbd image to
a different pool.

Co-authored-by: Madhu Rajanna <madhupr007@gmail.com>
Signed-off-by: Yug <yuggupta27@gmail.com>
This commit is contained in:
Yug 2021-03-18 16:53:08 +05:30 committed by mergify[bot]
parent 671d6a7767
commit 961c1d12fd
4 changed files with 20 additions and 7 deletions

View File

@ -101,7 +101,7 @@ func (rv *rbdVolume) checkCloneImage(ctx context.Context, parentVol *rbdVolume)
// need to check for flatten here. // need to check for flatten here.
// as the snap exists,create clone image and delete temporary snapshot // as the snap exists,create clone image and delete temporary snapshot
// and add task to flatten temporary cloned image // and add task to flatten temporary cloned image
err = rv.cloneRbdImageFromSnapshot(ctx, snap) err = rv.cloneRbdImageFromSnapshot(ctx, snap, parentVol)
if err != nil { if err != nil {
util.ErrorLog(ctx, "failed to clone rbd image %s from snapshot %s: %v", rv.RbdImageName, snap.RbdSnapName, err) util.ErrorLog(ctx, "failed to clone rbd image %s from snapshot %s: %v", rv.RbdImageName, snap.RbdSnapName, err)
err = fmt.Errorf("failed to clone rbd image %s from snapshot %s: %w", rv.RbdImageName, snap.RbdSnapName, err) err = fmt.Errorf("failed to clone rbd image %s from snapshot %s: %w", rv.RbdImageName, snap.RbdSnapName, err)

View File

@ -464,8 +464,12 @@ func (cs *ControllerServer) createVolumeFromSnapshot(ctx context.Context, cr *ut
// update parent name(rbd image name in snapshot) // update parent name(rbd image name in snapshot)
rbdSnap.RbdImageName = rbdSnap.RbdSnapName rbdSnap.RbdImageName = rbdSnap.RbdSnapName
parentVol := generateVolFromSnap(rbdSnap)
// as we are operating on single cluster reuse the connection
parentVol.conn = rbdVol.conn.Copy()
// create clone image and delete snapshot // create clone image and delete snapshot
err = rbdVol.cloneRbdImageFromSnapshot(ctx, rbdSnap) err = rbdVol.cloneRbdImageFromSnapshot(ctx, rbdSnap, parentVol)
if err != nil { if err != nil {
util.ErrorLog(ctx, "failed to clone rbd image %s from snapshot %s: %v", rbdVol, rbdSnap, err) util.ErrorLog(ctx, "failed to clone rbd image %s from snapshot %s: %v", rbdVol, rbdSnap, err)
return err return err

View File

@ -1079,11 +1079,19 @@ func (rv *rbdVolume) deleteSnapshot(ctx context.Context, pOpts *rbdSnapshot) err
return err return err
} }
func (rv *rbdVolume) cloneRbdImageFromSnapshot(ctx context.Context, pSnapOpts *rbdSnapshot) error { func (rv *rbdVolume) cloneRbdImageFromSnapshot(ctx context.Context, pSnapOpts *rbdSnapshot, parentVol *rbdVolume) error {
image := rv.RbdImageName
var err error var err error
logMsg := "rbd: clone %s %s (features: %s) using mon %s" logMsg := "rbd: clone %s %s (features: %s) using mon %s"
err = parentVol.openIoctx()
if err != nil {
return fmt.Errorf("failed to get parent IOContext: %w", err)
}
defer func() {
defer parentVol.ioctx.Destroy()
parentVol.ioctx = nil
}()
options := librbd.NewRbdImageOptions() options := librbd.NewRbdImageOptions()
defer options.Destroy() defer options.Destroy()
@ -1096,7 +1104,7 @@ func (rv *rbdVolume) cloneRbdImageFromSnapshot(ctx context.Context, pSnapOpts *r
} }
util.DebugLog(ctx, logMsg, util.DebugLog(ctx, logMsg,
pSnapOpts, image, rv.imageFeatureSet.Names(), rv.Monitors) pSnapOpts, rv, rv.imageFeatureSet.Names(), rv.Monitors)
if rv.imageFeatureSet != 0 { if rv.imageFeatureSet != 0 {
err = options.SetUint64(librbd.RbdImageOptionFeatures, uint64(rv.imageFeatureSet)) err = options.SetUint64(librbd.RbdImageOptionFeatures, uint64(rv.imageFeatureSet))
@ -1110,12 +1118,13 @@ func (rv *rbdVolume) cloneRbdImageFromSnapshot(ctx context.Context, pSnapOpts *r
return fmt.Errorf("failed to set image features: %w", err) return fmt.Errorf("failed to set image features: %w", err)
} }
// As the clone is yet to be created, open the Ioctx.
err = rv.openIoctx() err = rv.openIoctx()
if err != nil { if err != nil {
return fmt.Errorf("failed to get IOContext: %w", err) return fmt.Errorf("failed to get IOContext: %w", err)
} }
err = librbd.CloneImage(rv.ioctx, pSnapOpts.RbdImageName, pSnapOpts.RbdSnapName, rv.ioctx, rv.RbdImageName, options) err = librbd.CloneImage(parentVol.ioctx, pSnapOpts.RbdImageName, pSnapOpts.RbdSnapName, rv.ioctx, rv.RbdImageName, options)
if err != nil { if err != nil {
return fmt.Errorf("failed to create rbd clone: %w", err) return fmt.Errorf("failed to create rbd clone: %w", err)
} }

View File

@ -33,7 +33,7 @@ func createRBDClone(ctx context.Context, parentVol, cloneRbdVol *rbdVolume, snap
snap.RbdImageName = parentVol.RbdImageName snap.RbdImageName = parentVol.RbdImageName
// create clone image and delete snapshot // create clone image and delete snapshot
err = cloneRbdVol.cloneRbdImageFromSnapshot(ctx, snap) err = cloneRbdVol.cloneRbdImageFromSnapshot(ctx, snap, parentVol)
if err != nil { if err != nil {
util.ErrorLog(ctx, "failed to clone rbd image %s from snapshot %s: %v", cloneRbdVol.RbdImageName, snap.RbdSnapName, err) util.ErrorLog(ctx, "failed to clone rbd image %s from snapshot %s: %v", cloneRbdVol.RbdImageName, snap.RbdSnapName, err)
err = fmt.Errorf("failed to clone rbd image %s from snapshot %s: %w", cloneRbdVol.RbdImageName, snap.RbdSnapName, err) err = fmt.Errorf("failed to clone rbd image %s from snapshot %s: %w", cloneRbdVol.RbdImageName, snap.RbdSnapName, err)