2018-01-09 18:59:50 +00:00
|
|
|
/*
|
2019-04-03 08:46:15 +00:00
|
|
|
Copyright 2018 The Ceph-CSI Authors.
|
2018-01-09 18:59:50 +00:00
|
|
|
|
|
|
|
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 (
|
2019-08-24 09:14:15 +00:00
|
|
|
"context"
|
2019-04-22 21:35:39 +00:00
|
|
|
"encoding/json"
|
2020-06-25 11:30:04 +00:00
|
|
|
"errors"
|
2018-01-09 18:59:50 +00:00
|
|
|
"fmt"
|
2022-05-24 03:38:42 +00:00
|
|
|
"math"
|
2019-08-03 22:11:28 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2019-12-13 11:41:32 +00:00
|
|
|
"strconv"
|
2018-01-09 18:59:50 +00:00
|
|
|
"strings"
|
|
|
|
"time"
|
2018-03-06 22:33:57 +00:00
|
|
|
|
2024-07-10 10:53:04 +00:00
|
|
|
"github.com/ceph/ceph-csi/internal/rbd/types"
|
2020-04-17 09:23:49 +00:00
|
|
|
"github.com/ceph/ceph-csi/internal/util"
|
2021-08-24 15:03:25 +00:00
|
|
|
"github.com/ceph/ceph-csi/internal/util/log"
|
2020-04-15 03:38:16 +00:00
|
|
|
|
2020-03-16 09:26:43 +00:00
|
|
|
"github.com/ceph/go-ceph/rados"
|
2020-01-07 13:45:52 +00:00
|
|
|
librbd "github.com/ceph/go-ceph/rbd"
|
2021-06-28 11:38:42 +00:00
|
|
|
"github.com/ceph/go-ceph/rbd/admin"
|
2020-01-24 16:26:56 +00:00
|
|
|
"github.com/container-storage-interface/spec/lib/go/csi"
|
2018-07-20 08:46:44 +00:00
|
|
|
"k8s.io/apimachinery/pkg/util/sets"
|
2020-01-17 15:44:06 +00:00
|
|
|
"k8s.io/cloud-provider/volume/helpers"
|
2021-06-15 12:25:29 +00:00
|
|
|
mount "k8s.io/mount-utils"
|
2018-01-09 18:59:50 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// The following three values are used for 30 seconds timeout
|
|
|
|
// while waiting for RBD Watcher to expire.
|
|
|
|
rbdImageWatcherInitDelay = 1 * time.Second
|
|
|
|
rbdImageWatcherFactor = 1.4
|
|
|
|
rbdImageWatcherSteps = 10
|
2018-09-18 14:09:12 +00:00
|
|
|
rbdDefaultMounter = "rbd"
|
2021-03-11 06:28:48 +00:00
|
|
|
rbdNbdMounter = "rbd-nbd"
|
2021-08-18 07:21:23 +00:00
|
|
|
defaultLogDir = "/var/log/ceph"
|
2021-09-01 11:53:43 +00:00
|
|
|
defaultLogStrategy = "remove" // supports remove, compress and preserve
|
2019-08-06 16:59:40 +00:00
|
|
|
|
|
|
|
// Output strings returned during invocation of "ceph rbd task add remove <imagespec>" when
|
|
|
|
// command is not supported by ceph manager. Used to check errors and recover when the command
|
|
|
|
// is unsupported.
|
2021-11-09 11:41:17 +00:00
|
|
|
rbdTaskRemoveCmdInvalidString = "No handler found"
|
|
|
|
rbdTaskRemoveCmdAccessDeniedMessage = "access denied:"
|
2021-01-28 08:45:51 +00:00
|
|
|
|
2021-10-01 04:18:19 +00:00
|
|
|
// migration label key and value for parameters in volume context.
|
2021-09-16 08:26:06 +00:00
|
|
|
intreeMigrationKey = "migration"
|
|
|
|
intreeMigrationLabel = "true"
|
2021-10-01 04:18:19 +00:00
|
|
|
migInTreeImagePrefix = "kubernetes-dynamic-pvc-"
|
|
|
|
// migration volume handle identifiers.
|
|
|
|
// total length of fields in the migration volume handle.
|
|
|
|
migVolIDTotalLength = 4
|
|
|
|
// split boundary length of fields.
|
|
|
|
migVolIDSplitLength = 3
|
|
|
|
// separator for migration handle fields.
|
|
|
|
migVolIDFieldSep = "_"
|
|
|
|
// identifier of a migration vol handle.
|
|
|
|
migIdentifier = "mig"
|
|
|
|
// prefix of image field.
|
|
|
|
migImageNamePrefix = "image-"
|
|
|
|
// prefix in the handle for monitors field.
|
|
|
|
migMonPrefix = "mons-"
|
2021-10-05 09:29:07 +00:00
|
|
|
|
|
|
|
// krbd attribute file to check supported features.
|
|
|
|
krbdSupportedFeaturesFile = "/sys/bus/rbd/supported_features"
|
2022-04-11 04:27:29 +00:00
|
|
|
|
|
|
|
// clusterNameKey cluster Key, set on RBD image.
|
|
|
|
clusterNameKey = "csi.ceph.com/cluster/name"
|
2018-01-09 18:59:50 +00:00
|
|
|
)
|
|
|
|
|
2021-03-12 12:37:15 +00:00
|
|
|
// rbdImage contains common attributes and methods for the rbdVolume and
|
|
|
|
// rbdSnapshot types.
|
|
|
|
type rbdImage struct {
|
|
|
|
// RbdImageName is the name of the RBD image backing this rbdVolume.
|
|
|
|
// This does not have a JSON tag as it is not stashed in JSON encoded
|
|
|
|
// config maps in v1.0.0
|
|
|
|
RbdImageName string
|
|
|
|
// ImageID contains the image id of the image
|
|
|
|
ImageID string
|
|
|
|
// VolID is the volume ID that is exchanged with CSI drivers,
|
|
|
|
// identifying this rbd image
|
cleanup: reduce struct padding
internal/rbd/rbd_util.go:89:15: struct of size 312 bytes could be of
size 304 bytes:
``
struct{
RbdImageName string,
ImageID string,
VolID string,
Monitors string,
JournalPool string,
Pool string,
RadosNamespace string,
ClusterID string,
RequestName string,
NamePrefix string,
ParentName string,
ParentPool string,
ClusterName string,
Owner string,
VolSize int64,
StripeCount uint64,
StripeUnit uint64,
ObjectSize uint64,
ImageFeatureSet github.com/ceph/go-ceph/rbd.FeatureSet,
encryption
*github.com/ceph/ceph-csi/internal/util.VolumeEncryption,
CreatedAt
*google.golang.org/protobuf/types/known/timestamppb.Timestamp,
conn
*github.com/ceph/ceph-csi/internal/util.ClusterConnection,
ioctx *github.com/ceph/go-ceph/rados.IOContext,
Primary bool,
EnableMetadata bool,
}
`` (maligned)
type rbdImage struct {
^}`
make: *** [Makefile:118: go-lint] Error 1
Signed-off-by: Prasanna Kumar Kalever <prasanna.kalever@redhat.com>
2022-06-16 10:58:31 +00:00
|
|
|
VolID string `json:"volID"`
|
2021-03-12 12:37:15 +00:00
|
|
|
Monitors string
|
2021-04-19 05:23:55 +00:00
|
|
|
// JournalPool is the ceph pool in which the CSI Journal/CSI snapshot Journal is
|
2021-03-12 12:37:15 +00:00
|
|
|
// stored
|
|
|
|
JournalPool string
|
2021-04-19 05:23:55 +00:00
|
|
|
// Pool is where the image journal/image snapshot journal and image/snapshot
|
|
|
|
// is stored, and could be the same as `JournalPool` (retained as Pool instead of
|
2021-03-12 12:37:15 +00:00
|
|
|
// renaming to ImagePool or such, as this is referenced in the code
|
|
|
|
// extensively)
|
|
|
|
Pool string
|
|
|
|
RadosNamespace string
|
|
|
|
ClusterID string `json:"clusterId"`
|
|
|
|
// RequestName is the CSI generated volume name for the rbdVolume.
|
|
|
|
// This does not have a JSON tag as it is not stashed in JSON encoded
|
|
|
|
// config maps in v1.0.0
|
|
|
|
RequestName string
|
2024-09-24 10:55:26 +00:00
|
|
|
ReservedID string
|
2021-03-12 12:37:15 +00:00
|
|
|
NamePrefix string
|
2021-12-20 15:14:28 +00:00
|
|
|
// ParentName represents the parent image name of the image.
|
|
|
|
ParentName string
|
|
|
|
// Parent Pool is the pool that contains the parent image.
|
cleanup: reduce struct padding
internal/rbd/rbd_util.go:89:15: struct of size 312 bytes could be of
size 304 bytes:
``
struct{
RbdImageName string,
ImageID string,
VolID string,
Monitors string,
JournalPool string,
Pool string,
RadosNamespace string,
ClusterID string,
RequestName string,
NamePrefix string,
ParentName string,
ParentPool string,
ClusterName string,
Owner string,
VolSize int64,
StripeCount uint64,
StripeUnit uint64,
ObjectSize uint64,
ImageFeatureSet github.com/ceph/go-ceph/rbd.FeatureSet,
encryption
*github.com/ceph/ceph-csi/internal/util.VolumeEncryption,
CreatedAt
*google.golang.org/protobuf/types/known/timestamppb.Timestamp,
conn
*github.com/ceph/ceph-csi/internal/util.ClusterConnection,
ioctx *github.com/ceph/go-ceph/rados.IOContext,
Primary bool,
EnableMetadata bool,
}
`` (maligned)
type rbdImage struct {
^}`
make: *** [Makefile:118: go-lint] Error 1
Signed-off-by: Prasanna Kumar Kalever <prasanna.kalever@redhat.com>
2022-06-16 10:58:31 +00:00
|
|
|
ParentPool string
|
2022-04-11 04:27:29 +00:00
|
|
|
// Cluster name
|
|
|
|
ClusterName string
|
2022-05-27 18:03:32 +00:00
|
|
|
|
2021-03-12 12:37:15 +00:00
|
|
|
// Owner is the creator (tenant, Kubernetes Namespace) of the volume
|
|
|
|
Owner string
|
|
|
|
|
cleanup: reduce struct padding
internal/rbd/rbd_util.go:89:15: struct of size 312 bytes could be of
size 304 bytes:
``
struct{
RbdImageName string,
ImageID string,
VolID string,
Monitors string,
JournalPool string,
Pool string,
RadosNamespace string,
ClusterID string,
RequestName string,
NamePrefix string,
ParentName string,
ParentPool string,
ClusterName string,
Owner string,
VolSize int64,
StripeCount uint64,
StripeUnit uint64,
ObjectSize uint64,
ImageFeatureSet github.com/ceph/go-ceph/rbd.FeatureSet,
encryption
*github.com/ceph/ceph-csi/internal/util.VolumeEncryption,
CreatedAt
*google.golang.org/protobuf/types/known/timestamppb.Timestamp,
conn
*github.com/ceph/ceph-csi/internal/util.ClusterConnection,
ioctx *github.com/ceph/go-ceph/rados.IOContext,
Primary bool,
EnableMetadata bool,
}
`` (maligned)
type rbdImage struct {
^}`
make: *** [Makefile:118: go-lint] Error 1
Signed-off-by: Prasanna Kumar Kalever <prasanna.kalever@redhat.com>
2022-06-16 10:58:31 +00:00
|
|
|
// VolSize is the size of the RBD image backing this rbdImage.
|
|
|
|
VolSize int64
|
|
|
|
|
|
|
|
// image striping configurations.
|
|
|
|
StripeCount uint64
|
|
|
|
StripeUnit uint64
|
|
|
|
ObjectSize uint64
|
2021-03-12 12:37:15 +00:00
|
|
|
|
cleanup: reduce struct padding
internal/rbd/rbd_util.go:89:15: struct of size 312 bytes could be of
size 304 bytes:
``
struct{
RbdImageName string,
ImageID string,
VolID string,
Monitors string,
JournalPool string,
Pool string,
RadosNamespace string,
ClusterID string,
RequestName string,
NamePrefix string,
ParentName string,
ParentPool string,
ClusterName string,
Owner string,
VolSize int64,
StripeCount uint64,
StripeUnit uint64,
ObjectSize uint64,
ImageFeatureSet github.com/ceph/go-ceph/rbd.FeatureSet,
encryption
*github.com/ceph/ceph-csi/internal/util.VolumeEncryption,
CreatedAt
*google.golang.org/protobuf/types/known/timestamppb.Timestamp,
conn
*github.com/ceph/ceph-csi/internal/util.ClusterConnection,
ioctx *github.com/ceph/go-ceph/rados.IOContext,
Primary bool,
EnableMetadata bool,
}
`` (maligned)
type rbdImage struct {
^}`
make: *** [Makefile:118: go-lint] Error 1
Signed-off-by: Prasanna Kumar Kalever <prasanna.kalever@redhat.com>
2022-06-16 10:58:31 +00:00
|
|
|
ImageFeatureSet librbd.FeatureSet
|
2022-05-27 18:03:32 +00:00
|
|
|
|
|
|
|
// blockEncryption provides access to optional VolumeEncryption functions (e.g LUKS)
|
|
|
|
blockEncryption *util.VolumeEncryption
|
|
|
|
// fileEncryption provides access to optional VolumeEncryption functions (e.g fscrypt)
|
|
|
|
fileEncryption *util.VolumeEncryption
|
|
|
|
|
2024-08-05 16:27:15 +00:00
|
|
|
CreatedAt *time.Time
|
2022-05-27 18:03:32 +00:00
|
|
|
|
2021-03-12 12:37:15 +00:00
|
|
|
// conn is a connection to the Ceph cluster obtained from a ConnPool
|
|
|
|
conn *util.ClusterConnection
|
|
|
|
// an opened IOContext, call .openIoctx() before using
|
|
|
|
ioctx *rados.IOContext
|
cleanup: reduce struct padding
internal/rbd/rbd_util.go:89:15: struct of size 312 bytes could be of
size 304 bytes:
``
struct{
RbdImageName string,
ImageID string,
VolID string,
Monitors string,
JournalPool string,
Pool string,
RadosNamespace string,
ClusterID string,
RequestName string,
NamePrefix string,
ParentName string,
ParentPool string,
ClusterName string,
Owner string,
VolSize int64,
StripeCount uint64,
StripeUnit uint64,
ObjectSize uint64,
ImageFeatureSet github.com/ceph/go-ceph/rbd.FeatureSet,
encryption
*github.com/ceph/ceph-csi/internal/util.VolumeEncryption,
CreatedAt
*google.golang.org/protobuf/types/known/timestamppb.Timestamp,
conn
*github.com/ceph/ceph-csi/internal/util.ClusterConnection,
ioctx *github.com/ceph/go-ceph/rados.IOContext,
Primary bool,
EnableMetadata bool,
}
`` (maligned)
type rbdImage struct {
^}`
make: *** [Makefile:118: go-lint] Error 1
Signed-off-by: Prasanna Kumar Kalever <prasanna.kalever@redhat.com>
2022-06-16 10:58:31 +00:00
|
|
|
|
|
|
|
// Set metadata on volume
|
|
|
|
EnableMetadata bool
|
2024-03-29 09:36:01 +00:00
|
|
|
// ParentInTrash indicates the parent image is in trash.
|
|
|
|
ParentInTrash bool
|
2021-03-12 12:37:15 +00:00
|
|
|
}
|
|
|
|
|
2024-07-05 08:25:34 +00:00
|
|
|
// check that rbdVolume implements the types.Volume interface.
|
|
|
|
var _ types.Volume = &rbdVolume{}
|
|
|
|
|
2020-07-19 12:21:03 +00:00
|
|
|
// rbdVolume represents a CSI volume and its RBD image specifics.
|
2018-03-06 22:33:57 +00:00
|
|
|
type rbdVolume struct {
|
2021-03-12 12:37:15 +00:00
|
|
|
rbdImage
|
|
|
|
|
2019-05-31 18:09:24 +00:00
|
|
|
// VolName and MonValueFromSecret are retained from older plugin versions (<= 1.0.0)
|
2021-06-11 08:06:32 +00:00
|
|
|
// for backward compatibility reasons
|
2020-01-24 16:26:56 +00:00
|
|
|
TopologyPools *[]util.TopologyConstrainedPool
|
|
|
|
TopologyRequirement *csi.TopologyRequirement
|
|
|
|
Topology map[string]string
|
2021-03-12 12:37:15 +00:00
|
|
|
// DataPool is where the data for images in `Pool` are stored, this is used as the `--data-pool`
|
2021-06-11 08:06:32 +00:00
|
|
|
// argument when the pool is created, and is not used anywhere else
|
2021-12-20 15:14:28 +00:00
|
|
|
DataPool string
|
2021-12-20 14:03:52 +00:00
|
|
|
AdminID string
|
|
|
|
UserID string
|
|
|
|
Mounter string
|
2021-03-12 12:37:15 +00:00
|
|
|
MapOptions string
|
|
|
|
UnmapOptions string
|
2021-08-18 07:21:23 +00:00
|
|
|
LogDir string
|
2021-09-01 11:53:43 +00:00
|
|
|
LogStrategy string
|
2021-12-20 14:03:52 +00:00
|
|
|
VolName string
|
|
|
|
MonValueFromSecret string
|
2022-03-31 13:29:33 +00:00
|
|
|
// Network namespace file path to execute nsenter command
|
|
|
|
NetNamespaceFilePath string
|
2021-12-20 13:49:35 +00:00
|
|
|
// RequestedVolSize has the size of the volume requested by the user and
|
|
|
|
// this value will not be updated when doing getImageInfo() on rbdVolume.
|
|
|
|
RequestedVolSize int64
|
2021-12-20 14:03:52 +00:00
|
|
|
DisableInUseChecks bool
|
2021-03-12 12:37:15 +00:00
|
|
|
readOnly bool
|
2018-01-09 18:59:50 +00:00
|
|
|
}
|
|
|
|
|
2020-07-19 12:21:03 +00:00
|
|
|
// rbdSnapshot represents a CSI snapshot and its RBD snapshot specifics.
|
2018-08-08 05:42:17 +00:00
|
|
|
type rbdSnapshot struct {
|
2021-03-12 12:37:15 +00:00
|
|
|
rbdImage
|
|
|
|
|
2019-04-22 21:35:39 +00:00
|
|
|
// SourceVolumeID is the volume ID of RbdImageName, that is exchanged with CSI drivers
|
|
|
|
// RbdSnapName is the name of the RBD snapshot backing this rbdSnapshot
|
|
|
|
SourceVolumeID string
|
|
|
|
RbdSnapName string
|
2018-08-08 05:42:17 +00:00
|
|
|
}
|
|
|
|
|
2021-03-11 06:28:48 +00:00
|
|
|
// imageFeature represents required image features and value.
|
|
|
|
type imageFeature struct {
|
|
|
|
// needRbdNbd indicates whether this image feature requires an rbd-nbd mounter
|
|
|
|
needRbdNbd bool
|
|
|
|
// dependsOn is the image features required for this imageFeature
|
|
|
|
dependsOn []string
|
|
|
|
}
|
|
|
|
|
2021-10-01 04:18:19 +00:00
|
|
|
// migrationvolID is a struct which consists of required fields of a rbd volume
|
|
|
|
// from migrated volumeID.
|
|
|
|
type migrationVolID struct {
|
|
|
|
imageName string
|
|
|
|
poolName string
|
|
|
|
clusterID string
|
|
|
|
}
|
|
|
|
|
2022-05-06 13:33:25 +00:00
|
|
|
var (
|
|
|
|
supportedFeatures = map[string]imageFeature{
|
|
|
|
librbd.FeatureNameLayering: {
|
|
|
|
needRbdNbd: false,
|
|
|
|
},
|
|
|
|
librbd.FeatureNameExclusiveLock: {
|
|
|
|
needRbdNbd: false,
|
|
|
|
},
|
|
|
|
librbd.FeatureNameObjectMap: {
|
|
|
|
needRbdNbd: false,
|
|
|
|
dependsOn: []string{librbd.FeatureNameExclusiveLock},
|
|
|
|
},
|
|
|
|
librbd.FeatureNameFastDiff: {
|
|
|
|
needRbdNbd: false,
|
|
|
|
dependsOn: []string{librbd.FeatureNameObjectMap},
|
|
|
|
},
|
|
|
|
librbd.FeatureNameJournaling: {
|
|
|
|
needRbdNbd: true,
|
|
|
|
dependsOn: []string{librbd.FeatureNameExclusiveLock},
|
|
|
|
},
|
|
|
|
librbd.FeatureNameDeepFlatten: {
|
|
|
|
needRbdNbd: false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
krbdLayeringSupport = []util.KernelVersion{
|
|
|
|
{
|
|
|
|
Version: 3,
|
|
|
|
PatchLevel: 8,
|
|
|
|
SubLevel: 0,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
krbdStripingV2Support = []util.KernelVersion{
|
|
|
|
{
|
|
|
|
Version: 3,
|
|
|
|
PatchLevel: 10,
|
|
|
|
SubLevel: 0,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
krbdExclusiveLockSupport = []util.KernelVersion{
|
|
|
|
{
|
|
|
|
Version: 4,
|
|
|
|
PatchLevel: 9,
|
|
|
|
SubLevel: 0,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
krbdDataPoolSupport = []util.KernelVersion{
|
|
|
|
{
|
|
|
|
Version: 4,
|
|
|
|
PatchLevel: 11,
|
|
|
|
SubLevel: 0,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
// prepareKrbdFeatureAttrs prepare krbd fearure set based on kernel version.
|
|
|
|
// Minimum kernel version should be 3.8, else it will return error.
|
|
|
|
func prepareKrbdFeatureAttrs() (uint64, error) {
|
|
|
|
// fetch the current running kernel info
|
|
|
|
release, err := util.GetKernelVersion()
|
|
|
|
if err != nil {
|
|
|
|
return 0, fmt.Errorf("fetching current kernel version failed: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case util.CheckKernelSupport(release, krbdDataPoolSupport):
|
|
|
|
return librbd.FeatureDataPool, nil
|
|
|
|
case util.CheckKernelSupport(release, krbdExclusiveLockSupport):
|
|
|
|
return librbd.FeatureExclusiveLock, nil
|
|
|
|
case util.CheckKernelSupport(release, krbdStripingV2Support):
|
|
|
|
return librbd.FeatureStripingV2, nil
|
|
|
|
case util.CheckKernelSupport(release, krbdLayeringSupport):
|
|
|
|
return librbd.FeatureLayering, nil
|
|
|
|
}
|
|
|
|
log.ErrorLogMsg("kernel version is too old: %q", release)
|
|
|
|
|
|
|
|
return 0, os.ErrNotExist
|
2021-12-09 08:12:37 +00:00
|
|
|
}
|
2021-10-05 09:29:07 +00:00
|
|
|
|
2021-12-09 08:12:37 +00:00
|
|
|
// GetKrbdSupportedFeatures load the module if needed and return supported
|
2021-10-05 09:29:07 +00:00
|
|
|
// features attribute as a string.
|
2021-12-09 08:12:37 +00:00
|
|
|
func GetKrbdSupportedFeatures() (string, error) {
|
2022-02-14 11:18:31 +00:00
|
|
|
var stderr string
|
2021-10-05 09:29:07 +00:00
|
|
|
// check if the module is loaded or compiled in
|
|
|
|
_, err := os.Stat(krbdSupportedFeaturesFile)
|
|
|
|
if err != nil {
|
|
|
|
if !os.IsNotExist(err) {
|
|
|
|
log.ErrorLogMsg("stat on %q failed: %v", krbdSupportedFeaturesFile, err)
|
|
|
|
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
// try to load the module
|
2022-02-14 11:18:31 +00:00
|
|
|
_, stderr, err = util.ExecCommand(context.TODO(), "modprobe", rbdDefaultMounter)
|
2021-10-05 09:29:07 +00:00
|
|
|
if err != nil {
|
2022-02-14 11:18:31 +00:00
|
|
|
log.ErrorLogMsg("modprobe failed (%v): %q", err, stderr)
|
2021-10-05 09:29:07 +00:00
|
|
|
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
}
|
2022-01-21 09:32:34 +00:00
|
|
|
val, err := os.ReadFile(krbdSupportedFeaturesFile)
|
2021-10-05 09:29:07 +00:00
|
|
|
if err != nil {
|
2022-05-06 13:33:25 +00:00
|
|
|
if !errors.Is(err, os.ErrNotExist) {
|
|
|
|
log.ErrorLogMsg("reading file %q failed: %v", krbdSupportedFeaturesFile, err)
|
|
|
|
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
attrs, err := prepareKrbdFeatureAttrs()
|
|
|
|
if err != nil {
|
|
|
|
log.ErrorLogMsg("preparing krbd feature attributes failed, %v", err)
|
|
|
|
|
|
|
|
return "", err
|
|
|
|
}
|
2021-10-05 09:29:07 +00:00
|
|
|
|
2022-05-06 13:33:25 +00:00
|
|
|
return strconv.FormatUint(attrs, 16), nil
|
2021-10-05 09:29:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return strings.TrimSuffix(string(val), "\n"), nil
|
|
|
|
}
|
|
|
|
|
2021-12-09 08:16:32 +00:00
|
|
|
// HexStringToInteger convert hex value to uint.
|
|
|
|
func HexStringToInteger(hexString string) (uint, error) {
|
2021-10-05 09:29:07 +00:00
|
|
|
// trim 0x prefix
|
|
|
|
numberStr := strings.TrimPrefix(strings.ToLower(hexString), "0x")
|
|
|
|
|
|
|
|
output, err := strconv.ParseUint(numberStr, 16, 64)
|
|
|
|
if err != nil {
|
|
|
|
log.ErrorLogMsg("converting string %q to integer failed: %v", numberStr, err)
|
|
|
|
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
2021-12-09 08:16:32 +00:00
|
|
|
return uint(output), nil
|
2021-10-05 09:29:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// isKrbdFeatureSupported checks if a given Image Feature is supported by krbd
|
|
|
|
// driver or not.
|
2022-05-13 07:02:11 +00:00
|
|
|
func isKrbdFeatureSupported(ctx context.Context, imageFeatures string) (bool, error) {
|
|
|
|
// return false when /sys/bus/rbd/supported_features is absent and we are
|
|
|
|
// not in a position to prepare krbd feature attributes, i.e. if kernel <= 3.8
|
|
|
|
if krbdFeatures == 0 {
|
|
|
|
return false, os.ErrNotExist
|
|
|
|
}
|
2021-10-05 09:29:07 +00:00
|
|
|
arr := strings.Split(imageFeatures, ",")
|
|
|
|
log.UsefulLog(ctx, "checking for ImageFeatures: %v", arr)
|
|
|
|
imageFeatureSet := librbd.FeatureSetFromNames(arr)
|
|
|
|
|
|
|
|
supported := true
|
|
|
|
for _, featureName := range imageFeatureSet.Names() {
|
2021-12-09 08:16:32 +00:00
|
|
|
if (uint(librbd.FeatureSetFromNames(strings.Split(featureName, " "))) & krbdFeatures) == 0 {
|
2021-10-05 09:29:07 +00:00
|
|
|
supported = false
|
|
|
|
log.ErrorLog(ctx, "krbd feature %q not supported", featureName)
|
|
|
|
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-13 07:02:11 +00:00
|
|
|
return supported, nil
|
2021-07-13 12:21:05 +00:00
|
|
|
}
|
2018-01-09 18:59:50 +00:00
|
|
|
|
2020-07-19 12:21:03 +00:00
|
|
|
// Connect an rbdVolume to the Ceph cluster.
|
2021-03-12 12:37:15 +00:00
|
|
|
func (ri *rbdImage) Connect(cr *util.Credentials) error {
|
|
|
|
if ri.conn != nil {
|
2020-03-18 08:30:02 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
conn := &util.ClusterConnection{}
|
2021-03-12 12:37:15 +00:00
|
|
|
if err := conn.Connect(ri.Monitors, cr); err != nil {
|
2020-03-18 08:30:02 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-03-12 12:37:15 +00:00
|
|
|
ri.conn = conn
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2020-03-18 08:30:02 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Destroy cleans up the rbdVolume and closes the connection to the Ceph
|
|
|
|
// cluster in case one was setup.
|
2024-03-21 14:01:56 +00:00
|
|
|
func (ri *rbdImage) Destroy(ctx context.Context) {
|
2021-03-12 12:37:15 +00:00
|
|
|
if ri.ioctx != nil {
|
|
|
|
ri.ioctx.Destroy()
|
2024-09-25 09:36:32 +00:00
|
|
|
ri.ioctx = nil
|
2020-06-03 07:37:44 +00:00
|
|
|
}
|
2021-03-12 12:37:15 +00:00
|
|
|
if ri.conn != nil {
|
|
|
|
ri.conn.Destroy()
|
2024-09-25 09:36:32 +00:00
|
|
|
ri.conn = nil
|
2020-03-18 08:30:02 +00:00
|
|
|
}
|
2022-05-27 18:03:32 +00:00
|
|
|
if ri.isBlockEncrypted() {
|
|
|
|
ri.blockEncryption.Destroy()
|
2024-09-25 09:36:32 +00:00
|
|
|
ri.blockEncryption = nil
|
2020-12-03 08:25:52 +00:00
|
|
|
}
|
2022-05-27 19:55:58 +00:00
|
|
|
if ri.isFileEncrypted() {
|
|
|
|
ri.fileEncryption.Destroy()
|
2024-09-25 09:36:32 +00:00
|
|
|
ri.fileEncryption = nil
|
2022-05-27 19:55:58 +00:00
|
|
|
}
|
2020-03-18 08:30:02 +00:00
|
|
|
}
|
|
|
|
|
2020-06-01 13:57:51 +00:00
|
|
|
// String returns the image-spec (pool/{namespace/}image) format of the image.
|
2021-03-12 12:37:15 +00:00
|
|
|
func (ri *rbdImage) String() string {
|
|
|
|
if ri.RadosNamespace != "" {
|
|
|
|
return fmt.Sprintf("%s/%s/%s", ri.Pool, ri.RadosNamespace, ri.RbdImageName)
|
2020-06-01 13:57:51 +00:00
|
|
|
}
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2021-03-12 12:37:15 +00:00
|
|
|
return fmt.Sprintf("%s/%s", ri.Pool, ri.RbdImageName)
|
2020-05-28 18:39:44 +00:00
|
|
|
}
|
|
|
|
|
2024-07-26 08:01:02 +00:00
|
|
|
func (ri *rbdImage) GetPoolName() string {
|
|
|
|
return ri.Pool
|
|
|
|
}
|
|
|
|
|
2020-06-01 13:57:51 +00:00
|
|
|
// String returns the snap-spec (pool/{namespace/}image@snap) format of the snapshot.
|
2020-05-28 18:39:44 +00:00
|
|
|
func (rs *rbdSnapshot) String() string {
|
2020-06-01 13:57:51 +00:00
|
|
|
if rs.RadosNamespace != "" {
|
|
|
|
return fmt.Sprintf("%s/%s/%s@%s", rs.Pool, rs.RadosNamespace, rs.RbdImageName, rs.RbdSnapName)
|
|
|
|
}
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2020-05-28 18:39:44 +00:00
|
|
|
return fmt.Sprintf("%s/%s@%s", rs.Pool, rs.RbdImageName, rs.RbdSnapName)
|
|
|
|
}
|
|
|
|
|
2019-04-22 21:35:39 +00:00
|
|
|
// createImage creates a new ceph image with provision and volume options.
|
2020-02-26 09:35:18 +00:00
|
|
|
func createImage(ctx context.Context, pOpts *rbdVolume, cr *util.Credentials) error {
|
|
|
|
volSzMiB := fmt.Sprintf("%dM", util.RoundOffVolSize(pOpts.VolSize))
|
2020-01-07 13:45:52 +00:00
|
|
|
|
2022-05-24 03:38:42 +00:00
|
|
|
log.DebugLog(ctx, "rbd: create %s size %s (features: %s) using mon %s",
|
2021-12-20 15:14:28 +00:00
|
|
|
pOpts, volSzMiB, pOpts.ImageFeatureSet.Names(), pOpts.Monitors)
|
2019-09-11 07:08:55 +00:00
|
|
|
|
2024-09-11 16:11:03 +00:00
|
|
|
options, err := pOpts.constructImageOptions(ctx)
|
2022-05-24 03:38:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2020-01-07 13:45:52 +00:00
|
|
|
}
|
2024-09-11 16:11:03 +00:00
|
|
|
defer options.Destroy()
|
2019-09-10 09:56:08 +00:00
|
|
|
|
2022-05-24 03:38:42 +00:00
|
|
|
err = pOpts.Connect(cr)
|
2020-03-18 08:30:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-06-03 07:37:44 +00:00
|
|
|
err = pOpts.openIoctx()
|
2020-01-07 13:45:52 +00:00
|
|
|
if err != nil {
|
2020-06-25 11:30:04 +00:00
|
|
|
return fmt.Errorf("failed to get IOContext: %w", err)
|
2019-09-10 09:56:08 +00:00
|
|
|
}
|
2018-01-09 18:59:50 +00:00
|
|
|
|
2020-06-03 07:37:44 +00:00
|
|
|
err = librbd.CreateImage(pOpts.ioctx, pOpts.RbdImageName,
|
2020-01-17 15:44:06 +00:00
|
|
|
uint64(util.RoundOffVolSize(pOpts.VolSize)*helpers.MiB), options)
|
2018-01-09 18:59:50 +00:00
|
|
|
if err != nil {
|
2020-06-25 11:30:04 +00:00
|
|
|
return fmt.Errorf("failed to create rbd image: %w", err)
|
2018-01-09 18:59:50 +00:00
|
|
|
}
|
|
|
|
|
2022-05-27 18:03:32 +00:00
|
|
|
if pOpts.isBlockEncrypted() {
|
|
|
|
err = pOpts.setupBlockEncryption(ctx)
|
2021-04-01 11:18:28 +00:00
|
|
|
if err != nil {
|
2021-07-10 10:45:11 +00:00
|
|
|
return fmt.Errorf("failed to setup encryption for image %s: %w", pOpts, err)
|
2021-04-01 11:18:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-09 18:59:50 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-03-12 12:37:15 +00:00
|
|
|
func (ri *rbdImage) openIoctx() error {
|
|
|
|
if ri.ioctx != nil {
|
2020-06-03 07:37:44 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-09-16 16:24:58 +00:00
|
|
|
if ri.conn == nil {
|
|
|
|
return fmt.Errorf("can not get IOContext of unconnected image %q", ri)
|
|
|
|
}
|
|
|
|
|
2021-03-12 12:37:15 +00:00
|
|
|
ioctx, err := ri.conn.GetIoctx(ri.Pool)
|
2020-06-03 07:37:44 +00:00
|
|
|
if err != nil {
|
|
|
|
// GetIoctx() can return util.ErrPoolNotFound
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-03-12 12:37:15 +00:00
|
|
|
ioctx.SetNamespace(ri.RadosNamespace)
|
|
|
|
ri.ioctx = ioctx
|
2020-06-03 07:37:44 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-06-24 06:46:47 +00:00
|
|
|
// getImageID queries rbd about the given image and stores its id, returns
|
2020-07-19 12:21:03 +00:00
|
|
|
// ErrImageNotFound if provided image is not found.
|
2022-01-12 13:51:31 +00:00
|
|
|
func (ri *rbdImage) getImageID() error {
|
|
|
|
if ri.ImageID != "" {
|
2020-06-24 06:46:47 +00:00
|
|
|
return nil
|
|
|
|
}
|
2022-01-12 13:51:31 +00:00
|
|
|
image, err := ri.open()
|
2020-06-24 06:46:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer image.Close()
|
|
|
|
|
|
|
|
id, err := image.GetId()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-01-12 13:51:31 +00:00
|
|
|
ri.ImageID = id
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2020-06-24 06:46:47 +00:00
|
|
|
return nil
|
|
|
|
}
|
2020-06-24 06:56:39 +00:00
|
|
|
|
2021-03-12 12:37:15 +00:00
|
|
|
// open the rbdImage after it has been connected.
|
2020-05-08 14:10:18 +00:00
|
|
|
// ErrPoolNotFound or ErrImageNotFound are returned in case the pool or image
|
|
|
|
// can not be found, other errors will contain more details about other issues
|
|
|
|
// (permission denied, ...) and are expected to relate to configuration issues.
|
2021-03-12 12:37:15 +00:00
|
|
|
func (ri *rbdImage) open() (*librbd.Image, error) {
|
|
|
|
err := ri.openIoctx()
|
2020-05-08 14:10:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-03-12 12:37:15 +00:00
|
|
|
image, err := librbd.OpenImage(ri.ioctx, ri.RbdImageName, librbd.NoSnapshot)
|
2020-05-08 14:10:18 +00:00
|
|
|
if err != nil {
|
2020-07-02 21:43:40 +00:00
|
|
|
if errors.Is(err, librbd.ErrNotFound) {
|
2024-05-07 22:51:05 +00:00
|
|
|
err = fmt.Errorf("Failed as %w (internal %w)", ErrImageNotFound, err)
|
2020-05-08 14:10:18 +00:00
|
|
|
}
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2020-05-08 14:10:18 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2020-05-08 14:10:18 +00:00
|
|
|
return image, nil
|
|
|
|
}
|
|
|
|
|
2020-07-22 13:33:36 +00:00
|
|
|
// isInUse checks if there is a watcher on the image. It returns true if there
|
|
|
|
// is a watcher on the image, otherwise returns false.
|
2022-09-05 05:11:37 +00:00
|
|
|
// In case of mirroring, the image should be primary to check watchers if the
|
|
|
|
// image is secondary it returns an error.
|
|
|
|
// isInUse is called with exponential backoff to check the image is used by
|
|
|
|
// anyone else the returned bool value is discarded if its a RWX access.
|
2022-01-12 13:51:31 +00:00
|
|
|
func (ri *rbdImage) isInUse() (bool, error) {
|
|
|
|
image, err := ri.open()
|
2020-07-22 13:33:36 +00:00
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, ErrImageNotFound) || errors.Is(err, util.ErrPoolNotFound) {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
// any error should assume something else is using the image
|
|
|
|
return true, err
|
|
|
|
}
|
|
|
|
defer image.Close()
|
|
|
|
|
|
|
|
watchers, err := image.ListWatchers()
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
2021-10-07 04:57:17 +00:00
|
|
|
mirrorInfo, err := image.GetMirrorImageInfo()
|
2020-10-21 05:34:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
2022-09-05 05:11:37 +00:00
|
|
|
|
|
|
|
if mirrorInfo.State == librbd.MirrorImageEnabled && !mirrorInfo.Primary {
|
|
|
|
// Mapping secondary image can cause issues.returning error as the
|
|
|
|
// bool value is discarded if it its RWX access.
|
|
|
|
return false, fmt.Errorf("cannot map image %s it is not primary", ri)
|
|
|
|
}
|
2021-10-07 04:57:17 +00:00
|
|
|
|
2020-07-22 13:33:36 +00:00
|
|
|
// because we opened the image, there is at least one watcher
|
2020-10-21 05:34:03 +00:00
|
|
|
defaultWatchers := 1
|
2022-09-05 05:11:37 +00:00
|
|
|
if mirrorInfo.Primary {
|
2024-04-18 12:02:29 +00:00
|
|
|
count, err := util.GetRBDMirrorDaemonCount(util.CsiConfigFile, ri.ClusterID)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
2021-04-19 11:12:46 +00:00
|
|
|
// if rbd mirror daemon is running, a watcher will be added by the rbd
|
|
|
|
// mirror daemon for mirrored images.
|
2024-04-18 12:02:29 +00:00
|
|
|
defaultWatchers += count
|
2020-10-21 05:34:03 +00:00
|
|
|
}
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2021-04-19 11:12:46 +00:00
|
|
|
return len(watchers) > defaultWatchers, nil
|
2020-07-22 13:33:36 +00:00
|
|
|
}
|
|
|
|
|
2022-02-17 06:24:47 +00:00
|
|
|
// checkValidImageFeatures check presence of imageFeatures parameter. It returns false when
|
|
|
|
// there imageFeatures is present and empty.
|
|
|
|
func checkValidImageFeatures(imageFeatures string, ok bool) bool {
|
|
|
|
return !(ok && imageFeatures == "")
|
2021-06-07 07:31:38 +00:00
|
|
|
}
|
|
|
|
|
2021-06-15 12:25:29 +00:00
|
|
|
// isNotMountPoint checks whether MountPoint does not exists and
|
|
|
|
// also discards error indicating mountPoint exists.
|
|
|
|
func isNotMountPoint(mounter mount.Interface, stagingTargetPath string) (bool, error) {
|
2022-08-02 07:50:18 +00:00
|
|
|
isMnt, err := mounter.IsMountPoint(stagingTargetPath)
|
2021-06-15 12:25:29 +00:00
|
|
|
if os.IsNotExist(err) {
|
|
|
|
err = nil
|
|
|
|
}
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2022-08-02 07:50:18 +00:00
|
|
|
return !isMnt, err
|
2021-06-15 12:25:29 +00:00
|
|
|
}
|
|
|
|
|
2021-11-09 11:41:17 +00:00
|
|
|
// isCephMgrSupported determines if the cluster has support for MGR based operation
|
|
|
|
// depending on the error.
|
|
|
|
func isCephMgrSupported(ctx context.Context, clusterID string, err error) bool {
|
|
|
|
switch {
|
|
|
|
case err == nil:
|
|
|
|
return true
|
|
|
|
case strings.Contains(err.Error(), rbdTaskRemoveCmdInvalidString):
|
|
|
|
log.WarningLog(
|
|
|
|
ctx,
|
|
|
|
"cluster with cluster ID (%s) does not support Ceph manager based rbd commands"+
|
|
|
|
"(minimum ceph version required is v14.2.3)",
|
|
|
|
clusterID)
|
|
|
|
|
|
|
|
return false
|
|
|
|
case strings.Contains(err.Error(), rbdTaskRemoveCmdAccessDeniedMessage):
|
|
|
|
log.WarningLog(ctx, "access denied to Ceph MGR-based rbd commands on cluster ID (%s)", clusterID)
|
|
|
|
|
|
|
|
return false
|
2021-06-15 06:09:01 +00:00
|
|
|
}
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2021-11-09 11:41:17 +00:00
|
|
|
return true
|
2019-08-06 16:59:40 +00:00
|
|
|
}
|
|
|
|
|
2021-08-23 11:34:29 +00:00
|
|
|
// ensureImageCleanup finds image in trash and if found removes it
|
|
|
|
// from trash.
|
2022-01-12 13:51:31 +00:00
|
|
|
func (ri *rbdImage) ensureImageCleanup(ctx context.Context) error {
|
2024-10-01 13:10:27 +00:00
|
|
|
err := ri.openIoctx()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-01-12 13:51:31 +00:00
|
|
|
trashInfoList, err := librbd.GetTrashList(ri.ioctx)
|
2021-08-23 11:34:29 +00:00
|
|
|
if err != nil {
|
|
|
|
log.ErrorLog(ctx, "failed to list images in trash: %v", err)
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, val := range trashInfoList {
|
2022-01-12 13:51:31 +00:00
|
|
|
if val.Name == ri.RbdImageName {
|
|
|
|
ri.ImageID = val.Id
|
2021-08-23 11:34:29 +00:00
|
|
|
|
2022-01-12 13:51:31 +00:00
|
|
|
return ri.trashRemoveImage(ctx)
|
2021-08-23 11:34:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-07-05 06:46:09 +00:00
|
|
|
// Delete deletes a ceph image with provision and volume options.
|
|
|
|
func (ri *rbdImage) Delete(ctx context.Context) error {
|
2022-01-12 13:51:31 +00:00
|
|
|
image := ri.RbdImageName
|
2021-03-15 14:25:01 +00:00
|
|
|
|
2022-01-12 13:51:31 +00:00
|
|
|
log.DebugLog(ctx, "rbd: delete %s using mon %s, pool %s", image, ri.Monitors, ri.Pool)
|
2021-03-15 14:25:01 +00:00
|
|
|
|
2020-06-24 06:56:39 +00:00
|
|
|
// Support deleting the older rbd images whose imageID is not stored in omap
|
2022-01-12 13:51:31 +00:00
|
|
|
err := ri.getImageID()
|
2020-06-24 06:56:39 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-05-27 18:03:32 +00:00
|
|
|
if ri.isBlockEncrypted() {
|
2022-05-27 19:55:58 +00:00
|
|
|
log.DebugLog(ctx, "rbd: going to remove DEK for %q (block encryption)", ri)
|
2024-03-04 15:13:31 +00:00
|
|
|
if err = ri.blockEncryption.RemoveDEK(ctx, ri.VolID); err != nil {
|
2022-05-27 19:55:58 +00:00
|
|
|
log.WarningLog(ctx, "failed to clean the passphrase for volume %s (block encryption): %s", ri.VolID, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ri.isFileEncrypted() {
|
|
|
|
log.DebugLog(ctx, "rbd: going to remove DEK for %q (file encryption)", ri)
|
2024-03-04 15:13:31 +00:00
|
|
|
if err = ri.fileEncryption.RemoveDEK(ctx, ri.VolID); err != nil {
|
2022-05-27 19:55:58 +00:00
|
|
|
log.WarningLog(ctx, "failed to clean the passphrase for volume %s (file encryption): %s", ri.VolID, err)
|
2021-03-24 08:54:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-12 13:51:31 +00:00
|
|
|
err = ri.openIoctx()
|
2020-06-24 06:56:39 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2018-01-09 18:59:50 +00:00
|
|
|
}
|
2019-04-22 21:35:39 +00:00
|
|
|
|
2022-01-12 13:51:31 +00:00
|
|
|
rbdImage := librbd.GetImage(ri.ioctx, image)
|
2020-06-24 06:56:39 +00:00
|
|
|
err = rbdImage.Trash(0)
|
|
|
|
if err != nil {
|
2022-01-12 13:51:31 +00:00
|
|
|
log.ErrorLog(ctx, "failed to delete rbd image: %s, error: %v", ri, err)
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2020-06-24 06:56:39 +00:00
|
|
|
return err
|
|
|
|
}
|
2019-08-06 16:59:40 +00:00
|
|
|
|
2022-01-12 13:51:31 +00:00
|
|
|
return ri.trashRemoveImage(ctx)
|
2021-08-23 11:34:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// trashRemoveImage adds a task to trash remove an image using ceph manager if supported,
|
|
|
|
// otherwise removes the image from trash.
|
2022-01-12 13:51:31 +00:00
|
|
|
func (ri *rbdImage) trashRemoveImage(ctx context.Context) error {
|
2019-08-06 16:59:40 +00:00
|
|
|
// attempt to use Ceph manager based deletion support if available
|
2022-01-12 13:51:31 +00:00
|
|
|
log.DebugLog(ctx, "rbd: adding task to remove image %q with id %q from trash", ri, ri.ImageID)
|
2021-07-27 06:43:39 +00:00
|
|
|
|
2022-01-12 13:51:31 +00:00
|
|
|
ta, err := ri.conn.GetTaskAdmin()
|
2021-11-09 11:41:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2020-06-24 06:56:39 +00:00
|
|
|
}
|
2021-11-09 11:41:17 +00:00
|
|
|
|
2022-01-12 13:51:31 +00:00
|
|
|
_, err = ta.AddTrashRemove(admin.NewImageSpec(ri.Pool, ri.RadosNamespace, ri.ImageID))
|
2021-11-09 11:41:17 +00:00
|
|
|
|
2022-01-12 13:51:31 +00:00
|
|
|
rbdCephMgrSupported := isCephMgrSupported(ctx, ri.ClusterID, err)
|
2020-03-16 04:59:16 +00:00
|
|
|
if rbdCephMgrSupported && err != nil {
|
2022-01-12 13:51:31 +00:00
|
|
|
log.ErrorLog(ctx, "failed to add task to delete rbd image: %s, %v", ri, err)
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2020-03-16 04:59:16 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-08-06 16:59:40 +00:00
|
|
|
if !rbdCephMgrSupported {
|
2022-01-12 13:51:31 +00:00
|
|
|
err = librbd.TrashRemove(ri.ioctx, ri.ImageID, true)
|
2020-03-16 09:26:43 +00:00
|
|
|
if err != nil {
|
2022-01-12 13:51:31 +00:00
|
|
|
log.ErrorLog(ctx, "failed to delete rbd image: %s, %v", ri, err)
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2020-03-16 09:26:43 +00:00
|
|
|
return err
|
|
|
|
}
|
2021-11-09 11:41:17 +00:00
|
|
|
} else {
|
2022-01-12 13:51:31 +00:00
|
|
|
log.DebugLog(ctx, "rbd: successfully added task to move image %q with id %q to trash", ri, ri.ImageID)
|
2020-06-24 06:56:39 +00:00
|
|
|
}
|
2020-03-16 09:26:43 +00:00
|
|
|
|
2020-06-24 06:56:39 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-06-06 11:44:08 +00:00
|
|
|
// DeleteTempImage deletes the temporary image created for volume datasource.
|
|
|
|
func (rv *rbdVolume) DeleteTempImage(ctx context.Context) error {
|
|
|
|
tempClone := rv.generateTempClone()
|
2024-07-05 06:46:09 +00:00
|
|
|
err := tempClone.Delete(ctx)
|
2024-06-06 11:44:08 +00:00
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, ErrImageNotFound) {
|
|
|
|
return tempClone.ensureImageCleanup(ctx)
|
|
|
|
} else {
|
|
|
|
// return error if it is not ErrImageNotFound
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-01-12 13:51:31 +00:00
|
|
|
func (ri *rbdImage) getCloneDepth(ctx context.Context) (uint, error) {
|
2020-06-24 07:43:24 +00:00
|
|
|
var depth uint
|
2021-03-12 12:37:15 +00:00
|
|
|
vol := rbdVolume{}
|
2021-03-15 09:58:29 +00:00
|
|
|
|
2022-01-12 13:51:31 +00:00
|
|
|
vol.Pool = ri.Pool
|
|
|
|
vol.Monitors = ri.Monitors
|
|
|
|
vol.RbdImageName = ri.RbdImageName
|
2022-07-06 09:03:12 +00:00
|
|
|
vol.RadosNamespace = ri.RadosNamespace
|
2022-01-12 13:51:31 +00:00
|
|
|
vol.conn = ri.conn.Copy()
|
2020-06-24 07:43:24 +00:00
|
|
|
|
|
|
|
for {
|
|
|
|
if vol.RbdImageName == "" {
|
|
|
|
return depth, nil
|
|
|
|
}
|
2021-03-18 05:55:22 +00:00
|
|
|
err := vol.openIoctx()
|
|
|
|
if err != nil {
|
|
|
|
return depth, err
|
|
|
|
}
|
|
|
|
|
2020-06-24 07:43:24 +00:00
|
|
|
err = vol.getImageInfo()
|
2021-03-18 05:55:22 +00:00
|
|
|
// FIXME: create and destroy the vol inside the loop.
|
|
|
|
// see https://github.com/ceph/ceph-csi/pull/1838#discussion_r598530807
|
|
|
|
vol.ioctx.Destroy()
|
|
|
|
vol.ioctx = nil
|
2020-06-24 07:43:24 +00:00
|
|
|
if err != nil {
|
2020-07-14 07:26:55 +00:00
|
|
|
// if the parent image is moved to trash the name will be present
|
|
|
|
// in rbd image info but the image will be in trash, in that case
|
|
|
|
// return the found depth
|
2020-07-10 01:05:42 +00:00
|
|
|
if errors.Is(err, ErrImageNotFound) {
|
2020-07-14 07:26:55 +00:00
|
|
|
return depth, nil
|
|
|
|
}
|
2021-08-24 15:03:25 +00:00
|
|
|
log.ErrorLog(ctx, "failed to check depth on image %s: %s", &vol, err)
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2020-06-24 07:43:24 +00:00
|
|
|
return depth, err
|
|
|
|
}
|
|
|
|
if vol.ParentName != "" {
|
|
|
|
depth++
|
|
|
|
}
|
|
|
|
vol.RbdImageName = vol.ParentName
|
2021-03-18 11:32:10 +00:00
|
|
|
vol.Pool = vol.ParentPool
|
2020-06-24 07:43:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-27 11:09:45 +00:00
|
|
|
type trashSnapInfo struct {
|
|
|
|
origSnapName string
|
|
|
|
}
|
|
|
|
|
2021-06-25 11:52:34 +00:00
|
|
|
func flattenClonedRbdImages(
|
|
|
|
ctx context.Context,
|
|
|
|
snaps []librbd.SnapInfo,
|
|
|
|
pool, monitors, rbdImageName string,
|
2022-06-01 10:17:19 +00:00
|
|
|
cr *util.Credentials,
|
|
|
|
) error {
|
2021-03-12 12:37:15 +00:00
|
|
|
rv := &rbdVolume{}
|
|
|
|
rv.Monitors = monitors
|
|
|
|
rv.Pool = pool
|
|
|
|
rv.RbdImageName = rbdImageName
|
|
|
|
|
2024-03-21 14:01:56 +00:00
|
|
|
defer rv.Destroy(ctx)
|
2020-07-01 07:05:07 +00:00
|
|
|
err := rv.Connect(cr)
|
2020-06-24 06:56:39 +00:00
|
|
|
if err != nil {
|
2021-08-24 15:03:25 +00:00
|
|
|
log.ErrorLog(ctx, "failed to open connection %s; err %v", rv, err)
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2020-06-24 06:56:39 +00:00
|
|
|
return err
|
|
|
|
}
|
2020-08-27 11:09:45 +00:00
|
|
|
var origNameList []trashSnapInfo
|
|
|
|
for _, snapInfo := range snaps {
|
|
|
|
// check if the snapshot belongs to trash namespace.
|
|
|
|
isTrash, retErr := rv.isTrashSnap(snapInfo.Id)
|
|
|
|
if retErr != nil {
|
|
|
|
return retErr
|
|
|
|
}
|
|
|
|
|
|
|
|
if isTrash {
|
|
|
|
// get original snap name for the snapshot in trash namespace
|
|
|
|
origSnapName, retErr := rv.getOrigSnapName(snapInfo.Id)
|
|
|
|
if retErr != nil {
|
|
|
|
return retErr
|
2020-07-01 07:05:07 +00:00
|
|
|
}
|
2020-08-27 11:09:45 +00:00
|
|
|
origNameList = append(origNameList, trashSnapInfo{origSnapName})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, snapName := range origNameList {
|
|
|
|
rv.RbdImageName = snapName.origSnapName
|
2021-11-10 09:38:32 +00:00
|
|
|
err = rv.flattenRbdImage(ctx, true, rbdHardMaxCloneDepth, rbdSoftMaxCloneDepth)
|
2020-08-27 11:09:45 +00:00
|
|
|
if err != nil {
|
2021-08-24 15:03:25 +00:00
|
|
|
log.ErrorLog(ctx, "failed to flatten %s; err %v", rv, err)
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2020-08-27 11:09:45 +00:00
|
|
|
continue
|
2020-07-01 07:05:07 +00:00
|
|
|
}
|
|
|
|
}
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2020-07-01 07:05:07 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-01-12 13:51:31 +00:00
|
|
|
func (ri *rbdImage) flattenRbdImage(
|
2021-06-25 11:52:34 +00:00
|
|
|
ctx context.Context,
|
|
|
|
forceFlatten bool,
|
2022-06-01 10:17:19 +00:00
|
|
|
hardlimit, softlimit uint,
|
|
|
|
) error {
|
2020-07-01 07:05:07 +00:00
|
|
|
var depth uint
|
|
|
|
var err error
|
|
|
|
|
|
|
|
// skip clone depth check if request is for force flatten
|
|
|
|
if !forceFlatten {
|
2022-01-12 13:51:31 +00:00
|
|
|
depth, err = ri.getCloneDepth(ctx)
|
2020-07-01 07:05:07 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-08-24 15:03:25 +00:00
|
|
|
log.ExtendedLog(
|
2021-06-25 11:52:34 +00:00
|
|
|
ctx,
|
|
|
|
"clone depth is (%d), configured softlimit (%d) and hardlimit (%d) for %s",
|
|
|
|
depth,
|
|
|
|
softlimit,
|
|
|
|
hardlimit,
|
2022-01-12 13:51:31 +00:00
|
|
|
ri)
|
2020-07-01 07:05:07 +00:00
|
|
|
}
|
2020-06-24 07:43:24 +00:00
|
|
|
|
2021-04-05 04:40:00 +00:00
|
|
|
if !forceFlatten && (depth < hardlimit) && (depth < softlimit) {
|
|
|
|
return nil
|
|
|
|
}
|
2021-11-09 11:41:17 +00:00
|
|
|
|
2022-01-12 13:51:31 +00:00
|
|
|
log.DebugLog(ctx, "rbd: adding task to flatten image %q", ri)
|
2021-11-16 10:36:18 +00:00
|
|
|
|
2022-01-12 13:51:31 +00:00
|
|
|
ta, err := ri.conn.GetTaskAdmin()
|
2021-11-09 11:41:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-01-12 13:51:31 +00:00
|
|
|
_, err = ta.AddFlatten(admin.NewImageSpec(ri.Pool, ri.RadosNamespace, ri.RbdImageName))
|
|
|
|
rbdCephMgrSupported := isCephMgrSupported(ctx, ri.ClusterID, err)
|
2021-11-09 11:41:17 +00:00
|
|
|
if rbdCephMgrSupported {
|
2021-04-05 04:40:00 +00:00
|
|
|
if err != nil {
|
|
|
|
// discard flattening error if the image does not have any parent
|
2022-01-12 13:51:31 +00:00
|
|
|
rbdFlattenNoParent := fmt.Sprintf("Image %s/%s does not have a parent", ri.Pool, ri.RbdImageName)
|
2021-04-05 04:40:00 +00:00
|
|
|
if strings.Contains(err.Error(), rbdFlattenNoParent) {
|
|
|
|
return nil
|
|
|
|
}
|
2022-01-12 13:51:31 +00:00
|
|
|
log.ErrorLog(ctx, "failed to add task flatten for %s : %v", ri, err)
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2021-04-05 04:40:00 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if forceFlatten || depth >= hardlimit {
|
2022-01-12 13:51:31 +00:00
|
|
|
return fmt.Errorf("%w: flatten is in progress for image %s", ErrFlattenInProgress, ri.RbdImageName)
|
2021-04-05 04:40:00 +00:00
|
|
|
}
|
2022-01-12 13:51:31 +00:00
|
|
|
log.DebugLog(ctx, "successfully added task to flatten image %q", ri)
|
2021-04-05 04:40:00 +00:00
|
|
|
}
|
2021-11-09 11:41:17 +00:00
|
|
|
if !rbdCephMgrSupported {
|
2021-08-24 15:03:25 +00:00
|
|
|
log.ErrorLog(
|
2021-06-25 11:52:34 +00:00
|
|
|
ctx,
|
|
|
|
"task manager does not support flatten,image will be flattened once hardlimit is reached: %v",
|
|
|
|
err)
|
2021-04-05 04:40:00 +00:00
|
|
|
if forceFlatten || depth >= hardlimit {
|
2022-01-12 13:51:31 +00:00
|
|
|
err := ri.flatten()
|
2021-04-05 04:40:00 +00:00
|
|
|
if err != nil {
|
2022-01-12 13:51:31 +00:00
|
|
|
log.ErrorLog(ctx, "rbd failed to flatten image %s %s: %v", ri.Pool, ri.RbdImageName, err)
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2021-04-05 04:40:00 +00:00
|
|
|
return err
|
2020-06-24 07:43:24 +00:00
|
|
|
}
|
2020-03-16 04:59:16 +00:00
|
|
|
}
|
2019-04-22 21:35:39 +00:00
|
|
|
}
|
2021-04-05 04:40:00 +00:00
|
|
|
|
2020-06-24 06:56:39 +00:00
|
|
|
return nil
|
2019-04-22 21:35:39 +00:00
|
|
|
}
|
|
|
|
|
2022-01-12 13:51:31 +00:00
|
|
|
func (ri *rbdImage) getParentName() (string, error) {
|
|
|
|
rbdImage, err := ri.open()
|
2020-08-04 08:42:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
defer rbdImage.Close()
|
|
|
|
|
2020-11-02 11:55:41 +00:00
|
|
|
parentInfo, err := rbdImage.GetParent()
|
2020-08-04 08:42:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2020-11-02 11:55:41 +00:00
|
|
|
return parentInfo.Image.ImageName, nil
|
2020-08-04 08:42:16 +00:00
|
|
|
}
|
|
|
|
|
2022-01-12 13:51:31 +00:00
|
|
|
func (ri *rbdImage) flatten() error {
|
|
|
|
rbdImage, err := ri.open()
|
2020-08-04 08:42:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer rbdImage.Close()
|
|
|
|
|
|
|
|
err = rbdImage.Flatten()
|
|
|
|
if err != nil {
|
|
|
|
// rbd image flatten will fail if the rbd image does not have a parent
|
2022-01-12 13:51:31 +00:00
|
|
|
parent, pErr := ri.getParentName()
|
2020-08-04 08:42:16 +00:00
|
|
|
if pErr != nil {
|
2024-05-07 22:51:05 +00:00
|
|
|
return fmt.Errorf("Failed as %w (internal %w)", err, pErr)
|
2020-08-04 08:42:16 +00:00
|
|
|
}
|
|
|
|
if parent == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2020-08-04 08:42:16 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-01-12 13:51:31 +00:00
|
|
|
func (ri *rbdImage) hasFeature(feature uint64) bool {
|
|
|
|
return (uint64(ri.ImageFeatureSet) & feature) == feature
|
2020-06-24 07:43:24 +00:00
|
|
|
}
|
|
|
|
|
2022-01-12 13:51:31 +00:00
|
|
|
func (ri *rbdImage) checkImageChainHasFeature(ctx context.Context, feature uint64) (bool, error) {
|
|
|
|
rbdImg := rbdImage{}
|
2021-03-15 09:58:29 +00:00
|
|
|
|
2022-01-12 13:51:31 +00:00
|
|
|
rbdImg.Pool = ri.Pool
|
|
|
|
rbdImg.RadosNamespace = ri.RadosNamespace
|
|
|
|
rbdImg.Monitors = ri.Monitors
|
|
|
|
rbdImg.RbdImageName = ri.RbdImageName
|
|
|
|
rbdImg.conn = ri.conn.Copy()
|
2021-03-12 12:37:15 +00:00
|
|
|
|
2020-06-24 07:43:24 +00:00
|
|
|
for {
|
2022-01-12 13:51:31 +00:00
|
|
|
if rbdImg.RbdImageName == "" {
|
2020-06-24 07:43:24 +00:00
|
|
|
return false, nil
|
|
|
|
}
|
2022-01-12 13:51:31 +00:00
|
|
|
err := rbdImg.openIoctx()
|
2021-03-18 05:55:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
2022-01-12 13:51:31 +00:00
|
|
|
err = rbdImg.getImageInfo()
|
2021-03-18 05:55:22 +00:00
|
|
|
// FIXME: create and destroy the vol inside the loop.
|
|
|
|
// see https://github.com/ceph/ceph-csi/pull/1838#discussion_r598530807
|
2022-01-12 13:51:31 +00:00
|
|
|
rbdImg.ioctx.Destroy()
|
|
|
|
rbdImg.ioctx = nil
|
2020-06-24 07:43:24 +00:00
|
|
|
if err != nil {
|
2021-05-03 11:00:05 +00:00
|
|
|
// call to getImageInfo returns the parent name even if the parent
|
|
|
|
// is in the trash, when we try to open the parent image to get its
|
|
|
|
// information it fails because it is already in trash. We should
|
|
|
|
// treat error as nil if the parent is not found.
|
|
|
|
if errors.Is(err, ErrImageNotFound) {
|
|
|
|
return false, nil
|
|
|
|
}
|
2022-01-12 13:51:31 +00:00
|
|
|
log.ErrorLog(ctx, "failed to get image info for %s: %s", rbdImg.String(), err)
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2020-06-24 07:43:24 +00:00
|
|
|
return false, err
|
|
|
|
}
|
2022-01-12 13:51:31 +00:00
|
|
|
if f := rbdImg.hasFeature(feature); f {
|
2020-06-24 07:43:24 +00:00
|
|
|
return true, nil
|
|
|
|
}
|
2022-01-12 13:51:31 +00:00
|
|
|
rbdImg.RbdImageName = rbdImg.ParentName
|
|
|
|
rbdImg.Pool = rbdImg.ParentPool
|
2020-06-24 07:43:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-22 21:35:39 +00:00
|
|
|
// genSnapFromSnapID generates a rbdSnapshot structure from the provided identifier, updating
|
2020-07-19 12:21:03 +00:00
|
|
|
// the structure with elements from on-disk snapshot metadata as well.
|
2024-03-21 09:48:50 +00:00
|
|
|
//
|
|
|
|
// NOTE: The returned rbdSnapshot can be non-nil in case of an error. That
|
|
|
|
// seems to be required for the DeleteSnapshot procedure, so that OMAP
|
|
|
|
// attributes can be cleaned-up.
|
2021-06-25 11:52:34 +00:00
|
|
|
func genSnapFromSnapID(
|
|
|
|
ctx context.Context,
|
|
|
|
snapshotID string,
|
|
|
|
cr *util.Credentials,
|
2022-06-01 10:17:19 +00:00
|
|
|
secrets map[string]string,
|
2024-03-21 09:48:50 +00:00
|
|
|
) (*rbdSnapshot, error) {
|
2021-09-07 06:05:11 +00:00
|
|
|
var vi util.CSIIdentifier
|
2019-04-22 21:35:39 +00:00
|
|
|
|
2024-03-21 09:48:50 +00:00
|
|
|
err := vi.DecomposeCSIID(snapshotID)
|
2019-04-22 21:35:39 +00:00
|
|
|
if err != nil {
|
2024-03-21 09:48:50 +00:00
|
|
|
log.ErrorLog(ctx, "error decoding snapshot ID (%s) (%s)", err, snapshotID)
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2024-03-21 09:48:50 +00:00
|
|
|
return nil, err
|
2019-04-22 21:35:39 +00:00
|
|
|
}
|
|
|
|
|
2024-03-21 09:48:50 +00:00
|
|
|
rbdSnap := &rbdSnapshot{}
|
|
|
|
rbdSnap.VolID = snapshotID
|
2019-04-22 21:35:39 +00:00
|
|
|
rbdSnap.ClusterID = vi.ClusterID
|
|
|
|
|
2021-09-07 06:05:11 +00:00
|
|
|
rbdSnap.Monitors, _, err = util.GetMonsAndClusterID(ctx, rbdSnap.ClusterID, false)
|
2019-04-22 21:35:39 +00:00
|
|
|
if err != nil {
|
2021-08-24 15:03:25 +00:00
|
|
|
log.ErrorLog(ctx, "failed getting mons (%s)", err)
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2024-03-21 09:48:50 +00:00
|
|
|
return nil, err
|
2019-04-22 21:35:39 +00:00
|
|
|
}
|
|
|
|
|
2020-05-14 13:28:14 +00:00
|
|
|
rbdSnap.Pool, err = util.GetPoolName(rbdSnap.Monitors, cr, vi.LocationID)
|
2019-04-22 21:35:39 +00:00
|
|
|
if err != nil {
|
2024-03-21 09:48:50 +00:00
|
|
|
return nil, err
|
2019-04-22 21:35:39 +00:00
|
|
|
}
|
2020-01-24 16:26:56 +00:00
|
|
|
rbdSnap.JournalPool = rbdSnap.Pool
|
2019-04-22 21:35:39 +00:00
|
|
|
|
2024-06-05 05:34:58 +00:00
|
|
|
rbdSnap.RadosNamespace, err = util.GetRBDRadosNamespace(util.CsiConfigFile, rbdSnap.ClusterID)
|
2020-06-01 13:57:51 +00:00
|
|
|
if err != nil {
|
2024-03-21 09:48:50 +00:00
|
|
|
return nil, err
|
2020-06-01 13:57:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
j, err := snapJournal.Connect(rbdSnap.Monitors, rbdSnap.RadosNamespace, cr)
|
2020-05-12 21:05:55 +00:00
|
|
|
if err != nil {
|
2024-03-21 09:48:50 +00:00
|
|
|
return nil, err
|
2020-05-12 21:05:55 +00:00
|
|
|
}
|
|
|
|
defer j.Destroy()
|
|
|
|
|
|
|
|
imageAttributes, err := j.GetImageAttributes(
|
|
|
|
ctx, rbdSnap.Pool, vi.ObjectUUID, true)
|
2019-04-22 21:35:39 +00:00
|
|
|
if err != nil {
|
2024-03-21 09:48:50 +00:00
|
|
|
return rbdSnap, err
|
2019-04-22 21:35:39 +00:00
|
|
|
}
|
2020-07-06 06:22:34 +00:00
|
|
|
rbdSnap.ImageID = imageAttributes.ImageID
|
2020-01-24 16:26:56 +00:00
|
|
|
rbdSnap.RequestName = imageAttributes.RequestName
|
|
|
|
rbdSnap.RbdImageName = imageAttributes.SourceName
|
|
|
|
rbdSnap.RbdSnapName = imageAttributes.ImageName
|
2020-06-24 07:43:24 +00:00
|
|
|
rbdSnap.ReservedID = vi.ObjectUUID
|
2021-03-30 20:08:24 +00:00
|
|
|
rbdSnap.Owner = imageAttributes.Owner
|
2020-01-24 16:26:56 +00:00
|
|
|
// convert the journal pool ID to name, for use in DeleteSnapshot cases
|
|
|
|
if imageAttributes.JournalPoolID != util.InvalidPoolID {
|
2020-05-14 13:28:14 +00:00
|
|
|
rbdSnap.JournalPool, err = util.GetPoolName(rbdSnap.Monitors, cr, imageAttributes.JournalPoolID)
|
2020-01-24 16:26:56 +00:00
|
|
|
if err != nil {
|
|
|
|
// TODO: If pool is not found we may leak the image (as DeleteSnapshot will return success)
|
2024-03-21 09:48:50 +00:00
|
|
|
return rbdSnap, err
|
2020-01-24 16:26:56 +00:00
|
|
|
}
|
|
|
|
}
|
2019-04-22 21:35:39 +00:00
|
|
|
|
2021-03-30 20:08:24 +00:00
|
|
|
err = rbdSnap.Connect(cr)
|
2024-03-21 09:48:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return rbdSnap, fmt.Errorf("failed to connect to %q: %w",
|
|
|
|
rbdSnap, err)
|
|
|
|
}
|
2021-03-30 20:08:24 +00:00
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
2024-03-21 14:01:56 +00:00
|
|
|
rbdSnap.Destroy(ctx)
|
2021-03-30 20:08:24 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2022-08-04 15:52:36 +00:00
|
|
|
if imageAttributes.KmsID != "" && imageAttributes.EncryptionType == util.EncryptionTypeBlock {
|
2022-05-27 18:03:32 +00:00
|
|
|
err = rbdSnap.configureBlockEncryption(imageAttributes.KmsID, secrets)
|
2021-03-30 20:08:24 +00:00
|
|
|
if err != nil {
|
2024-03-21 09:48:50 +00:00
|
|
|
return rbdSnap, fmt.Errorf("failed to configure block encryption for "+
|
2022-08-04 15:52:36 +00:00
|
|
|
"%q: %w", rbdSnap, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if imageAttributes.KmsID != "" && imageAttributes.EncryptionType == util.EncryptionTypeFile {
|
2024-03-04 15:13:31 +00:00
|
|
|
err = rbdSnap.configureFileEncryption(ctx, imageAttributes.KmsID, secrets)
|
2022-08-04 15:52:36 +00:00
|
|
|
if err != nil {
|
2024-03-21 09:48:50 +00:00
|
|
|
return rbdSnap, fmt.Errorf("failed to configure file encryption for "+
|
2021-05-07 05:30:37 +00:00
|
|
|
"%q: %w", rbdSnap, err)
|
2021-03-30 20:08:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-16 12:30:24 +00:00
|
|
|
err = rbdSnap.getImageInfo()
|
2021-12-15 06:05:34 +00:00
|
|
|
if err != nil {
|
2024-03-21 09:48:50 +00:00
|
|
|
return rbdSnap, fmt.Errorf("failed to update snapshot details for %q: %w", rbdSnap, err)
|
2021-12-15 06:05:34 +00:00
|
|
|
}
|
|
|
|
|
2024-03-21 09:48:50 +00:00
|
|
|
return rbdSnap, err
|
2019-04-22 21:35:39 +00:00
|
|
|
}
|
|
|
|
|
rbd:store/Read volumeID in/from PV annotation
In the case of the Async DR, the volumeID will
not be the same if the clusterID or the PoolID
is different, With Earlier implementation, it
is expected that the new volumeID mapping is
stored in the rados omap pool. In the case of the
ControllerExpand or the DeleteVolume Request,
the only volumeID will be sent it's not possible
to find the corresponding poolID in the new cluster.
With This Change, it works as below
The csi-rbdplugin-controller will watch for the PV
objects, when there are any PV objects created it
will check the omap already exists, If the omap doesn't
exist it will generate the new volumeID and it checks for
the volumeID mapping entry in the PV annotation, if the
mapping does not exist, it will add the new entry
to the PV annotation.
The cephcsi will check for the PV annotations if the
omap does not exist if the mapping exists in the PV
annotation, it will use the new volumeID for further
operations.
Signed-off-by: Madhu Rajanna <madhupr007@gmail.com>
2021-03-30 10:33:55 +00:00
|
|
|
// generateVolumeFromVolumeID generates a rbdVolume structure from the provided identifier.
|
2021-06-25 11:52:34 +00:00
|
|
|
func generateVolumeFromVolumeID(
|
|
|
|
ctx context.Context,
|
|
|
|
volumeID string,
|
2021-07-22 10:36:39 +00:00
|
|
|
vi util.CSIIdentifier,
|
2021-06-25 11:52:34 +00:00
|
|
|
cr *util.Credentials,
|
2022-06-01 10:17:19 +00:00
|
|
|
secrets map[string]string,
|
|
|
|
) (*rbdVolume, error) {
|
2019-04-22 21:35:39 +00:00
|
|
|
var (
|
2021-09-07 06:05:11 +00:00
|
|
|
rbdVol *rbdVolume
|
|
|
|
err error
|
2019-04-22 21:35:39 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// rbdVolume fields that are not filled up in this function are:
|
2020-06-24 07:43:24 +00:00
|
|
|
// Mounter, MultiNodeWritable
|
2021-03-12 12:37:15 +00:00
|
|
|
rbdVol = &rbdVolume{}
|
|
|
|
rbdVol.VolID = volumeID
|
2020-10-21 12:49:45 +00:00
|
|
|
|
2019-04-22 21:35:39 +00:00
|
|
|
rbdVol.ClusterID = vi.ClusterID
|
|
|
|
|
2021-09-07 06:05:11 +00:00
|
|
|
rbdVol.Monitors, _, err = util.GetMonsAndClusterID(ctx, rbdVol.ClusterID, false)
|
2019-04-22 21:35:39 +00:00
|
|
|
if err != nil {
|
2021-08-24 15:03:25 +00:00
|
|
|
log.ErrorLog(ctx, "failed getting mons (%s)", err)
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2020-06-24 07:43:24 +00:00
|
|
|
return rbdVol, err
|
2019-04-22 21:35:39 +00:00
|
|
|
}
|
|
|
|
|
2024-06-05 05:34:58 +00:00
|
|
|
rbdVol.RadosNamespace, err = util.GetRBDRadosNamespace(util.CsiConfigFile, rbdVol.ClusterID)
|
2019-04-22 21:35:39 +00:00
|
|
|
if err != nil {
|
2020-10-29 09:48:31 +00:00
|
|
|
return rbdVol, err
|
2020-03-17 13:39:35 +00:00
|
|
|
}
|
|
|
|
|
2020-10-21 12:49:45 +00:00
|
|
|
j, err := volJournal.Connect(rbdVol.Monitors, rbdVol.RadosNamespace, cr)
|
2020-03-17 13:39:35 +00:00
|
|
|
if err != nil {
|
2020-06-24 07:43:24 +00:00
|
|
|
return rbdVol, err
|
2019-04-22 21:35:39 +00:00
|
|
|
}
|
2020-10-21 12:49:45 +00:00
|
|
|
defer j.Destroy()
|
2019-04-22 21:35:39 +00:00
|
|
|
|
2020-10-21 12:49:45 +00:00
|
|
|
rbdVol.Pool, err = util.GetPoolName(rbdVol.Monitors, cr, vi.LocationID)
|
|
|
|
if err != nil {
|
|
|
|
return rbdVol, err
|
2020-06-01 13:57:51 +00:00
|
|
|
}
|
|
|
|
|
2020-10-21 12:49:45 +00:00
|
|
|
err = rbdVol.Connect(cr)
|
2020-05-12 21:05:55 +00:00
|
|
|
if err != nil {
|
2020-06-24 07:43:24 +00:00
|
|
|
return rbdVol, err
|
2020-05-12 21:05:55 +00:00
|
|
|
}
|
2020-10-21 12:49:45 +00:00
|
|
|
rbdVol.JournalPool = rbdVol.Pool
|
2020-05-12 21:05:55 +00:00
|
|
|
|
|
|
|
imageAttributes, err := j.GetImageAttributes(
|
|
|
|
ctx, rbdVol.Pool, vi.ObjectUUID, false)
|
2019-04-22 21:35:39 +00:00
|
|
|
if err != nil {
|
2020-06-24 07:43:24 +00:00
|
|
|
return rbdVol, err
|
2019-04-22 21:35:39 +00:00
|
|
|
}
|
2020-06-24 07:43:24 +00:00
|
|
|
rbdVol.RequestName = imageAttributes.RequestName
|
|
|
|
rbdVol.RbdImageName = imageAttributes.ImageName
|
|
|
|
rbdVol.ReservedID = vi.ObjectUUID
|
2020-07-06 06:22:34 +00:00
|
|
|
rbdVol.ImageID = imageAttributes.ImageID
|
2020-11-25 17:06:53 +00:00
|
|
|
rbdVol.Owner = imageAttributes.Owner
|
2020-06-24 07:43:24 +00:00
|
|
|
|
2022-08-04 15:52:36 +00:00
|
|
|
if imageAttributes.KmsID != "" && imageAttributes.EncryptionType == util.EncryptionTypeBlock {
|
2022-05-27 18:03:32 +00:00
|
|
|
err = rbdVol.configureBlockEncryption(imageAttributes.KmsID, secrets)
|
2020-01-24 16:26:56 +00:00
|
|
|
if err != nil {
|
2020-06-24 07:43:24 +00:00
|
|
|
return rbdVol, err
|
2022-08-04 15:52:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if imageAttributes.KmsID != "" && imageAttributes.EncryptionType == util.EncryptionTypeFile {
|
2024-03-04 15:13:31 +00:00
|
|
|
err = rbdVol.configureFileEncryption(ctx, imageAttributes.KmsID, secrets)
|
2022-08-04 15:52:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return rbdVol, err
|
2020-01-24 16:26:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// convert the journal pool ID to name, for use in DeleteVolume cases
|
|
|
|
if imageAttributes.JournalPoolID >= 0 {
|
2020-05-14 13:28:14 +00:00
|
|
|
rbdVol.JournalPool, err = util.GetPoolName(rbdVol.Monitors, cr, imageAttributes.JournalPoolID)
|
2020-01-29 11:44:45 +00:00
|
|
|
if err != nil {
|
2020-01-24 16:26:56 +00:00
|
|
|
// TODO: If pool is not found we may leak the image (as DeleteVolume will return success)
|
2020-06-24 07:43:24 +00:00
|
|
|
return rbdVol, err
|
2020-01-29 11:44:45 +00:00
|
|
|
}
|
|
|
|
}
|
2019-04-22 21:35:39 +00:00
|
|
|
|
2020-07-06 06:22:34 +00:00
|
|
|
if rbdVol.ImageID == "" {
|
2020-12-04 08:36:05 +00:00
|
|
|
err = rbdVol.storeImageID(ctx, j)
|
2020-06-24 07:43:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return rbdVol, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
err = rbdVol.getImageInfo()
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2020-03-17 13:39:35 +00:00
|
|
|
return rbdVol, err
|
2018-01-09 18:59:50 +00:00
|
|
|
}
|
|
|
|
|
2021-12-09 08:00:52 +00:00
|
|
|
// GenVolFromVolID generates a rbdVolume structure from the provided identifier, updating
|
rbd:store/Read volumeID in/from PV annotation
In the case of the Async DR, the volumeID will
not be the same if the clusterID or the PoolID
is different, With Earlier implementation, it
is expected that the new volumeID mapping is
stored in the rados omap pool. In the case of the
ControllerExpand or the DeleteVolume Request,
the only volumeID will be sent it's not possible
to find the corresponding poolID in the new cluster.
With This Change, it works as below
The csi-rbdplugin-controller will watch for the PV
objects, when there are any PV objects created it
will check the omap already exists, If the omap doesn't
exist it will generate the new volumeID and it checks for
the volumeID mapping entry in the PV annotation, if the
mapping does not exist, it will add the new entry
to the PV annotation.
The cephcsi will check for the PV annotations if the
omap does not exist if the mapping exists in the PV
annotation, it will use the new volumeID for further
operations.
Signed-off-by: Madhu Rajanna <madhupr007@gmail.com>
2021-03-30 10:33:55 +00:00
|
|
|
// the structure with elements from on-disk image metadata as well.
|
2021-12-09 08:00:52 +00:00
|
|
|
func GenVolFromVolID(
|
2021-06-25 11:52:34 +00:00
|
|
|
ctx context.Context,
|
|
|
|
volumeID string,
|
|
|
|
cr *util.Credentials,
|
2022-06-01 10:17:19 +00:00
|
|
|
secrets map[string]string,
|
|
|
|
) (*rbdVolume, error) {
|
2021-07-22 10:36:39 +00:00
|
|
|
var (
|
|
|
|
vi util.CSIIdentifier
|
|
|
|
vol *rbdVolume
|
|
|
|
)
|
|
|
|
|
|
|
|
err := vi.DecomposeCSIID(volumeID)
|
|
|
|
if err != nil {
|
2023-06-02 12:18:47 +00:00
|
|
|
return vol, fmt.Errorf("%w: error decoding volume ID (%w) (%s)",
|
2021-07-22 10:36:39 +00:00
|
|
|
ErrInvalidVolID, err, volumeID)
|
|
|
|
}
|
|
|
|
|
|
|
|
vol, err = generateVolumeFromVolumeID(ctx, volumeID, vi, cr, secrets)
|
2024-10-01 15:34:09 +00:00
|
|
|
if !shouldRetryVolumeGeneration(err) {
|
rbd:store/Read volumeID in/from PV annotation
In the case of the Async DR, the volumeID will
not be the same if the clusterID or the PoolID
is different, With Earlier implementation, it
is expected that the new volumeID mapping is
stored in the rados omap pool. In the case of the
ControllerExpand or the DeleteVolume Request,
the only volumeID will be sent it's not possible
to find the corresponding poolID in the new cluster.
With This Change, it works as below
The csi-rbdplugin-controller will watch for the PV
objects, when there are any PV objects created it
will check the omap already exists, If the omap doesn't
exist it will generate the new volumeID and it checks for
the volumeID mapping entry in the PV annotation, if the
mapping does not exist, it will add the new entry
to the PV annotation.
The cephcsi will check for the PV annotations if the
omap does not exist if the mapping exists in the PV
annotation, it will use the new volumeID for further
operations.
Signed-off-by: Madhu Rajanna <madhupr007@gmail.com>
2021-03-30 10:33:55 +00:00
|
|
|
return vol, err
|
|
|
|
}
|
|
|
|
|
2021-07-22 10:36:39 +00:00
|
|
|
// Check clusterID mapping exists
|
|
|
|
mapping, mErr := util.GetClusterMappingInfo(vi.ClusterID)
|
|
|
|
if mErr != nil {
|
|
|
|
return vol, mErr
|
|
|
|
}
|
|
|
|
if mapping != nil {
|
|
|
|
rbdVol, vErr := generateVolumeFromMapping(ctx, mapping, volumeID, vi, cr, secrets)
|
2024-10-01 15:34:09 +00:00
|
|
|
if !shouldRetryVolumeGeneration(vErr) {
|
2021-07-22 10:36:39 +00:00
|
|
|
return rbdVol, vErr
|
|
|
|
}
|
|
|
|
}
|
2023-06-02 09:32:54 +00:00
|
|
|
|
rbd:store/Read volumeID in/from PV annotation
In the case of the Async DR, the volumeID will
not be the same if the clusterID or the PoolID
is different, With Earlier implementation, it
is expected that the new volumeID mapping is
stored in the rados omap pool. In the case of the
ControllerExpand or the DeleteVolume Request,
the only volumeID will be sent it's not possible
to find the corresponding poolID in the new cluster.
With This Change, it works as below
The csi-rbdplugin-controller will watch for the PV
objects, when there are any PV objects created it
will check the omap already exists, If the omap doesn't
exist it will generate the new volumeID and it checks for
the volumeID mapping entry in the PV annotation, if the
mapping does not exist, it will add the new entry
to the PV annotation.
The cephcsi will check for the PV annotations if the
omap does not exist if the mapping exists in the PV
annotation, it will use the new volumeID for further
operations.
Signed-off-by: Madhu Rajanna <madhupr007@gmail.com>
2021-03-30 10:33:55 +00:00
|
|
|
return vol, err
|
|
|
|
}
|
|
|
|
|
2021-07-22 10:36:39 +00:00
|
|
|
// generateVolumeFromMapping checks the clusterID and poolID mapping and
|
|
|
|
// generates retrieves the OMAP information from the poolID got from the
|
|
|
|
// mapping.
|
|
|
|
func generateVolumeFromMapping(
|
|
|
|
ctx context.Context,
|
|
|
|
mapping *[]util.ClusterMappingInfo,
|
|
|
|
volumeID string,
|
|
|
|
vi util.CSIIdentifier,
|
|
|
|
cr *util.Credentials,
|
2022-06-01 10:17:19 +00:00
|
|
|
secrets map[string]string,
|
|
|
|
) (*rbdVolume, error) {
|
2021-07-22 10:36:39 +00:00
|
|
|
nvi := vi
|
|
|
|
vol := &rbdVolume{}
|
|
|
|
// extract clusterID mapping
|
|
|
|
for _, cm := range *mapping {
|
|
|
|
for key, val := range cm.ClusterIDMapping {
|
2021-08-30 04:47:45 +00:00
|
|
|
mappedClusterID := util.GetMappedID(key, val, vi.ClusterID)
|
2021-07-22 10:36:39 +00:00
|
|
|
if mappedClusterID == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-08-24 15:03:25 +00:00
|
|
|
log.DebugLog(ctx,
|
2021-07-22 10:36:39 +00:00
|
|
|
"found new clusterID mapping %s for existing clusterID %s",
|
|
|
|
mappedClusterID,
|
|
|
|
vi.ClusterID)
|
|
|
|
// Add mapping clusterID to Identifier
|
|
|
|
nvi.ClusterID = mappedClusterID
|
2024-04-04 08:52:46 +00:00
|
|
|
poolID := strconv.FormatInt(vi.LocationID, 10)
|
2021-07-22 10:36:39 +00:00
|
|
|
for _, pools := range cm.RBDpoolIDMappingInfo {
|
|
|
|
for key, val := range pools {
|
2021-08-30 04:47:45 +00:00
|
|
|
mappedPoolID := util.GetMappedID(key, val, poolID)
|
2021-07-22 10:36:39 +00:00
|
|
|
if mappedPoolID == "" {
|
|
|
|
continue
|
|
|
|
}
|
2021-08-24 15:03:25 +00:00
|
|
|
log.DebugLog(ctx,
|
2021-07-22 10:36:39 +00:00
|
|
|
"found new poolID mapping %s for existing pooID %s",
|
|
|
|
mappedPoolID,
|
|
|
|
poolID)
|
|
|
|
pID, err := strconv.ParseInt(mappedPoolID, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return vol, err
|
|
|
|
}
|
|
|
|
// Add mapping poolID to Identifier
|
|
|
|
nvi.LocationID = pID
|
|
|
|
vol, err = generateVolumeFromVolumeID(ctx, volumeID, nvi, cr, secrets)
|
2024-10-01 15:34:09 +00:00
|
|
|
if !shouldRetryVolumeGeneration(err) {
|
2021-07-22 10:36:39 +00:00
|
|
|
return vol, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return vol, util.ErrPoolNotFound
|
|
|
|
}
|
|
|
|
|
2024-10-01 15:34:09 +00:00
|
|
|
// shouldRetryVolumeGeneration determines whether the process of finding or generating
|
|
|
|
// volumes should continue based on the type of error encountered.
|
|
|
|
//
|
|
|
|
// It checks if the given error matches any of the following known errors:
|
|
|
|
// - util.ErrKeyNotFound: The key required to locate the volume is missing in Rados omap.
|
|
|
|
// - util.ErrPoolNotFound: The rbd pool where the volume/omap is expected doesn't exist.
|
|
|
|
// - ErrImageNotFound: The image doesn't exist in the rbd pool.
|
|
|
|
// - rados.ErrPermissionDenied: Permissions to access the pool is denied.
|
|
|
|
//
|
|
|
|
// If any of these errors are encountered, the function returns `true`, indicating
|
|
|
|
// that the volume search should continue because of known error. Otherwise, it
|
|
|
|
// returns `false`, meaning the search should stop.
|
|
|
|
//
|
|
|
|
// This helper function is used in scenarios where multiple attempts may be made
|
|
|
|
// to retrieve or generate volume information, and we want to gracefully handle
|
|
|
|
// specific failure cases while retrying for others.
|
|
|
|
func shouldRetryVolumeGeneration(err error) bool {
|
|
|
|
if err == nil {
|
|
|
|
return false // No error, do not retry
|
|
|
|
}
|
|
|
|
// Continue searching for specific known errors
|
|
|
|
return (errors.Is(err, util.ErrKeyNotFound) ||
|
|
|
|
errors.Is(err, util.ErrPoolNotFound) ||
|
|
|
|
errors.Is(err, ErrImageNotFound) ||
|
|
|
|
errors.Is(err, rados.ErrPermissionDenied))
|
|
|
|
}
|
|
|
|
|
2021-06-25 11:52:34 +00:00
|
|
|
func genVolFromVolumeOptions(
|
|
|
|
ctx context.Context,
|
2022-03-15 12:58:02 +00:00
|
|
|
volOptions map[string]string,
|
2022-06-01 10:17:19 +00:00
|
|
|
disableInUseChecks, checkClusterIDMapping bool,
|
|
|
|
) (*rbdVolume, error) {
|
2019-03-13 13:46:56 +00:00
|
|
|
var (
|
2020-02-24 13:19:42 +00:00
|
|
|
ok bool
|
|
|
|
err error
|
|
|
|
namePrefix string
|
2019-03-13 13:46:56 +00:00
|
|
|
)
|
2019-03-02 17:29:52 +00:00
|
|
|
|
2018-03-06 22:33:57 +00:00
|
|
|
rbdVol := &rbdVolume{}
|
2024-02-26 13:59:58 +00:00
|
|
|
|
2018-03-06 22:33:57 +00:00
|
|
|
rbdVol.Pool, ok = volOptions["pool"]
|
2018-01-09 18:59:50 +00:00
|
|
|
if !ok {
|
2024-02-26 13:59:58 +00:00
|
|
|
if _, ok = volOptions["topologyConstrainedPools"]; !ok {
|
|
|
|
return nil, errors.New("empty pool name or topologyConstrainedPools to provision volume")
|
|
|
|
}
|
2018-01-09 18:59:50 +00:00
|
|
|
}
|
2019-03-02 17:29:52 +00:00
|
|
|
|
2019-09-10 09:56:08 +00:00
|
|
|
rbdVol.DataPool = volOptions["dataPool"]
|
2020-02-24 13:19:42 +00:00
|
|
|
if namePrefix, ok = volOptions["volumeNamePrefix"]; ok {
|
|
|
|
rbdVol.NamePrefix = namePrefix
|
|
|
|
}
|
2019-09-10 09:56:08 +00:00
|
|
|
|
2021-09-07 06:05:11 +00:00
|
|
|
clusterID, err := util.GetClusterID(volOptions)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
rbdVol.Monitors, rbdVol.ClusterID, err = util.GetMonsAndClusterID(ctx, clusterID, checkClusterIDMapping)
|
2020-07-10 10:44:59 +00:00
|
|
|
if err != nil {
|
2021-08-24 15:03:25 +00:00
|
|
|
log.ErrorLog(ctx, "failed getting mons (%s)", err)
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2020-07-10 10:44:59 +00:00
|
|
|
return nil, err
|
2018-01-09 18:59:50 +00:00
|
|
|
}
|
2019-03-02 17:29:52 +00:00
|
|
|
|
2024-06-05 05:34:58 +00:00
|
|
|
rbdVol.RadosNamespace, err = util.GetRBDRadosNamespace(util.CsiConfigFile, rbdVol.ClusterID)
|
2020-06-01 13:57:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-03-11 06:28:48 +00:00
|
|
|
if rbdVol.Mounter, ok = volOptions["mounter"]; !ok {
|
|
|
|
rbdVol.Mounter = rbdDefaultMounter
|
|
|
|
}
|
2019-09-11 07:08:55 +00:00
|
|
|
// if no image features is provided, it results in empty string
|
2020-01-10 09:09:49 +00:00
|
|
|
// which disable all RBD image features as we expected
|
2021-03-11 06:28:48 +00:00
|
|
|
if err = rbdVol.validateImageFeatures(volOptions["imageFeatures"]); err != nil {
|
2021-08-24 15:03:25 +00:00
|
|
|
log.ErrorLog(ctx, "failed to validate image features %v", err)
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2021-03-11 06:28:48 +00:00
|
|
|
return nil, err
|
2018-01-09 18:59:50 +00:00
|
|
|
}
|
2019-03-14 00:18:04 +00:00
|
|
|
|
2021-08-24 15:03:25 +00:00
|
|
|
log.ExtendedLog(
|
2021-06-25 11:52:34 +00:00
|
|
|
ctx,
|
|
|
|
"setting disableInUseChecks: %t image features: %v mounter: %s",
|
|
|
|
disableInUseChecks,
|
2021-12-20 15:14:28 +00:00
|
|
|
rbdVol.ImageFeatureSet.Names(),
|
2021-06-25 11:52:34 +00:00
|
|
|
rbdVol.Mounter)
|
2019-03-14 00:18:04 +00:00
|
|
|
rbdVol.DisableInUseChecks = disableInUseChecks
|
|
|
|
|
2022-05-24 03:38:42 +00:00
|
|
|
err = rbdVol.setStripeConfiguration(volOptions)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-06-01 21:26:42 +00:00
|
|
|
return rbdVol, nil
|
2018-01-09 18:59:50 +00:00
|
|
|
}
|
2019-03-02 17:29:52 +00:00
|
|
|
|
2022-05-24 03:38:42 +00:00
|
|
|
func (ri *rbdImage) setStripeConfiguration(options map[string]string) error {
|
|
|
|
var err error
|
|
|
|
if val, ok := options["stripeUnit"]; ok {
|
|
|
|
ri.StripeUnit, err = strconv.ParseUint(val, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to parse stripeUnit %s: %w", val, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if val, ok := options["stripeCount"]; ok {
|
|
|
|
ri.StripeCount, err = strconv.ParseUint(val, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to parse stripeCount %s: %w", val, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if val, ok := options["objectSize"]; ok {
|
|
|
|
ri.ObjectSize, err = strconv.ParseUint(val, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to parse objectSize %s: %w", val, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-03-11 06:28:48 +00:00
|
|
|
func (rv *rbdVolume) validateImageFeatures(imageFeatures string) error {
|
2021-06-05 14:59:50 +00:00
|
|
|
// It is possible for image features to be an empty string which
|
|
|
|
// the Go split function would return a single item array with
|
|
|
|
// an empty string, causing a failure when trying to validate
|
|
|
|
// the features.
|
2021-06-15 03:54:54 +00:00
|
|
|
if imageFeatures == "" {
|
2021-06-05 14:59:50 +00:00
|
|
|
return nil
|
|
|
|
}
|
2021-03-11 06:28:48 +00:00
|
|
|
arr := strings.Split(imageFeatures, ",")
|
|
|
|
featureSet := sets.NewString(arr...)
|
|
|
|
for _, f := range arr {
|
|
|
|
sf, found := supportedFeatures[f]
|
|
|
|
if !found {
|
|
|
|
return fmt.Errorf("invalid feature %s", f)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, r := range sf.dependsOn {
|
|
|
|
if !featureSet.Has(r) {
|
|
|
|
return fmt.Errorf("feature %s requires %s to be set", f, r)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if sf.needRbdNbd && rv.Mounter != rbdNbdMounter {
|
|
|
|
return fmt.Errorf("feature %s requires rbd-nbd for mounter", f)
|
|
|
|
}
|
|
|
|
}
|
2021-12-20 15:14:28 +00:00
|
|
|
rv.ImageFeatureSet = librbd.FeatureSetFromNames(arr)
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2021-03-11 06:28:48 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-07-30 08:54:15 +00:00
|
|
|
func genSnapFromOptions(ctx context.Context, rbdVol *rbdVolume, snapOptions map[string]string) (*rbdSnapshot, error) {
|
2019-05-31 18:43:38 +00:00
|
|
|
var err error
|
2019-03-02 17:29:52 +00:00
|
|
|
|
2018-08-08 05:42:17 +00:00
|
|
|
rbdSnap := &rbdSnapshot{}
|
2019-05-31 18:43:38 +00:00
|
|
|
rbdSnap.Pool = rbdVol.Pool
|
2020-01-24 16:26:56 +00:00
|
|
|
rbdSnap.JournalPool = rbdVol.JournalPool
|
2020-06-01 13:57:51 +00:00
|
|
|
rbdSnap.RadosNamespace = rbdVol.RadosNamespace
|
2019-03-02 17:29:52 +00:00
|
|
|
|
2021-09-07 06:05:11 +00:00
|
|
|
clusterID, err := util.GetClusterID(snapOptions)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
rbdSnap.Monitors, rbdSnap.ClusterID, err = util.GetMonsAndClusterID(ctx, clusterID, false)
|
2019-03-02 17:29:52 +00:00
|
|
|
if err != nil {
|
2021-08-24 15:03:25 +00:00
|
|
|
log.ErrorLog(ctx, "failed getting mons (%s)", err)
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2020-07-30 08:54:15 +00:00
|
|
|
return nil, err
|
2018-08-09 13:07:00 +00:00
|
|
|
}
|
2018-08-08 05:42:17 +00:00
|
|
|
|
2020-02-24 13:19:42 +00:00
|
|
|
if namePrefix, ok := snapOptions["snapshotNamePrefix"]; ok {
|
|
|
|
rbdSnap.NamePrefix = namePrefix
|
|
|
|
}
|
|
|
|
|
2020-07-30 08:54:15 +00:00
|
|
|
return rbdSnap, nil
|
2018-08-08 05:42:17 +00:00
|
|
|
}
|
|
|
|
|
2020-07-19 12:21:03 +00:00
|
|
|
// hasSnapshotFeature checks if Layering is enabled for this image.
|
2022-01-12 13:51:31 +00:00
|
|
|
func (ri *rbdImage) hasSnapshotFeature() bool {
|
|
|
|
return (uint64(ri.ImageFeatureSet) & librbd.FeatureLayering) == librbd.FeatureLayering
|
2020-01-07 13:45:52 +00:00
|
|
|
}
|
|
|
|
|
2022-01-12 13:51:31 +00:00
|
|
|
func (ri *rbdImage) createSnapshot(ctx context.Context, pOpts *rbdSnapshot) error {
|
2023-09-29 03:57:11 +00:00
|
|
|
pOpts.RbdImageName = ri.RbdImageName
|
2021-08-24 15:03:25 +00:00
|
|
|
log.DebugLog(ctx, "rbd: snap create %s using mon %s", pOpts, pOpts.Monitors)
|
2022-01-12 13:51:31 +00:00
|
|
|
image, err := ri.open()
|
2019-04-22 21:35:39 +00:00
|
|
|
if err != nil {
|
2020-06-24 07:06:29 +00:00
|
|
|
return err
|
2019-02-18 08:22:52 +00:00
|
|
|
}
|
2020-06-24 07:06:29 +00:00
|
|
|
defer image.Close()
|
2019-02-18 08:22:52 +00:00
|
|
|
|
2020-06-24 07:06:29 +00:00
|
|
|
_, err = image.CreateSnapshot(pOpts.RbdSnapName)
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2020-06-24 07:06:29 +00:00
|
|
|
return err
|
2018-08-08 05:42:17 +00:00
|
|
|
}
|
|
|
|
|
2022-01-12 13:51:31 +00:00
|
|
|
func (ri *rbdImage) deleteSnapshot(ctx context.Context, pOpts *rbdSnapshot) error {
|
2021-08-24 15:03:25 +00:00
|
|
|
log.DebugLog(ctx, "rbd: snap rm %s using mon %s", pOpts, pOpts.Monitors)
|
2022-01-12 13:51:31 +00:00
|
|
|
image, err := ri.open()
|
2019-04-22 21:35:39 +00:00
|
|
|
if err != nil {
|
2020-06-24 07:06:29 +00:00
|
|
|
return err
|
2019-04-22 21:35:39 +00:00
|
|
|
}
|
2020-06-24 07:06:29 +00:00
|
|
|
defer image.Close()
|
2019-04-22 21:35:39 +00:00
|
|
|
|
2020-06-24 07:06:29 +00:00
|
|
|
snap := image.GetSnapshot(pOpts.RbdSnapName)
|
|
|
|
if snap == nil {
|
2020-06-25 11:30:04 +00:00
|
|
|
return fmt.Errorf("snapshot value is nil for %s", pOpts.RbdSnapName)
|
2019-04-22 21:35:39 +00:00
|
|
|
}
|
2020-06-24 07:06:29 +00:00
|
|
|
err = snap.Remove()
|
2020-07-02 21:43:40 +00:00
|
|
|
if errors.Is(err, librbd.ErrNotFound) {
|
2024-05-07 22:51:05 +00:00
|
|
|
return fmt.Errorf("Failed as %w (internal %w)", ErrSnapNotFound, err)
|
2020-06-24 07:06:29 +00:00
|
|
|
}
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2020-06-24 07:06:29 +00:00
|
|
|
return err
|
2019-04-22 21:35:39 +00:00
|
|
|
}
|
|
|
|
|
2021-06-25 11:52:34 +00:00
|
|
|
func (rv *rbdVolume) cloneRbdImageFromSnapshot(
|
|
|
|
ctx context.Context,
|
|
|
|
pSnapOpts *rbdSnapshot,
|
2022-06-01 10:17:19 +00:00
|
|
|
parentVol *rbdVolume,
|
|
|
|
) error {
|
2020-06-24 07:17:21 +00:00
|
|
|
var err error
|
2022-05-24 03:38:42 +00:00
|
|
|
log.DebugLog(ctx, "rbd: clone %s %s (features: %s) using mon %s",
|
|
|
|
pSnapOpts, rv, rv.ImageFeatureSet.Names(), rv.Monitors)
|
2019-04-22 21:35:39 +00:00
|
|
|
|
2021-03-18 11:23:08 +00:00
|
|
|
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
|
|
|
|
}()
|
|
|
|
|
2024-09-11 16:11:03 +00:00
|
|
|
options, err := rv.constructImageOptions(ctx)
|
2022-05-24 03:38:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2020-06-24 07:17:21 +00:00
|
|
|
}
|
2024-09-11 16:11:03 +00:00
|
|
|
defer options.Destroy()
|
2018-08-08 05:42:17 +00:00
|
|
|
|
2020-07-21 05:10:13 +00:00
|
|
|
err = options.SetUint64(librbd.ImageOptionCloneFormat, 2)
|
2020-06-24 07:17:21 +00:00
|
|
|
if err != nil {
|
2022-05-24 03:38:42 +00:00
|
|
|
return err
|
2020-06-24 07:17:21 +00:00
|
|
|
}
|
2021-03-18 11:23:08 +00:00
|
|
|
// As the clone is yet to be created, open the Ioctx.
|
2020-06-24 07:17:21 +00:00
|
|
|
err = rv.openIoctx()
|
|
|
|
if err != nil {
|
2020-06-25 11:30:04 +00:00
|
|
|
return fmt.Errorf("failed to get IOContext: %w", err)
|
2020-06-24 07:17:21 +00:00
|
|
|
}
|
2018-08-08 05:42:17 +00:00
|
|
|
|
2021-06-25 11:52:34 +00:00
|
|
|
err = librbd.CloneImage(
|
|
|
|
parentVol.ioctx,
|
|
|
|
pSnapOpts.RbdImageName,
|
|
|
|
pSnapOpts.RbdSnapName,
|
|
|
|
rv.ioctx,
|
|
|
|
rv.RbdImageName,
|
|
|
|
options)
|
2018-08-08 05:42:17 +00:00
|
|
|
if err != nil {
|
2020-06-25 11:30:04 +00:00
|
|
|
return fmt.Errorf("failed to create rbd clone: %w", err)
|
2018-08-08 05:42:17 +00:00
|
|
|
}
|
|
|
|
|
2021-01-26 15:47:14 +00:00
|
|
|
// delete the cloned image if a next step fails
|
|
|
|
deleteClone := true
|
|
|
|
defer func() {
|
|
|
|
if deleteClone {
|
|
|
|
err = librbd.RemoveImage(rv.ioctx, rv.RbdImageName)
|
|
|
|
if err != nil {
|
2021-08-24 15:03:25 +00:00
|
|
|
log.ErrorLog(ctx, "failed to delete temporary image %q: %v", rv, err)
|
2021-01-26 15:47:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2021-12-20 15:18:35 +00:00
|
|
|
// get image latest information
|
|
|
|
err = rv.getImageInfo()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to get image info of %s: %w", rv, err)
|
|
|
|
}
|
|
|
|
|
2021-01-26 15:47:14 +00:00
|
|
|
// Success! Do not delete the cloned image now :)
|
|
|
|
deleteClone = false
|
|
|
|
|
2018-08-08 05:42:17 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-09-11 16:11:03 +00:00
|
|
|
// constructImageOptions constructs the ImageOptions that should get set on the
|
|
|
|
// RBD-image at the time of its creation/cloning.
|
|
|
|
func (rv *rbdVolume) constructImageOptions(ctx context.Context) (*librbd.ImageOptions, error) {
|
2022-05-24 03:38:42 +00:00
|
|
|
var err error
|
2024-09-11 16:11:03 +00:00
|
|
|
options := librbd.NewRbdImageOptions()
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
options.Destroy()
|
|
|
|
}
|
|
|
|
}()
|
2022-05-24 03:38:42 +00:00
|
|
|
|
|
|
|
logMsg := fmt.Sprintf("setting image options on %s", rv)
|
|
|
|
if rv.DataPool != "" {
|
2024-04-04 08:52:46 +00:00
|
|
|
logMsg += ", data pool %s" + rv.DataPool
|
2022-05-24 03:38:42 +00:00
|
|
|
err = options.SetString(librbd.RbdImageOptionDataPool, rv.DataPool)
|
|
|
|
if err != nil {
|
2024-09-11 16:11:03 +00:00
|
|
|
return nil, fmt.Errorf("failed to set data pool: %w", err)
|
2022-05-24 03:38:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if rv.ImageFeatureSet != 0 {
|
|
|
|
err = options.SetUint64(librbd.RbdImageOptionFeatures, uint64(rv.ImageFeatureSet))
|
|
|
|
if err != nil {
|
2024-09-11 16:11:03 +00:00
|
|
|
return nil, fmt.Errorf("failed to set image features: %w", err)
|
2022-05-24 03:38:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if rv.StripeCount != 0 {
|
|
|
|
logMsg += fmt.Sprintf(", stripe count %d, stripe unit %d", rv.StripeCount, rv.StripeUnit)
|
|
|
|
err = options.SetUint64(librbd.RbdImageOptionStripeCount, rv.StripeCount)
|
|
|
|
if err != nil {
|
2024-09-11 16:11:03 +00:00
|
|
|
return nil, fmt.Errorf("failed to set stripe count: %w", err)
|
2022-05-24 03:38:42 +00:00
|
|
|
}
|
|
|
|
err = options.SetUint64(librbd.RbdImageOptionStripeUnit, rv.StripeUnit)
|
|
|
|
if err != nil {
|
2024-09-11 16:11:03 +00:00
|
|
|
return nil, fmt.Errorf("failed to set stripe unit: %w", err)
|
2022-05-24 03:38:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if rv.ObjectSize != 0 {
|
|
|
|
order := uint64(math.Log2(float64(rv.ObjectSize)))
|
|
|
|
logMsg += fmt.Sprintf(", object size %d, order %d", rv.ObjectSize, order)
|
|
|
|
err = options.SetUint64(librbd.RbdImageOptionOrder, order)
|
|
|
|
if err != nil {
|
2024-09-11 16:11:03 +00:00
|
|
|
return nil, fmt.Errorf("failed to set object size: %w", err)
|
2022-05-24 03:38:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
log.DebugLog(ctx, logMsg)
|
|
|
|
|
2024-09-11 16:11:03 +00:00
|
|
|
return options, nil
|
2022-05-24 03:38:42 +00:00
|
|
|
}
|
|
|
|
|
2024-07-26 08:01:02 +00:00
|
|
|
// GetCreationTime returns the creation time of the image. if the image
|
2023-08-22 16:52:49 +00:00
|
|
|
// creation time is not set, it queries the image info and returns the creation time.
|
2024-08-05 16:27:15 +00:00
|
|
|
func (ri *rbdImage) GetCreationTime(ctx context.Context) (*time.Time, error) {
|
2023-08-22 16:52:49 +00:00
|
|
|
if ri.CreatedAt != nil {
|
|
|
|
return ri.CreatedAt, nil
|
|
|
|
}
|
|
|
|
if err := ri.getImageInfo(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-10-01 08:27:03 +00:00
|
|
|
if ri.CreatedAt == nil {
|
|
|
|
return nil, fmt.Errorf("failed to get creation time for image %q", ri)
|
|
|
|
}
|
|
|
|
|
2023-08-22 16:52:49 +00:00
|
|
|
return ri.CreatedAt, nil
|
|
|
|
}
|
|
|
|
|
2019-04-22 21:35:39 +00:00
|
|
|
// getImageInfo queries rbd about the given image and returns its metadata, and returns
|
2020-07-19 12:21:03 +00:00
|
|
|
// ErrImageNotFound if provided image is not found.
|
2022-01-12 13:51:31 +00:00
|
|
|
func (ri *rbdImage) getImageInfo() error {
|
|
|
|
image, err := ri.open()
|
2020-01-15 13:06:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer image.Close()
|
2019-04-22 21:35:39 +00:00
|
|
|
|
2020-01-15 13:06:03 +00:00
|
|
|
imageInfo, err := image.Stat()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2018-09-21 14:38:50 +00:00
|
|
|
}
|
2020-01-15 13:06:03 +00:00
|
|
|
// TODO: can rv.VolSize not be a uint64? Or initialize it to -1?
|
2022-01-12 13:51:31 +00:00
|
|
|
ri.VolSize = int64(imageInfo.Size)
|
2018-09-21 14:38:50 +00:00
|
|
|
|
2020-01-15 13:06:03 +00:00
|
|
|
features, err := image.GetFeatures()
|
2018-08-08 05:42:17 +00:00
|
|
|
if err != nil {
|
2020-01-15 13:06:03 +00:00
|
|
|
return err
|
2018-08-08 05:42:17 +00:00
|
|
|
}
|
2022-01-12 13:51:31 +00:00
|
|
|
ri.ImageFeatureSet = librbd.FeatureSet(features)
|
2020-08-23 08:29:44 +00:00
|
|
|
|
|
|
|
// Get parent information.
|
2020-11-02 11:55:41 +00:00
|
|
|
parentInfo, err := image.GetParent()
|
2020-08-23 08:29:44 +00:00
|
|
|
if err != nil {
|
|
|
|
// Caller should decide whether not finding
|
|
|
|
// the parent is an error or not.
|
2020-11-02 11:55:41 +00:00
|
|
|
if errors.Is(err, librbd.ErrNotFound) {
|
2022-01-12 13:51:31 +00:00
|
|
|
ri.ParentName = ""
|
2020-08-23 08:29:44 +00:00
|
|
|
} else {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
2022-01-12 13:51:31 +00:00
|
|
|
ri.ParentName = parentInfo.Image.ImageName
|
|
|
|
ri.ParentPool = parentInfo.Image.PoolName
|
2024-03-29 09:56:02 +00:00
|
|
|
ri.ParentInTrash = parentInfo.Image.Trash
|
2020-08-23 08:29:44 +00:00
|
|
|
}
|
|
|
|
// Get image creation time
|
|
|
|
tm, err := image.GetCreateTimestamp()
|
2020-06-24 07:43:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-08-05 16:27:15 +00:00
|
|
|
|
2020-08-23 08:29:44 +00:00
|
|
|
t := time.Unix(tm.Sec, tm.Nsec)
|
2024-08-05 16:27:15 +00:00
|
|
|
ri.CreatedAt = &t
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2020-01-15 13:06:03 +00:00
|
|
|
return nil
|
2019-04-22 21:35:39 +00:00
|
|
|
}
|
2020-10-21 05:34:03 +00:00
|
|
|
|
2022-02-21 12:25:23 +00:00
|
|
|
// getParent returns parent image if it exists.
|
|
|
|
func (ri *rbdImage) getParent() (*rbdImage, error) {
|
|
|
|
err := ri.getImageInfo()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-03-29 09:56:02 +00:00
|
|
|
// The image may not have a parent or the parent maybe in trash.
|
|
|
|
// Return nil in both the cases.
|
|
|
|
if ri.ParentName == "" || ri.ParentInTrash {
|
2022-02-21 12:25:23 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
parentImage := rbdImage{}
|
|
|
|
parentImage.conn = ri.conn.Copy()
|
|
|
|
parentImage.ClusterID = ri.ClusterID
|
|
|
|
parentImage.Monitors = ri.Monitors
|
|
|
|
parentImage.Pool = ri.ParentPool
|
|
|
|
parentImage.RadosNamespace = ri.RadosNamespace
|
|
|
|
parentImage.RbdImageName = ri.ParentName
|
|
|
|
|
|
|
|
err = parentImage.getImageInfo()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &parentImage, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// flattenParent flatten the given image's parent if it exists according to hard and soft
|
|
|
|
// limits.
|
|
|
|
func (ri *rbdImage) flattenParent(ctx context.Context, hardLimit, softLimit uint) error {
|
|
|
|
parentImage, err := ri.getParent()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if parentImage == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2024-03-21 14:01:56 +00:00
|
|
|
defer parentImage.Destroy(ctx)
|
2022-02-21 12:25:23 +00:00
|
|
|
|
|
|
|
return parentImage.flattenRbdImage(ctx, false, hardLimit, softLimit)
|
|
|
|
}
|
|
|
|
|
2019-04-22 21:35:39 +00:00
|
|
|
/*
|
2020-07-07 12:14:19 +00:00
|
|
|
checkSnapExists queries rbd about the snapshots of the given image and returns
|
|
|
|
ErrImageNotFound if provided image is not found, and ErrSnapNotFound if
|
2020-07-19 12:21:03 +00:00
|
|
|
provided snap is not found in the images snapshot list.
|
2019-04-22 21:35:39 +00:00
|
|
|
*/
|
2022-01-12 13:51:31 +00:00
|
|
|
func (ri *rbdImage) checkSnapExists(rbdSnap *rbdSnapshot) error {
|
|
|
|
image, err := ri.open()
|
2020-06-24 07:14:23 +00:00
|
|
|
if err != nil {
|
2020-07-07 12:14:19 +00:00
|
|
|
return err
|
2018-08-08 05:42:17 +00:00
|
|
|
}
|
2020-06-24 07:14:23 +00:00
|
|
|
defer image.Close()
|
2018-08-08 05:42:17 +00:00
|
|
|
|
2020-06-24 07:14:23 +00:00
|
|
|
snaps, err := image.GetSnapshotNames()
|
2019-04-22 21:35:39 +00:00
|
|
|
if err != nil {
|
2020-07-07 12:14:19 +00:00
|
|
|
return err
|
2019-04-22 21:35:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, snap := range snaps {
|
2020-06-24 07:14:23 +00:00
|
|
|
if snap.Name == rbdSnap.RbdSnapName {
|
2020-07-07 12:14:19 +00:00
|
|
|
return nil
|
2019-04-22 21:35:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-07 05:30:37 +00:00
|
|
|
return fmt.Errorf("%w: snap %s not found", ErrSnapNotFound, rbdSnap)
|
2018-08-08 05:42:17 +00:00
|
|
|
}
|
2019-08-03 22:11:28 +00:00
|
|
|
|
2020-07-19 12:21:03 +00:00
|
|
|
// rbdImageMetadataStash strongly typed JSON spec for stashed RBD image metadata.
|
2019-08-03 22:11:28 +00:00
|
|
|
type rbdImageMetadataStash struct {
|
2020-06-01 13:57:51 +00:00
|
|
|
Version int `json:"Version"`
|
|
|
|
Pool string `json:"pool"`
|
|
|
|
RadosNamespace string `json:"radosNamespace"`
|
|
|
|
ImageName string `json:"image"`
|
2020-09-08 05:23:28 +00:00
|
|
|
UnmapOptions string `json:"unmapOptions"`
|
2020-06-01 13:57:51 +00:00
|
|
|
NbdAccess bool `json:"accessType"`
|
|
|
|
Encrypted bool `json:"encrypted"`
|
2021-09-01 11:53:43 +00:00
|
|
|
DevicePath string `json:"device"` // holds NBD device path for now
|
|
|
|
LogDir string `json:"logDir"` // holds the client log path
|
|
|
|
LogStrategy string `json:"logFileStrategy"` // ceph client log strategy
|
2019-08-03 22:11:28 +00:00
|
|
|
}
|
|
|
|
|
2020-07-19 12:21:03 +00:00
|
|
|
// file name in which image metadata is stashed.
|
2019-08-03 22:11:28 +00:00
|
|
|
const stashFileName = "image-meta.json"
|
|
|
|
|
2020-06-01 13:57:51 +00:00
|
|
|
// spec returns the image-spec (pool/{namespace/}image) format of the image.
|
2020-05-28 18:39:44 +00:00
|
|
|
func (ri *rbdImageMetadataStash) String() string {
|
2020-06-01 13:57:51 +00:00
|
|
|
if ri.RadosNamespace != "" {
|
|
|
|
return fmt.Sprintf("%s/%s/%s", ri.Pool, ri.RadosNamespace, ri.ImageName)
|
|
|
|
}
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2020-05-28 18:39:44 +00:00
|
|
|
return fmt.Sprintf("%s/%s", ri.Pool, ri.ImageName)
|
|
|
|
}
|
|
|
|
|
2019-08-03 22:11:28 +00:00
|
|
|
// stashRBDImageMetadata stashes required fields into the stashFileName at the passed in path, in
|
2020-07-19 12:21:03 +00:00
|
|
|
// JSON format.
|
2021-06-04 06:24:52 +00:00
|
|
|
func stashRBDImageMetadata(volOptions *rbdVolume, metaDataPath string) error {
|
2021-07-13 12:21:05 +00:00
|
|
|
imgMeta := rbdImageMetadataStash{
|
2020-07-21 05:10:13 +00:00
|
|
|
// there are no checks for this at present
|
2023-06-02 08:59:52 +00:00
|
|
|
Version: 3, //nolint:gomnd // number specifies version.
|
2020-06-01 13:57:51 +00:00
|
|
|
Pool: volOptions.Pool,
|
|
|
|
RadosNamespace: volOptions.RadosNamespace,
|
|
|
|
ImageName: volOptions.RbdImageName,
|
2022-05-27 18:03:32 +00:00
|
|
|
Encrypted: volOptions.isBlockEncrypted(),
|
2020-09-08 05:23:28 +00:00
|
|
|
UnmapOptions: volOptions.UnmapOptions,
|
2019-08-03 22:11:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
imgMeta.NbdAccess = false
|
|
|
|
if volOptions.Mounter == rbdTonbd && hasNBD {
|
|
|
|
imgMeta.NbdAccess = true
|
2021-08-20 01:06:35 +00:00
|
|
|
imgMeta.LogDir = volOptions.LogDir
|
2021-09-01 11:53:43 +00:00
|
|
|
imgMeta.LogStrategy = volOptions.LogStrategy
|
2019-08-03 22:11:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
encodedBytes, err := json.Marshal(imgMeta)
|
|
|
|
if err != nil {
|
2020-12-08 14:09:33 +00:00
|
|
|
return fmt.Errorf("failed to marshall JSON image metadata for image (%s): %w", volOptions, err)
|
2019-08-03 22:11:28 +00:00
|
|
|
}
|
|
|
|
|
2021-06-04 06:24:52 +00:00
|
|
|
fPath := filepath.Join(metaDataPath, stashFileName)
|
2022-01-21 09:28:27 +00:00
|
|
|
err = os.WriteFile(fPath, encodedBytes, 0o600)
|
2019-08-03 22:11:28 +00:00
|
|
|
if err != nil {
|
2020-12-08 14:09:33 +00:00
|
|
|
return fmt.Errorf("failed to stash JSON image metadata for image (%s) at path (%s): %w", volOptions, fPath, err)
|
2019-08-03 22:11:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-01-27 09:33:01 +00:00
|
|
|
// checkRBDImageMetadataStashExists checks if the stashFile exists at the passed in path.
|
|
|
|
func checkRBDImageMetadataStashExists(metaDataPath string) bool {
|
|
|
|
imageMetaPath := filepath.Join(metaDataPath, stashFileName)
|
|
|
|
_, err := os.Stat(imageMetaPath)
|
|
|
|
|
|
|
|
return err == nil
|
|
|
|
}
|
|
|
|
|
2020-07-19 12:21:03 +00:00
|
|
|
// lookupRBDImageMetadataStash reads and returns stashed image metadata at passed in path.
|
2021-06-04 06:24:52 +00:00
|
|
|
func lookupRBDImageMetadataStash(metaDataPath string) (rbdImageMetadataStash, error) {
|
2019-08-03 22:11:28 +00:00
|
|
|
var imgMeta rbdImageMetadataStash
|
|
|
|
|
2021-06-04 06:24:52 +00:00
|
|
|
fPath := filepath.Join(metaDataPath, stashFileName)
|
2022-01-21 09:32:34 +00:00
|
|
|
encodedBytes, err := os.ReadFile(fPath) // #nosec - intended reading from fPath
|
2019-08-03 22:11:28 +00:00
|
|
|
if err != nil {
|
|
|
|
if !os.IsNotExist(err) {
|
2020-12-08 14:09:59 +00:00
|
|
|
return imgMeta, fmt.Errorf("failed to read stashed JSON image metadata from path (%s): %w", fPath, err)
|
2019-08-03 22:11:28 +00:00
|
|
|
}
|
|
|
|
|
2024-05-07 22:51:05 +00:00
|
|
|
return imgMeta, fmt.Errorf("Failed as %w (internal %w)", ErrMissingStash, err)
|
2019-08-03 22:11:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err = json.Unmarshal(encodedBytes, &imgMeta)
|
|
|
|
if err != nil {
|
2020-12-08 14:09:59 +00:00
|
|
|
return imgMeta, fmt.Errorf("failed to unmarshall stashed JSON image metadata from path (%s): %w", fPath, err)
|
2019-08-03 22:11:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return imgMeta, nil
|
|
|
|
}
|
|
|
|
|
2021-05-31 11:09:36 +00:00
|
|
|
// updateRBDImageMetadataStash reads and updates stashFile with the required
|
|
|
|
// fields at the passed in path, in JSON format.
|
|
|
|
func updateRBDImageMetadataStash(metaDataPath, device string) error {
|
|
|
|
if device == "" {
|
|
|
|
return errors.New("device is empty")
|
|
|
|
}
|
|
|
|
imgMeta, err := lookupRBDImageMetadataStash(metaDataPath)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to find image metadata: %w", err)
|
|
|
|
}
|
|
|
|
imgMeta.DevicePath = device
|
|
|
|
|
|
|
|
encodedBytes, err := json.Marshal(imgMeta)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to marshal JSON image metadata for spec:(%s) : %w", imgMeta.String(), err)
|
|
|
|
}
|
|
|
|
|
|
|
|
fPath := filepath.Join(metaDataPath, stashFileName)
|
2022-01-21 09:37:41 +00:00
|
|
|
err = os.WriteFile(fPath, encodedBytes, 0o600)
|
2021-05-31 11:09:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to stash JSON image metadata at path: (%s) for spec:(%s) : %w",
|
|
|
|
fPath, imgMeta.String(), err)
|
|
|
|
}
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2021-05-31 11:09:36 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-07-19 12:21:03 +00:00
|
|
|
// cleanupRBDImageMetadataStash cleans up any stashed metadata at passed in path.
|
2021-06-04 06:24:52 +00:00
|
|
|
func cleanupRBDImageMetadataStash(metaDataPath string) error {
|
|
|
|
fPath := filepath.Join(metaDataPath, stashFileName)
|
2019-08-03 22:11:28 +00:00
|
|
|
if err := os.Remove(fPath); err != nil {
|
2020-12-08 14:10:22 +00:00
|
|
|
return fmt.Errorf("failed to cleanup stashed JSON data (%s): %w", fPath, err)
|
2019-08-03 22:11:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2019-11-27 12:14:31 +00:00
|
|
|
|
2021-12-15 05:44:24 +00:00
|
|
|
// expand checks if the requestedVolume size and the existing image size both
|
|
|
|
// are same. If they are same, it returns nil else it resizes the image.
|
|
|
|
func (rv *rbdVolume) expand() error {
|
|
|
|
if rv.RequestedVolSize == rv.VolSize {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv.resize(rv.RequestedVolSize)
|
|
|
|
}
|
|
|
|
|
2020-07-23 08:01:21 +00:00
|
|
|
// resize the given volume to new size.
|
2020-07-30 06:28:51 +00:00
|
|
|
// updates Volsize of rbdVolume object to newSize in case of success.
|
2022-01-12 13:51:31 +00:00
|
|
|
func (ri *rbdImage) resize(newSize int64) error {
|
|
|
|
image, err := ri.open()
|
2020-07-30 06:28:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer image.Close()
|
2019-11-27 12:14:31 +00:00
|
|
|
|
2020-07-30 06:28:51 +00:00
|
|
|
err = image.Resize(uint64(util.RoundOffVolSize(newSize) * helpers.MiB))
|
2019-11-27 12:14:31 +00:00
|
|
|
if err != nil {
|
2020-07-30 06:28:51 +00:00
|
|
|
return err
|
2019-11-27 12:14:31 +00:00
|
|
|
}
|
2020-07-30 06:28:51 +00:00
|
|
|
// update Volsize of rbdVolume object to newSize.
|
2022-01-12 13:51:31 +00:00
|
|
|
ri.VolSize = newSize
|
2021-01-28 13:59:48 +00:00
|
|
|
|
2019-11-27 12:14:31 +00:00
|
|
|
return nil
|
|
|
|
}
|
2019-12-13 11:41:32 +00:00
|
|
|
|
2021-03-12 12:37:15 +00:00
|
|
|
func (ri *rbdImage) GetMetadata(key string) (string, error) {
|
|
|
|
image, err := ri.open()
|
2019-12-13 11:41:32 +00:00
|
|
|
if err != nil {
|
2020-05-12 14:56:08 +00:00
|
|
|
return "", err
|
2019-12-13 11:41:32 +00:00
|
|
|
}
|
2020-01-09 10:31:07 +00:00
|
|
|
defer image.Close()
|
|
|
|
|
|
|
|
return image.GetMetadata(key)
|
|
|
|
}
|
2019-12-13 11:41:32 +00:00
|
|
|
|
2021-03-12 12:37:15 +00:00
|
|
|
func (ri *rbdImage) SetMetadata(key, value string) error {
|
|
|
|
image, err := ri.open()
|
2020-01-09 10:31:07 +00:00
|
|
|
if err != nil {
|
2020-05-12 14:56:08 +00:00
|
|
|
return err
|
2020-01-09 10:31:07 +00:00
|
|
|
}
|
|
|
|
defer image.Close()
|
|
|
|
|
|
|
|
return image.SetMetadata(key, value)
|
|
|
|
}
|
|
|
|
|
2021-07-23 14:46:20 +00:00
|
|
|
// RemoveMetadata deletes the key and data from the metadata of the image.
|
|
|
|
func (ri *rbdImage) RemoveMetadata(key string) error {
|
|
|
|
image, err := ri.open()
|
2021-01-28 08:45:51 +00:00
|
|
|
if err != nil {
|
2021-07-23 14:46:20 +00:00
|
|
|
return err
|
2021-01-28 08:45:51 +00:00
|
|
|
}
|
2021-07-23 14:46:20 +00:00
|
|
|
defer image.Close()
|
2021-01-28 08:45:51 +00:00
|
|
|
|
2021-07-23 14:46:20 +00:00
|
|
|
return image.RemoveMetadata(key)
|
|
|
|
}
|
|
|
|
|
|
|
|
// MigrateMetadata reads the metadata contents from oldKey and stores it in
|
|
|
|
// newKey. In case oldKey was not set, the defaultValue is stored in newKey.
|
|
|
|
// Once done, oldKey will be removed as well.
|
|
|
|
func (ri *rbdImage) MigrateMetadata(oldKey, newKey, defaultValue string) (string, error) {
|
|
|
|
value, err := ri.GetMetadata(newKey)
|
|
|
|
if err == nil {
|
|
|
|
return value, nil
|
|
|
|
} else if !errors.Is(err, librbd.ErrNotFound) {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
// migrate contents from oldKey to newKey
|
|
|
|
removeOldKey := true
|
|
|
|
value, err = ri.GetMetadata(oldKey)
|
|
|
|
if errors.Is(err, librbd.ErrNotFound) {
|
|
|
|
// in case oldKey was not set, set newKey to defaultValue
|
|
|
|
value = defaultValue
|
|
|
|
removeOldKey = false
|
|
|
|
} else if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
// newKey was not set, set it now to prevent regular error cases for missing metadata
|
|
|
|
err = ri.SetMetadata(newKey, value)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
// the newKey was set with data from oldKey, oldKey is not needed anymore
|
|
|
|
if removeOldKey {
|
|
|
|
err = ri.RemoveMetadata(oldKey)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return value, nil
|
2021-01-28 08:45:51 +00:00
|
|
|
}
|
|
|
|
|
2021-05-27 07:09:49 +00:00
|
|
|
// DeepCopy creates an independent image (dest) from the source image. This
|
|
|
|
// process may take some time when the image is large.
|
2022-01-12 13:51:31 +00:00
|
|
|
func (ri *rbdImage) DeepCopy(dest *rbdImage) error {
|
2021-05-27 07:09:49 +00:00
|
|
|
opts := librbd.NewRbdImageOptions()
|
|
|
|
defer opts.Destroy()
|
|
|
|
|
|
|
|
// when doing DeepCopy, also flatten the new image
|
|
|
|
err := opts.SetUint64(librbd.ImageOptionFlatten, 1)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = dest.openIoctx()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-01-12 13:51:31 +00:00
|
|
|
image, err := ri.open()
|
2021-05-27 07:09:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer image.Close()
|
|
|
|
|
2021-06-16 15:19:55 +00:00
|
|
|
err = image.DeepCopy(dest.ioctx, dest.RbdImageName, opts)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// deep-flatten is not supported by all clients, so disable it
|
|
|
|
return dest.DisableDeepFlatten()
|
|
|
|
}
|
|
|
|
|
|
|
|
// DisableDeepFlatten removed the deep-flatten feature from the image.
|
2022-01-12 13:51:31 +00:00
|
|
|
func (ri *rbdImage) DisableDeepFlatten() error {
|
|
|
|
image, err := ri.open()
|
2021-06-16 15:19:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer image.Close()
|
|
|
|
|
|
|
|
return image.UpdateFeatures(librbd.FeatureDeepFlatten, false)
|
2021-05-27 07:09:49 +00:00
|
|
|
}
|
|
|
|
|
2022-01-12 13:51:31 +00:00
|
|
|
func (ri *rbdImage) listSnapshots() ([]librbd.SnapInfo, error) {
|
|
|
|
image, err := ri.open()
|
2020-08-27 11:09:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer image.Close()
|
|
|
|
|
|
|
|
snapInfoList, err := image.GetSnapshotNames()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2020-08-27 11:09:45 +00:00
|
|
|
return snapInfoList, nil
|
2020-06-25 13:00:31 +00:00
|
|
|
}
|
|
|
|
|
2020-08-27 11:09:45 +00:00
|
|
|
// isTrashSnap returns true if the snapshot belongs to trash namespace.
|
2022-01-12 13:51:31 +00:00
|
|
|
func (ri *rbdImage) isTrashSnap(snapID uint64) (bool, error) {
|
|
|
|
image, err := ri.open()
|
2020-08-27 11:09:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
2020-06-25 13:00:31 +00:00
|
|
|
}
|
2020-08-27 11:09:45 +00:00
|
|
|
defer image.Close()
|
2020-06-25 13:00:31 +00:00
|
|
|
|
2020-08-27 11:09:45 +00:00
|
|
|
// Get namespace type for the snapshot
|
|
|
|
nsType, err := image.GetSnapNamespaceType(snapID)
|
2020-06-25 13:00:31 +00:00
|
|
|
if err != nil {
|
2020-08-27 11:09:45 +00:00
|
|
|
return false, err
|
2020-06-25 13:00:31 +00:00
|
|
|
}
|
2020-08-27 11:09:45 +00:00
|
|
|
|
|
|
|
if nsType == librbd.SnapNamespaceTypeTrash {
|
|
|
|
return true, nil
|
|
|
|
}
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2020-08-27 11:09:45 +00:00
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// getOrigSnapName returns the original snap name for
|
|
|
|
// the snapshots in Trash Namespace.
|
2022-01-12 13:51:31 +00:00
|
|
|
func (ri *rbdImage) getOrigSnapName(snapID uint64) (string, error) {
|
|
|
|
image, err := ri.open()
|
2020-08-27 11:09:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
defer image.Close()
|
|
|
|
|
|
|
|
origSnapName, err := image.GetSnapTrashNamespace(snapID)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return origSnapName, nil
|
2020-06-25 13:00:31 +00:00
|
|
|
}
|
2021-06-01 11:33:29 +00:00
|
|
|
|
|
|
|
func (ri *rbdImage) isCompatibleEncryption(dst *rbdImage) error {
|
2022-05-27 19:55:58 +00:00
|
|
|
riEncrypted := ri.isBlockEncrypted() || ri.isFileEncrypted()
|
|
|
|
dstEncrypted := dst.isBlockEncrypted() || dst.isFileEncrypted()
|
2021-06-01 11:33:29 +00:00
|
|
|
switch {
|
2022-05-27 19:55:58 +00:00
|
|
|
case riEncrypted && !dstEncrypted:
|
2021-06-17 06:38:21 +00:00
|
|
|
return fmt.Errorf("cannot create unencrypted volume from encrypted volume %q", ri)
|
2021-06-01 11:33:29 +00:00
|
|
|
|
2022-05-27 19:55:58 +00:00
|
|
|
case !riEncrypted && dstEncrypted:
|
2021-06-17 06:38:21 +00:00
|
|
|
return fmt.Errorf("cannot create encrypted volume from unencrypted volume %q", ri)
|
2021-06-01 11:33:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2021-06-17 06:15:29 +00:00
|
|
|
|
2022-01-12 08:53:40 +00:00
|
|
|
func (ri *rbdImage) isCompabitableClone(dst *rbdImage) error {
|
|
|
|
if dst.VolSize < ri.VolSize {
|
|
|
|
return fmt.Errorf(
|
|
|
|
"volume size %d is smaller than source volume size %d",
|
|
|
|
dst.VolSize,
|
|
|
|
ri.VolSize)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-01-12 09:52:51 +00:00
|
|
|
func (ri *rbdImage) AddSnapshotScheduling(
|
2021-06-28 11:38:42 +00:00
|
|
|
interval admin.Interval,
|
2022-06-01 10:17:19 +00:00
|
|
|
startTime admin.StartTime,
|
|
|
|
) error {
|
2021-06-28 11:38:42 +00:00
|
|
|
ls := admin.NewLevelSpec(ri.Pool, ri.RadosNamespace, ri.RbdImageName)
|
|
|
|
ra, err := ri.conn.GetRBDAdmin()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
adminConn := ra.MirrorSnashotSchedule()
|
|
|
|
err = adminConn.Add(ls, interval, startTime)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2021-06-28 11:38:42 +00:00
|
|
|
return nil
|
|
|
|
}
|
2021-08-18 07:21:23 +00:00
|
|
|
|
|
|
|
// getCephClientLogFileName compiles the complete log file path based on inputs.
|
|
|
|
func getCephClientLogFileName(id, logDir, prefix string) string {
|
|
|
|
if prefix == "" {
|
|
|
|
prefix = "ceph"
|
|
|
|
}
|
|
|
|
|
|
|
|
if logDir == "" {
|
|
|
|
logDir = defaultLogDir
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Sprintf("%s/%s-%s.log", logDir, prefix, id)
|
|
|
|
}
|
2021-09-01 18:43:37 +00:00
|
|
|
|
2021-09-01 11:53:43 +00:00
|
|
|
// strategicActionOnLogFile act on log file based on cephLogStrategy.
|
|
|
|
func strategicActionOnLogFile(ctx context.Context, logStrategy, logFile string) {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
switch strings.ToLower(logStrategy) {
|
|
|
|
case "compress":
|
|
|
|
if err = log.GzipLogFile(logFile); err != nil {
|
|
|
|
log.ErrorLog(ctx, "failed to compress logfile %q: %v", logFile, err)
|
|
|
|
}
|
|
|
|
case "remove":
|
|
|
|
if err = os.Remove(logFile); err != nil {
|
|
|
|
log.ErrorLog(ctx, "failed to remove logfile %q: %v", logFile, err)
|
|
|
|
}
|
|
|
|
case "preserve":
|
|
|
|
// do nothing
|
|
|
|
default:
|
|
|
|
log.ErrorLog(ctx, "unknown cephLogStrategy option %q: hint: 'remove'|'compress'|'preserve'", logStrategy)
|
|
|
|
}
|
|
|
|
}
|
2021-12-22 05:59:09 +00:00
|
|
|
|
|
|
|
// genVolFromVolIDWithMigration populate a rbdVol structure based on the volID format.
|
|
|
|
func genVolFromVolIDWithMigration(
|
2022-06-01 10:17:19 +00:00
|
|
|
ctx context.Context, volID string, cr *util.Credentials, secrets map[string]string,
|
|
|
|
) (*rbdVolume, error) {
|
2021-12-22 05:59:09 +00:00
|
|
|
if isMigrationVolID(volID) {
|
|
|
|
pmVolID, pErr := parseMigrationVolID(volID)
|
|
|
|
if pErr != nil {
|
|
|
|
return nil, pErr
|
|
|
|
}
|
|
|
|
|
|
|
|
return genVolFromMigVolID(ctx, pmVolID, cr)
|
|
|
|
}
|
|
|
|
rv, err := GenVolFromVolID(ctx, volID, cr, secrets)
|
|
|
|
if err != nil {
|
2024-03-21 14:01:56 +00:00
|
|
|
rv.Destroy(ctx)
|
2021-12-22 05:59:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return rv, err
|
|
|
|
}
|
2022-02-22 12:23:45 +00:00
|
|
|
|
2022-04-28 06:33:30 +00:00
|
|
|
// setAllMetadata set all the metadata from arg parameters on RBD image.
|
|
|
|
func (rv *rbdVolume) setAllMetadata(parameters map[string]string) error {
|
2022-04-12 04:03:00 +00:00
|
|
|
if !rv.EnableMetadata {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-04-28 06:33:30 +00:00
|
|
|
for k, v := range parameters {
|
2022-03-02 09:12:22 +00:00
|
|
|
err := rv.SetMetadata(k, v)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to set metadata key %q, value %q on image: %w", k, v, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-11 04:27:29 +00:00
|
|
|
if rv.ClusterName != "" {
|
|
|
|
err := rv.SetMetadata(clusterNameKey, rv.ClusterName)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to set metadata key %q, value %q on image: %w",
|
|
|
|
clusterNameKey, rv.ClusterName, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-02 09:12:22 +00:00
|
|
|
return nil
|
|
|
|
}
|
2022-04-04 11:43:39 +00:00
|
|
|
|
|
|
|
// unsetAllMetadata unset all the metadata from arg keys on RBD image.
|
|
|
|
func (rv *rbdVolume) unsetAllMetadata(keys []string) error {
|
|
|
|
for _, key := range keys {
|
|
|
|
err := rv.RemoveMetadata(key)
|
2023-11-20 11:31:39 +00:00
|
|
|
if err != nil && !errors.Is(err, librbd.ErrNotExist) {
|
2022-04-04 11:43:39 +00:00
|
|
|
return fmt.Errorf("failed to unset metadata key %q on %q: %w", key, rv, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-16 07:15:02 +00:00
|
|
|
err := rv.RemoveMetadata(clusterNameKey)
|
2023-11-20 11:31:39 +00:00
|
|
|
if err != nil && !errors.Is(err, librbd.ErrNotExist) {
|
2022-05-16 07:15:02 +00:00
|
|
|
return fmt.Errorf("failed to unset metadata key %q on %q: %w", clusterNameKey, rv, err)
|
|
|
|
}
|
|
|
|
|
2022-04-04 11:43:39 +00:00
|
|
|
return nil
|
|
|
|
}
|
2024-07-29 11:07:37 +00:00
|
|
|
|
|
|
|
// GetID returns the ID of the volume.
|
|
|
|
func (ri *rbdImage) GetID(ctx context.Context) (string, error) {
|
|
|
|
if ri.VolID == "" {
|
|
|
|
return "", errors.New("BUG: VolID is not set")
|
|
|
|
}
|
|
|
|
|
|
|
|
return ri.VolID, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetName returns the name of the rbd image.
|
|
|
|
func (ri *rbdImage) GetName(ctx context.Context) (string, error) {
|
|
|
|
if ri.RbdImageName == "" {
|
|
|
|
return "", errors.New("BUG: name is not set")
|
|
|
|
}
|
|
|
|
|
|
|
|
return ri.RbdImageName, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetPool returns the name of the pool that holds the Volume.
|
|
|
|
func (ri *rbdImage) GetPool(ctx context.Context) (string, error) {
|
|
|
|
if ri.Pool == "" {
|
|
|
|
return "", errors.New("BUG: pool is not set")
|
|
|
|
}
|
|
|
|
|
|
|
|
return ri.Pool, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetClusterID returns the clusterID the volume belongs to.
|
|
|
|
func (ri *rbdImage) GetClusterID(ctx context.Context) (string, error) {
|
|
|
|
if ri.ClusterID == "" {
|
|
|
|
return "", errors.New("BUG: clusterID is not set")
|
|
|
|
}
|
|
|
|
|
|
|
|
return ri.ClusterID, nil
|
|
|
|
}
|