Add Hyper-V support for Gen-1 boot order with ISO

This commit is contained in:
William Brooks 2020-02-09 13:08:22 -06:00
parent e490b7651d
commit 55ae803852
7 changed files with 26 additions and 8 deletions

View File

@ -148,6 +148,13 @@ type CommonConfig struct {
// built. When this value is set to true, the machine will start without a
// console.
Headless bool `mapstructure:"headless" required:"false"`
// Over time the Hyper-V builder has been modified to change the original
// boot order that is used when an ISO is mounted. Hyper-V's default is to
// boot from the CD first, the original Hyper-V builder included code to
// codify this setting when the primary ISO is mounted, that code was eventually
// modified to place the IDE adapter before the the CD (only in generation 1).
// Setting this value to true, forces the original method of operation.
LegacyGen1BootOrder bool `mapstructure:"legacy_gen1_boot_order" required:"false"`
}
func (c *CommonConfig) Prepare(ctx *interpolate.Context, pc *common.PackerConfig) ([]error, []string) {

View File

@ -111,7 +111,7 @@ type Driver interface {
MountDvdDrive(string, string, uint, uint) error
SetBootDvdDrive(string, uint, uint, uint) error
SetBootDvdDrive(string, uint, uint, uint, bool) error
UnmountDvdDrive(string, uint, uint) error

View File

@ -263,8 +263,8 @@ func (d *HypervPS4Driver) MountDvdDrive(vmName string, path string, controllerNu
}
func (d *HypervPS4Driver) SetBootDvdDrive(vmName string, controllerNumber uint, controllerLocation uint,
generation uint) error {
return hyperv.SetBootDvdDrive(vmName, controllerNumber, controllerLocation, generation)
generation uint, legacyGen1BootOrder bool) error {
return hyperv.SetBootDvdDrive(vmName, controllerNumber, controllerLocation, generation, legacyGen1BootOrder)
}
func (d *HypervPS4Driver) UnmountDvdDrive(vmName string, controllerNumber uint, controllerLocation uint) error {

View File

@ -13,6 +13,7 @@ import (
type StepMountDvdDrive struct {
Generation uint
LegacyGen1BootOrder bool
}
func (s *StepMountDvdDrive) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
@ -58,7 +59,7 @@ func (s *StepMountDvdDrive) Run(ctx context.Context, state multistep.StateBag) m
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, s.Generation)
err = driver.SetBootDvdDrive(vmName, controllerNumber, controllerLocation, s.Generation, s.LegacyGen1BootOrder)
if err != nil {
err := fmt.Errorf(errorMsg, err)
state.Put("error", err)

View File

@ -242,7 +242,8 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack
&hypervcommon.StepEnableIntegrationService{},
&hypervcommon.StepMountDvdDrive{
Generation: b.config.Generation,
Generation: b.config.Generation,
LegacyGen1BootOrder: b.config.LegacyGen1BootOrder,
},
&hypervcommon.StepMountFloppydrive{
Generation: b.config.Generation,

View File

@ -282,7 +282,8 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack
&hypervcommon.StepEnableIntegrationService{},
&hypervcommon.StepMountDvdDrive{
Generation: b.config.Generation,
Generation: b.config.Generation,
LegacyGen1BootOrder: b.config.LegacyGen1BootOrder,
},
&hypervcommon.StepMountFloppydrive{
Generation: b.config.Generation,

View File

@ -156,13 +156,21 @@ Hyper-V\Set-VMDvdDrive -VMName $vmName -ControllerNumber $controllerNumber -Cont
return err
}
func SetBootDvdDrive(vmName string, controllerNumber uint, controllerLocation uint, generation uint) error {
func SetBootDvdDrive(vmName string, controllerNumber uint, controllerLocation uint, generation uint, legacyGen1BootOrder bool) error {
if generation < 2 {
script := `
var script string
if (legacyGen1BootOrder) {
script = `
param([string]$vmName)
Hyper-V\Set-VMBios -VMName $vmName -StartupOrder @("CD","IDE","LegacyNetworkAdapter","Floppy")
`
} else {
script = `
param([string]$vmName)
Hyper-V\Set-VMBios -VMName $vmName -StartupOrder @("IDE","CD","LegacyNetworkAdapter","Floppy")
`
}
var ps powershell.PowerShellCmd
err := ps.Run(script, vmName)
return err