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