2022-05-03 11:52:17 +00:00
|
|
|
package daemon // import "github.com/docker/docker/daemon"
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
2023-07-14 17:59:45 +00:00
|
|
|
"path/filepath"
|
2022-05-03 11:52:17 +00:00
|
|
|
|
|
|
|
"github.com/docker/docker/pkg/ioutils"
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
2023-07-14 17:59:45 +00:00
|
|
|
const idFilename = "engine-id"
|
|
|
|
|
|
|
|
// LoadOrCreateID loads the engine's ID from the given root, or generates a new ID
|
2022-05-03 11:52:17 +00:00
|
|
|
// if it doesn't exist. It returns the ID, and any error that occurred when
|
|
|
|
// saving the file.
|
|
|
|
//
|
|
|
|
// Note that this function expects the daemon's root directory to already have
|
|
|
|
// been created with the right permissions and ownership (usually this would
|
|
|
|
// be done by daemon.CreateDaemonRoot().
|
2023-07-14 17:59:45 +00:00
|
|
|
func LoadOrCreateID(root string) (string, error) {
|
2022-05-03 11:52:17 +00:00
|
|
|
var id string
|
2023-07-14 17:59:45 +00:00
|
|
|
idPath := filepath.Join(root, idFilename)
|
2022-05-03 11:52:17 +00:00
|
|
|
idb, err := os.ReadFile(idPath)
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
id = uuid.New().String()
|
2022-01-20 13:25:24 +00:00
|
|
|
if err := ioutils.AtomicWriteFile(idPath, []byte(id), os.FileMode(0o600)); err != nil {
|
2022-05-03 11:52:17 +00:00
|
|
|
return "", errors.Wrap(err, "error saving ID file")
|
|
|
|
}
|
|
|
|
} else if err != nil {
|
|
|
|
return "", errors.Wrapf(err, "error loading ID file %s", idPath)
|
|
|
|
} else {
|
|
|
|
id = string(idb)
|
|
|
|
}
|
|
|
|
return id, nil
|
|
|
|
}
|