From 18241e5215556e19ddc930b724836fd0d4caf160 Mon Sep 17 00:00:00 2001 From: Taliesin Sisson Date: Sat, 7 Nov 2015 13:42:26 +0000 Subject: [PATCH] Setting boot drive is generation specific --- builder/hyperv/common/driver.go | 8 +++---- builder/hyperv/common/driver_ps_4.go | 4 ++-- builder/hyperv/common/step_mount_dvddrive.go | 14 ++++++------- powershell/hyperv/hyperv.go | 22 ++++++++++++++------ 4 files changed, 29 insertions(+), 19 deletions(-) diff --git a/builder/hyperv/common/driver.go b/builder/hyperv/common/driver.go index f0a6d9d86..44fa37c90 100644 --- a/builder/hyperv/common/driver.go +++ b/builder/hyperv/common/driver.go @@ -89,14 +89,14 @@ type Driver interface { CreateDvdDrive(string, uint) (uint, uint, error) MountDvdDrive(string, string, uint, uint) error - - SetBootDvdDrive(string, uint, uint) error - + + SetBootDvdDrive(string, uint, uint, uint) error + UnmountDvdDrive(string, uint, uint) error DeleteDvdDrive(string, uint, uint) error MountFloppyDrive(string, string) error - + UnmountFloppyDrive(string) error } diff --git a/builder/hyperv/common/driver_ps_4.go b/builder/hyperv/common/driver_ps_4.go index bc2040811..0e6507ede 100644 --- a/builder/hyperv/common/driver_ps_4.go +++ b/builder/hyperv/common/driver_ps_4.go @@ -213,8 +213,8 @@ func (d *HypervPS4Driver) MountDvdDrive(vmName string, path string, controllerNu 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) SetBootDvdDrive(vmName string, controllerNumber uint, controllerLocation uint, generation uint) error { + return hyperv.SetBootDvdDrive(vmName, controllerNumber, controllerLocation, generation) } func (d *HypervPS4Driver) UnmountDvdDrive(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 75af552ad..346af2c9e 100644 --- a/builder/hyperv/common/step_mount_dvddrive.go +++ b/builder/hyperv/common/step_mount_dvddrive.go @@ -12,7 +12,7 @@ import ( ) type StepMountDvdDrive struct { - Generation uint + Generation uint } func (s *StepMountDvdDrive) Run(state multistep.StateBag) multistep.StepAction { @@ -40,11 +40,11 @@ func (s *StepMountDvdDrive) Run(state multistep.StateBag) multistep.StepAction { dvdControllerProperties.ControllerNumber = controllerNumber dvdControllerProperties.ControllerLocation = controllerLocation dvdControllerProperties.Existing = false - + state.Put("os.dvd.properties", dvdControllerProperties) 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, s.Generation) if err != nil { err := fmt.Errorf(errorMsg, err) state.Put("error", err) @@ -64,14 +64,14 @@ func (s *StepMountDvdDrive) Run(state multistep.StateBag) multistep.StepAction { return multistep.ActionContinue } -func (s *StepMountDvdDrive) Cleanup(state multistep.StateBag) { +func (s *StepMountDvdDrive) Cleanup(state multistep.StateBag) { dvdControllerState := state.Get("os.dvd.properties") - + if dvdControllerState == nil { return } - - dvdController := dvdControllerState.(DvdControllerProperties) + + dvdController := dvdControllerState.(DvdControllerProperties) driver := state.Get("driver").(Driver) vmName := state.Get("vmName").(string) ui := state.Get("ui").(packer.Ui) diff --git a/powershell/hyperv/hyperv.go b/powershell/hyperv/hyperv.go index 3e5e49308..e26e432cb 100644 --- a/powershell/hyperv/hyperv.go +++ b/powershell/hyperv/hyperv.go @@ -135,17 +135,27 @@ Set-VMDvdDrive -VMName $vmName -ControllerNumber $controllerNumber -ControllerLo return err } -func SetBootDvdDrive(vmName string, controllerNumber uint, controllerLocation uint) error { - var script = ` +func SetBootDvdDrive(vmName string, controllerNumber uint, controllerLocation uint, generation uint) error { + + if generation < 2 { + script := ` +param([string]$vmName) +Set-VMBios -VMName $vmName -StartupOrder @("CD", "IDE","LegacyNetworkAdapter","Floppy") +` + var ps powershell.PowerShellCmd + err := ps.Run(script, vmName) + return err + } else { + script := ` 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-VMFirmware -VMName $vmName -FirstBootDevice $vmDvdDrive ` - - var ps powershell.PowerShellCmd - err := ps.Run(script, vmName, strconv.FormatInt(int64(controllerNumber), 10), strconv.FormatInt(int64(controllerLocation), 10)) - return err + var ps powershell.PowerShellCmd + err := ps.Run(script, vmName, strconv.FormatInt(int64(controllerNumber), 10), strconv.FormatInt(int64(controllerLocation), 10)) + return err + } } func DeleteDvdDrive(vmName string, controllerNumber uint, controllerLocation uint) error {