Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add LXD server UUID file #12544

Merged
merged 2 commits into from
Nov 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions lxd/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
}
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions lxd/state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ type State struct {
// Local server name.
ServerName string

// Local server UUID.
ServerUUID string

// Local server start time.
StartTime time.Time

Expand Down
Loading