github.com/alouche/packer@v0.3.7/builder/virtualbox/step_attach_iso.go (about) 1 package virtualbox 2 3 import ( 4 "fmt" 5 "github.com/mitchellh/multistep" 6 "github.com/mitchellh/packer/packer" 7 ) 8 9 // This step attaches the ISO to the virtual machine. 10 // 11 // Uses: 12 // 13 // Produces: 14 type stepAttachISO struct { 15 diskPath string 16 } 17 18 func (s *stepAttachISO) Run(state multistep.StateBag) multistep.StepAction { 19 driver := state.Get("driver").(Driver) 20 isoPath := state.Get("iso_path").(string) 21 ui := state.Get("ui").(packer.Ui) 22 vmName := state.Get("vmName").(string) 23 24 // Attach the disk to the controller 25 command := []string{ 26 "storageattach", vmName, 27 "--storagectl", "IDE Controller", 28 "--port", "0", 29 "--device", "1", 30 "--type", "dvddrive", 31 "--medium", isoPath, 32 } 33 if err := driver.VBoxManage(command...); err != nil { 34 err := fmt.Errorf("Error attaching ISO: %s", err) 35 state.Put("error", err) 36 ui.Error(err.Error()) 37 return multistep.ActionHalt 38 } 39 40 // Track the path so that we can unregister it from VirtualBox later 41 s.diskPath = isoPath 42 43 return multistep.ActionContinue 44 } 45 46 func (s *stepAttachISO) Cleanup(state multistep.StateBag) { 47 if s.diskPath == "" { 48 return 49 } 50 51 driver := state.Get("driver").(Driver) 52 ui := state.Get("ui").(packer.Ui) 53 vmName := state.Get("vmName").(string) 54 55 command := []string{ 56 "storageattach", vmName, 57 "--storagectl", "IDE Controller", 58 "--port", "0", 59 "--device", "1", 60 "--medium", "none", 61 } 62 63 if err := driver.VBoxManage(command...); err != nil { 64 ui.Error(fmt.Sprintf("Error unregistering ISO: %s", err)) 65 } 66 }