2015-06-21 07:36:07 -04:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2018-01-19 19:18:44 -05:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
|
|
|
"github.com/hashicorp/packer/packer"
|
2015-06-21 07:36:07 -04:00
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
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
|
|
|
|
|
|
|
"github.com/hashicorp/packer/packer"
|
|
|
|
"github.com/mitchellh/multistep"
|
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
|
|
|
|
}
|
|
|
|
|
2018-01-22 18:31:41 -05:00
|
|
|
func (s *StepMountFloppydrive) Run(_ 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
|
|
|
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
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)
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
|
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) {
|
|
|
|
tempdir, err := ioutil.TempDir("", "packer")
|
|
|
|
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
|
|
|
|
}
|