github.phpd.cn/hashicorp/packer@v1.3.2/builder/virtualbox/iso/step_attach_iso.go (about) 1 package iso 2 3 import ( 4 "context" 5 "fmt" 6 "path/filepath" 7 8 vboxcommon "github.com/hashicorp/packer/builder/virtualbox/common" 9 "github.com/hashicorp/packer/helper/multistep" 10 "github.com/hashicorp/packer/packer" 11 ) 12 13 // This step attaches the ISO to the virtual machine. 14 // 15 // Uses: 16 // 17 // Produces: 18 type stepAttachISO struct { 19 diskPath string 20 } 21 22 func (s *stepAttachISO) Run(_ context.Context, state multistep.StateBag) multistep.StepAction { 23 config := state.Get("config").(*Config) 24 driver := state.Get("driver").(vboxcommon.Driver) 25 isoPath := state.Get("iso_path").(string) 26 ui := state.Get("ui").(packer.Ui) 27 vmName := state.Get("vmName").(string) 28 29 controllerName := "IDE Controller" 30 port := "0" 31 device := "1" 32 if config.ISOInterface == "sata" { 33 controllerName = "SATA Controller" 34 port = "1" 35 device = "0" 36 } 37 38 // If it's a symlink, resolve it to it's target. 39 resolvedIsoPath, err := filepath.EvalSymlinks(isoPath) 40 if err != nil { 41 err := fmt.Errorf("Error resolving symlink for ISO: %s", err) 42 state.Put("error", err) 43 ui.Error(err.Error()) 44 return multistep.ActionHalt 45 } 46 isoPath = resolvedIsoPath 47 48 // Attach the disk to the controller 49 command := []string{ 50 "storageattach", vmName, 51 "--storagectl", controllerName, 52 "--port", port, 53 "--device", device, 54 "--type", "dvddrive", 55 "--medium", isoPath, 56 } 57 if err := driver.VBoxManage(command...); err != nil { 58 err := fmt.Errorf("Error attaching ISO: %s", err) 59 state.Put("error", err) 60 ui.Error(err.Error()) 61 return multistep.ActionHalt 62 } 63 64 // Track the path so that we can unregister it from VirtualBox later 65 s.diskPath = isoPath 66 67 // Set some state so we know to remove 68 state.Put("attachedIso", true) 69 if controllerName == "SATA Controller" { 70 state.Put("attachedIsoOnSata", true) 71 } 72 73 return multistep.ActionContinue 74 } 75 76 func (s *stepAttachISO) Cleanup(state multistep.StateBag) { 77 if s.diskPath == "" { 78 return 79 } 80 81 config := state.Get("config").(*Config) 82 driver := state.Get("driver").(vboxcommon.Driver) 83 vmName := state.Get("vmName").(string) 84 85 controllerName := "IDE Controller" 86 port := "0" 87 device := "1" 88 if config.ISOInterface == "sata" { 89 controllerName = "SATA Controller" 90 port = "1" 91 device = "0" 92 } 93 94 command := []string{ 95 "storageattach", vmName, 96 "--storagectl", controllerName, 97 "--port", port, 98 "--device", device, 99 "--medium", "none", 100 } 101 102 // Remove the ISO. Note that this will probably fail since 103 // stepRemoveDevices does this as well. No big deal. 104 driver.VBoxManage(command...) 105 }