From 37cd9f168142e559f6f0c154d00757027b6d74dc Mon Sep 17 00:00:00 2001 From: Rakshith R Date: Tue, 20 Feb 2024 15:28:27 +0530 Subject: [PATCH] rbd: check parent image while mirroring of child 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 --- internal/rbd/mirror.go | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/internal/rbd/mirror.go b/internal/rbd/mirror.go index 4f6507614c3..b17feb5b6f9 100644 --- a/internal/rbd/mirror.go +++ b/internal/rbd/mirror.go @@ -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)