2017-03-12 07:31:31 -04:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
2018-01-22 18:32:33 -05:00
|
|
|
"context"
|
2017-03-12 07:31:31 -04:00
|
|
|
"fmt"
|
2017-05-21 12:29:26 -04:00
|
|
|
"log"
|
|
|
|
"path/filepath"
|
2017-05-29 20:16:03 -04:00
|
|
|
"strings"
|
2017-03-12 07:31:31 -04:00
|
|
|
|
2020-11-17 19:31:03 -05:00
|
|
|
"github.com/hashicorp/packer/packer-plugin-sdk/multistep"
|
2020-11-19 14:54:31 -05:00
|
|
|
packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer"
|
2017-03-12 07:31:31 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// This step clones an existing virtual machine.
|
|
|
|
//
|
|
|
|
// Produces:
|
|
|
|
// VMName string - The name of the VM
|
|
|
|
type StepCloneVM struct {
|
2018-07-19 13:53:42 -04:00
|
|
|
CloneFromVMCXPath string
|
2017-03-12 07:31:31 -04:00
|
|
|
CloneFromVMName string
|
|
|
|
CloneFromSnapshotName string
|
|
|
|
CloneAllSnapshots bool
|
|
|
|
VMName string
|
|
|
|
SwitchName string
|
2018-12-10 17:46:16 -05:00
|
|
|
CompareCopy bool
|
2017-03-12 07:31:31 -04:00
|
|
|
RamSize uint
|
|
|
|
Cpu uint
|
|
|
|
EnableMacSpoofing bool
|
|
|
|
EnableDynamicMemory bool
|
|
|
|
EnableSecureBoot bool
|
2018-05-10 13:00:35 -04:00
|
|
|
SecureBootTemplate string
|
2017-03-12 07:31:31 -04:00
|
|
|
EnableVirtualizationExtensions bool
|
2017-12-14 21:24:15 -05:00
|
|
|
MacAddress string
|
2019-04-12 18:59:09 -04:00
|
|
|
KeepRegistered bool
|
2019-10-18 14:43:11 -04:00
|
|
|
AdditionalDiskSize []uint
|
2019-10-18 16:01:22 -04:00
|
|
|
DiskBlockSize uint
|
2017-03-12 07:31:31 -04:00
|
|
|
}
|
|
|
|
|
2019-03-29 11:50:02 -04:00
|
|
|
func (s *StepCloneVM) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
2017-03-12 07:31:31 -04:00
|
|
|
driver := state.Get("driver").(Driver)
|
2020-11-19 14:54:31 -05:00
|
|
|
ui := state.Get("ui").(packersdk.Ui)
|
2017-03-12 19:51:59 -04:00
|
|
|
ui.Say("Cloning virtual machine...")
|
2017-03-12 07:31:31 -04:00
|
|
|
|
2018-07-08 18:27:37 -04:00
|
|
|
path := state.Get("build_dir").(string)
|
2017-05-29 20:16:03 -04:00
|
|
|
|
2017-05-21 12:29:26 -04:00
|
|
|
// Determine if we even have an existing virtual harddrive to attach
|
|
|
|
harddrivePath := ""
|
|
|
|
if harddrivePathRaw, ok := state.GetOk("iso_path"); ok {
|
|
|
|
extension := strings.ToLower(filepath.Ext(harddrivePathRaw.(string)))
|
2017-05-28 21:36:01 -04:00
|
|
|
if extension == ".vhd" || extension == ".vhdx" {
|
2017-05-21 12:29:26 -04:00
|
|
|
harddrivePath = harddrivePathRaw.(string)
|
|
|
|
} else {
|
|
|
|
log.Println("No existing virtual harddrive, not attaching.")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
log.Println("No existing virtual harddrive, not attaching.")
|
|
|
|
}
|
2017-03-12 07:31:31 -04:00
|
|
|
|
|
|
|
// convert the MB to bytes
|
|
|
|
ramSize := int64(s.RamSize * 1024 * 1024)
|
|
|
|
|
2018-12-10 17:46:16 -05:00
|
|
|
err := driver.CloneVirtualMachine(s.CloneFromVMCXPath, s.CloneFromVMName,
|
|
|
|
s.CloneFromSnapshotName, s.CloneAllSnapshots, s.VMName, path,
|
|
|
|
harddrivePath, ramSize, s.SwitchName, s.CompareCopy)
|
2017-03-12 07:31:31 -04:00
|
|
|
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 {
|
2018-05-10 13:00:35 -04:00
|
|
|
|
|
|
|
err = driver.SetVirtualMachineSecureBoot(s.VMName, s.EnableSecureBoot, s.SecureBootTemplate)
|
2017-03-12 07:31:31 -04:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-18 14:43:11 -04:00
|
|
|
if len(s.AdditionalDiskSize) > 0 {
|
|
|
|
for index, size := range s.AdditionalDiskSize {
|
|
|
|
diskSize := int64(size * 1024 * 1024)
|
|
|
|
diskFile := fmt.Sprintf("%s-%d.vhdx", s.VMName, index)
|
2019-10-18 16:01:22 -04:00
|
|
|
diskBlockSize := int64(s.DiskBlockSize) * 1024 * 1024
|
2019-10-18 14:43:11 -04:00
|
|
|
err = driver.AddVirtualMachineHardDrive(s.VMName, path, diskFile, diskSize, diskBlockSize, "SCSI")
|
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Error creating and attaching additional disk drive: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-14 21:24:15 -05:00
|
|
|
if s.MacAddress != "" {
|
|
|
|
err = driver.SetVmNetworkAdapterMacAddress(s.VMName, s.MacAddress)
|
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Error setting MAC address: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-12 07:31:31 -04:00
|
|
|
// Set the final name in the state bag so others can use it
|
|
|
|
state.Put("vmName", s.VMName)
|
2019-12-13 14:57:01 -05:00
|
|
|
// instance_id is the generic term used so that users can have access to the
|
|
|
|
// instance id inside of the provisioners, used in step_provision.
|
|
|
|
state.Put("instance_id", s.VMName)
|
2017-03-12 07:31:31 -04:00
|
|
|
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StepCloneVM) Cleanup(state multistep.StateBag) {
|
|
|
|
if s.VMName == "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
driver := state.Get("driver").(Driver)
|
2020-11-19 14:54:31 -05:00
|
|
|
ui := state.Get("ui").(packersdk.Ui)
|
2019-04-12 18:59:09 -04:00
|
|
|
|
|
|
|
if s.KeepRegistered {
|
|
|
|
ui.Say("keep_registered set. Skipping unregister/deletion of VM.")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-03-12 07:31:31 -04:00
|
|
|
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))
|
|
|
|
}
|
|
|
|
}
|