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,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

View File

@ -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{