Skip to content

Commit

Permalink
lxd/patches: Deactivate patchStorageSetVolumeUUID
Browse files Browse the repository at this point in the history
Signed-off-by: Julian Pelizäus <julian.pelizaeus@canonical.com>
  • Loading branch information
roosterfish committed Mar 15, 2024
1 parent 52bb283 commit 8021fa9
Showing 1 changed file with 2 additions and 154 deletions.
156 changes: 2 additions & 154 deletions lxd/patches.go
Original file line number Diff line number Diff line change
Expand Up @@ -1394,160 +1394,8 @@ func patchRemoveCandidRBACConfigKeys(_ string, d *Daemon) error {

// patchStorageSetVolumeUUID sets a unique volatile.uuid field for each volume and its snapshots.
func patchStorageSetVolumeUUID(_ string, d *Daemon) error {
s := d.State()

// Get all storage pool names.
var pools []string
err := s.DB.Cluster.Transaction(s.ShutdownCtx, func(ctx context.Context, ct *db.ClusterTx) error {
var err error
pools, err = ct.GetStoragePoolNames(ctx)

return err
})
if err != nil {
// Skip the rest of the patch if no storage pools were found.
if api.StatusErrorCheck(err, http.StatusNotFound) {
return nil
}

return fmt.Errorf("Failed getting storage pool names: %w", err)
}

// Check if this member is the current cluster leader.
isLeader := false

if !d.serverClustered {
// If we're not clustered, we're the leader.
isLeader = true
} else {
leaderAddress, err := d.gateway.LeaderAddress()
if err != nil {
return err
}

if s.LocalConfig.ClusterAddress() == leaderAddress {
isLeader = true
}
}

// Ensure the renaming is done on the cluster leader only.
if !isLeader {
return nil
}

poolIDNameMap := make(map[int64]string, 0)
poolVolumes := make(map[int64][]*db.StorageVolume, 0)
poolBuckets := make(map[int64][]*db.StorageBucket, 0)

for _, pool := range pools {
var poolID int64
var buckets []*db.StorageBucket
err = s.DB.Cluster.Transaction(s.ShutdownCtx, func(ctx context.Context, tx *db.ClusterTx) error {
// Get storage pool ID.
poolID, err = tx.GetStoragePoolID(ctx, pool)
if err != nil {
return fmt.Errorf("Failed getting storage pool ID of pool %q: %w", pool, err)
}

// Get the pool's storage buckets.
buckets, err = tx.GetStoragePoolBuckets(ctx, false, db.StorageBucketFilter{PoolID: &poolID})
if err != nil {
return fmt.Errorf("Failed getting custom storage volumes of pool %q: %w", pool, err)
}

return nil
})
if err != nil {
return err
}

if poolBuckets[poolID] == nil {
poolBuckets[poolID] = []*db.StorageBucket{}
}

poolIDNameMap[poolID] = pool
poolBuckets[poolID] = append(poolBuckets[poolID], buckets...)

var volumes []*db.StorageVolume
err = s.DB.Cluster.Transaction(s.ShutdownCtx, func(ctx context.Context, tx *db.ClusterTx) error {
// Get the pool's storage volumes.
volumes, err = tx.GetStoragePoolVolumes(ctx, poolID, false)
if err != nil {
return fmt.Errorf("Failed getting storage volumes of pool %q: %w", pool, err)
}

return nil
})
if err != nil {
return err
}

if poolVolumes[poolID] == nil {
poolVolumes[poolID] = []*db.StorageVolume{}
}

poolVolumes[poolID] = append(poolVolumes[poolID], volumes...)
}

for pool, buckets := range poolBuckets {
for _, bucket := range buckets {
// Skip buckets that already have a UUID.
if bucket.Config["volatile.uuid"] == "" {
bucket.Config["volatile.uuid"] = uuid.New().String()

err := s.DB.Cluster.Transaction(s.ShutdownCtx, func(ctx context.Context, ct *db.ClusterTx) error {
return ct.UpdateStoragePoolBucket(d.shutdownCtx, pool, bucket.ID, bucket.Writable())
})
if err != nil {
return err
}
}
}
}

for pool, volumes := range poolVolumes {
for _, vol := range volumes {
volDBType, err := storagePools.VolumeTypeNameToDBType(vol.Type)
if err != nil {
return err
}

err = s.DB.Cluster.Transaction(s.ShutdownCtx, func(ctx context.Context, ct *db.ClusterTx) error {
// Skip volumes that already have a UUID.
if vol.Config["volatile.uuid"] == "" {
vol.Config["volatile.uuid"] = uuid.New().String()

err := ct.UpdateStoragePoolVolume(ctx, vol.Project, vol.Name, volDBType, pool, vol.Description, vol.Config)
if err != nil {
return fmt.Errorf("Failed updating volume %q in project %q on pool %q: %w", vol.Name, vol.Project, poolIDNameMap[pool], err)
}
}

snapshots, err := ct.GetLocalStoragePoolVolumeSnapshotsWithType(ctx, vol.Project, vol.Name, volDBType, pool)
if err != nil {
return err
}

for _, snapshot := range snapshots {
// Skip snapshots that already have a UUID.
if snapshot.Config["volatile.uuid"] == "" {
snapshot.Config["volatile.uuid"] = uuid.New().String()

err = ct.UpdateStorageVolumeSnapshot(ctx, snapshot.ProjectName, snapshot.Name, volDBType, pool, snapshot.Description, snapshot.Config, snapshot.ExpiryDate)
if err != nil {
return fmt.Errorf("Failed updating snapshot %q in project %q on pool %q: %w", snapshot.Name, snapshot.ProjectName, poolIDNameMap[pool], err)
}
}
}

return nil
})
if err != nil {
return err
}
}
}

// This patch is superseded by patchStorageSetVolumeUUIDWithQuery.
// In its earlier version the patch might not have been applied due to leader election.
return nil
}

Expand Down

0 comments on commit 8021fa9

Please sign in to comment.