-
Notifications
You must be signed in to change notification settings - Fork 11
/
hardware.go
59 lines (52 loc) · 1.21 KB
/
hardware.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
package neco
import (
"errors"
"os"
"os/exec"
"strings"
)
// HardwareType represents
type HardwareType int
// hardware type
const (
HWTypeNil HardwareType = iota
HWTypeVM
HWTypeContainer
HWTypeDell
)
// DetectHardware detects hardware type.
func DetectHardware() (HardwareType, error) {
t, err := systemdDetectVirt()
if err != nil {
return HWTypeNil, err
}
switch t {
case "qemu", "kvm", "zvm", "vmware", "microsoft", "oracle", "xen", "bochs", "uml", "parallels", "bhyve":
// VM
return HWTypeVM, nil
case "openvz", "lxc", "lxc-libvirt", "systemd-nspawn", "docker":
// Container
return HWTypeContainer, nil
case "none":
default:
return HWTypeNil, errors.New("unsupported hardware type: " + t)
}
vendorBytes, err := os.ReadFile("/sys/class/dmi/id/chassis_vendor")
if err != nil {
return HWTypeNil, err
}
vendor := strings.TrimSpace(string(vendorBytes))
switch vendor {
case "Dell Inc.":
return HWTypeDell, nil
}
return HWTypeNil, errors.New("unsupported hardware vendor: " + vendor)
}
func systemdDetectVirt() (string, error) {
out, err := exec.Command("systemd-detect-virt").Output()
t := strings.TrimSpace(string(out))
if err != nil && t != "none" {
return "", err
}
return t, nil
}