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"
|
2019-04-08 11:57:27 -04:00
|
|
|
"time"
|
2017-03-24 02:58:15 -04:00
|
|
|
|
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"
|
2020-11-12 17:44:02 -05:00
|
|
|
"github.com/hashicorp/packer/packer-plugin-sdk/retry"
|
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 {
|
2020-09-11 17:15:17 -04:00
|
|
|
Bundling VBoxBundleConfig
|
2018-02-26 11:47:59 -05:00
|
|
|
}
|
2013-12-19 11:47:37 -05:00
|
|
|
|
2019-03-29 11:50:02 -04:00
|
|
|
func (s *StepRemoveDevices) Run(ctx 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
|
|
|
// Retry for 10 minutes to remove the floppy controller.
|
|
|
|
log.Printf("Trying for 10 minutes to remove floppy controller.")
|
2019-04-08 11:57:27 -04:00
|
|
|
err := retry.Config{
|
|
|
|
Tries: 40,
|
|
|
|
RetryDelay: (&retry.Backoff{InitialBackoff: 15 * time.Second, MaxBackoff: 15 * time.Second, Multiplier: 2}).Linear,
|
|
|
|
}.Run(ctx, func(ctx context.Context) 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",
|
|
|
|
}
|
2019-04-08 11:57:27 -04:00
|
|
|
err := driver.VBoxManage(command...)
|
|
|
|
if err != nil {
|
2017-03-24 14:56:40 -04:00
|
|
|
log.Printf("Error removing floppy controller. Retrying.")
|
2017-03-24 02:58:15 -04:00
|
|
|
}
|
2019-04-08 11:57:27 -04:00
|
|
|
return err
|
2017-03-24 02:58:15 -04:00
|
|
|
})
|
2019-04-08 11:57:27 -04:00
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Error removing floppy controller: %s", err)
|
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
|
|
|
}
|
|
|
|
|
2020-09-11 17:15:17 -04:00
|
|
|
var isoUnmountCommands map[string][]string
|
|
|
|
isoUnmountCommandsRaw, ok := state.GetOk("disk_unmount_commands")
|
|
|
|
if !ok {
|
|
|
|
// No disks to unmount
|
|
|
|
return multistep.ActionContinue
|
|
|
|
} else {
|
|
|
|
isoUnmountCommands = isoUnmountCommandsRaw.(map[string][]string)
|
2013-12-19 11:47:37 -05:00
|
|
|
}
|
|
|
|
|
2020-09-11 17:15:17 -04:00
|
|
|
for diskCategory, unmountCommand := range isoUnmountCommands {
|
|
|
|
if diskCategory == "boot_iso" && s.Bundling.BundleISO {
|
|
|
|
// skip the unmount if user wants to bundle the iso
|
|
|
|
continue
|
2019-02-08 12:15:15 -05:00
|
|
|
}
|
2020-09-11 17:15:17 -04:00
|
|
|
|
|
|
|
if err := driver.VBoxManage(unmountCommand...); err != nil {
|
|
|
|
err := fmt.Errorf("Error detaching ISO: %s", err)
|
2017-02-02 02:45:48 -05:00
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-14 14:00:35 -04:00
|
|
|
// log that we removed the isos, so we don't waste time trying to do it
|
|
|
|
// in the step_attach_isos cleanup.
|
|
|
|
state.Put("detached_isos", true)
|
|
|
|
|
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
|
|
|
}
|