diff --git a/builder/virtualbox/common/step_attach_guest_additions.go b/builder/virtualbox/common/step_attach_guest_additions.go index bb047ed9a..6017079d4 100644 --- a/builder/virtualbox/common/step_attach_guest_additions.go +++ b/builder/virtualbox/common/step_attach_guest_additions.go @@ -56,6 +56,7 @@ func (s *StepAttachGuestAdditions) Run(state multistep.StateBag) multistep.StepA // Track the path so that we can unregister it from VirtualBox later s.attachedPath = guestAdditionsPath + state.Put("guest_additions_attached", true) return multistep.ActionContinue } @@ -66,7 +67,6 @@ func (s *StepAttachGuestAdditions) Cleanup(state multistep.StateBag) { } driver := state.Get("driver").(Driver) - ui := state.Get("ui").(packer.Ui) vmName := state.Get("vmName").(string) command := []string{ @@ -77,7 +77,7 @@ func (s *StepAttachGuestAdditions) Cleanup(state multistep.StateBag) { "--medium", "none", } - if err := driver.VBoxManage(command...); err != nil { - ui.Error(fmt.Sprintf("Error unregistering guest additions: %s", err)) - } + // Remove the ISO. Note that this will probably fail since + // stepRemoveDevices does this as well. No big deal. + driver.VBoxManage(command...) } diff --git a/builder/virtualbox/common/step_remove_devices.go b/builder/virtualbox/common/step_remove_devices.go index c184c7c4f..54b85bbb4 100644 --- a/builder/virtualbox/common/step_remove_devices.go +++ b/builder/virtualbox/common/step_remove_devices.go @@ -79,6 +79,23 @@ func (s *StepRemoveDevices) Run(state multistep.StateBag) multistep.StepAction { } } + if _, ok := state.GetOk("guest_additions_attached"); ok { + ui.Message("Removing guest additions drive...") + command := []string{ + "storageattach", vmName, + "--storagectl", "IDE Controller", + "--port", "1", + "--device", "0", + "--medium", "none", + } + if err := driver.VBoxManage(command...); err != nil { + err := fmt.Errorf("Error removing guest additions: %s", err) + state.Put("error", err) + ui.Error(err.Error()) + return multistep.ActionHalt + } + } + return multistep.ActionContinue }