forked from denisbrodbeck/machineid
-
Notifications
You must be signed in to change notification settings - Fork 1
/
id_bsd.go
42 lines (36 loc) · 855 Bytes
/
id_bsd.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
// +build freebsd netbsd openbsd dragonfly solaris
package machineid
import (
"bytes"
"os"
)
const hostidPath = "/etc/hostid"
// machineID returns the uuid specified at `/etc/hostid`.
// If the returned value is empty, the uuid from a call to `kenv -q smbios.system.uuid` is returned.
// If there is an error an empty string is returned.
func machineID() (string, error) {
id, err := readHostid()
if err != nil {
// try fallback
id, err = readKenv()
}
if err != nil {
return "", err
}
return id, nil
}
func readHostid() (string, error) {
buf, err := readFile(hostidPath)
if err != nil {
return "", err
}
return trim(string(buf)), nil
}
func readKenv() (string, error) {
buf := &bytes.Buffer{}
err := run(buf, os.Stderr, "kenv", "-q", "smbios.system.uuid")
if err != nil {
return "", err
}
return trim(buf.String()), nil
}