2015-06-21 12:36:07 +01:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
2018-01-22 15:32:33 -08:00
|
|
|
"context"
|
2015-06-21 12:36:07 +01:00
|
|
|
"fmt"
|
2017-05-21 17:29:26 +01:00
|
|
|
"log"
|
|
|
|
"path/filepath"
|
2017-05-30 01:16:03 +01:00
|
|
|
"strings"
|
|
|
|
|
2018-01-19 16:18:44 -08:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
2017-04-04 13:39:01 -07:00
|
|
|
"github.com/hashicorp/packer/packer"
|
2015-06-21 12:36:07 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// This step creates the actual virtual machine.
|
|
|
|
//
|
|
|
|
// Produces:
|
|
|
|
// VMName string - The name of the VM
|
|
|
|
type StepCreateVM struct {
|
2016-08-07 12:26:27 +01:00
|
|
|
VMName string
|
|
|
|
SwitchName string
|
2017-05-21 17:29:26 +01:00
|
|
|
HarddrivePath string
|
2016-11-06 14:07:46 +00:00
|
|
|
RamSize uint
|
2016-08-07 12:26:27 +01:00
|
|
|
DiskSize uint
|
2018-02-23 20:19:26 +01:00
|
|
|
DiskBlockSize uint
|
2018-12-18 04:59:00 -06:00
|
|
|
UseLegacyNetworkAdapter bool
|
2016-08-07 12:26:27 +01:00
|
|
|
Generation uint
|
|
|
|
Cpu uint
|
|
|
|
EnableMacSpoofing bool
|
|
|
|
EnableDynamicMemory bool
|
|
|
|
EnableSecureBoot bool
|
2018-05-10 19:00:35 +02:00
|
|
|
SecureBootTemplate string
|
2016-08-05 23:09:33 -07:00
|
|
|
EnableVirtualizationExtensions bool
|
2017-10-20 09:29:17 +11:00
|
|
|
AdditionalDiskSize []uint
|
2017-10-12 16:05:31 +05:30
|
|
|
DifferencingDisk bool
|
2017-12-15 13:24:15 +11:00
|
|
|
MacAddress string
|
2018-04-20 15:13:12 -04:00
|
|
|
FixedVHD bool
|
2018-12-19 16:30:57 -08:00
|
|
|
Version string
|
2019-04-12 15:59:09 -07:00
|
|
|
KeepRegistered bool
|
2015-06-21 12:36:07 +01:00
|
|
|
}
|
|
|
|
|
2019-03-29 16:50:02 +01:00
|
|
|
func (s *StepCreateVM) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
2015-10-30 08:23:30 +00:00
|
|
|
driver := state.Get("driver").(Driver)
|
2015-06-21 12:36:07 +01:00
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
ui.Say("Creating virtual machine...")
|
|
|
|
|
2018-07-08 23:27:37 +01:00
|
|
|
var path string
|
|
|
|
if v, ok := state.GetOk("build_dir"); ok {
|
|
|
|
path = v.(string)
|
|
|
|
}
|
2017-05-30 01:16:03 +01:00
|
|
|
|
2019-06-13 14:09:45 -07:00
|
|
|
err := driver.CheckVMName(s.VMName)
|
|
|
|
if err != nil {
|
|
|
|
s.KeepRegistered = true
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
2017-05-21 17:29:26 +01: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-29 02:36:01 +01:00
|
|
|
if extension == ".vhd" || extension == ".vhdx" {
|
2017-05-21 17:29:26 +01:00
|
|
|
harddrivePath = harddrivePathRaw.(string)
|
|
|
|
} else {
|
|
|
|
log.Println("No existing virtual harddrive, not attaching.")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
log.Println("No existing virtual harddrive, not attaching.")
|
|
|
|
}
|
2017-10-11 22:10:39 +05:30
|
|
|
|
2015-06-21 12:36:07 +01:00
|
|
|
// convert the MB to bytes
|
2019-01-31 13:53:01 +11:00
|
|
|
ramSize := int64(s.RamSize) * 1024 * 1024
|
|
|
|
diskSize := int64(s.DiskSize) * 1024 * 1024
|
|
|
|
diskBlockSize := int64(s.DiskBlockSize) * 1024 * 1024
|
2015-06-21 12:36:07 +01:00
|
|
|
|
2019-06-13 14:09:45 -07:00
|
|
|
err = driver.CreateVirtualMachine(s.VMName, path, harddrivePath, ramSize, diskSize, diskBlockSize,
|
2018-12-19 16:30:57 -08:00
|
|
|
s.SwitchName, s.Generation, s.DifferencingDisk, s.FixedVHD, s.Version)
|
2015-06-21 12:36:07 +01:00
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Error creating virtual machine: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
2015-10-30 08:23:30 +00:00
|
|
|
|
2018-12-18 04:59:00 -06:00
|
|
|
if s.UseLegacyNetworkAdapter {
|
|
|
|
err := driver.ReplaceVirtualMachineNetworkAdapter(s.VMName, true)
|
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Error creating legacy network adapter: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-07 12:26:27 +01:00
|
|
|
err = driver.SetVirtualMachineCpuCount(s.VMName, s.Cpu)
|
2015-06-21 19:35:32 +01:00
|
|
|
if err != nil {
|
2017-09-20 14:53:37 +02:00
|
|
|
err := fmt.Errorf("Error setting virtual machine cpu count: %s", err)
|
2015-06-21 19:35:32 +01:00
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
2015-10-30 08:23:30 +00:00
|
|
|
|
2017-09-20 14:53:37 +02:00
|
|
|
err = driver.SetVirtualMachineDynamicMemory(s.VMName, s.EnableDynamicMemory)
|
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Error setting virtual machine dynamic memory: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
2016-08-07 12:26:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if s.EnableMacSpoofing {
|
|
|
|
err = driver.SetVirtualMachineMacSpoofing(s.VMName, s.EnableMacSpoofing)
|
|
|
|
if err != nil {
|
2017-09-20 14:53:37 +02:00
|
|
|
err := fmt.Errorf("Error setting virtual machine mac spoofing: %s", err)
|
2016-08-07 12:26:27 +01:00
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-30 08:23:30 +00:00
|
|
|
if s.Generation == 2 {
|
2018-05-10 19:00:35 +02:00
|
|
|
err = driver.SetVirtualMachineSecureBoot(s.VMName, s.EnableSecureBoot, s.SecureBootTemplate)
|
2015-06-29 21:18:25 +01:00
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Error setting secure boot: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
}
|
2015-06-21 19:35:32 +01:00
|
|
|
|
2016-08-07 12:26:27 +01:00
|
|
|
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 {
|
2017-09-20 14:53:37 +02:00
|
|
|
err := fmt.Errorf("Error setting virtual machine virtualization extensions: %s", err)
|
2016-08-07 12:26:27 +01:00
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-20 09:29:17 +11:00
|
|
|
if len(s.AdditionalDiskSize) > 0 {
|
|
|
|
for index, size := range s.AdditionalDiskSize {
|
2017-10-31 08:48:17 -07:00
|
|
|
diskSize := int64(size * 1024 * 1024)
|
|
|
|
diskFile := fmt.Sprintf("%s-%d.vhdx", s.VMName, index)
|
2018-07-08 11:16:02 +01:00
|
|
|
err = driver.AddVirtualMachineHardDrive(s.VMName, path, diskFile, diskSize, diskBlockSize, "SCSI")
|
2017-10-20 09:29:17 +11:00
|
|
|
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-15 13:24:15 +11: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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-21 12:36:07 +01:00
|
|
|
// Set the final name in the state bag so others can use it
|
|
|
|
state.Put("vmName", s.VMName)
|
|
|
|
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StepCreateVM) Cleanup(state multistep.StateBag) {
|
|
|
|
if s.VMName == "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-10-30 08:23:30 +00:00
|
|
|
driver := state.Get("driver").(Driver)
|
2015-06-21 12:36:07 +01:00
|
|
|
ui := state.Get("ui").(packer.Ui)
|
2019-04-12 15:59:09 -07:00
|
|
|
|
|
|
|
if s.KeepRegistered {
|
|
|
|
ui.Say("keep_registered set. Skipping unregister/deletion of VM.")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-06-21 12:36:07 +01:00
|
|
|
ui.Say("Unregistering and deleting virtual machine...")
|
|
|
|
|
2015-10-30 08:23:30 +00:00
|
|
|
err := driver.DeleteVirtualMachine(s.VMName)
|
2015-06-21 12:36:07 +01:00
|
|
|
if err != nil {
|
|
|
|
ui.Error(fmt.Sprintf("Error deleting virtual machine: %s", err))
|
|
|
|
}
|
2018-02-23 20:19:26 +01:00
|
|
|
|
|
|
|
// TODO: Clean up created VHDX
|
2015-06-21 12:36:07 +01:00
|
|
|
}
|