2013-12-22 12:54:00 -05:00
|
|
|
package common
|
2013-12-19 11:47:37 -05:00
|
|
|
|
|
|
|
import (
|
2018-01-22 18:32:33 -05:00
|
|
|
"context"
|
2013-12-19 11:47:37 -05:00
|
|
|
"fmt"
|
2017-03-24 02:58:15 -04:00
|
|
|
"log"
|
|
|
|
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/common"
|
2018-01-19 19:18:44 -05:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/packer"
|
2013-12-19 11:47:37 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// This step removes any devices (floppy disks, ISOs, etc.) from the
|
|
|
|
// machine that we may have added.
|
|
|
|
//
|
|
|
|
// Uses:
|
2013-12-22 12:54:00 -05:00
|
|
|
// driver Driver
|
|
|
|
// ui packer.Ui
|
|
|
|
// vmName string
|
2013-12-19 11:47:37 -05:00
|
|
|
//
|
|
|
|
// Produces:
|
2018-03-07 12:02:30 -05:00
|
|
|
type StepRemoveDevices struct {
|
2018-02-26 11:47:59 -05:00
|
|
|
Bundling VBoxBundleConfig
|
|
|
|
}
|
2013-12-19 11:47:37 -05:00
|
|
|
|
2018-01-22 18:31:41 -05:00
|
|
|
func (s *StepRemoveDevices) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
|
2013-12-22 12:54:00 -05:00
|
|
|
driver := state.Get("driver").(Driver)
|
2013-12-19 11:47:37 -05:00
|
|
|
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
|
|
|
|
}
|
2015-06-08 23:25:21 -04:00
|
|
|
|
2017-03-24 02:58:15 -04:00
|
|
|
var vboxErr error
|
|
|
|
// Retry for 10 minutes to remove the floppy controller.
|
|
|
|
log.Printf("Trying for 10 minutes to remove floppy controller.")
|
2017-06-12 20:34:32 -04:00
|
|
|
err := common.Retry(15, 15, 40, func(_ uint) (bool, error) {
|
2017-03-24 02:58:15 -04:00
|
|
|
// Don't forget to remove the floppy controller as well
|
|
|
|
command = []string{
|
|
|
|
"storagectl", vmName,
|
|
|
|
"--name", "Floppy Controller",
|
|
|
|
"--remove",
|
|
|
|
}
|
|
|
|
vboxErr = driver.VBoxManage(command...)
|
|
|
|
if vboxErr != nil {
|
2017-03-24 14:56:40 -04:00
|
|
|
log.Printf("Error removing floppy controller. Retrying.")
|
2017-03-24 02:58:15 -04:00
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
})
|
|
|
|
if err == common.RetryExhaustedError {
|
|
|
|
err := fmt.Errorf("Error removing floppy controller: %s", vboxErr)
|
2015-06-08 23:25:21 -04:00
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
2013-12-19 11:47:37 -05:00
|
|
|
}
|
|
|
|
|
2018-02-26 11:47:59 -05:00
|
|
|
if !s.Bundling.BundleISO {
|
|
|
|
if _, ok := state.GetOk("attachedIso"); ok {
|
|
|
|
controllerName := "IDE Controller"
|
|
|
|
port := "0"
|
|
|
|
device := "1"
|
|
|
|
if _, ok := state.GetOk("attachedIsoOnSata"); ok {
|
|
|
|
controllerName = "SATA Controller"
|
|
|
|
port = "1"
|
|
|
|
device = "0"
|
|
|
|
}
|
2014-05-23 21:14:24 -04:00
|
|
|
|
2018-02-26 11:47:59 -05:00
|
|
|
command := []string{
|
|
|
|
"storageattach", vmName,
|
|
|
|
"--storagectl", controllerName,
|
|
|
|
"--port", port,
|
|
|
|
"--device", device,
|
|
|
|
"--medium", "none",
|
|
|
|
}
|
2013-12-19 11:47:37 -05:00
|
|
|
|
2018-02-26 11:47:59 -05:00
|
|
|
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
|
|
|
|
}
|
2013-12-22 20:08:07 -05:00
|
|
|
}
|
2013-12-19 11:47:37 -05:00
|
|
|
}
|
|
|
|
|
2017-02-02 02:45:48 -05:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-19 11:47:37 -05:00
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2013-12-22 12:54:00 -05:00
|
|
|
func (s *StepRemoveDevices) Cleanup(state multistep.StateBag) {
|
2013-12-19 11:47:37 -05:00
|
|
|
}
|