forked from denisbrodbeck/machineid
-
Notifications
You must be signed in to change notification settings - Fork 1
/
id_linux.go
36 lines (32 loc) · 1014 Bytes
/
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
//go:build linux
// +build linux
package machineid
const (
// 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"
// Some old release haven't above two paths.
// Workaround.
productPath = "/sys/class/dmi/id/product_uuid"
)
// machineID returns the uuid specified at `/var/lib/dbus/machine-id` or `/etc/machine-id`.
// If there is an error reading the files an empty string is returned.
// See https://unix.stackexchange.com/questions/144812/generate-consistent-machine-unique-id
func machineID() (string, error) {
id, err := readFile(dbusPath)
if err != nil {
// try fallback path
id, err = readFile(dbusPathEtc)
}
if err != nil {
// try fallback path
id, err = readFile(productPath)
}
if err != nil {
return "", err
}
return trim(string(id)), nil
}