github.com/sneal/packer@v0.5.2/builder/virtualbox/common/step_attach_floppy.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  // This step attaches the ISO to the virtual machine.
    15  //
    16  // Uses:
    17  //   driver Driver
    18  //   ui packer.Ui
    19  //   vmName string
    20  //
    21  // Produces:
    22  type StepAttachFloppy struct {
    23  	floppyPath string
    24  }
    25  
    26  func (s *StepAttachFloppy) Run(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  	// VirtualBox is really dumb and can't figure out the format of the file
    37  	// without an extension, so we need to add the "vfd" extension to the
    38  	// floppy.
    39  	floppyPath, err := s.copyFloppy(floppyPath)
    40  	if err != nil {
    41  		state.Put("error", fmt.Errorf("Error preparing floppy: %s", err))
    42  		return multistep.ActionHalt
    43  	}
    44  
    45  	driver := state.Get("driver").(Driver)
    46  	ui := state.Get("ui").(packer.Ui)
    47  	vmName := state.Get("vmName").(string)
    48  
    49  	ui.Say("Attaching floppy disk...")
    50  
    51  	// Create the floppy disk controller
    52  	command := []string{
    53  		"storagectl", vmName,
    54  		"--name", "Floppy Controller",
    55  		"--add", "floppy",
    56  	}
    57  	if err := driver.VBoxManage(command...); err != nil {
    58  		state.Put("error", fmt.Errorf("Error creating floppy controller: %s", err))
    59  		return multistep.ActionHalt
    60  	}
    61  
    62  	// Attach the floppy to the controller
    63  	command = []string{
    64  		"storageattach", vmName,
    65  		"--storagectl", "Floppy Controller",
    66  		"--port", "0",
    67  		"--device", "0",
    68  		"--type", "fdd",
    69  		"--medium", floppyPath,
    70  	}
    71  	if err := driver.VBoxManage(command...); err != nil {
    72  		state.Put("error", fmt.Errorf("Error attaching floppy: %s", err))
    73  		return multistep.ActionHalt
    74  	}
    75  
    76  	// Track the path so that we can unregister it from VirtualBox later
    77  	s.floppyPath = floppyPath
    78  
    79  	return multistep.ActionContinue
    80  }
    81  
    82  func (s *StepAttachFloppy) Cleanup(state multistep.StateBag) {
    83  	if s.floppyPath == "" {
    84  		return
    85  	}
    86  
    87  	// Delete the floppy disk
    88  	defer os.Remove(s.floppyPath)
    89  
    90  	driver := state.Get("driver").(Driver)
    91  	vmName := state.Get("vmName").(string)
    92  
    93  	command := []string{
    94  		"storageattach", vmName,
    95  		"--storagectl", "Floppy Controller",
    96  		"--port", "0",
    97  		"--device", "0",
    98  		"--medium", "none",
    99  	}
   100  
   101  	if err := driver.VBoxManage(command...); err != nil {
   102  		log.Printf("Error unregistering floppy: %s", err)
   103  	}
   104  }
   105  
   106  func (s *StepAttachFloppy) copyFloppy(path string) (string, error) {
   107  	tempdir, err := ioutil.TempDir("", "packer")
   108  	if err != nil {
   109  		return "", err
   110  	}
   111  
   112  	floppyPath := filepath.Join(tempdir, "floppy.vfd")
   113  	f, err := os.Create(floppyPath)
   114  	if err != nil {
   115  		return "", err
   116  	}
   117  	defer f.Close()
   118  
   119  	sourceF, err := os.Open(path)
   120  	if err != nil {
   121  		return "", err
   122  	}
   123  	defer sourceF.Close()
   124  
   125  	log.Printf("Copying floppy to temp location: %s", floppyPath)
   126  	if _, err := io.Copy(f, sourceF); err != nil {
   127  		return "", err
   128  	}
   129  
   130  	return floppyPath, nil
   131  }