[lxc-devel] [lxd/master] Instance: Adds volatile.uuid key for container and VMs, replaces volatile.vm.uuid for VMs

tomponline on Github lxc-bot at linuxcontainers.org
Fri Nov 13 15:49:26 UTC 2020


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 421 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20201113/e09c2af4/attachment-0001.bin>
-------------- next part --------------
From cfa1d01ef9de7c93b034103905a92f45d55a0f73 Mon Sep 17 00:00:00 2001
From: Thomas Parrott <thomas.parrott at canonical.com>
Date: Fri, 13 Nov 2020 15:37:43 +0000
Subject: [PATCH 1/8] lxd/patches: Adds patchVMRenameUUIDKey patch to renane
 config key from volatile.vm.uuid to volatile.uuid

Brings into line with container's `volatile.uuid` key.

Signed-off-by: Thomas Parrott <thomas.parrott at canonical.com>
---
 lxd/patches.go | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 52 insertions(+)

diff --git a/lxd/patches.go b/lxd/patches.go
index d3ca662f9b..e0569dafee 100644
--- a/lxd/patches.go
+++ b/lxd/patches.go
@@ -116,6 +116,7 @@ var patches = []patch{
 	{name: "network_ovn_remove_routes", stage: patchPostDaemonStorage, run: patchNetworkOVNRemoveRoutes},
 	{name: "network_fan_enable_nat", stage: patchPostDaemonStorage, run: patchNetworkFANEnableNAT},
 	{name: "thinpool_typo_fix", stage: patchPostDaemonStorage, run: patchThinpoolTypoFix},
+	{name: "vm_rename_uuid_key", stage: patchPostDaemonStorage, run: patchVMRenameUUIDKey},
 }
 
 type patch struct {
@@ -180,6 +181,57 @@ func patchesApply(d *Daemon, stage patchStage) error {
 
 // Patches begin here
 
+// patchVMRenameUUIDKey renames the volatile.vm.uuid key to volatile.uuid.
+func patchVMRenameUUIDKey(name string, d *Daemon) error {
+	oldUUIDKey := "volatile.vm.uuid"
+	newUUIDKey := "volatile.uuid"
+
+	return d.State().Cluster.InstanceList(func(inst db.Instance, p api.Project, profiles []api.Profile) error {
+		if inst.Type != instancetype.VM {
+			return nil
+		}
+
+		return d.State().Cluster.Transaction(func(tx *db.ClusterTx) error {
+			uuid := inst.Config[oldUUIDKey]
+			if uuid != "" {
+				changes := map[string]string{
+					oldUUIDKey: "",
+					newUUIDKey: uuid,
+				}
+
+				logger.Debugf("Renaming config key %q to %q for VM %q (Project %q)", oldUUIDKey, newUUIDKey, inst.Name, inst.Project)
+				err := tx.UpdateInstanceConfig(inst.ID, changes)
+				if err != nil {
+					return errors.Wrapf(err, "Failed renaming config key %q to %q for VM %q (Project %q)", oldUUIDKey, newUUIDKey, inst.Name, inst.Project)
+				}
+			}
+
+			snaps, err := tx.GetInstanceSnapshotsWithName(inst.Project, inst.Name)
+			if err != nil {
+				return err
+			}
+
+			for _, snap := range snaps {
+				uuid := snap.Config[oldUUIDKey]
+				if uuid != "" {
+					changes := map[string]string{
+						oldUUIDKey: "",
+						newUUIDKey: uuid,
+					}
+
+					logger.Debugf("Renaming config key %q to %q for VM %q (Project %q)", oldUUIDKey, newUUIDKey, inst.Name, inst.Project)
+					err = tx.UpdateInstanceSnapshotConfig(snap.ID, changes)
+					if err != nil {
+						return errors.Wrapf(err, "Failed renaming config key %q to %q for VM %q (Project %q)", oldUUIDKey, newUUIDKey, inst.Name, inst.Project)
+					}
+				}
+			}
+
+			return nil
+		})
+	})
+}
+
 // patchThinpoolTypoFix renames any config incorrectly set config file entries due to the lvm.thinpool_name typo.
 func patchThinpoolTypoFix(name string, d *Daemon) error {
 	revert := revert.New()

From 79a0fecff37d6e0a42ef9d84293439defcdabdcf Mon Sep 17 00:00:00 2001
From: Thomas Parrott <thomas.parrott at canonical.com>
Date: Fri, 13 Nov 2020 14:48:19 +0000
Subject: [PATCH 2/8] shared/validate: Adds IsUUID function

Signed-off-by: Thomas Parrott <thomas.parrott at canonical.com>
---
 shared/validate/validate.go | 11 +++++++++++
 1 file changed, 11 insertions(+)

diff --git a/shared/validate/validate.go b/shared/validate/validate.go
index 2a84a2bf3e..70ac4ef807 100644
--- a/shared/validate/validate.go
+++ b/shared/validate/validate.go
@@ -7,6 +7,8 @@ import (
 	"strconv"
 	"strings"
 
+	"github.com/pborman/uuid"
+
 	"github.com/lxc/lxd/shared/units"
 )
 
@@ -451,3 +453,12 @@ func IsURLSegmentSafe(value string) error {
 
 	return nil
 }
+
+// IsUUID validates whether a value is a UUID.
+func IsUUID(value string) error {
+	if uuid.Parse(value) == nil {
+		return fmt.Errorf("Invalid UUID")
+	}
+
+	return nil
+}

From 4173e42f63e374b6096bab99c7a7ed3ade8362ca Mon Sep 17 00:00:00 2001
From: Thomas Parrott <thomas.parrott at canonical.com>
Date: Fri, 13 Nov 2020 14:48:40 +0000
Subject: [PATCH 3/8] shared/instance: Adds volatile.uuid key to instance
 validation

Signed-off-by: Thomas Parrott <thomas.parrott at canonical.com>
---
 shared/instance.go | 1 +
 1 file changed, 1 insertion(+)

diff --git a/shared/instance.go b/shared/instance.go
index 06541b4539..75bf75161a 100644
--- a/shared/instance.go
+++ b/shared/instance.go
@@ -255,6 +255,7 @@ var KnownInstanceConfigKeys = map[string]func(value string) error{
 	"volatile.idmap.current":    validate.IsAny,
 	"volatile.idmap.next":       validate.IsAny,
 	"volatile.apply_quota":      validate.IsAny,
+	"volatile.uuid":             validate.Optional(validate.IsUUID),
 }
 
 // ConfigKeyChecker returns a function that will check whether or not

From 20bbd6e21b081f15e853399d52ac9a3164facdb5 Mon Sep 17 00:00:00 2001
From: Thomas Parrott <thomas.parrott at canonical.com>
Date: Fri, 13 Nov 2020 14:49:01 +0000
Subject: [PATCH 4/8] shared/instance: Removes vm.uuid from instance validation
 in ConfigKeyChecker

Signed-off-by: Thomas Parrott <thomas.parrott at canonical.com>
---
 shared/instance.go | 4 ----
 1 file changed, 4 deletions(-)

diff --git a/shared/instance.go b/shared/instance.go
index 75bf75161a..023573a0cf 100644
--- a/shared/instance.go
+++ b/shared/instance.go
@@ -306,10 +306,6 @@ func ConfigKeyChecker(key string) (func(value string) error, error) {
 			return validate.IsAny, nil
 		}
 
-		if strings.HasSuffix(key, "vm.uuid") {
-			return validate.IsAny, nil
-		}
-
 		if strings.HasSuffix(key, ".ceph_rbd") {
 			return validate.IsAny, nil
 		}

From fe619580b047fbc5127a9469be260b8f123ac451 Mon Sep 17 00:00:00 2001
From: Thomas Parrott <thomas.parrott at canonical.com>
Date: Fri, 13 Nov 2020 14:49:29 +0000
Subject: [PATCH 5/8] doc/instances: Replaces volatile.vm.uuid with
 volatile.uuid

So can be used for containers too.

Signed-off-by: Thomas Parrott <thomas.parrott at canonical.com>
---
 doc/instances.md | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/doc/instances.md b/doc/instances.md
index d6809ff115..171d4a1979 100644
--- a/doc/instances.md
+++ b/doc/instances.md
@@ -110,7 +110,7 @@ volatile.idmap.current                      | string    | -             | The id
 volatile.idmap.next                         | string    | -             | The idmap to use next time the instance starts
 volatile.last\_state.idmap                  | string    | -             | Serialized instance uid/gid map
 volatile.last\_state.power                  | string    | -             | Instance state as of last host shutdown
-volatile.vm.uuid                            | string    | -             | Virtual machine UUID
+volatile.uuid                               | string    | -             | Instance UUID
 volatile.\<name\>.apply\_quota              | string    | -             | Disk quota to be applied on next instance start
 volatile.\<name\>.ceph\_rbd                 | string    | -             | RBD device path for Ceph disk devices
 volatile.\<name\>.host\_name                | string    | -             | Network device name on the host

From 04028a667a328abd4b0806c3b03b34364cfe92ca Mon Sep 17 00:00:00 2001
From: Thomas Parrott <thomas.parrott at canonical.com>
Date: Fri, 13 Nov 2020 14:50:06 +0000
Subject: [PATCH 6/8] lxd/instance/drivers/driver/qemu: Updates Start to use
 and populate volatile.uuid instead of volatile.vm.uuid

Signed-off-by: Thomas Parrott <thomas.parrott at canonical.com>
---
 lxd/instance/drivers/driver_qemu.go | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/lxd/instance/drivers/driver_qemu.go b/lxd/instance/drivers/driver_qemu.go
index bda16f90ae..d58cf874b5 100644
--- a/lxd/instance/drivers/driver_qemu.go
+++ b/lxd/instance/drivers/driver_qemu.go
@@ -779,10 +779,10 @@ func (vm *qemu) Start(stateful bool) error {
 	}
 
 	// Get a UUID for Qemu.
-	vmUUID := vm.localConfig["volatile.vm.uuid"]
+	vmUUID := vm.localConfig["volatile.uuid"]
 	if vmUUID == "" {
 		vmUUID = uuid.New()
-		vm.VolatileSet(map[string]string{"volatile.vm.uuid": vmUUID})
+		vm.VolatileSet(map[string]string{"volatile.uuid": vmUUID})
 	}
 
 	// Copy OVMF settings firmware to nvram file.

From 647102c5a3cfdfaf4d6aaf4defeebf9bde0fcd41 Mon Sep 17 00:00:00 2001
From: Thomas Parrott <thomas.parrott at canonical.com>
Date: Fri, 13 Nov 2020 15:44:46 +0000
Subject: [PATCH 7/8] lxd/instance/drivers/driver/lxc: Generate instance UUID
 if not set in startCommon

Signed-off-by: Thomas Parrott <thomas.parrott at canonical.com>
---
 lxd/instance/drivers/driver_lxc.go | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/lxd/instance/drivers/driver_lxc.go b/lxd/instance/drivers/driver_lxc.go
index 4705a36d90..ae53526fb2 100644
--- a/lxd/instance/drivers/driver_lxc.go
+++ b/lxd/instance/drivers/driver_lxc.go
@@ -19,6 +19,7 @@ import (
 	"time"
 
 	"github.com/flosch/pongo2"
+	"github.com/pborman/uuid"
 	"github.com/pkg/errors"
 	"golang.org/x/sys/unix"
 	liblxc "gopkg.in/lxc/go-lxc.v2"
@@ -2129,6 +2130,13 @@ func (c *lxc) startCommon() (string, []func() error, error) {
 	}
 	ourStart = mountInfo.OurMount
 
+	// Generate UUID if not present.
+	instUUID := c.localConfig["volatile.uuid"]
+	if instUUID == "" {
+		instUUID = uuid.New()
+		c.VolatileSet(map[string]string{"volatile.uuid": instUUID})
+	}
+
 	// Create the devices
 	postStartHooks := []func() error{}
 	nicID := -1

From 2b4dadffa029f4b2d6945ed1deb0f34ea166d0ee Mon Sep 17 00:00:00 2001
From: Thomas Parrott <thomas.parrott at canonical.com>
Date: Fri, 13 Nov 2020 15:45:18 +0000
Subject: [PATCH 8/8] lxd/instance/drivers/driver/qemu: Makes UUID generation
 terminology consistent with container

Signed-off-by: Thomas Parrott <thomas.parrott at canonical.com>
---
 lxd/instance/drivers/driver_qemu.go | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)

diff --git a/lxd/instance/drivers/driver_qemu.go b/lxd/instance/drivers/driver_qemu.go
index d58cf874b5..65197efeaf 100644
--- a/lxd/instance/drivers/driver_qemu.go
+++ b/lxd/instance/drivers/driver_qemu.go
@@ -778,11 +778,11 @@ func (vm *qemu) Start(stateful bool) error {
 		logger.Warn("Unable to use virtio-fs for config drive, using 9p as a fallback: virtiofsd missing")
 	}
 
-	// Get a UUID for Qemu.
-	vmUUID := vm.localConfig["volatile.uuid"]
-	if vmUUID == "" {
-		vmUUID = uuid.New()
-		vm.VolatileSet(map[string]string{"volatile.uuid": vmUUID})
+	// Generate UUID if not present.
+	instUUID := vm.localConfig["volatile.uuid"]
+	if instUUID == "" {
+		instUUID = uuid.New()
+		vm.VolatileSet(map[string]string{"volatile.uuid": instUUID})
 	}
 
 	// Copy OVMF settings firmware to nvram file.
@@ -850,7 +850,7 @@ func (vm *qemu) Start(stateful bool) error {
 		qemuPath,
 		"-S",
 		"-name", vm.Name(),
-		"-uuid", vmUUID,
+		"-uuid", instUUID,
 		"-daemonize",
 		"-cpu", "host",
 		"-nographic",


More information about the lxc-devel mailing list