From dcae79e67f2649dc2091acb4a501eed3af24a58d Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Thu, 19 Dec 2013 08:47:37 -0800 Subject: [PATCH] builder/virtualbox: remove devices in separate step --- builder/virtualbox/builder.go | 1 + builder/virtualbox/step_export.go | 21 ++------ builder/virtualbox/step_remove_devices.go | 59 +++++++++++++++++++++++ 3 files changed, 63 insertions(+), 18 deletions(-) create mode 100644 builder/virtualbox/step_remove_devices.go diff --git a/builder/virtualbox/builder.go b/builder/virtualbox/builder.go index 31e265ad0..1091858ec 100644 --- a/builder/virtualbox/builder.go +++ b/builder/virtualbox/builder.go @@ -419,6 +419,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe new(stepUploadGuestAdditions), new(common.StepProvision), new(stepShutdown), + new(stepRemoveDevices), new(stepExport), } diff --git a/builder/virtualbox/step_export.go b/builder/virtualbox/step_export.go index 726af7937..3a875c5d7 100644 --- a/builder/virtualbox/step_export.go +++ b/builder/virtualbox/step_export.go @@ -29,7 +29,9 @@ func (s *stepExport) Run(state multistep.StateBag) multistep.StepAction { // Clear out the Packer-created forwarding rule ui.Say("Preparing to export machine...") - ui.Message(fmt.Sprintf("Deleting forwarded port mapping for SSH (host port %d)", state.Get("sshHostPort"))) + ui.Message(fmt.Sprintf( + "Deleting forwarded port mapping for SSH (host port %d)", + state.Get("sshHostPort"))) command := []string{"modifyvm", vmName, "--natpf1", "delete", "packerssh"} if err := driver.VBoxManage(command...); err != nil { err := fmt.Errorf("Error deleting port forwarding rule: %s", err) @@ -38,23 +40,6 @@ func (s *stepExport) Run(state multistep.StateBag) multistep.StepAction { return multistep.ActionHalt } - // Remove the attached floppy disk, if it exists - if _, ok := state.GetOk("floppy_path"); ok { - ui.Message("Removing floppy drive...") - command := []string{ - "storageattach", vmName, - "--storagectl", "Floppy Controller", - "--port", "0", - "--device", "0", - "--medium", "none", - } - if err := driver.VBoxManage(command...); err != nil { - state.Put("error", fmt.Errorf("Error removing floppy: %s", err)) - return multistep.ActionHalt - } - - } - // Export the VM to an OVF outputPath := filepath.Join(config.OutputDir, vmName+"."+config.Format) diff --git a/builder/virtualbox/step_remove_devices.go b/builder/virtualbox/step_remove_devices.go new file mode 100644 index 000000000..f0f4a46c3 --- /dev/null +++ b/builder/virtualbox/step_remove_devices.go @@ -0,0 +1,59 @@ +package virtualbox + +import ( + "fmt" + "github.com/mitchellh/multistep" + "github.com/mitchellh/packer/packer" +) + +// This step removes any devices (floppy disks, ISOs, etc.) from the +// machine that we may have added. +// +// Uses: +// +// Produces: +type stepRemoveDevices struct{} + +func (s *stepRemoveDevices) Run(state multistep.StateBag) multistep.StepAction { + driver := state.Get("driver").(Driver) + ui := state.Get("ui").(packer.Ui) + vmName := state.Get("vmName").(string) + + // Remove the attached floppy disk, if it exists + if _, ok := state.GetOk("floppy_path"); ok { + ui.Message("Removing floppy drive...") + command := []string{ + "storageattach", vmName, + "--storagectl", "Floppy Controller", + "--port", "0", + "--device", "0", + "--medium", "none", + } + if err := driver.VBoxManage(command...); err != nil { + err := fmt.Errorf("Error removing floppy: %s", err) + state.Put("error", err) + ui.Error(err.Error()) + return multistep.ActionHalt + } + } + + command := []string{ + "storageattach", vmName, + "--storagectl", "IDE Controller", + "--port", "0", + "--device", "1", + "--medium", "none", + } + + if err := driver.VBoxManage(command...); err != nil { + err := fmt.Errorf("Error detaching ISO: %s", err) + state.Put("error", err) + ui.Error(err.Error()) + return multistep.ActionHalt + } + + return multistep.ActionContinue +} + +func (s *stepRemoveDevices) Cleanup(state multistep.StateBag) { +}