github.com/rahart/packer@v0.12.2-0.20161229105310-282bb6ad370f/builder/hyperv/common/step_mount_floppydrive.go (about)

     1  package common
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/mitchellh/multistep"
     6  	"github.com/mitchellh/packer/packer"
     7  	"io"
     8  	"io/ioutil"
     9  	"log"
    10  	"os"
    11  	"path/filepath"
    12  )
    13  
    14  const (
    15  	FloppyFileName = "assets.vfd"
    16  )
    17  
    18  type StepMountFloppydrive struct {
    19  	Generation uint
    20  	floppyPath string
    21  }
    22  
    23  func (s *StepMountFloppydrive) Run(state multistep.StateBag) multistep.StepAction {
    24  	if s.Generation > 1 {
    25  		return multistep.ActionContinue
    26  	}
    27  
    28  	driver := state.Get("driver").(Driver)
    29  
    30  	// Determine if we even have a floppy disk to attach
    31  	var floppyPath string
    32  	if floppyPathRaw, ok := state.GetOk("floppy_path"); ok {
    33  		floppyPath = floppyPathRaw.(string)
    34  	} else {
    35  		log.Println("No floppy disk, not attaching.")
    36  		return multistep.ActionContinue
    37  	}
    38  
    39  	// Hyper-V is really dumb and can't figure out the format of the file
    40  	// without an extension, so we need to add the "vfd" extension to the
    41  	// floppy.
    42  	floppyPath, err := s.copyFloppy(floppyPath)
    43  	if err != nil {
    44  		state.Put("error", fmt.Errorf("Error preparing floppy: %s", err))
    45  		return multistep.ActionHalt
    46  	}
    47  
    48  	ui := state.Get("ui").(packer.Ui)
    49  	vmName := state.Get("vmName").(string)
    50  
    51  	ui.Say("Mounting floppy drive...")
    52  
    53  	err = driver.MountFloppyDrive(vmName, floppyPath)
    54  	if err != nil {
    55  		state.Put("error", fmt.Errorf("Error mounting floppy drive: %s", err))
    56  		return multistep.ActionHalt
    57  	}
    58  
    59  	// Track the path so that we can unregister it from Hyper-V later
    60  	s.floppyPath = floppyPath
    61  
    62  	return multistep.ActionContinue
    63  }
    64  
    65  func (s *StepMountFloppydrive) Cleanup(state multistep.StateBag) {
    66  	if s.Generation > 1 {
    67  		return
    68  	}
    69  	driver := state.Get("driver").(Driver)
    70  	if s.floppyPath == "" {
    71  		return
    72  	}
    73  
    74  	errorMsg := "Error unmounting floppy drive: %s"
    75  
    76  	vmName := state.Get("vmName").(string)
    77  	ui := state.Get("ui").(packer.Ui)
    78  
    79  	ui.Say("Cleanup floppy drive...")
    80  
    81  	err := driver.UnmountFloppyDrive(vmName)
    82  	if err != nil {
    83  		log.Print(fmt.Sprintf(errorMsg, err))
    84  	}
    85  
    86  	err = os.Remove(s.floppyPath)
    87  
    88  	if err != nil {
    89  		log.Print(fmt.Sprintf(errorMsg, err))
    90  	}
    91  }
    92  
    93  func (s *StepMountFloppydrive) copyFloppy(path string) (string, error) {
    94  	tempdir, err := ioutil.TempDir("", "packer")
    95  	if err != nil {
    96  		return "", err
    97  	}
    98  
    99  	floppyPath := filepath.Join(tempdir, "floppy.vfd")
   100  	f, err := os.Create(floppyPath)
   101  	if err != nil {
   102  		return "", err
   103  	}
   104  	defer f.Close()
   105  
   106  	sourceF, err := os.Open(path)
   107  	if err != nil {
   108  		return "", err
   109  	}
   110  	defer sourceF.Close()
   111  
   112  	log.Printf("Copying floppy to temp location: %s", floppyPath)
   113  	if _, err := io.Copy(f, sourceF); err != nil {
   114  		return "", err
   115  	}
   116  
   117  	return floppyPath, nil
   118  }