From 51948daf9204d0e08f0e64723fc113d60ef976cc Mon Sep 17 00:00:00 2001 From: Ali Rizvi-Santiago Date: Sat, 17 Nov 2018 04:24:32 -0600 Subject: [PATCH] Added options for the cpu count, memory, audio, and usb to the virtualbox builder. --- builder/virtualbox/common/hw_config.go | 46 +++++++++++++++++++++ builder/virtualbox/common/hw_config_test.go | 20 +++++++++ builder/virtualbox/iso/builder.go | 2 + builder/virtualbox/iso/step_create_vm.go | 15 +++++-- 4 files changed, 80 insertions(+), 3 deletions(-) create mode 100644 builder/virtualbox/common/hw_config.go create mode 100644 builder/virtualbox/common/hw_config_test.go diff --git a/builder/virtualbox/common/hw_config.go b/builder/virtualbox/common/hw_config.go new file mode 100644 index 000000000..48ce852aa --- /dev/null +++ b/builder/virtualbox/common/hw_config.go @@ -0,0 +1,46 @@ +package common + +import ( + "fmt" + + "github.com/hashicorp/packer/template/interpolate" +) + +type HWConfig struct { + + // cpu information + CpuCount int `mapstructure:"cpu_count"` + MemorySize int `mapstructure:"memory_size"` + + // device presence + Sound string `mapstructure:"sound"` + USB bool `mapstructure:"usb"` +} + +func (c *HWConfig) Prepare(ctx *interpolate.Context) []error { + var errs []error + + // Hardware and cpu options + if c.CpuCount < 0 { + errs = append(errs, fmt.Errorf("An invalid cpu_count was specified (cpu_count < 0): %d", c.CpuCount)) + c.CpuCount = 0 + } + if c.CpuCount == 0 { + c.CpuCount = 1 + } + + if c.MemorySize < 0 { + errs = append(errs, fmt.Errorf("An invalid memory_size was specified (memory_size < 0): %d", c.MemorySize)) + c.MemorySize = 0 + } + if c.MemorySize == 0 { + c.MemorySize = 512 + } + + // devices + if c.Sound == "" { + c.Sound = "none" + } + + return nil +} diff --git a/builder/virtualbox/common/hw_config_test.go b/builder/virtualbox/common/hw_config_test.go new file mode 100644 index 000000000..079868a7e --- /dev/null +++ b/builder/virtualbox/common/hw_config_test.go @@ -0,0 +1,20 @@ +package common + +import ( + "testing" +) + +func TestHWConfigPrepare(t *testing.T) { + c := new(HWConfig) + if errs := c.Prepare(testConfigTemplate(t)); len(errs) > 0 { + t.Fatalf("err: %#v", errs) + } + + if c.CpuCount < 1 { + t.Errorf("bad cpu count: %d", c.CpuCount) + } + + if c.MemorySize < 64 { + t.Errorf("bad memory size: %d", c.MemorySize) + } +} diff --git a/builder/virtualbox/iso/builder.go b/builder/virtualbox/iso/builder.go index df0c9630b..cb8813896 100644 --- a/builder/virtualbox/iso/builder.go +++ b/builder/virtualbox/iso/builder.go @@ -35,6 +35,7 @@ type Config struct { vboxcommon.RunConfig `mapstructure:",squash"` vboxcommon.ShutdownConfig `mapstructure:",squash"` vboxcommon.SSHConfig `mapstructure:",squash"` + vboxcommon.HWConfig `mapstructure:",squash"` vboxcommon.VBoxManageConfig `mapstructure:",squash"` vboxcommon.VBoxManagePostConfig `mapstructure:",squash"` vboxcommon.VBoxVersionConfig `mapstructure:",squash"` @@ -92,6 +93,7 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) { errs = packer.MultiErrorAppend(errs, b.config.RunConfig.Prepare(&b.config.ctx)...) errs = packer.MultiErrorAppend(errs, b.config.ShutdownConfig.Prepare(&b.config.ctx)...) errs = packer.MultiErrorAppend(errs, b.config.SSHConfig.Prepare(&b.config.ctx)...) + errs = packer.MultiErrorAppend(errs, b.config.HWConfig.Prepare(&b.config.ctx)...) errs = packer.MultiErrorAppend(errs, b.config.VBoxManageConfig.Prepare(&b.config.ctx)...) errs = packer.MultiErrorAppend(errs, b.config.VBoxManagePostConfig.Prepare(&b.config.ctx)...) errs = packer.MultiErrorAppend(errs, b.config.VBoxVersionConfig.Prepare(&b.config.ctx)...) diff --git a/builder/virtualbox/iso/step_create_vm.go b/builder/virtualbox/iso/step_create_vm.go index 72a556737..2a903db93 100644 --- a/builder/virtualbox/iso/step_create_vm.go +++ b/builder/virtualbox/iso/step_create_vm.go @@ -3,6 +3,8 @@ package iso import ( "context" "fmt" + "strconv" + "strings" vboxcommon "github.com/hashicorp/packer/builder/virtualbox/common" "github.com/hashicorp/packer/helper/multistep" @@ -24,7 +26,7 @@ func (s *stepCreateVM) Run(_ context.Context, state multistep.StateBag) multiste name := config.VMName - commands := make([][]string, 4) + commands := make([][]string, 6) commands[0] = []string{ "createvm", "--name", name, "--ostype", config.GuestOSType, "--register", @@ -33,8 +35,15 @@ func (s *stepCreateVM) Run(_ context.Context, state multistep.StateBag) multiste "modifyvm", name, "--boot1", "disk", "--boot2", "dvd", "--boot3", "none", "--boot4", "none", } - commands[2] = []string{"modifyvm", name, "--cpus", "1"} - commands[3] = []string{"modifyvm", name, "--memory", "512"} + 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" { + commands[5] = []string{"modifyvm", name, "--audio", config.HWConfig.Sound} + } else { + commands[5] = []string{"modifyvm", name, "--audio", config.HWConfig.Sound, "--audioin", "on", "--audioout", "on"} + } ui.Say("Creating virtual machine...") for _, command := range commands {