From 6470fe2c3f779dbec984e4b161f276e9662168ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julian=20Peliz=C3=A4us?= Date: Fri, 13 Oct 2023 11:52:57 +0200 Subject: [PATCH 1/2] lxd/state: Add local server UUID MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Julian Pelizäus --- lxd/state/state.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lxd/state/state.go b/lxd/state/state.go index 2d5f5e3519b8..ed4aa08409e8 100644 --- a/lxd/state/state.go +++ b/lxd/state/state.go @@ -76,6 +76,9 @@ type State struct { // Local server name. ServerName string + // Local server UUID. + ServerUUID string + // Local server start time. StartTime time.Time From dac89083bf98ba4fefdaa6a968b7cffca4ebe867 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julian=20Peliz=C3=A4us?= Date: Fri, 13 Oct 2023 11:55:48 +0200 Subject: [PATCH 2/2] lxd/daemon: Setup and load UUID file on init MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Julian Pelizäus --- lxd/daemon.go | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/lxd/daemon.go b/lxd/daemon.go index 854782aab274..f2947cfd09fe 100644 --- a/lxd/daemon.go +++ b/lxd/daemon.go @@ -21,6 +21,7 @@ import ( dqliteClient "github.com/canonical/go-dqlite/client" "github.com/canonical/go-dqlite/driver" "github.com/go-macaroon-bakery/macaroon-bakery/v3/bakery" + "github.com/google/uuid" "github.com/gorilla/mux" liblxc "github.com/lxc/go-lxc" "golang.org/x/sys/unix" @@ -146,6 +147,9 @@ type Daemon struct { // Cluster. serverName string + // Server's UUID from file. + serverUUID string + lokiClient *loki.Client // HTTP-01 challenge provider for ACME @@ -423,6 +427,7 @@ func (d *Daemon) State() *state.State { GlobalConfig: globalConfig, LocalConfig: localConfig, ServerName: d.serverName, + ServerUUID: d.serverUUID, StartTime: d.startTime, Authorizer: d.authorizer, } @@ -1207,6 +1212,29 @@ func (d *Daemon) init() error { return err } + // Setup and load the server's UUID file. + // Use os.VarDir to allow setting up the uuid file also in the test suite. + var serverUUID string + uuidPath := filepath.Join(d.os.VarDir, "server.uuid") + if !shared.PathExists(uuidPath) { + serverUUID = uuid.New().String() + err := os.WriteFile(uuidPath, []byte(serverUUID), 0600) + if err != nil { + return fmt.Errorf("Failed to create server.uuid file: %w", err) + } + } + + if serverUUID == "" { + uuidBytes, err := os.ReadFile(uuidPath) + if err != nil { + return fmt.Errorf("Failed to read server.uuid file: %w", err) + } + + serverUUID = string(uuidBytes) + } + + d.serverUUID = serverUUID + // Mount the storage pools. logger.Infof("Initializing storage pools") err = storageStartup(d.State(), false)