2013-12-21 17:27:00 -05:00
|
|
|
package iso
|
2013-06-11 19:12:19 -04:00
|
|
|
|
|
|
|
import (
|
2018-01-22 18:32:33 -05:00
|
|
|
"context"
|
2013-06-11 19:12:19 -04:00
|
|
|
"fmt"
|
2018-11-17 05:24:32 -05:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2017-10-25 13:17:08 -04:00
|
|
|
|
2020-12-17 16:29:25 -05:00
|
|
|
"github.com/hashicorp/packer-plugin-sdk/multistep"
|
|
|
|
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
|
2017-04-04 16:39:01 -04:00
|
|
|
vboxcommon "github.com/hashicorp/packer/builder/virtualbox/common"
|
2013-06-11 19:12:19 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// This step creates the actual virtual machine.
|
2013-06-11 19:44:43 -04:00
|
|
|
//
|
|
|
|
// Produces:
|
|
|
|
// vmName string - The name of the VM
|
2013-06-11 23:07:11 -04:00
|
|
|
type stepCreateVM struct {
|
2013-06-11 19:12:19 -04:00
|
|
|
vmName string
|
|
|
|
}
|
|
|
|
|
2019-03-29 11:50:02 -04:00
|
|
|
func (s *stepCreateVM) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
2015-05-27 17:01:08 -04:00
|
|
|
config := state.Get("config").(*Config)
|
2013-12-21 18:00:48 -05:00
|
|
|
driver := state.Get("driver").(vboxcommon.Driver)
|
2020-11-19 14:54:31 -05:00
|
|
|
ui := state.Get("ui").(packersdk.Ui)
|
2013-06-11 19:12:19 -04:00
|
|
|
|
|
|
|
name := config.VMName
|
|
|
|
|
2021-03-08 05:53:07 -05:00
|
|
|
commands := make([][]string, 14)
|
2013-07-07 21:04:30 -04:00
|
|
|
commands[0] = []string{
|
|
|
|
"createvm", "--name", name,
|
|
|
|
"--ostype", config.GuestOSType, "--register",
|
|
|
|
}
|
2013-06-11 19:12:19 -04:00
|
|
|
commands[1] = []string{
|
|
|
|
"modifyvm", name,
|
|
|
|
"--boot1", "disk", "--boot2", "dvd", "--boot3", "none", "--boot4", "none",
|
|
|
|
}
|
2018-11-17 05:24:32 -05:00
|
|
|
commands[2] = []string{"modifyvm", name, "--cpus", strconv.Itoa(config.HWConfig.CpuCount)}
|
|
|
|
commands[3] = []string{"modifyvm", name, "--memory", strconv.Itoa(config.HWConfig.MemorySize)}
|
|
|
|
commands[4] = []string{"modifyvm", name, "--usb", map[bool]string{true: "on", false: "off"}[config.HWConfig.USB]}
|
|
|
|
|
|
|
|
if strings.ToLower(config.HWConfig.Sound) == "none" {
|
2021-02-20 14:09:16 -05:00
|
|
|
commands[5] = []string{"modifyvm", name, "--audio", config.HWConfig.Sound,
|
2021-02-20 14:31:09 -05:00
|
|
|
"--audiocontroller", config.AudioController}
|
2018-11-17 05:24:32 -05:00
|
|
|
} else {
|
2021-02-20 14:09:16 -05:00
|
|
|
commands[5] = []string{"modifyvm", name, "--audio", config.HWConfig.Sound, "--audioin", "on", "--audioout", "on",
|
2021-02-20 14:31:09 -05:00
|
|
|
"--audiocontroller", config.AudioController}
|
2018-11-17 05:24:32 -05:00
|
|
|
}
|
2013-06-11 19:12:19 -04:00
|
|
|
|
2021-02-20 13:31:46 -05:00
|
|
|
commands[6] = []string{"modifyvm", name, "--chipset", config.Chipset}
|
|
|
|
commands[7] = []string{"modifyvm", name, "--firmware", config.Firmware}
|
2021-02-20 13:59:39 -05:00
|
|
|
// Set the configured NIC type for all 8 possible NICs
|
|
|
|
commands[8] = []string{"modifyvm", name,
|
2021-02-20 14:31:09 -05:00
|
|
|
"--nictype1", config.NICType,
|
|
|
|
"--nictype2", config.NICType,
|
|
|
|
"--nictype3", config.NICType,
|
|
|
|
"--nictype4", config.NICType,
|
|
|
|
"--nictype5", config.NICType,
|
|
|
|
"--nictype6", config.NICType,
|
|
|
|
"--nictype7", config.NICType,
|
|
|
|
"--nictype8", config.NICType}
|
2021-03-08 05:53:07 -05:00
|
|
|
commands[9] = []string{"modifyvm", name, "--graphicscontroller", config.GfxController, "--vram", strconv.FormatUint(uint64(config.GfxVramSize), 10)}
|
2021-03-08 03:58:17 -05:00
|
|
|
if config.RTCTimeBase == "UTC" {
|
|
|
|
commands[10] = []string{"modifyvm", name, "--rtcuseutc", "on"}
|
|
|
|
} else {
|
|
|
|
commands[10] = []string{"modifyvm", name, "--rtcuseutc", "off"}
|
|
|
|
}
|
|
|
|
if config.NestedVirt == true {
|
|
|
|
commands[11] = []string{"modifyvm", name, "--nested-hw-virt", "on"}
|
|
|
|
} else {
|
|
|
|
commands[11] = []string{"modifyvm", name, "--nested-hw-virt", "off"}
|
|
|
|
}
|
2021-02-20 13:31:46 -05:00
|
|
|
|
2021-03-08 05:53:07 -05:00
|
|
|
if config.GfxAccelerate3D {
|
|
|
|
commands[12] = []string{"modifyvm", name, "--accelerate3d", "on"}
|
|
|
|
} else {
|
|
|
|
commands[12] = []string{"modifyvm", name, "--accelerate3d", "off"}
|
2021-03-08 06:37:00 -05:00
|
|
|
}
|
2021-03-08 05:53:07 -05:00
|
|
|
if config.GfxEFIResolution != "" {
|
|
|
|
commands[13] = []string{"setextradata", name, "VBoxInternal2/EfiGraphicsResolution", config.GfxEFIResolution}
|
|
|
|
}
|
|
|
|
|
2013-06-11 19:12:19 -04:00
|
|
|
ui.Say("Creating virtual machine...")
|
|
|
|
for _, command := range commands {
|
|
|
|
err := driver.VBoxManage(command...)
|
|
|
|
if err != nil {
|
2013-06-20 00:07:53 -04:00
|
|
|
err := fmt.Errorf("Error creating VM: %s", err)
|
2013-08-31 15:44:58 -04:00
|
|
|
state.Put("error", err)
|
2013-06-20 00:07:53 -04:00
|
|
|
ui.Error(err.Error())
|
2013-06-11 19:12:19 -04:00
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
2013-10-24 14:46:54 -04:00
|
|
|
// Set the VM name property on the first command
|
2013-06-11 19:12:19 -04:00
|
|
|
if s.vmName == "" {
|
|
|
|
s.vmName = name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-11 19:44:43 -04:00
|
|
|
// Set the final name in the state bag so others can use it
|
2013-08-31 15:44:58 -04:00
|
|
|
state.Put("vmName", s.vmName)
|
2013-06-11 19:44:43 -04:00
|
|
|
|
2013-06-11 19:12:19 -04:00
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2013-08-31 15:44:58 -04:00
|
|
|
func (s *stepCreateVM) Cleanup(state multistep.StateBag) {
|
2013-06-11 19:12:19 -04:00
|
|
|
if s.vmName == "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-12-21 18:00:48 -05:00
|
|
|
driver := state.Get("driver").(vboxcommon.Driver)
|
2020-11-19 14:54:31 -05:00
|
|
|
ui := state.Get("ui").(packersdk.Ui)
|
2016-10-03 15:11:21 -04:00
|
|
|
config := state.Get("config").(*Config)
|
|
|
|
|
|
|
|
_, cancelled := state.GetOk(multistep.StateCancelled)
|
|
|
|
_, halted := state.GetOk(multistep.StateHalted)
|
|
|
|
if (config.KeepRegistered) && (!cancelled && !halted) {
|
|
|
|
ui.Say("Keeping virtual machine registered with VirtualBox host (keep_registered = true)")
|
|
|
|
return
|
|
|
|
}
|
2013-06-11 19:12:19 -04:00
|
|
|
|
2017-10-25 13:17:08 -04:00
|
|
|
ui.Say("Deregistering and deleting VM...")
|
|
|
|
if err := driver.Delete(s.vmName); err != nil {
|
|
|
|
ui.Error(fmt.Sprintf("Error deleting VM: %s", err))
|
2013-06-11 19:12:19 -04:00
|
|
|
}
|
|
|
|
}
|