-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathhardware.go
159 lines (141 loc) · 3.02 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package hardware
import (
"encoding/json"
"io/ioutil"
"sort"
"github.com/jouyouyun/hardware/cpu"
"github.com/jouyouyun/hardware/disk"
hdisk "github.com/jouyouyun/hardware/disk"
hdmi "github.com/jouyouyun/hardware/dmi"
"github.com/jouyouyun/hardware/network"
"github.com/jouyouyun/hardware/utils"
)
const (
etcMachineIDFile = "/etc/machine-id"
)
var (
_mid string
IncludeDiskInfo bool
)
// GenMachineID generate this machine's id
func GenMachineID() (string, error) {
if len(_mid) != 0 {
return _mid, nil
}
var (
mid string
err error
dmi *hdmi.DMI
disks disk.DiskList
root *disk.Disk
)
dmi, err = hdmi.GetDMI()
if !IncludeDiskInfo && err == nil && dmi.IsValid() {
mid, err = genMachineIDWithDMI(*dmi)
if len(mid) != 0 {
goto out
}
} else if dmi == nil {
dmi = &hdmi.DMI{}
}
// if dmi product uuid null or IncludeDiskInfo is true, generate machine id with root disk serial
disks, err = hdisk.GetDiskList()
if err == nil {
root = disks.GetRoot()
if root != nil && len(root.Serial) != 0 {
mid, err = genMachineIDWithDisk(*dmi, root)
if len(mid) != 0 {
goto out
}
}
}
mid, err = genMachineIDWithNet(*dmi)
if len(mid) != 0 {
goto out
}
mid, err = genMachineIDWithFile(etcMachineIDFile)
out:
_mid = mid
return mid, err
}
func genMachineIDWithDMI(dmi hdmi.DMI) (string, error) {
// bios info maybe changed after upgraded
dmi.BiosDate = ""
dmi.BiosVendor = ""
dmi.BiosVersion = ""
return doGenMachineID(&dmi)
}
func genMachineIDWithDisk(dmi hdmi.DMI, disk *hdisk.Disk) (string, error) {
// bios info maybe changed after upgraded
dmi.BiosDate = ""
dmi.BiosVendor = ""
dmi.BiosVersion = ""
var info = struct {
hdmi.DMI
DiskSerial string
}{
DMI: dmi,
DiskSerial: disk.Serial,
}
return doGenMachineID(&info)
}
func doGenMachineID(info interface{}) (string, error) {
data, err := json.Marshal(info)
if err != nil {
return "", err
}
return utils.SHA256Sum(data), nil
}
func genMachineIDWithNet(dmi hdmi.DMI) (string, error) {
cpuInfo, err := cpu.NewCPU()
if err != nil {
return "", err
}
netInfo, err := network.GetNetworkList()
if err != nil {
return "", err
}
var (
plist []string
vlist []string
alist []string
)
for _, info := range netInfo {
plist = append(plist, info.Product)
vlist = append(vlist, info.Vendor)
alist = append(alist, info.Address)
}
sort.Strings(plist)
sort.Strings(vlist)
sort.Strings(alist)
// bios info maybe changed after upgraded
dmi.BiosDate = ""
dmi.BiosVendor = ""
dmi.BiosVersion = ""
var info = struct {
hdmi.DMI
CPU string
ProductList []string
VendorList []string
AddressList []string
}{
DMI: dmi,
CPU: cpuInfo.Name,
ProductList: plist,
VendorList: vlist,
AddressList: alist,
}
return doGenMachineID(&info)
}
func genMachineIDWithFile(filename string) (string, error) {
contents, err := ioutil.ReadFile(filename)
if err != nil {
return "", err
}
var info = struct {
ID string
}{
ID: string(contents),
}
return doGenMachineID(&info)
}