github.com/mmcquillan/packer@v1.1.1-0.20171009221028-c85cf0483a5d/builder/virtualbox/common/step_remove_devices.go (about)

     1  package common
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  
     7  	"github.com/hashicorp/packer/common"
     8  	"github.com/hashicorp/packer/packer"
     9  	"github.com/mitchellh/multistep"
    10  )
    11  
    12  // This step removes any devices (floppy disks, ISOs, etc.) from the
    13  // machine that we may have added.
    14  //
    15  // Uses:
    16  //   driver Driver
    17  //   ui packer.Ui
    18  //   vmName string
    19  //
    20  // Produces:
    21  type StepRemoveDevices struct{}
    22  
    23  func (s *StepRemoveDevices) Run(state multistep.StateBag) multistep.StepAction {
    24  	driver := state.Get("driver").(Driver)
    25  	ui := state.Get("ui").(packer.Ui)
    26  	vmName := state.Get("vmName").(string)
    27  
    28  	// Remove the attached floppy disk, if it exists
    29  	if _, ok := state.GetOk("floppy_path"); ok {
    30  		ui.Message("Removing floppy drive...")
    31  		command := []string{
    32  			"storageattach", vmName,
    33  			"--storagectl", "Floppy Controller",
    34  			"--port", "0",
    35  			"--device", "0",
    36  			"--medium", "none",
    37  		}
    38  		if err := driver.VBoxManage(command...); err != nil {
    39  			err := fmt.Errorf("Error removing floppy: %s", err)
    40  			state.Put("error", err)
    41  			ui.Error(err.Error())
    42  			return multistep.ActionHalt
    43  		}
    44  
    45  		var vboxErr error
    46  		// Retry for 10 minutes to remove the floppy controller.
    47  		log.Printf("Trying for 10 minutes to remove floppy controller.")
    48  		err := common.Retry(15, 15, 40, func(_ uint) (bool, error) {
    49  			// Don't forget to remove the floppy controller as well
    50  			command = []string{
    51  				"storagectl", vmName,
    52  				"--name", "Floppy Controller",
    53  				"--remove",
    54  			}
    55  			vboxErr = driver.VBoxManage(command...)
    56  			if vboxErr != nil {
    57  				log.Printf("Error removing floppy controller. Retrying.")
    58  				return false, nil
    59  			}
    60  			return true, nil
    61  		})
    62  		if err == common.RetryExhaustedError {
    63  			err := fmt.Errorf("Error removing floppy controller: %s", vboxErr)
    64  			state.Put("error", err)
    65  			ui.Error(err.Error())
    66  			return multistep.ActionHalt
    67  		}
    68  	}
    69  
    70  	if _, ok := state.GetOk("attachedIso"); ok {
    71  		controllerName := "IDE Controller"
    72  		port := "0"
    73  		device := "1"
    74  		if _, ok := state.GetOk("attachedIsoOnSata"); ok {
    75  			controllerName = "SATA Controller"
    76  			port = "1"
    77  			device = "0"
    78  		}
    79  
    80  		command := []string{
    81  			"storageattach", vmName,
    82  			"--storagectl", controllerName,
    83  			"--port", port,
    84  			"--device", device,
    85  			"--medium", "none",
    86  		}
    87  
    88  		if err := driver.VBoxManage(command...); err != nil {
    89  			err := fmt.Errorf("Error detaching ISO: %s", err)
    90  			state.Put("error", err)
    91  			ui.Error(err.Error())
    92  			return multistep.ActionHalt
    93  		}
    94  	}
    95  
    96  	if _, ok := state.GetOk("guest_additions_attached"); ok {
    97  		ui.Message("Removing guest additions drive...")
    98  		command := []string{
    99  			"storageattach", vmName,
   100  			"--storagectl", "IDE Controller",
   101  			"--port", "1",
   102  			"--device", "0",
   103  			"--medium", "none",
   104  		}
   105  		if err := driver.VBoxManage(command...); err != nil {
   106  			err := fmt.Errorf("Error removing guest additions: %s", err)
   107  			state.Put("error", err)
   108  			ui.Error(err.Error())
   109  			return multistep.ActionHalt
   110  		}
   111  	}
   112  
   113  	return multistep.ActionContinue
   114  }
   115  
   116  func (s *StepRemoveDevices) Cleanup(state multistep.StateBag) {
   117  }