Enable use of separate temp path for Hyper-V VHD

This commit is contained in:
Sander Saares 2017-08-01 12:23:19 +03:00
parent 3974976518
commit 028c941b77
7 changed files with 39 additions and 14 deletions

1
.gitignore vendored
View File

@ -22,3 +22,4 @@ packer-test*.log
.idea/ .idea/
*.iml *.iml
Thumbs.db Thumbs.db
/packer.exe

View File

@ -64,7 +64,7 @@ type Driver interface {
DeleteVirtualSwitch(string) error DeleteVirtualSwitch(string) error
CreateVirtualMachine(string, string, int64, int64, string, uint) error CreateVirtualMachine(string, string, string, int64, int64, string, uint) error
DeleteVirtualMachine(string) error DeleteVirtualMachine(string) error

View File

@ -166,8 +166,8 @@ func (d *HypervPS4Driver) CreateVirtualSwitch(switchName string, switchType stri
return hyperv.CreateVirtualSwitch(switchName, switchType) return hyperv.CreateVirtualSwitch(switchName, switchType)
} }
func (d *HypervPS4Driver) CreateVirtualMachine(vmName string, path string, ram int64, diskSize int64, switchName string, generation uint) error { func (d *HypervPS4Driver) CreateVirtualMachine(vmName string, path string, vhdPath string, ram int64, diskSize int64, switchName string, generation uint) error {
return hyperv.CreateVirtualMachine(vmName, path, ram, diskSize, switchName, generation) return hyperv.CreateVirtualMachine(vmName, path, vhdPath, ram, diskSize, switchName, generation)
} }
func (d *HypervPS4Driver) DeleteVirtualMachine(vmName string) error { func (d *HypervPS4Driver) DeleteVirtualMachine(vmName string) error {

View File

@ -11,6 +11,7 @@ import (
type StepCreateTempDir struct { type StepCreateTempDir struct {
TempPath string TempPath string
VhdTempPath string
dirPath string dirPath string
} }
@ -34,6 +35,22 @@ func (s *StepCreateTempDir) Run(state multistep.StateBag) multistep.StepAction {
s.dirPath = packerTempDir s.dirPath = packerTempDir
state.Put("packerTempDir", packerTempDir) state.Put("packerTempDir", packerTempDir)
if s.VhdTempPath == "" {
// Fall back to regular temp dir if no separate VHD temp dir set.
state.Put("packerVhdTempDir", packerTempDir)
} else {
packerVhdTempDir, err := ioutil.TempDir(s.VhdTempPath, "packerhv-vhd")
if err != nil {
err := fmt.Errorf("Error creating temporary VHD directory: %s", err)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
s.dirPath = packerVhdTempDir
state.Put("packerVhdTempDir", packerVhdTempDir)
}
// ui.Say("packerTempDir = '" + packerTempDir + "'") // ui.Say("packerTempDir = '" + packerTempDir + "'")
return multistep.ActionContinue return multistep.ActionContinue

View File

@ -30,12 +30,13 @@ func (s *StepCreateVM) Run(state multistep.StateBag) multistep.StepAction {
ui.Say("Creating virtual machine...") ui.Say("Creating virtual machine...")
path := state.Get("packerTempDir").(string) path := state.Get("packerTempDir").(string)
vhdPath := state.Get("packerVhdTempDir").(string)
// convert the MB to bytes // convert the MB to bytes
ramSize := int64(s.RamSize * 1024 * 1024) ramSize := int64(s.RamSize * 1024 * 1024)
diskSize := int64(s.DiskSize * 1024 * 1024) diskSize := int64(s.DiskSize * 1024 * 1024)
err := driver.CreateVirtualMachine(s.VMName, path, ramSize, diskSize, s.SwitchName, s.Generation) err := driver.CreateVirtualMachine(s.VMName, path, vhdPath, ramSize, diskSize, s.SwitchName, s.Generation)
if err != nil { if err != nil {
err := fmt.Errorf("Error creating virtual machine: %s", err) err := fmt.Errorf("Error creating virtual machine: %s", err)
state.Put("error", err) state.Put("error", err)

View File

@ -82,6 +82,11 @@ type Config struct {
EnableVirtualizationExtensions bool `mapstructure:"enable_virtualization_extensions"` EnableVirtualizationExtensions bool `mapstructure:"enable_virtualization_extensions"`
TempPath string `mapstructure:"temp_path"` TempPath string `mapstructure:"temp_path"`
// A separate path can be used for storing the VM's disk image. The purpose is to enable
// reading and writing to take place on different physical disks (read from VHD temp path
// write to regular temp path while exporting the VM) to eliminate a single-disk bottleneck.
VhdTempPath string `mapstructure:"vhd_temp_path"`
Communicator string `mapstructure:"communicator"` Communicator string `mapstructure:"communicator"`
SkipCompaction bool `mapstructure:"skip_compaction"` SkipCompaction bool `mapstructure:"skip_compaction"`
@ -297,6 +302,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
steps := []multistep.Step{ steps := []multistep.Step{
&hypervcommon.StepCreateTempDir{ &hypervcommon.StepCreateTempDir{
TempPath: b.config.TempPath, TempPath: b.config.TempPath,
VhdTempPath: b.config.VhdTempPath,
}, },
&hypervcommon.StepOutputDir{ &hypervcommon.StepOutputDir{
Force: b.config.PackerForce, Force: b.config.PackerForce,

View File

@ -187,27 +187,27 @@ Set-VMFloppyDiskDrive -VMName $vmName -Path $null
return err return err
} }
func CreateVirtualMachine(vmName string, path string, ram int64, diskSize int64, switchName string, generation uint) error { func CreateVirtualMachine(vmName string, path string, vhdRoot string, ram int64, diskSize int64, switchName string, generation uint) error {
if generation == 2 { 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, [string]$vhdRoot, [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 $vhdRoot -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, strconv.FormatInt(ram, 10), strconv.FormatInt(diskSize, 10), switchName, strconv.FormatInt(int64(generation), 10)) err := ps.Run(script, vmName, path, vhdRoot, strconv.FormatInt(ram, 10), strconv.FormatInt(diskSize, 10), switchName, strconv.FormatInt(int64(generation), 10))
return err return err
} else { } else {
var script = ` var script = `
param([string]$vmName, [string]$path, [long]$memoryStartupBytes, [long]$newVHDSizeBytes, [string]$switchName) param([string]$vmName, [string]$path, [string]$vhdRoot, [long]$memoryStartupBytes, [long]$newVHDSizeBytes, [string]$switchName)
$vhdx = $vmName + '.vhdx' $vhdx = $vmName + '.vhdx'
$vhdPath = Join-Path -Path $path -ChildPath $vhdx $vhdPath = Join-Path -Path $vhdRoot -ChildPath $vhdx
New-VM -Name $vmName -Path $path -MemoryStartupBytes $memoryStartupBytes -NewVHDPath $vhdPath -NewVHDSizeBytes $newVHDSizeBytes -SwitchName $switchName New-VM -Name $vmName -Path $path -MemoryStartupBytes $memoryStartupBytes -NewVHDPath $vhdPath -NewVHDSizeBytes $newVHDSizeBytes -SwitchName $switchName
` `
var ps powershell.PowerShellCmd var ps powershell.PowerShellCmd
err := ps.Run(script, vmName, path, strconv.FormatInt(ram, 10), strconv.FormatInt(diskSize, 10), switchName) err := ps.Run(script, vmName, path, vhdRoot, strconv.FormatInt(ram, 10), strconv.FormatInt(diskSize, 10), switchName)
if err != nil { if err != nil {
return err return err