From c2437ba592e11167054ffd2b391514e285346d03 Mon Sep 17 00:00:00 2001 From: DanHam Date: Sun, 8 Jul 2018 11:16:02 +0100 Subject: [PATCH] Remove the option to place the VHD files in a separate directory The export process now exports the VM directly from the build directory into the output directory. There are no intermediate steps or copying of files involved. This means that there is no longer any benefit in having a separate directory to house the VHD files - see #5206 for the reasoning behind the introduction of this feature. If a user wishes to house the build files on a separate disk from the output directory (perhaps for performance reasons or due to disk space limitations) they can still do so through the use of `temp_path`. --- builder/hyperv/common/driver.go | 2 +- builder/hyperv/common/driver_mock.go | 4 +- builder/hyperv/common/driver_ps_4.go | 4 +- builder/hyperv/common/step_create_tempdir.go | 59 ++++++-------------- builder/hyperv/common/step_create_vm.go | 6 +- builder/hyperv/iso/builder.go | 8 +-- common/powershell/hyperv/hyperv.go | 14 ++--- 7 files changed, 32 insertions(+), 65 deletions(-) diff --git a/builder/hyperv/common/driver.go b/builder/hyperv/common/driver.go index de3ba2651..c1bceed27 100644 --- a/builder/hyperv/common/driver.go +++ b/builder/hyperv/common/driver.go @@ -70,7 +70,7 @@ type Driver interface { DeleteVirtualSwitch(string) error - CreateVirtualMachine(string, string, string, string, int64, int64, int64, string, uint, bool, bool) error + CreateVirtualMachine(string, string, string, int64, int64, int64, string, uint, bool, bool) error AddVirtualMachineHardDrive(string, string, string, int64, int64, string) error diff --git a/builder/hyperv/common/driver_mock.go b/builder/hyperv/common/driver_mock.go index 8e5a52cbd..35dc5ced1 100644 --- a/builder/hyperv/common/driver_mock.go +++ b/builder/hyperv/common/driver_mock.go @@ -124,7 +124,6 @@ type DriverMock struct { CreateVirtualMachine_VmName string CreateVirtualMachine_Path string CreateVirtualMachine_HarddrivePath string - CreateVirtualMachine_VhdPath string CreateVirtualMachine_Ram int64 CreateVirtualMachine_DiskSize int64 CreateVirtualMachine_DiskBlockSize int64 @@ -402,12 +401,11 @@ func (d *DriverMock) AddVirtualMachineHardDrive(vmName string, vhdFile string, v return d.AddVirtualMachineHardDrive_Err } -func (d *DriverMock) CreateVirtualMachine(vmName string, path string, harddrivePath string, vhdPath string, ram int64, diskSize int64, diskBlockSize int64, switchName string, generation uint, diffDisks bool, fixedVHD bool) error { +func (d *DriverMock) CreateVirtualMachine(vmName string, path string, harddrivePath string, ram int64, diskSize int64, diskBlockSize int64, switchName string, generation uint, diffDisks bool, fixedVHD bool) error { d.CreateVirtualMachine_Called = true d.CreateVirtualMachine_VmName = vmName d.CreateVirtualMachine_Path = path d.CreateVirtualMachine_HarddrivePath = harddrivePath - d.CreateVirtualMachine_VhdPath = vhdPath d.CreateVirtualMachine_Ram = ram d.CreateVirtualMachine_DiskSize = diskSize d.CreateVirtualMachine_DiskBlockSize = diskBlockSize diff --git a/builder/hyperv/common/driver_ps_4.go b/builder/hyperv/common/driver_ps_4.go index acf399210..ff9ef1407 100644 --- a/builder/hyperv/common/driver_ps_4.go +++ b/builder/hyperv/common/driver_ps_4.go @@ -179,8 +179,8 @@ func (d *HypervPS4Driver) AddVirtualMachineHardDrive(vmName string, vhdFile stri return hyperv.AddVirtualMachineHardDiskDrive(vmName, vhdFile, vhdName, vhdSizeBytes, diskBlockSize, controllerType) } -func (d *HypervPS4Driver) CreateVirtualMachine(vmName string, path string, harddrivePath string, vhdPath string, ram int64, diskSize int64, diskBlockSize int64, switchName string, generation uint, diffDisks bool, fixedVHD bool) error { - return hyperv.CreateVirtualMachine(vmName, path, harddrivePath, vhdPath, ram, diskSize, diskBlockSize, switchName, generation, diffDisks, fixedVHD) +func (d *HypervPS4Driver) CreateVirtualMachine(vmName string, path string, harddrivePath string, ram int64, diskSize int64, diskBlockSize int64, switchName string, generation uint, diffDisks bool, fixedVHD bool) error { + return hyperv.CreateVirtualMachine(vmName, path, harddrivePath, ram, diskSize, diskBlockSize, switchName, generation, diffDisks, fixedVHD) } func (d *HypervPS4Driver) CloneVirtualMachine(cloneFromVmxcPath string, cloneFromVmName string, cloneFromSnapshotName string, cloneAllSnapshots bool, vmName string, path string, harddrivePath string, ram int64, switchName string) error { diff --git a/builder/hyperv/common/step_create_tempdir.go b/builder/hyperv/common/step_create_tempdir.go index b445a4702..6692afad4 100644 --- a/builder/hyperv/common/step_create_tempdir.go +++ b/builder/hyperv/common/step_create_tempdir.go @@ -11,14 +11,18 @@ import ( ) type StepCreateTempDir struct { - // The user-supplied root directories into which we create subdirectories. - TempPath string - VhdTempPath string - // The subdirectories with the randomly generated name. - dirPath string - vhdDirPath string + // User supplied directory under which we create the main build + // directory. The build directory is used to house the VM files and + // folders during the build. If unspecified the default temp directory + // for the OS is used + TempPath string + // The full path to the build directory. This is concatenation of + // TempPath plus a directory uniquely named for the build + dirPath string } +// Creates the main directory used to house the VMs files and folders +// during the build func (s *StepCreateTempDir) Run(_ context.Context, state multistep.StateBag) multistep.StepAction { ui := state.Get("ui").(packer.Ui) @@ -39,47 +43,20 @@ func (s *StepCreateTempDir) Run(_ context.Context, state multistep.StateBag) mul s.dirPath = 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.vhdDirPath = packerVhdTempDir - state.Put("packerVhdTempDir", packerVhdTempDir) - } - - // ui.Say("packerTempDir = '" + packerTempDir + "'") - return multistep.ActionContinue } +// Cleanup removes the build directory func (s *StepCreateTempDir) Cleanup(state multistep.StateBag) { - ui := state.Get("ui").(packer.Ui) - - if s.dirPath != "" { - ui.Say("Deleting temporary directory...") - - err := os.RemoveAll(s.dirPath) - - if err != nil { - ui.Error(fmt.Sprintf("Error deleting temporary directory: %s", err)) - } + if s.dirPath == "" { + return } - if s.vhdDirPath != "" && s.dirPath != s.vhdDirPath { - ui.Say("Deleting temporary VHD directory...") - - err := os.RemoveAll(s.vhdDirPath) + ui := state.Get("ui").(packer.Ui) + ui.Say("Deleting temporary directory...") - if err != nil { - ui.Error(fmt.Sprintf("Error deleting temporary VHD directory: %s", err)) - } + err := os.RemoveAll(s.dirPath) + if err != nil { + ui.Error(fmt.Sprintf("Error deleting temporary directory: %s", err)) } } diff --git a/builder/hyperv/common/step_create_vm.go b/builder/hyperv/common/step_create_vm.go index 255eef558..9987c50b4 100644 --- a/builder/hyperv/common/step_create_vm.go +++ b/builder/hyperv/common/step_create_vm.go @@ -55,14 +55,12 @@ func (s *StepCreateVM) Run(_ context.Context, state multistep.StateBag) multiste log.Println("No existing virtual harddrive, not attaching.") } - vhdPath := state.Get("packerVhdTempDir").(string) - // convert the MB to bytes ramSize := int64(s.RamSize * 1024 * 1024) diskSize := int64(s.DiskSize * 1024 * 1024) diskBlockSize := int64(s.DiskBlockSize * 1024 * 1024) - err := driver.CreateVirtualMachine(s.VMName, path, harddrivePath, vhdPath, ramSize, diskSize, diskBlockSize, s.SwitchName, s.Generation, s.DifferencingDisk, s.FixedVHD) + err := driver.CreateVirtualMachine(s.VMName, path, harddrivePath, ramSize, diskSize, diskBlockSize, s.SwitchName, s.Generation, s.DifferencingDisk, s.FixedVHD) if err != nil { err := fmt.Errorf("Error creating virtual machine: %s", err) state.Put("error", err) @@ -121,7 +119,7 @@ func (s *StepCreateVM) Run(_ context.Context, state multistep.StateBag) multiste for index, size := range s.AdditionalDiskSize { diskSize := int64(size * 1024 * 1024) diskFile := fmt.Sprintf("%s-%d.vhdx", s.VMName, index) - err = driver.AddVirtualMachineHardDrive(s.VMName, vhdPath, diskFile, diskSize, diskBlockSize, "SCSI") + err = driver.AddVirtualMachineHardDrive(s.VMName, path, diskFile, diskSize, diskBlockSize, "SCSI") if err != nil { err := fmt.Errorf("Error creating and attaching additional disk drive: %s", err) state.Put("error", err) diff --git a/builder/hyperv/iso/builder.go b/builder/hyperv/iso/builder.go index 78425dc33..e9ed68216 100644 --- a/builder/hyperv/iso/builder.go +++ b/builder/hyperv/iso/builder.go @@ -96,11 +96,6 @@ type Config struct { EnableVirtualizationExtensions bool `mapstructure:"enable_virtualization_extensions"` 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"` AdditionalDiskSize []uint `mapstructure:"disk_additional_size"` @@ -356,8 +351,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe steps := []multistep.Step{ &hypervcommon.StepCreateTempDir{ - TempPath: b.config.TempPath, - VhdTempPath: b.config.VhdTempPath, + TempPath: b.config.TempPath, }, &hypervcommon.StepOutputDir{ Force: b.config.PackerForce, diff --git a/common/powershell/hyperv/hyperv.go b/common/powershell/hyperv/hyperv.go index 055d12706..e21513c6d 100644 --- a/common/powershell/hyperv/hyperv.go +++ b/common/powershell/hyperv/hyperv.go @@ -198,13 +198,13 @@ Hyper-V\Set-VMFloppyDiskDrive -VMName $vmName -Path $null return err } -func CreateVirtualMachine(vmName string, path string, harddrivePath string, vhdRoot string, ram int64, diskSize int64, diskBlockSize int64, switchName string, generation uint, diffDisks bool, fixedVHD bool) error { +func CreateVirtualMachine(vmName string, path string, harddrivePath string, ram int64, diskSize int64, diskBlockSize int64, switchName string, generation uint, diffDisks bool, fixedVHD bool) error { if generation == 2 { var script = ` -param([string]$vmName, [string]$path, [string]$harddrivePath, [string]$vhdRoot, [long]$memoryStartupBytes, [long]$newVHDSizeBytes, [long]$vhdBlockSizeBytes, [string]$switchName, [int]$generation, [string]$diffDisks) +param([string]$vmName, [string]$path, [string]$harddrivePath, [long]$memoryStartupBytes, [long]$newVHDSizeBytes, [long]$vhdBlockSizeBytes, [string]$switchName, [int]$generation, [string]$diffDisks) $vhdx = $vmName + '.vhdx' -$vhdPath = Join-Path -Path $vhdRoot -ChildPath $vhdx +$vhdPath = Join-Path -Path $path -ChildPath $vhdx if ($harddrivePath){ if($diffDisks -eq "true"){ New-VHD -Path $vhdPath -ParentPath $harddrivePath -Differencing -BlockSizeBytes $vhdBlockSizeBytes @@ -218,21 +218,21 @@ if ($harddrivePath){ } ` var ps powershell.PowerShellCmd - if err := ps.Run(script, vmName, path, harddrivePath, vhdRoot, strconv.FormatInt(ram, 10), strconv.FormatInt(diskSize, 10), strconv.FormatInt(diskBlockSize, 10), switchName, strconv.FormatInt(int64(generation), 10), strconv.FormatBool(diffDisks)); err != nil { + if err := ps.Run(script, vmName, path, harddrivePath, strconv.FormatInt(ram, 10), strconv.FormatInt(diskSize, 10), strconv.FormatInt(diskBlockSize, 10), switchName, strconv.FormatInt(int64(generation), 10), strconv.FormatBool(diffDisks)); err != nil { return err } return DisableAutomaticCheckpoints(vmName) } else { var script = ` -param([string]$vmName, [string]$path, [string]$harddrivePath, [string]$vhdRoot, [long]$memoryStartupBytes, [long]$newVHDSizeBytes, [long]$vhdBlockSizeBytes, [string]$switchName, [string]$diffDisks, [string]$fixedVHD) +param([string]$vmName, [string]$path, [string]$harddrivePath, [long]$memoryStartupBytes, [long]$newVHDSizeBytes, [long]$vhdBlockSizeBytes, [string]$switchName, [string]$diffDisks, [string]$fixedVHD) if($fixedVHD -eq "true"){ $vhdx = $vmName + '.vhd' } else{ $vhdx = $vmName + '.vhdx' } -$vhdPath = Join-Path -Path $vhdRoot -ChildPath $vhdx +$vhdPath = Join-Path -Path $path -ChildPath $vhdx if ($harddrivePath){ if($diffDisks -eq "true"){ New-VHD -Path $vhdPath -ParentPath $harddrivePath -Differencing -BlockSizeBytes $vhdBlockSizeBytes @@ -252,7 +252,7 @@ if ($harddrivePath){ } ` var ps powershell.PowerShellCmd - if err := ps.Run(script, vmName, path, harddrivePath, vhdRoot, strconv.FormatInt(ram, 10), strconv.FormatInt(diskSize, 10), strconv.FormatInt(diskBlockSize, 10), switchName, strconv.FormatBool(diffDisks), strconv.FormatBool(fixedVHD)); err != nil { + if err := ps.Run(script, vmName, path, harddrivePath, strconv.FormatInt(ram, 10), strconv.FormatInt(diskSize, 10), strconv.FormatInt(diskBlockSize, 10), switchName, strconv.FormatBool(diffDisks), strconv.FormatBool(fixedVHD)); err != nil { return err }