github.phpd.cn/hashicorp/packer@v1.3.2/builder/parallels/common/step_attach_floppy.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 // StepAttachFloppy is a step that attaches a floppy to the virtual machine. 13 // 14 // Uses: 15 // driver Driver 16 // ui packer.Ui 17 // vmName string 18 // 19 // Produces: 20 type StepAttachFloppy struct { 21 floppyPath string 22 } 23 24 // Run adds a virtual FDD device to the VM and attaches the image. 25 // If the image is not specified, then this step will be skipped. 26 func (s *StepAttachFloppy) Run(_ context.Context, state multistep.StateBag) multistep.StepAction { 27 // Determine if we even have a floppy disk to attach 28 var floppyPath string 29 if floppyPathRaw, ok := state.GetOk("floppy_path"); ok { 30 floppyPath = floppyPathRaw.(string) 31 } else { 32 log.Println("No floppy disk, not attaching.") 33 return multistep.ActionContinue 34 } 35 36 driver := state.Get("driver").(Driver) 37 ui := state.Get("ui").(packer.Ui) 38 vmName := state.Get("vmName").(string) 39 40 ui.Say("Deleting any current floppy disk...") 41 // Delete the floppy disk controller 42 delCommand := []string{ 43 "set", vmName, 44 "--device-del", "fdd0", 45 } 46 // This will almost certainly fail with 'The fdd0 device does not exist.' 47 driver.Prlctl(delCommand...) 48 49 ui.Say("Attaching floppy disk...") 50 // Attaching the floppy disk 51 addCommand := []string{ 52 "set", vmName, 53 "--device-add", "fdd", 54 "--image", floppyPath, 55 "--connect", 56 } 57 if err := driver.Prlctl(addCommand...); err != nil { 58 state.Put("error", fmt.Errorf("Error adding floppy: %s", err)) 59 return multistep.ActionHalt 60 } 61 62 // Track the path so that we can unregister it from Parallels later 63 s.floppyPath = floppyPath 64 65 return multistep.ActionContinue 66 } 67 68 // Cleanup removes the virtual FDD device attached to the VM. 69 func (s *StepAttachFloppy) Cleanup(state multistep.StateBag) { 70 driver := state.Get("driver").(Driver) 71 vmName := state.Get("vmName").(string) 72 73 if s.floppyPath == "" { 74 return 75 } 76 77 log.Println("Detaching floppy disk...") 78 command := []string{ 79 "set", vmName, 80 "--device-del", "fdd0", 81 } 82 driver.Prlctl(command...) 83 }