Skip to content
This repository was archived by the owner on Sep 26, 2021. It is now read-only.

Commit 5a8ce1a

Browse files
jdashin-
authored andcommitted
use CPUID instead of shelling out for VT-d detection
Signed-off-by: Jade Auer <[email protected]>
1 parent ff19455 commit 5a8ce1a

14 files changed

+1468
-215
lines changed

drivers/virtualbox/virtualbox_darwin.go

-22
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,5 @@
11
package virtualbox
22

3-
import (
4-
"strings"
5-
"syscall"
6-
7-
"github.com/docker/machine/libmachine/log"
8-
)
9-
10-
// IsVTXDisabled checks if VT-X is disabled in the BIOS. If it is, the vm will fail to start.
11-
// If we can't be sure it is disabled, we carry on and will check the vm logs after it's started.
12-
func (d *Driver) IsVTXDisabled() bool {
13-
features, err := syscall.Sysctl("machdep.cpu.features")
14-
if err != nil {
15-
log.Debugf("Couldn't check that VT-X/AMD-v is enabled. Will check that the vm is properly created: %v", err)
16-
return false
17-
}
18-
return isVTXDisabled(features)
19-
}
20-
21-
func isVTXDisabled(features string) bool {
22-
return !strings.Contains(features, "VMX")
23-
}
24-
253
func detectVBoxManageCmd() string {
264
return detectVBoxManageCmdInPath()
275
}

drivers/virtualbox/virtualbox_darwin_test.go

-10
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,10 @@ import (
66
"github.com/stretchr/testify/assert"
77
)
88

9-
const (
10-
featuresWithVMX = "FPU VME DE PSE TSC MSR PAE MCE CX8 APIC SEP MTRR PGE MCA CMOV PAT PSE36 VMX PBE SSE3 PCLMULQDQ DTES64 AVX1.0 RDRAND F16C"
11-
featuresNoVMX = "FPU VME DE PSE TSC MSR PAE MCE CX8 APIC SEP MTRR PGE MCA CMOV PAT PSE36 PBE SSE3 PCLMULQDQ DTES64 AVX1.0 RDRAND F16C"
12-
)
13-
149
func TestShareName(t *testing.T) {
1510
name, dir := getShareDriveAndName()
1611

1712
assert.Equal(t, name, "Users")
1813
assert.Equal(t, dir, "/Users")
1914

2015
}
21-
22-
func TestIsVTXEnabled(t *testing.T) {
23-
assert.False(t, isVTXDisabled(featuresWithVMX))
24-
assert.True(t, isVTXDisabled(featuresNoVMX))
25-
}

drivers/virtualbox/virtualbox_freebsd.go

-32
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,5 @@
11
package virtualbox
22

3-
import (
4-
"bytes"
5-
"io/ioutil"
6-
7-
"github.com/docker/machine/libmachine/log"
8-
)
9-
10-
// IsVTXDisabled checks if VT-X is disabled in the BIOS. If it is, the vm will fail to start.
11-
// If we can't be sure it is disabled, we carry on and will check the vm logs after it's started.
12-
// We want to check that either vmx or svm flags are present in /proc/cpuinfo.
13-
func (d *Driver) IsVTXDisabled() bool {
14-
cpuinfo, err := ioutil.ReadFile("/proc/cpuinfo")
15-
if err != nil {
16-
log.Debugf("Couldn't check that VT-X/AMD-v is enabled. Will check that the vm is properly created: %v", err)
17-
return false
18-
}
19-
return isVTXDisabled(cpuinfo)
20-
}
21-
22-
func isVTXDisabled(cpuinfo []byte) bool {
23-
features := [2][]byte{
24-
{'v', 'm', 'x'},
25-
{'s', 'v', 'm'},
26-
}
27-
for _, v := range features {
28-
if bytes.Contains(cpuinfo, v) {
29-
return false
30-
}
31-
}
32-
return true
33-
}
34-
353
func detectVBoxManageCmd() string {
364
return detectVBoxManageCmdInPath()
375
}

drivers/virtualbox/virtualbox_linux.go

-32
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,5 @@
11
package virtualbox
22

