With generation 2 machine by default a dvd drive is not created. So create a dvd drive for os if it does not exist.
Allow secure boot mode to be configured from config.
This commit is contained in:
parent
aa1f1da1ff
commit
02db768018
|
@ -23,6 +23,7 @@ type StepCreateVM struct {
|
||||||
DiskSize uint
|
DiskSize uint
|
||||||
Generation uint
|
Generation uint
|
||||||
Cpu uint
|
Cpu uint
|
||||||
|
EnabeSecureBoot bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *StepCreateVM) Run(state multistep.StateBag) multistep.StepAction {
|
func (s *StepCreateVM) Run(state multistep.StateBag) multistep.StepAction {
|
||||||
|
@ -40,6 +41,7 @@ func (s *StepCreateVM) Run(state multistep.StateBag) multistep.StepAction {
|
||||||
switchName := s.SwitchName
|
switchName := s.SwitchName
|
||||||
generation := strconv.FormatInt(int64(s.Generation), 10)
|
generation := strconv.FormatInt(int64(s.Generation), 10)
|
||||||
cpu := strconv.FormatInt(int64(s.Cpu), 10)
|
cpu := strconv.FormatInt(int64(s.Cpu), 10)
|
||||||
|
enabeSecureBoot := s.EnabeSecureBoot
|
||||||
|
|
||||||
err := hyperv.CreateVirtualMachine(s.VMName, path, ram, diskSize, switchName, generation)
|
err := hyperv.CreateVirtualMachine(s.VMName, path, ram, diskSize, switchName, generation)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -57,6 +59,16 @@ func (s *StepCreateVM) Run(state multistep.StateBag) multistep.StepAction {
|
||||||
return multistep.ActionHalt
|
return multistep.ActionHalt
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if generation == "2" {
|
||||||
|
err = hyperv.SetSecureBoot(s.VMName, enabeSecureBoot)
|
||||||
|
if err != nil {
|
||||||
|
err := fmt.Errorf("Error setting secure boot: %s", err)
|
||||||
|
state.Put("error", err)
|
||||||
|
ui.Error(err.Error())
|
||||||
|
return multistep.ActionHalt
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Set the final name in the state bag so others can use it
|
// Set the final name in the state bag so others can use it
|
||||||
state.Put("vmName", s.VMName)
|
state.Put("vmName", s.VMName)
|
||||||
|
|
||||||
|
|
|
@ -8,10 +8,10 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/mitchellh/multistep"
|
"github.com/mitchellh/multistep"
|
||||||
"github.com/mitchellh/packer/packer"
|
"github.com/mitchellh/packer/packer"
|
||||||
|
powershell "github.com/mitchellh/packer/powershell"
|
||||||
"github.com/mitchellh/packer/powershell/hyperv"
|
"github.com/mitchellh/packer/powershell/hyperv"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
type StepMountDvdDrive struct {
|
type StepMountDvdDrive struct {
|
||||||
RawSingleISOUrl string
|
RawSingleISOUrl string
|
||||||
path string
|
path string
|
||||||
|
@ -25,9 +25,36 @@ func (s *StepMountDvdDrive) Run(state multistep.StateBag) multistep.StepAction {
|
||||||
vmName := state.Get("vmName").(string)
|
vmName := state.Get("vmName").(string)
|
||||||
isoPath := s.RawSingleISOUrl
|
isoPath := s.RawSingleISOUrl
|
||||||
|
|
||||||
|
// Check that there is a virtual dvd drive
|
||||||
|
var script powershell.ScriptBuilder
|
||||||
|
powershell := new(powershell.PowerShellCmd)
|
||||||
|
|
||||||
|
script.Reset()
|
||||||
|
script.WriteLine("param([string]$vmName)")
|
||||||
|
script.WriteLine("(Get-VMDvdDrive -VMName $vmName).ControllerNumber")
|
||||||
|
controllerNumber, err := powershell.Output(script.String(), vmName)
|
||||||
|
if err != nil {
|
||||||
|
state.Put("error", err)
|
||||||
|
ui.Error(err.Error())
|
||||||
|
return multistep.ActionHalt
|
||||||
|
}
|
||||||
|
|
||||||
|
if controllerNumber == "" {
|
||||||
|
// Add a virtual dvd drive as there is none
|
||||||
|
script.Reset()
|
||||||
|
script.WriteLine("param([string]$vmName)")
|
||||||
|
script.WriteLine("Add-VMDvdDrive -VMName $vmName")
|
||||||
|
err = powershell.Run(script.String(), vmName)
|
||||||
|
if err != nil {
|
||||||
|
state.Put("error", err)
|
||||||
|
ui.Error(err.Error())
|
||||||
|
return multistep.ActionHalt
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
ui.Say("Mounting dvd drive...")
|
ui.Say("Mounting dvd drive...")
|
||||||
|
|
||||||
err := hyperv.MountDvdDrive(vmName, isoPath)
|
err = hyperv.MountDvdDrive(vmName, isoPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
err := fmt.Errorf(errorMsg, err)
|
err := fmt.Errorf(errorMsg, err)
|
||||||
state.Put("error", err)
|
state.Put("error", err)
|
||||||
|
|
|
@ -99,6 +99,7 @@ type Config struct {
|
||||||
SwitchName string `mapstructure:"switch_name"`
|
SwitchName string `mapstructure:"switch_name"`
|
||||||
Cpu uint `mapstructure:"cpu"`
|
Cpu uint `mapstructure:"cpu"`
|
||||||
Generation uint `mapstructure:"generation"`
|
Generation uint `mapstructure:"generation"`
|
||||||
|
EnableSecureBoot bool `mapstructure:"enable_secure_boot"`
|
||||||
|
|
||||||
Communicator string `mapstructure:"communicator"`
|
Communicator string `mapstructure:"communicator"`
|
||||||
|
|
||||||
|
@ -291,6 +292,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
|
||||||
DiskSize: b.config.DiskSize,
|
DiskSize: b.config.DiskSize,
|
||||||
Generation: b.config.Generation,
|
Generation: b.config.Generation,
|
||||||
Cpu: b.config.Cpu,
|
Cpu: b.config.Cpu,
|
||||||
|
EnabeSecureBoot: b.config.EnableSecureBoot,
|
||||||
},
|
},
|
||||||
&hypervcommon.StepEnableIntegrationService{},
|
&hypervcommon.StepEnableIntegrationService{},
|
||||||
|
|
||||||
|
|
|
@ -98,16 +98,27 @@ Set-VMFloppyDiskDrive -VMName $vmName -Path $null
|
||||||
|
|
||||||
func CreateVirtualMachine(vmName string, path string, ram string, diskSize string, switchName string, generation string) error {
|
func CreateVirtualMachine(vmName string, path string, ram string, diskSize string, switchName string, generation string) error {
|
||||||
|
|
||||||
|
if generation == "2" {
|
||||||
var script = `
|
var script = `
|
||||||
param([string]$vmName, [string]$path, [long]$memoryStartupBytes, [long]$newVHDSizeBytes, [string]$switchName, [int]$generation)
|
param([string]$vmName, [string]$path, [long]$memoryStartupBytes, [long]$newVHDSizeBytes, [string]$switchName, [int]$generation)
|
||||||
$vhdx = $vmName + '.vhdx'
|
$vhdx = $vmName + '.vhdx'
|
||||||
$vhdPath = Join-Path -Path $path -ChildPath $vhdx
|
$vhdPath = Join-Path -Path $path -ChildPath $vhdx
|
||||||
New-VM -Name $vmName -Path $path -MemoryStartupBytes $memoryStartupBytes -NewVHDPath $vhdPath -NewVHDSizeBytes $newVHDSizeBytes -SwitchName $switchName -Generation $generation
|
New-VM -Name $vmName -Path $path -MemoryStartupBytes $memoryStartupBytes -NewVHDPath $vhdPath -NewVHDSizeBytes $newVHDSizeBytes -SwitchName $switchName -Generation $generation
|
||||||
`
|
`
|
||||||
|
|
||||||
var ps powershell.PowerShellCmd
|
var ps powershell.PowerShellCmd
|
||||||
err := ps.Run(script, vmName, path, ram, diskSize, switchName, generation)
|
err := ps.Run(script, vmName, path, ram, diskSize, switchName, generation)
|
||||||
return err
|
return err
|
||||||
|
} else {
|
||||||
|
var script = `
|
||||||
|
param([string]$vmName, [string]$path, [long]$memoryStartupBytes, [long]$newVHDSizeBytes, [string]$switchName)
|
||||||
|
$vhdx = $vmName + '.vhdx'
|
||||||
|
$vhdPath = Join-Path -Path $path -ChildPath $vhdx
|
||||||
|
New-VM -Name $vmName -Path $path -MemoryStartupBytes $memoryStartupBytes -NewVHDPath $vhdPath -NewVHDSizeBytes $newVHDSizeBytes -SwitchName $switchName
|
||||||
|
`
|
||||||
|
var ps powershell.PowerShellCmd
|
||||||
|
err := ps.Run(script, vmName, path, ram, diskSize, switchName)
|
||||||
|
return err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func SetVirtualMachineCpu(vmName string, cpu string) error {
|
func SetVirtualMachineCpu(vmName string, cpu string) error {
|
||||||
|
@ -122,6 +133,23 @@ Set-VMProcessor -VMName $vmName -Count $cpu
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func SetSecureBoot(vmName string, enable bool) error {
|
||||||
|
var script = `
|
||||||
|
param([string]$vmName, $enableSecureBoot)
|
||||||
|
Set-VMFirmware -VMName $vmName -EnableSecureBoot $enableSecureBoot
|
||||||
|
`
|
||||||
|
|
||||||
|
var ps powershell.PowerShellCmd
|
||||||
|
|
||||||
|
enableSecureBoot := "Off"
|
||||||
|
if enable {
|
||||||
|
enableSecureBoot = "On"
|
||||||
|
}
|
||||||
|
|
||||||
|
err := ps.Run(script, vmName, enableSecureBoot)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
func DeleteVirtualMachine(vmName string) error {
|
func DeleteVirtualMachine(vmName string) error {
|
||||||
|
|
||||||
var script = `
|
var script = `
|
||||||
|
@ -635,8 +663,6 @@ param([string]$vmName, [string]$scanCodes)
|
||||||
$vmConsole.TypeScancodes($_)
|
$vmConsole.TypeScancodes($_)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
`
|
`
|
||||||
|
|
||||||
var ps powershell.PowerShellCmd
|
var ps powershell.PowerShellCmd
|
||||||
|
|
Loading…
Reference in New Issue