2013-07-27 16:00:21 -04:00
|
|
|
// +build windows
|
|
|
|
|
2013-12-24 00:58:41 -05:00
|
|
|
package iso
|
2013-07-27 16:00:21 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
2013-07-27 17:59:23 -04:00
|
|
|
"syscall"
|
2013-07-27 16:00:21 -04:00
|
|
|
"unsafe"
|
|
|
|
)
|
|
|
|
|
2013-07-31 15:36:17 -04:00
|
|
|
func workstationCheckLicense() error {
|
|
|
|
// Not implemented on Windows
|
2013-07-27 16:00:21 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-07-31 15:36:17 -04:00
|
|
|
func workstationFindVdiskManager() (string, error) {
|
2013-08-09 20:31:43 -04:00
|
|
|
path, err := exec.LookPath("vmware-vdiskmanager.exe")
|
|
|
|
if err == nil {
|
2013-07-31 15:36:17 -04:00
|
|
|
return path, nil
|
2013-07-27 16:00:21 -04:00
|
|
|
}
|
|
|
|
|
2013-08-09 20:31:43 -04:00
|
|
|
return findFile("vmware-vdiskmanager.exe", workstationProgramFilePaths()), nil
|
2013-07-27 16:00:21 -04:00
|
|
|
}
|
|
|
|
|
2013-07-31 15:36:17 -04:00
|
|
|
func workstationFindVMware() (string, error) {
|
2013-08-12 17:01:50 -04:00
|
|
|
path, err := exec.LookPath("vmware.exe")
|
2013-08-09 20:31:43 -04:00
|
|
|
if err == nil {
|
2013-07-31 15:36:17 -04:00
|
|
|
return path, nil
|
2013-07-27 16:00:21 -04:00
|
|
|
}
|
|
|
|
|
2013-08-09 20:31:43 -04:00
|
|
|
return findFile("vmware.exe", workstationProgramFilePaths()), nil
|
2013-07-27 16:00:21 -04:00
|
|
|
}
|
|
|
|
|
2013-07-31 15:36:17 -04:00
|
|
|
func workstationFindVmrun() (string, error) {
|
2013-08-12 17:01:50 -04:00
|
|
|
path, err := exec.LookPath("vmrun.exe")
|
2013-08-09 20:31:43 -04:00
|
|
|
if err == nil {
|
2013-07-31 15:36:17 -04:00
|
|
|
return path, nil
|
2013-07-27 16:00:21 -04:00
|
|
|
}
|
|
|
|
|
2013-08-09 20:31:43 -04:00
|
|
|
return findFile("vmrun.exe", workstationProgramFilePaths()), nil
|
2013-07-27 16:00:21 -04:00
|
|
|
}
|
|
|
|
|
2013-07-31 15:36:17 -04:00
|
|
|
func workstationToolsIsoPath(flavor string) string {
|
2013-08-12 17:01:50 -04:00
|
|
|
return findFile(flavor+".iso", workstationProgramFilePaths())
|
2013-07-27 16:00:21 -04:00
|
|
|
}
|
|
|
|
|
2013-07-31 15:36:17 -04:00
|
|
|
func workstationDhcpLeasesPath(device string) string {
|
2013-08-09 20:31:43 -04:00
|
|
|
path, err := workstationDhcpLeasesPathRegistry()
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Error finding leases in registry: %s", err)
|
|
|
|
} else if _, err := os.Stat(path); err == nil {
|
2013-08-02 02:55:56 -04:00
|
|
|
return path
|
2013-07-27 16:00:21 -04:00
|
|
|
}
|
|
|
|
|
2013-08-12 17:01:50 -04:00
|
|
|
return findFile("vmnetdhcp.leases", workstationDataFilePaths())
|
2013-08-09 20:31:43 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func workstationVmnetnatConfPath() string {
|
2013-08-12 17:01:50 -04:00
|
|
|
return findFile("vmnetnat.conf", workstationDataFilePaths())
|
2013-07-27 16:00:21 -04:00
|
|
|
}
|
|
|
|
|
2013-07-31 15:36:17 -04:00
|
|
|
// See http://blog.natefinch.com/2012/11/go-win-stuff.html
|
|
|
|
//
|
|
|
|
// This is used by workstationVMwareRoot in order to read some registry data.
|
2013-07-27 16:00:21 -04:00
|
|
|
func readRegString(hive syscall.Handle, subKeyPath, valueName string) (value string, err error) {
|
2013-07-27 17:59:23 -04:00
|
|
|
var h syscall.Handle
|
|
|
|
err = syscall.RegOpenKeyEx(hive, syscall.StringToUTF16Ptr(subKeyPath), 0, syscall.KEY_READ, &h)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer syscall.RegCloseKey(h)
|
|
|
|
|
|
|
|
var typ uint32
|
|
|
|
var bufSize uint32
|
|
|
|
err = syscall.RegQueryValueEx(
|
|
|
|
h,
|
|
|
|
syscall.StringToUTF16Ptr(valueName),
|
|
|
|
nil,
|
|
|
|
&typ,
|
|
|
|
nil,
|
|
|
|
&bufSize)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
data := make([]uint16, bufSize/2+1)
|
|
|
|
err = syscall.RegQueryValueEx(
|
|
|
|
h,
|
|
|
|
syscall.StringToUTF16Ptr(valueName),
|
|
|
|
nil,
|
|
|
|
&typ,
|
|
|
|
(*byte)(unsafe.Pointer(&data[0])),
|
|
|
|
&bufSize)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
return syscall.UTF16ToString(data), nil
|
2013-07-27 16:00:21 -04:00
|
|
|
}
|
|
|
|
|
2013-07-31 15:36:17 -04:00
|
|
|
// This reads the VMware installation path from the Windows registry.
|
|
|
|
func workstationVMwareRoot() (s string, err error) {
|
|
|
|
key := `SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\vmware.exe`
|
2013-07-27 16:00:21 -04:00
|
|
|
subkey := "Path"
|
2013-07-31 15:36:17 -04:00
|
|
|
s, err = readRegString(syscall.HKEY_LOCAL_MACHINE, key, subkey)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf(`Unable to read registry key %s\%s`, key, subkey)
|
|
|
|
return
|
2013-07-27 16:00:21 -04:00
|
|
|
}
|
2013-07-31 15:36:17 -04:00
|
|
|
|
2013-08-02 02:55:56 -04:00
|
|
|
return normalizePath(s), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// This reads the VMware DHCP leases path from the Windows registry.
|
2013-08-09 20:31:43 -04:00
|
|
|
func workstationDhcpLeasesPathRegistry() (s string, err error) {
|
2013-08-02 02:55:56 -04:00
|
|
|
key := "SYSTEM\\CurrentControlSet\\services\\VMnetDHCP\\Parameters"
|
|
|
|
subkey := "LeaseFile"
|
|
|
|
s, err = readRegString(syscall.HKEY_LOCAL_MACHINE, key, subkey)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf(`Unable to read registry key %s\%s`, key, subkey)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
return normalizePath(s), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func normalizePath(path string) string {
|
|
|
|
path = strings.Replace(path, "\\", "/", -1)
|
|
|
|
path = strings.Replace(path, "//", "/", -1)
|
|
|
|
path = strings.TrimRight(path, "/")
|
|
|
|
return path
|
|
|
|
}
|
|
|
|
|
2013-08-09 20:31:43 -04:00
|
|
|
func findFile(file string, paths []string) string {
|
|
|
|
for _, path := range paths {
|
|
|
|
path = filepath.Join(path, file)
|
2013-08-02 02:55:56 -04:00
|
|
|
path = normalizePath(path)
|
|
|
|
log.Printf("Searching for file '%s'", path)
|
|
|
|
|
2013-08-12 19:13:06 -04:00
|
|
|
if _, err := os.Stat(path); err == nil {
|
2013-08-02 02:55:56 -04:00
|
|
|
log.Printf("Found file '%s'", path)
|
|
|
|
return path
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("File not found: '%s'", file)
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2013-08-09 20:31:43 -04:00
|
|
|
// workstationProgramFilesPaths returns a list of paths that are eligible
|
|
|
|
// to contain program files we may want just as vmware.exe.
|
|
|
|
func workstationProgramFilePaths() []string {
|
|
|
|
path, err := workstationVMwareRoot()
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Error finding VMware root: %s", err)
|
|
|
|
}
|
2013-08-02 02:55:56 -04:00
|
|
|
|
2013-08-09 20:31:43 -04:00
|
|
|
paths := make([]string, 0, 5)
|
|
|
|
if os.Getenv("VMWARE_HOME") != "" {
|
|
|
|
paths = append(paths, os.Getenv("VMWARE_HOME"))
|
2013-08-02 02:55:56 -04:00
|
|
|
}
|
|
|
|
|
2013-08-09 20:31:43 -04:00
|
|
|
if path != "" {
|
|
|
|
paths = append(paths, path)
|
|
|
|
}
|
|
|
|
|
|
|
|
if os.Getenv("ProgramFiles(x86)") != "" {
|
|
|
|
paths = append(paths,
|
|
|
|
filepath.Join(os.Getenv("ProgramFiles(x86)"), "/VMware/VMware Workstation"))
|
|
|
|
}
|
|
|
|
|
|
|
|
if os.Getenv("ProgramFiles") != "" {
|
|
|
|
paths = append(paths,
|
|
|
|
filepath.Join(os.Getenv("ProgramFiles"), "/VMware/VMware Workstation"))
|
|
|
|
}
|
|
|
|
|
|
|
|
return paths
|
2013-08-02 02:55:56 -04:00
|
|
|
}
|
|
|
|
|
2013-08-09 20:31:43 -04:00
|
|
|
// workstationDataFilePaths returns a list of paths that are eligible
|
|
|
|
// to contain data files we may want such as vmnet NAT configuration files.
|
|
|
|
func workstationDataFilePaths() []string {
|
2013-08-12 17:01:50 -04:00
|
|
|
leasesPath, err := workstationDhcpLeasesPathRegistry()
|
2013-08-09 20:31:43 -04:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("Error getting DHCP leases path: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if leasesPath != "" {
|
|
|
|
leasesPath = filepath.Dir(leasesPath)
|
|
|
|
}
|
2013-08-02 02:55:56 -04:00
|
|
|
|
2013-08-09 20:31:43 -04:00
|
|
|
paths := make([]string, 0, 5)
|
|
|
|
if os.Getenv("VMWARE_DATA") != "" {
|
|
|
|
paths = append(paths, os.Getenv("VMWARE_DATA"))
|
2013-08-02 02:55:56 -04:00
|
|
|
}
|
|
|
|
|
2013-08-12 17:01:50 -04:00
|
|
|
if leasesPath != "" {
|
2013-08-09 20:31:43 -04:00
|
|
|
paths = append(paths, leasesPath)
|
2013-08-02 02:55:56 -04:00
|
|
|
}
|
|
|
|
|
2013-08-09 20:31:43 -04:00
|
|
|
if os.Getenv("ProgramData") != "" {
|
|
|
|
paths = append(paths,
|
|
|
|
filepath.Join(os.Getenv("ProgramData"), "/VMware"))
|
|
|
|
}
|
2013-08-02 02:55:56 -04:00
|
|
|
|
2013-08-09 20:31:43 -04:00
|
|
|
if os.Getenv("ALLUSERSPROFILE") != "" {
|
|
|
|
paths = append(paths,
|
|
|
|
filepath.Join(os.Getenv("ALLUSERSPROFILE"), "/Application Data/VMware"))
|
|
|
|
}
|
2013-08-02 02:55:56 -04:00
|
|
|
|
2013-08-09 20:31:43 -04:00
|
|
|
return paths
|
2013-07-27 16:00:21 -04:00
|
|
|
}
|