mirror of https://github.com/hashicorp/packer
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.pull/8824/head
commit
9c9826ee4b
@ -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)
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in new issue