2015-06-21 07:36:07 -04:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"runtime"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2016-08-07 07:26:27 -04:00
|
|
|
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/common/powershell"
|
|
|
|
"github.com/hashicorp/packer/common/powershell/hyperv"
|
2015-06-21 07:36:07 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2015-10-30 04:23:30 -04:00
|
|
|
func (d *HypervPS4Driver) IsOff(vmName string) (bool, error) {
|
|
|
|
return hyperv.IsOff(vmName)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *HypervPS4Driver) Uptime(vmName string) (uint64, error) {
|
|
|
|
return hyperv.Uptime(vmName)
|
|
|
|
}
|
|
|
|
|
2015-06-21 07:36:07 -04:00
|
|
|
// Start starts a VM specified by the name given.
|
|
|
|
func (d *HypervPS4Driver) Start(vmName string) error {
|
2015-06-27 17:36:39 -04:00
|
|
|
return hyperv.StartVirtualMachine(vmName)
|
2015-06-21 07:36:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Stop stops a VM specified by the name given.
|
|
|
|
func (d *HypervPS4Driver) Stop(vmName string) error {
|
2015-06-27 17:36:39 -04:00
|
|
|
return hyperv.StopVirtualMachine(vmName)
|
2015-06-21 07:36:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *HypervPS4Driver) Verify() error {
|
|
|
|
|
|
|
|
if err := d.verifyPSVersion(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := d.verifyPSHypervModule(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-12-22 09:59:25 -05:00
|
|
|
if err := d.verifyHypervPermissions(); err != nil {
|
2015-06-21 07:36:07 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get mac address for VM.
|
|
|
|
func (d *HypervPS4Driver) Mac(vmName string) (string, error) {
|
2015-06-22 17:26:06 -04:00
|
|
|
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
|
2015-06-21 07:36:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get ip address for mac address.
|
|
|
|
func (d *HypervPS4Driver) IpAddress(mac string) (string, error) {
|
2015-06-22 17:26:06 -04:00
|
|
|
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
|
2015-06-21 07:36:07 -04:00
|
|
|
}
|
|
|
|
|
2015-10-30 04:23:30 -04:00
|
|
|
// Get host name from ip address
|
|
|
|
func (d *HypervPS4Driver) GetHostName(ip string) (string, error) {
|
|
|
|
return powershell.GetHostName(ip)
|
|
|
|
}
|
|
|
|
|
2017-03-12 07:31:31 -04:00
|
|
|
func (d *HypervPS4Driver) GetVirtualMachineGeneration(vmName string) (uint, error) {
|
|
|
|
return hyperv.GetVirtualMachineGeneration(vmName)
|
|
|
|
}
|
|
|
|
|
2015-06-27 17:36:39 -04:00
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
|
2015-10-30 04:23:30 -04:00
|
|
|
// 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) 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)
|
|
|
|
}
|
|
|
|
|
2017-10-19 18:29:17 -04:00
|
|
|
func (d *HypervPS4Driver) AddVirtualMachineHardDrive(vmName string, vhdFile string, vhdName string, vhdSizeBytes int64, controllerType string) error {
|
|
|
|
return hyperv.AddVirtualMachineHardDiskDrive(vmName, vhdFile, vhdName, vhdSizeBytes, controllerType)
|
|
|
|
}
|
|
|
|
|
2017-10-12 06:35:31 -04:00
|
|
|
func (d *HypervPS4Driver) CreateVirtualMachine(vmName string, path string, harddrivePath string, vhdPath string, ram int64, diskSize int64, switchName string, generation uint, diffDisks bool) error {
|
|
|
|
return hyperv.CreateVirtualMachine(vmName, path, harddrivePath, vhdPath, ram, diskSize, switchName, generation, diffDisks)
|
2015-10-30 04:23:30 -04:00
|
|
|
}
|
|
|
|
|
2017-05-21 12:29:26 -04:00
|
|
|
func (d *HypervPS4Driver) CloneVirtualMachine(cloneFromVmxcPath string, cloneFromVmName string, cloneFromSnapshotName string, cloneAllSnapshots bool, vmName string, path string, harddrivePath string, ram int64, switchName string) error {
|
|
|
|
return hyperv.CloneVirtualMachine(cloneFromVmxcPath, cloneFromVmName, cloneFromSnapshotName, cloneAllSnapshots, vmName, path, harddrivePath, ram, switchName)
|
2017-03-12 07:31:31 -04:00
|
|
|
}
|
|
|
|
|
2015-10-30 04:23:30 -04:00
|
|
|
func (d *HypervPS4Driver) DeleteVirtualMachine(vmName string) error {
|
|
|
|
return hyperv.DeleteVirtualMachine(vmName)
|
|
|
|
}
|
|
|
|
|
2016-08-07 07:26:27 -04:00
|
|
|
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) error {
|
|
|
|
return hyperv.SetVirtualMachineSecureBoot(vmName, enable)
|
2015-10-30 04:23:30 -04:00
|
|
|
}
|
|
|
|
|
2016-08-07 07:26:27 -04:00
|
|
|
func (d *HypervPS4Driver) SetVirtualMachineVirtualizationExtensions(vmName string, enable bool) error {
|
|
|
|
return hyperv.SetVirtualMachineVirtualizationExtensions(vmName, enable)
|
2015-10-30 04:23:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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) CompactDisks(expPath string, vhdDir string) error {
|
|
|
|
return hyperv.CompactDisks(expPath, vhdDir)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *HypervPS4Driver) CopyExportedVirtualMachine(expPath string, outputPath string, vhdDir string, vmDir string) error {
|
|
|
|
return hyperv.CopyExportedVirtualMachine(expPath, outputPath, vhdDir, vmDir)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *HypervPS4Driver) RestartVirtualMachine(vmName string) error {
|
|
|
|
return hyperv.RestartVirtualMachine(vmName)
|
|
|
|
}
|
|
|
|
|
2015-12-05 06:03:33 -05:00
|
|
|
func (d *HypervPS4Driver) CreateDvdDrive(vmName string, isoPath string, generation uint) (uint, uint, error) {
|
|
|
|
return hyperv.CreateDvdDrive(vmName, isoPath, generation)
|
2015-10-30 04:23:30 -04:00
|
|
|
}
|
|
|
|
|
2015-11-01 11:00:56 -05:00
|
|
|
func (d *HypervPS4Driver) MountDvdDrive(vmName string, path string, controllerNumber uint, controllerLocation uint) error {
|
|
|
|
return hyperv.MountDvdDrive(vmName, path, controllerNumber, controllerLocation)
|
2015-10-30 04:23:30 -04:00
|
|
|
}
|
|
|
|
|
2015-11-07 08:42:26 -05:00
|
|
|
func (d *HypervPS4Driver) SetBootDvdDrive(vmName string, controllerNumber uint, controllerLocation uint, generation uint) error {
|
|
|
|
return hyperv.SetBootDvdDrive(vmName, controllerNumber, controllerLocation, generation)
|
2015-10-30 13:19:25 -04:00
|
|
|
}
|
|
|
|
|
2015-11-01 11:00:56 -05:00
|
|
|
func (d *HypervPS4Driver) UnmountDvdDrive(vmName string, controllerNumber uint, controllerLocation uint) error {
|
|
|
|
return hyperv.UnmountDvdDrive(vmName, controllerNumber, controllerLocation)
|
2015-10-30 04:23:30 -04:00
|
|
|
}
|
|
|
|
|
2015-10-30 13:19:25 -04:00
|
|
|
func (d *HypervPS4Driver) DeleteDvdDrive(vmName string, controllerNumber uint, controllerLocation uint) error {
|
2015-10-30 04:23:30 -04:00
|
|
|
return hyperv.DeleteDvdDrive(vmName, controllerNumber, controllerLocation)
|
|
|
|
}
|
|
|
|
|
2015-10-30 13:19:25 -04:00
|
|
|
func (d *HypervPS4Driver) MountFloppyDrive(vmName string, path string) error {
|
|
|
|
return hyperv.MountFloppyDrive(vmName, path)
|
|
|
|
}
|
|
|
|
|
2015-10-30 04:23:30 -04:00
|
|
|
func (d *HypervPS4Driver) UnmountFloppyDrive(vmName string) error {
|
|
|
|
return hyperv.UnmountFloppyDrive(vmName)
|
|
|
|
}
|
|
|
|
|
2015-06-21 07:36:07 -04:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2017-03-28 21:47:10 -04:00
|
|
|
versionOutput := strings.TrimSpace(cmdOut)
|
2015-06-21 07:36:07 -04:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2017-10-25 12:24:06 -04:00
|
|
|
if powershell.IsFalse(cmdOut) {
|
2015-06-21 07:36:07 -04:00
|
|
|
err := fmt.Errorf("%s", "PS Hyper-V module is not loaded. Make sure Hyper-V feature is on.")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-08-28 16:36:29 -04:00
|
|
|
func (d *HypervPS4Driver) isCurrentUserAHyperVAdministrator() (bool, error) {
|
2017-06-27 19:44:04 -04:00
|
|
|
//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
|
2017-08-28 16:36:29 -04:00
|
|
|
|
|
|
|
var script = `
|
|
|
|
$identity = [System.Security.Principal.WindowsIdentity]::GetCurrent()
|
|
|
|
$principal = new-object System.Security.Principal.WindowsPrincipal($identity)
|
2017-10-25 12:25:12 -04:00
|
|
|
$hypervrole = [System.Security.Principal.SecurityIdentifier]"S-1-5-32-578"
|
2017-08-28 16:36:29 -04:00
|
|
|
return $principal.IsInRole($hypervrole)
|
|
|
|
`
|
2015-06-21 07:36:07 -04:00
|
|
|
|
2016-12-22 09:59:25 -05:00
|
|
|
var ps powershell.PowerShellCmd
|
2017-08-28 16:36:29 -04:00
|
|
|
cmdOut, err := ps.Output(script)
|
2016-12-22 09:59:25 -05:00
|
|
|
if err != nil {
|
2017-08-28 16:36:29 -04:00
|
|
|
return false, err
|
2015-06-21 07:36:07 -04:00
|
|
|
}
|
|
|
|
|
2017-10-25 12:24:06 -04:00
|
|
|
return powershell.IsTrue(cmdOut), nil
|
2017-08-28 16:36:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2016-12-22 09:59:25 -05:00
|
|
|
|
|
|
|
isAdmin, _ := powershell.IsCurrentUserAnAdministrator()
|
|
|
|
|
|
|
|
if !isAdmin {
|
|
|
|
err := fmt.Errorf("%s", "Current user is not a member of 'Hyper-V Administrators' or 'Administrators' group")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-21 07:36:07 -04:00
|
|
|
return nil
|
|
|
|
}
|