Initial check in to add a builder that can clone existing hyper v machines
This commit is contained in:
parent
1e162ffd26
commit
6fd7f0877d
|
@ -66,8 +66,12 @@ type Driver interface {
|
|||
|
||||
CreateVirtualMachine(string, string, string, int64, int64, string, uint) error
|
||||
|
||||
CloneVirtualMachine(string, string, bool, string, string, int64, string) error
|
||||
|
||||
DeleteVirtualMachine(string) error
|
||||
|
||||
GetVirtualMachineGeneration(string) (uint, error)
|
||||
|
||||
SetVirtualMachineCpuCount(string, uint) error
|
||||
|
||||
SetVirtualMachineMacSpoofing(string, bool) error
|
||||
|
|
|
@ -107,6 +107,10 @@ 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)
|
||||
|
@ -170,6 +174,10 @@ func (d *HypervPS4Driver) CreateVirtualMachine(vmName string, path string, vhdPa
|
|||
return hyperv.CreateVirtualMachine(vmName, path, vhdPath, ram, diskSize, switchName, generation)
|
||||
}
|
||||
|
||||
func (d *HypervPS4Driver) CloneVirtualMachine(cloneFromVmName string, cloneFromSnapshotName string, cloneAllSnapshots bool, vmName string, path string, ram int64, switchName string) error {
|
||||
return hyperv.CloneVirtualMachine(cloneFromVmName, cloneFromSnapshotName, cloneAllSnapshots, vmName, path, ram, switchName)
|
||||
}
|
||||
|
||||
func (d *HypervPS4Driver) DeleteVirtualMachine(vmName string) error {
|
||||
return hyperv.DeleteVirtualMachine(vmName)
|
||||
}
|
||||
|
|
|
@ -0,0 +1,122 @@
|
|||
package common
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/mitchellh/multistep"
|
||||
"github.com/mitchellh/packer/packer"
|
||||
)
|
||||
|
||||
// This step clones an existing virtual machine.
|
||||
//
|
||||
// Produces:
|
||||
// VMName string - The name of the VM
|
||||
type StepCloneVM struct {
|
||||
CloneFromVMName string
|
||||
CloneFromSnapshotName string
|
||||
CloneAllSnapshots bool
|
||||
VMName string
|
||||
SwitchName string
|
||||
RamSize uint
|
||||
Cpu uint
|
||||
EnableMacSpoofing bool
|
||||
EnableDynamicMemory bool
|
||||
EnableSecureBoot bool
|
||||
EnableVirtualizationExtensions bool
|
||||
}
|
||||
|
||||
func (s *StepCloneVM) Run(state multistep.StateBag) multistep.StepAction {
|
||||
driver := state.Get("driver").(Driver)
|
||||
ui := state.Get("ui").(packer.Ui)
|
||||
ui.Say("Creating virtual machine...")
|
||||
|
||||
path := state.Get("packerTempDir").(string)
|
||||
|
||||
// convert the MB to bytes
|
||||
ramSize := int64(s.RamSize * 1024 * 1024)
|
||||
|
||||
err := driver.CloneVirtualMachine(s.CloneFromVMName, s.CloneFromSnapshotName, s.CloneAllSnapshots, s.VMName, path, ramSize, s.SwitchName)
|
||||
if err != nil {
|
||||
err := fmt.Errorf("Error cloning virtual machine: %s", err)
|
||||
state.Put("error", err)
|
||||
ui.Error(err.Error())
|
||||
return multistep.ActionHalt
|
||||
}
|
||||
|
||||
err = driver.SetVirtualMachineCpuCount(s.VMName, s.Cpu)
|
||||
if err != nil {
|
||||
err := fmt.Errorf("Error creating setting virtual machine cpu: %s", err)
|
||||
state.Put("error", err)
|
||||
ui.Error(err.Error())
|
||||
return multistep.ActionHalt
|
||||
}
|
||||
|
||||
if s.EnableDynamicMemory {
|
||||
err = driver.SetVirtualMachineDynamicMemory(s.VMName, s.EnableDynamicMemory)
|
||||
if err != nil {
|
||||
err := fmt.Errorf("Error creating setting virtual machine dynamic memory: %s", err)
|
||||
state.Put("error", err)
|
||||
ui.Error(err.Error())
|
||||
return multistep.ActionHalt
|
||||
}
|
||||
}
|
||||
|
||||
if s.EnableMacSpoofing {
|
||||
err = driver.SetVirtualMachineMacSpoofing(s.VMName, s.EnableMacSpoofing)
|
||||
if err != nil {
|
||||
err := fmt.Errorf("Error creating setting virtual machine mac spoofing: %s", err)
|
||||
state.Put("error", err)
|
||||
ui.Error(err.Error())
|
||||
return multistep.ActionHalt
|
||||
}
|
||||
}
|
||||
|
||||
generation, err := driver.GetVirtualMachineGeneration(s.VMName)
|
||||
if err != nil {
|
||||
err := fmt.Errorf("Error detecting vm generation: %s", err)
|
||||
state.Put("error", err)
|
||||
ui.Error(err.Error())
|
||||
return multistep.ActionHalt
|
||||
}
|
||||
|
||||
if generation == 2 {
|
||||
err = driver.SetVirtualMachineSecureBoot(s.VMName, s.EnableSecureBoot)
|
||||
if err != nil {
|
||||
err := fmt.Errorf("Error setting secure boot: %s", err)
|
||||
state.Put("error", err)
|
||||
ui.Error(err.Error())
|
||||
return multistep.ActionHalt
|
||||
}
|
||||
}
|
||||
|
||||
if s.EnableVirtualizationExtensions {
|
||||
//This is only supported on Windows 10 and Windows Server 2016 onwards
|
||||
err = driver.SetVirtualMachineVirtualizationExtensions(s.VMName, s.EnableVirtualizationExtensions)
|
||||
if err != nil {
|
||||
err := fmt.Errorf("Error creating setting virtual machine virtualization extensions: %s", err)
|
||||
state.Put("error", err)
|
||||
ui.Error(err.Error())
|
||||
return multistep.ActionHalt
|
||||
}
|
||||
}
|
||||
|
||||
// Set the final name in the state bag so others can use it
|
||||
state.Put("vmName", s.VMName)
|
||||
|
||||
return multistep.ActionContinue
|
||||
}
|
||||
|
||||
func (s *StepCloneVM) Cleanup(state multistep.StateBag) {
|
||||
if s.VMName == "" {
|
||||
return
|
||||
}
|
||||
|
||||
driver := state.Get("driver").(Driver)
|
||||
ui := state.Get("ui").(packer.Ui)
|
||||
ui.Say("Unregistering and deleting virtual machine...")
|
||||
|
||||
err := driver.DeleteVirtualMachine(s.VMName)
|
||||
if err != nil {
|
||||
ui.Error(fmt.Sprintf("Error deleting virtual machine: %s", err))
|
||||
}
|
||||
}
|
|
@ -0,0 +1,536 @@
|
|||
package vmcx
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/mitchellh/multistep"
|
||||
hypervcommon "github.com/mitchellh/packer/builder/hyperv/common"
|
||||
"github.com/mitchellh/packer/common"
|
||||
powershell "github.com/mitchellh/packer/common/powershell"
|
||||
"github.com/mitchellh/packer/common/powershell/hyperv"
|
||||
"github.com/mitchellh/packer/helper/communicator"
|
||||
"github.com/mitchellh/packer/helper/config"
|
||||
"github.com/mitchellh/packer/packer"
|
||||
"github.com/mitchellh/packer/template/interpolate"
|
||||
)
|
||||
|
||||
const (
|
||||
DefaultRamSize = 1 * 1024 // 1GB
|
||||
MinRamSize = 32 // 32MB
|
||||
MaxRamSize = 32 * 1024 // 32GB
|
||||
MinNestedVirtualizationRamSize = 4 * 1024 // 4GB
|
||||
|
||||
LowRam = 256 // 256MB
|
||||
|
||||
DefaultUsername = ""
|
||||
DefaultPassword = ""
|
||||
)
|
||||
|
||||
// Builder implements packer.Builder and builds the actual Hyperv
|
||||
// images.
|
||||
type Builder struct {
|
||||
config Config
|
||||
runner multistep.Runner
|
||||
}
|
||||
|
||||
type Config struct {
|
||||
common.PackerConfig `mapstructure:",squash"`
|
||||
common.HTTPConfig `mapstructure:",squash"`
|
||||
common.ISOConfig `mapstructure:",squash"`
|
||||
hypervcommon.FloppyConfig `mapstructure:",squash"`
|
||||
hypervcommon.OutputConfig `mapstructure:",squash"`
|
||||
hypervcommon.SSHConfig `mapstructure:",squash"`
|
||||
hypervcommon.RunConfig `mapstructure:",squash"`
|
||||
hypervcommon.ShutdownConfig `mapstructure:",squash"`
|
||||
|
||||
// The size, in megabytes, of the computer memory in the VM.
|
||||
// By default, this is 1024 (about 1 GB).
|
||||
RamSize uint `mapstructure:"ram_size"`
|
||||
// A list of files to place onto a floppy disk that is attached when the
|
||||
// VM is booted. This is most useful for unattended Windows installs,
|
||||
// which look for an Autounattend.xml file on removable media. By default,
|
||||
// no floppy will be attached. All files listed in this setting get
|
||||
// placed into the root directory of the floppy and the floppy is attached
|
||||
// as the first floppy device. Currently, no support exists for creating
|
||||
// sub-directories on the floppy. Wildcard characters (*, ?, and [])
|
||||
// are allowed. Directory names are also allowed, which will add all
|
||||
// the files found in the directory to the floppy.
|
||||
FloppyFiles []string `mapstructure:"floppy_files"`
|
||||
//
|
||||
SecondaryDvdImages []string `mapstructure:"secondary_iso_images"`
|
||||
|
||||
// Should integration services iso be mounted
|
||||
GuestAdditionsMode string `mapstructure:"guest_additions_mode"`
|
||||
|
||||
// The path to the integration services iso
|
||||
GuestAdditionsPath string `mapstructure:"guest_additions_path"`
|
||||
|
||||
// This is the name of the virtual machine to clone from.
|
||||
CloneFromVMName string `mapstructure:"clone_from_vm_name"`
|
||||
|
||||
// This is the name of the snapshot to clone from. A blank snapshot name will use the latest snapshot.
|
||||
CloneFromSnapshotName string `mapstructure:"clone_from_snapshot_name"`
|
||||
|
||||
// This will clone all snapshots if true. It will clone latest snapshot if false.
|
||||
CloneAllSnapshots bool `mapstructure:"clone_all_snapshots"`
|
||||
|
||||
// This is the name of the new virtual machine.
|
||||
// By default this is "packer-BUILDNAME", where "BUILDNAME" is the name of the build.
|
||||
VMName string `mapstructure:"vm_name"`
|
||||
|
||||
BootCommand []string `mapstructure:"boot_command"`
|
||||
SwitchName string `mapstructure:"switch_name"`
|
||||
SwitchVlanId string `mapstructure:"switch_vlan_id"`
|
||||
VlanId string `mapstructure:"vlan_id"`
|
||||
Cpu uint `mapstructure:"cpu"`
|
||||
Generation uint `mapstructure:"generation"`
|
||||
EnableMacSpoofing bool `mapstructure:"enable_mac_spoofing"`
|
||||
EnableDynamicMemory bool `mapstructure:"enable_dynamic_memory"`
|
||||
EnableSecureBoot bool `mapstructure:"enable_secure_boot"`
|
||||
EnableVirtualizationExtensions bool `mapstructure:"enable_virtualization_extensions"`
|
||||
|
||||
Communicator string `mapstructure:"communicator"`
|
||||
|
||||
SkipCompaction bool `mapstructure:"skip_compaction"`
|
||||
|
||||
ctx interpolate.Context
|
||||
}
|
||||
|
||||
// Prepare processes the build configuration parameters.
|
||||
func (b *Builder) Prepare(raws ...interface{}) ([]string, error) {
|
||||
err := config.Decode(&b.config, &config.DecodeOpts{
|
||||
Interpolate: true,
|
||||
InterpolateFilter: &interpolate.RenderFilter{
|
||||
Exclude: []string{
|
||||
"boot_command",
|
||||
},
|
||||
},
|
||||
}, raws...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Accumulate any errors and warnings
|
||||
var errs *packer.MultiError
|
||||
warnings := make([]string, 0)
|
||||
|
||||
isoWarnings, isoErrs := b.config.ISOConfig.Prepare(&b.config.ctx)
|
||||
warnings = append(warnings, isoWarnings...)
|
||||
errs = packer.MultiErrorAppend(errs, isoErrs...)
|
||||
|
||||
errs = packer.MultiErrorAppend(errs, b.config.FloppyConfig.Prepare(&b.config.ctx)...)
|
||||
errs = packer.MultiErrorAppend(errs, b.config.HTTPConfig.Prepare(&b.config.ctx)...)
|
||||
errs = packer.MultiErrorAppend(errs, b.config.RunConfig.Prepare(&b.config.ctx)...)
|
||||
errs = packer.MultiErrorAppend(errs, b.config.OutputConfig.Prepare(&b.config.ctx, &b.config.PackerConfig)...)
|
||||
errs = packer.MultiErrorAppend(errs, b.config.SSHConfig.Prepare(&b.config.ctx)...)
|
||||
errs = packer.MultiErrorAppend(errs, b.config.ShutdownConfig.Prepare(&b.config.ctx)...)
|
||||
|
||||
err = b.checkRamSize()
|
||||
if err != nil {
|
||||
errs = packer.MultiErrorAppend(errs, err)
|
||||
}
|
||||
|
||||
if b.config.VMName == "" {
|
||||
b.config.VMName = fmt.Sprintf("packer-%s", b.config.PackerBuildName)
|
||||
}
|
||||
|
||||
log.Println(fmt.Sprintf("%s: %v", "VMName", b.config.VMName))
|
||||
|
||||
if b.config.SwitchName == "" {
|
||||
b.config.SwitchName = b.detectSwitchName()
|
||||
}
|
||||
|
||||
if b.config.Cpu < 1 {
|
||||
b.config.Cpu = 1
|
||||
}
|
||||
|
||||
if b.config.Generation != 2 {
|
||||
b.config.Generation = 1
|
||||
}
|
||||
|
||||
if b.config.Generation == 2 {
|
||||
if len(b.config.FloppyFiles) > 0 {
|
||||
err = errors.New("Generation 2 vms don't support floppy drives. Use ISO image instead.")
|
||||
errs = packer.MultiErrorAppend(errs, err)
|
||||
}
|
||||
}
|
||||
|
||||
log.Println(fmt.Sprintf("Using switch %s", b.config.SwitchName))
|
||||
log.Println(fmt.Sprintf("%s: %v", "SwitchName", b.config.SwitchName))
|
||||
|
||||
// Errors
|
||||
if b.config.GuestAdditionsMode == "" {
|
||||
if b.config.GuestAdditionsPath != "" {
|
||||
b.config.GuestAdditionsMode = "attach"
|
||||
} else {
|
||||
b.config.GuestAdditionsPath = os.Getenv("WINDIR") + "\\system32\\vmguest.iso"
|
||||
|
||||
if _, err := os.Stat(b.config.GuestAdditionsPath); os.IsNotExist(err) {
|
||||
if err != nil {
|
||||
b.config.GuestAdditionsPath = ""
|
||||
b.config.GuestAdditionsMode = "none"
|
||||
} else {
|
||||
b.config.GuestAdditionsMode = "attach"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if b.config.GuestAdditionsPath == "" && b.config.GuestAdditionsMode == "attach" {
|
||||
b.config.GuestAdditionsPath = os.Getenv("WINDIR") + "\\system32\\vmguest.iso"
|
||||
|
||||
if _, err := os.Stat(b.config.GuestAdditionsPath); os.IsNotExist(err) {
|
||||
if err != nil {
|
||||
b.config.GuestAdditionsPath = ""
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for _, isoPath := range b.config.SecondaryDvdImages {
|
||||
if _, err := os.Stat(isoPath); os.IsNotExist(err) {
|
||||
if err != nil {
|
||||
errs = packer.MultiErrorAppend(
|
||||
errs, fmt.Errorf("Secondary Dvd image does not exist: %s", err))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
numberOfIsos := len(b.config.SecondaryDvdImages)
|
||||
|
||||
if b.config.GuestAdditionsMode == "attach" {
|
||||
if _, err := os.Stat(b.config.GuestAdditionsPath); os.IsNotExist(err) {
|
||||
if err != nil {
|
||||
errs = packer.MultiErrorAppend(
|
||||
errs, fmt.Errorf("Guest additions iso does not exist: %s", err))
|
||||
}
|
||||
}
|
||||
|
||||
numberOfIsos = numberOfIsos + 1
|
||||
}
|
||||
|
||||
if b.config.Generation < 2 && numberOfIsos > 2 {
|
||||
if b.config.GuestAdditionsMode == "attach" {
|
||||
errs = packer.MultiErrorAppend(errs, fmt.Errorf("There are only 2 ide controllers available, so we can't support guest additions and these secondary dvds: %s", strings.Join(b.config.SecondaryDvdImages, ", ")))
|
||||
} else {
|
||||
errs = packer.MultiErrorAppend(errs, fmt.Errorf("There are only 2 ide controllers available, so we can't support these secondary dvds: %s", strings.Join(b.config.SecondaryDvdImages, ", ")))
|
||||
}
|
||||
} else if b.config.Generation > 1 && len(b.config.SecondaryDvdImages) > 16 {
|
||||
if b.config.GuestAdditionsMode == "attach" {
|
||||
errs = packer.MultiErrorAppend(errs, fmt.Errorf("There are not enough drive letters available for scsi (limited to 16), so we can't support guest additions and these secondary dvds: %s", strings.Join(b.config.SecondaryDvdImages, ", ")))
|
||||
} else {
|
||||
errs = packer.MultiErrorAppend(errs, fmt.Errorf("There are not enough drive letters available for scsi (limited to 16), so we can't support these secondary dvds: %s", strings.Join(b.config.SecondaryDvdImages, ", ")))
|
||||
}
|
||||
}
|
||||
|
||||
if b.config.EnableVirtualizationExtensions {
|
||||
hasVirtualMachineVirtualizationExtensions, err := powershell.HasVirtualMachineVirtualizationExtensions()
|
||||
if err != nil {
|
||||
errs = packer.MultiErrorAppend(errs, fmt.Errorf("Failed detecting virtual machine virtualization extensions support: %s", err))
|
||||
} else {
|
||||
if !hasVirtualMachineVirtualizationExtensions {
|
||||
errs = packer.MultiErrorAppend(errs, fmt.Errorf("This version of Hyper-V does not support virtual machine virtualization extension. Please use Windows 10 or Windows Server 2016 or newer."))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
virtualMachineExists, err := powershell.DoesVirtualMachineExist(b.config.CloneFromVMName)
|
||||
if err != nil {
|
||||
errs = packer.MultiErrorAppend(errs, fmt.Errorf("Failed detecting if virtual machine to clone from exists: %s", err))
|
||||
} else {
|
||||
if !virtualMachineExists {
|
||||
errs = packer.MultiErrorAppend(errs, fmt.Errorf("Virtual machine '%s' to clone from does not exist.", b.config.CloneFromVMName))
|
||||
} else {
|
||||
if b.config.CloneFromSnapshotName != "" {
|
||||
virtualMachineSnapshotExists, err := powershell.DoesVirtualMachineSnapshotExist(b.config.CloneFromVMName, b.config.CloneFromSnapshotName)
|
||||
if err != nil {
|
||||
errs = packer.MultiErrorAppend(errs, fmt.Errorf("Failed detecting if virtual machine snapshot to clone from exists: %s", err))
|
||||
} else {
|
||||
if !virtualMachineSnapshotExists {
|
||||
errs = packer.MultiErrorAppend(errs, fmt.Errorf("Virtual machine snapshot '%s' on virtual machine '%s' to clone from does not exist.", b.config.CloneFromSnapshotName, b.config.CloneFromVMName))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
virtualMachineOn, err := powershell.IsVirtualMachineOn(b.config.CloneFromVMName)
|
||||
if err != nil {
|
||||
errs = packer.MultiErrorAppend(errs, fmt.Errorf("Failed detecting if virtual machine to clone is running: %s", err))
|
||||
} else {
|
||||
if virtualMachineOn {
|
||||
warning := fmt.Sprintf("Cloning from a virtual machine that is running.")
|
||||
warnings = appendWarnings(warnings, warning)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Warnings
|
||||
|
||||
if b.config.ShutdownCommand == "" {
|
||||
warnings = appendWarnings(warnings,
|
||||
"A shutdown_command was not specified. Without a shutdown command, Packer\n"+
|
||||
"will forcibly halt the virtual machine, which may result in data loss.")
|
||||
}
|
||||
|
||||
warning := b.checkHostAvailableMemory()
|
||||
if warning != "" {
|
||||
warnings = appendWarnings(warnings, warning)
|
||||
}
|
||||
|
||||
if b.config.EnableVirtualizationExtensions {
|
||||
if b.config.EnableDynamicMemory {
|
||||
warning = fmt.Sprintf("For nested virtualization, when virtualization extension is enabled, dynamic memory should not be allowed.")
|
||||
warnings = appendWarnings(warnings, warning)
|
||||
}
|
||||
|
||||
if !b.config.EnableMacSpoofing {
|
||||
warning = fmt.Sprintf("For nested virtualization, when virtualization extension is enabled, mac spoofing should be allowed.")
|
||||
warnings = appendWarnings(warnings, warning)
|
||||
}
|
||||
|
||||
if b.config.RamSize < MinNestedVirtualizationRamSize {
|
||||
warning = fmt.Sprintf("For nested virtualization, when virtualization extension is enabled, there should be 4GB or more memory set for the vm, otherwise Hyper-V may fail to start any nested VMs.")
|
||||
warnings = appendWarnings(warnings, warning)
|
||||
}
|
||||
}
|
||||
|
||||
if b.config.SwitchVlanId != "" {
|
||||
if b.config.SwitchVlanId != b.config.VlanId {
|
||||
warning = fmt.Sprintf("Switch network adaptor vlan should match virtual machine network adaptor vlan. The switch will not be able to see traffic from the VM.")
|
||||
warnings = appendWarnings(warnings, warning)
|
||||
}
|
||||
}
|
||||
|
||||
if errs != nil && len(errs.Errors) > 0 {
|
||||
return warnings, errs
|
||||
}
|
||||
|
||||
return warnings, nil
|
||||
}
|
||||
|
||||
// Run executes a Packer build and returns a packer.Artifact representing
|
||||
// a Hyperv appliance.
|
||||
func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packer.Artifact, error) {
|
||||
// Create the driver that we'll use to communicate with Hyperv
|
||||
driver, err := hypervcommon.NewHypervPS4Driver()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Failed creating Hyper-V driver: %s", err)
|
||||
}
|
||||
|
||||
// Set up the state.
|
||||
state := new(multistep.BasicStateBag)
|
||||
state.Put("cache", cache)
|
||||
state.Put("config", &b.config)
|
||||
state.Put("debug", b.config.PackerDebug)
|
||||
state.Put("driver", driver)
|
||||
state.Put("hook", hook)
|
||||
state.Put("ui", ui)
|
||||
|
||||
steps := []multistep.Step{
|
||||
&hypervcommon.StepCreateTempDir{},
|
||||
&hypervcommon.StepOutputDir{
|
||||
Force: b.config.PackerForce,
|
||||
Path: b.config.OutputDir,
|
||||
},
|
||||
&common.StepDownload{
|
||||
Checksum: b.config.ISOChecksum,
|
||||
ChecksumType: b.config.ISOChecksumType,
|
||||
Description: "ISO",
|
||||
ResultKey: "iso_path",
|
||||
Url: b.config.ISOUrls,
|
||||
Extension: b.config.TargetExtension,
|
||||
TargetPath: b.config.TargetPath,
|
||||
},
|
||||
&common.StepCreateFloppy{
|
||||
Files: b.config.FloppyFiles,
|
||||
},
|
||||
&common.StepHTTPServer{
|
||||
HTTPDir: b.config.HTTPDir,
|
||||
HTTPPortMin: b.config.HTTPPortMin,
|
||||
HTTPPortMax: b.config.HTTPPortMax,
|
||||
},
|
||||
&hypervcommon.StepCreateSwitch{
|
||||
SwitchName: b.config.SwitchName,
|
||||
},
|
||||
&hypervcommon.StepCloneVM{
|
||||
CloneFromVMName: b.config.CloneFromVMName,
|
||||
CloneFromSnapshotName: b.config.CloneFromSnapshotName,
|
||||
CloneAllSnapshots: b.config.CloneAllSnapshots,
|
||||
VMName: b.config.VMName,
|
||||
SwitchName: b.config.SwitchName,
|
||||
RamSize: b.config.RamSize,
|
||||
Cpu: b.config.Cpu,
|
||||
EnableMacSpoofing: b.config.EnableMacSpoofing,
|
||||
EnableDynamicMemory: b.config.EnableDynamicMemory,
|
||||
EnableSecureBoot: b.config.EnableSecureBoot,
|
||||
EnableVirtualizationExtensions: b.config.EnableVirtualizationExtensions,
|
||||
},
|
||||
|
||||
&hypervcommon.StepEnableIntegrationService{},
|
||||
|
||||
&hypervcommon.StepMountDvdDrive{
|
||||
Generation: b.config.Generation,
|
||||
},
|
||||
&hypervcommon.StepMountFloppydrive{
|
||||
Generation: b.config.Generation,
|
||||
},
|
||||
|
||||
&hypervcommon.StepMountGuestAdditions{
|
||||
GuestAdditionsMode: b.config.GuestAdditionsMode,
|
||||
GuestAdditionsPath: b.config.GuestAdditionsPath,
|
||||
Generation: b.config.Generation,
|
||||
},
|
||||
|
||||
&hypervcommon.StepMountSecondaryDvdImages{
|
||||
IsoPaths: b.config.SecondaryDvdImages,
|
||||
Generation: b.config.Generation,
|
||||
},
|
||||
|
||||
&hypervcommon.StepConfigureVlan{
|
||||
VlanId: b.config.VlanId,
|
||||
SwitchVlanId: b.config.SwitchVlanId,
|
||||
},
|
||||
|
||||
&hypervcommon.StepRun{
|
||||
BootWait: b.config.BootWait,
|
||||
},
|
||||
|
||||
&hypervcommon.StepTypeBootCommand{
|
||||
BootCommand: b.config.BootCommand,
|
||||
SwitchName: b.config.SwitchName,
|
||||
Ctx: b.config.ctx,
|
||||
},
|
||||
|
||||
// configure the communicator ssh, winrm
|
||||
&communicator.StepConnect{
|
||||
Config: &b.config.SSHConfig.Comm,
|
||||
Host: hypervcommon.CommHost,
|
||||
SSHConfig: hypervcommon.SSHConfigFunc(&b.config.SSHConfig),
|
||||
},
|
||||
|
||||
// provision requires communicator to be setup
|
||||
&common.StepProvision{},
|
||||
|
||||
&hypervcommon.StepShutdown{
|
||||
Command: b.config.ShutdownCommand,
|
||||
Timeout: b.config.ShutdownTimeout,
|
||||
},
|
||||
|
||||
// wait for the vm to be powered off
|
||||
&hypervcommon.StepWaitForPowerOff{},
|
||||
|
||||
// remove the secondary dvd images
|
||||
// after we power down
|
||||
&hypervcommon.StepUnmountSecondaryDvdImages{},
|
||||
&hypervcommon.StepUnmountGuestAdditions{},
|
||||
&hypervcommon.StepUnmountDvdDrive{},
|
||||
&hypervcommon.StepUnmountFloppyDrive{
|
||||
Generation: b.config.Generation,
|
||||
},
|
||||
&hypervcommon.StepExportVm{
|
||||
OutputDir: b.config.OutputDir,
|
||||
SkipCompaction: b.config.SkipCompaction,
|
||||
},
|
||||
|
||||
// the clean up actions for each step will be executed reverse order
|
||||
}
|
||||
|
||||
// Run the steps.
|
||||
if b.config.PackerDebug {
|
||||
pauseFn := common.MultistepDebugFn(ui)
|
||||
state.Put("pauseFn", pauseFn)
|
||||
b.runner = &multistep.DebugRunner{
|
||||
Steps: steps,
|
||||
PauseFn: pauseFn,
|
||||
}
|
||||
} else {
|
||||
b.runner = &multistep.BasicRunner{Steps: steps}
|
||||
}
|
||||
|
||||
b.runner.Run(state)
|
||||
|
||||
// Report any errors.
|
||||
if rawErr, ok := state.GetOk("error"); ok {
|
||||
return nil, rawErr.(error)
|
||||
}
|
||||
|
||||
// If we were interrupted or cancelled, then just exit.
|
||||
if _, ok := state.GetOk(multistep.StateCancelled); ok {
|
||||
return nil, errors.New("Build was cancelled.")
|
||||
}
|
||||
|
||||
if _, ok := state.GetOk(multistep.StateHalted); ok {
|
||||
return nil, errors.New("Build was halted.")
|
||||
}
|
||||
|
||||
return hypervcommon.NewArtifact(b.config.OutputDir)
|
||||
}
|
||||
|
||||
// Cancel.
|
||||
func (b *Builder) Cancel() {
|
||||
if b.runner != nil {
|
||||
log.Println("Cancelling the step runner...")
|
||||
b.runner.Cancel()
|
||||
}
|
||||
}
|
||||
|
||||
func appendWarnings(slice []string, data ...string) []string {
|
||||
m := len(slice)
|
||||
n := m + len(data)
|
||||
if n > cap(slice) { // if necessary, reallocate
|
||||
// allocate double what's needed, for future growth.
|
||||
newSlice := make([]string, (n+1)*2)
|
||||
copy(newSlice, slice)
|
||||
slice = newSlice
|
||||
}
|
||||
slice = slice[0:n]
|
||||
copy(slice[m:n], data)
|
||||
return slice
|
||||
}
|
||||
|
||||
func (b *Builder) checkRamSize() error {
|
||||
if b.config.RamSize == 0 {
|
||||
b.config.RamSize = DefaultRamSize
|
||||
}
|
||||
|
||||
log.Println(fmt.Sprintf("%s: %v", "RamSize", b.config.RamSize))
|
||||
|
||||
if b.config.RamSize < MinRamSize {
|
||||
return fmt.Errorf("ram_size: Virtual machine requires memory size >= %v MB, but defined: %v", MinRamSize, b.config.RamSize)
|
||||
} else if b.config.RamSize > MaxRamSize {
|
||||
return fmt.Errorf("ram_size: Virtual machine requires memory size <= %v MB, but defined: %v", MaxRamSize, b.config.RamSize)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *Builder) checkHostAvailableMemory() string {
|
||||
powershellAvailable, _, _ := powershell.IsPowershellAvailable()
|
||||
|
||||
if powershellAvailable {
|
||||
freeMB := powershell.GetHostAvailableMemory()
|
||||
|
||||
if (freeMB - float64(b.config.RamSize)) < LowRam {
|
||||
return fmt.Sprintf("Hyper-V might fail to create a VM if there is not enough free memory in the system.")
|
||||
}
|
||||
}
|
||||
|
||||
return ""
|
||||
}
|
||||
|
||||
func (b *Builder) detectSwitchName() string {
|
||||
powershellAvailable, _, _ := powershell.IsPowershellAvailable()
|
||||
|
||||
if powershellAvailable {
|
||||
// no switch name, try to get one attached to a online network adapter
|
||||
onlineSwitchName, err := hyperv.GetExternalOnlineVirtualSwitch()
|
||||
if onlineSwitchName != "" && err == nil {
|
||||
return onlineSwitchName
|
||||
}
|
||||
}
|
||||
|
||||
return fmt.Sprintf("packer-%s", b.config.PackerBuildName)
|
||||
}
|
|
@ -0,0 +1,250 @@
|
|||
package vmcx
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/mitchellh/packer/packer"
|
||||
)
|
||||
|
||||
func testConfig() map[string]interface{} {
|
||||
return map[string]interface{}{
|
||||
"iso_checksum": "foo",
|
||||
"iso_checksum_type": "md5",
|
||||
"iso_url": "http://www.packer.io",
|
||||
"shutdown_command": "yes",
|
||||
"ssh_username": "foo",
|
||||
"ram_size": 64,
|
||||
"disk_size": 256,
|
||||
"guest_additions_mode": "none",
|
||||
packer.BuildNameConfigKey: "foo",
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuilder_ImplementsBuilder(t *testing.T) {
|
||||
var raw interface{}
|
||||
raw = &Builder{}
|
||||
if _, ok := raw.(packer.Builder); !ok {
|
||||
t.Error("Builder must implement builder.")
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuilderPrepare_Defaults(t *testing.T) {
|
||||
var b Builder
|
||||
config := testConfig()
|
||||
|
||||
warns, err := b.Prepare(config)
|
||||
if len(warns) > 0 {
|
||||
t.Fatalf("bad: %#v", warns)
|
||||
}
|
||||
if err != nil {
|
||||
t.Fatalf("should not have error: %s", err)
|
||||
}
|
||||
|
||||
if b.config.VMName != "packer-foo" {
|
||||
t.Errorf("bad vm name: %s", b.config.VMName)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuilderPrepare_DiskSize(t *testing.T) {
|
||||
var b Builder
|
||||
config := testConfig()
|
||||
|
||||
delete(config, "disk_size")
|
||||
warns, err := b.Prepare(config)
|
||||
if len(warns) > 0 {
|
||||
t.Fatalf("bad: %#v", warns)
|
||||
}
|
||||
if err != nil {
|
||||
t.Fatalf("bad err: %s", err)
|
||||
}
|
||||
|
||||
if b.config.DiskSize != 40*1024 {
|
||||
t.Fatalf("bad size: %d", b.config.DiskSize)
|
||||
}
|
||||
|
||||
config["disk_size"] = 256
|
||||
b = Builder{}
|
||||
warns, err = b.Prepare(config)
|
||||
if len(warns) > 0 {
|
||||
t.Fatalf("bad: %#v", warns)
|
||||
}
|
||||
if err != nil {
|
||||
t.Fatalf("should not have error: %s", err)
|
||||
}
|
||||
|
||||
if b.config.DiskSize != 256 {
|
||||
t.Fatalf("bad size: %d", b.config.DiskSize)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuilderPrepare_InvalidKey(t *testing.T) {
|
||||
var b Builder
|
||||
config := testConfig()
|
||||
|
||||
// Add a random key
|
||||
config["i_should_not_be_valid"] = true
|
||||
warns, err := b.Prepare(config)
|
||||
if len(warns) > 0 {
|
||||
t.Fatalf("bad: %#v", warns)
|
||||
}
|
||||
if err == nil {
|
||||
t.Fatal("should have error")
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuilderPrepare_ISOChecksum(t *testing.T) {
|
||||
var b Builder
|
||||
config := testConfig()
|
||||
|
||||
// Test bad
|
||||
config["iso_checksum"] = ""
|
||||
warns, err := b.Prepare(config)
|
||||
if len(warns) > 0 {
|
||||
t.Fatalf("bad: %#v", warns)
|
||||
}
|
||||
if err == nil {
|
||||
t.Fatal("should have error")
|
||||
}
|
||||
|
||||
// Test good
|
||||
config["iso_checksum"] = "FOo"
|
||||
b = Builder{}
|
||||
warns, err = b.Prepare(config)
|
||||
if len(warns) > 0 {
|
||||
t.Fatalf("bad: %#v", warns)
|
||||
}
|
||||
if err != nil {
|
||||
t.Fatalf("should not have error: %s", err)
|
||||
}
|
||||
|
||||
if b.config.ISOChecksum != "foo" {
|
||||
t.Fatalf("should've lowercased: %s", b.config.ISOChecksum)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuilderPrepare_ISOChecksumType(t *testing.T) {
|
||||
var b Builder
|
||||
config := testConfig()
|
||||
|
||||
// Test bad
|
||||
config["iso_checksum_type"] = ""
|
||||
warns, err := b.Prepare(config)
|
||||
if len(warns) > 0 {
|
||||
t.Fatalf("bad: %#v", warns)
|
||||
}
|
||||
if err == nil {
|
||||
t.Fatal("should have error")
|
||||
}
|
||||
|
||||
// Test good
|
||||
config["iso_checksum_type"] = "mD5"
|
||||
b = Builder{}
|
||||
warns, err = b.Prepare(config)
|
||||
if len(warns) > 0 {
|
||||
t.Fatalf("bad: %#v", warns)
|
||||
}
|
||||
if err != nil {
|
||||
t.Fatalf("should not have error: %s", err)
|
||||
}
|
||||
|
||||
if b.config.ISOChecksumType != "md5" {
|
||||
t.Fatalf("should've lowercased: %s", b.config.ISOChecksumType)
|
||||
}
|
||||
|
||||
// Test unknown
|
||||
config["iso_checksum_type"] = "fake"
|
||||
b = Builder{}
|
||||
warns, err = b.Prepare(config)
|
||||
if len(warns) > 0 {
|
||||
t.Fatalf("bad: %#v", warns)
|
||||
}
|
||||
if err == nil {
|
||||
t.Fatal("should have error")
|
||||
}
|
||||
|
||||
// Test none
|
||||
config["iso_checksum_type"] = "none"
|
||||
b = Builder{}
|
||||
warns, err = b.Prepare(config)
|
||||
if len(warns) == 0 {
|
||||
t.Fatalf("bad: %#v", warns)
|
||||
}
|
||||
if err != nil {
|
||||
t.Fatalf("should not have error: %s", err)
|
||||
}
|
||||
|
||||
if b.config.ISOChecksumType != "none" {
|
||||
t.Fatalf("should've lowercased: %s", b.config.ISOChecksumType)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuilderPrepare_ISOUrl(t *testing.T) {
|
||||
var b Builder
|
||||
config := testConfig()
|
||||
delete(config, "iso_url")
|
||||
delete(config, "iso_urls")
|
||||
|
||||
// Test both epty
|
||||
config["iso_url"] = ""
|
||||
b = Builder{}
|
||||
warns, err := b.Prepare(config)
|
||||
if len(warns) > 0 {
|
||||
t.Fatalf("bad: %#v", warns)
|
||||
}
|
||||
if err == nil {
|
||||
t.Fatal("should have error")
|
||||
}
|
||||
|
||||
// Test iso_url set
|
||||
config["iso_url"] = "http://www.packer.io"
|
||||
b = Builder{}
|
||||
warns, err = b.Prepare(config)
|
||||
if len(warns) > 0 {
|
||||
t.Fatalf("bad: %#v", warns)
|
||||
}
|
||||
if err != nil {
|
||||
t.Errorf("should not have error: %s", err)
|
||||
}
|
||||
|
||||
expected := []string{"http://www.packer.io"}
|
||||
if !reflect.DeepEqual(b.config.ISOUrls, expected) {
|
||||
t.Fatalf("bad: %#v", b.config.ISOUrls)
|
||||
}
|
||||
|
||||
// Test both set
|
||||
config["iso_url"] = "http://www.packer.io"
|
||||
config["iso_urls"] = []string{"http://www.packer.io"}
|
||||
b = Builder{}
|
||||
warns, err = b.Prepare(config)
|
||||
if len(warns) > 0 {
|
||||
t.Fatalf("bad: %#v", warns)
|
||||
}
|
||||
if err == nil {
|
||||
t.Fatal("should have error")
|
||||
}
|
||||
|
||||
// Test just iso_urls set
|
||||
delete(config, "iso_url")
|
||||
config["iso_urls"] = []string{
|
||||
"http://www.packer.io",
|
||||
"http://www.hashicorp.com",
|
||||
}
|
||||
|
||||
b = Builder{}
|
||||
warns, err = b.Prepare(config)
|
||||
if len(warns) > 0 {
|
||||
t.Fatalf("bad: %#v", warns)
|
||||
}
|
||||
if err != nil {
|
||||
t.Errorf("should not have error: %s", err)
|
||||
}
|
||||
|
||||
expected = []string{
|
||||
"http://www.packer.io",
|
||||
"http://www.hashicorp.com",
|
||||
}
|
||||
if !reflect.DeepEqual(b.config.ISOUrls, expected) {
|
||||
t.Fatalf("bad: %#v", b.config.ISOUrls)
|
||||
}
|
||||
}
|
|
@ -234,6 +234,112 @@ if ((Get-Command Set-Vm).Parameters["AutomaticCheckpointsEnabled"]) {
|
|||
return err
|
||||
}
|
||||
|
||||
func DisableAutomaticCheckpoints(vmName string) error {
|
||||
var script = `
|
||||
param([string]$vmName)
|
||||
if ((Get-Command Set-Vm).Parameters["AutomaticCheckpointsEnabled"]) {
|
||||
Set-Vm -Name $vmName -AutomaticCheckpointsEnabled $false }
|
||||
`
|
||||
var ps powershell.PowerShellCmd
|
||||
err := ps.Run(script, vmName)
|
||||
return err
|
||||
}
|
||||
|
||||
func CloneVirtualMachine(cloneFromVmName string, cloneFromSnapshotName string, cloneAllSnapshots bool, vmName string, path string, ram int64, switchName string) error {
|
||||
|
||||
var script = `
|
||||
param([string]$CloneFromVMName, [string]$CloneFromSnapshotName, [string]CloneAllSnapshotsString, [string]$vmName, [string]$path, [long]$memoryStartupBytes, [string]$switchName)
|
||||
|
||||
$CloneAllSnapshots = [System.Boolean]::Parse($CloneAllSnapshotsString)
|
||||
|
||||
$ExportPath = Join-Path $path $VMName
|
||||
|
||||
if ($CloneFromSnapshotName) {
|
||||
$snapshot = Get-VMSnapshot -VMName $CloneFromVMName -Name $CloneFromSnapshotName
|
||||
Export-VMSnapshot -VMSnapshot $snapshot -Path $ExportPath -ErrorAction Stop
|
||||
} else {
|
||||
if (!$CloneAllSnapshots) {
|
||||
#Use last snapshot if one was not specified
|
||||
$snapshot = Get-VMSnapshot -VMName $CloneFromVMName | Select -Last 1
|
||||
} else {
|
||||
$snapshot = $null
|
||||
}
|
||||
|
||||
if (!$snapshot) {
|
||||
#No snapshot clone
|
||||
Export-VM -Name $CloneFromVMName -Path $ExportPath -ErrorAction Stop
|
||||
} else {
|
||||
#Snapshot clone
|
||||
Export-VMSnapshot -VMSnapshot $snapshot -Path $ExportPath -ErrorAction Stop
|
||||
}
|
||||
}
|
||||
|
||||
$result = Get-ChildItem -Path (Join-Path $ExportPath $CloneFromVMName) | Move-Item -Destination $ExportPath -Force
|
||||
$result = Remove-Item -Path (Join-Path $ExportPath $CloneFromVMName)
|
||||
|
||||
$VirtualMachinePath = Get-ChildItem -Path (Join-Path $ExportPath 'Virtual Machines') -Filter *.vmcx -Recurse -ErrorAction SilentlyContinue | select -First 1 | %{$_.FullName}
|
||||
if (!$VirtualMachinePath){
|
||||
$VirtualMachinePath = Get-ChildItem -Path (Join-Path $ExportPath 'Virtual Machines') -Filter *.xml -Recurse -ErrorAction SilentlyContinue | select -First 1 | %{$_.FullName}
|
||||
}
|
||||
if (!$VirtualMachinePath){
|
||||
$VirtualMachinePath = Get-ChildItem -Path $ExportPath -Filter *.xml -Recurse -ErrorAction SilentlyContinue | select -First 1 | %{$_.FullName}
|
||||
}
|
||||
|
||||
$compatibilityReport = Compare-VM -Path $VirtualMachinePath -VirtualMachinePath $ExportPath -SmartPagingFilePath $ExportPath -SnapshotFilePath $ExportPath -VhdDestinationPath (Join-Path -Path $ExportPath -ChildPath 'Virtual Hard Disks') -GenerateNewId -Copy:$false
|
||||
Set-VMMemory -VM $compatibilityReport.VM -StartupBytes $memoryStartupBytes
|
||||
$networkAdaptor = $compatibilityReport.VM.NetworkAdapters | Select -First 1
|
||||
Disconnect-VMNetworkAdapter -VMNetworkAdapter $networkAdaptor
|
||||
Connect-VMNetworkAdapter -VMNetworkAdapter $networkAdaptor -SwitchName $switchName
|
||||
$vm = Import-VM -CompatibilityReport $compatibilityReport
|
||||
|
||||
if ($vm) {
|
||||
$result = Rename-VM -VM $vm -NewName $VMName
|
||||
}
|
||||
`
|
||||
|
||||
CloneAllSnapshotsString := "False"
|
||||
if cloneAllSnapshots {
|
||||
CloneAllSnapshotsString = "True"
|
||||
}
|
||||
|
||||
var ps powershell.PowerShellCmd
|
||||
err := ps.Run(script, cloneFromVmName, cloneFromSnapshotName, CloneAllSnapshotsString, vmName, path, strconv.FormatInt(ram, 10), switchName)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return DeleteAllDvdDrives(vmName)
|
||||
|
||||
}
|
||||
|
||||
func GetVirtualMachineGeneration(vmName string) (uint, error) {
|
||||
var script = `
|
||||
param([string]$vmName)
|
||||
$generation = Get-Vm -Name $vmName | %{$_.Generation}
|
||||
if (!$generation){
|
||||
$generation = 1
|
||||
}
|
||||
return $generation
|
||||
`
|
||||
var ps powershell.PowerShellCmd
|
||||
cmdOut, err := ps.Output(script, vmName)
|
||||
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
generationUint32, err := strconv.ParseUint(strings.TrimSpace(string(cmdOut)), 10, 32)
|
||||
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
generation := uint(generationUint32)
|
||||
|
||||
return generation, err
|
||||
}
|
||||
|
||||
func SetVirtualMachineCpuCount(vmName string, cpu uint) error {
|
||||
|
||||
var script = `
|
||||
|
|
|
@ -246,6 +246,61 @@ func HasVirtualMachineVirtualizationExtensions() (bool, error) {
|
|||
return hasVirtualMachineVirtualizationExtensions, err
|
||||
}
|
||||
|
||||
func DoesVirtualMachineExist(vmName string) (bool, error) {
|
||||
|
||||
var script = `
|
||||
param([string]$vmName)
|
||||
return (Get-VM | ?{$_.Name -eq $vmName}) -ne $null
|
||||
`
|
||||
|
||||
var ps PowerShellCmd
|
||||
cmdOut, err := ps.Output(script, vmName)
|
||||
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
var exists = strings.TrimSpace(cmdOut) == "True"
|
||||
return exists, err
|
||||
}
|
||||
|
||||
func DoesVirtualMachineSnapshotExist(vmName string, snapshotName string) (bool, error) {
|
||||
|
||||
var script = `
|
||||
param([string]$vmName, [string]$snapshotName)
|
||||
return (Get-VMSnapshot -VMName $vmName | ?{$_.Name -eq $snapshotName}) -ne $null
|
||||
`
|
||||
|
||||
var ps PowerShellCmd
|
||||
cmdOut, err := ps.Output(script, vmName, snapshotName)
|
||||
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
var exists = strings.TrimSpace(cmdOut) == "True"
|
||||
return exists, err
|
||||
}
|
||||
|
||||
func IsVirtualMachineOn(vmName string) (bool, error) {
|
||||
|
||||
var script = `
|
||||
param([string]$vmName)
|
||||
$vm = Get-VM -Name $vmName -ErrorAction SilentlyContinue
|
||||
$vm.State -eq [Microsoft.HyperV.PowerShell.VMState]::Running
|
||||
`
|
||||
|
||||
var ps PowerShellCmd
|
||||
cmdOut, err := ps.Output(script, vmName)
|
||||
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
var isRunning = strings.TrimSpace(cmdOut) == "True"
|
||||
return isRunning, err
|
||||
}
|
||||
|
||||
func SetUnattendedProductKey(path string, productKey string) error {
|
||||
|
||||
var script = `
|
||||
|
|
Loading…
Reference in New Issue