forked from hashicorp/packer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
step_mount_floppydrive.go
118 lines (94 loc) · 2.49 KB
/
step_mount_floppydrive.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package common
import (
"fmt"
"github.com/mitchellh/multistep"
"github.com/mitchellh/packer/packer"
"io"
"io/ioutil"
"log"
"os"
"path/filepath"
)
const (
FloppyFileName = "assets.vfd"
)
type StepMountFloppydrive struct {
Generation uint
floppyPath string
}
func (s *StepMountFloppydrive) Run(state multistep.StateBag) multistep.StepAction {
if s.Generation > 1 {
return multistep.ActionContinue
}
driver := state.Get("driver").(Driver)
// 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
}
ui := state.Get("ui").(packer.Ui)
vmName := state.Get("vmName").(string)
ui.Say("Mounting floppy drive...")
err = driver.MountFloppyDrive(vmName, floppyPath)
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
return multistep.ActionContinue
}
func (s *StepMountFloppydrive) Cleanup(state multistep.StateBag) {
if s.Generation > 1 {
return
}
driver := state.Get("driver").(Driver)
if s.floppyPath == "" {
return
}
errorMsg := "Error unmounting floppy drive: %s"
vmName := state.Get("vmName").(string)
ui := state.Get("ui").(packer.Ui)
ui.Say("Cleanup floppy drive...")
err := driver.UnmountFloppyDrive(vmName)
if err != nil {
log.Print(fmt.Sprintf(errorMsg, err))
}
err = os.Remove(s.floppyPath)
if err != nil {
log.Print(fmt.Sprintf(errorMsg, err))
}
}
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
}