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