forked from hashicorp/packer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
step_mount_guest_additions.go
92 lines (72 loc) · 2.71 KB
/
step_mount_guest_additions.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
package common
import (
"fmt"
"github.com/mitchellh/multistep"
"github.com/mitchellh/packer/packer"
"log"
)
type StepMountGuestAdditions struct {
GuestAdditionsMode string
GuestAdditionsPath string
Generation uint
}
func (s *StepMountGuestAdditions) Run(state multistep.StateBag) multistep.StepAction {
ui := state.Get("ui").(packer.Ui)
if s.GuestAdditionsMode != "attach" {
ui.Say("Skipping mounting Integration Services Setup Disk...")
return multistep.ActionContinue
}
driver := state.Get("driver").(Driver)
ui.Say("Mounting Integration Services Setup Disk...")
vmName := state.Get("vmName").(string)
// should be able to mount up to 60 additional iso images using SCSI
// but Windows would only allow a max of 22 due to available drive letters
// Will Windows assign DVD drives to A: and B: ?
// For IDE, there are only 2 controllers (0,1) with 2 locations each (0,1)
var dvdControllerProperties DvdControllerProperties
controllerNumber, controllerLocation, err := driver.CreateDvdDrive(vmName, s.GuestAdditionsPath, s.Generation)
if err != nil {
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
dvdControllerProperties.ControllerNumber = controllerNumber
dvdControllerProperties.ControllerLocation = controllerLocation
dvdControllerProperties.Existing = false
state.Put("guest.dvd.properties", dvdControllerProperties)
ui.Say(fmt.Sprintf("Mounting Integration Services dvd drive %s ...", s.GuestAdditionsPath))
err = driver.MountDvdDrive(vmName, s.GuestAdditionsPath, controllerNumber, controllerLocation)
if err != nil {
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
log.Println(fmt.Sprintf("ISO %s mounted on DVD controller %v, location %v", s.GuestAdditionsPath, controllerNumber, controllerLocation))
return multistep.ActionContinue
}
func (s *StepMountGuestAdditions) Cleanup(state multistep.StateBag) {
if s.GuestAdditionsMode != "attach" {
return
}
dvdControllerState := state.Get("guest.dvd.properties")
if dvdControllerState == nil {
return
}
dvdController := dvdControllerState.(DvdControllerProperties)
ui := state.Get("ui").(packer.Ui)
driver := state.Get("driver").(Driver)
vmName := state.Get("vmName").(string)
errorMsg := "Error unmounting Integration Services dvd drive: %s"
ui.Say("Cleanup Integration Services dvd drive...")
if dvdController.Existing {
err := driver.UnmountDvdDrive(vmName, dvdController.ControllerNumber, dvdController.ControllerLocation)
if err != nil {
log.Print(fmt.Sprintf(errorMsg, err))
}
} else {
err := driver.DeleteDvdDrive(vmName, dvdController.ControllerNumber, dvdController.ControllerLocation)
if err != nil {
log.Print(fmt.Sprintf(errorMsg, err))
}
}
}