github.com/rothwerx/packer@v0.9.0/builder/virtualbox/iso/step_attach_iso.go (about)

     1  package iso
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/mitchellh/multistep"
     6  	vboxcommon "github.com/mitchellh/packer/builder/virtualbox/common"
     7  	"github.com/mitchellh/packer/packer"
     8  )
     9  
    10  // This step attaches the ISO to the virtual machine.
    11  //
    12  // Uses:
    13  //
    14  // Produces:
    15  type stepAttachISO struct {
    16  	diskPath string
    17  }
    18  
    19  func (s *stepAttachISO) Run(state multistep.StateBag) multistep.StepAction {
    20  	config := state.Get("config").(*Config)
    21  	driver := state.Get("driver").(vboxcommon.Driver)
    22  	isoPath := state.Get("iso_path").(string)
    23  	ui := state.Get("ui").(packer.Ui)
    24  	vmName := state.Get("vmName").(string)
    25  
    26  	controllerName := "IDE Controller"
    27  	port := "0"
    28  	device := "1"
    29  	if config.ISOInterface == "sata" {
    30  		controllerName = "SATA Controller"
    31  		port = "1"
    32  		device = "0"
    33  	}
    34  
    35  	// Attach the disk to the controller
    36  	command := []string{
    37  		"storageattach", vmName,
    38  		"--storagectl", controllerName,
    39  		"--port", port,
    40  		"--device", device,
    41  		"--type", "dvddrive",
    42  		"--medium", isoPath,
    43  	}
    44  	if err := driver.VBoxManage(command...); err != nil {
    45  		err := fmt.Errorf("Error attaching ISO: %s", err)
    46  		state.Put("error", err)
    47  		ui.Error(err.Error())
    48  		return multistep.ActionHalt
    49  	}
    50  
    51  	// Track the path so that we can unregister it from VirtualBox later
    52  	s.diskPath = isoPath
    53  
    54  	// Set some state so we know to remove
    55  	state.Put("attachedIso", true)
    56  	if controllerName == "SATA Controller" {
    57  		state.Put("attachedIsoOnSata", true)
    58  	}
    59  
    60  	return multistep.ActionContinue
    61  }
    62  
    63  func (s *stepAttachISO) Cleanup(state multistep.StateBag) {
    64  	if s.diskPath == "" {
    65  		return
    66  	}
    67  
    68  	config := state.Get("config").(*Config)
    69  	driver := state.Get("driver").(vboxcommon.Driver)
    70  	vmName := state.Get("vmName").(string)
    71  
    72  	controllerName := "IDE Controller"
    73  	port := "0"
    74  	device := "1"
    75  	if config.ISOInterface == "sata" {
    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  	// Remove the ISO. Note that this will probably fail since
    90  	// stepRemoveDevices does this as well. No big deal.
    91  	driver.VBoxManage(command...)
    92  }