forked from clearcontainers/runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cc-check.go
300 lines (244 loc) · 7.15 KB
/
cc-check.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
// Copyright (c) 2017 Intel Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"fmt"
"os/exec"
"path/filepath"
"regexp"
"strings"
vc "github.com/containers/virtcontainers"
"github.com/sirupsen/logrus"
"github.com/urfave/cli"
)
type kernelModule struct {
// description
desc string
// maps parameter names to values
parameters map[string]string
}
const (
moduleParamDir = "parameters"
cpuFlagsTag = "flags"
successMessage = "System is capable of running " + project
failMessage = "System is not capable of running " + project
kernelPropertyCorrect = "Kernel property value correct"
)
// variables rather than consts to allow tests to modify them
var (
procCPUInfo = "/proc/cpuinfo"
sysModuleDir = "/sys/module"
modInfoCmd = "modinfo"
)
// requiredCPUFlags maps a CPU flag value to search for and a
// human-readable description of that value.
var requiredCPUFlags = map[string]string{
"vmx": "Virtualization support",
"lm": "64Bit CPU",
"sse4_1": "SSE4.1",
}
// requiredCPUAttribs maps a CPU (non-CPU flag) attribute value to search for
// and a human-readable description of that value.
var requiredCPUAttribs = map[string]string{
"GenuineIntel": "Intel Architecture CPU",
}
// requiredKernelModules maps a required module name to a human-readable
// description of the modules functionality and an optional list of
// required module parameters.
var requiredKernelModules = map[string]kernelModule{
"kvm": {
desc: "Kernel-based Virtual Machine",
},
"kvm_intel": {
desc: "Intel KVM",
parameters: map[string]string{
"nested": "Y",
"unrestricted_guest": "Y",
},
},
"vhost": {
desc: "Host kernel accelerator for virtio",
},
"vhost_net": {
desc: "Host kernel accelerator for virtio network",
},
}
// getCPUInfo returns details of the first CPU
func getCPUInfo(cpuInfoFile string) (string, error) {
text, err := getFileContents(cpuInfoFile)
if err != nil {
return "", err
}
cpus := strings.SplitAfter(text, "\n\n")
trimmed := strings.TrimSpace(cpus[0])
if trimmed == "" {
return "", fmt.Errorf("Cannot determine CPU details")
}
return trimmed, nil
}
func findAnchoredString(haystack, needle string) bool {
if haystack == "" || needle == "" {
return false
}
// Ensure the search string is anchored
pattern := regexp.MustCompile(`\b` + needle + `\b`)
return pattern.MatchString(haystack)
}
func getCPUFlags(cpuinfo string) string {
for _, line := range strings.Split(cpuinfo, "\n") {
if strings.HasPrefix(line, cpuFlagsTag) {
fields := strings.Split(line, ":")
if len(fields) == 2 {
return strings.TrimSpace(fields[1])
}
}
}
return ""
}
func haveKernelModule(module string) bool {
// First, check to see if the module is already loaded
path := filepath.Join(sysModuleDir, module)
if fileExists(path) {
return true
}
// Now, check if the module is unloaded, but available
cmd := exec.Command(modInfoCmd, module)
err := cmd.Run()
return err == nil
}
// checkCPU checks all required CPU attributes modules and returns a count of
// the number of CPU attribute errors (all of which are logged by this
// function). Only fatal errors result in an error return.
func checkCPU(tag, cpuinfo string, attribs map[string]string) (count uint32, err error) {
if cpuinfo == "" {
return 0, fmt.Errorf("Need cpuinfo")
}
for attrib, desc := range attribs {
fields := logrus.Fields{
"type": tag,
"name": attrib,
"description": desc,
}
found := findAnchoredString(cpuinfo, attrib)
if !found {
ccLog.WithFields(fields).Errorf("CPU property not found")
count++
continue
}
ccLog.WithFields(fields).Infof("CPU property found")
}
return count, nil
}
func checkCPUFlags(cpuflags string, required map[string]string) (uint32, error) {
return checkCPU("flag", cpuflags, required)
}
func checkCPUAttribs(cpuinfo string, attribs map[string]string) (uint32, error) {
return checkCPU("attribute", cpuinfo, attribs)
}
// checkKernelModules checks all required kernel modules modules and returns a count of
// the number of module errors (all of which are logged by this
// function). Only fatal errors result in an error return.
func checkKernelModules(modules map[string]kernelModule) (count uint32, err error) {
onVMM, err := vc.RunningOnVMM(procCPUInfo)
if err != nil {
return 0, err
}
for module, details := range modules {
fields := logrus.Fields{
"type": "module",
"name": module,
"description": details.desc,
}
if !haveKernelModule(module) {
ccLog.WithFields(fields).Error("kernel property not found")
count++
continue
}
ccLog.WithFields(fields).Infof("kernel property found")
for param, expected := range details.parameters {
path := filepath.Join(sysModuleDir, module, moduleParamDir, param)
value, err := getFileContents(path)
if err != nil {
return 0, err
}
value = strings.TrimRight(value, "\n\r")
if value != expected {
fields["expected"] = expected
fields["actual"] = value
fields["parameter"] = param
msg := "kernel module parameter has unexpected value"
// this option is not required when
// already running under a hypervisor.
if param == "unrestricted_guest" && onVMM {
ccLog.WithFields(fields).Warn(kernelPropertyCorrect)
continue
}
if param == "nested" {
ccLog.WithFields(fields).Warn(msg)
continue
}
ccLog.WithFields(fields).Error(msg)
count++
}
ccLog.WithFields(fields).Info(kernelPropertyCorrect)
}
}
return count, nil
}
// hostIsClearContainersCapable determines if the system is capable of
// running Clear Containers.
func hostIsClearContainersCapable(cpuinfoFile string) error {
cpuinfo, err := getCPUInfo(cpuinfoFile)
if err != nil {
return err
}
cpuFlags := getCPUFlags(cpuinfo)
if cpuFlags == "" {
return fmt.Errorf("Cannot find CPU flags")
}
// Keep a track of the error count, but don't error until all tests
// have been performed!
errorCount := uint32(0)
count, err := checkCPUAttribs(cpuinfo, requiredCPUAttribs)
if err != nil {
return err
}
errorCount += count
count, err = checkCPUFlags(cpuFlags, requiredCPUFlags)
if err != nil {
return err
}
errorCount += count
count, err = checkKernelModules(requiredKernelModules)
if err != nil {
return err
}
errorCount += count
if errorCount == 0 {
return nil
}
return fmt.Errorf("ERROR: %s", failMessage)
}
var ccCheckCLICommand = cli.Command{
Name: "cc-check",
Usage: "tests if system can run " + project,
Action: func(context *cli.Context) error {
err := hostIsClearContainersCapable(procCPUInfo)
if err != nil {
return err
}
ccLog.Info(successMessage)
return nil
},
}