2019-05-31 08:27:41 -04:00
|
|
|
//go:generate struct-markdown
|
|
|
|
|
2017-03-12 07:31:31 -04:00
|
|
|
package vmcx
|
|
|
|
|
|
|
|
import (
|
2019-03-22 09:53:28 -04:00
|
|
|
"context"
|
2017-03-12 07:31:31 -04:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
|
2017-05-21 12:29:26 -04:00
|
|
|
hypervcommon "github.com/hashicorp/packer/builder/hyperv/common"
|
|
|
|
"github.com/hashicorp/packer/common"
|
2018-04-18 19:26:48 -04:00
|
|
|
"github.com/hashicorp/packer/common/bootcommand"
|
2017-05-21 12:29:26 -04:00
|
|
|
powershell "github.com/hashicorp/packer/common/powershell"
|
|
|
|
"github.com/hashicorp/packer/common/powershell/hyperv"
|
2019-06-13 11:23:21 -04:00
|
|
|
"github.com/hashicorp/packer/common/shutdowncommand"
|
2017-05-21 12:29:26 -04:00
|
|
|
"github.com/hashicorp/packer/helper/communicator"
|
|
|
|
"github.com/hashicorp/packer/helper/config"
|
2018-01-19 19:18:44 -05:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
2017-05-21 12:29:26 -04:00
|
|
|
"github.com/hashicorp/packer/packer"
|
|
|
|
"github.com/hashicorp/packer/template/interpolate"
|
2017-03-12 07:31:31 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2019-06-24 19:39:27 -04:00
|
|
|
DefaultRamSize = 1 * 1024 // 1GB
|
|
|
|
MinRamSize = 32 // 32MB
|
|
|
|
MaxRamSize = 1024 * 1024 // 1TB
|
|
|
|
MinNestedVirtualizationRamSize = 4 * 1024 // 4GB
|
2017-03-12 07:31:31 -04:00
|
|
|
|
|
|
|
LowRam = 256 // 256MB
|
|
|
|
|
|
|
|
DefaultUsername = ""
|
|
|
|
DefaultPassword = ""
|
|
|
|
)
|
|
|
|
|
|
|
|
// Builder implements packer.Builder and builds the actual Hyperv
|
|
|
|
// images.
|
|
|
|
type Builder struct {
|
|
|
|
config Config
|
|
|
|
runner multistep.Runner
|
|
|
|
}
|
|
|
|
|
|
|
|
type Config struct {
|
2019-06-13 11:23:21 -04:00
|
|
|
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"`
|
|
|
|
shutdowncommand.ShutdownConfig `mapstructure:",squash"`
|
2019-05-28 11:50:58 -04:00
|
|
|
// The amount, in megabytes, of RAM to assign to the
|
2019-06-06 10:29:25 -04:00
|
|
|
// VM. By default, this is 1 GB.
|
2019-05-28 11:50:58 -04:00
|
|
|
RamSize uint `mapstructure:"memory" required:"false"`
|
|
|
|
// A list of ISO paths to
|
2019-06-06 10:29:25 -04:00
|
|
|
// 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.
|
2019-05-28 11:50:58 -04:00
|
|
|
SecondaryDvdImages []string `mapstructure:"secondary_iso_images" required:"false"`
|
|
|
|
// If set to attach then attach and
|
2019-06-06 10:29:25 -04:00
|
|
|
// 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.
|
2019-05-28 11:50:58 -04:00
|
|
|
GuestAdditionsMode string `mapstructure:"guest_additions_mode" required:"false"`
|
|
|
|
// The path to the ISO image for guest
|
2019-06-06 10:29:25 -04:00
|
|
|
// additions.
|
2019-05-28 11:50:58 -04:00
|
|
|
GuestAdditionsPath string `mapstructure:"guest_additions_path" required:"false"`
|
2017-03-12 07:31:31 -04:00
|
|
|
|
2017-05-21 12:29:26 -04:00
|
|
|
// This is the path to a directory containing an exported virtual machine.
|
2018-07-19 13:53:42 -04:00
|
|
|
CloneFromVMCXPath string `mapstructure:"clone_from_vmcx_path"`
|
2017-05-29 20:16:03 -04:00
|
|
|
|
2017-03-12 07:31:31 -04:00
|
|
|
// This is the name of the virtual machine to clone from.
|
|
|
|
CloneFromVMName string `mapstructure:"clone_from_vm_name"`
|
2019-05-28 11:50:58 -04:00
|
|
|
// The name of a snapshot in the
|
2019-06-06 10:29:25 -04:00
|
|
|
// 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.
|
2019-05-28 11:50:58 -04:00
|
|
|
CloneFromSnapshotName string `mapstructure:"clone_from_snapshot_name" required:"false"`
|
|
|
|
// If set to true all snapshots
|
2019-06-06 10:29:25 -04:00
|
|
|
// 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.
|
2019-05-28 11:50:58 -04:00
|
|
|
CloneAllSnapshots bool `mapstructure:"clone_all_snapshots" required:"false"`
|
|
|
|
// This is the name of the new virtual machine,
|
2019-06-06 10:29:25 -04:00
|
|
|
// without the file extension. By default this is "packer-BUILDNAME",
|
|
|
|
// where "BUILDNAME" is the name of the build.
|
2019-05-28 11:50:58 -04:00
|
|
|
VMName string `mapstructure:"vm_name" required:"false"`
|
|
|
|
// If true enables differencing disks. Only
|
2019-06-06 10:29:25 -04:00
|
|
|
// the changes will be written to the new disk. This is especially useful if
|
|
|
|
// your source is a VHD/VHDX. This defaults to false.
|
2019-05-28 11:50:58 -04:00
|
|
|
DifferencingDisk bool `mapstructure:"differencing_disk" required:"false"`
|
|
|
|
// The name of the switch to connect the virtual
|
2019-06-06 10:29:25 -04:00
|
|
|
// 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"`
|
2019-05-28 11:50:58 -04:00
|
|
|
// When cloning a vm to build from, we run a powershell
|
2019-06-06 10:29:25 -04:00
|
|
|
// 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"`
|
2019-05-28 11:50:58 -04:00
|
|
|
// This is the VLAN of the virtual switch's
|
2019-06-06 10:29:25 -04:00
|
|
|
// 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"`
|
2019-05-28 11:50:58 -04:00
|
|
|
// This allows a specific MAC address to be used on
|
2019-06-06 10:29:25 -04:00
|
|
|
// 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"`
|
2019-05-28 11:50:58 -04:00
|
|
|
// This is the VLAN of the virtual machine's network
|
2019-06-06 10:29:25 -04:00
|
|
|
// 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"`
|
2019-05-28 11:50:58 -04:00
|
|
|
// The number of CPUs the virtual machine should use. If
|
2019-06-06 10:29:25 -04:00
|
|
|
// this isn't specified, the default is 1 CPU.
|
|
|
|
Cpu uint `mapstructure:"cpus" required:"false"`
|
2019-05-28 11:50:58 -04:00
|
|
|
// The Hyper-V generation for the virtual machine. By
|
2019-06-06 10:29:25 -04:00
|
|
|
// 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"`
|
2019-05-28 11:50:58 -04:00
|
|
|
// If true enable MAC address spoofing
|
2019-06-06 10:29:25 -04:00
|
|
|
// for the virtual machine. This defaults to false.
|
|
|
|
EnableMacSpoofing bool `mapstructure:"enable_mac_spoofing" required:"false"`
|
2019-05-28 11:50:58 -04:00
|
|
|
// If true enable dynamic memory for
|
2019-06-06 10:29:25 -04:00
|
|
|
// the virtual machine. This defaults to false.
|
|
|
|
EnableDynamicMemory bool `mapstructure:"enable_dynamic_memory" required:"false"`
|
2019-05-28 11:50:58 -04:00
|
|
|
// If true enable secure boot for the
|
2019-06-06 10:29:25 -04:00
|
|
|
// virtual machine. This defaults to false. See secure_boot_template
|
|
|
|
// below for additional settings.
|
|
|
|
EnableSecureBoot bool `mapstructure:"enable_secure_boot" required:"false"`
|
2019-05-28 11:50:58 -04:00
|
|
|
// The secure boot template to be
|
2019-06-06 10:29:25 -04:00
|
|
|
// 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"`
|
2019-05-28 11:50:58 -04:00
|
|
|
// If true enable
|
2019-06-06 10:29:25 -04:00
|
|
|
// 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"`
|
2019-05-28 11:50:58 -04:00
|
|
|
// The location under which Packer will create a
|
2019-06-06 10:29:25 -04:00
|
|
|
// 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"`
|
2019-05-28 11:50:58 -04:00
|
|
|
// This allows you to set the vm version when
|
2019-06-06 10:29:25 -04:00
|
|
|
// calling New-VM to generate the vm.
|
|
|
|
Version string `mapstructure:"configuration_version" required:"false"`
|
2019-05-28 11:50:58 -04:00
|
|
|
// If "true", Packer will not delete the VM from
|
2019-06-06 10:29:25 -04:00
|
|
|
// The Hyper-V manager.
|
|
|
|
KeepRegistered bool `mapstructure:"keep_registered" required:"false"`
|
2017-03-12 07:31:31 -04:00
|
|
|
|
|
|
|
Communicator string `mapstructure:"communicator"`
|
2019-05-28 11:50:58 -04:00
|
|
|
// If true skip compacting the hard disk for
|
2019-06-06 10:29:25 -04:00
|
|
|
// the virtual machine when exporting. This defaults to false.
|
2019-05-28 11:50:58 -04:00
|
|
|
SkipCompaction bool `mapstructure:"skip_compaction" required:"false"`
|
|
|
|
// If true Packer will skip the export of the VM.
|
2019-06-06 10:29:25 -04:00
|
|
|
// If you are interested only in the VHD/VHDX files, you can enable this
|
|
|
|
// option. The resulting VHD/VHDX file will be output to
|
|
|
|
// <output_directory>/Virtual Hard Disks. By default this option is false
|
|
|
|
// and Packer will export the VM to output_directory.
|
2019-05-28 11:50:58 -04:00
|
|
|
SkipExport bool `mapstructure:"skip_export" required:"false"`
|
|
|
|
// Packer defaults to building Hyper-V virtual
|
2019-06-06 10:29:25 -04:00
|
|
|
// 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.
|
2019-05-28 11:50:58 -04:00
|
|
|
Headless bool `mapstructure:"headless" required:"false"`
|
2018-05-05 17:54:58 -04:00
|
|
|
|
2017-03-12 07:31:31 -04:00
|
|
|
ctx interpolate.Context
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prepare processes the build configuration parameters.
|
|
|
|
func (b *Builder) Prepare(raws ...interface{}) ([]string, error) {
|
|
|
|
err := config.Decode(&b.config, &config.DecodeOpts{
|
2017-11-05 09:16:47 -05:00
|
|
|
Interpolate: true,
|
2017-11-05 08:55:56 -05:00
|
|
|
InterpolateContext: &b.config.ctx,
|
2017-03-12 07:31:31 -04:00
|
|
|
InterpolateFilter: &interpolate.RenderFilter{
|
|
|
|
Exclude: []string{
|
|
|
|
"boot_command",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, raws...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Accumulate any errors and warnings
|
|
|
|
var errs *packer.MultiError
|
|
|
|
warnings := make([]string, 0)
|
|
|
|
|
2017-03-12 13:31:56 -04:00
|
|
|
if b.config.RawSingleISOUrl != "" || len(b.config.ISOUrls) > 0 {
|
|
|
|
isoWarnings, isoErrs := b.config.ISOConfig.Prepare(&b.config.ctx)
|
|
|
|
warnings = append(warnings, isoWarnings...)
|
|
|
|
errs = packer.MultiErrorAppend(errs, isoErrs...)
|
|
|
|
}
|
2017-03-12 07:31:31 -04:00
|
|
|
|
2018-04-18 19:26:48 -04:00
|
|
|
errs = packer.MultiErrorAppend(errs, b.config.BootConfig.Prepare(&b.config.ctx)...)
|
2017-03-12 07:31:31 -04:00
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
|
|
|
if b.config.Cpu < 1 {
|
|
|
|
b.config.Cpu = 1
|
|
|
|
}
|
|
|
|
|
2017-03-12 13:31:56 -04:00
|
|
|
if b.config.CloneFromVMName == "" {
|
2018-07-19 13:53:42 -04:00
|
|
|
if b.config.CloneFromVMCXPath == "" {
|
2018-07-09 12:20:38 -04:00
|
|
|
errs = packer.MultiErrorAppend(errs, fmt.Errorf("The clone_from_vm_name must be specified if "+
|
2018-07-19 13:53:42 -04:00
|
|
|
"clone_from_vmcx_path is not specified."))
|
2017-05-21 12:29:26 -04:00
|
|
|
}
|
2017-03-12 13:31:56 -04:00
|
|
|
} else {
|
|
|
|
virtualMachineExists, err := powershell.DoesVirtualMachineExist(b.config.CloneFromVMName)
|
|
|
|
if err != nil {
|
2018-07-09 12:20:38 -04:00
|
|
|
errs = packer.MultiErrorAppend(errs, fmt.Errorf("Failed detecting if virtual machine to clone "+
|
|
|
|
"from exists: %s", err))
|
2017-03-12 13:31:56 -04:00
|
|
|
} else {
|
|
|
|
if !virtualMachineExists {
|
2018-07-09 12:20:38 -04:00
|
|
|
errs = packer.MultiErrorAppend(errs, fmt.Errorf("Virtual machine '%s' to clone from does not "+
|
|
|
|
"exist.", b.config.CloneFromVMName))
|
2017-03-12 13:31:56 -04:00
|
|
|
} else {
|
|
|
|
b.config.Generation, err = powershell.GetVirtualMachineGeneration(b.config.CloneFromVMName)
|
|
|
|
if err != nil {
|
2018-07-09 12:20:38 -04:00
|
|
|
errs = packer.MultiErrorAppend(errs, fmt.Errorf("Failed detecting virtual machine to clone "+
|
|
|
|
"from generation: %s", err))
|
2017-03-12 13:31:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if b.config.CloneFromSnapshotName != "" {
|
2018-07-09 12:20:38 -04:00
|
|
|
virtualMachineSnapshotExists, err := powershell.DoesVirtualMachineSnapshotExist(
|
|
|
|
b.config.CloneFromVMName, b.config.CloneFromSnapshotName)
|
2017-03-12 13:31:56 -04:00
|
|
|
if err != nil {
|
2018-07-09 12:20:38 -04:00
|
|
|
errs = packer.MultiErrorAppend(errs, fmt.Errorf("Failed detecting if virtual machine "+
|
|
|
|
"snapshot to clone from exists: %s", err))
|
2017-03-12 13:31:56 -04:00
|
|
|
} else {
|
|
|
|
if !virtualMachineSnapshotExists {
|
2018-07-09 12:20:38 -04:00
|
|
|
errs = packer.MultiErrorAppend(errs, fmt.Errorf("Virtual machine snapshot '%s' on "+
|
|
|
|
"virtual machine '%s' to clone from does not exist.",
|
|
|
|
b.config.CloneFromSnapshotName, b.config.CloneFromVMName))
|
2017-03-12 13:31:56 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
virtualMachineOn, err := powershell.IsVirtualMachineOn(b.config.CloneFromVMName)
|
|
|
|
if err != nil {
|
2018-07-09 12:20:38 -04:00
|
|
|
errs = packer.MultiErrorAppend(errs, fmt.Errorf("Failed detecting if virtual machine to "+
|
|
|
|
"clone is running: %s", err))
|
2017-03-12 13:31:56 -04:00
|
|
|
} else {
|
|
|
|
if virtualMachineOn {
|
|
|
|
warning := fmt.Sprintf("Cloning from a virtual machine that is running.")
|
|
|
|
warnings = appendWarnings(warnings, warning)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-05-29 20:16:03 -04:00
|
|
|
|
2018-07-19 13:53:42 -04:00
|
|
|
if b.config.CloneFromVMCXPath == "" {
|
2017-05-21 12:29:26 -04:00
|
|
|
if b.config.CloneFromVMName == "" {
|
2018-07-19 13:53:42 -04:00
|
|
|
errs = packer.MultiErrorAppend(errs, fmt.Errorf("The clone_from_vmcx_path be specified if "+
|
2018-07-09 12:20:38 -04:00
|
|
|
"clone_from_vm_name must is not specified."))
|
2017-05-21 12:29:26 -04:00
|
|
|
}
|
|
|
|
} else {
|
2018-07-19 13:53:42 -04:00
|
|
|
if _, err := os.Stat(b.config.CloneFromVMCXPath); os.IsNotExist(err) {
|
2017-05-21 12:29:26 -04:00
|
|
|
if err != nil {
|
|
|
|
errs = packer.MultiErrorAppend(
|
2018-07-19 13:53:42 -04:00
|
|
|
errs, fmt.Errorf("CloneFromVMCXPath does not exist: %s", err))
|
2017-05-21 12:29:26 -04:00
|
|
|
}
|
|
|
|
}
|
2019-06-24 13:56:32 -04:00
|
|
|
if strings.HasSuffix(strings.ToLower(b.config.CloneFromVMCXPath), ".vmcx") {
|
|
|
|
// User has provided the vmcx file itself rather than the containing
|
|
|
|
// folder.
|
|
|
|
if strings.Contains(b.config.CloneFromVMCXPath, "Virtual Machines") {
|
|
|
|
keep := strings.Split(b.config.CloneFromVMCXPath, "Virtual Machines")
|
|
|
|
b.config.CloneFromVMCXPath = keep[0]
|
|
|
|
} else {
|
|
|
|
errs = packer.MultiErrorAppend(errs, fmt.Errorf("Unable to "+
|
|
|
|
"parse the clone_from_vmcx_path to find the vm directory. "+
|
|
|
|
"Please provide the path to the folder containing the "+
|
|
|
|
"vmcx file, not the file itself. Example: instead of "+
|
|
|
|
"C:\\path\\to\\output-hyperv-iso\\Virtual Machines\\filename.vmcx"+
|
|
|
|
", provide C:\\path\\to\\output-hyperv-iso\\."))
|
|
|
|
}
|
|
|
|
}
|
2017-05-21 12:29:26 -04:00
|
|
|
}
|
2017-03-12 13:31:56 -04:00
|
|
|
|
2017-05-29 20:16:03 -04:00
|
|
|
if b.config.Generation < 1 || b.config.Generation > 2 {
|
2017-03-12 07:31:31 -04:00
|
|
|
b.config.Generation = 1
|
|
|
|
}
|
|
|
|
|
|
|
|
if b.config.Generation == 2 {
|
2017-06-19 16:22:55 -04:00
|
|
|
if len(b.config.FloppyFiles) > 0 || len(b.config.FloppyDirectories) > 0 {
|
2017-03-12 07:31:31 -04:00
|
|
|
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)
|
|
|
|
|
|
|
|
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" {
|
2018-07-09 12:20:38 -04:00
|
|
|
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, ", ")))
|
2017-03-12 07:31:31 -04:00
|
|
|
} else {
|
2018-07-09 12:20:38 -04:00
|
|
|
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, ", ")))
|
2017-03-12 07:31:31 -04:00
|
|
|
}
|
|
|
|
} else if b.config.Generation > 1 && len(b.config.SecondaryDvdImages) > 16 {
|
|
|
|
if b.config.GuestAdditionsMode == "attach" {
|
2018-07-09 12:20:38 -04:00
|
|
|
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, ", ")))
|
2017-03-12 07:31:31 -04:00
|
|
|
} else {
|
2018-07-09 12:20:38 -04:00
|
|
|
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, ", ")))
|
2017-03-12 07:31:31 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if b.config.EnableVirtualizationExtensions {
|
|
|
|
hasVirtualMachineVirtualizationExtensions, err := powershell.HasVirtualMachineVirtualizationExtensions()
|
|
|
|
if err != nil {
|
2018-07-09 12:20:38 -04:00
|
|
|
errs = packer.MultiErrorAppend(errs, fmt.Errorf("Failed detecting virtual machine virtualization "+
|
|
|
|
"extensions support: %s", err))
|
2017-03-12 07:31:31 -04:00
|
|
|
} else {
|
|
|
|
if !hasVirtualMachineVirtualizationExtensions {
|
2018-07-09 12:20:38 -04:00
|
|
|
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."))
|
2017-03-12 07:31:31 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Warnings
|
|
|
|
|
|
|
|
if b.config.ShutdownCommand == "" {
|
|
|
|
warnings = appendWarnings(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 {
|
2018-07-09 12:20:38 -04:00
|
|
|
warning = fmt.Sprintf("For nested virtualization, when virtualization extension is enabled, " +
|
|
|
|
"dynamic memory should not be allowed.")
|
2017-03-12 07:31:31 -04:00
|
|
|
warnings = appendWarnings(warnings, warning)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !b.config.EnableMacSpoofing {
|
2018-07-09 12:20:38 -04:00
|
|
|
warning = fmt.Sprintf("For nested virtualization, when virtualization extension is enabled, " +
|
|
|
|
"mac spoofing should be allowed.")
|
2017-03-12 07:31:31 -04:00
|
|
|
warnings = appendWarnings(warnings, warning)
|
|
|
|
}
|
|
|
|
|
|
|
|
if b.config.RamSize < MinNestedVirtualizationRamSize {
|
2018-07-09 12:20:38 -04:00
|
|
|
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.")
|
2017-03-12 07:31:31 -04:00
|
|
|
warnings = appendWarnings(warnings, warning)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if b.config.SwitchVlanId != "" {
|
|
|
|
if b.config.SwitchVlanId != b.config.VlanId {
|
2018-07-09 12:20:38 -04:00
|
|
|
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.")
|
2017-03-12 07:31:31 -04:00
|
|
|
warnings = appendWarnings(warnings, warning)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if errs != nil && len(errs.Errors) > 0 {
|
|
|
|
return warnings, errs
|
|
|
|
}
|
|
|
|
|
|
|
|
return warnings, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run executes a Packer build and returns a packer.Artifact representing
|
|
|
|
// a Hyperv appliance.
|
2019-03-22 09:53:28 -04:00
|
|
|
func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (packer.Artifact, error) {
|
2017-03-12 07:31:31 -04:00
|
|
|
// Create the driver that we'll use to communicate with Hyperv
|
|
|
|
driver, err := hypervcommon.NewHypervPS4Driver()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Failed creating Hyper-V driver: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set up the state.
|
|
|
|
state := new(multistep.BasicStateBag)
|
|
|
|
state.Put("config", &b.config)
|
|
|
|
state.Put("debug", b.config.PackerDebug)
|
|
|
|
state.Put("driver", driver)
|
|
|
|
state.Put("hook", hook)
|
|
|
|
state.Put("ui", ui)
|
|
|
|
|
|
|
|
steps := []multistep.Step{
|
2018-07-09 07:06:45 -04:00
|
|
|
&hypervcommon.StepCreateBuildDir{
|
|
|
|
TempPath: b.config.TempPath,
|
|
|
|
},
|
2019-01-11 18:06:36 -05:00
|
|
|
&common.StepOutputDir{
|
2017-03-12 07:31:31 -04:00
|
|
|
Force: b.config.PackerForce,
|
|
|
|
Path: b.config.OutputDir,
|
|
|
|
},
|
2017-03-12 19:51:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if b.config.RawSingleISOUrl != "" || len(b.config.ISOUrls) > 0 {
|
|
|
|
steps = append(steps,
|
|
|
|
&common.StepDownload{
|
|
|
|
Checksum: b.config.ISOChecksum,
|
|
|
|
ChecksumType: b.config.ISOChecksumType,
|
|
|
|
Description: "ISO",
|
|
|
|
ResultKey: "iso_path",
|
|
|
|
Url: b.config.ISOUrls,
|
|
|
|
Extension: b.config.TargetExtension,
|
|
|
|
TargetPath: b.config.TargetPath,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
steps = append(steps,
|
2017-03-12 07:31:31 -04:00
|
|
|
&common.StepCreateFloppy{
|
2017-09-11 13:11:45 -04:00
|
|
|
Files: b.config.FloppyFiles,
|
2017-06-19 16:22:55 -04:00
|
|
|
Directories: b.config.FloppyConfig.FloppyDirectories,
|
2019-09-12 08:25:22 -04:00
|
|
|
Label: b.config.FloppyConfig.FloppyLabel,
|
2017-03-12 07:31:31 -04:00
|
|
|
},
|
|
|
|
&common.StepHTTPServer{
|
|
|
|
HTTPDir: b.config.HTTPDir,
|
|
|
|
HTTPPortMin: b.config.HTTPPortMin,
|
|
|
|
HTTPPortMax: b.config.HTTPPortMax,
|
|
|
|
},
|
|
|
|
&hypervcommon.StepCreateSwitch{
|
|
|
|
SwitchName: b.config.SwitchName,
|
|
|
|
},
|
|
|
|
&hypervcommon.StepCloneVM{
|
2018-07-19 13:53:42 -04:00
|
|
|
CloneFromVMCXPath: b.config.CloneFromVMCXPath,
|
2017-03-12 07:31:31 -04:00
|
|
|
CloneFromVMName: b.config.CloneFromVMName,
|
|
|
|
CloneFromSnapshotName: b.config.CloneFromSnapshotName,
|
|
|
|
CloneAllSnapshots: b.config.CloneAllSnapshots,
|
|
|
|
VMName: b.config.VMName,
|
|
|
|
SwitchName: b.config.SwitchName,
|
2018-12-10 17:46:16 -05:00
|
|
|
CompareCopy: b.config.CompareCopy,
|
2017-03-12 07:31:31 -04:00
|
|
|
RamSize: b.config.RamSize,
|
|
|
|
Cpu: b.config.Cpu,
|
|
|
|
EnableMacSpoofing: b.config.EnableMacSpoofing,
|
|
|
|
EnableDynamicMemory: b.config.EnableDynamicMemory,
|
|
|
|
EnableSecureBoot: b.config.EnableSecureBoot,
|
2018-05-10 13:00:35 -04:00
|
|
|
SecureBootTemplate: b.config.SecureBootTemplate,
|
2017-03-12 07:31:31 -04:00
|
|
|
EnableVirtualizationExtensions: b.config.EnableVirtualizationExtensions,
|
2017-12-14 21:24:15 -05:00
|
|
|
MacAddress: b.config.MacAddress,
|
2019-04-12 18:59:09 -04:00
|
|
|
KeepRegistered: b.config.KeepRegistered,
|
2017-03-12 07:31:31 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
&hypervcommon.StepEnableIntegrationService{},
|
|
|
|
|
|
|
|
&hypervcommon.StepMountDvdDrive{
|
|
|
|
Generation: b.config.Generation,
|
|
|
|
},
|
|
|
|
&hypervcommon.StepMountFloppydrive{
|
|
|
|
Generation: b.config.Generation,
|
|
|
|
},
|
|
|
|
|
|
|
|
&hypervcommon.StepMountGuestAdditions{
|
|
|
|
GuestAdditionsMode: b.config.GuestAdditionsMode,
|
|
|
|
GuestAdditionsPath: b.config.GuestAdditionsPath,
|
|
|
|
Generation: b.config.Generation,
|
|
|
|
},
|
|
|
|
|
|
|
|
&hypervcommon.StepMountSecondaryDvdImages{
|
|
|
|
IsoPaths: b.config.SecondaryDvdImages,
|
|
|
|
Generation: b.config.Generation,
|
|
|
|
},
|
|
|
|
|
|
|
|
&hypervcommon.StepConfigureVlan{
|
|
|
|
VlanId: b.config.VlanId,
|
|
|
|
SwitchVlanId: b.config.SwitchVlanId,
|
|
|
|
},
|
|
|
|
|
2018-05-05 17:54:58 -04:00
|
|
|
&hypervcommon.StepRun{
|
2019-08-10 15:03:08 -04:00
|
|
|
Headless: b.config.Headless,
|
|
|
|
SwitchName: b.config.SwitchName,
|
2018-05-05 17:54:58 -04:00
|
|
|
},
|
2017-03-12 07:31:31 -04:00
|
|
|
|
|
|
|
&hypervcommon.StepTypeBootCommand{
|
2018-08-22 14:25:43 -04:00
|
|
|
BootCommand: b.config.FlatBootCommand(),
|
|
|
|
BootWait: b.config.BootWait,
|
|
|
|
SwitchName: b.config.SwitchName,
|
|
|
|
Ctx: b.config.ctx,
|
|
|
|
GroupInterval: b.config.BootConfig.BootGroupInterval,
|
2017-03-12 07:31:31 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
// configure the communicator ssh, winrm
|
|
|
|
&communicator.StepConnect{
|
|
|
|
Config: &b.config.SSHConfig.Comm,
|
2018-12-27 03:33:58 -05:00
|
|
|
Host: hypervcommon.CommHost(b.config.SSHConfig.Comm.SSHHost),
|
2018-08-22 11:02:23 -04:00
|
|
|
SSHConfig: b.config.SSHConfig.Comm.SSHConfigFunc(),
|
2017-03-12 07:31:31 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
// provision requires communicator to be setup
|
|
|
|
&common.StepProvision{},
|
|
|
|
|
2018-09-14 14:03:23 -04:00
|
|
|
// Remove ephemeral SSH keys, if using
|
|
|
|
&common.StepCleanupTempKeys{
|
|
|
|
Comm: &b.config.SSHConfig.Comm,
|
|
|
|
},
|
|
|
|
|
2017-03-12 07:31:31 -04:00
|
|
|
&hypervcommon.StepShutdown{
|
|
|
|
Command: b.config.ShutdownCommand,
|
|
|
|
Timeout: b.config.ShutdownTimeout,
|
|
|
|
},
|
|
|
|
|
|
|
|
// wait for the vm to be powered off
|
|
|
|
&hypervcommon.StepWaitForPowerOff{},
|
|
|
|
|
|
|
|
// remove the secondary dvd images
|
|
|
|
// after we power down
|
|
|
|
&hypervcommon.StepUnmountSecondaryDvdImages{},
|
|
|
|
&hypervcommon.StepUnmountGuestAdditions{},
|
|
|
|
&hypervcommon.StepUnmountDvdDrive{},
|
|
|
|
&hypervcommon.StepUnmountFloppyDrive{
|
|
|
|
Generation: b.config.Generation,
|
|
|
|
},
|
2018-07-07 07:42:22 -04:00
|
|
|
&hypervcommon.StepCompactDisk{
|
2017-03-12 07:31:31 -04:00
|
|
|
SkipCompaction: b.config.SkipCompaction,
|
2018-07-07 07:42:22 -04:00
|
|
|
},
|
2017-03-12 07:31:31 -04:00
|
|
|
&hypervcommon.StepExportVm{
|
2018-07-07 07:27:09 -04:00
|
|
|
OutputDir: b.config.OutputDir,
|
|
|
|
SkipExport: b.config.SkipExport,
|
2017-03-12 07:31:31 -04:00
|
|
|
},
|
2018-07-08 09:12:32 -04:00
|
|
|
&hypervcommon.StepCollateArtifacts{
|
|
|
|
OutputDir: b.config.OutputDir,
|
|
|
|
SkipExport: b.config.SkipExport,
|
2017-03-12 07:31:31 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
// the clean up actions for each step will be executed reverse order
|
2017-03-12 19:51:59 -04:00
|
|
|
)
|
2017-03-12 07:31:31 -04:00
|
|
|
|
|
|
|
// Run the steps.
|
2017-09-11 13:38:47 -04:00
|
|
|
b.runner = common.NewRunner(steps, b.config.PackerConfig, ui)
|
2019-03-22 09:53:28 -04:00
|
|
|
b.runner.Run(ctx, state)
|
2017-03-12 07:31:31 -04:00
|
|
|
|
|
|
|
// Report any errors.
|
|
|
|
if rawErr, ok := state.GetOk("error"); ok {
|
|
|
|
return nil, rawErr.(error)
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we were interrupted or cancelled, then just exit.
|
|
|
|
if _, ok := state.GetOk(multistep.StateCancelled); ok {
|
|
|
|
return nil, errors.New("Build was cancelled.")
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := state.GetOk(multistep.StateHalted); ok {
|
|
|
|
return nil, errors.New("Build was halted.")
|
|
|
|
}
|
|
|
|
|
|
|
|
return hypervcommon.NewArtifact(b.config.OutputDir)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
2019-03-29 18:52:41 -04:00
|
|
|
return fmt.Errorf("memory: Virtual machine requires memory size >= %v MB, but defined: %v",
|
2018-07-09 12:20:38 -04:00
|
|
|
MinRamSize, b.config.RamSize)
|
2017-03-12 07:31:31 -04:00
|
|
|
} else if b.config.RamSize > MaxRamSize {
|
2019-03-29 18:52:41 -04:00
|
|
|
return fmt.Errorf("memory: Virtual machine requires memory size <= %v MB, but defined: %v",
|
2018-07-09 12:20:38 -04:00
|
|
|
MaxRamSize, b.config.RamSize)
|
2017-03-12 07:31:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|