github.com/rahart/packer@v0.12.2-0.20161229105310-282bb6ad370f/builder/hyperv/common/step_create_vm.go (about) 1 package common 2 3 import ( 4 "fmt" 5 6 "github.com/mitchellh/multistep" 7 "github.com/mitchellh/packer/packer" 8 ) 9 10 // This step creates the actual virtual machine. 11 // 12 // Produces: 13 // VMName string - The name of the VM 14 type StepCreateVM struct { 15 VMName string 16 SwitchName string 17 RamSize uint 18 DiskSize uint 19 Generation uint 20 Cpu uint 21 EnableMacSpoofing bool 22 EnableDynamicMemory bool 23 EnableSecureBoot bool 24 EnableVirtualizationExtensions bool 25 } 26 27 func (s *StepCreateVM) Run(state multistep.StateBag) multistep.StepAction { 28 driver := state.Get("driver").(Driver) 29 ui := state.Get("ui").(packer.Ui) 30 ui.Say("Creating virtual machine...") 31 32 path := state.Get("packerTempDir").(string) 33 34 // convert the MB to bytes 35 ramSize := int64(s.RamSize * 1024 * 1024) 36 diskSize := int64(s.DiskSize * 1024 * 1024) 37 38 err := driver.CreateVirtualMachine(s.VMName, path, ramSize, diskSize, s.SwitchName, s.Generation) 39 if err != nil { 40 err := fmt.Errorf("Error creating virtual machine: %s", err) 41 state.Put("error", err) 42 ui.Error(err.Error()) 43 return multistep.ActionHalt 44 } 45 46 err = driver.SetVirtualMachineCpuCount(s.VMName, s.Cpu) 47 if err != nil { 48 err := fmt.Errorf("Error creating setting virtual machine cpu: %s", err) 49 state.Put("error", err) 50 ui.Error(err.Error()) 51 return multistep.ActionHalt 52 } 53 54 if s.EnableDynamicMemory { 55 err = driver.SetVirtualMachineDynamicMemory(s.VMName, s.EnableDynamicMemory) 56 if err != nil { 57 err := fmt.Errorf("Error creating setting virtual machine dynamic memory: %s", err) 58 state.Put("error", err) 59 ui.Error(err.Error()) 60 return multistep.ActionHalt 61 } 62 } 63 64 if s.EnableMacSpoofing { 65 err = driver.SetVirtualMachineMacSpoofing(s.VMName, s.EnableMacSpoofing) 66 if err != nil { 67 err := fmt.Errorf("Error creating setting virtual machine mac spoofing: %s", err) 68 state.Put("error", err) 69 ui.Error(err.Error()) 70 return multistep.ActionHalt 71 } 72 } 73 74 if s.Generation == 2 { 75 err = driver.SetVirtualMachineSecureBoot(s.VMName, s.EnableSecureBoot) 76 if err != nil { 77 err := fmt.Errorf("Error setting secure boot: %s", err) 78 state.Put("error", err) 79 ui.Error(err.Error()) 80 return multistep.ActionHalt 81 } 82 } 83 84 if s.EnableVirtualizationExtensions { 85 //This is only supported on Windows 10 and Windows Server 2016 onwards 86 err = driver.SetVirtualMachineVirtualizationExtensions(s.VMName, s.EnableVirtualizationExtensions) 87 if err != nil { 88 err := fmt.Errorf("Error creating setting virtual machine virtualization extensions: %s", err) 89 state.Put("error", err) 90 ui.Error(err.Error()) 91 return multistep.ActionHalt 92 } 93 } 94 95 // Set the final name in the state bag so others can use it 96 state.Put("vmName", s.VMName) 97 98 return multistep.ActionContinue 99 } 100 101 func (s *StepCreateVM) Cleanup(state multistep.StateBag) { 102 if s.VMName == "" { 103 return 104 } 105 106 driver := state.Get("driver").(Driver) 107 ui := state.Get("ui").(packer.Ui) 108 ui.Say("Unregistering and deleting virtual machine...") 109 110 err := driver.DeleteVirtualMachine(s.VMName) 111 if err != nil { 112 ui.Error(fmt.Sprintf("Error deleting virtual machine: %s", err)) 113 } 114 }