From 71d15d05c06c6537bb7b697eceff2c95549a101b Mon Sep 17 00:00:00 2001 From: Ali Rizvi-Santiago Date: Sat, 17 Nov 2018 05:16:14 -0600 Subject: [PATCH 1/4] Added support for the cpu_count, memory_size, sound, and usb options to the parallels builder. --- builder/parallels/common/driver_10.go | 24 +++++------ builder/parallels/common/driver_11.go | 24 +++++------ builder/parallels/common/driver_9.go | 14 +++--- builder/parallels/common/hw_config.go | 50 ++++++++++++++++++++++ builder/parallels/common/hw_config_test.go | 20 +++++++++ builder/parallels/iso/builder.go | 2 + builder/parallels/iso/step_create_vm.go | 40 ++++++++++++++--- 7 files changed, 134 insertions(+), 40 deletions(-) create mode 100644 builder/parallels/common/hw_config.go create mode 100644 builder/parallels/common/hw_config_test.go diff --git a/builder/parallels/common/driver_10.go b/builder/parallels/common/driver_10.go index be62251d5..cdcf0f4d5 100644 --- a/builder/parallels/common/driver_10.go +++ b/builder/parallels/common/driver_10.go @@ -7,19 +7,17 @@ type Parallels10Driver struct { // SetDefaultConfiguration applies pre-defined default settings to the VM config. func (d *Parallels10Driver) SetDefaultConfiguration(vmName string) error { - commands := make([][]string, 12) - commands[0] = []string{"set", vmName, "--cpus", "1"} - commands[1] = []string{"set", vmName, "--memsize", "512"} - commands[2] = []string{"set", vmName, "--startup-view", "same"} - commands[3] = []string{"set", vmName, "--on-shutdown", "close"} - commands[4] = []string{"set", vmName, "--on-window-close", "keep-running"} - commands[5] = []string{"set", vmName, "--auto-share-camera", "off"} - commands[6] = []string{"set", vmName, "--smart-guard", "off"} - commands[7] = []string{"set", vmName, "--shared-cloud", "off"} - commands[8] = []string{"set", vmName, "--shared-profile", "off"} - commands[9] = []string{"set", vmName, "--smart-mount", "off"} - commands[10] = []string{"set", vmName, "--sh-app-guest-to-host", "off"} - commands[11] = []string{"set", vmName, "--sh-app-host-to-guest", "off"} + commands := make([][]string, 10) + commands[0] = []string{"set", vmName, "--startup-view", "same"} + commands[1] = []string{"set", vmName, "--on-shutdown", "close"} + commands[2] = []string{"set", vmName, "--on-window-close", "keep-running"} + commands[3] = []string{"set", vmName, "--auto-share-camera", "off"} + commands[4] = []string{"set", vmName, "--smart-guard", "off"} + commands[5] = []string{"set", vmName, "--shared-cloud", "off"} + commands[6] = []string{"set", vmName, "--shared-profile", "off"} + commands[7] = []string{"set", vmName, "--smart-mount", "off"} + commands[8] = []string{"set", vmName, "--sh-app-guest-to-host", "off"} + commands[9] = []string{"set", vmName, "--sh-app-host-to-guest", "off"} for _, command := range commands { err := d.Prlctl(command...) diff --git a/builder/parallels/common/driver_11.go b/builder/parallels/common/driver_11.go index 09ebdeeb5..e167b7837 100644 --- a/builder/parallels/common/driver_11.go +++ b/builder/parallels/common/driver_11.go @@ -38,19 +38,17 @@ func (d *Parallels11Driver) Verify() error { // SetDefaultConfiguration applies pre-defined default settings to the VM config. func (d *Parallels11Driver) SetDefaultConfiguration(vmName string) error { - commands := make([][]string, 12) - commands[0] = []string{"set", vmName, "--cpus", "1"} - commands[1] = []string{"set", vmName, "--memsize", "512"} - commands[2] = []string{"set", vmName, "--startup-view", "headless"} - commands[3] = []string{"set", vmName, "--on-shutdown", "close"} - commands[4] = []string{"set", vmName, "--on-window-close", "keep-running"} - commands[5] = []string{"set", vmName, "--auto-share-camera", "off"} - commands[6] = []string{"set", vmName, "--smart-guard", "off"} - commands[7] = []string{"set", vmName, "--shared-cloud", "off"} - commands[8] = []string{"set", vmName, "--shared-profile", "off"} - commands[9] = []string{"set", vmName, "--smart-mount", "off"} - commands[10] = []string{"set", vmName, "--sh-app-guest-to-host", "off"} - commands[11] = []string{"set", vmName, "--sh-app-host-to-guest", "off"} + commands := make([][]string, 10) + commands[0] = []string{"set", vmName, "--startup-view", "headless"} + commands[1] = []string{"set", vmName, "--on-shutdown", "close"} + commands[2] = []string{"set", vmName, "--on-window-close", "keep-running"} + commands[3] = []string{"set", vmName, "--auto-share-camera", "off"} + commands[4] = []string{"set", vmName, "--smart-guard", "off"} + commands[5] = []string{"set", vmName, "--shared-cloud", "off"} + commands[6] = []string{"set", vmName, "--shared-profile", "off"} + commands[7] = []string{"set", vmName, "--smart-mount", "off"} + commands[8] = []string{"set", vmName, "--sh-app-guest-to-host", "off"} + commands[9] = []string{"set", vmName, "--sh-app-host-to-guest", "off"} for _, command := range commands { err := d.Prlctl(command...) diff --git a/builder/parallels/common/driver_9.go b/builder/parallels/common/driver_9.go index 5634fb123..67600523a 100644 --- a/builder/parallels/common/driver_9.go +++ b/builder/parallels/common/driver_9.go @@ -331,14 +331,12 @@ func prepend(head string, tail []string) []string { // SetDefaultConfiguration applies pre-defined default settings to the VM config. func (d *Parallels9Driver) SetDefaultConfiguration(vmName string) error { - commands := make([][]string, 7) - commands[0] = []string{"set", vmName, "--cpus", "1"} - commands[1] = []string{"set", vmName, "--memsize", "512"} - commands[2] = []string{"set", vmName, "--startup-view", "same"} - commands[3] = []string{"set", vmName, "--on-shutdown", "close"} - commands[4] = []string{"set", vmName, "--on-window-close", "keep-running"} - commands[5] = []string{"set", vmName, "--auto-share-camera", "off"} - commands[6] = []string{"set", vmName, "--smart-guard", "off"} + commands := make([][]string, 5) + commands[0] = []string{"set", vmName, "--startup-view", "same"} + commands[1] = []string{"set", vmName, "--on-shutdown", "close"} + commands[2] = []string{"set", vmName, "--on-window-close", "keep-running"} + commands[3] = []string{"set", vmName, "--auto-share-camera", "off"} + commands[4] = []string{"set", vmName, "--smart-guard", "off"} for _, command := range commands { err := d.Prlctl(command...) diff --git a/builder/parallels/common/hw_config.go b/builder/parallels/common/hw_config.go new file mode 100644 index 000000000..1f73f88ab --- /dev/null +++ b/builder/parallels/common/hw_config.go @@ -0,0 +1,50 @@ +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 bool `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 + } + + // Peripherals + if !c.Sound { + c.Sound = false + } + + if !c.USB { + c.USB = false + } + + return nil +} diff --git a/builder/parallels/common/hw_config_test.go b/builder/parallels/common/hw_config_test.go new file mode 100644 index 000000000..079868a7e --- /dev/null +++ b/builder/parallels/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/parallels/iso/builder.go b/builder/parallels/iso/builder.go index b3609cf7f..6d91b2a86 100644 --- a/builder/parallels/iso/builder.go +++ b/builder/parallels/iso/builder.go @@ -29,6 +29,7 @@ type Config struct { common.FloppyConfig `mapstructure:",squash"` bootcommand.BootConfig `mapstructure:",squash"` parallelscommon.OutputConfig `mapstructure:",squash"` + parallelscommon.HWConfig `mapstructure:",squash"` parallelscommon.PrlctlConfig `mapstructure:",squash"` parallelscommon.PrlctlPostConfig `mapstructure:",squash"` parallelscommon.PrlctlVersionConfig `mapstructure:",squash"` @@ -76,6 +77,7 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) { errs = packer.MultiErrorAppend(errs, b.config.FloppyConfig.Prepare(&b.config.ctx)...) errs = packer.MultiErrorAppend( errs, b.config.OutputConfig.Prepare(&b.config.ctx, &b.config.PackerConfig)...) + errs = packer.MultiErrorAppend(errs, b.config.HWConfig.Prepare(&b.config.ctx)...) errs = packer.MultiErrorAppend(errs, b.config.PrlctlConfig.Prepare(&b.config.ctx)...) errs = packer.MultiErrorAppend(errs, b.config.PrlctlPostConfig.Prepare(&b.config.ctx)...) errs = packer.MultiErrorAppend(errs, b.config.PrlctlVersionConfig.Prepare(&b.config.ctx)...) diff --git a/builder/parallels/iso/step_create_vm.go b/builder/parallels/iso/step_create_vm.go index 8a4e22517..6ec9049eb 100644 --- a/builder/parallels/iso/step_create_vm.go +++ b/builder/parallels/iso/step_create_vm.go @@ -3,6 +3,7 @@ package iso import ( "context" "fmt" + "strconv" parallelscommon "github.com/hashicorp/packer/builder/parallels/common" "github.com/hashicorp/packer/helper/multistep" @@ -24,19 +25,46 @@ func (s *stepCreateVM) Run(_ context.Context, state multistep.StateBag) multiste ui := state.Get("ui").(packer.Ui) name := config.VMName - command := []string{ + commands := make([][]string, 3) + + commands[0] = []string{ "create", name, "--distribution", config.GuestOSType, "--dst", config.OutputDir, "--no-hdd", } + commands[1] = []string{ + "set", name, + "--cpus", strconv.Itoa(config.HWConfig.CpuCount), + } + commands[2] = []string{ + "set", name, + "--memsize", strconv.Itoa(config.HWConfig.MemorySize), + } + + if config.HWConfig.Sound { + commands = append(commands, []string{ + "set", name, + "--device-add-sound", + "--connect", + }) + } + + if config.HWConfig.USB { + commands = append(commands, []string{ + "set", name, + "--device-add-usb", + }) + } ui.Say("Creating virtual machine...") - if err := driver.Prlctl(command...); err != nil { - err := fmt.Errorf("Error creating VM: %s", err) - state.Put("error", err) - ui.Error(err.Error()) - return multistep.ActionHalt + for _, command := range commands { + if err := driver.Prlctl(command...); err != nil { + err := fmt.Errorf("Error creating VM: %s", err) + state.Put("error", err) + ui.Error(err.Error()) + return multistep.ActionHalt + } } ui.Say("Applying default settings...") From 57f00dfc4945a8a37e10e3eaba23660a03f2b480 Mon Sep 17 00:00:00 2001 From: Ali Rizvi-Santiago Date: Sat, 17 Nov 2018 05:28:02 -0600 Subject: [PATCH 2/4] Added docs for the parallels builder to describe the cpu_count, memory_size, sound, and usb options. --- .../source/docs/builders/parallels-iso.html.md.erb | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/website/source/docs/builders/parallels-iso.html.md.erb b/website/source/docs/builders/parallels-iso.html.md.erb index 5e1b19da4..afb086b1a 100644 --- a/website/source/docs/builders/parallels-iso.html.md.erb +++ b/website/source/docs/builders/parallels-iso.html.md.erb @@ -102,6 +102,9 @@ builder. five seconds and one minute 30 seconds, respectively. If this isn't specified, the default is 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). @@ -173,6 +176,9 @@ builder. URLs must point to the same file (same checksum). By default this is empty and `iso_url` is used. Only one of `iso_url` or `iso_urls` can be specified. +- `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` @@ -220,6 +226,9 @@ builder. machine once all the provisioning is done. By default this is an empty string, which tells Packer to just forcefully shut down the machine. +- `sound` (boolean) - Specifies whether to enable the sound device when + building the VM. Defaults to `false`. + - `shutdown_timeout` (string) - The amount of time to wait after executing the `shutdown_command` for the virtual machine to actually shut down. If it doesn't shut down in this time, it is an error. By default, the timeout is @@ -231,6 +240,9 @@ builder. the resulting disk image. If you find this to be the case, you can disable compaction using this configuration value. +- `usb` (boolean) - Specifies whether to enable the USB bus when building + the VM. Defaults to `false`. + - `vm_name` (string) - This is the name of the PVM directory for the new virtual machine, without the file extension. By default this is "packer-BUILDNAME", where "BUILDNAME" is the name of the build. From 24140131c78a710f932cba2194a048ea73e13619 Mon Sep 17 00:00:00 2001 From: Ali Rizvi-Santiago Date: Sat, 17 Nov 2018 06:31:49 -0600 Subject: [PATCH 3/4] Renamed both `cpu_count` and `memory_size` to `cpus` and `memory` (respective). --- builder/parallels/common/hw_config.go | 8 ++++---- website/source/docs/builders/parallels-iso.html.md.erb | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/builder/parallels/common/hw_config.go b/builder/parallels/common/hw_config.go index 1f73f88ab..85f6d58b5 100644 --- a/builder/parallels/common/hw_config.go +++ b/builder/parallels/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 bool `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/parallels-iso.html.md.erb b/website/source/docs/builders/parallels-iso.html.md.erb index afb086b1a..e66e4b7bf 100644 --- a/website/source/docs/builders/parallels-iso.html.md.erb +++ b/website/source/docs/builders/parallels-iso.html.md.erb @@ -102,7 +102,7 @@ builder. five seconds and one minute 30 seconds, respectively. If this isn't specified, the default is 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 @@ -176,7 +176,7 @@ builder. URLs must point to the same file (same checksum). By default this is empty and `iso_url` is used. Only one of `iso_url` or `iso_urls` can be specified. -- `memory_size` (number) - The amount of memory to use for building the VM in +- `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 784535a4e32bd93c229081f3c86da315834777ac Mon Sep 17 00:00:00 2001 From: Ali Rizvi-Santiago Date: Thu, 22 Nov 2018 20:19:00 -0600 Subject: [PATCH 4/4] Updated things related to the parallels-iso builder to correspond to @azr's suggestions. --- builder/parallels/common/hw_config.go | 4 +--- website/source/docs/builders/parallels-iso.html.md.erb | 2 +- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/builder/parallels/common/hw_config.go b/builder/parallels/common/hw_config.go index 85f6d58b5..bc51c97a6 100644 --- a/builder/parallels/common/hw_config.go +++ b/builder/parallels/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 @@ -46,5 +44,5 @@ func (c *HWConfig) Prepare(ctx *interpolate.Context) []error { c.USB = false } - return nil + return errs } diff --git a/website/source/docs/builders/parallels-iso.html.md.erb b/website/source/docs/builders/parallels-iso.html.md.erb index e66e4b7bf..0a109efe3 100644 --- a/website/source/docs/builders/parallels-iso.html.md.erb +++ b/website/source/docs/builders/parallels-iso.html.md.erb @@ -103,7 +103,7 @@ builder. specified, the default is 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).