github.com/rothwerx/packer@v0.9.0/builder/virtualbox/common/step_remove_devices.go (about)

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