3-
import (
4-
"bytes"
5-
"io/ioutil"
6-
7-
"github.com/docker/machine/libmachine/log"
8-
)
9-
10-
// IsVTXDisabled checks if VT-X is disabled in the BIOS. If it is, the vm will fail to start.
11-
// If we can't be sure it is disabled, we carry on and will check the vm logs after it's started.
12-
// We want to check that either vmx or svm flags are present in /proc/cpuinfo.
13-
func (d *Driver) IsVTXDisabled() bool {
14-
cpuinfo, err := ioutil.ReadFile("/proc/cpuinfo")
15-
if err != nil {
16-
log.Debugf("Couldn't check that VT-X/AMD-v is enabled. Will check that the vm is properly created: %v", err)
17-
return false
18-
}
19-
return isVTXDisabled(cpuinfo)
20-
}
21-
22-
func isVTXDisabled(cpuinfo []byte) bool {
23-
features := [2][]byte{
24-
{'v', 'm', 'x'},
25-
{'s', 'v', 'm'},
26-
}
27-
for _, v := range features {
28-
if bytes.Contains(cpuinfo, v) {
29-
return false
30-
}
31-
}
32-
return true
33-
}
34-
353
func detectVBoxManageCmd() string {
364
return detectVBoxManageCmdInPath()
375
}

drivers/virtualbox/virtualbox_linux_test.go

-101
Original file line numberDiff line numberDiff line change
@@ -6,110 +6,9 @@ import (
66
"github.com/stretchr/testify/assert"
77
)
88

9-
const (
10-
amdCPUInfo = `
11-
processor : 0
12-
vendor_id : AuthenticAMD
13-
cpu family : 20
14-
model : 1
15-
model name : AMD C-50 Processor
16-
stepping : 0
17-
microcode : 0x5000026
18-
cpu MHz : 800.000
19-
cache size : 512 KB
20-
physical id : 0
21-
siblings : 2
22-
core id : 0
23-
cpu cores : 2
24-
apicid : 0
25-
initial apicid : 0
26-
fpu : yes
27-
fpu_exception : yes
28-
cpuid level : 6
29-
wp : yes
30-
flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall nx mmxext fxsr_opt pdpe1gb rdtscp lm constant_tsc rep_good nopl nonstop_tsc extd_apicid aperfmperf pni monitor ssse3 cx16 popcnt lahf_lm cmp_legacy svm extapic cr8_legacy abm sse4a misalignsse 3dnowprefetch ibs skinit wdt arat hw_pstate npt lbrv svm_lock nrip_save pausefilter vmmcall
31-
bugs : fxsave_leak sysret_ss_attrs
32-
bogomips : 1995.09
33-
TLB size : 1024 4K pages
34-
clflush size : 64
35-
cache_alignment : 64
36-
address sizes : 36 bits physical, 48 bits virtual
37-
power management: ts ttp tm stc 100mhzsteps hwpstate
38-
`
39-
intelCPUInfo = `
40-
processor : 0
41-
vendor_id : GenuineIntel
42-
cpu family : 6
43-
model : 70
44-
model name : Intel(R) Core(TM) i7-4850HQ CPU @ 2.30GHz
45-
stepping : 1
46-
microcode : 0x19
47-
cpu MHz : 2294.688
48-
cache size : 6144 KB
49-
physical id : 0
50-
siblings : 1
51-
core id : 0
52-
cpu cores : 1
53-
apicid : 0
54-
initial apicid : 0
55-
fpu : yes
56-
fpu_exception : yes
57-
cpuid level : 13
58-
wp : yes
59-
flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr vmx pge mca cmov pat pse36 clflush mmx fxsr sse sse2 syscall nx rdtscp lm constant_tsc rep_good nopl xtopology nonstop_tsc pni pclmulqdq monitor ssse3 cx16 sse4_1 sse4_2 movbe popcnt aes xsave avx rdrand hypervisor lahf_lm abm
60-
bugs :
61-
bogomips : 4589.37
62-
clflush size : 64
63-
cache_alignment : 64
64-
address sizes : 39 bits physical, 48 bits virtual
65-
power management:
66-
`
67-
68-
faultyCPUInfo = `
69-
processor : 0
70-
vendor_id : GenuineIntel
71-
cpu family : 6
72-
model : 70
73-
model name : Intel(R) Core(TM) i7-4850HQ CPU @ 2.30GHz
74-
stepping : 1
75-
microcode : 0x19
76-
cpu MHz : 2294.688
77-
cache size : 6144 KB
78-
physical id : 0
79-
siblings : 1
80-
core id : 0
81-
cpu cores : 1
82-
apicid : 0
83-
initial apicid : 0
84-
fpu : yes
85-
fpu_exception : yes
86-
cpuid level : 13
87-
wp : yes
88-
flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 syscall nx rdtscp lm constant_tsc rep_good nopl xtopology nonstop_tsc pni pclmulqdq monitor ssse3 cx16 sse4_1 sse4_2 movbe popcnt aes xsave avx rdrand hypervisor lahf_lm abm
89-
bugs :
90-
bogomips : 4589.37
91-
clflush size : 64
92-
cache_alignment : 64
93-
address sizes : 39 bits physical, 48 bits virtual
94-
power management:
95-
`
96-
)
97-
989
func TestShareName(t *testing.T) {
9910
name, dir := getShareDriveAndName()
10011

10112
assert.Equal(t, name, "hosthome")
10213
assert.Equal(t, dir, "/home")
10314
}
104-
105-
func TestCpuInfoOnAMD(t *testing.T) {
106-
assert.False(t, isVTXDisabled([]byte(amdCPUInfo)))
107-
}
108-
109-
func TestCpuInfoOnIntel(t *testing.T) {
110-
assert.False(t, isVTXDisabled([]byte(intelCPUInfo)))
111-
}
112-
113-
func TestCpuInfoOnNone(t *testing.T) {
114-
assert.True(t, isVTXDisabled([]byte(faultyCPUInfo)))
115-
}

