2021-02-11 09:59:47 +00:00
|
|
|
/*
|
|
|
|
Copyright 2021 The Ceph-CSI Authors.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
package rbd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
librbd "github.com/ceph/go-ceph/rbd"
|
|
|
|
)
|
|
|
|
|
|
|
|
// enableImageMirroring enables mirroring on an image.
|
2021-03-17 07:54:19 +00:00
|
|
|
func (ri *rbdImage) enableImageMirroring(mode librbd.ImageMirrorMode) error {
|
|
|
|
image, err := ri.open()
|
2021-02-11 09:59:47 +00:00
|
|
|
if err != nil {
|
2021-05-07 05:30:37 +00:00
|
|
|
return fmt.Errorf("failed to open image %q with error: %w", ri, err)
|
2021-02-11 09:59:47 +00:00
|
|
|
}
|
|
|
|
defer image.Close()
|
|
|
|
|
|
|
|
err = image.MirrorEnable(mode)
|
|
|
|
if err != nil {
|
2021-05-07 05:30:37 +00:00
|
|
|
return fmt.Errorf("failed to enable mirroring on %q with error: %w", ri, err)
|
2021-02-11 09:59:47 +00:00
|
|
|
}
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2021-02-11 09:59:47 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// disableImageMirroring disables mirroring on an image.
|
2021-03-17 07:54:19 +00:00
|
|
|
func (ri *rbdImage) disableImageMirroring(force bool) error {
|
|
|
|
image, err := ri.open()
|
2021-02-11 09:59:47 +00:00
|
|
|
if err != nil {
|
2021-05-07 05:30:37 +00:00
|
|
|
return fmt.Errorf("failed to open image %q with error: %w", ri, err)
|
2021-02-11 09:59:47 +00:00
|
|
|
}
|
|
|
|
defer image.Close()
|
|
|
|
|
|
|
|
err = image.MirrorDisable(force)
|
|
|
|
if err != nil {
|
2021-05-07 05:30:37 +00:00
|
|
|
return fmt.Errorf("failed to disable mirroring on %q with error: %w", ri, err)
|
2021-02-11 09:59:47 +00:00
|
|
|
}
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2021-02-11 09:59:47 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// getImageMirroringInfo gets mirroring information of an image.
|
2021-03-17 07:54:19 +00:00
|
|
|
func (ri *rbdImage) getImageMirroringInfo() (*librbd.MirrorImageInfo, error) {
|
|
|
|
image, err := ri.open()
|
2021-02-11 09:59:47 +00:00
|
|
|
if err != nil {
|
2021-05-07 05:30:37 +00:00
|
|
|
return nil, fmt.Errorf("failed to open image %q with error: %w", ri, err)
|
2021-02-11 09:59:47 +00:00
|
|
|
}
|
|
|
|
defer image.Close()
|
|
|
|
|
|
|
|
info, err := image.GetMirrorImageInfo()
|
|
|
|
if err != nil {
|
2021-05-07 05:30:37 +00:00
|
|
|
return nil, fmt.Errorf("failed to get mirroring info of %q with error: %w", ri, err)
|
2021-02-11 09:59:47 +00:00
|
|
|
}
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2021-02-11 09:59:47 +00:00
|
|
|
return info, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// promoteImage promotes image to primary.
|
2021-03-17 07:54:19 +00:00
|
|
|
func (ri *rbdImage) promoteImage(force bool) error {
|
|
|
|
image, err := ri.open()
|
2021-02-11 09:59:47 +00:00
|
|
|
if err != nil {
|
2021-05-07 05:30:37 +00:00
|
|
|
return fmt.Errorf("failed to open image %q with error: %w", ri, err)
|
2021-02-11 09:59:47 +00:00
|
|
|
}
|
|
|
|
defer image.Close()
|
|
|
|
err = image.MirrorPromote(force)
|
|
|
|
if err != nil {
|
2021-05-07 05:30:37 +00:00
|
|
|
return fmt.Errorf("failed to promote image %q with error: %w", ri, err)
|
2021-02-11 09:59:47 +00:00
|
|
|
}
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2021-02-11 09:59:47 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// demoteImage demotes image to secondary.
|
2021-03-17 07:54:19 +00:00
|
|
|
func (ri *rbdImage) demoteImage() error {
|
|
|
|
image, err := ri.open()
|
2021-02-11 09:59:47 +00:00
|
|
|
if err != nil {
|
2021-05-07 05:30:37 +00:00
|
|
|
return fmt.Errorf("failed to open image %q with error: %w", ri, err)
|
2021-02-11 09:59:47 +00:00
|
|
|
}
|
|
|
|
defer image.Close()
|
|
|
|
err = image.MirrorDemote()
|
|
|
|
if err != nil {
|
2021-05-07 05:30:37 +00:00
|
|
|
return fmt.Errorf("failed to demote image %q with error: %w", ri, err)
|
2021-02-11 09:59:47 +00:00
|
|
|
}
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2021-02-11 09:59:47 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// resyncImage resync image to correct the split-brain.
|
2021-03-17 07:54:19 +00:00
|
|
|
func (ri *rbdImage) resyncImage() error {
|
|
|
|
image, err := ri.open()
|
2021-02-11 09:59:47 +00:00
|
|
|
if err != nil {
|
2021-05-07 05:30:37 +00:00
|
|
|
return fmt.Errorf("failed to open image %q with error: %w", ri, err)
|
2021-02-11 09:59:47 +00:00
|
|
|
}
|
|
|
|
defer image.Close()
|
|
|
|
err = image.MirrorResync()
|
|
|
|
if err != nil {
|
2021-05-07 05:30:37 +00:00
|
|
|
return fmt.Errorf("failed to resync image %q with error: %w", ri, err)
|
2021-02-11 09:59:47 +00:00
|
|
|
}
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2021-02-11 09:59:47 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-06-23 13:14:48 +00:00
|
|
|
// getImageMirroingStatus get the mirroring status of an image.
|
|
|
|
func (ri *rbdImage) getImageMirroringStatus() (*librbd.GlobalMirrorImageStatus, error) {
|
|
|
|
image, err := ri.open()
|
2021-02-11 09:59:47 +00:00
|
|
|
if err != nil {
|
2021-06-23 13:14:48 +00:00
|
|
|
return nil, fmt.Errorf("failed to open image %q with error: %w", ri, err)
|
2021-02-11 09:59:47 +00:00
|
|
|
}
|
2021-06-23 13:14:48 +00:00
|
|
|
defer image.Close()
|
|
|
|
statusInfo, err := image.GetGlobalMirrorStatus()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to get image mirroring status %q with error: %w", ri, err)
|
2021-02-11 09:59:47 +00:00
|
|
|
}
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2021-06-23 13:14:48 +00:00
|
|
|
return &statusInfo, nil
|
2021-02-11 09:59:47 +00:00
|
|
|
}
|
2021-08-12 05:17:25 +00:00
|
|
|
|
|
|
|
// getLocalState returns the local state of the image.
|
|
|
|
func (ri *rbdImage) getLocalState() (librbd.SiteMirrorImageStatus, error) {
|
|
|
|
localStatus := librbd.SiteMirrorImageStatus{}
|
|
|
|
image, err := ri.open()
|
|
|
|
if err != nil {
|
|
|
|
return localStatus, fmt.Errorf("failed to open image %q with error: %w", ri, err)
|
|
|
|
}
|
|
|
|
defer image.Close()
|
|
|
|
|
|
|
|
statusInfo, err := image.GetGlobalMirrorStatus()
|
|
|
|
if err != nil {
|
|
|
|
return localStatus, fmt.Errorf("failed to get image mirroring status %q with error: %w", ri, err)
|
|
|
|
}
|
|
|
|
localStatus, err = statusInfo.LocalStatus()
|
|
|
|
if err != nil {
|
|
|
|
return localStatus, fmt.Errorf("failed to get local status: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return localStatus, nil
|
|
|
|
}
|