This repository has been archived by the owner on Oct 12, 2022. It is now read-only.
/
step_mount_floppydrive.go
123 lines (95 loc) · 2.83 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
119
120
121
122
123
package iso
import (
"fmt"
"bytes"
"os"
"github.com/mitchellh/multistep"
hypervcommon "github.com/MSOpenTech/packer-hyperv/packer/builder/hyperv/common"
"github.com/mitchellh/packer/packer"
b64 "encoding/base64"
"path/filepath"
)
const(
FloppyFileName = "assets.vfd"
)
type StepMountFloppydrive struct {
FileName string
Dir string
}
func (s *StepMountFloppydrive) Run(state multistep.StateBag) multistep.StepAction {
config := state.Get("config").(*iso_config)
driver := state.Get("driver").(hypervcommon.Driver)
ui := state.Get("ui").(packer.Ui)
errorMsg := "Error mounting floppy drive: %s"
vmName := state.Get("vmName").(string)
packerTempDir := state.Get("packerTempDir").(string)
var err error
var decBytes []byte
if config.GuestOSType == WS2012R2DC {
decBytes, err = b64.StdEncoding.DecodeString(FileAsStringBase64Win2012R2)
// } else if config.GuestOSType == WS2012DC {
// decBytes, err = b64.StdEncoding.DecodeString(FileAsStringBase64Win2012)
}
var fmtError error
if err != nil {
fmtError = fmt.Errorf(errorMsg, err)
state.Put("error", fmtError)
ui.Error(fmtError.Error())
return multistep.ActionHalt
}
f, err := os.Create(filepath.Join(packerTempDir,FloppyFileName))
if err != nil {
fmtError = fmt.Errorf(errorMsg, err)
state.Put("error", fmtError)
ui.Error(fmtError.Error())
return multistep.ActionHalt
}
_, err = f.Write(decBytes)
if err != nil {
fmtError = fmt.Errorf(errorMsg, err)
state.Put("error", fmtError)
ui.Error(fmtError.Error())
return multistep.ActionHalt
}
s.FileName = f.Name()
s.Dir = packerTempDir
defer f.Close()
ui.Say("Mounting floppy drive...")
var blockBuffer bytes.Buffer
blockBuffer.WriteString("Invoke-Command -scriptblock {Set-VMFloppyDiskDrive -VMName '")
blockBuffer.WriteString(vmName)
blockBuffer.WriteString("' -Path '")
blockBuffer.WriteString(s.FileName)
blockBuffer.WriteString("'}")
err = driver.HypervManage( blockBuffer.String() )
if err != nil {
fmtError = fmt.Errorf(errorMsg, err)
state.Put("error", fmtError)
ui.Error(fmtError.Error())
return multistep.ActionHalt
}
return multistep.ActionContinue
}
func (s *StepMountFloppydrive) Cleanup(state multistep.StateBag) {
if s.FileName == "" {
return
}
errorMsg := "Error unmounting floppy drive: %s"
vmName := state.Get("vmName").(string)
driver := state.Get("driver").(hypervcommon.Driver)
ui := state.Get("ui").(packer.Ui)
ui.Say("Unmounting floppy drive...")
var err error = nil
var blockBuffer bytes.Buffer
blockBuffer.WriteString("Invoke-Command -scriptblock {Set-VMFloppyDiskDrive -VMName '")
blockBuffer.WriteString(vmName)
blockBuffer.WriteString("' -Path $null}")
err = driver.HypervManage( blockBuffer.String() )
if err != nil {
ui.Error(fmt.Sprintf(errorMsg, err))
}
err = os.Remove(s.FileName)
if err != nil {
ui.Error(fmt.Sprintf(errorMsg, err))
}
}