drivers/virtualbox/virtualbox_openbsd.go

-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
package virtualbox
22

3-
func (d *Driver) IsVTXDisabled() bool {
4-
return false
5-
}
6-
73
func detectVBoxManageCmd() string {
84
return detectVBoxManageCmdInPath()
95
}

drivers/virtualbox/virtualbox_windows.go

-14
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,6 @@ import (
1212
"golang.org/x/sys/windows/registry"
1313
)
1414

15-
// IsVTXDisabled checks if VT-X is disabled in the BIOS. If it is, the vm will fail to start.
16-
// If we can't be sure it is disabled, we carry on and will check the vm logs after it's started.
17-
func (d *Driver) IsVTXDisabled() bool {
18-
errmsg := "Couldn't check that VT-X/AMD-v is enabled. Will check that the vm is properly created: %v"
19-
output, err := cmdOutput("wmic", "cpu", "get", "VirtualizationFirmwareEnabled")
20-
if err != nil {
21-
log.Debugf(errmsg, err)
22-
return false
23-
}
24-
25-
disabled := strings.Contains(output, "FALSE")
26-
return disabled
27-
}
28-
2915
// cmdOutput runs a shell command and returns its output.
3016
func cmdOutput(name string, args ...string) (string, error) {
3117
cmd := exec.Command(name, args...)

drivers/virtualbox/vtx_intel.go

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// +build 386 amd64
2+
3+
package virtualbox
4+
5+
import "github.com/intel-go/cpuid"
6+
7+
// IsVTXDisabled checks if VT-x is disabled in the CPU.
8+
func (d *Driver) IsVTXDisabled() bool {
9+
if cpuid.HasFeature(cpuid.VMX) || cpuid.HasFeature(cpuid.SVM) {
10+
return false
11+
}
12+
13+
return true
14+
}

drivers/virtualbox/vtx_other.go

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// +build !386,!amd64
2+
3+
package virtualbox
4+
5+
// IsVTXDisabled checks if VT-x is disabled in the CPU.
6+
func (d *Driver) IsVTXDisabled() bool {
7+
return true
8+
}

vendor/github.com/intel-go/cpuid/.gitignore

+28
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/intel-go/cpuid/LICENSE

+28
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)