From 67eb600c08c6a4bed5438f681acfc8516ddff0f1 Mon Sep 17 00:00:00 2001 From: Ben Phegan Date: Wed, 22 Mar 2017 23:09:25 +1100 Subject: [PATCH] Add sata_port_count so that a SATA controller can be created with more than 1 port. The default of prevents additional drives being added. Default preserved if not overridden. --- builder/virtualbox/common/driver.go | 2 +- builder/virtualbox/common/driver_4_2.go | 5 +++-- builder/virtualbox/iso/builder.go | 10 ++++++++++ builder/virtualbox/iso/step_create_disk.go | 9 +++++---- 4 files changed, 19 insertions(+), 7 deletions(-) diff --git a/builder/virtualbox/common/driver.go b/builder/virtualbox/common/driver.go index add59c7f1..c0085fe2d 100644 --- a/builder/virtualbox/common/driver.go +++ b/builder/virtualbox/common/driver.go @@ -17,7 +17,7 @@ import ( // extremely specific. type Driver interface { // Create a SATA controller. - CreateSATAController(vm string, controller string) error + CreateSATAController(vm string, controller string, portcount int) error // Create a SCSI controller. CreateSCSIController(vm string, controller string) error diff --git a/builder/virtualbox/common/driver_4_2.go b/builder/virtualbox/common/driver_4_2.go index 38c978f1d..4824fd8c9 100644 --- a/builder/virtualbox/common/driver_4_2.go +++ b/builder/virtualbox/common/driver_4_2.go @@ -6,6 +6,7 @@ import ( "log" "os/exec" "regexp" + "strconv" "strings" "time" ) @@ -15,7 +16,7 @@ type VBox42Driver struct { VBoxManagePath string } -func (d *VBox42Driver) CreateSATAController(vmName string, name string) error { +func (d *VBox42Driver) CreateSATAController(vmName string, name string, portcount int) error { version, err := d.Version() if err != nil { return err @@ -30,7 +31,7 @@ func (d *VBox42Driver) CreateSATAController(vmName string, name string) error { "storagectl", vmName, "--name", name, "--add", "sata", - portCountArg, "1", + portCountArg, strconv.Itoa(portcount), } return d.VBoxManage(command...) diff --git a/builder/virtualbox/iso/builder.go b/builder/virtualbox/iso/builder.go index f9def2528..4f61d9960 100644 --- a/builder/virtualbox/iso/builder.go +++ b/builder/virtualbox/iso/builder.go @@ -46,6 +46,7 @@ type Config struct { GuestOSType string `mapstructure:"guest_os_type"` HardDriveDiscard bool `mapstructure:"hard_drive_discard"` HardDriveInterface string `mapstructure:"hard_drive_interface"` + SATAPortCount int `mapstructure:"sata_port_count"` HardDriveNonrotational bool `mapstructure:"hard_drive_nonrotational"` ISOInterface string `mapstructure:"iso_interface"` KeepRegistered bool `mapstructure:"keep_registered"` @@ -128,6 +129,15 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) { errs, errors.New("hard_drive_interface can only be ide, sata, or scsi")) } + if b.config.SATAPortCount == 0 { + b.config.SATAPortCount = 1 + } + + if b.config.SATAPortCount > 30 { + errs = packer.MultiErrorAppend( + errs, errors.New("sata_port_count cannot be greater than 30")) + } + if b.config.ISOInterface != "ide" && b.config.ISOInterface != "sata" { errs = packer.MultiErrorAppend( errs, errors.New("iso_interface can only be ide or sata")) diff --git a/builder/virtualbox/iso/step_create_disk.go b/builder/virtualbox/iso/step_create_disk.go index 52e159687..1d562620f 100644 --- a/builder/virtualbox/iso/step_create_disk.go +++ b/builder/virtualbox/iso/step_create_disk.go @@ -2,12 +2,13 @@ package iso import ( "fmt" - "github.com/mitchellh/multistep" - vboxcommon "github.com/mitchellh/packer/builder/virtualbox/common" - "github.com/mitchellh/packer/packer" "path/filepath" "strconv" "strings" + + "github.com/mitchellh/multistep" + vboxcommon "github.com/mitchellh/packer/builder/virtualbox/common" + "github.com/mitchellh/packer/packer" ) // This step creates the virtual disk that will be used as the @@ -55,7 +56,7 @@ func (s *stepCreateDisk) Run(state multistep.StateBag) multistep.StepAction { // the IDE controller above because some other things (disks) require // that. if config.HardDriveInterface == "sata" || config.ISOInterface == "sata" { - if err := driver.CreateSATAController(vmName, "SATA Controller"); err != nil { + if err := driver.CreateSATAController(vmName, "SATA Controller", config.SATAPortCount); err != nil { err := fmt.Errorf("Error creating disk controller: %s", err) state.Put("error", err) ui.Error(err.Error())