github.com/rothwerx/packer@v0.9.0/builder/vmware/common/driver_player5_windows.go (about) 1 // +build windows 2 3 package common 4 5 import ( 6 "log" 7 "os" 8 "os/exec" 9 "path/filepath" 10 "syscall" 11 ) 12 13 func playerFindVdiskManager() (string, error) { 14 path, err := exec.LookPath("vmware-vdiskmanager.exe") 15 if err == nil { 16 return path, nil 17 } 18 19 return findFile("vmware-vdiskmanager.exe", playerProgramFilePaths()), nil 20 } 21 22 func playerFindQemuImg() (string, error) { 23 path, err := exec.LookPath("qemu-img.exe") 24 if err == nil { 25 return path, nil 26 } 27 28 return findFile("qemu-img.exe", playerProgramFilePaths()), nil 29 } 30 31 func playerFindVMware() (string, error) { 32 path, err := exec.LookPath("vmplayer.exe") 33 if err == nil { 34 return path, nil 35 } 36 37 return findFile("vmplayer.exe", playerProgramFilePaths()), nil 38 } 39 40 func playerFindVmrun() (string, error) { 41 path, err := exec.LookPath("vmrun.exe") 42 if err == nil { 43 return path, nil 44 } 45 46 return findFile("vmrun.exe", playerProgramFilePaths()), nil 47 } 48 49 func playerToolsIsoPath(flavor string) string { 50 return findFile(flavor+".iso", playerProgramFilePaths()) 51 } 52 53 func playerDhcpLeasesPath(device string) string { 54 path, err := playerDhcpLeasesPathRegistry() 55 if err != nil { 56 log.Printf("Error finding leases in registry: %s", err) 57 } else if _, err := os.Stat(path); err == nil { 58 return path 59 } 60 61 return findFile("vmnetdhcp.leases", playerDataFilePaths()) 62 } 63 64 func playerVmnetnatConfPath() string { 65 return findFile("vmnetnat.conf", playerDataFilePaths()) 66 } 67 68 // This reads the VMware installation path from the Windows registry. 69 func playerVMwareRoot() (s string, err error) { 70 key := `SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\vmplayer.exe` 71 subkey := "Path" 72 s, err = readRegString(syscall.HKEY_LOCAL_MACHINE, key, subkey) 73 if err != nil { 74 log.Printf(`Unable to read registry key %s\%s`, key, subkey) 75 return 76 } 77 78 return normalizePath(s), nil 79 } 80 81 // This reads the VMware DHCP leases path from the Windows registry. 82 func playerDhcpLeasesPathRegistry() (s string, err error) { 83 key := "SYSTEM\\CurrentControlSet\\services\\VMnetDHCP\\Parameters" 84 subkey := "LeaseFile" 85 s, err = readRegString(syscall.HKEY_LOCAL_MACHINE, key, subkey) 86 if err != nil { 87 log.Printf(`Unable to read registry key %s\%s`, key, subkey) 88 return 89 } 90 91 return normalizePath(s), nil 92 } 93 94 // playerProgramFilesPaths returns a list of paths that are eligible 95 // to contain program files we may want just as vmware.exe. 96 func playerProgramFilePaths() []string { 97 path, err := playerVMwareRoot() 98 if err != nil { 99 log.Printf("Error finding VMware root: %s", err) 100 } 101 102 paths := make([]string, 0, 5) 103 if os.Getenv("VMWARE_HOME") != "" { 104 paths = append(paths, os.Getenv("VMWARE_HOME")) 105 } 106 107 if path != "" { 108 paths = append(paths, path) 109 } 110 111 if os.Getenv("ProgramFiles(x86)") != "" { 112 paths = append(paths, 113 filepath.Join(os.Getenv("ProgramFiles(x86)"), "/VMware/VMware Player")) 114 } 115 116 if os.Getenv("ProgramFiles") != "" { 117 paths = append(paths, 118 filepath.Join(os.Getenv("ProgramFiles"), "/VMware/VMware Player")) 119 } 120 121 if os.Getenv("QEMU_HOME") != "" { 122 paths = append(paths, os.Getenv("QEMU_HOME")) 123 } 124 125 if os.Getenv("ProgramFiles(x86)") != "" { 126 paths = append(paths, 127 filepath.Join(os.Getenv("ProgramFiles(x86)"), "/QEMU")) 128 } 129 130 if os.Getenv("ProgramFiles") != "" { 131 paths = append(paths, 132 filepath.Join(os.Getenv("ProgramFiles"), "/QEMU")) 133 } 134 135 if os.Getenv("SystemDrive") != "" { 136 paths = append(paths, 137 filepath.Join(os.Getenv("SystemDrive"), "/QEMU")) 138 } 139 140 return paths 141 } 142 143 // playerDataFilePaths returns a list of paths that are eligible 144 // to contain data files we may want such as vmnet NAT configuration files. 145 func playerDataFilePaths() []string { 146 leasesPath, err := playerDhcpLeasesPathRegistry() 147 if err != nil { 148 log.Printf("Error getting DHCP leases path: %s", err) 149 } 150 151 if leasesPath != "" { 152 leasesPath = filepath.Dir(leasesPath) 153 } 154 155 paths := make([]string, 0, 5) 156 if os.Getenv("VMWARE_DATA") != "" { 157 paths = append(paths, os.Getenv("VMWARE_DATA")) 158 } 159 160 if leasesPath != "" { 161 paths = append(paths, leasesPath) 162 } 163 164 if os.Getenv("ProgramData") != "" { 165 paths = append(paths, 166 filepath.Join(os.Getenv("ProgramData"), "/VMware")) 167 } 168 169 if os.Getenv("ALLUSERSPROFILE") != "" { 170 paths = append(paths, 171 filepath.Join(os.Getenv("ALLUSERSPROFILE"), "/Application Data/VMware")) 172 } 173 174 return paths 175 }