1// +build freebsd netbsd openbsd dragonfly solaris
2
3package machineid
4
5import (
6	"bytes"
7	"os"
8)
9
10const hostidPath = "/etc/hostid"
11
12// machineID returns the uuid specified at `/etc/hostid`.
13// If the returned value is empty, the uuid from a call to `kenv -q smbios.system.uuid` is returned.
14// If there is an error an empty string is returned.
15func machineID() (string, error) {
16	id, err := readHostid()
17	if err != nil {
18		// try fallback
19		id, err = readKenv()
20	}
21	if err != nil {
22		return "", err
23	}
24	return id, nil
25}
26
27func readHostid() (string, error) {
28	buf, err := readFile(hostidPath)
29	if err != nil {
30		return "", err
31	}
32	return trim(string(buf)), nil
33}
34
35func readKenv() (string, error) {
36	buf := &bytes.Buffer{}
37	err := run(buf, os.Stderr, "kenv", "-q", "smbios.system.uuid")
38	if err != nil {
39		return "", err
40	}
41	return trim(buf.String()), nil
42}
43