Hyperv generation 2 machines use scsi for dvd drives. Allow gen 1 machines to have at least 1 iso image attached

This commit is contained in:
Taliesin Sisson 2015-07-12 18:44:10 +01:00
parent ef507c7bd7
commit e18594f404
2 changed files with 33 additions and 20 deletions

View File

@ -6,16 +6,17 @@ package common
import ( import (
"fmt" "fmt"
"log"
"os"
"github.com/mitchellh/multistep" "github.com/mitchellh/multistep"
"github.com/mitchellh/packer/packer" "github.com/mitchellh/packer/packer"
powershell "github.com/mitchellh/packer/powershell" powershell "github.com/mitchellh/packer/powershell"
"log"
"os"
) )
type StepMountSecondaryDvdImages struct { type StepMountSecondaryDvdImages struct {
Files [] string Files []string
dvdProperties []DvdControllerProperties dvdProperties []DvdControllerProperties
generation uint
} }
type DvdControllerProperties struct { type DvdControllerProperties struct {
@ -34,7 +35,7 @@ func (s *StepMountSecondaryDvdImages) Run(state multistep.StateBag) multistep.St
// Will Windows assign DVD drives to A: and B: ? // Will Windows assign DVD drives to A: and B: ?
// For IDE, there are only 2 controllers (0,1) with 2 locations each (0,1) // 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 { if err != nil {
state.Put("error", err) state.Put("error", err)
ui.Error(err.Error()) ui.Error(err.Error())
@ -52,7 +53,6 @@ func (s *StepMountSecondaryDvdImages) Cleanup(state multistep.StateBag) {
} }
func (s *StepMountSecondaryDvdImages) mountFiles(vmName string) ([]DvdControllerProperties, error) { func (s *StepMountSecondaryDvdImages) mountFiles(vmName string) ([]DvdControllerProperties, error) {
var dvdProperties []DvdControllerProperties var dvdProperties []DvdControllerProperties
@ -76,7 +76,6 @@ func (s *StepMountSecondaryDvdImages) mountFiles(vmName string) ([]DvdController
return dvdProperties, nil return dvdProperties, nil
} }
func (s *StepMountSecondaryDvdImages) addAndMountIntegrationServicesSetupDisk(vmName string) (DvdControllerProperties, error) { func (s *StepMountSecondaryDvdImages) addAndMountIntegrationServicesSetupDisk(vmName string) (DvdControllerProperties, error) {
isoPath := os.Getenv("WINDIR") + "\\system32\\vmguest.iso" isoPath := os.Getenv("WINDIR") + "\\system32\\vmguest.iso"
@ -88,28 +87,41 @@ func (s *StepMountSecondaryDvdImages) addAndMountIntegrationServicesSetupDisk(vm
return properties, nil return properties, nil
} }
func (s *StepMountSecondaryDvdImages) addAndMountDvdDisk(vmName string, isoPath string) (DvdControllerProperties, error) { func (s *StepMountSecondaryDvdImages) addAndMountDvdDisk(vmName string, isoPath string) (DvdControllerProperties, error) {
var properties DvdControllerProperties var properties DvdControllerProperties
var script powershell.ScriptBuilder var script powershell.ScriptBuilder
powershell := new(powershell.PowerShellCmd) powershell := new(powershell.PowerShellCmd)
controllerNumber := "0"
if s.generation < 2 {
// get the controller number that the OS install disk is mounted on // 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.Reset()
script.WriteLine("param([string]$vmName)") script.WriteLine("param([string]$vmName)")
script.WriteLine("(Get-VMDvdDrive -VMName $vmName).ControllerNumber") 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) controllerNumber, err := powershell.Output(script.String(), vmName)
if err != nil { if err != nil {
return properties, err 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.Reset()
script.WriteLine("param([string]$vmName,[int]$controllerNumber)") script.WriteLine("param([string]$vmName,[int]$controllerNumber)")
script.WriteLine("Add-VMDvdDrive -VMName $vmName -ControllerNumber $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 { if err != nil {
return properties, err return properties, err
} }
@ -133,7 +145,7 @@ func (s *StepMountSecondaryDvdImages) addAndMountDvdDisk(vmName string, isoPath
return properties, err 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.ControllerNumber = controllerNumber
properties.ControllerLocation = controllerLocation properties.ControllerLocation = controllerLocation

View File

@ -310,6 +310,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
&hypervcommon.StepMountSecondaryDvdImages{ &hypervcommon.StepMountSecondaryDvdImages{
Files: b.config.SecondaryDvdImages, Files: b.config.SecondaryDvdImages,
generation: b.config.Generation,
}, },
&hypervcommon.StepRun{ &hypervcommon.StepRun{