github.com/supr/packer@v0.3.10-0.20131015195147-7b09e24ac3c1/builder/virtualbox/step_attach_guest_additions.go (about)

     1  package virtualbox
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/mitchellh/multistep"
     6  	"github.com/mitchellh/packer/packer"
     7  	"log"
     8  )
     9  
    10  // This step attaches the VirtualBox guest additions as a inserted CD onto
    11  // the virtual machine.
    12  //
    13  // Uses:
    14  //   config *config
    15  //   driver Driver
    16  //   guest_additions_path string
    17  //   ui packer.Ui
    18  //   vmName string
    19  //
    20  // Produces:
    21  type stepAttachGuestAdditions struct {
    22  	attachedPath string
    23  }
    24  
    25  func (s *stepAttachGuestAdditions) Run(state multistep.StateBag) multistep.StepAction {
    26  	config := state.Get("config").(*config)
    27  	driver := state.Get("driver").(Driver)
    28  	guestAdditionsPath := state.Get("guest_additions_path").(string)
    29  	ui := state.Get("ui").(packer.Ui)
    30  	vmName := state.Get("vmName").(string)
    31  
    32  	// If we're not attaching the guest additions then just return
    33  	if !config.GuestAdditionsAttach {
    34  		log.Println("Not attaching guest additions since we're uploading.")
    35  		return multistep.ActionContinue
    36  	}
    37  
    38  	// Attach the guest additions to the computer
    39  	log.Println("Attaching guest additions ISO onto IDE controller...")
    40  	command := []string{
    41  		"storageattach", vmName,
    42  		"--storagectl", "IDE Controller",
    43  		"--port", "1",
    44  		"--device", "0",
    45  		"--type", "dvddrive",
    46  		"--medium", guestAdditionsPath,
    47  	}
    48  	if err := driver.VBoxManage(command...); err != nil {
    49  		err := fmt.Errorf("Error attaching guest additions: %s", err)
    50  		state.Put("error", err)
    51  		ui.Error(err.Error())
    52  		return multistep.ActionHalt
    53  	}
    54  
    55  	// Track the path so that we can unregister it from VirtualBox later
    56  	s.attachedPath = guestAdditionsPath
    57  
    58  	return multistep.ActionContinue
    59  }
    60  
    61  func (s *stepAttachGuestAdditions) Cleanup(state multistep.StateBag) {
    62  	if s.attachedPath == "" {
    63  		return
    64  	}
    65  
    66  	driver := state.Get("driver").(Driver)
    67  	ui := state.Get("ui").(packer.Ui)
    68  	vmName := state.Get("vmName").(string)
    69  
    70  	command := []string{
    71  		"storageattach", vmName,
    72  		"--storagectl", "IDE Controller",
    73  		"--port", "1",
    74  		"--device", "0",
    75  		"--medium", "none",
    76  	}
    77  
    78  	if err := driver.VBoxManage(command...); err != nil {
    79  		ui.Error(fmt.Sprintf("Error unregistering guest additions: %s", err))
    80  	}
    81  }