github.com/mitchellh/packer@v1.3.2/builder/virtualbox/common/step_remove_devices.go (about)

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