github.com/sneal/packer@v0.5.2/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  	driver := state.Get("driver").(vboxcommon.Driver)
    21  	isoPath := state.Get("iso_path").(string)
    22  	ui := state.Get("ui").(packer.Ui)
    23  	vmName := state.Get("vmName").(string)
    24  
    25  	// Attach the disk to the controller
    26  	command := []string{
    27  		"storageattach", vmName,
    28  		"--storagectl", "IDE Controller",
    29  		"--port", "0",
    30  		"--device", "1",
    31  		"--type", "dvddrive",
    32  		"--medium", isoPath,
    33  	}
    34  	if err := driver.VBoxManage(command...); err != nil {
    35  		err := fmt.Errorf("Error attaching ISO: %s", err)
    36  		state.Put("error", err)
    37  		ui.Error(err.Error())
    38  		return multistep.ActionHalt
    39  	}
    40  
    41  	// Track the path so that we can unregister it from VirtualBox later
    42  	s.diskPath = isoPath
    43  
    44  	// Set some state so we know to remove
    45  	state.Put("attachedIso", true)
    46  
    47  	return multistep.ActionContinue
    48  }
    49  
    50  func (s *stepAttachISO) Cleanup(state multistep.StateBag) {
    51  	if s.diskPath == "" {
    52  		return
    53  	}
    54  
    55  	driver := state.Get("driver").(vboxcommon.Driver)
    56  	vmName := state.Get("vmName").(string)
    57  
    58  	command := []string{
    59  		"storageattach", vmName,
    60  		"--storagectl", "IDE Controller",
    61  		"--port", "0",
    62  		"--device", "1",
    63  		"--medium", "none",
    64  	}
    65  
    66  	// Remove the ISO. Note that this will probably fail since
    67  	// stepRemoveDevices does this as well. No big deal.
    68  	driver.VBoxManage(command...)
    69  }