/
driver_ps_4.go
376 lines (287 loc) · 11.1 KB
/
driver_ps_4.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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
package common
import (
"context"
"fmt"
"log"
"runtime"
"strconv"
"strings"
"github.com/hashicorp/packer/common/powershell"
"github.com/hashicorp/packer/common/powershell/hyperv"
)
type HypervPS4Driver struct {
}
func NewHypervPS4Driver() (Driver, error) {
appliesTo := "Applies to Windows 8.1, Windows PowerShell 4.0, Windows Server 2012 R2 only"
// Check this is Windows
if runtime.GOOS != "windows" {
err := fmt.Errorf("%s", appliesTo)
return nil, err
}
ps4Driver := &HypervPS4Driver{}
if err := ps4Driver.Verify(); err != nil {
return nil, err
}
return ps4Driver, nil
}
func (d *HypervPS4Driver) IsRunning(vmName string) (bool, error) {
return hyperv.IsRunning(vmName)
}
func (d *HypervPS4Driver) IsOff(vmName string) (bool, error) {
return hyperv.IsOff(vmName)
}
func (d *HypervPS4Driver) Uptime(vmName string) (uint64, error) {
return hyperv.Uptime(vmName)
}
// Start starts a VM specified by the name given.
func (d *HypervPS4Driver) Start(vmName string) error {
return hyperv.StartVirtualMachine(vmName)
}
// Stop stops a VM specified by the name given.
func (d *HypervPS4Driver) Stop(vmName string) error {
return hyperv.StopVirtualMachine(vmName)
}
func (d *HypervPS4Driver) Verify() error {
if err := d.verifyPSVersion(); err != nil {
return err
}
if err := d.verifyPSHypervModule(); err != nil {
return err
}
if err := d.verifyHypervPermissions(); err != nil {
return err
}
return nil
}
// Get mac address for VM.
func (d *HypervPS4Driver) Mac(vmName string) (string, error) {
res, err := hyperv.Mac(vmName)
if err != nil {
return res, err
}
if res == "" {
err := fmt.Errorf("%s", "No mac address.")
return res, err
}
return res, err
}
// Get ip address for mac address.
func (d *HypervPS4Driver) IpAddress(mac string) (string, error) {
res, err := hyperv.IpAddress(mac)
if err != nil {
return res, err
}
if res == "" {
err := fmt.Errorf("%s", "No ip address.")
return res, err
}
return res, err
}
// Get host name from ip address
func (d *HypervPS4Driver) GetHostName(ip string) (string, error) {
return powershell.GetHostName(ip)
}
func (d *HypervPS4Driver) GetVirtualMachineGeneration(vmName string) (uint, error) {
return hyperv.GetVirtualMachineGeneration(vmName)
}
// Finds the IP address of a host adapter connected to switch
func (d *HypervPS4Driver) GetHostAdapterIpAddressForSwitch(switchName string) (string, error) {
res, err := hyperv.GetHostAdapterIpAddressForSwitch(switchName)
if err != nil {
return res, err
}
if res == "" {
err := fmt.Errorf("%s", "No ip address.")
return res, err
}
return res, err
}
// Type scan codes to virtual keyboard of vm
func (d *HypervPS4Driver) TypeScanCodes(vmName string, scanCodes string) error {
return hyperv.TypeScanCodes(vmName, scanCodes)
}
// Get network adapter address
func (d *HypervPS4Driver) GetVirtualMachineNetworkAdapterAddress(vmName string) (string, error) {
return hyperv.GetVirtualMachineNetworkAdapterAddress(vmName)
}
//Set the vlan to use for switch
func (d *HypervPS4Driver) SetNetworkAdapterVlanId(switchName string, vlanId string) error {
return hyperv.SetNetworkAdapterVlanId(switchName, vlanId)
}
//Set the vlan to use for machine
func (d *HypervPS4Driver) SetVirtualMachineVlanId(vmName string, vlanId string) error {
return hyperv.SetVirtualMachineVlanId(vmName, vlanId)
}
func (d *HypervPS4Driver) SetVmNetworkAdapterMacAddress(vmName string, mac string) error {
return hyperv.SetVmNetworkAdapterMacAddress(vmName, mac)
}
func (d *HypervPS4Driver) UntagVirtualMachineNetworkAdapterVlan(vmName string, switchName string) error {
return hyperv.UntagVirtualMachineNetworkAdapterVlan(vmName, switchName)
}
func (d *HypervPS4Driver) CreateExternalVirtualSwitch(vmName string, switchName string) error {
return hyperv.CreateExternalVirtualSwitch(vmName, switchName)
}
func (d *HypervPS4Driver) GetVirtualMachineSwitchName(vmName string) (string, error) {
return hyperv.GetVirtualMachineSwitchName(vmName)
}
func (d *HypervPS4Driver) ConnectVirtualMachineNetworkAdapterToSwitch(vmName string, switchName string) error {
return hyperv.ConnectVirtualMachineNetworkAdapterToSwitch(vmName, switchName)
}
func (d *HypervPS4Driver) DeleteVirtualSwitch(switchName string) error {
return hyperv.DeleteVirtualSwitch(switchName)
}
func (d *HypervPS4Driver) CreateVirtualSwitch(switchName string, switchType string) (bool, error) {
return hyperv.CreateVirtualSwitch(switchName, switchType)
}
func (d *HypervPS4Driver) AddVirtualMachineHardDrive(vmName string, vhdFile string, vhdName string,
vhdSizeBytes int64, diskBlockSize int64, controllerType string) error {
return hyperv.AddVirtualMachineHardDiskDrive(vmName, vhdFile, vhdName, vhdSizeBytes,
diskBlockSize, controllerType)
}
func (d *HypervPS4Driver) CreateVirtualMachine(vmName string, path string, harddrivePath string, ram int64,
diskSize int64, diskBlockSize int64, switchName string, generation uint, diffDisks bool,
fixedVHD bool) error {
return hyperv.CreateVirtualMachine(vmName, path, harddrivePath, ram, diskSize, diskBlockSize, switchName,
generation, diffDisks, fixedVHD)
}
func (d *HypervPS4Driver) CloneVirtualMachine(cloneFromVmcxPath string, cloneFromVmName string,
cloneFromSnapshotName string, cloneAllSnapshots bool, vmName string, path string, harddrivePath string,
ram int64, switchName string) error {
return hyperv.CloneVirtualMachine(cloneFromVmcxPath, cloneFromVmName, cloneFromSnapshotName,
cloneAllSnapshots, vmName, path, harddrivePath, ram, switchName)
}
func (d *HypervPS4Driver) DeleteVirtualMachine(vmName string) error {
return hyperv.DeleteVirtualMachine(vmName)
}
func (d *HypervPS4Driver) SetVirtualMachineCpuCount(vmName string, cpu uint) error {
return hyperv.SetVirtualMachineCpuCount(vmName, cpu)
}
func (d *HypervPS4Driver) SetVirtualMachineMacSpoofing(vmName string, enable bool) error {
return hyperv.SetVirtualMachineMacSpoofing(vmName, enable)
}
func (d *HypervPS4Driver) SetVirtualMachineDynamicMemory(vmName string, enable bool) error {
return hyperv.SetVirtualMachineDynamicMemory(vmName, enable)
}
func (d *HypervPS4Driver) SetVirtualMachineSecureBoot(vmName string, enable bool, templateName string) error {
return hyperv.SetVirtualMachineSecureBoot(vmName, enable, templateName)
}
func (d *HypervPS4Driver) SetVirtualMachineVirtualizationExtensions(vmName string, enable bool) error {
return hyperv.SetVirtualMachineVirtualizationExtensions(vmName, enable)
}
func (d *HypervPS4Driver) EnableVirtualMachineIntegrationService(vmName string,
integrationServiceName string) error {
return hyperv.EnableVirtualMachineIntegrationService(vmName, integrationServiceName)
}
func (d *HypervPS4Driver) ExportVirtualMachine(vmName string, path string) error {
return hyperv.ExportVirtualMachine(vmName, path)
}
func (d *HypervPS4Driver) PreserveLegacyExportBehaviour(srcPath string, dstPath string) error {
return hyperv.PreserveLegacyExportBehaviour(srcPath, dstPath)
}
func (d *HypervPS4Driver) MoveCreatedVHDsToOutputDir(srcPath string, dstPath string) error {
return hyperv.MoveCreatedVHDsToOutputDir(srcPath, dstPath)
}
func (d *HypervPS4Driver) CompactDisks(path string) (result string, err error) {
return hyperv.CompactDisks(path)
}
func (d *HypervPS4Driver) RestartVirtualMachine(vmName string) error {
return hyperv.RestartVirtualMachine(vmName)
}
func (d *HypervPS4Driver) CreateDvdDrive(vmName string, isoPath string, generation uint) (uint, uint, error) {
return hyperv.CreateDvdDrive(vmName, isoPath, generation)
}
func (d *HypervPS4Driver) MountDvdDrive(vmName string, path string, controllerNumber uint,
controllerLocation uint) error {
return hyperv.MountDvdDrive(vmName, path, controllerNumber, controllerLocation)
}
func (d *HypervPS4Driver) SetBootDvdDrive(vmName string, controllerNumber uint, controllerLocation uint,
generation uint) error {
return hyperv.SetBootDvdDrive(vmName, controllerNumber, controllerLocation, generation)
}
func (d *HypervPS4Driver) UnmountDvdDrive(vmName string, controllerNumber uint, controllerLocation uint) error {
return hyperv.UnmountDvdDrive(vmName, controllerNumber, controllerLocation)
}
func (d *HypervPS4Driver) DeleteDvdDrive(vmName string, controllerNumber uint, controllerLocation uint) error {
return hyperv.DeleteDvdDrive(vmName, controllerNumber, controllerLocation)
}
func (d *HypervPS4Driver) MountFloppyDrive(vmName string, path string) error {
return hyperv.MountFloppyDrive(vmName, path)
}
func (d *HypervPS4Driver) UnmountFloppyDrive(vmName string) error {
return hyperv.UnmountFloppyDrive(vmName)
}
func (d *HypervPS4Driver) verifyPSVersion() error {
log.Printf("Enter method: %s", "verifyPSVersion")
// check PS is available and is of proper version
versionCmd := "$host.version.Major"
var ps powershell.PowerShellCmd
cmdOut, err := ps.Output(versionCmd)
if err != nil {
return err
}
versionOutput := strings.TrimSpace(cmdOut)
log.Printf("%s output: %s", versionCmd, versionOutput)
ver, err := strconv.ParseInt(versionOutput, 10, 32)
if err != nil {
return err
}
if ver < 4 {
err := fmt.Errorf("%s", "Windows PowerShell version 4.0 or higher is expected")
return err
}
return nil
}
func (d *HypervPS4Driver) verifyPSHypervModule() error {
log.Printf("Enter method: %s", "verifyPSHypervModule")
versionCmd := "function foo(){try{ $commands = Get-Command -Module Hyper-V;if($commands.Length -eq 0){return $false} }catch{return $false}; return $true} foo"
var ps powershell.PowerShellCmd
cmdOut, err := ps.Output(versionCmd)
if err != nil {
return err
}
if powershell.IsFalse(cmdOut) {
err := fmt.Errorf("%s", "PS Hyper-V module is not loaded. Make sure Hyper-V feature is on.")
return err
}
return nil
}
func (d *HypervPS4Driver) isCurrentUserAHyperVAdministrator() (bool, error) {
//SID:S-1-5-32-578 = 'BUILTIN\Hyper-V Administrators'
//https://support.microsoft.com/en-us/help/243330/well-known-security-identifiers-in-windows-operating-systems
var script = `
$identity = [System.Security.Principal.WindowsIdentity]::GetCurrent()
$principal = new-object System.Security.Principal.WindowsPrincipal($identity)
$hypervrole = [System.Security.Principal.SecurityIdentifier]"S-1-5-32-578"
return $principal.IsInRole($hypervrole)
`
var ps powershell.PowerShellCmd
cmdOut, err := ps.Output(script)
if err != nil {
return false, err
}
return powershell.IsTrue(cmdOut), nil
}
func (d *HypervPS4Driver) verifyHypervPermissions() error {
log.Printf("Enter method: %s", "verifyHypervPermissions")
hyperVAdmin, err := d.isCurrentUserAHyperVAdministrator()
if err != nil {
log.Printf("Error discovering if current is is a Hyper-V Admin: %s", err)
}
if !hyperVAdmin {
isAdmin, _ := powershell.IsCurrentUserAnAdministrator()
if !isAdmin {
err := fmt.Errorf("%s", "Current user is not a member of 'Hyper-V Administrators' or 'Administrators' group")
return err
}
}
return nil
}
// Connect connects to a VM specified by the name given.
func (d *HypervPS4Driver) Connect(vmName string) (context.CancelFunc, error) {
return hyperv.ConnectVirtualMachine(vmName)
}
// Disconnect disconnects to a VM specified by calling the context cancel function returned
// from Connect.
func (d *HypervPS4Driver) Disconnect(cancel context.CancelFunc) {
hyperv.DisconnectVirtualMachine(cancel)
}