github.com/alouche/packer@v0.3.7/builder/virtualbox/step_attach_floppy.go (about)

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