Skip to content

Commit b6ca103

Browse files
committed
Fix for docker-archive-public#1737 - Better detect non default VirtualBox Installation Path
Signed-off-by: Jean-Laurent de Morlhon <jeanlaurent@morlhon.net>
1 parent 9775f9e commit b6ca103

File tree

5 files changed

+86
-46
lines changed

5 files changed

+86
-46
lines changed

drivers/virtualbox/vbm.go

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,8 @@ import (
44
"bytes"
55
"errors"
66
"fmt"
7-
"os"
87
"os/exec"
9-
"path/filepath"
108
"regexp"
11-
"runtime"
129
"strings"
1310

1411
"github.com/docker/machine/libmachine/log"
@@ -78,31 +75,3 @@ func (v *VBoxCmdManager) vbmOutErr(args ...string) (string, string, error) {
7875

7976
return stdout.String(), stderrStr, err
8077
}
81-
82-
// detectVBoxManageCmd detects the VBoxManage cmd's path if needed
83-
func detectVBoxManageCmd() string {
84-
cmd := "VBoxManage"
85-
if path, err := exec.LookPath(cmd); err == nil {
86-
return path
87-
}
88-
89-
if runtime.GOOS == "windows" {
90-
if p := os.Getenv("VBOX_INSTALL_PATH"); p != "" {
91-
if path, err := exec.LookPath(filepath.Join(p, cmd)); err == nil {
92-
return path
93-
}
94-
}
95-
if p := os.Getenv("VBOX_MSI_INSTALL_PATH"); p != "" {
96-
if path, err := exec.LookPath(filepath.Join(p, cmd)); err == nil {
97-
return path
98-
}
99-
}
100-
// look at HKEY_LOCAL_MACHINE\SOFTWARE\Oracle\VirtualBox\InstallDir
101-
p := "C:\\Program Files\\Oracle\\VirtualBox"
102-
if path, err := exec.LookPath(filepath.Join(p, cmd)); err == nil {
103-
return path
104-
}
105-
}
106-
107-
return cmd
108-
}

drivers/virtualbox/virtualbox.go

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -186,21 +186,6 @@ func (d *Driver) PreCreateCheck() error {
186186
return d.vbm()
187187
}
188188

189-
// cmdOutput runs a shell command and returns its output.
190-
func cmdOutput(name string, args ...string) (string, error) {
191-
cmd := exec.Command(name, args...)
192-
log.Debugf("COMMAND: %v %v", name, strings.Join(args, " "))
193-
194-
stdout, err := cmd.Output()
195-
if err != nil {
196-
return "", err
197-
}
198-
199-
log.Debugf("STDOUT:\n{\n%v}", string(stdout))
200-
201-
return string(stdout), nil
202-
}
203-
204189
// IsVTXDisabledInTheVM checks if VT-X is disabled in the started vm.
205190
func (d *Driver) IsVTXDisabledInTheVM() (bool, error) {
206191
logPath := filepath.Join(d.ResolveStorePath(d.MachineName), "Logs", "VBox.log")
@@ -845,3 +830,11 @@ func getRandomIPinSubnet(baseIP net.IP) (net.IP, error) {
845830

846831
return dhcpAddr, nil
847832
}
833+
834+
func detectVBoxManageCmdInPath() string {
835+
cmd := "VBoxManage"
836+
if path, err := exec.LookPath(cmd); err == nil {
837+
return path
838+
}
839+
return cmd
840+
}

drivers/virtualbox/virtualbox_darwin.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,7 @@ func (d *Driver) IsVTXDisabled() bool {
2020
disabled := !strings.Contains(features, "VMX")
2121
return disabled
2222
}
23+
24+
func detectVBoxManageCmd() string {
25+
return detectVBoxManageCmdInPath()
26+
}

drivers/virtualbox/virtualbox_linux.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,7 @@ package virtualbox
55
func (d *Driver) IsVTXDisabled() bool {
66
return false
77
}
8+
9+
func detectVBoxManageCmd() string {
10+
return detectVBoxManageCmdInPath()
11+
}

drivers/virtualbox/virtualbox_windows.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,13 @@ package virtualbox
33
import (
44
"strings"
55

6+
"fmt"
7+
"os"
8+
"os/exec"
9+
"path/filepath"
10+
611
"github.com/docker/machine/libmachine/log"
12+
"golang.org/x/sys/windows/registry"
713
)
814

915
// IsVTXDisabled checks if VT-X is disabled in the BIOS. If it is, the vm will fail to start.
@@ -19,3 +25,67 @@ func (d *Driver) IsVTXDisabled() bool {
1925
disabled := strings.Contains(output, "FALSE")
2026
return disabled
2127
}
28+
29+
// cmdOutput runs a shell command and returns its output.
30+
func cmdOutput(name string, args ...string) (string, error) {
31+
cmd := exec.Command(name, args...)
32+
log.Debugf("COMMAND: %v %v", name, strings.Join(args, " "))
33+
34+
stdout, err := cmd.Output()
35+
if err != nil {
36+
return "", err
37+
}
38+
39+
log.Debugf("STDOUT:\n{\n%v}", string(stdout))
40+
41+
return string(stdout), nil
42+
}
43+
44+
func detectVBoxManageCmd() string {
45+
cmd := "VBoxManage"
46+
if p := os.Getenv("VBOX_INSTALL_PATH"); p != "" {
47+
if path, err := exec.LookPath(filepath.Join(p, cmd)); err == nil {
48+
return path
49+
}
50+
}
51+
52+
if p := os.Getenv("VBOX_MSI_INSTALL_PATH"); p != "" {
53+
if path, err := exec.LookPath(filepath.Join(p, cmd)); err == nil {
54+
return path
55+
}
56+
}
57+
58+
// Look in default installation path for VirtualBox version > 5
59+
if path, err := exec.LookPath(filepath.Join("C:\\Program Files\\Oracle\\VirtualBox", cmd)); err == nil {
60+
return path
61+
}
62+
63+
// Look in windows registry
64+
if p, err := findVBoxInstallDirInRegistry(); err == nil {
65+
if path, err := exec.LookPath(filepath.Join(p, cmd)); err == nil {
66+
return path
67+
}
68+
}
69+
70+
return detectVBoxManageCmdInPath() //fallback to path
71+
}
72+
73+
func findVBoxInstallDirInRegistry() (string, error) {
74+
registryKey, err := registry.OpenKey(registry.LOCAL_MACHINE, `SOFTWARE\Oracle\VirtualBox`, registry.QUERY_VALUE)
75+
if err != nil {
76+
errorMessage := fmt.Sprintf("Can't find VirtualBox registry entries, is VirtualBox really installed properly? %s", err)
77+
log.Debugf(errorMessage)
78+
return nil, fmt.Errorf(errorMessage)
79+
}
80+
81+
defer registryKey.Close()
82+
83+
installDir, _, err := registryKey.GetStringValue("InstallDir")
84+
if err != nil {
85+
errorMessage := fmt.Sprintf("Can't find InstallDir registry key within VirtualBox registries entries, is VirtualBox really installed properly? %s", err)
86+
log.Debugf(errorMessage)
87+
return nil, fmt.Errorf(errorMessage)
88+
}
89+
90+
return installDir, nil
91+
}

0 commit comments

Comments
 (0)