Get rid of mount and unmount that does not specify controller location exactly

Handle cleanup if unmount step has not already done so
This commit is contained in:
Taliesin Sisson 2015-11-01 16:00:56 +00:00
parent 31ac1da28d
commit 3cb621f890
10 changed files with 107 additions and 98 deletions

View File

@ -88,13 +88,11 @@ type Driver interface {
CreateDvdDrive(string, uint) (uint, uint, error) CreateDvdDrive(string, uint) (uint, uint, error)
MountDvdDrive(string, string) error MountDvdDrive(string, string, uint, uint) error
MountDvdDriveByLocation(string, string, uint, uint) error
SetBootDvdDrive(string, uint, uint) error SetBootDvdDrive(string, uint, uint) error
UnmountDvdDrive(string) error UnmountDvdDrive(string, uint, uint) error
DeleteDvdDrive(string, uint, uint) error DeleteDvdDrive(string, uint, uint) error

View File

@ -209,20 +209,16 @@ func (d *HypervPS4Driver) CreateDvdDrive(vmName string, generation uint) (uint,
return hyperv.CreateDvdDrive(vmName, generation) return hyperv.CreateDvdDrive(vmName, generation)
} }
func (d *HypervPS4Driver) MountDvdDrive(vmName string, path string) error { func (d *HypervPS4Driver) MountDvdDrive(vmName string, path string, controllerNumber uint, controllerLocation uint) error {
return hyperv.MountDvdDrive(vmName, path) return hyperv.MountDvdDrive(vmName, path, controllerNumber, controllerLocation)
}
func (d *HypervPS4Driver) MountDvdDriveByLocation(vmName string, path string, controllerNumber uint, controllerLocation uint) error {
return hyperv.MountDvdDriveByLocation(vmName, path, controllerNumber, controllerLocation)
} }
func (d *HypervPS4Driver) SetBootDvdDrive(vmName string, controllerNumber uint, controllerLocation uint) error { func (d *HypervPS4Driver) SetBootDvdDrive(vmName string, controllerNumber uint, controllerLocation uint) error {
return hyperv.SetBootDvdDrive(vmName, controllerNumber, controllerLocation) return hyperv.SetBootDvdDrive(vmName, controllerNumber, controllerLocation)
} }
func (d *HypervPS4Driver) UnmountDvdDrive(vmName string) error { func (d *HypervPS4Driver) UnmountDvdDrive(vmName string, controllerNumber uint, controllerLocation uint) error {
return hyperv.UnmountDvdDrive(vmName) return hyperv.UnmountDvdDrive(vmName, controllerNumber, controllerLocation)
} }
func (d *HypervPS4Driver) DeleteDvdDrive(vmName string, controllerNumber uint, controllerLocation uint) error { func (d *HypervPS4Driver) DeleteDvdDrive(vmName string, controllerNumber uint, controllerLocation uint) error {

View File

@ -13,8 +13,6 @@ import (
type StepMountDvdDrive struct { type StepMountDvdDrive struct {
Generation uint Generation uint
cleanup bool
dvdProperties DvdControllerProperties
} }
func (s *StepMountDvdDrive) Run(state multistep.StateBag) multistep.StepAction { func (s *StepMountDvdDrive) Run(state multistep.StateBag) multistep.StepAction {
@ -41,10 +39,11 @@ func (s *StepMountDvdDrive) Run(state multistep.StateBag) multistep.StepAction {
dvdControllerProperties.ControllerNumber = controllerNumber dvdControllerProperties.ControllerNumber = controllerNumber
dvdControllerProperties.ControllerLocation = controllerLocation dvdControllerProperties.ControllerLocation = controllerLocation
s.cleanup = true dvdControllerProperties.Existing = false
s.dvdProperties = dvdControllerProperties
state.Put("os.dvd.properties", dvdControllerProperties)
ui.Say(fmt.Sprintf("Setting boot drive to os dvd drive %s ..."), isoPath) ui.Say(fmt.Sprintf("Setting boot drive to os dvd drive %s ...", isoPath))
err = driver.SetBootDvdDrive(vmName, controllerNumber, controllerLocation) err = driver.SetBootDvdDrive(vmName, controllerNumber, controllerLocation)
if err != nil { if err != nil {
err := fmt.Errorf(errorMsg, err) err := fmt.Errorf(errorMsg, err)
@ -54,7 +53,7 @@ func (s *StepMountDvdDrive) Run(state multistep.StateBag) multistep.StepAction {
} }
ui.Say(fmt.Sprintf("Mounting os dvd drive %s ...", isoPath)) ui.Say(fmt.Sprintf("Mounting os dvd drive %s ...", isoPath))
err = driver.MountDvdDrive(vmName, isoPath) err = driver.MountDvdDrive(vmName, isoPath, controllerNumber, controllerLocation)
if err != nil { if err != nil {
err := fmt.Errorf(errorMsg, err) err := fmt.Errorf(errorMsg, err)
state.Put("error", err) state.Put("error", err)
@ -62,35 +61,32 @@ func (s *StepMountDvdDrive) Run(state multistep.StateBag) multistep.StepAction {
return multistep.ActionHalt return multistep.ActionHalt
} }
state.Put("os.dvd.properties", dvdControllerProperties)
return multistep.ActionContinue return multistep.ActionContinue
} }
func (s *StepMountDvdDrive) Cleanup(state multistep.StateBag) { func (s *StepMountDvdDrive) Cleanup(state multistep.StateBag) {
if !s.cleanup { dvdControllerState := state.Get("os.dvd.properties")
if dvdControllerState == nil {
return return
} }
dvdController := dvdControllerState.(DvdControllerProperties)
driver := state.Get("driver").(Driver) driver := state.Get("driver").(Driver)
errorMsg := "Error unmounting os dvd drive: %s"
vmName := state.Get("vmName").(string) vmName := state.Get("vmName").(string)
ui := state.Get("ui").(packer.Ui) ui := state.Get("ui").(packer.Ui)
errorMsg := "Error unmounting os dvd drive: %s"
ui.Say("Clean up os dvd drive...") ui.Say("Clean up os dvd drive...")
dvdControllerProperties := s.dvdProperties if dvdController.Existing {
err := driver.UnmountDvdDrive(vmName, dvdController.ControllerNumber, dvdController.ControllerLocation)
if dvdControllerProperties.Existing {
err := driver.UnmountDvdDrive(vmName)
if err != nil { if err != nil {
err := fmt.Errorf("Error unmounting dvd drive: %s", err) err := fmt.Errorf("Error unmounting dvd drive: %s", err)
log.Print(fmt.Sprintf(errorMsg, err)) log.Print(fmt.Sprintf(errorMsg, err))
} }
} else { } else {
err := driver.DeleteDvdDrive(vmName, dvdControllerProperties.ControllerNumber, dvdControllerProperties.ControllerLocation) err := driver.DeleteDvdDrive(vmName, dvdController.ControllerNumber, dvdController.ControllerLocation)
if err != nil { if err != nil {
err := fmt.Errorf("Error deleting dvd drive: %s", err) err := fmt.Errorf("Error deleting dvd drive: %s", err)
log.Print(fmt.Sprintf(errorMsg, err)) log.Print(fmt.Sprintf(errorMsg, err))

View File

@ -15,8 +15,6 @@ type StepMountGuestAdditions struct {
GuestAdditionsMode string GuestAdditionsMode string
GuestAdditionsPath string GuestAdditionsPath string
Generation uint Generation uint
cleanup bool
dvdProperties DvdControllerProperties
} }
func (s *StepMountGuestAdditions) Run(state multistep.StateBag) multistep.StepAction { func (s *StepMountGuestAdditions) Run(state multistep.StateBag) multistep.StepAction {
@ -49,11 +47,11 @@ func (s *StepMountGuestAdditions) Run(state multistep.StateBag) multistep.StepAc
dvdControllerProperties.ControllerNumber = controllerNumber dvdControllerProperties.ControllerNumber = controllerNumber
dvdControllerProperties.ControllerLocation = controllerLocation dvdControllerProperties.ControllerLocation = controllerLocation
s.cleanup = true dvdControllerProperties.Existing = false
s.dvdProperties = dvdControllerProperties state.Put("guest.dvd.properties", dvdControllerProperties)
ui.Say(fmt.Sprintf("Mounting Integration Services dvd drive %s ...", s.GuestAdditionsPath)) ui.Say(fmt.Sprintf("Mounting Integration Services dvd drive %s ...", s.GuestAdditionsPath))
err = driver.MountDvdDriveByLocation(vmName, s.GuestAdditionsPath, controllerNumber, controllerLocation) err = driver.MountDvdDrive(vmName, s.GuestAdditionsPath, controllerNumber, controllerLocation)
if err != nil { if err != nil {
state.Put("error", err) state.Put("error", err)
ui.Error(err.Error()) ui.Error(err.Error())
@ -62,35 +60,37 @@ func (s *StepMountGuestAdditions) Run(state multistep.StateBag) multistep.StepAc
log.Println(fmt.Sprintf("ISO %s mounted on DVD controller %v, location %v", s.GuestAdditionsPath, controllerNumber, controllerLocation)) log.Println(fmt.Sprintf("ISO %s mounted on DVD controller %v, location %v", s.GuestAdditionsPath, controllerNumber, controllerLocation))
state.Put("guest.dvd.properties", dvdControllerProperties)
return multistep.ActionContinue return multistep.ActionContinue
} }
func (s *StepMountGuestAdditions) Cleanup(state multistep.StateBag) { func (s *StepMountGuestAdditions) Cleanup(state multistep.StateBag) {
if !s.cleanup || s.GuestAdditionsMode != "attach" { if s.GuestAdditionsMode != "attach" {
return return
} }
dvdControllerState := state.Get("guest.dvd.properties")
if dvdControllerState == nil {
return
}
dvdController := dvdControllerState.(DvdControllerProperties)
ui := state.Get("ui").(packer.Ui) ui := state.Get("ui").(packer.Ui)
driver := state.Get("driver").(Driver)
driver := state.Get("driver").(Driver)
ui.Say("Cleanup Integration Services dvd drive...")
vmName := state.Get("vmName").(string) vmName := state.Get("vmName").(string)
dvdControllerProperties := s.dvdProperties
errorMsg := "Error unmounting Integration Services dvd drive: %s" errorMsg := "Error unmounting Integration Services dvd drive: %s"
if dvdControllerProperties.Existing { ui.Say("Cleanup Integration Services dvd drive...")
err := driver.UnmountDvdDrive(vmName)
if dvdController.Existing {
err := driver.UnmountDvdDrive(vmName, dvdController.ControllerNumber, dvdController.ControllerLocation)
if err != nil { if err != nil {
log.Print(fmt.Sprintf(errorMsg, err)) log.Print(fmt.Sprintf(errorMsg, err))
} }
} else { } else {
err := driver.DeleteDvdDrive(vmName, dvdControllerProperties.ControllerNumber, dvdControllerProperties.ControllerLocation) err := driver.DeleteDvdDrive(vmName, dvdController.ControllerNumber, dvdController.ControllerLocation)
if err != nil { if err != nil {
log.Print(fmt.Sprintf(errorMsg, err)) log.Print(fmt.Sprintf(errorMsg, err))
} }
} }
} }

View File

@ -14,8 +14,6 @@ import (
type StepMountSecondaryDvdImages struct { type StepMountSecondaryDvdImages struct {
IsoPaths []string IsoPaths []string
Generation uint Generation uint
cleanup bool
dvdProperties []DvdControllerProperties
} }
type DvdControllerProperties struct { type DvdControllerProperties struct {
@ -50,13 +48,12 @@ func (s *StepMountSecondaryDvdImages) Run(state multistep.StateBag) multistep.St
properties.ControllerNumber = controllerNumber properties.ControllerNumber = controllerNumber
properties.ControllerLocation = controllerLocation properties.ControllerLocation = controllerLocation
properties.Existing = false
s.cleanup = true
dvdProperties = append(dvdProperties, properties) dvdProperties = append(dvdProperties, properties)
s.dvdProperties = dvdProperties state.Put("secondary.dvd.properties", dvdProperties)
ui.Say(fmt.Sprintf("Mounting secondary dvd drive %s ...", isoPath)) ui.Say(fmt.Sprintf("Mounting secondary dvd drive %s ...", isoPath))
err = driver.MountDvdDriveByLocation(vmName, isoPath, controllerNumber, controllerLocation) err = driver.MountDvdDrive(vmName, isoPath, controllerNumber, controllerLocation)
if err != nil { if err != nil {
state.Put("error", err) state.Put("error", err)
ui.Error(err.Error()) ui.Error(err.Error())
@ -66,33 +63,33 @@ func (s *StepMountSecondaryDvdImages) Run(state multistep.StateBag) multistep.St
log.Println(fmt.Sprintf("ISO %s mounted on DVD controller %v, location %v", isoPath, controllerNumber, controllerLocation)) log.Println(fmt.Sprintf("ISO %s mounted on DVD controller %v, location %v", isoPath, controllerNumber, controllerLocation))
} }
state.Put("secondary.dvd.properties", dvdProperties)
return multistep.ActionContinue return multistep.ActionContinue
} }
func (s *StepMountSecondaryDvdImages) Cleanup(state multistep.StateBag) { func (s *StepMountSecondaryDvdImages) Cleanup(state multistep.StateBag) {
if (!s.cleanup){ dvdControllersState := state.Get("secondary.dvd.properties")
if dvdControllersState == nil {
return return
} }
dvdControllers := dvdControllersState.([]DvdControllerProperties)
driver := state.Get("driver").(Driver) driver := state.Get("driver").(Driver)
ui := state.Get("ui").(packer.Ui) ui := state.Get("ui").(packer.Ui)
vmName := state.Get("vmName").(string)
errorMsg := "Error unmounting secondary dvd drive: %s"
ui.Say("Clean up secondary dvd drives...") ui.Say("Clean up secondary dvd drives...")
vmName := state.Get("vmName").(string) for _, dvdController := range dvdControllers {
errorMsg := "Error unmounting secondary dvd drive: %s"
for _, dvdControllerProperties := range s.dvdProperties {
if dvdControllerProperties.Existing { if dvdController.Existing {
err := driver.UnmountDvdDrive(vmName) err := driver.UnmountDvdDrive(vmName, dvdController.ControllerNumber, dvdController.ControllerLocation)
if err != nil { if err != nil {
log.Print(fmt.Sprintf(errorMsg, err)) log.Print(fmt.Sprintf(errorMsg, err))
} }
} else { } else {
err := driver.DeleteDvdDrive(vmName, dvdControllerProperties.ControllerNumber, dvdControllerProperties.ControllerLocation) err := driver.DeleteDvdDrive(vmName, dvdController.ControllerNumber, dvdController.ControllerLocation)
if err != nil { if err != nil {
log.Print(fmt.Sprintf(errorMsg, err)) log.Print(fmt.Sprintf(errorMsg, err))
} }

View File

@ -18,12 +18,19 @@ func (s *StepUnmountDvdDrive) Run(state multistep.StateBag) multistep.StepAction
vmName := state.Get("vmName").(string) vmName := state.Get("vmName").(string)
ui := state.Get("ui").(packer.Ui) ui := state.Get("ui").(packer.Ui)
ui.Say("Unmounting os dvd drive...") ui.Say("Unmount/delete os dvd drive...")
dvdController := state.Get("os.dvd.properties").(DvdControllerProperties) dvdControllerState := state.Get("os.dvd.properties")
if dvdControllerState == nil {
return multistep.ActionContinue
}
dvdController := dvdControllerState.(DvdControllerProperties)
if dvdController.Existing { if dvdController.Existing {
err := driver.UnmountDvdDrive(vmName) ui.Say(fmt.Sprintf("Unmounting os dvd drives controller %d location %d ...", dvdController.ControllerNumber, dvdController.ControllerLocation))
err := driver.UnmountDvdDrive(vmName, dvdController.ControllerNumber, dvdController.ControllerLocation)
if err != nil { if err != nil {
err := fmt.Errorf("Error unmounting os dvd drive: %s", err) err := fmt.Errorf("Error unmounting os dvd drive: %s", err)
state.Put("error", err) state.Put("error", err)
@ -31,6 +38,7 @@ func (s *StepUnmountDvdDrive) Run(state multistep.StateBag) multistep.StepAction
return multistep.ActionHalt return multistep.ActionHalt
} }
} else { } else {
ui.Say(fmt.Sprintf("Delete os dvd drives controller %d location %d ...", dvdController.ControllerNumber, dvdController.ControllerLocation))
err := driver.DeleteDvdDrive(vmName, dvdController.ControllerNumber, dvdController.ControllerLocation) err := driver.DeleteDvdDrive(vmName, dvdController.ControllerNumber, dvdController.ControllerLocation)
if err != nil { if err != nil {
err := fmt.Errorf("Error deleting os dvd drive: %s", err) err := fmt.Errorf("Error deleting os dvd drive: %s", err)
@ -40,6 +48,8 @@ func (s *StepUnmountDvdDrive) Run(state multistep.StateBag) multistep.StepAction
} }
} }
state.Put("os.dvd.properties", nil)
return multistep.ActionContinue return multistep.ActionContinue
} }

View File

@ -23,7 +23,7 @@ func (s *StepUnmountFloppyDrive) Run(state multistep.StateBag) multistep.StepAct
} }
vmName := state.Get("vmName").(string) vmName := state.Get("vmName").(string)
ui.Say("Unmounting floppy drive (Run)...") ui.Say("Unmount/delete floppy drive (Run)...")
errorMsg := "Error Unmounting floppy drive: %s" errorMsg := "Error Unmounting floppy drive: %s"

View File

@ -18,12 +18,19 @@ func (s *StepUnmountGuestAdditions) Run(state multistep.StateBag) multistep.Step
vmName := state.Get("vmName").(string) vmName := state.Get("vmName").(string)
ui := state.Get("ui").(packer.Ui) ui := state.Get("ui").(packer.Ui)
ui.Say("Unmounting Integration Services dvd drive...") ui.Say("Unmount/delete Integration Services dvd drive...")
dvdController := state.Get("guest.dvd.properties").(DvdControllerProperties) dvdControllerState := state.Get("guest.dvd.properties")
if dvdControllerState == nil {
return multistep.ActionContinue
}
dvdController := dvdControllerState.(DvdControllerProperties)
if dvdController.Existing { if dvdController.Existing {
err := driver.UnmountDvdDrive(vmName) ui.Say(fmt.Sprintf("Unmounting Integration Services dvd drives controller %d location %d ...", dvdController.ControllerNumber, dvdController.ControllerLocation))
err := driver.UnmountDvdDrive(vmName, dvdController.ControllerNumber, dvdController.ControllerLocation)
if err != nil { if err != nil {
err := fmt.Errorf("Error unmounting Integration Services dvd drive: %s", err) err := fmt.Errorf("Error unmounting Integration Services dvd drive: %s", err)
state.Put("error", err) state.Put("error", err)
@ -31,6 +38,7 @@ func (s *StepUnmountGuestAdditions) Run(state multistep.StateBag) multistep.Step
return multistep.ActionHalt return multistep.ActionHalt
} }
} else { } else {
ui.Say(fmt.Sprintf("Delete Integration Services dvd drives controller %d location %d ...", dvdController.ControllerNumber, dvdController.ControllerLocation))
err := driver.DeleteDvdDrive(vmName, dvdController.ControllerNumber, dvdController.ControllerLocation) err := driver.DeleteDvdDrive(vmName, dvdController.ControllerNumber, dvdController.ControllerLocation)
if err != nil { if err != nil {
err := fmt.Errorf("Error deleting Integration Services dvd drive: %s", err) err := fmt.Errorf("Error deleting Integration Services dvd drive: %s", err)
@ -40,6 +48,8 @@ func (s *StepUnmountGuestAdditions) Run(state multistep.StateBag) multistep.Step
} }
} }
state.Put("guest.dvd.properties", nil)
return multistep.ActionContinue return multistep.ActionContinue
} }

View File

@ -18,13 +18,20 @@ func (s *StepUnmountSecondaryDvdImages) Run(state multistep.StateBag) multistep.
ui := state.Get("ui").(packer.Ui) ui := state.Get("ui").(packer.Ui)
vmName := state.Get("vmName").(string) vmName := state.Get("vmName").(string)
ui.Say("Unmounting secondary dvd drives...") ui.Say("Unmount/delete secondary dvd drives...")
dvdControllers := state.Get("secondary.dvd.properties").([]DvdControllerProperties) dvdControllersState := state.Get("secondary.dvd.properties")
if dvdControllersState == nil {
return multistep.ActionContinue
}
dvdControllers := dvdControllersState.([]DvdControllerProperties)
for _, dvdController := range dvdControllers { for _, dvdController := range dvdControllers {
if dvdController.Existing { if dvdController.Existing {
err := driver.UnmountDvdDrive(vmName) ui.Say(fmt.Sprintf("Unmounting secondary dvd drives controller %d location %d ...", dvdController.ControllerNumber, dvdController.ControllerLocation))
err := driver.UnmountDvdDrive(vmName, dvdController.ControllerNumber, dvdController.ControllerLocation)
if err != nil { if err != nil {
err := fmt.Errorf("Error unmounting secondary dvd drive: %s", err) err := fmt.Errorf("Error unmounting secondary dvd drive: %s", err)
state.Put("error", err) state.Put("error", err)
@ -32,6 +39,7 @@ func (s *StepUnmountSecondaryDvdImages) Run(state multistep.StateBag) multistep.
return multistep.ActionHalt return multistep.ActionHalt
} }
} else { } else {
ui.Say(fmt.Sprintf("Delete secondary dvd drives controller %d location %d ...", dvdController.ControllerNumber, dvdController.ControllerLocation))
err := driver.DeleteDvdDrive(vmName, dvdController.ControllerNumber, dvdController.ControllerLocation) err := driver.DeleteDvdDrive(vmName, dvdController.ControllerNumber, dvdController.ControllerLocation)
if err != nil { if err != nil {
err := fmt.Errorf("Error deleting secondary dvd drive: %s", err) err := fmt.Errorf("Error deleting secondary dvd drive: %s", err)
@ -41,6 +49,8 @@ func (s *StepUnmountSecondaryDvdImages) Run(state multistep.StateBag) multistep.
} }
} }
} }
state.Put("secondary.dvd.properties", nil)
return multistep.ActionContinue return multistep.ActionContinue
} }

View File

@ -118,23 +118,13 @@ param([string]$vmName)
return controllerNumber, controllerLocation, err return controllerNumber, controllerLocation, err
} }
func MountDvdDrive(vmName string, path string) error { func MountDvdDrive(vmName string, path string, controllerNumber uint, controllerLocation uint) error {
var script = `
param([string]$vmName,[string]$path)
Set-VMDvdDrive -VMName $vmName -Path $path
`
var ps powershell.PowerShellCmd
err := ps.Run(script, vmName, path)
return err
}
func MountDvdDriveByLocation(vmName string, path string, controllerNumber uint, controllerLocation uint) error {
var script = ` var script = `
param([string]$vmName,[string]$path,[string]$controllerNumber,[string]$controllerLocation) param([string]$vmName,[string]$path,[string]$controllerNumber,[string]$controllerLocation)
Set-VMDvdDrive -VMName $vmName -Path $path -ControllerNumber $controllerNumber -ControllerLocation $controllerLocation $vmDvdDrive = Get-VMDvdDrive -VMName $vmName -ControllerNumber $controllerNumber -ControllerLocation $controllerLocation
if (!$vmDvdDrive) {throw 'unable to find dvd drive'}
Set-VMDvdDrive -VMName $vmName -ControllerNumber $controllerNumber -ControllerLocation $controllerLocation -Path $path
` `
var ps powershell.PowerShellCmd var ps powershell.PowerShellCmd
@ -142,14 +132,16 @@ Set-VMDvdDrive -VMName $vmName -Path $path -ControllerNumber $controllerNumber -
return err return err
} }
func UnmountDvdDrive(vmName string) error { func UnmountDvdDrive(vmName string, controllerNumber uint, controllerLocation uint) error {
var script = ` var script = `
param([string]$vmName) param([string]$vmName,[int]$controllerNumber,[int]$controllerLocation)
Get-VMDvdDrive -VMName $vmName | Set-VMDvdDrive -Path $null $vmDvdDrive = Get-VMDvdDrive -VMName $vmName -ControllerNumber $controllerNumber -ControllerLocation $controllerLocation
if (!$vmDvdDrive) {throw 'unable to find dvd drive'}
Set-VMDvdDrive -VMName $vmName -ControllerNumber $controllerNumber -ControllerLocation $controllerLocation -Path $null
` `
var ps powershell.PowerShellCmd var ps powershell.PowerShellCmd
err := ps.Run(script, vmName) err := ps.Run(script, vmName, strconv.FormatInt(int64(controllerNumber), 10), strconv.FormatInt(int64(controllerLocation), 10))
return err return err
} }