From eb7bce072868d9c00f95693943898a7775a94e85 Mon Sep 17 00:00:00 2001 From: Megan Marsh Date: Fri, 18 Oct 2019 11:43:11 -0700 Subject: [PATCH 1/6] add additional disk size option to hyperv-vmcx --- builder/hyperv/common/step_clone_vm.go | 15 +++++++++++++++ builder/hyperv/vmcx/builder.go | 13 ++++++++++--- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/builder/hyperv/common/step_clone_vm.go b/builder/hyperv/common/step_clone_vm.go index 8233e8306..6ce856cf2 100644 --- a/builder/hyperv/common/step_clone_vm.go +++ b/builder/hyperv/common/step_clone_vm.go @@ -32,6 +32,7 @@ type StepCloneVM struct { EnableVirtualizationExtensions bool MacAddress string KeepRegistered bool + AdditionalDiskSize []uint } func (s *StepCloneVM) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction { @@ -125,6 +126,20 @@ func (s *StepCloneVM) Run(ctx context.Context, state multistep.StateBag) multist } } + if len(s.AdditionalDiskSize) > 0 { + 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, path, diskFile, diskSize, diskBlockSize, "SCSI") + if err != nil { + err := fmt.Errorf("Error creating and attaching additional disk drive: %s", err) + state.Put("error", err) + ui.Error(err.Error()) + return multistep.ActionHalt + } + } + } + if s.MacAddress != "" { err = driver.SetVmNetworkAdapterMacAddress(s.VMName, s.MacAddress) if err != nil { diff --git a/builder/hyperv/vmcx/builder.go b/builder/hyperv/vmcx/builder.go index b95630706..d43a36500 100644 --- a/builder/hyperv/vmcx/builder.go +++ b/builder/hyperv/vmcx/builder.go @@ -59,6 +59,14 @@ type Config struct { // Windows installs, which look for an Autounattend.xml file on removable // media. By default, no secondary ISO will be attached. SecondaryDvdImages []string `mapstructure:"secondary_iso_images" required:"false"` + // The size or sizes of any + // additional hard disks for the VM in megabytes. If this is not specified + // then the VM will only contain a primary hard disk. Additional drives + // will be attached to the SCSI interface only. The builder uses + // expandable rather than fixed-size virtual hard disks, so the actual + // file representing the disk will not use the full size unless it is + // full. + AdditionalDiskSize []uint `mapstructure:"disk_additional_size" required:"false"` // If set to attach then attach and // mount the ISO image specified in guest_additions_path. If set to // none then guest additions are not attached and mounted; This is the @@ -67,10 +75,8 @@ type Config struct { // The path to the ISO image for guest // additions. GuestAdditionsPath string `mapstructure:"guest_additions_path" required:"false"` - // This is the path to a directory containing an exported virtual machine. CloneFromVMCXPath string `mapstructure:"clone_from_vmcx_path"` - // This is the name of the virtual machine to clone from. CloneFromVMName string `mapstructure:"clone_from_vm_name"` // The name of a snapshot in the @@ -354,7 +360,7 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) { } } - numberOfIsos := len(b.config.SecondaryDvdImages) + numberOfIsos := len(b.config.SecondaryDvdImages) + len(b.config.AdditionalDiskSize) if b.config.GuestAdditionsMode == "attach" { if _, err := os.Stat(b.config.GuestAdditionsPath); os.IsNotExist(err) { @@ -524,6 +530,7 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack EnableVirtualizationExtensions: b.config.EnableVirtualizationExtensions, MacAddress: b.config.MacAddress, KeepRegistered: b.config.KeepRegistered, + AdditionalDiskSize: b.config.AdditionalDiskSize, }, &hypervcommon.StepEnableIntegrationService{}, From fe5ea886a23e0a65a9e660d4a27adbdc3020f97c Mon Sep 17 00:00:00 2001 From: Megan Marsh Date: Fri, 18 Oct 2019 13:01:22 -0700 Subject: [PATCH 2/6] refactir hyperv builders to remove duplicated config and prepare code --- builder/hyperv/common/config.go | 399 +++++++++++++++++++++++++ builder/hyperv/common/step_clone_vm.go | 2 + builder/hyperv/iso/builder.go | 351 +--------------------- builder/hyperv/vmcx/builder.go | 324 +------------------- 4 files changed, 414 insertions(+), 662 deletions(-) create mode 100644 builder/hyperv/common/config.go diff --git a/builder/hyperv/common/config.go b/builder/hyperv/common/config.go new file mode 100644 index 000000000..982f9c6ac --- /dev/null +++ b/builder/hyperv/common/config.go @@ -0,0 +1,399 @@ +//go:generate struct-markdown + +package common + +import ( + "fmt" + "log" + "os" + "strings" + + "github.com/hashicorp/packer/common" + powershell "github.com/hashicorp/packer/common/powershell" + "github.com/hashicorp/packer/common/powershell/hyperv" + "github.com/hashicorp/packer/template/interpolate" +) + +const ( + DefaultDiskSize = 40 * 1024 // ~40GB + MinDiskSize = 256 // 256MB + MaxDiskSize = 64 * 1024 * 1024 // 64TB + MaxVHDSize = 2040 * 1024 // 2040GB + + DefaultDiskBlockSize = 32 // 32MB + MinDiskBlockSize = 1 // 1MB + MaxDiskBlockSize = 256 // 256MB + + DefaultRamSize = 1 * 1024 // 1GB + MinRamSize = 32 // 32MB + MaxRamSize = 32 * 1024 // 32GB + MinNestedVirtualizationRamSize = 4 * 1024 // 4GB + + LowRam = 256 // 256MB + + DefaultUsername = "" + DefaultPassword = "" +) + +// AccessConfig is for common configuration related to AWS access +type CommonConfig struct { + common.FloppyConfig `mapstructure:",squash"` + // The block size of the VHD to be created. + // Recommended disk block size for Linux hyper-v guests is 1 MiB. This + // defaults to "32" MiB. + DiskBlockSize uint `mapstructure:"disk_block_size" required:"false"` + // The amount, in megabytes, of RAM to assign to the + // VM. By default, this is 1 GB. + RamSize uint `mapstructure:"memory" required:"false"` + // A list of ISO paths to + // attach to a VM when it is booted. This is most useful for unattended + // Windows installs, which look for an Autounattend.xml file on removable + // media. By default, no secondary ISO will be attached. + SecondaryDvdImages []string `mapstructure:"secondary_iso_images" required:"false"` + // The size or sizes of any + // additional hard disks for the VM in megabytes. If this is not specified + // then the VM will only contain a primary hard disk. Additional drives + // will be attached to the SCSI interface only. The builder uses + // expandable rather than fixed-size virtual hard disks, so the actual + // file representing the disk will not use the full size unless it is + // full. + AdditionalDiskSize []uint `mapstructure:"disk_additional_size" required:"false"` + // If set to attach then attach and + // mount the ISO image specified in guest_additions_path. If set to + // none then guest additions are not attached and mounted; This is the + // default. + GuestAdditionsMode string `mapstructure:"guest_additions_mode" required:"false"` + // The path to the ISO image for guest + // additions. + GuestAdditionsPath string `mapstructure:"guest_additions_path" required:"false"` + // This is the name of the new virtual machine, + // without the file extension. By default this is "packer-BUILDNAME", + // where "BUILDNAME" is the name of the build. + VMName string `mapstructure:"vm_name" required:"false"` + // The name of the switch to connect the virtual + // machine to. By default, leaving this value unset will cause Packer to + // try and determine the switch to use by looking for an external switch + // that is up and running. + SwitchName string `mapstructure:"switch_name" required:"false"` + // This is the VLAN of the virtual switch's + // network card. By default none is set. If none is set then a VLAN is not + // set on the switch's network card. If this value is set it should match + // the VLAN specified in by vlan_id. + SwitchVlanId string `mapstructure:"switch_vlan_id" required:"false"` + // This allows a specific MAC address to be used on + // the default virtual network card. The MAC address must be a string with + // no delimiters, for example "0000deadbeef". + MacAddress string `mapstructure:"mac_address" required:"false"` + // This is the VLAN of the virtual machine's network + // card for the new virtual machine. By default none is set. If none is set + // then VLANs are not set on the virtual machine's network card. + VlanId string `mapstructure:"vlan_id" required:"false"` + // The number of CPUs the virtual machine should use. If + // this isn't specified, the default is 1 CPU. + Cpu uint `mapstructure:"cpus" required:"false"` + // The Hyper-V generation for the virtual machine. By + // default, this is 1. Generation 2 Hyper-V virtual machines do not support + // floppy drives. In this scenario use secondary_iso_images instead. Hard + // drives and DVD drives will also be SCSI and not IDE. + Generation uint `mapstructure:"generation" required:"false"` + // If true enable MAC address spoofing + // for the virtual machine. This defaults to false. + EnableMacSpoofing bool `mapstructure:"enable_mac_spoofing" required:"false"` + // If true enable dynamic memory for + // the virtual machine. This defaults to false. + EnableDynamicMemory bool `mapstructure:"enable_dynamic_memory" required:"false"` + // If true enable secure boot for the + // virtual machine. This defaults to false. See secure_boot_template + // below for additional settings. + EnableSecureBoot bool `mapstructure:"enable_secure_boot" required:"false"` + // The secure boot template to be + // configured. Valid values are "MicrosoftWindows" (Windows) or + // "MicrosoftUEFICertificateAuthority" (Linux). This only takes effect if + // enable_secure_boot is set to "true". This defaults to "MicrosoftWindows". + SecureBootTemplate string `mapstructure:"secure_boot_template" required:"false"` + // If true enable + // virtualization extensions for the virtual machine. This defaults to + // false. For nested virtualization you need to enable MAC spoofing, + // disable dynamic memory and have at least 4GB of RAM assigned to the + // virtual machine. + EnableVirtualizationExtensions bool `mapstructure:"enable_virtualization_extensions" required:"false"` + // The location under which Packer will create a directory to house all the + // VM files and folders during the build. By default `%TEMP%` is used + // which, for most systems, will evaluate to + // `%USERPROFILE%/AppData/Local/Temp`. + // + // The build directory housed under `temp_path` will have a name similar to + // `packerhv1234567`. The seven digit number at the end of the name is + // automatically generated by Packer to ensure the directory name is + // unique. + TempPath string `mapstructure:"temp_path" required:"false"` + // This allows you to set the vm version when calling New-VM to generate + // the vm. + Version string `mapstructure:"configuration_version" required:"false"` + // If "true", Packer will not delete the VM from + // The Hyper-V manager. + KeepRegistered bool `mapstructure:"keep_registered" required:"false"` + + Communicator string `mapstructure:"communicator"` + // If true skip compacting the hard disk for + // the virtual machine when exporting. This defaults to false. + SkipCompaction bool `mapstructure:"skip_compaction" required:"false"` + // If true Packer will skip the export of the VM. + // If you are interested only in the VHD/VHDX files, you can enable this + // option. The resulting VHD/VHDX file will be output to + // /Virtual Hard Disks. By default this option is false + // and Packer will export the VM to output_directory. + SkipExport bool `mapstructure:"skip_export" required:"false"` + // Packer defaults to building Hyper-V virtual + // machines by launching a GUI that shows the console of the machine being + // built. When this value is set to true, the machine will start without a + // console. + Headless bool `mapstructure:"headless" required:"false"` +} + +func (c *CommonConfig) Prepare(ctx *interpolate.Context, pc *common.PackerConfig) ([]error, []string) { + // Accumulate any errors and warns + errs := make([]error, 0) + warns := make([]string, 0) + + if c.VMName == "" { + c.VMName = fmt.Sprintf("packer-%s", pc.PackerBuildName) + log.Println(fmt.Sprintf("%s: %v", "VMName", c.VMName)) + } + + if c.SwitchName == "" { + c.SwitchName = c.detectSwitchName(pc.PackerBuildName) + log.Println(fmt.Sprintf("Using switch %s", c.SwitchName)) + } + + if c.Generation < 1 || c.Generation > 2 { + c.Generation = 1 + } + + if c.Generation == 2 { + if len(c.FloppyFiles) > 0 || len(c.FloppyDirectories) > 0 { + err := fmt.Errorf("Generation 2 vms don't support floppy drives. Use ISO image instead.") + errs = append(errs, err) + } + } + + if len(c.AdditionalDiskSize) > 64 { + err := fmt.Errorf("VM's currently support a maximum of 64 additional SCSI attached disks.") + errs = append(errs, err) + } + + // Errors + errs = c.FloppyConfig.Prepare(ctx) + if c.GuestAdditionsMode == "" { + if c.GuestAdditionsPath != "" { + c.GuestAdditionsMode = "attach" + } else { + c.GuestAdditionsPath = os.Getenv("WINDIR") + "\\system32\\vmguest.iso" + + if _, err := os.Stat(c.GuestAdditionsPath); os.IsNotExist(err) { + if err != nil { + c.GuestAdditionsPath = "" + c.GuestAdditionsMode = "none" + } else { + c.GuestAdditionsMode = "attach" + } + } + } + } + + if c.GuestAdditionsPath == "" && c.GuestAdditionsMode == "attach" { + c.GuestAdditionsPath = os.Getenv("WINDIR") + "\\system32\\vmguest.iso" + + if _, err := os.Stat(c.GuestAdditionsPath); os.IsNotExist(err) { + if err != nil { + c.GuestAdditionsPath = "" + } + } + } + + for _, isoPath := range c.SecondaryDvdImages { + if _, err := os.Stat(isoPath); os.IsNotExist(err) { + if err != nil { + errs = append( + errs, fmt.Errorf("Secondary Dvd image does not exist: %s", err)) + } + } + } + + numberOfIsos := len(c.SecondaryDvdImages) + + if c.GuestAdditionsMode == "attach" { + if _, err := os.Stat(c.GuestAdditionsPath); os.IsNotExist(err) { + if err != nil { + errs = append( + errs, fmt.Errorf("Guest additions iso does not exist: %s", err)) + } + } + + numberOfIsos = numberOfIsos + 1 + } + + if c.Generation < 2 && numberOfIsos > 2 { + if c.GuestAdditionsMode == "attach" { + errs = append(errs, fmt.Errorf("There are only 2 ide controllers available, so "+ + "we can't support guest additions and these secondary dvds: %s", + strings.Join(c.SecondaryDvdImages, ", "))) + } else { + errs = append(errs, fmt.Errorf("There are only 2 ide controllers available, so "+ + "we can't support these secondary dvds: %s", + strings.Join(c.SecondaryDvdImages, ", "))) + } + } else if c.Generation > 1 && len(c.SecondaryDvdImages) > 16 { + if c.GuestAdditionsMode == "attach" { + errs = append(errs, fmt.Errorf("There are not enough drive letters available for "+ + "scsi (limited to 16), so we can't support guest additions and these secondary dvds: %s", + strings.Join(c.SecondaryDvdImages, ", "))) + } else { + errs = append(errs, fmt.Errorf("There are not enough drive letters available for "+ + "scsi (limited to 16), so we can't support these secondary dvds: %s", + strings.Join(c.SecondaryDvdImages, ", "))) + } + } + + if c.EnableVirtualizationExtensions { + hasVirtualMachineVirtualizationExtensions, err := powershell.HasVirtualMachineVirtualizationExtensions() + if err != nil { + errs = append(errs, fmt.Errorf("Failed detecting virtual machine virtualization "+ + "extensions support: %s", err)) + } else { + if !hasVirtualMachineVirtualizationExtensions { + errs = append(errs, fmt.Errorf("This version of Hyper-V does not support "+ + "virtual machine virtualization extension. Please use Windows 10 or Windows Server 2016 "+ + "or newer.")) + } + } + } + + if c.EnableVirtualizationExtensions { + if c.EnableDynamicMemory { + warning := fmt.Sprintf("For nested virtualization, when virtualization extension is enabled, " + + "dynamic memory should not be allowed.") + warns = Appendwarns(warns, warning) + } + + if !c.EnableMacSpoofing { + warning := fmt.Sprintf("For nested virtualization, when virtualization extension is enabled, " + + "mac spoofing should be allowed.") + warns = Appendwarns(warns, warning) + } + + if c.RamSize < MinNestedVirtualizationRamSize { + warning := fmt.Sprintf("For nested virtualization, when virtualization extension is enabled, " + + "there should be 4GB or more memory set for the vm, otherwise Hyper-V may fail to start " + + "any nested VMs.") + warns = Appendwarns(warns, warning) + } + } + + if c.SwitchVlanId != "" { + if c.SwitchVlanId != c.VlanId { + warning := fmt.Sprintf("Switch network adaptor vlan should match virtual machine network adaptor " + + "vlan. The switch will not be able to see traffic from the VM.") + warns = Appendwarns(warns, warning) + } + } + + err := c.checkDiskBlockSize() + if err != nil { + errs = append(errs, err) + } + err = c.checkRamSize() + if err != nil { + errs = append(errs, err) + } + + // warns + warning := c.checkHostAvailableMemory() + if warning != "" { + warns = Appendwarns(warns, warning) + } + + if errs != nil && len(errs) > 0 { + return errs, warns + } + + return nil, warns +} + +func (c *CommonConfig) checkDiskBlockSize() error { + if c.DiskBlockSize == 0 { + c.DiskBlockSize = DefaultDiskBlockSize + } + + log.Println(fmt.Sprintf("%s: %v", "DiskBlockSize", c.DiskBlockSize)) + + if c.DiskBlockSize < MinDiskBlockSize { + return fmt.Errorf("disk_block_size: Virtual machine requires disk block size >= %v MB, but defined: %v", + MinDiskBlockSize, c.DiskBlockSize) + } else if c.DiskBlockSize > MaxDiskBlockSize { + return fmt.Errorf("disk_block_size: Virtual machine requires disk block size <= %v MB, but defined: %v", + MaxDiskBlockSize, c.DiskBlockSize) + } + + return nil +} + +func (c *CommonConfig) checkHostAvailableMemory() string { + powershellAvailable, _, _ := powershell.IsPowershellAvailable() + + if powershellAvailable { + freeMB := powershell.GetHostAvailableMemory() + + if (freeMB - float64(c.RamSize)) < LowRam { + return fmt.Sprintf("Hyper-V might fail to create a VM if there is not enough free memory in the system.") + } + } + + return "" +} + +func (c *CommonConfig) checkRamSize() error { + if c.RamSize == 0 { + c.RamSize = DefaultRamSize + } + + log.Println(fmt.Sprintf("%s: %v", "RamSize", c.RamSize)) + + if c.RamSize < MinRamSize { + return fmt.Errorf("memory: Virtual machine requires memory size >= %v MB, but defined: %v", + MinRamSize, c.RamSize) + } else if c.RamSize > MaxRamSize { + return fmt.Errorf("memory: Virtual machine requires memory size <= %v MB, but defined: %v", + MaxRamSize, c.RamSize) + } + + return nil +} + +func (c *CommonConfig) detectSwitchName(buildName string) string { + powershellAvailable, _, _ := powershell.IsPowershellAvailable() + + if powershellAvailable { + // no switch name, try to get one attached to a online network adapter + onlineSwitchName, err := hyperv.GetExternalOnlineVirtualSwitch() + if onlineSwitchName != "" && err == nil { + return onlineSwitchName + } + } + + return fmt.Sprintf("packer-%s", buildName) +} + +func Appendwarns(slice []string, data ...string) []string { + m := len(slice) + n := m + len(data) + if n > cap(slice) { // if necessary, reallocate + // allocate double what's needed, for future growth. + newSlice := make([]string, (n+1)*2) + copy(newSlice, slice) + slice = newSlice + } + slice = slice[0:n] + copy(slice[m:n], data) + return slice +} diff --git a/builder/hyperv/common/step_clone_vm.go b/builder/hyperv/common/step_clone_vm.go index 6ce856cf2..20d40abfe 100644 --- a/builder/hyperv/common/step_clone_vm.go +++ b/builder/hyperv/common/step_clone_vm.go @@ -33,6 +33,7 @@ type StepCloneVM struct { MacAddress string KeepRegistered bool AdditionalDiskSize []uint + DiskBlockSize uint } func (s *StepCloneVM) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction { @@ -130,6 +131,7 @@ func (s *StepCloneVM) Run(ctx context.Context, state multistep.StateBag) multist for index, size := range s.AdditionalDiskSize { diskSize := int64(size * 1024 * 1024) diskFile := fmt.Sprintf("%s-%d.vhdx", s.VMName, index) + diskBlockSize := int64(s.DiskBlockSize) * 1024 * 1024 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) diff --git a/builder/hyperv/iso/builder.go b/builder/hyperv/iso/builder.go index a364c3226..1ee63f142 100644 --- a/builder/hyperv/iso/builder.go +++ b/builder/hyperv/iso/builder.go @@ -7,15 +7,12 @@ import ( "errors" "fmt" "log" - "os" "path/filepath" "strings" hypervcommon "github.com/hashicorp/packer/builder/hyperv/common" "github.com/hashicorp/packer/common" "github.com/hashicorp/packer/common/bootcommand" - powershell "github.com/hashicorp/packer/common/powershell" - "github.com/hashicorp/packer/common/powershell/hyperv" "github.com/hashicorp/packer/common/shutdowncommand" "github.com/hashicorp/packer/helper/communicator" "github.com/hashicorp/packer/helper/config" @@ -60,121 +57,16 @@ type Config struct { bootcommand.BootConfig `mapstructure:",squash"` hypervcommon.OutputConfig `mapstructure:",squash"` hypervcommon.SSHConfig `mapstructure:",squash"` + hypervcommon.CommonConfig `mapstructure:",squash"` shutdowncommand.ShutdownConfig `mapstructure:",squash"` // The size, in megabytes, of the hard disk to create // for the VM. By default, this is 40 GB. DiskSize uint `mapstructure:"disk_size" required:"false"` - // The block size of the VHD to be created. - // Recommended disk block size for Linux hyper-v guests is 1 MiB. This - // defaults to "32" MiB. - DiskBlockSize uint `mapstructure:"disk_block_size" required:"false"` - // The amount, in megabytes, of RAM to assign to the - // VM. By default, this is 1 GB. - RamSize uint `mapstructure:"memory" required:"false"` - // A list of ISO paths to - // attach to a VM when it is booted. This is most useful for unattended - // Windows installs, which look for an Autounattend.xml file on removable - // media. By default, no secondary ISO will be attached. - SecondaryDvdImages []string `mapstructure:"secondary_iso_images" required:"false"` - // If set to attach then attach and - // mount the ISO image specified in guest_additions_path. If set to - // none then guest additions are not attached and mounted; This is the - // default. - GuestAdditionsMode string `mapstructure:"guest_additions_mode" required:"false"` - // The path to the ISO image for guest - // additions. - GuestAdditionsPath string `mapstructure:"guest_additions_path" required:"false"` - // This is the name of the new virtual machine, - // without the file extension. By default this is "packer-BUILDNAME", - // where "BUILDNAME" is the name of the build. - VMName string `mapstructure:"vm_name" required:"false"` - // The name of the switch to connect the virtual - // machine to. By default, leaving this value unset will cause Packer to - // try and determine the switch to use by looking for an external switch - // that is up and running. - SwitchName string `mapstructure:"switch_name" required:"false"` - // This is the VLAN of the virtual switch's - // network card. By default none is set. If none is set then a VLAN is not - // set on the switch's network card. If this value is set it should match - // the VLAN specified in by vlan_id. - SwitchVlanId string `mapstructure:"switch_vlan_id" required:"false"` - // This allows a specific MAC address to be used on - // the default virtual network card. The MAC address must be a string with - // no delimiters, for example "0000deadbeef". - MacAddress string `mapstructure:"mac_address" required:"false"` - // This is the VLAN of the virtual machine's network - // card for the new virtual machine. By default none is set. If none is set - // then VLANs are not set on the virtual machine's network card. - VlanId string `mapstructure:"vlan_id" required:"false"` - // The number of CPUs the virtual machine should use. If - // this isn't specified, the default is 1 CPU. - Cpu uint `mapstructure:"cpus" required:"false"` - // The Hyper-V generation for the virtual machine. By - // default, this is 1. Generation 2 Hyper-V virtual machines do not support - // floppy drives. In this scenario use secondary_iso_images instead. Hard - // drives and DVD drives will also be SCSI and not IDE. - Generation uint `mapstructure:"generation" required:"false"` - // If true enable MAC address spoofing - // for the virtual machine. This defaults to false. - EnableMacSpoofing bool `mapstructure:"enable_mac_spoofing" required:"false"` // If true use a legacy network adapter as the NIC. // This defaults to false. A legacy network adapter is fully emulated NIC, and is thus // supported by various exotic operating systems, but this emulation requires // additional overhead and should only be used if absolutely necessary. UseLegacyNetworkAdapter bool `mapstructure:"use_legacy_network_adapter" required:"false"` - // If true enable dynamic memory for - // the virtual machine. This defaults to false. - EnableDynamicMemory bool `mapstructure:"enable_dynamic_memory" required:"false"` - // If true enable secure boot for the - // virtual machine. This defaults to false. See secure_boot_template - // below for additional settings. - EnableSecureBoot bool `mapstructure:"enable_secure_boot" required:"false"` - // The secure boot template to be - // configured. Valid values are "MicrosoftWindows" (Windows) or - // "MicrosoftUEFICertificateAuthority" (Linux). This only takes effect if - // enable_secure_boot is set to "true". This defaults to "MicrosoftWindows". - SecureBootTemplate string `mapstructure:"secure_boot_template" required:"false"` - // If true enable - // virtualization extensions for the virtual machine. This defaults to - // false. For nested virtualization you need to enable MAC spoofing, - // disable dynamic memory and have at least 4GB of RAM assigned to the - // virtual machine. - EnableVirtualizationExtensions bool `mapstructure:"enable_virtualization_extensions" required:"false"` - // The location under which Packer will create a directory to house all the - // VM files and folders during the build. By default `%TEMP%` is used - // which, for most systems, will evaluate to - // `%USERPROFILE%/AppData/Local/Temp`. - // - // The build directory housed under `temp_path` will have a name similar to - // `packerhv1234567`. The seven digit number at the end of the name is - // automatically generated by Packer to ensure the directory name is - // unique. - TempPath string `mapstructure:"temp_path" required:"false"` - // This allows you to set the vm version when calling New-VM to generate - // the vm. - Version string `mapstructure:"configuration_version" required:"false"` - // If "true", Packer will not delete the VM from - // The Hyper-V manager. - KeepRegistered bool `mapstructure:"keep_registered" required:"false"` - - Communicator string `mapstructure:"communicator"` - // The size or sizes of any - // additional hard disks for the VM in megabytes. If this is not specified - // then the VM will only contain a primary hard disk. Additional drives - // will be attached to the SCSI interface only. The builder uses - // expandable rather than fixed-size virtual hard disks, so the actual - // file representing the disk will not use the full size unless it is - // full. - AdditionalDiskSize []uint `mapstructure:"disk_additional_size" required:"false"` - // If true skip compacting the hard disk for - // the virtual machine when exporting. This defaults to false. - SkipCompaction bool `mapstructure:"skip_compaction" required:"false"` - // If true Packer will skip the export of the VM. - // If you are interested only in the VHD/VHDX files, you can enable this - // option. The resulting VHD/VHDX file will be output to - // /Virtual Hard Disks. By default this option is false - // and Packer will export the VM to output_directory. - SkipExport bool `mapstructure:"skip_export" required:"false"` // 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. @@ -188,11 +80,6 @@ type Config struct { // option is outputing a disk that is in the format required for upload to // Azure. FixedVHD bool `mapstructure:"use_fixed_vhd_format" required:"false"` - // Packer defaults to building Hyper-V virtual - // machines by launching a GUI that shows the console of the machine being - // built. When this value is set to true, the machine will start without a - // console. - Headless bool `mapstructure:"headless" required:"false"` ctx interpolate.Context } @@ -227,6 +114,10 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) { errs = packer.MultiErrorAppend(errs, b.config.SSHConfig.Prepare(&b.config.ctx)...) errs = packer.MultiErrorAppend(errs, b.config.ShutdownConfig.Prepare(&b.config.ctx)...) + commonErrs, commonWarns := b.config.CommonConfig.Prepare(&b.config.ctx, &b.config.PackerConfig) + packer.MultiErrorAppend(errs, commonErrs...) + warnings = append(warnings, commonWarns...) + if len(b.config.ISOConfig.ISOUrls) < 1 || (strings.ToLower(filepath.Ext(b.config.ISOConfig.ISOUrls[0])) != ".vhd" && strings.ToLower(filepath.Ext(b.config.ISOConfig.ISOUrls[0])) != ".vhdx") { @@ -237,139 +128,19 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) { } } - err = b.checkDiskBlockSize() - if err != nil { - errs = packer.MultiErrorAppend(errs, err) - } - - err = b.checkRamSize() - if err != nil { - errs = packer.MultiErrorAppend(errs, err) - } - - if b.config.VMName == "" { - b.config.VMName = fmt.Sprintf("packer-%s", b.config.PackerBuildName) - } - - log.Println(fmt.Sprintf("%s: %v", "VMName", b.config.VMName)) - - if b.config.SwitchName == "" { - b.config.SwitchName = b.detectSwitchName() - } - if b.config.Cpu < 1 { b.config.Cpu = 1 } - if b.config.Generation < 1 || b.config.Generation > 2 { - b.config.Generation = 1 - } - if b.config.Generation == 2 { - if len(b.config.FloppyFiles) > 0 || len(b.config.FloppyDirectories) > 0 { - err = errors.New("Generation 2 vms don't support floppy drives. Use ISO image instead.") - errs = packer.MultiErrorAppend(errs, err) - } if b.config.UseLegacyNetworkAdapter { err = errors.New("Generation 2 vms don't support legacy network adapters.") errs = packer.MultiErrorAppend(errs, err) } } - if len(b.config.AdditionalDiskSize) > 64 { - err = errors.New("VM's currently support a maximum of 64 additional SCSI attached disks.") - errs = packer.MultiErrorAppend(errs, err) - } - - log.Println(fmt.Sprintf("Using switch %s", b.config.SwitchName)) - log.Println(fmt.Sprintf("%s: %v", "SwitchName", b.config.SwitchName)) - // Errors - if b.config.GuestAdditionsMode == "" { - if b.config.GuestAdditionsPath != "" { - b.config.GuestAdditionsMode = "attach" - } else { - b.config.GuestAdditionsPath = os.Getenv("WINDIR") + "\\system32\\vmguest.iso" - - if _, err := os.Stat(b.config.GuestAdditionsPath); os.IsNotExist(err) { - if err != nil { - b.config.GuestAdditionsPath = "" - b.config.GuestAdditionsMode = "none" - } else { - b.config.GuestAdditionsMode = "attach" - } - } - } - } - - if b.config.GuestAdditionsPath == "" && b.config.GuestAdditionsMode == "attach" { - b.config.GuestAdditionsPath = os.Getenv("WINDIR") + "\\system32\\vmguest.iso" - - if _, err := os.Stat(b.config.GuestAdditionsPath); os.IsNotExist(err) { - if err != nil { - b.config.GuestAdditionsPath = "" - } - } - } - - for _, isoPath := range b.config.SecondaryDvdImages { - if _, err := os.Stat(isoPath); os.IsNotExist(err) { - if err != nil { - errs = packer.MultiErrorAppend( - errs, fmt.Errorf("Secondary Dvd image does not exist: %s", err)) - } - } - } - - numberOfIsos := len(b.config.SecondaryDvdImages) - - if b.config.GuestAdditionsMode == "attach" { - if _, err := os.Stat(b.config.GuestAdditionsPath); os.IsNotExist(err) { - if err != nil { - errs = packer.MultiErrorAppend( - errs, fmt.Errorf("Guest additions iso does not exist: %s", err)) - } - } - - numberOfIsos = numberOfIsos + 1 - } - - if b.config.Generation < 2 && numberOfIsos > 2 { - if b.config.GuestAdditionsMode == "attach" { - errs = packer.MultiErrorAppend(errs, fmt.Errorf("There are only 2 ide controllers available, "+ - "so we can't support guest additions and these secondary dvds: %s", - strings.Join(b.config.SecondaryDvdImages, ", "))) - } else { - errs = packer.MultiErrorAppend(errs, fmt.Errorf("There are only 2 ide controllers available, "+ - "so we can't support these secondary dvds: %s", strings.Join(b.config.SecondaryDvdImages, ", "))) - } - } else if b.config.Generation > 1 && len(b.config.SecondaryDvdImages) > 16 { - if b.config.GuestAdditionsMode == "attach" { - errs = packer.MultiErrorAppend(errs, fmt.Errorf("There are not enough drive letters available "+ - "for scsi (limited to 16), so we can't support guest additions and these secondary dvds: %s", - strings.Join(b.config.SecondaryDvdImages, ", "))) - } else { - errs = packer.MultiErrorAppend(errs, fmt.Errorf("There are not enough drive letters available "+ - "for scsi (limited to 16), so we can't support these secondary dvds: %s", - strings.Join(b.config.SecondaryDvdImages, ", "))) - } - } - - if b.config.EnableVirtualizationExtensions { - hasVirtualMachineVirtualizationExtensions, err := powershell.HasVirtualMachineVirtualizationExtensions() - if err != nil { - errs = packer.MultiErrorAppend(errs, fmt.Errorf("Failed detecting virtual machine virtualization "+ - "extensions support: %s", err)) - } else { - if !hasVirtualMachineVirtualizationExtensions { - errs = packer.MultiErrorAppend(errs, fmt.Errorf("This version of Hyper-V does not support "+ - "virtual machine virtualization extension. Please use Windows 10 or Windows Server "+ - "2016 or newer.")) - } - } - } - if b.config.Generation > 1 && b.config.FixedVHD { err = errors.New("Fixed VHD disks are only supported on Generation 1 virtual machines.") errs = packer.MultiErrorAppend(errs, err) @@ -393,40 +164,6 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) { "will forcibly halt the virtual machine, which may result in data loss.") } - warning := b.checkHostAvailableMemory() - if warning != "" { - warnings = appendWarnings(warnings, warning) - } - - if b.config.EnableVirtualizationExtensions { - if b.config.EnableDynamicMemory { - warning = fmt.Sprintf("For nested virtualization, when virtualization extension is enabled, " + - "dynamic memory should not be allowed.") - warnings = appendWarnings(warnings, warning) - } - - if !b.config.EnableMacSpoofing { - warning = fmt.Sprintf("For nested virtualization, when virtualization extension is enabled, " + - "mac spoofing should be allowed.") - warnings = appendWarnings(warnings, warning) - } - - if b.config.RamSize < MinNestedVirtualizationRamSize { - warning = fmt.Sprintf("For nested virtualization, when virtualization extension is enabled, " + - "there should be 4GB or more memory set for the vm, otherwise Hyper-V may fail to start " + - "any nested VMs.") - warnings = appendWarnings(warnings, warning) - } - } - - if b.config.SwitchVlanId != "" { - if b.config.SwitchVlanId != b.config.VlanId { - warning = fmt.Sprintf("Switch network adaptor vlan should match virtual machine network adaptor " + - "vlan. The switch will not be able to see traffic from the VM.") - warnings = appendWarnings(warnings, warning) - } - } - if errs != nil && len(errs.Errors) > 0 { return warnings, errs } @@ -609,20 +346,6 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack // Cancel. -func appendWarnings(slice []string, data ...string) []string { - m := len(slice) - n := m + len(data) - if n > cap(slice) { // if necessary, reallocate - // allocate double what's needed, for future growth. - newSlice := make([]string, (n+1)*2) - copy(newSlice, slice) - slice = newSlice - } - slice = slice[0:n] - copy(slice[m:n], data) - return slice -} - func (b *Builder) checkDiskSize() error { if b.config.DiskSize == 0 { b.config.DiskSize = DefaultDiskSize @@ -643,67 +366,3 @@ func (b *Builder) checkDiskSize() error { return nil } - -func (b *Builder) checkDiskBlockSize() error { - if b.config.DiskBlockSize == 0 { - b.config.DiskBlockSize = DefaultDiskBlockSize - } - - log.Println(fmt.Sprintf("%s: %v", "DiskBlockSize", b.config.DiskBlockSize)) - - if b.config.DiskBlockSize < MinDiskBlockSize { - return fmt.Errorf("disk_block_size: Virtual machine requires disk block size >= %v MB, but defined: %v", - MinDiskBlockSize, b.config.DiskBlockSize) - } else if b.config.DiskBlockSize > MaxDiskBlockSize { - return fmt.Errorf("disk_block_size: Virtual machine requires disk block size <= %v MB, but defined: %v", - MaxDiskBlockSize, b.config.DiskBlockSize) - } - - return nil -} - -func (b *Builder) checkRamSize() error { - if b.config.RamSize == 0 { - b.config.RamSize = DefaultRamSize - } - - log.Println(fmt.Sprintf("%s: %v", "RamSize", b.config.RamSize)) - - if b.config.RamSize < MinRamSize { - return fmt.Errorf("memory: Virtual machine requires memory size >= %v MB, but defined: %v", - MinRamSize, b.config.RamSize) - } else if b.config.RamSize > MaxRamSize { - return fmt.Errorf("memory: Virtual machine requires memory size <= %v MB, but defined: %v", - MaxRamSize, b.config.RamSize) - } - - return nil -} - -func (b *Builder) checkHostAvailableMemory() string { - powershellAvailable, _, _ := powershell.IsPowershellAvailable() - - if powershellAvailable { - freeMB := powershell.GetHostAvailableMemory() - - if (freeMB - float64(b.config.RamSize)) < LowRam { - return fmt.Sprintf("Hyper-V might fail to create a VM if there is not enough free memory in the system.") - } - } - - return "" -} - -func (b *Builder) detectSwitchName() string { - powershellAvailable, _, _ := powershell.IsPowershellAvailable() - - if powershellAvailable { - // no switch name, try to get one attached to a online network adapter - onlineSwitchName, err := hyperv.GetExternalOnlineVirtualSwitch() - if onlineSwitchName != "" && err == nil { - return onlineSwitchName - } - } - - return fmt.Sprintf("packer-%s", b.config.PackerBuildName) -} diff --git a/builder/hyperv/vmcx/builder.go b/builder/hyperv/vmcx/builder.go index d43a36500..e296d2012 100644 --- a/builder/hyperv/vmcx/builder.go +++ b/builder/hyperv/vmcx/builder.go @@ -6,7 +6,6 @@ import ( "context" "errors" "fmt" - "log" "os" "strings" @@ -14,7 +13,6 @@ import ( "github.com/hashicorp/packer/common" "github.com/hashicorp/packer/common/bootcommand" powershell "github.com/hashicorp/packer/common/powershell" - "github.com/hashicorp/packer/common/powershell/hyperv" "github.com/hashicorp/packer/common/shutdowncommand" "github.com/hashicorp/packer/helper/communicator" "github.com/hashicorp/packer/helper/config" @@ -46,35 +44,12 @@ type Config struct { common.PackerConfig `mapstructure:",squash"` common.HTTPConfig `mapstructure:",squash"` common.ISOConfig `mapstructure:",squash"` - common.FloppyConfig `mapstructure:",squash"` bootcommand.BootConfig `mapstructure:",squash"` hypervcommon.OutputConfig `mapstructure:",squash"` hypervcommon.SSHConfig `mapstructure:",squash"` + hypervcommon.CommonConfig `mapstructure:",squash"` shutdowncommand.ShutdownConfig `mapstructure:",squash"` - // The amount, in megabytes, of RAM to assign to the - // VM. By default, this is 1 GB. - RamSize uint `mapstructure:"memory" required:"false"` - // A list of ISO paths to - // attach to a VM when it is booted. This is most useful for unattended - // Windows installs, which look for an Autounattend.xml file on removable - // media. By default, no secondary ISO will be attached. - SecondaryDvdImages []string `mapstructure:"secondary_iso_images" required:"false"` - // The size or sizes of any - // additional hard disks for the VM in megabytes. If this is not specified - // then the VM will only contain a primary hard disk. Additional drives - // will be attached to the SCSI interface only. The builder uses - // expandable rather than fixed-size virtual hard disks, so the actual - // file representing the disk will not use the full size unless it is - // full. - AdditionalDiskSize []uint `mapstructure:"disk_additional_size" required:"false"` - // If set to attach then attach and - // mount the ISO image specified in guest_additions_path. If set to - // none then guest additions are not attached and mounted; This is the - // default. - GuestAdditionsMode string `mapstructure:"guest_additions_mode" required:"false"` - // The path to the ISO image for guest - // additions. - GuestAdditionsPath string `mapstructure:"guest_additions_path" required:"false"` + // This is the path to a directory containing an exported virtual machine. CloneFromVMCXPath string `mapstructure:"clone_from_vmcx_path"` // This is the name of the virtual machine to clone from. @@ -89,92 +64,14 @@ type Config struct { // cloned. The final result of the build will be an exported virtual // machine that contains all the snapshots of the parent. CloneAllSnapshots bool `mapstructure:"clone_all_snapshots" required:"false"` - // This is the name of the new virtual machine, - // without the file extension. By default this is "packer-BUILDNAME", - // where "BUILDNAME" is the name of the build. - VMName string `mapstructure:"vm_name" required:"false"` // 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. DifferencingDisk bool `mapstructure:"differencing_disk" required:"false"` - // The name of the switch to connect the virtual - // machine to. By default, leaving this value unset will cause Packer to - // try and determine the switch to use by looking for an external switch - // that is up and running. - SwitchName string `mapstructure:"switch_name" required:"false"` // When cloning a vm to build from, we run a powershell // Compare-VM command, which, depending on your version of Windows, may need // the "Copy" flag to be set to true or false. Defaults to "false". Command: CompareCopy bool `mapstructure:"copy_in_compare" required:"false"` - // This is the VLAN of the virtual switch's - // network card. By default none is set. If none is set then a VLAN is not - // set on the switch's network card. If this value is set it should match - // the VLAN specified in by vlan_id. - SwitchVlanId string `mapstructure:"switch_vlan_id" required:"false"` - // This allows a specific MAC address to be used on - // the default virtual network card. The MAC address must be a string with - // no delimiters, for example "0000deadbeef". - MacAddress string `mapstructure:"mac_address" required:"false"` - // This is the VLAN of the virtual machine's network - // card for the new virtual machine. By default none is set. If none is set - // then VLANs are not set on the virtual machine's network card. - VlanId string `mapstructure:"vlan_id" required:"false"` - // The number of CPUs the virtual machine should use. If - // this isn't specified, the default is 1 CPU. - Cpu uint `mapstructure:"cpus" required:"false"` - // The Hyper-V generation for the virtual machine. By - // default, this is 1. Generation 2 Hyper-V virtual machines do not support - // floppy drives. In this scenario use secondary_iso_images instead. Hard - // drives and DVD drives will also be SCSI and not IDE. - Generation uint `mapstructure:"generation" required:"false"` - // If true enable MAC address spoofing - // for the virtual machine. This defaults to false. - EnableMacSpoofing bool `mapstructure:"enable_mac_spoofing" required:"false"` - // If true enable dynamic memory for - // the virtual machine. This defaults to false. - EnableDynamicMemory bool `mapstructure:"enable_dynamic_memory" required:"false"` - // If true enable secure boot for the - // virtual machine. This defaults to false. See secure_boot_template - // below for additional settings. - EnableSecureBoot bool `mapstructure:"enable_secure_boot" required:"false"` - // The secure boot template to be - // configured. Valid values are "MicrosoftWindows" (Windows) or - // "MicrosoftUEFICertificateAuthority" (Linux). This only takes effect if - // enable_secure_boot is set to "true". This defaults to "MicrosoftWindows". - SecureBootTemplate string `mapstructure:"secure_boot_template" required:"false"` - // If true enable - // virtualization extensions for the virtual machine. This defaults to - // false. For nested virtualization you need to enable MAC spoofing, - // disable dynamic memory and have at least 4GB of RAM assigned to the - // virtual machine. - EnableVirtualizationExtensions bool `mapstructure:"enable_virtualization_extensions" required:"false"` - // The location under which Packer will create a - // directory to house all the VM files and folders during the build. - // By default %TEMP% is used which, for most systems, will evaluate to - // %USERPROFILE%/AppData/Local/Temp. - TempPath string `mapstructure:"temp_path" required:"false"` - // This allows you to set the vm version when - // calling New-VM to generate the vm. - Version string `mapstructure:"configuration_version" required:"false"` - // If "true", Packer will not delete the VM from - // The Hyper-V manager. - KeepRegistered bool `mapstructure:"keep_registered" required:"false"` - - Communicator string `mapstructure:"communicator"` - // If true skip compacting the hard disk for - // the virtual machine when exporting. This defaults to false. - SkipCompaction bool `mapstructure:"skip_compaction" required:"false"` - // If true Packer will skip the export of the VM. - // If you are interested only in the VHD/VHDX files, you can enable this - // option. The resulting VHD/VHDX file will be output to - // /Virtual Hard Disks. By default this option is false - // and Packer will export the VM to output_directory. - SkipExport bool `mapstructure:"skip_export" required:"false"` - // Packer defaults to building Hyper-V virtual - // machines by launching a GUI that shows the console of the machine being - // built. When this value is set to true, the machine will start without a - // console. - Headless bool `mapstructure:"headless" required:"false"` ctx interpolate.Context } @@ -205,26 +102,14 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) { } errs = packer.MultiErrorAppend(errs, b.config.BootConfig.Prepare(&b.config.ctx)...) - errs = packer.MultiErrorAppend(errs, b.config.FloppyConfig.Prepare(&b.config.ctx)...) errs = packer.MultiErrorAppend(errs, b.config.HTTPConfig.Prepare(&b.config.ctx)...) errs = packer.MultiErrorAppend(errs, b.config.OutputConfig.Prepare(&b.config.ctx, &b.config.PackerConfig)...) errs = packer.MultiErrorAppend(errs, b.config.SSHConfig.Prepare(&b.config.ctx)...) errs = packer.MultiErrorAppend(errs, b.config.ShutdownConfig.Prepare(&b.config.ctx)...) - err = b.checkRamSize() - if err != nil { - errs = packer.MultiErrorAppend(errs, err) - } - - if b.config.VMName == "" { - b.config.VMName = fmt.Sprintf("packer-%s", b.config.PackerBuildName) - } - - log.Println(fmt.Sprintf("%s: %v", "VMName", b.config.VMName)) - - if b.config.SwitchName == "" { - b.config.SwitchName = b.detectSwitchName() - } + commonErrs, commonWarns := b.config.CommonConfig.Prepare(&b.config.ctx, &b.config.PackerConfig) + packer.MultiErrorAppend(errs, commonErrs...) + warnings = append(warnings, commonWarns...) if b.config.Cpu < 1 { b.config.Cpu = 1 @@ -273,7 +158,7 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) { } else { if virtualMachineOn { warning := fmt.Sprintf("Cloning from a virtual machine that is running.") - warnings = appendWarnings(warnings, warning) + warnings = hypervcommon.Appendwarns(warnings, warning) } } } @@ -309,148 +194,14 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) { } } - if b.config.Generation < 1 || b.config.Generation > 2 { - b.config.Generation = 1 - } - - if b.config.Generation == 2 { - if len(b.config.FloppyFiles) > 0 || len(b.config.FloppyDirectories) > 0 { - err = errors.New("Generation 2 vms don't support floppy drives. Use ISO image instead.") - errs = packer.MultiErrorAppend(errs, err) - } - } - - log.Println(fmt.Sprintf("Using switch %s", b.config.SwitchName)) - log.Println(fmt.Sprintf("%s: %v", "SwitchName", b.config.SwitchName)) - - // Errors - if b.config.GuestAdditionsMode == "" { - if b.config.GuestAdditionsPath != "" { - b.config.GuestAdditionsMode = "attach" - } else { - b.config.GuestAdditionsPath = os.Getenv("WINDIR") + "\\system32\\vmguest.iso" - - if _, err := os.Stat(b.config.GuestAdditionsPath); os.IsNotExist(err) { - if err != nil { - b.config.GuestAdditionsPath = "" - b.config.GuestAdditionsMode = "none" - } else { - b.config.GuestAdditionsMode = "attach" - } - } - } - } - - if b.config.GuestAdditionsPath == "" && b.config.GuestAdditionsMode == "attach" { - b.config.GuestAdditionsPath = os.Getenv("WINDIR") + "\\system32\\vmguest.iso" - - if _, err := os.Stat(b.config.GuestAdditionsPath); os.IsNotExist(err) { - if err != nil { - b.config.GuestAdditionsPath = "" - } - } - } - - for _, isoPath := range b.config.SecondaryDvdImages { - if _, err := os.Stat(isoPath); os.IsNotExist(err) { - if err != nil { - errs = packer.MultiErrorAppend( - errs, fmt.Errorf("Secondary Dvd image does not exist: %s", err)) - } - } - } - - numberOfIsos := len(b.config.SecondaryDvdImages) + len(b.config.AdditionalDiskSize) - - if b.config.GuestAdditionsMode == "attach" { - if _, err := os.Stat(b.config.GuestAdditionsPath); os.IsNotExist(err) { - if err != nil { - errs = packer.MultiErrorAppend( - errs, fmt.Errorf("Guest additions iso does not exist: %s", err)) - } - } - - numberOfIsos = numberOfIsos + 1 - } - - if b.config.Generation < 2 && numberOfIsos > 2 { - if b.config.GuestAdditionsMode == "attach" { - errs = packer.MultiErrorAppend(errs, fmt.Errorf("There are only 2 ide controllers available, so "+ - "we can't support guest additions and these secondary dvds: %s", - strings.Join(b.config.SecondaryDvdImages, ", "))) - } else { - errs = packer.MultiErrorAppend(errs, fmt.Errorf("There are only 2 ide controllers available, so "+ - "we can't support these secondary dvds: %s", - strings.Join(b.config.SecondaryDvdImages, ", "))) - } - } else if b.config.Generation > 1 && len(b.config.SecondaryDvdImages) > 16 { - if b.config.GuestAdditionsMode == "attach" { - errs = packer.MultiErrorAppend(errs, fmt.Errorf("There are not enough drive letters available for "+ - "scsi (limited to 16), so we can't support guest additions and these secondary dvds: %s", - strings.Join(b.config.SecondaryDvdImages, ", "))) - } else { - errs = packer.MultiErrorAppend(errs, fmt.Errorf("There are not enough drive letters available for "+ - "scsi (limited to 16), so we can't support these secondary dvds: %s", - strings.Join(b.config.SecondaryDvdImages, ", "))) - } - } - - if b.config.EnableVirtualizationExtensions { - hasVirtualMachineVirtualizationExtensions, err := powershell.HasVirtualMachineVirtualizationExtensions() - if err != nil { - errs = packer.MultiErrorAppend(errs, fmt.Errorf("Failed detecting virtual machine virtualization "+ - "extensions support: %s", err)) - } else { - if !hasVirtualMachineVirtualizationExtensions { - errs = packer.MultiErrorAppend(errs, fmt.Errorf("This version of Hyper-V does not support "+ - "virtual machine virtualization extension. Please use Windows 10 or Windows Server 2016 "+ - "or newer.")) - } - } - } - // Warnings if b.config.ShutdownCommand == "" { - warnings = appendWarnings(warnings, + warnings = hypervcommon.Appendwarns(warnings, "A shutdown_command was not specified. Without a shutdown command, Packer\n"+ "will forcibly halt the virtual machine, which may result in data loss.") } - warning := b.checkHostAvailableMemory() - if warning != "" { - warnings = appendWarnings(warnings, warning) - } - - if b.config.EnableVirtualizationExtensions { - if b.config.EnableDynamicMemory { - warning = fmt.Sprintf("For nested virtualization, when virtualization extension is enabled, " + - "dynamic memory should not be allowed.") - warnings = appendWarnings(warnings, warning) - } - - if !b.config.EnableMacSpoofing { - warning = fmt.Sprintf("For nested virtualization, when virtualization extension is enabled, " + - "mac spoofing should be allowed.") - warnings = appendWarnings(warnings, warning) - } - - if b.config.RamSize < MinNestedVirtualizationRamSize { - warning = fmt.Sprintf("For nested virtualization, when virtualization extension is enabled, " + - "there should be 4GB or more memory set for the vm, otherwise Hyper-V may fail to start " + - "any nested VMs.") - warnings = appendWarnings(warnings, warning) - } - } - - if b.config.SwitchVlanId != "" { - if b.config.SwitchVlanId != b.config.VlanId { - warning = fmt.Sprintf("Switch network adaptor vlan should match virtual machine network adaptor " + - "vlan. The switch will not be able to see traffic from the VM.") - warnings = appendWarnings(warnings, warning) - } - } - if errs != nil && len(errs.Errors) > 0 { return warnings, errs } @@ -531,6 +282,7 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack MacAddress: b.config.MacAddress, KeepRegistered: b.config.KeepRegistered, AdditionalDiskSize: b.config.AdditionalDiskSize, + DiskBlockSize: b.config.DiskBlockSize, }, &hypervcommon.StepEnableIntegrationService{}, @@ -639,63 +391,3 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack } // Cancel. - -func appendWarnings(slice []string, data ...string) []string { - m := len(slice) - n := m + len(data) - if n > cap(slice) { // if necessary, reallocate - // allocate double what's needed, for future growth. - newSlice := make([]string, (n+1)*2) - copy(newSlice, slice) - slice = newSlice - } - slice = slice[0:n] - copy(slice[m:n], data) - return slice -} - -func (b *Builder) checkRamSize() error { - if b.config.RamSize == 0 { - b.config.RamSize = DefaultRamSize - } - - log.Println(fmt.Sprintf("%s: %v", "RamSize", b.config.RamSize)) - - if b.config.RamSize < MinRamSize { - return fmt.Errorf("memory: Virtual machine requires memory size >= %v MB, but defined: %v", - MinRamSize, b.config.RamSize) - } else if b.config.RamSize > MaxRamSize { - return fmt.Errorf("memory: Virtual machine requires memory size <= %v MB, but defined: %v", - MaxRamSize, b.config.RamSize) - } - - return nil -} - -func (b *Builder) checkHostAvailableMemory() string { - powershellAvailable, _, _ := powershell.IsPowershellAvailable() - - if powershellAvailable { - freeMB := powershell.GetHostAvailableMemory() - - if (freeMB - float64(b.config.RamSize)) < LowRam { - return fmt.Sprintf("Hyper-V might fail to create a VM if there is not enough free memory in the system.") - } - } - - return "" -} - -func (b *Builder) detectSwitchName() string { - powershellAvailable, _, _ := powershell.IsPowershellAvailable() - - if powershellAvailable { - // no switch name, try to get one attached to a online network adapter - onlineSwitchName, err := hyperv.GetExternalOnlineVirtualSwitch() - if onlineSwitchName != "" && err == nil { - return onlineSwitchName - } - } - - return fmt.Sprintf("packer-%s", b.config.PackerBuildName) -} From e439dd039be444d9f5fe033425c60a81a31ed7e0 Mon Sep 17 00:00:00 2001 From: Megan Marsh Date: Mon, 21 Oct 2019 14:08:49 -0700 Subject: [PATCH 3/6] fix tests --- builder/hyperv/common/config.go | 10 +++++----- builder/hyperv/iso/builder.go | 2 -- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/builder/hyperv/common/config.go b/builder/hyperv/common/config.go index 982f9c6ac..e890d5ac3 100644 --- a/builder/hyperv/common/config.go +++ b/builder/hyperv/common/config.go @@ -153,8 +153,8 @@ type CommonConfig struct { func (c *CommonConfig) Prepare(ctx *interpolate.Context, pc *common.PackerConfig) ([]error, []string) { // Accumulate any errors and warns - errs := make([]error, 0) - warns := make([]string, 0) + var errs []error + var warns []string if c.VMName == "" { c.VMName = fmt.Sprintf("packer-%s", pc.PackerBuildName) @@ -178,12 +178,12 @@ func (c *CommonConfig) Prepare(ctx *interpolate.Context, pc *common.PackerConfig } if len(c.AdditionalDiskSize) > 64 { - err := fmt.Errorf("VM's currently support a maximum of 64 additional SCSI attached disks.") - errs = append(errs, err) + errs = append(errs, fmt.Errorf("VM's currently support a maximum of 64 additional SCSI attached disks.")) } // Errors - errs = c.FloppyConfig.Prepare(ctx) + floppyerrs := c.FloppyConfig.Prepare(ctx) + errs = append(errs, floppyerrs...) if c.GuestAdditionsMode == "" { if c.GuestAdditionsPath != "" { c.GuestAdditionsMode = "attach" diff --git a/builder/hyperv/iso/builder.go b/builder/hyperv/iso/builder.go index 1ee63f142..796182760 100644 --- a/builder/hyperv/iso/builder.go +++ b/builder/hyperv/iso/builder.go @@ -53,7 +53,6 @@ type Config struct { common.PackerConfig `mapstructure:",squash"` common.HTTPConfig `mapstructure:",squash"` common.ISOConfig `mapstructure:",squash"` - common.FloppyConfig `mapstructure:",squash"` bootcommand.BootConfig `mapstructure:",squash"` hypervcommon.OutputConfig `mapstructure:",squash"` hypervcommon.SSHConfig `mapstructure:",squash"` @@ -108,7 +107,6 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) { errs = packer.MultiErrorAppend(errs, isoErrs...) errs = packer.MultiErrorAppend(errs, b.config.BootConfig.Prepare(&b.config.ctx)...) - errs = packer.MultiErrorAppend(errs, b.config.FloppyConfig.Prepare(&b.config.ctx)...) errs = packer.MultiErrorAppend(errs, b.config.HTTPConfig.Prepare(&b.config.ctx)...) errs = packer.MultiErrorAppend(errs, b.config.OutputConfig.Prepare(&b.config.ctx, &b.config.PackerConfig)...) errs = packer.MultiErrorAppend(errs, b.config.SSHConfig.Prepare(&b.config.ctx)...) From 9eee63a659e7fa36195d11602ce47f3dcf33f55d Mon Sep 17 00:00:00 2001 From: Megan Marsh Date: Mon, 21 Oct 2019 14:11:45 -0700 Subject: [PATCH 4/6] regenerate docs based on new struct format --- .../common/_CommonConfig-not-required.html.md | 112 ++++++++++++++++++ .../hyperv/common/_CommonConfig.html.md | 2 + .../hyperv/iso/_Config-not-required.html.md | 110 ----------------- .../hyperv/vmcx/_Config-not-required.html.md | 93 --------------- 4 files changed, 114 insertions(+), 203 deletions(-) create mode 100644 website/source/partials/builder/hyperv/common/_CommonConfig-not-required.html.md create mode 100644 website/source/partials/builder/hyperv/common/_CommonConfig.html.md diff --git a/website/source/partials/builder/hyperv/common/_CommonConfig-not-required.html.md b/website/source/partials/builder/hyperv/common/_CommonConfig-not-required.html.md new file mode 100644 index 000000000..de742a737 --- /dev/null +++ b/website/source/partials/builder/hyperv/common/_CommonConfig-not-required.html.md @@ -0,0 +1,112 @@ + + +- `disk_block_size` (uint) - The block size of the VHD to be created. + Recommended disk block size for Linux hyper-v guests is 1 MiB. This + defaults to "32" MiB. + +- `memory` (uint) - The amount, in megabytes, of RAM to assign to the + VM. By default, this is 1 GB. + +- `secondary_iso_images` ([]string) - A list of ISO paths to + attach to a VM when it is booted. This is most useful for unattended + Windows installs, which look for an Autounattend.xml file on removable + media. By default, no secondary ISO will be attached. + +- `disk_additional_size` ([]uint) - The size or sizes of any + additional hard disks for the VM in megabytes. If this is not specified + then the VM will only contain a primary hard disk. Additional drives + will be attached to the SCSI interface only. The builder uses + expandable rather than fixed-size virtual hard disks, so the actual + file representing the disk will not use the full size unless it is + full. + +- `guest_additions_mode` (string) - If set to attach then attach and + mount the ISO image specified in guest_additions_path. If set to + none then guest additions are not attached and mounted; This is the + default. + +- `guest_additions_path` (string) - The path to the ISO image for guest + additions. + +- `vm_name` (string) - This is the name of the new virtual machine, + without the file extension. By default this is "packer-BUILDNAME", + where "BUILDNAME" is the name of the build. + +- `switch_name` (string) - The name of the switch to connect the virtual + machine to. By default, leaving this value unset will cause Packer to + try and determine the switch to use by looking for an external switch + that is up and running. + +- `switch_vlan_id` (string) - This is the VLAN of the virtual switch's + network card. By default none is set. If none is set then a VLAN is not + set on the switch's network card. If this value is set it should match + the VLAN specified in by vlan_id. + +- `mac_address` (string) - This allows a specific MAC address to be used on + the default virtual network card. The MAC address must be a string with + no delimiters, for example "0000deadbeef". + +- `vlan_id` (string) - This is the VLAN of the virtual machine's network + card for the new virtual machine. By default none is set. If none is set + then VLANs are not set on the virtual machine's network card. + +- `cpus` (uint) - The number of CPUs the virtual machine should use. If + this isn't specified, the default is 1 CPU. + +- `generation` (uint) - The Hyper-V generation for the virtual machine. By + default, this is 1. Generation 2 Hyper-V virtual machines do not support + floppy drives. In this scenario use secondary_iso_images instead. Hard + drives and DVD drives will also be SCSI and not IDE. + +- `enable_mac_spoofing` (bool) - If true enable MAC address spoofing + for the virtual machine. This defaults to false. + +- `enable_dynamic_memory` (bool) - If true enable dynamic memory for + the virtual machine. This defaults to false. + +- `enable_secure_boot` (bool) - If true enable secure boot for the + virtual machine. This defaults to false. See secure_boot_template + below for additional settings. + +- `secure_boot_template` (string) - The secure boot template to be + configured. Valid values are "MicrosoftWindows" (Windows) or + "MicrosoftUEFICertificateAuthority" (Linux). This only takes effect if + enable_secure_boot is set to "true". This defaults to "MicrosoftWindows". + +- `enable_virtualization_extensions` (bool) - If true enable + virtualization extensions for the virtual machine. This defaults to + false. For nested virtualization you need to enable MAC spoofing, + disable dynamic memory and have at least 4GB of RAM assigned to the + virtual machine. + +- `temp_path` (string) - The location under which Packer will create a directory to house all the + VM files and folders during the build. By default `%TEMP%` is used + which, for most systems, will evaluate to + `%USERPROFILE%/AppData/Local/Temp`. + + The build directory housed under `temp_path` will have a name similar to + `packerhv1234567`. The seven digit number at the end of the name is + automatically generated by Packer to ensure the directory name is + unique. + +- `configuration_version` (string) - This allows you to set the vm version when calling New-VM to generate + the vm. + +- `keep_registered` (bool) - If "true", Packer will not delete the VM from + The Hyper-V manager. + +- `communicator` (string) - Communicator +- `skip_compaction` (bool) - If true skip compacting the hard disk for + the virtual machine when exporting. This defaults to false. + +- `skip_export` (bool) - If true Packer will skip the export of the VM. + If you are interested only in the VHD/VHDX files, you can enable this + option. The resulting VHD/VHDX file will be output to + /Virtual Hard Disks. By default this option is false + and Packer will export the VM to output_directory. + +- `headless` (bool) - Packer defaults to building Hyper-V virtual + machines by launching a GUI that shows the console of the machine being + built. When this value is set to true, the machine will start without a + console. + \ No newline at end of file diff --git a/website/source/partials/builder/hyperv/common/_CommonConfig.html.md b/website/source/partials/builder/hyperv/common/_CommonConfig.html.md new file mode 100644 index 000000000..f356377a3 --- /dev/null +++ b/website/source/partials/builder/hyperv/common/_CommonConfig.html.md @@ -0,0 +1,2 @@ + +AccessConfig is for common configuration related to AWS access diff --git a/website/source/partials/builder/hyperv/iso/_Config-not-required.html.md b/website/source/partials/builder/hyperv/iso/_Config-not-required.html.md index 4bdde113c..f9c805c4f 100644 --- a/website/source/partials/builder/hyperv/iso/_Config-not-required.html.md +++ b/website/source/partials/builder/hyperv/iso/_Config-not-required.html.md @@ -3,116 +3,11 @@ - `disk_size` (uint) - The size, in megabytes, of the hard disk to create for the VM. By default, this is 40 GB. -- `disk_block_size` (uint) - The block size of the VHD to be created. - Recommended disk block size for Linux hyper-v guests is 1 MiB. This - defaults to "32" MiB. - -- `memory` (uint) - The amount, in megabytes, of RAM to assign to the - VM. By default, this is 1 GB. - -- `secondary_iso_images` ([]string) - A list of ISO paths to - attach to a VM when it is booted. This is most useful for unattended - Windows installs, which look for an Autounattend.xml file on removable - media. By default, no secondary ISO will be attached. - -- `guest_additions_mode` (string) - If set to attach then attach and - mount the ISO image specified in guest_additions_path. If set to - none then guest additions are not attached and mounted; This is the - default. - -- `guest_additions_path` (string) - The path to the ISO image for guest - additions. - -- `vm_name` (string) - This is the name of the new virtual machine, - without the file extension. By default this is "packer-BUILDNAME", - where "BUILDNAME" is the name of the build. - -- `switch_name` (string) - The name of the switch to connect the virtual - machine to. By default, leaving this value unset will cause Packer to - try and determine the switch to use by looking for an external switch - that is up and running. - -- `switch_vlan_id` (string) - This is the VLAN of the virtual switch's - network card. By default none is set. If none is set then a VLAN is not - set on the switch's network card. If this value is set it should match - the VLAN specified in by vlan_id. - -- `mac_address` (string) - This allows a specific MAC address to be used on - the default virtual network card. The MAC address must be a string with - no delimiters, for example "0000deadbeef". - -- `vlan_id` (string) - This is the VLAN of the virtual machine's network - card for the new virtual machine. By default none is set. If none is set - then VLANs are not set on the virtual machine's network card. - -- `cpus` (uint) - The number of CPUs the virtual machine should use. If - this isn't specified, the default is 1 CPU. - -- `generation` (uint) - The Hyper-V generation for the virtual machine. By - default, this is 1. Generation 2 Hyper-V virtual machines do not support - floppy drives. In this scenario use secondary_iso_images instead. Hard - drives and DVD drives will also be SCSI and not IDE. - -- `enable_mac_spoofing` (bool) - If true enable MAC address spoofing - for the virtual machine. This defaults to false. - - `use_legacy_network_adapter` (bool) - If true use a legacy network adapter as the NIC. This defaults to false. A legacy network adapter is fully emulated NIC, and is thus supported by various exotic operating systems, but this emulation requires additional overhead and should only be used if absolutely necessary. -- `enable_dynamic_memory` (bool) - If true enable dynamic memory for - the virtual machine. This defaults to false. - -- `enable_secure_boot` (bool) - If true enable secure boot for the - virtual machine. This defaults to false. See secure_boot_template - below for additional settings. - -- `secure_boot_template` (string) - The secure boot template to be - configured. Valid values are "MicrosoftWindows" (Windows) or - "MicrosoftUEFICertificateAuthority" (Linux). This only takes effect if - enable_secure_boot is set to "true". This defaults to "MicrosoftWindows". - -- `enable_virtualization_extensions` (bool) - If true enable - virtualization extensions for the virtual machine. This defaults to - false. For nested virtualization you need to enable MAC spoofing, - disable dynamic memory and have at least 4GB of RAM assigned to the - virtual machine. - -- `temp_path` (string) - The location under which Packer will create a directory to house all the - VM files and folders during the build. By default `%TEMP%` is used - which, for most systems, will evaluate to - `%USERPROFILE%/AppData/Local/Temp`. - - The build directory housed under `temp_path` will have a name similar to - `packerhv1234567`. The seven digit number at the end of the name is - automatically generated by Packer to ensure the directory name is - unique. - -- `configuration_version` (string) - This allows you to set the vm version when calling New-VM to generate - the vm. - -- `keep_registered` (bool) - If "true", Packer will not delete the VM from - The Hyper-V manager. - -- `communicator` (string) - Communicator -- `disk_additional_size` ([]uint) - The size or sizes of any - additional hard disks for the VM in megabytes. If this is not specified - then the VM will only contain a primary hard disk. Additional drives - will be attached to the SCSI interface only. The builder uses - expandable rather than fixed-size virtual hard disks, so the actual - file representing the disk will not use the full size unless it is - full. - -- `skip_compaction` (bool) - If true skip compacting the hard disk for - the virtual machine when exporting. This defaults to false. - -- `skip_export` (bool) - If true Packer will skip the export of the VM. - If you are interested only in the VHD/VHDX files, you can enable this - option. The resulting VHD/VHDX file will be output to - /Virtual Hard Disks. By default this option is false - and Packer will export the VM to output_directory. - - `differencing_disk` (bool) - 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. @@ -125,9 +20,4 @@ disk_block_size will be ignored. The most likely use case for this option is outputing a disk that is in the format required for upload to Azure. - -- `headless` (bool) - Packer defaults to building Hyper-V virtual - machines by launching a GUI that shows the console of the machine being - built. When this value is set to true, the machine will start without a - console. \ No newline at end of file diff --git a/website/source/partials/builder/hyperv/vmcx/_Config-not-required.html.md b/website/source/partials/builder/hyperv/vmcx/_Config-not-required.html.md index 42fbfaad2..688686134 100644 --- a/website/source/partials/builder/hyperv/vmcx/_Config-not-required.html.md +++ b/website/source/partials/builder/hyperv/vmcx/_Config-not-required.html.md @@ -1,21 +1,5 @@ -- `memory` (uint) - The amount, in megabytes, of RAM to assign to the - VM. By default, this is 1 GB. - -- `secondary_iso_images` ([]string) - A list of ISO paths to - attach to a VM when it is booted. This is most useful for unattended - Windows installs, which look for an Autounattend.xml file on removable - media. By default, no secondary ISO will be attached. - -- `guest_additions_mode` (string) - If set to attach then attach and - mount the ISO image specified in guest_additions_path. If set to - none then guest additions are not attached and mounted; This is the - default. - -- `guest_additions_path` (string) - The path to the ISO image for guest - additions. - - `clone_from_vmcx_path` (string) - This is the path to a directory containing an exported virtual machine. - `clone_from_vm_name` (string) - This is the name of the virtual machine to clone from. @@ -30,88 +14,11 @@ cloned. The final result of the build will be an exported virtual machine that contains all the snapshots of the parent. -- `vm_name` (string) - This is the name of the new virtual machine, - without the file extension. By default this is "packer-BUILDNAME", - where "BUILDNAME" is the name of the build. - - `differencing_disk` (bool) - 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. -- `switch_name` (string) - The name of the switch to connect the virtual - machine to. By default, leaving this value unset will cause Packer to - try and determine the switch to use by looking for an external switch - that is up and running. - - `copy_in_compare` (bool) - When cloning a vm to build from, we run a powershell Compare-VM command, which, depending on your version of Windows, may need the "Copy" flag to be set to true or false. Defaults to "false". Command: - -- `switch_vlan_id` (string) - This is the VLAN of the virtual switch's - network card. By default none is set. If none is set then a VLAN is not - set on the switch's network card. If this value is set it should match - the VLAN specified in by vlan_id. - -- `mac_address` (string) - This allows a specific MAC address to be used on - the default virtual network card. The MAC address must be a string with - no delimiters, for example "0000deadbeef". - -- `vlan_id` (string) - This is the VLAN of the virtual machine's network - card for the new virtual machine. By default none is set. If none is set - then VLANs are not set on the virtual machine's network card. - -- `cpus` (uint) - The number of CPUs the virtual machine should use. If - this isn't specified, the default is 1 CPU. - -- `generation` (uint) - The Hyper-V generation for the virtual machine. By - default, this is 1. Generation 2 Hyper-V virtual machines do not support - floppy drives. In this scenario use secondary_iso_images instead. Hard - drives and DVD drives will also be SCSI and not IDE. - -- `enable_mac_spoofing` (bool) - If true enable MAC address spoofing - for the virtual machine. This defaults to false. - -- `enable_dynamic_memory` (bool) - If true enable dynamic memory for - the virtual machine. This defaults to false. - -- `enable_secure_boot` (bool) - If true enable secure boot for the - virtual machine. This defaults to false. See secure_boot_template - below for additional settings. - -- `secure_boot_template` (string) - The secure boot template to be - configured. Valid values are "MicrosoftWindows" (Windows) or - "MicrosoftUEFICertificateAuthority" (Linux). This only takes effect if - enable_secure_boot is set to "true". This defaults to "MicrosoftWindows". - -- `enable_virtualization_extensions` (bool) - If true enable - virtualization extensions for the virtual machine. This defaults to - false. For nested virtualization you need to enable MAC spoofing, - disable dynamic memory and have at least 4GB of RAM assigned to the - virtual machine. - -- `temp_path` (string) - The location under which Packer will create a - directory to house all the VM files and folders during the build. - By default %TEMP% is used which, for most systems, will evaluate to - %USERPROFILE%/AppData/Local/Temp. - -- `configuration_version` (string) - This allows you to set the vm version when - calling New-VM to generate the vm. - -- `keep_registered` (bool) - If "true", Packer will not delete the VM from - The Hyper-V manager. - -- `communicator` (string) - Communicator -- `skip_compaction` (bool) - If true skip compacting the hard disk for - the virtual machine when exporting. This defaults to false. - -- `skip_export` (bool) - If true Packer will skip the export of the VM. - If you are interested only in the VHD/VHDX files, you can enable this - option. The resulting VHD/VHDX file will be output to - /Virtual Hard Disks. By default this option is false - and Packer will export the VM to output_directory. - -- `headless` (bool) - Packer defaults to building Hyper-V virtual - machines by launching a GUI that shows the console of the machine being - built. When this value is set to true, the machine will start without a - console. \ No newline at end of file From 34f5bfa498f13b838faa5d30cd9495bb6fe3f833 Mon Sep 17 00:00:00 2001 From: Megan Marsh Date: Mon, 21 Oct 2019 14:21:15 -0700 Subject: [PATCH 5/6] regenerated things --- builder/hyperv/common/config.go | 1 - website/source/docs/builders/hyperv-iso.html.md.erb | 5 +++++ website/source/docs/builders/hyperv-vmcx.html.md.erb | 2 ++ .../partials/builder/hyperv/common/_CommonConfig.html.md | 2 -- 4 files changed, 7 insertions(+), 3 deletions(-) delete mode 100644 website/source/partials/builder/hyperv/common/_CommonConfig.html.md diff --git a/builder/hyperv/common/config.go b/builder/hyperv/common/config.go index e890d5ac3..152868e1d 100644 --- a/builder/hyperv/common/config.go +++ b/builder/hyperv/common/config.go @@ -35,7 +35,6 @@ const ( DefaultPassword = "" ) -// AccessConfig is for common configuration related to AWS access type CommonConfig struct { common.FloppyConfig `mapstructure:",squash"` // The block size of the VHD to be created. diff --git a/website/source/docs/builders/hyperv-iso.html.md.erb b/website/source/docs/builders/hyperv-iso.html.md.erb index 78f95802b..22db5fe51 100644 --- a/website/source/docs/builders/hyperv-iso.html.md.erb +++ b/website/source/docs/builders/hyperv-iso.html.md.erb @@ -70,6 +70,10 @@ In addition to the options listed here, a [communicator](/docs/templates/communicator.html) can be configured for this builder. +### Required: + +<%= partial "partials/builder/hyperv/common/CommonConfig" %> + ### Optional: - `output_directory` (string) - This setting specifies the directory that @@ -81,6 +85,7 @@ builder. "output-BUILDNAME" where "BUILDNAME" is the name of the build. <%= partial "partials/builder/hyperv/iso/Config-not-required" %> +<%= partial "partials/builder/hyperv/common/CommonConfig-not-required" %> ## Http directory configuration reference diff --git a/website/source/docs/builders/hyperv-vmcx.html.md.erb b/website/source/docs/builders/hyperv-vmcx.html.md.erb index 31d195e6d..6956516dc 100644 --- a/website/source/docs/builders/hyperv-vmcx.html.md.erb +++ b/website/source/docs/builders/hyperv-vmcx.html.md.erb @@ -76,10 +76,12 @@ builder. ### Required: <%= partial "partials/common/ISOConfig-required" %> +<%= partial "partials/common/CommonConfig" %> ### Optional: <%= partial "partials/common/ISOConfig-not-required" %> +<%= partial "partials/common/CommonConfig-not-required" %> ### Required for virtual machine import: diff --git a/website/source/partials/builder/hyperv/common/_CommonConfig.html.md b/website/source/partials/builder/hyperv/common/_CommonConfig.html.md deleted file mode 100644 index f356377a3..000000000 --- a/website/source/partials/builder/hyperv/common/_CommonConfig.html.md +++ /dev/null @@ -1,2 +0,0 @@ - -AccessConfig is for common configuration related to AWS access From 7b428f8c68e96a914fd84aed98ef07a41a1017cb Mon Sep 17 00:00:00 2001 From: Megan Marsh Date: Mon, 21 Oct 2019 14:31:39 -0700 Subject: [PATCH 6/6] switch hyperv vmcx docs to use generated partials --- .../docs/builders/hyperv-iso.html.md.erb | 4 - .../docs/builders/hyperv-vmcx.html.md.erb | 197 +----------------- 2 files changed, 2 insertions(+), 199 deletions(-) diff --git a/website/source/docs/builders/hyperv-iso.html.md.erb b/website/source/docs/builders/hyperv-iso.html.md.erb index 22db5fe51..1e376acfe 100644 --- a/website/source/docs/builders/hyperv-iso.html.md.erb +++ b/website/source/docs/builders/hyperv-iso.html.md.erb @@ -70,10 +70,6 @@ In addition to the options listed here, a [communicator](/docs/templates/communicator.html) can be configured for this builder. -### Required: - -<%= partial "partials/builder/hyperv/common/CommonConfig" %> - ### Optional: - `output_directory` (string) - This setting specifies the directory that diff --git a/website/source/docs/builders/hyperv-vmcx.html.md.erb b/website/source/docs/builders/hyperv-vmcx.html.md.erb index 6956516dc..9b3eed870 100644 --- a/website/source/docs/builders/hyperv-vmcx.html.md.erb +++ b/website/source/docs/builders/hyperv-vmcx.html.md.erb @@ -76,12 +76,10 @@ builder. ### Required: <%= partial "partials/common/ISOConfig-required" %> -<%= partial "partials/common/CommonConfig" %> ### Optional: <%= partial "partials/common/ISOConfig-not-required" %> -<%= partial "partials/common/CommonConfig-not-required" %> ### Required for virtual machine import: @@ -101,199 +99,8 @@ builder. ### Optional: -- `boot_command` (array of strings) - This is an array of commands to type - when the virtual machine is first booted. The goal of these commands - should be to type just enough to initialize the operating system - installer. Special keys can be typed as well, and are covered in the - section below on the boot command. If this is not specified, it is assumed - the installer will start itself. - -- `boot_wait` (string) - The time to wait after booting the initial virtual - machine before typing the `boot_command`. The value specified should be - a duration. For example, setting a duration of "1m30s" would cause - Packer to wait for 1 minute 30 seconds before typing the boot command. - The default duration is "10s" (10 seconds). - -- `clone_all_snapshots` (boolean) - If set to `true` all snapshots - present in the source machine will be copied when the machine is - cloned. The final result of the build will be an exported virtual - machine that contains all the snapshots of the parent. - - If `clone_from_vmcx_path` is set, this setting has no effect and any - snapshots associated with the source VM will also be present in the - resultant VM. - - If `clone_from_snapshot_name` is configured, this setting has no - effect and the resultant machine will not contain any snapshots. - -- `clone_from_snapshot_name` (string) - The name of a snapshot in the - source machine to use as a starting point for the clone. If the value - given is an empty string, the last snapshot present in the source will - be chosen as the starting point for the new VM. - - This setting only has an effect if using `clone_from_vm_name` and is - ignored otherwise. - -- `copy_in_compare` (boolean) - When cloning a vm to build from, we run a powershell - Compare-VM command, which, depending on your version of Windows, may need - the "Copy" flag to be set to true or false. Defaults to "false". Command: - - `$compatibilityReport = Hyper-V\Compare-VM -Path $VirtualMachinePath -VirtualMachinePath $importPath -SmartPagingFilePath $importPath -SnapshotFilePath $importPath -VhdDestinationPath $VirtualHarddisksPath -GenerateNewId -Copy:$copy` - - Where $copy is replaced with either true or false depending on the value of - "copy_in_compare". - -- `configuration_version` (string) - This allows you to set the vm version when - calling New-VM to generate the vm. - -- `cpus` (number) - The number of CPUs the virtual machine should use. If - this isn't specified, the default is 1 CPU. - -- `enable_dynamic_memory` (boolean) - If `true` enable dynamic memory for - the virtual machine. This defaults to `false`. - -- `enable_mac_spoofing` (boolean) - If `true` enable MAC address spoofing - for the virtual machine. This defaults to `false`. - -- `enable_secure_boot` (boolean) - If `true` enable secure boot for the - virtual machine. This defaults to `false`. See `secure_boot_template` - below for additional settings. - -- `enable_virtualization_extensions` (boolean) - If `true` enable - virtualization extensions for the virtual machine. This defaults to - `false`. For nested virtualization you need to enable MAC spoofing, - disable dynamic memory and have at least 4GB of RAM assigned to the - virtual machine. - -- `floppy_dirs` (array of strings) - A list of directories to place onto - the floppy disk recursively. This is similar to the `floppy_files` option - except that the directory structure is preserved. This is useful for when - your floppy disk includes drivers or if you just want to organize it's - contents as a hierarchy. Wildcard characters (\*, ?, and \[\]) are - allowed. The maximum summary size of all files in the listed directories - are the same as in `floppy_files`. - -- `floppy_files` (array of strings) - A list of files to place onto a floppy - disk that is attached when the VM is booted. This is most useful for - unattended Windows installs, which look for an `Autounattend.xml` file on - removable media. By default, no floppy will be attached. All files listed - in this setting get placed into the root directory of the floppy and the - floppy is attached as the first floppy device. Currently, no support - exists for creating sub-directories on the floppy. Wildcard characters - (`*`, `?`, and `[]`) are allowed. Directory names are also allowed, which - will add all the files found in the directory to the floppy. - -- `floppy_label` (string) - The label to use for the floppy disk that - is attached when the VM is booted. This is most useful for cloud-init, - Kickstart or other early initialization tools, which can benefit from labelled floppy disks. - By default, the floppy label will be 'packer'. - -- `guest_additions_mode` (string) - If set to `attach` then attach and - mount the ISO image specified in `guest_additions_path`. If set to - `none` then guest additions are not attached and mounted; This is the - default. - -- `guest_additions_path` (string) - The path to the ISO image for guest - additions. - -- `headless` (boolean) - Packer defaults to building Hyper-V virtual - machines by launching a GUI that shows the console of the machine being - built. When this value is set to true, the machine will start without a - console. - -- `http_directory` (string) - Path to a directory to serve using Packers - inbuilt HTTP server. The files in this directory will be available - over HTTP to the virtual machine. This is useful for hosting kickstart - files and so on. By default this value is unset and the HTTP server is - not started. The address and port of the HTTP server will be available - as variables in `boot_command`. This is covered in more detail below. - -- `http_port_min` and `http_port_max` (number) - These are the minimum and - maximum port to use for the HTTP server started to serve the - `http_directory`. Since Packer often runs in parallel, a randomly - available port in this range will be repeatedly chosen until an - available port is found. To force the HTTP server to use a specific - port, set an identical value for `http_port_min` and `http_port_max`. - By default the values are 8000 and 9000, respectively. - -- `keep_registered` (bool) - If "true", Packer will not delete the VM from - The Hyper-V manager. - -- `mac_address` (string) - This allows a specific MAC address to be used on - the default virtual network card. The MAC address must be a string with - no delimiters, for example "0000deadbeef". - -- `output_directory` (string) - This setting specifies the directory that - artifacts from the build, such as the virtual machine files and disks, - will be output to. The path to the directory may be relative or - absolute. If relative, the path is relative to the working directory - `packer` is executed from. This directory must not exist or, if - created, must be empty prior to running the builder. By default this is - "output-BUILDNAME" where "BUILDNAME" is the name of the build. - -- `memory` (number) - The amount, in megabytes, of RAM to assign to the - VM. By default, this is 1 GB. - -- `secondary_iso_images` (array of strings) - A list of ISO paths to - attach to a VM when it is booted. This is most useful for unattended - Windows installs, which look for an `Autounattend.xml` file on removable - media. By default, no secondary ISO will be attached. - -- `secure_boot_template` (string) - The secure boot template to be - configured. Valid values are "MicrosoftWindows" (Windows) or - "MicrosoftUEFICertificateAuthority" (Linux). This only takes effect if - `enable_secure_boot` is set to "true". This defaults to "MicrosoftWindows". - -- `shutdown_command` (string) - The command to use to gracefully shut down - the machine once all provisioning is complete. By default this is an - empty string, which tells Packer to just forcefully shut down the - machine. This setting can be safely omitted if for example, a shutdown - command to gracefully halt the machine is configured inside a - provisioning script. If one or more scripts require a reboot it is - suggested to leave this blank (since reboots may fail) and instead - specify the final shutdown command in your last script. - -- `shutdown_timeout` (string) - The amount of time to wait after executing - the `shutdown_command` for the virtual machine to actually shut down. - If the machine doesn't shut down in this time it is considered an - error. By default, the time out is "5m" (five minutes). - -- `skip_compaction` (boolean) - If `true` skip compacting the hard disk for - the virtual machine when exporting. This defaults to `false`. - -- `skip_export` (boolean) - If `true` Packer will skip the export of the VM. - If you are interested only in the VHD/VHDX files, you can enable this - option. The resulting VHD/VHDX file will be output to - `/Virtual Hard Disks`. By default this option is `false` - and Packer will export the VM to `output_directory`. - -- `switch_name` (string) - The name of the switch to connect the virtual - machine to. By default, leaving this value unset will cause Packer to - try and determine the switch to use by looking for an external switch - that is up and running. - -- `switch_vlan_id` (string) - This is the VLAN of the virtual switch's - network card. By default none is set. If none is set then a VLAN is not - set on the switch's network card. If this value is set it should match - the VLAN specified in by `vlan_id`. - -- `temp_path` (string) - The location under which Packer will create a - directory to house all the VM files and folders during the build. - By default `%TEMP%` is used which, for most systems, will evaluate to - `%USERPROFILE%/AppData/Local/Temp`. - - The build directory housed under `temp_path` will have a name similar - to `packerhv1234567`. The seven digit number at the end of the name is - automatically generated by Packer to ensure the directory name is - unique. - -- `vlan_id` (string) - This is the VLAN of the virtual machine's network - card for the new virtual machine. By default none is set. If none is set - then VLANs are not set on the virtual machine's network card. - -- `vm_name` (string) - This is the name of the new virtual machine, - without the file extension. By default this is "packer-BUILDNAME", - where "BUILDNAME" is the name of the build. +<%= partial "partials/builder/hyperv/vmcx/Config-not-required" %> +<%= partial "partials/builder/hyperv/common/CommonConfig-not-required" %> ## Boot Command