forked from denisbrodbeck/machineid
-
Notifications
You must be signed in to change notification settings - Fork 13
/
id_linux.go
59 lines (51 loc) · 1.86 KB
/
id_linux.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// +build linux
package machineid
import (
"os"
"path"
)
const (
// the environment variable name pointing to the machine id pathname
ENV_VARNAME = "MACHINE_ID_FILE"
// dbusPath is the default path for dbus machine id.
dbusPath = "/var/lib/dbus/machine-id"
// dbusPathEtc is the default path for dbus machine id located in /etc.
// Some systems (like Fedora 20) only know this path.
// Sometimes it's the other way round.
dbusPathEtc = "/etc/machine-id"
// this returns a random uuid each time it's read
linuxRandomUuid = "/proc/sys/kernel/random/uuid"
)
// machineID returns the uuid specified in the "canonical" locations. If not such value is found, one is generated and persisted.
// The machine id is looked in:
// - the file pointed by the `MACHINE_ID_FILE` env var
// - `/var/lib/dbus/machine-id`
// - `/etc/machine-id`
// - `$HOME/.config/machine-id`
//
// If no such file is found, a random uuid is generated and persisted in the first
// writable file among `$MACHINE_ID_FILE`, `/var/lib/dbus/machine-id`, `/etc/machine-id`, `$HOME/.config/machine-id`.
//
// If there is an error reading _all_ the files an empty string is returned.
// The logic implemented is a variation of the one described in https://github.com/denisbrodbeck/machineid/issues/5#issuecomment-523803164
// See also https://unix.stackexchange.com/questions/144812/generate-consistent-machine-unique-id
func machineID() (string, error) {
env_pathname := os.Getenv(ENV_VARNAME)
home := os.Getenv("HOME")
userMachineId := path.Join(home, ".config", "machine-id")
id, err := readFirstFile([]string{
env_pathname, dbusPath, dbusPathEtc, userMachineId,
})
if err != nil {
id, err = readFile(linuxRandomUuid)
if err == nil {
writeFirstFile([]string{
env_pathname, dbusPathEtc, dbusPath, userMachineId,
}, id)
}
}
if err != nil {
return "", err
}
return trim(string(id)), nil
}