Skip to content

Commit

Permalink
rbd: check parent image while mirroring of child
Browse files Browse the repository at this point in the history
This commit adds code to check parent image while enabling
mirroring on child image. It errors out if the parent is in
trash or not mirror enabled.
This enables better communication with the user on why mirroring
cannot be enabled on the child image.

Signed-off-by: Rakshith R <[email protected]>
  • Loading branch information
Rakshith-R committed Mar 20, 2024
1 parent 3ad922d commit 37cd9f1
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions internal/rbd/mirror.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,39 @@ func (ri *rbdImage) EnableImageMirroring(mode librbd.ImageMirrorMode) error {
}
defer image.Close()

parentInfo, err := image.GetParent()
if err != nil {
return fmt.Errorf("failed to get parent info of image %q: %w",
ri, err)
}

if parentInfo.Image.Trash {
return fmt.Errorf("failed to enable mirroring on image %q:"+
" parent %q is in trash",
parentInfo.Image.ImageID, ri)
}

parent, err := ri.getParent()
if err != nil {
return fmt.Errorf("failed to enable mirroring on image %q:"+
" failed to get parent: %w", ri, err)
}

if parent != nil {
parentMirroringInfo, err := parent.GetImageMirroringInfo()
if err != nil {
return fmt.Errorf("failed to enable mirroring on image %q:"+
" failed to get mirroring info of parent %q: %w",
ri, parent, err)
}

if parentMirroringInfo.State != librbd.MirrorImageEnabled {
return fmt.Errorf("failed to enable mirroring on image %q:"+
"parent image %q is not enabled for mirroring",
ri, parent)
}
}

err = image.MirrorEnable(mode)
if err != nil {
return fmt.Errorf("failed to enable mirroring on %q with error: %w", ri, err)
Expand Down

0 comments on commit 37cd9f1

Please sign in to comment.