From 05affceaefbb8ce56fd0c3e5f61601252681d8ed Mon Sep 17 00:00:00 2001 From: Mikhail Zholobov Date: Thu, 27 Nov 2014 15:20:19 +0300 Subject: [PATCH 1/2] builder/parallels: Attach bootable ISO exactly to cdrom0 [GH-1667] --- builder/parallels/iso/step_attach_iso.go | 59 ++++++++---------------- 1 file changed, 19 insertions(+), 40 deletions(-) diff --git a/builder/parallels/iso/step_attach_iso.go b/builder/parallels/iso/step_attach_iso.go index 428b12fdb..188846331 100644 --- a/builder/parallels/iso/step_attach_iso.go +++ b/builder/parallels/iso/step_attach_iso.go @@ -17,9 +17,8 @@ import ( // vmName string // // Produces: -type stepAttachISO struct { - cdromDevice string -} +// attachedIso bool +type stepAttachISO struct{} func (s *stepAttachISO) Run(state multistep.StateBag) multistep.StepAction { driver := state.Get("driver").(parallelscommon.Driver) @@ -27,22 +26,11 @@ func (s *stepAttachISO) Run(state multistep.StateBag) multistep.StepAction { ui := state.Get("ui").(packer.Ui) vmName := state.Get("vmName").(string) - // Attach the disk to the controller - ui.Say("Attaching ISO to the new CD/DVD drive...") - cdrom, err := driver.DeviceAddCdRom(vmName, isoPath) - - if err != nil { - err := fmt.Errorf("Error attaching ISO: %s", err) - state.Put("error", err) - ui.Error(err.Error()) - return multistep.ActionHalt - } - // Set new boot order ui.Say("Setting the boot order...") command := []string{ "set", vmName, - "--device-bootorder", fmt.Sprintf("hdd0 %s cdrom0 net0", cdrom), + "--device-bootorder", fmt.Sprintf("hdd0 cdrom0 net0"), } if err := driver.Prlctl(command...); err != nil { @@ -52,51 +40,42 @@ func (s *stepAttachISO) Run(state multistep.StateBag) multistep.StepAction { return multistep.ActionHalt } - // Disable 'cdrom0' device - ui.Say("Disabling default CD/DVD drive...") + // Attach the disk to the cdrom0 device. We couldn't use a separated device because it is failed to boot in PD9 [GH-1667] + ui.Say("Attaching ISO to the default CD/DVD ROM device...") command = []string{ "set", vmName, - "--device-set", "cdrom0", "--disable", + "--device-set", "cdrom0", + "--image", isoPath, + "--enable", "--connect", } - if err := driver.Prlctl(command...); err != nil { - err := fmt.Errorf("Error disabling default CD/DVD drive: %s", err) + err := fmt.Errorf("Error attaching ISO: %s", err) state.Put("error", err) ui.Error(err.Error()) return multistep.ActionHalt } - // Track the device name so that we can can delete later - s.cdromDevice = cdrom + // Set some state so we know to remove + state.Put("attachedIso", true) return multistep.ActionContinue } func (s *stepAttachISO) Cleanup(state multistep.StateBag) { + if _, ok := state.GetOk("attachedIso"); !ok { + return + } + driver := state.Get("driver").(parallelscommon.Driver) ui := state.Get("ui").(packer.Ui) vmName := state.Get("vmName").(string) - // Enable 'cdrom0' device back - log.Println("Enabling default CD/DVD drive...") + // Detach ISO by setting an empty string image. + log.Println("Detaching ISO from the default CD/DVD ROM device...") command := []string{ "set", vmName, - "--device-set", "cdrom0", "--enable", "--disconnect", - } - - if err := driver.Prlctl(command...); err != nil { - ui.Error(fmt.Sprintf("Error enabling default CD/DVD drive: %s", err)) - } - - // Detach ISO - if s.cdromDevice == "" { - return - } - - log.Println("Detaching ISO...") - command = []string{ - "set", vmName, - "--device-del", s.cdromDevice, + "--device-set", "cdrom0", + "--image", "", "--disconnect", "--enable", } if err := driver.Prlctl(command...); err != nil { From a5b21c59ba3012cc10bbffc6940916ba5e0de4b2 Mon Sep 17 00:00:00 2001 From: Mikhail Zholobov Date: Thu, 27 Nov 2014 17:13:50 +0300 Subject: [PATCH 2/2] builder/parallels: Move setting the boot order to the separated step --- builder/parallels/iso/builder.go | 1 + builder/parallels/iso/step_attach_iso.go | 16 +------- builder/parallels/iso/step_set_boot_order.go | 42 ++++++++++++++++++++ 3 files changed, 44 insertions(+), 15 deletions(-) create mode 100644 builder/parallels/iso/step_set_boot_order.go diff --git a/builder/parallels/iso/builder.go b/builder/parallels/iso/builder.go index 5b4c628a1..e5e42e8e7 100644 --- a/builder/parallels/iso/builder.go +++ b/builder/parallels/iso/builder.go @@ -256,6 +256,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe new(stepHTTPServer), new(stepCreateVM), new(stepCreateDisk), + new(stepSetBootOrder), new(stepAttachISO), ¶llelscommon.StepAttachParallelsTools{ ParallelsToolsMode: b.config.ParallelsToolsMode, diff --git a/builder/parallels/iso/step_attach_iso.go b/builder/parallels/iso/step_attach_iso.go index 188846331..049f2edf3 100644 --- a/builder/parallels/iso/step_attach_iso.go +++ b/builder/parallels/iso/step_attach_iso.go @@ -26,23 +26,9 @@ func (s *stepAttachISO) Run(state multistep.StateBag) multistep.StepAction { ui := state.Get("ui").(packer.Ui) vmName := state.Get("vmName").(string) - // Set new boot order - ui.Say("Setting the boot order...") - command := []string{ - "set", vmName, - "--device-bootorder", fmt.Sprintf("hdd0 cdrom0 net0"), - } - - if err := driver.Prlctl(command...); err != nil { - err := fmt.Errorf("Error setting the boot order: %s", err) - state.Put("error", err) - ui.Error(err.Error()) - return multistep.ActionHalt - } - // Attach the disk to the cdrom0 device. We couldn't use a separated device because it is failed to boot in PD9 [GH-1667] ui.Say("Attaching ISO to the default CD/DVD ROM device...") - command = []string{ + command := []string{ "set", vmName, "--device-set", "cdrom0", "--image", isoPath, diff --git a/builder/parallels/iso/step_set_boot_order.go b/builder/parallels/iso/step_set_boot_order.go new file mode 100644 index 000000000..0b98dfc68 --- /dev/null +++ b/builder/parallels/iso/step_set_boot_order.go @@ -0,0 +1,42 @@ +package iso + +import ( + "fmt" + "github.com/mitchellh/multistep" + parallelscommon "github.com/mitchellh/packer/builder/parallels/common" + "github.com/mitchellh/packer/packer" +) + +// This step sets the device boot order for the virtual machine. +// +// Uses: +// driver Driver +// ui packer.Ui +// vmName string +// +// Produces: +type stepSetBootOrder struct{} + +func (s *stepSetBootOrder) Run(state multistep.StateBag) multistep.StepAction { + driver := state.Get("driver").(parallelscommon.Driver) + ui := state.Get("ui").(packer.Ui) + vmName := state.Get("vmName").(string) + + // Set new boot order + ui.Say("Setting the boot order...") + command := []string{ + "set", vmName, + "--device-bootorder", fmt.Sprintf("hdd0 cdrom0 net0"), + } + + if err := driver.Prlctl(command...); err != nil { + err := fmt.Errorf("Error setting the boot order: %s", err) + state.Put("error", err) + ui.Error(err.Error()) + return multistep.ActionHalt + } + + return multistep.ActionContinue +} + +func (s *stepSetBootOrder) Cleanup(state multistep.StateBag) {}