2015-06-21 07:36:07 -04:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
2018-01-22 18:32:33 -05:00
|
|
|
"context"
|
2015-06-21 07:36:07 -04:00
|
|
|
"fmt"
|
|
|
|
"io"
|
2015-10-30 04:23:30 -04:00
|
|
|
"log"
|
|
|
|
"os"
|
2015-06-21 07:36:07 -04:00
|
|
|
"path/filepath"
|
2018-01-22 20:21:10 -05:00
|
|
|
|
2020-12-17 16:29:25 -05:00
|
|
|
"github.com/hashicorp/packer-plugin-sdk/multistep"
|
|
|
|
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
|
|
|
|
"github.com/hashicorp/packer-plugin-sdk/tmp"
|
2015-06-21 07:36:07 -04:00
|
|
|
)
|
|
|
|
|
2015-10-30 04:23:30 -04:00
|
|
|
const (
|
2015-06-21 07:36:07 -04:00
|
|
|
FloppyFileName = "assets.vfd"
|
|
|
|
)
|
|
|
|
|
|
|
|
type StepMountFloppydrive struct {
|
2015-10-30 13:19:25 -04:00
|
|
|
Generation uint
|
2015-06-21 07:36:07 -04:00
|
|
|
floppyPath string
|
|
|
|
}
|
|
|
|
|
2019-03-29 11:50:02 -04:00
|
|
|
func (s *StepMountFloppydrive) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
2015-10-30 13:19:25 -04:00
|
|
|
if s.Generation > 1 {
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
2016-11-06 11:02:00 -05:00
|
|
|
|
2015-10-30 13:19:25 -04:00
|
|
|
driver := state.Get("driver").(Driver)
|
2016-11-06 11:02:00 -05:00
|
|
|
|
2015-06-21 07:36:07 -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
|
|
|
|
}
|
|
|
|
|
|
|
|
// Hyper-V is really dumb and can't figure out the format of the file
|
|
|
|
// without an extension, so we need to add the "vfd" extension to the
|
|
|
|
// floppy.
|
|
|
|
floppyPath, err := s.copyFloppy(floppyPath)
|
|
|
|
if err != nil {
|
|
|
|
state.Put("error", fmt.Errorf("Error preparing floppy: %s", err))
|
|
|
|
return multistep.ActionHalt
|
2015-10-30 04:23:30 -04:00
|
|
|
}
|
2015-06-21 07:36:07 -04:00
|
|
|
|
2020-11-19 14:54:31 -05:00
|
|
|
ui := state.Get("ui").(packersdk.Ui)
|
2015-06-21 07:36:07 -04:00
|
|
|
vmName := state.Get("vmName").(string)
|
|
|
|
|
|
|
|
ui.Say("Mounting floppy drive...")
|
|
|
|
|
2015-10-30 13:19:25 -04:00
|
|
|
err = driver.MountFloppyDrive(vmName, floppyPath)
|
2015-06-21 07:36:07 -04:00
|
|
|
if err != nil {
|
|
|
|
state.Put("error", fmt.Errorf("Error mounting floppy drive: %s", err))
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
// Track the path so that we can unregister it from Hyper-V later
|
|
|
|
s.floppyPath = floppyPath
|
|
|
|
|
2015-10-30 04:23:30 -04:00
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
2015-06-21 07:36:07 -04:00
|
|
|
|
|
|
|
func (s *StepMountFloppydrive) Cleanup(state multistep.StateBag) {
|
2015-10-30 13:19:25 -04:00
|
|
|
if s.Generation > 1 {
|
|
|
|
return
|
2016-11-06 11:02:00 -05:00
|
|
|
}
|
2015-10-30 13:19:25 -04:00
|
|
|
driver := state.Get("driver").(Driver)
|
2015-06-21 07:36:07 -04:00
|
|
|
if s.floppyPath == "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
errorMsg := "Error unmounting floppy drive: %s"
|
|
|
|
|
|
|
|
vmName := state.Get("vmName").(string)
|
2020-11-19 14:54:31 -05:00
|
|
|
ui := state.Get("ui").(packersdk.Ui)
|
2015-06-21 07:36:07 -04:00
|
|
|
|
2015-10-30 13:19:25 -04:00
|
|
|
ui.Say("Cleanup floppy drive...")
|
2015-06-21 07:36:07 -04:00
|
|
|
|
2015-10-30 13:19:25 -04:00
|
|
|
err := driver.UnmountFloppyDrive(vmName)
|
2015-06-21 07:36:07 -04:00
|
|
|
if err != nil {
|
2015-10-30 13:19:25 -04:00
|
|
|
log.Print(fmt.Sprintf(errorMsg, err))
|
2015-06-21 07:36:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
err = os.Remove(s.floppyPath)
|
|
|
|
|
|
|
|
if err != nil {
|
2015-10-30 13:19:25 -04:00
|
|
|
log.Print(fmt.Sprintf(errorMsg, err))
|
2015-06-21 07:36:07 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StepMountFloppydrive) copyFloppy(path string) (string, error) {
|
2018-12-12 09:45:00 -05:00
|
|
|
tempdir, err := tmp.Dir("hyperv")
|
2015-06-21 07:36:07 -04:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
floppyPath := filepath.Join(tempdir, "floppy.vfd")
|
|
|
|
f, err := os.Create(floppyPath)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
sourceF, err := os.Open(path)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
defer sourceF.Close()
|
|
|
|
|
|
|
|
log.Printf("Copying floppy to temp location: %s", floppyPath)
|
|
|
|
if _, err := io.Copy(f, sourceF); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return floppyPath, nil
|
|
|
|
}
|