Merge pull request #8714 from williamb1024/hyperv-gen1-boot-order
WIP: Add Hyper-V builder `first_boot_device` setting to allow the selection of the initial device or device class used for booting the VM.
This commit is contained in:
commit
9c9826ee4b
|
@ -148,6 +148,21 @@ type CommonConfig struct {
|
|||
// built. When this value is set to true, the machine will start without a
|
||||
// console.
|
||||
Headless bool `mapstructure:"headless" required:"false"`
|
||||
// When configured, determines the device or device type that is given preferential
|
||||
// treatment when choosing a boot device.
|
||||
//
|
||||
// For Generation 1:
|
||||
// - `IDE`
|
||||
// - `CD` *or* `DVD`
|
||||
// - `Floppy`
|
||||
// - `NET`
|
||||
//
|
||||
// For Generation 2:
|
||||
// - `IDE:x:y`
|
||||
// - `SCSI:x:y`
|
||||
// - `CD` *or* `DVD`
|
||||
// - `NET`
|
||||
FirstBootDevice string `mapstructure:"first_boot_device" required:"false"`
|
||||
}
|
||||
|
||||
func (c *CommonConfig) Prepare(ctx *interpolate.Context, pc *common.PackerConfig) ([]error, []string) {
|
||||
|
@ -268,6 +283,13 @@ func (c *CommonConfig) Prepare(ctx *interpolate.Context, pc *common.PackerConfig
|
|||
}
|
||||
}
|
||||
|
||||
if c.FirstBootDevice != "" {
|
||||
_, _, _, err := ParseBootDeviceIdentifier(c.FirstBootDevice, c.Generation)
|
||||
if err != nil {
|
||||
errs = append(errs, fmt.Errorf("first_boot_device: %s", err))
|
||||
}
|
||||
}
|
||||
|
||||
if c.EnableVirtualizationExtensions {
|
||||
if c.EnableDynamicMemory {
|
||||
warning := fmt.Sprintf("For nested virtualization, when virtualization extension is enabled, " +
|
||||
|
|
|
@ -113,6 +113,8 @@ type Driver interface {
|
|||
|
||||
SetBootDvdDrive(string, uint, uint, uint) error
|
||||
|
||||
SetFirstBootDevice(string, string, uint, uint, uint) error
|
||||
|
||||
UnmountDvdDrive(string, uint, uint) error
|
||||
|
||||
DeleteDvdDrive(string, uint, uint) error
|
||||
|
|
|
@ -236,6 +236,14 @@ type DriverMock struct {
|
|||
SetBootDvdDrive_Generation uint
|
||||
SetBootDvdDrive_Err error
|
||||
|
||||
SetFirstBootDevice_Called bool
|
||||
SetFirstBootDevice_VmName string
|
||||
SetFirstBootDevice_ControllerType string
|
||||
SetFirstBootDevice_ControllerNumber uint
|
||||
SetFirstBootDevice_ControllerLocation uint
|
||||
SetFirstBootDevice_Generation uint
|
||||
SetFirstBootDevice_Err error
|
||||
|
||||
UnmountDvdDrive_Called bool
|
||||
UnmountDvdDrive_VmName string
|
||||
UnmountDvdDrive_ControllerNumber uint
|
||||
|
@ -575,6 +583,17 @@ func (d *DriverMock) SetBootDvdDrive(vmName string, controllerNumber uint, contr
|
|||
return d.SetBootDvdDrive_Err
|
||||
}
|
||||
|
||||
func (d *DriverMock) SetFirstBootDevice(vmName string, controllerType string, controllerNumber uint,
|
||||
controllerLocation uint, generation uint) error {
|
||||
d.SetFirstBootDevice_Called = true
|
||||
d.SetFirstBootDevice_VmName = vmName
|
||||
d.SetFirstBootDevice_ControllerType = controllerType
|
||||
d.SetFirstBootDevice_ControllerNumber = controllerNumber
|
||||
d.SetFirstBootDevice_ControllerLocation = controllerLocation
|
||||
d.SetFirstBootDevice_Generation = generation
|
||||
return d.SetFirstBootDevice_Err
|
||||
}
|
||||
|
||||
func (d *DriverMock) UnmountDvdDrive(vmName string, controllerNumber uint, controllerLocation uint) error {
|
||||
d.UnmountDvdDrive_Called = true
|
||||
d.UnmountDvdDrive_VmName = vmName
|
||||
|
|
|
@ -267,6 +267,11 @@ func (d *HypervPS4Driver) SetBootDvdDrive(vmName string, controllerNumber uint,
|
|||
return hyperv.SetBootDvdDrive(vmName, controllerNumber, controllerLocation, generation)
|
||||
}
|
||||
|
||||
func (d *HypervPS4Driver) SetFirstBootDevice(vmName string, controllerType string, controllerNumber uint,
|
||||
controllerLocation uint, generation uint) error {
|
||||
return hyperv.SetFirstBootDevice(vmName, controllerType, controllerNumber, controllerLocation, generation)
|
||||
}
|
||||
|
||||
func (d *HypervPS4Driver) UnmountDvdDrive(vmName string, controllerNumber uint, controllerLocation uint) error {
|
||||
return hyperv.UnmountDvdDrive(vmName, controllerNumber, controllerLocation)
|
||||
}
|
||||
|
|
|
@ -12,7 +12,8 @@ import (
|
|||
)
|
||||
|
||||
type StepMountDvdDrive struct {
|
||||
Generation uint
|
||||
Generation uint
|
||||
FirstBootDevice string
|
||||
}
|
||||
|
||||
func (s *StepMountDvdDrive) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
||||
|
@ -57,13 +58,24 @@ 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)
|
||||
if err != nil {
|
||||
err := fmt.Errorf(errorMsg, err)
|
||||
state.Put("error", err)
|
||||
ui.Error(err.Error())
|
||||
return multistep.ActionHalt
|
||||
// the "first_boot_device" setting has precedence over the legacy boot order
|
||||
// configuration, but only if its been assigned a value.
|
||||
|
||||
if s.FirstBootDevice == "" {
|
||||
|
||||
if s.Generation > 1 {
|
||||
// only print this message for Gen2, it's not a true statement for Gen1 VMs
|
||||
ui.Say(fmt.Sprintf("Setting boot drive to os dvd drive %s ...", isoPath))
|
||||
}
|
||||
|
||||
err = driver.SetBootDvdDrive(vmName, controllerNumber, controllerLocation, s.Generation)
|
||||
if err != nil {
|
||||
err := fmt.Errorf(errorMsg, err)
|
||||
state.Put("error", err)
|
||||
ui.Error(err.Error())
|
||||
return multistep.ActionHalt
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
ui.Say(fmt.Sprintf("Mounting os dvd drive %s ...", isoPath))
|
||||
|
|
|
@ -0,0 +1,160 @@
|
|||
package common
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/hashicorp/packer/helper/multistep"
|
||||
"github.com/hashicorp/packer/packer"
|
||||
)
|
||||
|
||||
type StepSetFirstBootDevice struct {
|
||||
Generation uint
|
||||
FirstBootDevice string
|
||||
}
|
||||
|
||||
func ParseBootDeviceIdentifier(deviceIdentifier string, generation uint) (string, uint, uint, error) {
|
||||
|
||||
// all input strings are forced to upperCase for comparison, I believe this is
|
||||
// safe as all of our values are 7bit ASCII clean.
|
||||
|
||||
lookupDeviceIdentifier := strings.ToUpper(deviceIdentifier)
|
||||
|
||||
if generation == 1 {
|
||||
|
||||
// Gen1 values are a simple set of if/then/else values, which we coalesce into a map
|
||||
// here for simplicity
|
||||
|
||||
lookupTable := map[string]string{
|
||||
"FLOPPY": "FLOPPY",
|
||||
"IDE": "IDE",
|
||||
"NET": "NET",
|
||||
"CD": "CD",
|
||||
"DVD": "CD",
|
||||
}
|
||||
|
||||
controllerType, isDefined := lookupTable[lookupDeviceIdentifier]
|
||||
if !isDefined {
|
||||
|
||||
return "", 0, 0, fmt.Errorf("The value %q is not a properly formatted device group identifier.", deviceIdentifier)
|
||||
|
||||
}
|
||||
|
||||
// success
|
||||
return controllerType, 0, 0, nil
|
||||
}
|
||||
|
||||
// everything else is treated as generation 2... the first set of lookups covers
|
||||
// the simple options..
|
||||
|
||||
lookupTable := map[string]string{
|
||||
"CD": "CD",
|
||||
"DVD": "CD",
|
||||
"NET": "NET",
|
||||
}
|
||||
|
||||
controllerType, isDefined := lookupTable[lookupDeviceIdentifier]
|
||||
if isDefined {
|
||||
|
||||
// these types do not require controllerNumber or controllerLocation
|
||||
return controllerType, 0, 0, nil
|
||||
|
||||
}
|
||||
|
||||
// not a simple option, check for a controllerType:controllerNumber:controllerLocation formatted
|
||||
// device..
|
||||
|
||||
r, err := regexp.Compile(`^(IDE|SCSI):(\d+):(\d+)$`)
|
||||
if err != nil {
|
||||
return "", 0, 0, err
|
||||
}
|
||||
|
||||
controllerMatch := r.FindStringSubmatch(lookupDeviceIdentifier)
|
||||
if controllerMatch != nil {
|
||||
|
||||
var controllerLocation int64
|
||||
var controllerNumber int64
|
||||
|
||||
// NOTE: controllerNumber and controllerLocation cannot be negative, the regex expression
|
||||
// would not have matched if either number was signed
|
||||
|
||||
controllerNumber, err = strconv.ParseInt(controllerMatch[2], 10, 8)
|
||||
if err == nil {
|
||||
|
||||
controllerLocation, err = strconv.ParseInt(controllerMatch[3], 10, 8)
|
||||
if err == nil {
|
||||
|
||||
return controllerMatch[1], uint(controllerNumber), uint(controllerLocation), nil
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return "", 0, 0, err
|
||||
|
||||
}
|
||||
|
||||
return "", 0, 0, fmt.Errorf("The value %q is not a properly formatted device identifier.", deviceIdentifier)
|
||||
}
|
||||
|
||||
func (s *StepSetFirstBootDevice) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
||||
|
||||
driver := state.Get("driver").(Driver)
|
||||
ui := state.Get("ui").(packer.Ui)
|
||||
vmName := state.Get("vmName").(string)
|
||||
|
||||
if s.FirstBootDevice != "" {
|
||||
|
||||
controllerType, controllerNumber, controllerLocation, err := ParseBootDeviceIdentifier(s.FirstBootDevice, s.Generation)
|
||||
if err == nil {
|
||||
|
||||
switch {
|
||||
|
||||
case controllerType == "CD":
|
||||
{
|
||||
// the "DVD" controller is special, we only apply the setting if we actually mounted
|
||||
// an ISO and only if that was mounted as the "IsoUrl" not a secondary ISO.
|
||||
|
||||
dvdControllerState := state.Get("os.dvd.properties")
|
||||
if dvdControllerState == nil {
|
||||
|
||||
ui.Say("First Boot Device is DVD, but no primary ISO mounted. Ignoring.")
|
||||
return multistep.ActionContinue
|
||||
|
||||
}
|
||||
|
||||
ui.Say(fmt.Sprintf("Setting boot device to %q", s.FirstBootDevice))
|
||||
dvdController := dvdControllerState.(DvdControllerProperties)
|
||||
err = driver.SetFirstBootDevice(vmName, controllerType, dvdController.ControllerNumber, dvdController.ControllerLocation, s.Generation)
|
||||
|
||||
}
|
||||
|
||||
default:
|
||||
{
|
||||
// anything else, we just pass as is..
|
||||
ui.Say(fmt.Sprintf("Setting boot device to %q", s.FirstBootDevice))
|
||||
err = driver.SetFirstBootDevice(vmName, controllerType, controllerNumber, controllerLocation, s.Generation)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
err := fmt.Errorf("Error setting first boot device: %s", err)
|
||||
state.Put("error", err)
|
||||
ui.Error(err.Error())
|
||||
return multistep.ActionHalt
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return multistep.ActionContinue
|
||||
}
|
||||
|
||||
func (s *StepSetFirstBootDevice) Cleanup(state multistep.StateBag) {
|
||||
// do nothing
|
||||
}
|
|
@ -0,0 +1,170 @@
|
|||
package common
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/hashicorp/packer/helper/multistep"
|
||||
)
|
||||
|
||||
type parseBootDeviceIdentifierTest struct {
|
||||
generation uint
|
||||
deviceIdentifier string
|
||||
controllerType string
|
||||
controllerNumber uint
|
||||
controllerLocation uint
|
||||
failInParse bool // true if ParseBootDeviceIdentifier should return an error
|
||||
haltStep bool // true if Step.Run should return Halt action
|
||||
shouldCallSet bool // true if driver.SetFirstBootDevice should have been called
|
||||
setDvdProps bool // true to set DvdDeviceProperties state
|
||||
}
|
||||
|
||||
var parseIdentifierTests = [...]parseBootDeviceIdentifierTest{
|
||||
{1, "IDE", "IDE", 0, 0, false, false, true, false},
|
||||
{1, "idE", "IDE", 0, 0, false, false, true, false},
|
||||
{1, "CD", "CD", 0, 0, false, false, false, false},
|
||||
{1, "CD", "CD", 0, 0, false, false, true, true},
|
||||
{1, "cD", "CD", 0, 0, false, false, false, false},
|
||||
{1, "DVD", "CD", 0, 0, false, false, false, false},
|
||||
{1, "DVD", "CD", 0, 0, false, false, true, true},
|
||||
{1, "Dvd", "CD", 0, 0, false, false, false, false},
|
||||
{1, "FLOPPY", "FLOPPY", 0, 0, false, false, true, false},
|
||||
{1, "FloppY", "FLOPPY", 0, 0, false, false, true, false},
|
||||
{1, "NET", "NET", 0, 0, false, false, true, false},
|
||||
{1, "net", "NET", 0, 0, false, false, true, false},
|
||||
{1, "", "", 0, 0, true, false, false, false},
|
||||
{1, "bad", "", 0, 0, true, true, false, false},
|
||||
{1, "IDE:0:0", "", 0, 0, true, true, true, false},
|
||||
{1, "SCSI:0:0", "", 0, 0, true, true, true, false},
|
||||
{2, "IDE", "", 0, 0, true, true, true, false},
|
||||
{2, "idE", "", 0, 0, true, true, true, false},
|
||||
{2, "CD", "CD", 0, 0, false, false, false, false},
|
||||
{2, "CD", "CD", 0, 0, false, false, true, true},
|
||||
{2, "cD", "CD", 0, 0, false, false, false, false},
|
||||
{2, "DVD", "CD", 0, 0, false, false, false, false},
|
||||
{2, "DVD", "CD", 0, 0, false, false, true, true},
|
||||
{2, "Dvd", "CD", 0, 0, false, false, false, false},
|
||||
{2, "FLOPPY", "", 0, 0, true, true, true, false},
|
||||
{2, "FloppY", "", 0, 0, true, true, true, false},
|
||||
{2, "NET", "NET", 0, 0, false, false, true, false},
|
||||
{2, "net", "NET", 0, 0, false, false, true, false},
|
||||
{2, "", "", 0, 0, true, false, false, false},
|
||||
{2, "bad", "", 0, 0, true, true, false, false},
|
||||
{2, "IDE:0:0", "IDE", 0, 0, false, false, true, false},
|
||||
{2, "SCSI:0:0", "SCSI", 0, 0, false, false, true, false},
|
||||
{2, "Ide:0:0", "IDE", 0, 0, false, false, true, false},
|
||||
{2, "sCsI:0:0", "SCSI", 0, 0, false, false, true, false},
|
||||
{2, "IDEscsi:0:0", "", 0, 0, true, true, false, false},
|
||||
{2, "SCSIide:0:0", "", 0, 0, true, true, false, false},
|
||||
{2, "IDE:0", "", 0, 0, true, true, false, false},
|
||||
{2, "SCSI:0", "", 0, 0, true, true, false, false},
|
||||
{2, "IDE:0:a", "", 0, 0, true, true, false, false},
|
||||
{2, "SCSI:0:a", "", 0, 0, true, true, false, false},
|
||||
{2, "IDE:0:653", "", 0, 0, true, true, false, false},
|
||||
{2, "SCSI:-10:0", "", 0, 0, true, true, false, false},
|
||||
}
|
||||
|
||||
func TestStepSetFirstBootDevice_impl(t *testing.T) {
|
||||
var _ multistep.Step = new(StepSetFirstBootDevice)
|
||||
}
|
||||
|
||||
func TestStepSetFirstBootDevice_ParseIdentifier(t *testing.T) {
|
||||
|
||||
for _, identifierTest := range parseIdentifierTests {
|
||||
|
||||
controllerType, controllerNumber, controllerLocation, err := ParseBootDeviceIdentifier(
|
||||
identifierTest.deviceIdentifier,
|
||||
identifierTest.generation)
|
||||
|
||||
if (err != nil) != identifierTest.failInParse {
|
||||
|
||||
t.Fatalf("Test %q (gen %v): failInParse: %v but err: %v", identifierTest.deviceIdentifier,
|
||||
identifierTest.generation, identifierTest.failInParse, err)
|
||||
|
||||
}
|
||||
|
||||
switch {
|
||||
|
||||
case controllerType != identifierTest.controllerType:
|
||||
t.Fatalf("Test %q (gen %v): controllerType: %q != %q", identifierTest.deviceIdentifier, identifierTest.generation,
|
||||
identifierTest.controllerType, controllerType)
|
||||
|
||||
case controllerNumber != identifierTest.controllerNumber:
|
||||
t.Fatalf("Test %q (gen %v): controllerNumber: %v != %v", identifierTest.deviceIdentifier, identifierTest.generation,
|
||||
identifierTest.controllerNumber, controllerNumber)
|
||||
|
||||
case controllerLocation != identifierTest.controllerLocation:
|
||||
t.Fatalf("Test %q (gen %v): controllerLocation: %v != %v", identifierTest.deviceIdentifier, identifierTest.generation,
|
||||
identifierTest.controllerLocation, controllerLocation)
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestStepSetFirstBootDevice(t *testing.T) {
|
||||
|
||||
step := new(StepSetFirstBootDevice)
|
||||
|
||||
for _, identifierTest := range parseIdentifierTests {
|
||||
|
||||
state := testState(t)
|
||||
driver := state.Get("driver").(*DriverMock)
|
||||
|
||||
// requires the vmName state value
|
||||
vmName := "foo"
|
||||
state.Put("vmName", vmName)
|
||||
|
||||
// pretend that we mounted a DVD somewhere (CD:0:0)
|
||||
if identifierTest.setDvdProps {
|
||||
var dvdControllerProperties DvdControllerProperties
|
||||
dvdControllerProperties.ControllerNumber = 0
|
||||
dvdControllerProperties.ControllerLocation = 0
|
||||
dvdControllerProperties.Existing = false
|
||||
state.Put("os.dvd.properties", dvdControllerProperties)
|
||||
}
|
||||
|
||||
step.Generation = identifierTest.generation
|
||||
step.FirstBootDevice = identifierTest.deviceIdentifier
|
||||
|
||||
action := step.Run(context.Background(), state)
|
||||
if (action != multistep.ActionContinue) != identifierTest.haltStep {
|
||||
t.Fatalf("Test %q (gen %v): Bad action: %v", identifierTest.deviceIdentifier, identifierTest.generation, action)
|
||||
}
|
||||
|
||||
if identifierTest.haltStep {
|
||||
|
||||
if _, ok := state.GetOk("error"); !ok {
|
||||
t.Fatalf("Test %q (gen %v): Should have error", identifierTest.deviceIdentifier, identifierTest.generation)
|
||||
}
|
||||
|
||||
// don't perform the remaining checks..
|
||||
continue
|
||||
|
||||
} else {
|
||||
|
||||
if _, ok := state.GetOk("error"); ok {
|
||||
t.Fatalf("Test %q (gen %v): Should NOT have error", identifierTest.deviceIdentifier, identifierTest.generation)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if driver.SetFirstBootDevice_Called != identifierTest.shouldCallSet {
|
||||
if identifierTest.shouldCallSet {
|
||||
t.Fatalf("Test %q (gen %v): Should have called SetFirstBootDevice", identifierTest.deviceIdentifier, identifierTest.generation)
|
||||
}
|
||||
|
||||
t.Fatalf("Test %q (gen %v): Should NOT have called SetFirstBootDevice", identifierTest.deviceIdentifier, identifierTest.generation)
|
||||
}
|
||||
|
||||
if (driver.SetFirstBootDevice_Called) &&
|
||||
((driver.SetFirstBootDevice_VmName != vmName) ||
|
||||
(driver.SetFirstBootDevice_ControllerType != identifierTest.controllerType) ||
|
||||
(driver.SetFirstBootDevice_ControllerNumber != identifierTest.controllerNumber) ||
|
||||
(driver.SetFirstBootDevice_ControllerLocation != identifierTest.controllerLocation) ||
|
||||
(driver.SetFirstBootDevice_Generation != identifierTest.generation)) {
|
||||
|
||||
t.Fatalf("Test %q (gen %v): Called SetFirstBootDevice with unexpected arguments.", identifierTest.deviceIdentifier, identifierTest.generation)
|
||||
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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,
|
||||
FirstBootDevice: b.config.FirstBootDevice,
|
||||
},
|
||||
&hypervcommon.StepMountFloppydrive{
|
||||
Generation: b.config.Generation,
|
||||
|
@ -264,6 +265,11 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack
|
|||
SwitchVlanId: b.config.SwitchVlanId,
|
||||
},
|
||||
|
||||
&hypervcommon.StepSetFirstBootDevice{
|
||||
Generation: b.config.Generation,
|
||||
FirstBootDevice: b.config.FirstBootDevice,
|
||||
},
|
||||
|
||||
&hypervcommon.StepRun{
|
||||
Headless: b.config.Headless,
|
||||
SwitchName: b.config.SwitchName,
|
||||
|
|
|
@ -97,6 +97,7 @@ type FlatConfig struct {
|
|||
SkipCompaction *bool `mapstructure:"skip_compaction" required:"false" cty:"skip_compaction"`
|
||||
SkipExport *bool `mapstructure:"skip_export" required:"false" cty:"skip_export"`
|
||||
Headless *bool `mapstructure:"headless" required:"false" cty:"headless"`
|
||||
FirstBootDevice *string `mapstructure:"first_boot_device" required:"false" cty:"first_boot_device"`
|
||||
ShutdownCommand *string `mapstructure:"shutdown_command" required:"false" cty:"shutdown_command"`
|
||||
ShutdownTimeout *string `mapstructure:"shutdown_timeout" required:"false" cty:"shutdown_timeout"`
|
||||
DiskSize *uint `mapstructure:"disk_size" required:"false" cty:"disk_size"`
|
||||
|
@ -205,6 +206,7 @@ func (*FlatConfig) HCL2Spec() map[string]hcldec.Spec {
|
|||
"skip_compaction": &hcldec.AttrSpec{Name: "skip_compaction", Type: cty.Bool, Required: false},
|
||||
"skip_export": &hcldec.AttrSpec{Name: "skip_export", Type: cty.Bool, Required: false},
|
||||
"headless": &hcldec.AttrSpec{Name: "headless", Type: cty.Bool, Required: false},
|
||||
"first_boot_device": &hcldec.AttrSpec{Name: "first_boot_device", Type: cty.String, Required: false},
|
||||
"shutdown_command": &hcldec.AttrSpec{Name: "shutdown_command", Type: cty.String, Required: false},
|
||||
"shutdown_timeout": &hcldec.AttrSpec{Name: "shutdown_timeout", Type: cty.String, Required: false},
|
||||
"disk_size": &hcldec.AttrSpec{Name: "disk_size", Type: cty.Number, Required: false},
|
||||
|
|
|
@ -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,
|
||||
FirstBootDevice: b.config.FirstBootDevice,
|
||||
},
|
||||
&hypervcommon.StepMountFloppydrive{
|
||||
Generation: b.config.Generation,
|
||||
|
@ -304,6 +305,11 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack
|
|||
SwitchVlanId: b.config.SwitchVlanId,
|
||||
},
|
||||
|
||||
&hypervcommon.StepSetFirstBootDevice{
|
||||
Generation: b.config.Generation,
|
||||
FirstBootDevice: b.config.FirstBootDevice,
|
||||
},
|
||||
|
||||
&hypervcommon.StepRun{
|
||||
Headless: b.config.Headless,
|
||||
SwitchName: b.config.SwitchName,
|
||||
|
|
|
@ -97,6 +97,7 @@ type FlatConfig struct {
|
|||
SkipCompaction *bool `mapstructure:"skip_compaction" required:"false" cty:"skip_compaction"`
|
||||
SkipExport *bool `mapstructure:"skip_export" required:"false" cty:"skip_export"`
|
||||
Headless *bool `mapstructure:"headless" required:"false" cty:"headless"`
|
||||
FirstBootDevice *string `mapstructure:"first_boot_device" required:"false" cty:"first_boot_device"`
|
||||
ShutdownCommand *string `mapstructure:"shutdown_command" required:"false" cty:"shutdown_command"`
|
||||
ShutdownTimeout *string `mapstructure:"shutdown_timeout" required:"false" cty:"shutdown_timeout"`
|
||||
CloneFromVMCXPath *string `mapstructure:"clone_from_vmcx_path" cty:"clone_from_vmcx_path"`
|
||||
|
@ -207,6 +208,7 @@ func (*FlatConfig) HCL2Spec() map[string]hcldec.Spec {
|
|||
"skip_compaction": &hcldec.AttrSpec{Name: "skip_compaction", Type: cty.Bool, Required: false},
|
||||
"skip_export": &hcldec.AttrSpec{Name: "skip_export", Type: cty.Bool, Required: false},
|
||||
"headless": &hcldec.AttrSpec{Name: "headless", Type: cty.Bool, Required: false},
|
||||
"first_boot_device": &hcldec.AttrSpec{Name: "first_boot_device", Type: cty.String, Required: false},
|
||||
"shutdown_command": &hcldec.AttrSpec{Name: "shutdown_command", Type: cty.String, Required: false},
|
||||
"shutdown_timeout": &hcldec.AttrSpec{Name: "shutdown_timeout", Type: cty.String, Required: false},
|
||||
"clone_from_vmcx_path": &hcldec.AttrSpec{Name: "clone_from_vmcx_path", Type: cty.String, Required: false},
|
||||
|
|
|
@ -180,6 +180,75 @@ Hyper-V\Set-VMFirmware -VMName $vmName -FirstBootDevice $vmDvdDrive -ErrorAction
|
|||
}
|
||||
}
|
||||
|
||||
func SetFirstBootDeviceGen1(vmName string, controllerType string) error {
|
||||
|
||||
// for Generation 1 VMs, we read the value of the VM's boot order, strip the value specified in
|
||||
// controllerType and insert that value back at the beginning of the list.
|
||||
//
|
||||
// controllerType must be 'NET', 'DVD', 'IDE' or 'FLOPPY' (case sensitive)
|
||||
// The 'NET' value is always replaced with 'LegacyNetworkAdapter'
|
||||
|
||||
if controllerType == "NET" {
|
||||
controllerType = "LegacyNetworkAdapter"
|
||||
}
|
||||
|
||||
script := `
|
||||
param([string] $vmName, [string] $controllerType)
|
||||
$vmBootOrder = Hyper-V\Get-VMBios -VMName $vmName | Select-Object -ExpandProperty StartupOrder | Where-Object { $_ -ne $controllerType }
|
||||
Hyper-V\Set-VMBios -VMName $vmName -StartupOrder (@($controllerType) + $vmBootOrder)
|
||||
`
|
||||
|
||||
var ps powershell.PowerShellCmd
|
||||
err := ps.Run(script, vmName, controllerType)
|
||||
return err
|
||||
}
|
||||
|
||||
func SetFirstBootDeviceGen2(vmName string, controllerType string, controllerNumber uint, controllerLocation uint) error {
|
||||
|
||||
script := `param ([string] $vmName, [string] $controllerType, [int] $controllerNumber, [int] $controllerLocation)`
|
||||
|
||||
switch {
|
||||
|
||||
case controllerType == "CD":
|
||||
// for CDs we have to use Get-VMDvdDrive to find the device
|
||||
script += `
|
||||
$vmDevice = Hyper-V\Get-VMDvdDrive -VMName $vmName -ControllerNumber $controllerNumber -ControllerLocation $controllerLocation -ErrorAction SilentlyContinue`
|
||||
|
||||
case controllerType == "NET":
|
||||
// for "NET" device, we select the first network adapter on the VM
|
||||
script += `
|
||||
$vmDevice = Hyper-V\Get-VMNetworkAdapter -VMName $vmName -ErrorAction SilentlyContinue | Select-Object -First 1`
|
||||
|
||||
default:
|
||||
script += `
|
||||
$vmDevice = @(Hyper-V\Get-VMIdeController -VMName $vmName -ErrorAction SilentlyContinue) +
|
||||
@(Hyper-V\Get-VMScsiController -VMName $vmName -ErrorAction SilentlyContinue) |
|
||||
Select-Object -ExpandProperty Drives |
|
||||
Where-Object { $_.ControllerType -eq $controllerType } |
|
||||
Where-Object { ($_.ControllerNumber -eq $controllerNumber) -and ($_.ControllerLocation -eq $controllerLocation) }
|
||||
`
|
||||
|
||||
}
|
||||
|
||||
script += `
|
||||
if ($vmDevice -eq $null) { throw 'unable to find boot device' }
|
||||
Hyper-V\Set-VMFirmware -VMName $vmName -FirstBootDevice $vmDevice
|
||||
`
|
||||
|
||||
var ps powershell.PowerShellCmd
|
||||
err := ps.Run(script, vmName, controllerType, strconv.FormatInt(int64(controllerNumber), 10), strconv.FormatInt(int64(controllerLocation), 10))
|
||||
return err
|
||||
}
|
||||
|
||||
func SetFirstBootDevice(vmName string, controllerType string, controllerNumber uint, controllerLocation uint, generation uint) error {
|
||||
|
||||
if generation == 1 {
|
||||
return SetFirstBootDeviceGen1(vmName, controllerType)
|
||||
} else {
|
||||
return SetFirstBootDeviceGen2(vmName, controllerType, controllerNumber, controllerLocation)
|
||||
}
|
||||
}
|
||||
|
||||
func DeleteDvdDrive(vmName string, controllerNumber uint, controllerLocation uint) error {
|
||||
var script = `
|
||||
param([string]$vmName,[int]$controllerNumber,[int]$controllerLocation)
|
||||
|
|
|
@ -109,4 +109,19 @@
|
|||
machines by launching a GUI that shows the console of the machine being
|
||||
built. When this value is set to true, the machine will start without a
|
||||
console.
|
||||
|
||||
- `first_boot_device` (string) - When configured, determines the device or device type that is given preferential
|
||||
treatment when choosing a boot device.
|
||||
|
||||
For Generation 1:
|
||||
- `IDE`
|
||||
- `CD` *or* `DVD`
|
||||
- `Floppy`
|
||||
- `NET`
|
||||
|
||||
For Generation 2:
|
||||
- `IDE:x:y`
|
||||
- `SCSI:x:y`
|
||||
- `CD` *or* `DVD`
|
||||
- `NET`
|
||||
|
Loading…
Reference in New Issue