From 51948daf9204d0e08f0e64723fc113d60ef976cc Mon Sep 17 00:00:00 2001 From: Ali Rizvi-Santiago Date: Sat, 17 Nov 2018 04:24:32 -0600 Subject: [PATCH 1/5] 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 { From 2249d829b8a352511339c2697e3763b75a5db44f Mon Sep 17 00:00:00 2001 From: Ali Rizvi-Santiago Date: Sat, 17 Nov 2018 04:40:44 -0600 Subject: [PATCH 2/5] Added docs for the virtualbox builders to describe the cpu_count, memory_size, sound, and usb options. --- .../source/docs/builders/virtualbox-iso.html.md.erb | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/website/source/docs/builders/virtualbox-iso.html.md.erb b/website/source/docs/builders/virtualbox-iso.html.md.erb index b97f9eb1c..50ec05ec6 100644 --- a/website/source/docs/builders/virtualbox-iso.html.md.erb +++ b/website/source/docs/builders/virtualbox-iso.html.md.erb @@ -94,6 +94,9 @@ builder. five seconds and one minute 30 seconds, respectively. If this isn't specified, the default is `10s` or 10 seconds. +- `cpu_count` (number) - The number of cpus to use for building the VM. + Defaults to building with just one. + - `disk_size` (number) - The size, in megabytes, of the hard disk to create for the VM. By default, this is `40000` (about 40 GB). @@ -249,6 +252,9 @@ builder. - `keep_registered` (boolean) - Set this to `true` if you would like to keep the VM registered with virtualbox. Defaults to `false`. +- `memory_size` (number) - The amount of memory to use for building the VM + in megabytes. Defaults to `512` megabytes. + - `output_directory` (string) - This is the path to the directory where the resulting virtual machine will be created. This may be relative or absolute. If relative, the path is relative to the working directory when `packer` @@ -278,6 +284,10 @@ builder. not export the VM. Useful if the build output is not the resultant image, but created inside the VM. +- `sound` (string) - Defaults to `none`. The type of audio device to use for + sound when building the VM. Some of the options that are available are + `dsound`, `oss`, `alsa`, `pulse`, `coreaudio`, `null`. + - `ssh_host_port_min` and `ssh_host_port_max` (number) - The minimum and maximum port to use for the SSH port on the host machine which is forwarded to the SSH port on the guest machine. Because Packer often runs in parallel, @@ -288,6 +298,9 @@ builder. does not setup forwarded port mapping for SSH requests and uses `ssh_port` on the host to communicate to the virtual machine. +- `usb` (boolean) - Specifies whether or not to enable the USB bus when + building the VM. Defaults to `false`. + - `vboxmanage` (array of array of strings) - Custom `VBoxManage` commands to execute in order to further customize the virtual machine being created. The value of this is an array of commands to execute. The commands are executed From eca26f43f21e6ba96972bb9f360bbe672ce10004 Mon Sep 17 00:00:00 2001 From: Ali Rizvi-Santiago Date: Sat, 17 Nov 2018 06:34:22 -0600 Subject: [PATCH 3/5] Renamed both `cpu_count` and `memory_size` to `cpus` and `memory` (respective). --- builder/virtualbox/common/hw_config.go | 8 ++++---- website/source/docs/builders/virtualbox-iso.html.md.erb | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/builder/virtualbox/common/hw_config.go b/builder/virtualbox/common/hw_config.go index 48ce852aa..6da48bf7d 100644 --- a/builder/virtualbox/common/hw_config.go +++ b/builder/virtualbox/common/hw_config.go @@ -9,8 +9,8 @@ import ( type HWConfig struct { // cpu information - CpuCount int `mapstructure:"cpu_count"` - MemorySize int `mapstructure:"memory_size"` + CpuCount int `mapstructure:"cpus"` + MemorySize int `mapstructure:"memory"` // device presence Sound string `mapstructure:"sound"` @@ -22,7 +22,7 @@ func (c *HWConfig) Prepare(ctx *interpolate.Context) []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)) + errs = append(errs, fmt.Errorf("An invalid number of cpus was specified (cpus < 0): %d", c.CpuCount)) c.CpuCount = 0 } if c.CpuCount == 0 { @@ -30,7 +30,7 @@ func (c *HWConfig) Prepare(ctx *interpolate.Context) []error { } if c.MemorySize < 0 { - errs = append(errs, fmt.Errorf("An invalid memory_size was specified (memory_size < 0): %d", c.MemorySize)) + errs = append(errs, fmt.Errorf("An invalid memory size was specified (memory < 0): %d", c.MemorySize)) c.MemorySize = 0 } if c.MemorySize == 0 { diff --git a/website/source/docs/builders/virtualbox-iso.html.md.erb b/website/source/docs/builders/virtualbox-iso.html.md.erb index 50ec05ec6..0ebfa3679 100644 --- a/website/source/docs/builders/virtualbox-iso.html.md.erb +++ b/website/source/docs/builders/virtualbox-iso.html.md.erb @@ -94,7 +94,7 @@ builder. five seconds and one minute 30 seconds, respectively. If this isn't specified, the default is `10s` or 10 seconds. -- `cpu_count` (number) - The number of cpus to use for building the VM. +- `cpus` (number) - The number of cpus to use for building the VM. Defaults to building with just one. - `disk_size` (number) - The size, in megabytes, of the hard disk to create @@ -252,7 +252,7 @@ builder. - `keep_registered` (boolean) - Set this to `true` if you would like to keep the VM registered with virtualbox. Defaults to `false`. -- `memory_size` (number) - The amount of memory to use for building the VM +- `memory` (number) - The amount of memory to use for building the VM in megabytes. Defaults to `512` megabytes. - `output_directory` (string) - This is the path to the directory where the From 0f019407d6347d8229a86d53001cfe340e624cdf Mon Sep 17 00:00:00 2001 From: Ali Rizvi-Santiago Date: Thu, 22 Nov 2018 20:48:00 -0600 Subject: [PATCH 4/5] Updated things related to the general hw config in the virtualbox builders to correspond to @azr's suggestions. --- builder/virtualbox/common/hw_config.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/builder/virtualbox/common/hw_config.go b/builder/virtualbox/common/hw_config.go index 6da48bf7d..9e7079237 100644 --- a/builder/virtualbox/common/hw_config.go +++ b/builder/virtualbox/common/hw_config.go @@ -23,7 +23,6 @@ func (c *HWConfig) Prepare(ctx *interpolate.Context) []error { // Hardware and cpu options if c.CpuCount < 0 { errs = append(errs, fmt.Errorf("An invalid number of cpus was specified (cpus < 0): %d", c.CpuCount)) - c.CpuCount = 0 } if c.CpuCount == 0 { c.CpuCount = 1 @@ -31,7 +30,6 @@ func (c *HWConfig) Prepare(ctx *interpolate.Context) []error { if c.MemorySize < 0 { errs = append(errs, fmt.Errorf("An invalid memory size was specified (memory < 0): %d", c.MemorySize)) - c.MemorySize = 0 } if c.MemorySize == 0 { c.MemorySize = 512 @@ -42,5 +40,5 @@ func (c *HWConfig) Prepare(ctx *interpolate.Context) []error { c.Sound = "none" } - return nil + return errs } From e7282a2c27a2fa2c6076a323cefbe811836501d6 Mon Sep 17 00:00:00 2001 From: Ali Rizvi-Santiago Date: Thu, 22 Nov 2018 20:49:16 -0600 Subject: [PATCH 5/5] Updated the docs for the virtualbox-iso builder to include @azr's suggestion. --- website/source/docs/builders/virtualbox-iso.html.md.erb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/website/source/docs/builders/virtualbox-iso.html.md.erb b/website/source/docs/builders/virtualbox-iso.html.md.erb index 0ebfa3679..9fca33b6f 100644 --- a/website/source/docs/builders/virtualbox-iso.html.md.erb +++ b/website/source/docs/builders/virtualbox-iso.html.md.erb @@ -95,7 +95,7 @@ builder. specified, the default is `10s` or 10 seconds. - `cpus` (number) - The number of cpus to use for building the VM. - Defaults to building with just one. + Defaults to `1`. - `disk_size` (number) - The size, in megabytes, of the hard disk to create for the VM. By default, this is `40000` (about 40 GB).