From 19713ca20d93ad5e4227a66488f4436e2ba6d056 Mon Sep 17 00:00:00 2001 From: Andrei Tonkikh Date: Tue, 30 Jan 2018 20:25:05 +0300 Subject: [PATCH] Add support of customizing boot order --- clone/builder.go | 4 +++- clone/config.go | 18 ++++++++++-------- common/step_run.go | 19 ++++++++++++++++++- driver/vm.go | 13 +++++++++++++ iso/builder.go | 4 +++- iso/config.go | 14 ++++++++------ 6 files changed, 55 insertions(+), 17 deletions(-) diff --git a/clone/builder.go b/clone/builder.go index 334484a38..c4838efdf 100644 --- a/clone/builder.go +++ b/clone/builder.go @@ -47,7 +47,9 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe if b.config.Comm.Type != "none" { steps = append(steps, - &common.StepRun{}, + &common.StepRun{ + Config: &b.config.RunConfig, + }, &communicator.StepConnect{ Config: &b.config.Comm, Host: common.CommHost, diff --git a/clone/config.go b/clone/config.go index 950769215..93f680e3f 100644 --- a/clone/config.go +++ b/clone/config.go @@ -9,14 +9,15 @@ import ( ) type Config struct { - packerCommon.PackerConfig `mapstructure:",squash"` - common.ConnectConfig `mapstructure:",squash"` - CloneConfig `mapstructure:",squash"` - common.HardwareConfig `mapstructure:",squash"` - Comm communicator.Config `mapstructure:",squash"` - common.ShutdownConfig `mapstructure:",squash"` - CreateSnapshot bool `mapstructure:"create_snapshot"` - ConvertToTemplate bool `mapstructure:"convert_to_template"` + packerCommon.PackerConfig `mapstructure:",squash"` + common.ConnectConfig `mapstructure:",squash"` + common.RunConfig `mapstructure:",squash"` + CloneConfig `mapstructure:",squash"` + common.HardwareConfig `mapstructure:",squash"` + Comm communicator.Config `mapstructure:",squash"` + common.ShutdownConfig `mapstructure:",squash"` + CreateSnapshot bool `mapstructure:"create_snapshot"` + ConvertToTemplate bool `mapstructure:"convert_to_template"` ctx interpolate.Context } @@ -29,6 +30,7 @@ func NewConfig(raws ...interface{}) (*Config, []string, error) { errs := new(packer.MultiError) errs = packer.MultiErrorAppend(errs, c.Comm.Prepare(&c.ctx)...) + errs = packer.MultiErrorAppend(errs, c.RunConfig.Prepare()...) errs = packer.MultiErrorAppend(errs, c.ConnectConfig.Prepare()...) errs = packer.MultiErrorAppend(errs, c.CloneConfig.Prepare()...) errs = packer.MultiErrorAppend(errs, c.HardwareConfig.Prepare()...) diff --git a/common/step_run.go b/common/step_run.go index 340c226ad..e4210b2ce 100644 --- a/common/step_run.go +++ b/common/step_run.go @@ -5,9 +5,19 @@ import ( "github.com/hashicorp/packer/packer" "fmt" "github.com/jetbrains-infra/packer-builder-vsphere/driver" + "strings" ) +type RunConfig struct { + BootOrder string `mapstructure:"boot_order"` // example: "floppy,cdrom,ethernet,disk" +} + +func (c *RunConfig) Prepare() []error { + return nil +} + type StepRun struct { + Config *RunConfig } func (s *StepRun) Run(state multistep.StateBag) multistep.StepAction { @@ -16,9 +26,16 @@ func (s *StepRun) Run(state multistep.StateBag) multistep.StepAction { ui.Say("Power on VM...") + if s.Config.BootOrder != "" { + if err := vm.SetBootOrder(strings.Split(s.Config.BootOrder, ",")); err != nil { + state.Put("error", fmt.Errorf("error selecting boot order: %v", err)) + return multistep.ActionHalt + } + } + err := vm.PowerOn() if err != nil { - state.Put("error", err) + state.Put("error", fmt.Errorf("error powering on VM: %v", err)) return multistep.ActionHalt } diff --git a/driver/vm.go b/driver/vm.go index 74f076370..5a307a0ee 100644 --- a/driver/vm.go +++ b/driver/vm.go @@ -463,6 +463,19 @@ func (vm *VirtualMachine) AddFloppy(imgPath string) error { return vm.addDevice(floppy) } +func (vm *VirtualMachine) SetBootOrder(order []string) error { + devices, err := vm.vm.Device(vm.driver.ctx) + if err != nil { + return err + } + + bootOptions := types.VirtualMachineBootOptions{ + BootOrder: devices.BootOrder(order), + } + + return vm.vm.SetBootOptions(vm.driver.ctx, &bootOptions) +} + func (vm *VirtualMachine) addDevice(device types.BaseVirtualDevice) error { newDevices := object.VirtualDeviceList{device} confSpec := types.VirtualMachineConfigSpec{} diff --git a/iso/builder.go b/iso/builder.go index e55bc1d73..99869c393 100644 --- a/iso/builder.go +++ b/iso/builder.go @@ -49,7 +49,9 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe if b.config.Comm.Type != "none" { steps = append(steps, - &common.StepRun{}, + &common.StepRun{ + Config: &b.config.RunConfig, + }, &communicator.StepConnect{ Config: &b.config.Comm, Host: common.CommHost, diff --git a/iso/config.go b/iso/config.go index c3ca72d5d..6646d3235 100644 --- a/iso/config.go +++ b/iso/config.go @@ -9,12 +9,13 @@ import ( ) type Config struct { - packerCommon.PackerConfig `mapstructure:",squash"` - common.ConnectConfig `mapstructure:",squash"` - Comm communicator.Config `mapstructure:",squash"` - common.ShutdownConfig `mapstructure:",squash"` - CreateSnapshot bool `mapstructure:"create_snapshot"` - ConvertToTemplate bool `mapstructure:"convert_to_template"` + packerCommon.PackerConfig `mapstructure:",squash"` + common.RunConfig `mapstructure:",squash"` + common.ConnectConfig `mapstructure:",squash"` + Comm communicator.Config `mapstructure:",squash"` + common.ShutdownConfig `mapstructure:",squash"` + CreateSnapshot bool `mapstructure:"create_snapshot"` + ConvertToTemplate bool `mapstructure:"convert_to_template"` CreateConfig `mapstructure:",squash"` CDRomConfig `mapstructure:",squash"` @@ -31,6 +32,7 @@ func NewConfig(raws ...interface{}) (*Config, []string, error) { errs := new(packer.MultiError) errs = packer.MultiErrorAppend(errs, c.Comm.Prepare(&c.ctx)...) + errs = packer.MultiErrorAppend(errs, c.RunConfig.Prepare()...) errs = packer.MultiErrorAppend(errs, c.ConnectConfig.Prepare()...) errs = packer.MultiErrorAppend(errs, c.HardwareConfig.Prepare()...) errs = packer.MultiErrorAppend(errs, c.ShutdownConfig.Prepare()...)