Skip to content

Commit

Permalink
lxd/instance/utils: Use stable random generator for temporary instanc…
Browse files Browse the repository at this point in the history
…e name

This allows not having the full size UUID name but instead use a shorter name which is still drived from the instances UUID. Required to accomodate the PowerFlex storage driver which has a maximum lenght of 31 characters per storage volume

Signed-off-by: Julian Pelizäus <[email protected]>
  • Loading branch information
roosterfish committed Oct 9, 2023
1 parent 2319853 commit 99faea1
Showing 1 changed file with 24 additions and 8 deletions.
32 changes: 24 additions & 8 deletions lxd/instance/instance_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
liblxc "github.com/lxc/go-lxc"
"github.com/pborman/uuid"

"github.com/canonical/lxd/client"
lxd "github.com/canonical/lxd/client"
"github.com/canonical/lxd/lxd/backup"
"github.com/canonical/lxd/lxd/db"
"github.com/canonical/lxd/lxd/db/cluster"
Expand All @@ -31,6 +31,7 @@ import (
"github.com/canonical/lxd/lxd/seccomp"
"github.com/canonical/lxd/lxd/state"
"github.com/canonical/lxd/lxd/sys"
"github.com/canonical/lxd/lxd/util"
"github.com/canonical/lxd/shared"
"github.com/canonical/lxd/shared/api"
"github.com/canonical/lxd/shared/idmap"
Expand Down Expand Up @@ -1050,9 +1051,14 @@ func NextSnapshotName(s *state.State, inst Instance, defaultPattern string) (str
return pattern, nil
}

// temporaryName concatenates the move prefix and instUUID for a temporary instance.
func temporaryName(instUUID string) string {
return fmt.Sprintf("lxd-move-of-%s", instUUID)
// temporaryName returns the temporary instance name using a stable random generator.
func temporaryName(instUUID string) (string, error) {
r, err := util.GetStableRandomGenerator(instUUID)
if err != nil {
return "", err
}

return fmt.Sprintf("lxd-move-%d", r.Uint64()), nil
}

// MoveTemporaryName returns a name derived from the instance's volatile.uuid, to use when moving an instance
Expand All @@ -1065,11 +1071,11 @@ func MoveTemporaryName(inst Instance) (string, error) {
instUUID = uuid.New()
err := inst.VolatileSet(map[string]string{"volatile.uuid": instUUID})
if err != nil {
return "", fmt.Errorf("Failed generating instance UUID: %w", err)
return "", fmt.Errorf("Failed setting volatile.uuid: %w", err)
}
}

return temporaryName(instUUID), nil
return temporaryName(instUUID)
}

// IsSameLogicalInstance returns true if the supplied Instance and db.Instance have the same project and name or
Expand All @@ -1084,12 +1090,22 @@ func IsSameLogicalInstance(inst Instance, dbInst *db.InstanceArgs) bool {
if dbInst.Config["volatile.uuid"] == inst.LocalConfig()["volatile.uuid"] {
// Accommodate moving instances between storage pools.
// Check temporary copy against source.
if dbInst.Name == temporaryName(inst.LocalConfig()["volatile.uuid"]) {
tempName, err := temporaryName(inst.LocalConfig()["volatile.uuid"])
if err != nil {
return false
}

if dbInst.Name == tempName {
return true
}

// Check source against temporary copy.
if inst.Name() == temporaryName(dbInst.Config["volatile.uuid"]) {
tempName, err = temporaryName(dbInst.Config["volatile.uuid"])
if err != nil {
return false
}

if inst.Name() == tempName {
return true
}

Expand Down

0 comments on commit 99faea1

Please sign in to comment.