2014-04-06 13:21:22 -04:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
2018-01-22 18:32:33 -05:00
|
|
|
"context"
|
2014-04-06 13:21:22 -04:00
|
|
|
"fmt"
|
2016-12-11 14:13:37 -05:00
|
|
|
"log"
|
|
|
|
|
2020-11-17 19:31:03 -05:00
|
|
|
"github.com/hashicorp/packer/packer-plugin-sdk/multistep"
|
2020-11-19 14:54:31 -05:00
|
|
|
packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer"
|
2014-04-06 13:21:22 -04:00
|
|
|
)
|
|
|
|
|
2016-12-11 17:37:41 -05:00
|
|
|
// StepAttachFloppy is a step that attaches a floppy to the virtual machine.
|
2014-04-06 13:21:22 -04:00
|
|
|
//
|
|
|
|
// Uses:
|
|
|
|
// driver Driver
|
2020-11-19 14:54:31 -05:00
|
|
|
// ui packersdk.Ui
|
2014-04-06 13:21:22 -04:00
|
|
|
// vmName string
|
|
|
|
//
|
|
|
|
// Produces:
|
|
|
|
type StepAttachFloppy struct {
|
|
|
|
floppyPath string
|
|
|
|
}
|
|
|
|
|
2016-12-16 14:51:55 -05:00
|
|
|
// Run adds a virtual FDD device to the VM and attaches the image.
|
|
|
|
// If the image is not specified, then this step will be skipped.
|
2019-03-29 11:50:02 -04:00
|
|
|
func (s *StepAttachFloppy) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
2014-04-06 13:21:22 -04:00
|
|
|
// Determine if we even have a floppy disk to attach
|
|
|
|
var floppyPath string
|
|
|
|
if floppyPathRaw, ok := state.GetOk("floppy_path"); ok {
|
|
|
|
floppyPath = floppyPathRaw.(string)
|
|
|
|
} else {
|
|
|
|
log.Println("No floppy disk, not attaching.")
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
driver := state.Get("driver").(Driver)
|
2020-11-19 14:54:31 -05:00
|
|
|
ui := state.Get("ui").(packersdk.Ui)
|
2014-04-06 13:21:22 -04:00
|
|
|
vmName := state.Get("vmName").(string)
|
|
|
|
|
2014-06-06 09:54:19 -04:00
|
|
|
ui.Say("Deleting any current floppy disk...")
|
|
|
|
// Delete the floppy disk controller
|
2016-12-11 13:49:54 -05:00
|
|
|
delCommand := []string{
|
2014-06-06 09:54:19 -04:00
|
|
|
"set", vmName,
|
|
|
|
"--device-del", "fdd0",
|
|
|
|
}
|
2014-09-15 17:57:39 -04:00
|
|
|
// This will almost certainly fail with 'The fdd0 device does not exist.'
|
2016-12-11 13:49:54 -05:00
|
|
|
driver.Prlctl(delCommand...)
|
2014-04-06 13:21:22 -04:00
|
|
|
|
2014-09-15 17:57:39 -04:00
|
|
|
ui.Say("Attaching floppy disk...")
|
2014-08-08 02:12:48 -04:00
|
|
|
// Attaching the floppy disk
|
2016-12-11 13:49:54 -05:00
|
|
|
addCommand := []string{
|
2014-04-06 13:21:22 -04:00
|
|
|
"set", vmName,
|
|
|
|
"--device-add", "fdd",
|
|
|
|
"--image", floppyPath,
|
2014-06-06 09:54:19 -04:00
|
|
|
"--connect",
|
2014-04-06 13:21:22 -04:00
|
|
|
}
|
2016-12-11 13:49:54 -05:00
|
|
|
if err := driver.Prlctl(addCommand...); err != nil {
|
2014-04-06 13:21:22 -04:00
|
|
|
state.Put("error", fmt.Errorf("Error adding floppy: %s", err))
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
// Track the path so that we can unregister it from Parallels later
|
|
|
|
s.floppyPath = floppyPath
|
|
|
|
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2016-12-16 14:51:55 -05:00
|
|
|
// Cleanup removes the virtual FDD device attached to the VM.
|
2014-08-08 02:12:48 -04:00
|
|
|
func (s *StepAttachFloppy) Cleanup(state multistep.StateBag) {
|
|
|
|
driver := state.Get("driver").(Driver)
|
|
|
|
vmName := state.Get("vmName").(string)
|
|
|
|
|
|
|
|
if s.floppyPath == "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Println("Detaching floppy disk...")
|
|
|
|
command := []string{
|
|
|
|
"set", vmName,
|
|
|
|
"--device-del", "fdd0",
|
|
|
|
}
|
|
|
|
driver.Prlctl(command...)
|
|
|
|
}
|