Merge pull request #5631 from vijayinvites/diffdisks
[Hyper-V] Use differential disks and Inline disk creation to improve build time (a lot) and to reduce disk usage(a lot)
This commit is contained in:
commit
6d14eb6ea4
|
@ -29,6 +29,8 @@ type StepCreateVM struct {
|
||||||
EnableVirtualizationExtensions bool
|
EnableVirtualizationExtensions bool
|
||||||
AdditionalDiskSize []uint
|
AdditionalDiskSize []uint
|
||||||
DifferencingDisk bool
|
DifferencingDisk bool
|
||||||
|
SkipExport bool
|
||||||
|
OutputDir string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *StepCreateVM) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
|
func (s *StepCreateVM) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
|
||||||
|
@ -52,6 +54,12 @@ func (s *StepCreateVM) Run(_ context.Context, state multistep.StateBag) multiste
|
||||||
}
|
}
|
||||||
|
|
||||||
vhdPath := state.Get("packerVhdTempDir").(string)
|
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
|
// 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)
|
||||||
|
|
|
@ -18,18 +18,19 @@ const (
|
||||||
type StepExportVm struct {
|
type StepExportVm struct {
|
||||||
OutputDir string
|
OutputDir string
|
||||||
SkipCompaction bool
|
SkipCompaction bool
|
||||||
|
SkipExport bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *StepExportVm) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
|
func (s *StepExportVm) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
|
||||||
driver := state.Get("driver").(Driver)
|
driver := state.Get("driver").(Driver)
|
||||||
ui := state.Get("ui").(packer.Ui)
|
ui := state.Get("ui").(packer.Ui)
|
||||||
|
|
||||||
var err error
|
var err error
|
||||||
var errorMsg string
|
var errorMsg string
|
||||||
|
|
||||||
vmName := state.Get("vmName").(string)
|
vmName := state.Get("vmName").(string)
|
||||||
tmpPath := state.Get("packerTempDir").(string)
|
tmpPath := state.Get("packerTempDir").(string)
|
||||||
outputPath := s.OutputDir
|
outputPath := s.OutputDir
|
||||||
|
expPath := s.OutputDir
|
||||||
|
|
||||||
// create temp path to export vm
|
// create temp path to export vm
|
||||||
errorMsg = "Error creating temp export path: %s"
|
errorMsg = "Error creating temp export path: %s"
|
||||||
|
@ -40,21 +41,21 @@ func (s *StepExportVm) Run(_ context.Context, state multistep.StateBag) multiste
|
||||||
ui.Error(err.Error())
|
ui.Error(err.Error())
|
||||||
return multistep.ActionHalt
|
return multistep.ActionHalt
|
||||||
}
|
}
|
||||||
|
if !s.SkipExport {
|
||||||
|
ui.Say("Exporting vm...")
|
||||||
|
|
||||||
ui.Say("Exporting vm...")
|
err = driver.ExportVirtualMachine(vmName, vmExportPath)
|
||||||
|
if err != nil {
|
||||||
err = driver.ExportVirtualMachine(vmName, vmExportPath)
|
errorMsg = "Error exporting vm: %s"
|
||||||
if err != nil {
|
err := fmt.Errorf(errorMsg, err)
|
||||||
errorMsg = "Error exporting vm: %s"
|
state.Put("error", err)
|
||||||
err := fmt.Errorf(errorMsg, err)
|
ui.Error(err.Error())
|
||||||
state.Put("error", err)
|
return multistep.ActionHalt
|
||||||
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 {
|
if s.SkipCompaction {
|
||||||
ui.Say("Skipping disk compaction...")
|
ui.Say("Skipping disk compaction...")
|
||||||
} else {
|
} else {
|
||||||
|
@ -69,16 +70,17 @@ func (s *StepExportVm) Run(_ context.Context, state multistep.StateBag) multiste
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ui.Say("Copying to output dir...")
|
if !s.SkipExport {
|
||||||
err = driver.CopyExportedVirtualMachine(expPath, outputPath, vhdDir, vmDir)
|
ui.Say("Copying to output dir...")
|
||||||
if err != nil {
|
err = driver.CopyExportedVirtualMachine(expPath, outputPath, vhdDir, vmDir)
|
||||||
errorMsg = "Error exporting vm: %s"
|
if err != nil {
|
||||||
err := fmt.Errorf(errorMsg, err)
|
errorMsg = "Error exporting vm: %s"
|
||||||
state.Put("error", err)
|
err := fmt.Errorf(errorMsg, err)
|
||||||
ui.Error(err.Error())
|
state.Put("error", err)
|
||||||
return multistep.ActionHalt
|
ui.Error(err.Error())
|
||||||
|
return multistep.ActionHalt
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return multistep.ActionContinue
|
return multistep.ActionContinue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -94,6 +94,8 @@ type Config struct {
|
||||||
|
|
||||||
SkipCompaction bool `mapstructure:"skip_compaction"`
|
SkipCompaction bool `mapstructure:"skip_compaction"`
|
||||||
|
|
||||||
|
SkipExport bool `mapstructure:"skip_export"`
|
||||||
|
|
||||||
// Use differencing disk
|
// Use differencing disk
|
||||||
DifferencingDisk bool `mapstructure:"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,
|
EnableVirtualizationExtensions: b.config.EnableVirtualizationExtensions,
|
||||||
AdditionalDiskSize: b.config.AdditionalDiskSize,
|
AdditionalDiskSize: b.config.AdditionalDiskSize,
|
||||||
DifferencingDisk: b.config.DifferencingDisk,
|
DifferencingDisk: b.config.DifferencingDisk,
|
||||||
|
SkipExport: b.config.SkipExport,
|
||||||
|
OutputDir: b.config.OutputDir,
|
||||||
},
|
},
|
||||||
&hypervcommon.StepEnableIntegrationService{},
|
&hypervcommon.StepEnableIntegrationService{},
|
||||||
|
|
||||||
|
@ -422,6 +426,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
|
||||||
&hypervcommon.StepExportVm{
|
&hypervcommon.StepExportVm{
|
||||||
OutputDir: b.config.OutputDir,
|
OutputDir: b.config.OutputDir,
|
||||||
SkipCompaction: b.config.SkipCompaction,
|
SkipCompaction: b.config.SkipCompaction,
|
||||||
|
SkipExport: b.config.SkipExport,
|
||||||
},
|
},
|
||||||
|
|
||||||
// the clean up actions for each step will be executed reverse order
|
// the clean up actions for each step will be executed reverse order
|
||||||
|
|
|
@ -94,6 +94,8 @@ type Config struct {
|
||||||
|
|
||||||
SkipCompaction bool `mapstructure:"skip_compaction"`
|
SkipCompaction bool `mapstructure:"skip_compaction"`
|
||||||
|
|
||||||
|
SkipExport bool `mapstructure:"skip_export"`
|
||||||
|
|
||||||
ctx interpolate.Context
|
ctx interpolate.Context
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -469,6 +471,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
|
||||||
&hypervcommon.StepExportVm{
|
&hypervcommon.StepExportVm{
|
||||||
OutputDir: b.config.OutputDir,
|
OutputDir: b.config.OutputDir,
|
||||||
SkipCompaction: b.config.SkipCompaction,
|
SkipCompaction: b.config.SkipCompaction,
|
||||||
|
SkipExport: b.config.SkipExport,
|
||||||
},
|
},
|
||||||
|
|
||||||
// the clean up actions for each step will be executed reverse order
|
// the clean up actions for each step will be executed reverse order
|
||||||
|
|
|
@ -97,6 +97,12 @@ can be configured for this builder.
|
||||||
- `disk_size` (number) - The size, in megabytes, of the hard disk to create
|
- `disk_size` (number) - The size, in megabytes, of the hard disk to create
|
||||||
for the VM. By default, this is 40 GB.
|
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.
|
- `enable_dynamic_memory` (boolean) - If true enable dynamic memory for virtual machine.
|
||||||
This defaults to false.
|
This defaults to false.
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue