From 54f059d3d4006cc1481709d8485ff14c67c03f43 Mon Sep 17 00:00:00 2001 From: Vijaya Bhaskar Reddy Kondreddi Date: Fri, 13 Oct 2017 00:46:24 +0530 Subject: [PATCH 1/2] Add support for skip export --- builder/hyperv/common/step_create_vm.go | 8 +++++ builder/hyperv/common/step_export_vm.go | 46 +++++++++++++------------ builder/hyperv/iso/builder.go | 5 +++ builder/hyperv/vmcx/builder.go | 3 ++ 4 files changed, 40 insertions(+), 22 deletions(-) diff --git a/builder/hyperv/common/step_create_vm.go b/builder/hyperv/common/step_create_vm.go index 02171d9c6..852880911 100644 --- a/builder/hyperv/common/step_create_vm.go +++ b/builder/hyperv/common/step_create_vm.go @@ -28,6 +28,8 @@ type StepCreateVM struct { EnableVirtualizationExtensions bool AdditionalDiskSize []uint DifferencingDisk bool + SkipExport bool + OutputDir string } func (s *StepCreateVM) Run(state multistep.StateBag) multistep.StepAction { @@ -51,6 +53,12 @@ func (s *StepCreateVM) Run(state multistep.StateBag) multistep.StepAction { } vhdPath := state.Get("packerVhdTempDir").(string) + + // inline vhd path if export is skipped + if s.SkipExport { + vhdPath = filepath.Join(s.OutputDir, "Virtual Hard Disks") + } + // convert the MB to bytes ramSize := int64(s.RamSize * 1024 * 1024) diskSize := int64(s.DiskSize * 1024 * 1024) diff --git a/builder/hyperv/common/step_export_vm.go b/builder/hyperv/common/step_export_vm.go index 8c32052a3..a9c2097a5 100644 --- a/builder/hyperv/common/step_export_vm.go +++ b/builder/hyperv/common/step_export_vm.go @@ -17,18 +17,19 @@ const ( type StepExportVm struct { OutputDir string SkipCompaction bool + SkipExport bool } func (s *StepExportVm) Run(state multistep.StateBag) multistep.StepAction { driver := state.Get("driver").(Driver) ui := state.Get("ui").(packer.Ui) - var err error var errorMsg string vmName := state.Get("vmName").(string) tmpPath := state.Get("packerTempDir").(string) outputPath := s.OutputDir + expPath := s.OutputDir // create temp path to export vm errorMsg = "Error creating temp export path: %s" @@ -39,21 +40,21 @@ func (s *StepExportVm) Run(state multistep.StateBag) multistep.StepAction { ui.Error(err.Error()) return multistep.ActionHalt } + if !s.SkipExport { + ui.Say("Exporting vm...") - ui.Say("Exporting vm...") - - err = driver.ExportVirtualMachine(vmName, vmExportPath) - if err != nil { - errorMsg = "Error exporting vm: %s" - err := fmt.Errorf(errorMsg, err) - state.Put("error", err) - ui.Error(err.Error()) - return multistep.ActionHalt + err = driver.ExportVirtualMachine(vmName, vmExportPath) + if err != nil { + errorMsg = "Error exporting vm: %s" + err := fmt.Errorf(errorMsg, err) + state.Put("error", err) + ui.Error(err.Error()) + return multistep.ActionHalt + } + // copy to output dir + expPath = filepath.Join(vmExportPath, vmName) } - // copy to output dir - expPath := filepath.Join(vmExportPath, vmName) - if s.SkipCompaction { ui.Say("Skipping disk compaction...") } else { @@ -68,16 +69,17 @@ func (s *StepExportVm) Run(state multistep.StateBag) multistep.StepAction { } } - ui.Say("Copying to output dir...") - err = driver.CopyExportedVirtualMachine(expPath, outputPath, vhdDir, vmDir) - if err != nil { - errorMsg = "Error exporting vm: %s" - err := fmt.Errorf(errorMsg, err) - state.Put("error", err) - ui.Error(err.Error()) - return multistep.ActionHalt + if !s.SkipExport { + ui.Say("Copying to output dir...") + err = driver.CopyExportedVirtualMachine(expPath, outputPath, vhdDir, vmDir) + if err != nil { + errorMsg = "Error exporting vm: %s" + err := fmt.Errorf(errorMsg, err) + state.Put("error", err) + ui.Error(err.Error()) + return multistep.ActionHalt + } } - return multistep.ActionContinue } diff --git a/builder/hyperv/iso/builder.go b/builder/hyperv/iso/builder.go index 849cbcacf..35ed2c698 100644 --- a/builder/hyperv/iso/builder.go +++ b/builder/hyperv/iso/builder.go @@ -94,6 +94,8 @@ type Config struct { SkipCompaction bool `mapstructure:"skip_compaction"` + SkipExport bool `mapstructure:"skip_export"` + // Use differencing disk DifferencingDisk bool `mapstructure:"differencing_disk"` @@ -357,6 +359,8 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe EnableVirtualizationExtensions: b.config.EnableVirtualizationExtensions, AdditionalDiskSize: b.config.AdditionalDiskSize, DifferencingDisk: b.config.DifferencingDisk, + SkipExport: b.config.SkipExport, + OutputDir: b.config.OutputDir, }, &hypervcommon.StepEnableIntegrationService{}, @@ -422,6 +426,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe &hypervcommon.StepExportVm{ OutputDir: b.config.OutputDir, SkipCompaction: b.config.SkipCompaction, + SkipExport: b.config.SkipExport, }, // the clean up actions for each step will be executed reverse order diff --git a/builder/hyperv/vmcx/builder.go b/builder/hyperv/vmcx/builder.go index 6a976b3df..51997a384 100644 --- a/builder/hyperv/vmcx/builder.go +++ b/builder/hyperv/vmcx/builder.go @@ -94,6 +94,8 @@ type Config struct { SkipCompaction bool `mapstructure:"skip_compaction"` + SkipExport bool `mapstructure:"skip_export"` + ctx interpolate.Context } @@ -469,6 +471,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe &hypervcommon.StepExportVm{ OutputDir: b.config.OutputDir, SkipCompaction: b.config.SkipCompaction, + SkipExport: b.config.SkipExport, }, // the clean up actions for each step will be executed reverse order From 5346583df96f06b888b6a2a69a652c61605751df Mon Sep 17 00:00:00 2001 From: Vijaya Bhaskar Reddy Kondreddi Date: Tue, 28 Nov 2017 14:56:58 +0530 Subject: [PATCH 2/2] Update docs --- website/source/docs/builders/hyperv-iso.html.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/website/source/docs/builders/hyperv-iso.html.md b/website/source/docs/builders/hyperv-iso.html.md index ff267392c..24fee6f05 100644 --- a/website/source/docs/builders/hyperv-iso.html.md +++ b/website/source/docs/builders/hyperv-iso.html.md @@ -97,6 +97,12 @@ can be configured for this builder. - `disk_size` (number) - The size, in megabytes, of the hard disk to create for the VM. By default, this is 40 GB. +- `differencing_disk` (boolean) - If true enables differencing disks. Only the changes will be written to the new disk. This is especially useful if your + source is a vhd/vhdx. This defaults to false. + +- `skip_export` (boolean) - If true skips VM export. If you are interested only in the vhd/vhdx files, you can enable this option. This will create + inline disks which improves the build performance. There will not be any copying of source vhds to temp directory. This defauls to false. + - `enable_dynamic_memory` (boolean) - If true enable dynamic memory for virtual machine. This defaults to false.