From e18594f40495ef9d9bef973dabf7dbbdb8b01dad Mon Sep 17 00:00:00 2001 From: Taliesin Sisson Date: Sun, 12 Jul 2015 18:44:10 +0100 Subject: [PATCH] Hyperv generation 2 machines use scsi for dvd drives. Allow gen 1 machines to have at least 1 iso image attached --- .../common/step_mount_integration_services.go | 52 ++++++++++++------- builder/hyperv/iso/builder.go | 1 + 2 files changed, 33 insertions(+), 20 deletions(-) diff --git a/builder/hyperv/common/step_mount_integration_services.go b/builder/hyperv/common/step_mount_integration_services.go index 05eaf6dc8..66684ea63 100644 --- a/builder/hyperv/common/step_mount_integration_services.go +++ b/builder/hyperv/common/step_mount_integration_services.go @@ -6,20 +6,21 @@ package common import ( "fmt" - "log" - "os" "github.com/mitchellh/multistep" "github.com/mitchellh/packer/packer" powershell "github.com/mitchellh/packer/powershell" + "log" + "os" ) type StepMountSecondaryDvdImages struct { - Files [] string + Files []string dvdProperties []DvdControllerProperties + generation uint } type DvdControllerProperties struct { - ControllerNumber string + ControllerNumber string ControllerLocation string } @@ -34,13 +35,13 @@ func (s *StepMountSecondaryDvdImages) Run(state multistep.StateBag) multistep.St // Will Windows assign DVD drives to A: and B: ? // For IDE, there are only 2 controllers (0,1) with 2 locations each (0,1) - dvdProperties, err := s.mountFiles(vmName); + dvdProperties, err := s.mountFiles(vmName) if err != nil { state.Put("error", err) ui.Error(err.Error()) return multistep.ActionHalt } - + log.Println(fmt.Sprintf("Saving DVD properties %s DVDs", len(dvdProperties))) state.Put("secondary.dvd.properties", dvdProperties) @@ -52,7 +53,6 @@ func (s *StepMountSecondaryDvdImages) Cleanup(state multistep.StateBag) { } - func (s *StepMountSecondaryDvdImages) mountFiles(vmName string) ([]DvdControllerProperties, error) { var dvdProperties []DvdControllerProperties @@ -76,7 +76,6 @@ func (s *StepMountSecondaryDvdImages) mountFiles(vmName string) ([]DvdController return dvdProperties, nil } - func (s *StepMountSecondaryDvdImages) addAndMountIntegrationServicesSetupDisk(vmName string) (DvdControllerProperties, error) { isoPath := os.Getenv("WINDIR") + "\\system32\\vmguest.iso" @@ -88,28 +87,41 @@ func (s *StepMountSecondaryDvdImages) addAndMountIntegrationServicesSetupDisk(vm return properties, nil } - - - func (s *StepMountSecondaryDvdImages) addAndMountDvdDisk(vmName string, isoPath string) (DvdControllerProperties, error) { var properties DvdControllerProperties var script powershell.ScriptBuilder powershell := new(powershell.PowerShellCmd) - // get the controller number that the OS install disk is mounted on - script.Reset() - script.WriteLine("param([string]$vmName)") - script.WriteLine("(Get-VMDvdDrive -VMName $vmName).ControllerNumber") - controllerNumber, err := powershell.Output(script.String(), vmName) - if err != nil { - return properties, err + controllerNumber := "0" + if s.generation < 2 { + // get the controller number that the OS install disk is mounted on + // generation 1 requires dvd to be added to ide controller, generation 2 uses scsi for dvd drives + script.Reset() + script.WriteLine("param([string]$vmName)") + script.WriteLine("$dvdDrives = (Get-VMDvdDrive -VMName $vmName)") + script.WriteLine("$lastControllerNumber = $dvdDrives | Sort-Object ControllerNumber | Select-Object -Last 1 | %{$_.ControllerNumber}") + script.WriteLine("if (!$lastControllerNumber) {") + script.WriteLine(" $lastControllerNumber = 0") + script.WriteLine("} elseif (!$lastControllerNumber -or ($dvdDrives | ?{ $_.ControllerNumber -eq $lastControllerNumber} | measure).count -gt 1) {") + script.WriteLine(" $lastControllerNumber += 1") + script.WriteLine("}") + script.WriteLine("$lastControllerNumber") + controllerNumber, err := powershell.Output(script.String(), vmName) + if err != nil { + return properties, err + } + + if controllerNumber != "0" || controllerNumber != "1" { + //There are only 2 ide controllers, try to use the one the hdd is attached too + controllerNumber = "0" + } } script.Reset() script.WriteLine("param([string]$vmName,[int]$controllerNumber)") script.WriteLine("Add-VMDvdDrive -VMName $vmName -ControllerNumber $controllerNumber") - err = powershell.Run(script.String(), vmName, controllerNumber) + err := powershell.Run(script.String(), vmName, controllerNumber) if err != nil { return properties, err } @@ -133,7 +145,7 @@ func (s *StepMountSecondaryDvdImages) addAndMountDvdDisk(vmName string, isoPath return properties, err } - 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)) properties.ControllerNumber = controllerNumber properties.ControllerLocation = controllerLocation diff --git a/builder/hyperv/iso/builder.go b/builder/hyperv/iso/builder.go index e1b2282f8..4955b733b 100644 --- a/builder/hyperv/iso/builder.go +++ b/builder/hyperv/iso/builder.go @@ -310,6 +310,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe &hypervcommon.StepMountSecondaryDvdImages{ Files: b.config.SecondaryDvdImages, + generation: b.config.Generation, }, &hypervcommon.StepRun{