2013-12-22 11:10:11 -05:00
|
|
|
package common
|
2013-07-09 15:29:40 -04:00
|
|
|
|
|
|
|
import (
|
2018-01-22 18:32:33 -05:00
|
|
|
"context"
|
2013-07-09 15:29:40 -04:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2018-01-22 20:21:10 -05:00
|
|
|
|
2018-01-22 18:32:33 -05:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
2018-01-22 20:21:10 -05:00
|
|
|
"github.com/hashicorp/packer/packer"
|
2018-12-12 09:45:00 -05:00
|
|
|
"github.com/hashicorp/packer/packer/tmp"
|
2013-07-09 15:29:40 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// This step attaches the ISO to the virtual machine.
|
|
|
|
//
|
|
|
|
// Uses:
|
2013-12-22 11:10:11 -05:00
|
|
|
// driver Driver
|
|
|
|
// ui packer.Ui
|
|
|
|
// vmName string
|
2013-07-09 15:29:40 -04:00
|
|
|
//
|
|
|
|
// Produces:
|
2013-12-22 11:10:11 -05:00
|
|
|
type StepAttachFloppy struct {
|
2013-07-09 15:29:40 -04:00
|
|
|
floppyPath string
|
|
|
|
}
|
|
|
|
|
2019-03-29 11:50:02 -04:00
|
|
|
func (s *StepAttachFloppy) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
2013-07-09 15:29:40 -04:00
|
|
|
// Determine if we even have a floppy disk to attach
|
|
|
|
var floppyPath string
|
2013-08-31 15:44:58 -04:00
|
|
|
if floppyPathRaw, ok := state.GetOk("floppy_path"); ok {
|
2013-07-09 15:29:40 -04:00
|
|
|
floppyPath = floppyPathRaw.(string)
|
|
|
|
} else {
|
|
|
|
log.Println("No floppy disk, not attaching.")
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
// VirtualBox 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 {
|
2013-08-31 15:44:58 -04:00
|
|
|
state.Put("error", fmt.Errorf("Error preparing floppy: %s", err))
|
2013-07-09 15:29:40 -04:00
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
2013-12-22 11:10:11 -05:00
|
|
|
driver := state.Get("driver").(Driver)
|
2013-08-31 15:44:58 -04:00
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
vmName := state.Get("vmName").(string)
|
2013-07-09 15:29:40 -04:00
|
|
|
|
2020-03-03 00:52:39 -05:00
|
|
|
ui.Say("Deleting any current floppy disk...")
|
|
|
|
if err := driver.RemoveFloppyControllers(vmName); err != nil {
|
|
|
|
state.Put("error", fmt.Errorf("Error deleting existing floppy controllers: %s", err))
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
2013-07-09 15:29:40 -04:00
|
|
|
ui.Say("Attaching floppy disk...")
|
|
|
|
|
|
|
|
// Create the floppy disk controller
|
|
|
|
command := []string{
|
|
|
|
"storagectl", vmName,
|
|
|
|
"--name", "Floppy Controller",
|
|
|
|
"--add", "floppy",
|
|
|
|
}
|
|
|
|
if err := driver.VBoxManage(command...); err != nil {
|
2013-08-31 15:44:58 -04:00
|
|
|
state.Put("error", fmt.Errorf("Error creating floppy controller: %s", err))
|
2013-07-09 15:29:40 -04:00
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
// Attach the floppy to the controller
|
|
|
|
command = []string{
|
|
|
|
"storageattach", vmName,
|
|
|
|
"--storagectl", "Floppy Controller",
|
|
|
|
"--port", "0",
|
|
|
|
"--device", "0",
|
|
|
|
"--type", "fdd",
|
|
|
|
"--medium", floppyPath,
|
|
|
|
}
|
|
|
|
if err := driver.VBoxManage(command...); err != nil {
|
2013-08-31 15:44:58 -04:00
|
|
|
state.Put("error", fmt.Errorf("Error attaching floppy: %s", err))
|
2013-07-09 15:29:40 -04:00
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
// Track the path so that we can unregister it from VirtualBox later
|
|
|
|
s.floppyPath = floppyPath
|
|
|
|
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2013-12-22 11:10:11 -05:00
|
|
|
func (s *StepAttachFloppy) Cleanup(state multistep.StateBag) {
|
2020-05-25 06:02:29 -04:00
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
ui.Say("Cleaning up floppy disk...")
|
2013-07-09 15:29:40 -04:00
|
|
|
if s.floppyPath == "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete the floppy disk
|
|
|
|
defer os.Remove(s.floppyPath)
|
|
|
|
|
2013-12-22 11:10:11 -05:00
|
|
|
driver := state.Get("driver").(Driver)
|
2013-08-31 15:44:58 -04:00
|
|
|
vmName := state.Get("vmName").(string)
|
2013-07-09 15:29:40 -04:00
|
|
|
|
|
|
|
command := []string{
|
|
|
|
"storageattach", vmName,
|
|
|
|
"--storagectl", "Floppy Controller",
|
|
|
|
"--port", "0",
|
|
|
|
"--device", "0",
|
|
|
|
"--medium", "none",
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := driver.VBoxManage(command...); err != nil {
|
2020-05-25 06:02:29 -04:00
|
|
|
ui.Error(fmt.Sprintf("Error unregistering floppy: %s. "+
|
|
|
|
"Not considering this a critical failure; build will continue.", err))
|
2013-07-09 15:29:40 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-22 11:10:11 -05:00
|
|
|
func (s *StepAttachFloppy) copyFloppy(path string) (string, error) {
|
2018-12-12 09:45:00 -05:00
|
|
|
tempdir, err := tmp.Dir("virtualbox")
|
2013-07-09 15:29:40 -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
|
|
|
|
}
|