github.com/mmcquillan/packer@v1.1.1-0.20171009221028-c85cf0483a5d/builder/virtualbox/common/step_attach_guest_additions.go (about) 1 package common 2 3 import ( 4 "fmt" 5 "github.com/hashicorp/packer/packer" 6 "github.com/mitchellh/multistep" 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 state.Put("guest_additions_attached", true) 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").(Driver) 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 // Remove the ISO. Note that this will probably fail since 81 // stepRemoveDevices does this as well. No big deal. 82 driver.VBoxManage(command...) 83 }