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

     1  package common
     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  	GuestAdditionsMode string
    24  }
    25  
    26  func (s *StepAttachGuestAdditions) Run(state multistep.StateBag) multistep.StepAction {
    27  	driver := state.Get("driver").(Driver)
    28  	ui := state.Get("ui").(packer.Ui)
    29  	vmName := state.Get("vmName").(string)
    30  
    31  	// If we're not attaching the guest additions then just return
    32  	if s.GuestAdditionsMode != GuestAdditionsModeAttach {
    33  		log.Println("Not attaching guest additions since we're uploading.")
    34  		return multistep.ActionContinue
    35  	}
    36  
    37  	// Get the guest additions path since we're doing it
    38  	guestAdditionsPath := state.Get("guest_additions_path").(string)
    39  
    40  	// Attach the guest additions to the computer
    41  	log.Println("Attaching guest additions ISO onto IDE controller...")
    42  	command := []string{
    43  		"storageattach", vmName,
    44  		"--storagectl", "IDE Controller",
    45  		"--port", "1",
    46  		"--device", "0",
    47  		"--type", "dvddrive",
    48  		"--medium", guestAdditionsPath,
    49  	}
    50  	if err := driver.VBoxManage(command...); err != nil {
    51  		err := fmt.Errorf("Error attaching guest additions: %s", err)
    52  		state.Put("error", err)
    53  		ui.Error(err.Error())
    54  		return multistep.ActionHalt
    55  	}
    56  
    57  	// Track the path so that we can unregister it from VirtualBox later
    58  	s.attachedPath = guestAdditionsPath
    59  
    60  	return multistep.ActionContinue
    61  }
    62  
    63  func (s *StepAttachGuestAdditions) Cleanup(state multistep.StateBag) {
    64  	if s.attachedPath == "" {
    65  		return
    66  	}
    67  
    68  	driver := state.Get("driver").(Driver)
    69  	ui := state.Get("ui").(packer.Ui)
    70  	vmName := state.Get("vmName").(string)
    71  
    72  	command := []string{
    73  		"storageattach", vmName,
    74  		"--storagectl", "IDE Controller",
    75  		"--port", "1",
    76  		"--device", "0",
    77  		"--medium", "none",
    78  	}
    79  
    80  	if err := driver.VBoxManage(command...); err != nil {
    81  		ui.Error(fmt.Sprintf("Error unregistering guest additions: %s", err))
    82  	}
    83  }