From 3cb621f89069ade893a96d10d95dbb3131e1ad6c Mon Sep 17 00:00:00 2001 From: Taliesin Sisson Date: Sun, 1 Nov 2015 16:00:56 +0000 Subject: [PATCH] Get rid of mount and unmount that does not specify controller location exactly Handle cleanup if unmount step has not already done so --- builder/hyperv/common/driver.go | 8 ++-- builder/hyperv/common/driver_ps_4.go | 12 ++---- builder/hyperv/common/step_mount_dvddrive.go | 34 ++++++++--------- .../common/step_mount_guest_additions.go | 38 +++++++++---------- .../common/step_mount_secondary_dvd_images.go | 31 +++++++-------- .../hyperv/common/step_unmount_dvddrive.go | 16 ++++++-- .../hyperv/common/step_unmount_floppydrive.go | 2 +- .../common/step_unmount_guest_additions.go | 18 +++++++-- .../step_unmount_secondary_dvd_images.go | 18 +++++++-- powershell/hyperv/hyperv.go | 28 +++++--------- 10 files changed, 107 insertions(+), 98 deletions(-) diff --git a/builder/hyperv/common/driver.go b/builder/hyperv/common/driver.go index 6ceb37c0f..f0a6d9d86 100644 --- a/builder/hyperv/common/driver.go +++ b/builder/hyperv/common/driver.go @@ -88,13 +88,11 @@ type Driver interface { CreateDvdDrive(string, uint) (uint, uint, error) - MountDvdDrive(string, string) error - - MountDvdDriveByLocation(string, string, uint, uint) error + MountDvdDrive(string, string, uint, uint) error SetBootDvdDrive(string, uint, uint) error - - UnmountDvdDrive(string) error + + UnmountDvdDrive(string, uint, uint) error DeleteDvdDrive(string, uint, uint) error diff --git a/builder/hyperv/common/driver_ps_4.go b/builder/hyperv/common/driver_ps_4.go index c6d6d4e65..bc2040811 100644 --- a/builder/hyperv/common/driver_ps_4.go +++ b/builder/hyperv/common/driver_ps_4.go @@ -209,20 +209,16 @@ func (d *HypervPS4Driver) CreateDvdDrive(vmName string, generation uint) (uint, return hyperv.CreateDvdDrive(vmName, generation) } -func (d *HypervPS4Driver) MountDvdDrive(vmName string, path string) error { - return hyperv.MountDvdDrive(vmName, path) -} - -func (d *HypervPS4Driver) MountDvdDriveByLocation(vmName string, path string, controllerNumber uint, controllerLocation uint) error { - return hyperv.MountDvdDriveByLocation(vmName, path, controllerNumber, controllerLocation) +func (d *HypervPS4Driver) MountDvdDrive(vmName string, path string, controllerNumber uint, controllerLocation uint) error { + return hyperv.MountDvdDrive(vmName, path, controllerNumber, controllerLocation) } func (d *HypervPS4Driver) SetBootDvdDrive(vmName string, controllerNumber uint, controllerLocation uint) error { return hyperv.SetBootDvdDrive(vmName, controllerNumber, controllerLocation) } -func (d *HypervPS4Driver) UnmountDvdDrive(vmName string) error { - return hyperv.UnmountDvdDrive(vmName) +func (d *HypervPS4Driver) UnmountDvdDrive(vmName string, controllerNumber uint, controllerLocation uint) error { + return hyperv.UnmountDvdDrive(vmName, controllerNumber, controllerLocation) } func (d *HypervPS4Driver) DeleteDvdDrive(vmName string, controllerNumber uint, controllerLocation uint) error { diff --git a/builder/hyperv/common/step_mount_dvddrive.go b/builder/hyperv/common/step_mount_dvddrive.go index f325ada94..75af552ad 100644 --- a/builder/hyperv/common/step_mount_dvddrive.go +++ b/builder/hyperv/common/step_mount_dvddrive.go @@ -13,8 +13,6 @@ import ( type StepMountDvdDrive struct { Generation uint - cleanup bool - dvdProperties DvdControllerProperties } 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.ControllerLocation = controllerLocation - s.cleanup = true - s.dvdProperties = dvdControllerProperties + dvdControllerProperties.Existing = false + + 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) if err != nil { 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)) - err = driver.MountDvdDrive(vmName, isoPath) + err = driver.MountDvdDrive(vmName, isoPath, controllerNumber, controllerLocation) if err != nil { err := fmt.Errorf(errorMsg, err) state.Put("error", err) @@ -62,35 +61,32 @@ func (s *StepMountDvdDrive) Run(state multistep.StateBag) multistep.StepAction { return multistep.ActionHalt } - state.Put("os.dvd.properties", dvdControllerProperties) - return multistep.ActionContinue } -func (s *StepMountDvdDrive) Cleanup(state multistep.StateBag) { - if !s.cleanup { +func (s *StepMountDvdDrive) Cleanup(state multistep.StateBag) { + dvdControllerState := state.Get("os.dvd.properties") + + if dvdControllerState == nil { return } - + + dvdController := dvdControllerState.(DvdControllerProperties) driver := state.Get("driver").(Driver) - - errorMsg := "Error unmounting os dvd drive: %s" - vmName := state.Get("vmName").(string) ui := state.Get("ui").(packer.Ui) + errorMsg := "Error unmounting os dvd drive: %s" ui.Say("Clean up os dvd drive...") - dvdControllerProperties := s.dvdProperties - - if dvdControllerProperties.Existing { - err := driver.UnmountDvdDrive(vmName) + if dvdController.Existing { + err := driver.UnmountDvdDrive(vmName, dvdController.ControllerNumber, dvdController.ControllerLocation) if err != nil { err := fmt.Errorf("Error unmounting dvd drive: %s", err) log.Print(fmt.Sprintf(errorMsg, err)) } } else { - err := driver.DeleteDvdDrive(vmName, dvdControllerProperties.ControllerNumber, dvdControllerProperties.ControllerLocation) + err := driver.DeleteDvdDrive(vmName, dvdController.ControllerNumber, dvdController.ControllerLocation) if err != nil { err := fmt.Errorf("Error deleting dvd drive: %s", err) log.Print(fmt.Sprintf(errorMsg, err)) diff --git a/builder/hyperv/common/step_mount_guest_additions.go b/builder/hyperv/common/step_mount_guest_additions.go index 33c252082..ee6b69afc 100644 --- a/builder/hyperv/common/step_mount_guest_additions.go +++ b/builder/hyperv/common/step_mount_guest_additions.go @@ -15,8 +15,6 @@ type StepMountGuestAdditions struct { GuestAdditionsMode string GuestAdditionsPath string Generation uint - cleanup bool - dvdProperties DvdControllerProperties } 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.ControllerLocation = controllerLocation - s.cleanup = true - s.dvdProperties = dvdControllerProperties + dvdControllerProperties.Existing = false + state.Put("guest.dvd.properties", dvdControllerProperties) 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 { state.Put("error", err) 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)) - state.Put("guest.dvd.properties", dvdControllerProperties) - return multistep.ActionContinue } func (s *StepMountGuestAdditions) Cleanup(state multistep.StateBag) { - if !s.cleanup || s.GuestAdditionsMode != "attach" { + 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) - ui.Say("Cleanup Integration Services dvd drive...") - + driver := state.Get("driver").(Driver) vmName := state.Get("vmName").(string) - - dvdControllerProperties := s.dvdProperties - errorMsg := "Error unmounting Integration Services dvd drive: %s" - if dvdControllerProperties.Existing { - err := driver.UnmountDvdDrive(vmName) + 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, dvdControllerProperties.ControllerNumber, dvdControllerProperties.ControllerLocation) + err := driver.DeleteDvdDrive(vmName, dvdController.ControllerNumber, dvdController.ControllerLocation) if err != nil { log.Print(fmt.Sprintf(errorMsg, err)) } } -} +} \ No newline at end of file diff --git a/builder/hyperv/common/step_mount_secondary_dvd_images.go b/builder/hyperv/common/step_mount_secondary_dvd_images.go index 5d220b55b..548fd4a8a 100644 --- a/builder/hyperv/common/step_mount_secondary_dvd_images.go +++ b/builder/hyperv/common/step_mount_secondary_dvd_images.go @@ -14,8 +14,6 @@ import ( type StepMountSecondaryDvdImages struct { IsoPaths []string Generation uint - cleanup bool - dvdProperties []DvdControllerProperties } type DvdControllerProperties struct { @@ -50,13 +48,12 @@ func (s *StepMountSecondaryDvdImages) Run(state multistep.StateBag) multistep.St properties.ControllerNumber = controllerNumber properties.ControllerLocation = controllerLocation - - s.cleanup = true + properties.Existing = false dvdProperties = append(dvdProperties, properties) - s.dvdProperties = dvdProperties + state.Put("secondary.dvd.properties", dvdProperties) 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 { state.Put("error", err) 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)) } - state.Put("secondary.dvd.properties", dvdProperties) - return multistep.ActionContinue } func (s *StepMountSecondaryDvdImages) Cleanup(state multistep.StateBag) { - if (!s.cleanup){ + dvdControllersState := state.Get("secondary.dvd.properties") + + if dvdControllersState == nil { return } + dvdControllers := dvdControllersState.([]DvdControllerProperties) driver := state.Get("driver").(Driver) 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...") - vmName := state.Get("vmName").(string) - - errorMsg := "Error unmounting secondary dvd drive: %s" - - for _, dvdControllerProperties := range s.dvdProperties { + for _, dvdController := range dvdControllers { - if dvdControllerProperties.Existing { - err := driver.UnmountDvdDrive(vmName) + 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, dvdControllerProperties.ControllerNumber, dvdControllerProperties.ControllerLocation) + err := driver.DeleteDvdDrive(vmName, dvdController.ControllerNumber, dvdController.ControllerLocation) if err != nil { log.Print(fmt.Sprintf(errorMsg, err)) } diff --git a/builder/hyperv/common/step_unmount_dvddrive.go b/builder/hyperv/common/step_unmount_dvddrive.go index c0f4d3e1c..26f327cc8 100644 --- a/builder/hyperv/common/step_unmount_dvddrive.go +++ b/builder/hyperv/common/step_unmount_dvddrive.go @@ -18,12 +18,19 @@ func (s *StepUnmountDvdDrive) Run(state multistep.StateBag) multistep.StepAction vmName := state.Get("vmName").(string) 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 { - 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 { err := fmt.Errorf("Error unmounting os dvd drive: %s", err) state.Put("error", err) @@ -31,6 +38,7 @@ func (s *StepUnmountDvdDrive) Run(state multistep.StateBag) multistep.StepAction return multistep.ActionHalt } } 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) if err != nil { 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 } diff --git a/builder/hyperv/common/step_unmount_floppydrive.go b/builder/hyperv/common/step_unmount_floppydrive.go index 45546e1d4..5ae920730 100644 --- a/builder/hyperv/common/step_unmount_floppydrive.go +++ b/builder/hyperv/common/step_unmount_floppydrive.go @@ -23,7 +23,7 @@ func (s *StepUnmountFloppyDrive) Run(state multistep.StateBag) multistep.StepAct } vmName := state.Get("vmName").(string) - ui.Say("Unmounting floppy drive (Run)...") + ui.Say("Unmount/delete floppy drive (Run)...") errorMsg := "Error Unmounting floppy drive: %s" diff --git a/builder/hyperv/common/step_unmount_guest_additions.go b/builder/hyperv/common/step_unmount_guest_additions.go index d4b505001..56c98791a 100644 --- a/builder/hyperv/common/step_unmount_guest_additions.go +++ b/builder/hyperv/common/step_unmount_guest_additions.go @@ -18,12 +18,19 @@ func (s *StepUnmountGuestAdditions) Run(state multistep.StateBag) multistep.Step vmName := state.Get("vmName").(string) ui := state.Get("ui").(packer.Ui) - ui.Say("Unmounting Integration Services dvd drive...") - - dvdController := state.Get("guest.dvd.properties").(DvdControllerProperties) + ui.Say("Unmount/delete Integration Services dvd drive...") + + dvdControllerState := state.Get("guest.dvd.properties") + + if dvdControllerState == nil { + return multistep.ActionContinue + } + + dvdController := dvdControllerState.(DvdControllerProperties) 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 { err := fmt.Errorf("Error unmounting Integration Services dvd drive: %s", err) state.Put("error", err) @@ -31,6 +38,7 @@ func (s *StepUnmountGuestAdditions) Run(state multistep.StateBag) multistep.Step return multistep.ActionHalt } } 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) if err != nil { 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 } diff --git a/builder/hyperv/common/step_unmount_secondary_dvd_images.go b/builder/hyperv/common/step_unmount_secondary_dvd_images.go index ceb6663b7..7c5b27781 100644 --- a/builder/hyperv/common/step_unmount_secondary_dvd_images.go +++ b/builder/hyperv/common/step_unmount_secondary_dvd_images.go @@ -18,13 +18,20 @@ func (s *StepUnmountSecondaryDvdImages) Run(state multistep.StateBag) multistep. ui := state.Get("ui").(packer.Ui) vmName := state.Get("vmName").(string) - ui.Say("Unmounting secondary dvd drives...") - - dvdControllers := state.Get("secondary.dvd.properties").([]DvdControllerProperties) + ui.Say("Unmount/delete secondary dvd drives...") + + dvdControllersState := state.Get("secondary.dvd.properties") + + if dvdControllersState == nil { + return multistep.ActionContinue + } + + dvdControllers := dvdControllersState.([]DvdControllerProperties) for _, dvdController := range dvdControllers { 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 { err := fmt.Errorf("Error unmounting secondary dvd drive: %s", err) state.Put("error", err) @@ -32,6 +39,7 @@ func (s *StepUnmountSecondaryDvdImages) Run(state multistep.StateBag) multistep. return multistep.ActionHalt } } 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) if err != nil { 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 } diff --git a/powershell/hyperv/hyperv.go b/powershell/hyperv/hyperv.go index bcba3edf5..e25ae9313 100644 --- a/powershell/hyperv/hyperv.go +++ b/powershell/hyperv/hyperv.go @@ -118,23 +118,13 @@ param([string]$vmName) return controllerNumber, controllerLocation, err } -func MountDvdDrive(vmName string, path string) 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 { +func MountDvdDrive(vmName string, path string, controllerNumber uint, controllerLocation uint) error { var script = ` 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 @@ -142,14 +132,16 @@ Set-VMDvdDrive -VMName $vmName -Path $path -ControllerNumber $controllerNumber - return err } -func UnmountDvdDrive(vmName string) error { +func UnmountDvdDrive(vmName string, controllerNumber uint, controllerLocation uint) error { var script = ` -param([string]$vmName) -Get-VMDvdDrive -VMName $vmName | Set-VMDvdDrive -Path $null +param([string]$vmName,[int]$controllerNumber,[int]$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 $null ` 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 }