@hashicorp
The Hyper-V plugin can be used with HashiCorp Packer to create custom images from Hyper-V.
- Community
Updated 10 months ago
- GitHub(opens in new tab)
Hyper-V ISO
Type: hyperv-iso
Artifact BuilderId: MSOpenTech.hyperv
The Hyper-V Packer builder is able to create Hyper-V virtual machines and export them, starting from an ISO image.
The builder builds a virtual machine by creating a new virtual machine from scratch. Typically, the VM is booted, an OS is installed, and software is provisioned within the OS. Finally the VM is shut down. The result of the Hyper-V builder is a directory containing all the files necessary to run the virtual machine portably.
Basic Example
Here is a basic example. This example is not functional. It will start the OS installer but then fail because we don't provide the preseed file for Ubuntu to self-install. Still, the example serves to show the basic configuration:
{ "type": "hyperv-iso", "iso_url": "http://releases.ubuntu.com/12.04/ubuntu-12.04.5-server-amd64.iso", "iso_checksum": "md5:769474248a3897f4865817446f9a4a53", "ssh_username": "packer", "ssh_password": "packer", "shutdown_command": "echo 'packer' | sudo -S shutdown -P now"}
By default Packer will perform a hard power off of a virtual machine.
However, when a machine is powered off this way, it is possible that
changes made to the VMs file system may not be fully synced, possibly
leading to corruption of files or lost changes. As such, it is important to
add a shutdown_command
. This tells Packer how to safely shutdown and
power off the VM.
Configuration Reference
There are many configuration options available for the Hyper-V builder. They are organized below into two categories: required and optional. Within each category, the available options are alphabetized and described.
In addition to the options listed here, a communicator can be configured for this builder.
ISO Configuration Reference
By default, Packer will symlink, download or copy image files to the Packer
cache into a "hash($iso_url+$iso_checksum).$iso_target_extension
" file.
Packer uses hashicorp/go-getter in
file mode in order to perform a download.
go-getter supports the following protocols:
- Local files
- Git
- Mercurial
- HTTP
- Amazon S3
Examples:
go-getter can guess the checksum type based on iso_checksum
length, and it is
also possible to specify the checksum type.
In JSON:
"iso_checksum": "946a6077af6f5f95a51f82fdc44051c7aa19f9cfc5f737954845a6050543d7c2", "iso_url": "ubuntu.org/.../ubuntu-14.04.1-server-amd64.iso"
"iso_checksum": "file:ubuntu.org/..../ubuntu-14.04.1-server-amd64.iso.sum", "iso_url": "ubuntu.org/.../ubuntu-14.04.1-server-amd64.iso"
"iso_checksum": "file://./shasums.txt", "iso_url": "ubuntu.org/.../ubuntu-14.04.1-server-amd64.iso"
"iso_checksum": "file:./shasums.txt", "iso_url": "ubuntu.org/.../ubuntu-14.04.1-server-amd64.iso"
In HCL2:
iso_checksum = "946a6077af6f5f95a51f82fdc44051c7aa19f9cfc5f737954845a6050543d7c2" iso_url = "ubuntu.org/.../ubuntu-14.04.1-server-amd64.iso"
iso_checksum = "file:ubuntu.org/..../ubuntu-14.04.1-server-amd64.iso.sum" iso_url = "ubuntu.org/.../ubuntu-14.04.1-server-amd64.iso"
iso_checksum = "file://./shasums.txt" iso_url = "ubuntu.org/.../ubuntu-14.04.1-server-amd64.iso"
iso_checksum = "file:./shasums.txt", iso_url = "ubuntu.org/.../ubuntu-14.04.1-server-amd64.iso"
iso_checksum
(string) - The checksum for the ISO file or virtual hard drive file. The type of the checksum is specified within the checksum field as a prefix, ex: "md5:{$checksum}". The type of the checksum can also be omitted and Packer will try to infer it based on string length. Valid values are "none", "{$checksum}", "md5:{$checksum}", "sha1:{$checksum}", "sha256:{$checksum}", "sha512:{$checksum}" or "file:{$path}". Here is a list of valid checksum values:- md5:090992ba9fd140077b0661cb75f7ce13
- 090992ba9fd140077b0661cb75f7ce13
- sha1:ebfb681885ddf1234c18094a45bbeafd91467911
- ebfb681885ddf1234c18094a45bbeafd91467911
- sha256:ed363350696a726b7932db864dda019bd2017365c9e299627830f06954643f93
- ed363350696a726b7932db864dda019bd2017365c9e299627830f06954643f93
- file:http://releases.ubuntu.com/20.04/SHA256SUMS
- file:file://./local/path/file.sum
- file:./local/path/file.sum
- none Although the checksum will not be verified when it is set to "none", this is not recommended since these files can be very large and corruption does happen from time to time.
iso_url
(string) - A URL to the ISO containing the installation image or virtual hard drive (VHD or VHDX) file to clone.
Optional:
iso_urls
([]string) - Multiple URLs for the ISO to download. Packer will try these in order. If anything goes wrong attempting to download or while downloading a single URL, it will move on to the next. All URLs must point to the same file (same checksum). By default this is empty andiso_url
is used. Only one ofiso_url
oriso_urls
can be specified.iso_target_path
(string) - The path where the iso should be saved after download. By default will go in the packer cache, with a hash of the original filename and checksum as its name.iso_target_extension
(string) - The extension of the iso file after download. This defaults toiso
.
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 directorypacker
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.
disable_shutdown
(bool) - Packer normally halts the virtual machine after all provisioners have run when noshutdown_command
is defined. If this is set totrue
, Packer will not halt the virtual machine but will assume that the VM will shut itself down when it's done, via the preseed.cfg or your final provisioner. Packer will wait for a default of 5 minutes until the virtual machine is shutdown. The timeout can be changed using theshutdown_timeout
option.disk_size
(uint) - The size, in megabytes, of the hard disk to create for the VM. By default, this is 40 GB.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.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.use_fixed_vhd_format
(bool) - If true, creates the boot disk on the virtual machine as a fixed VHD format disk. The default is false, which creates a dynamic VHDX format disk. This option requires setting generation to 1, skip_compaction to true, and differencing_disk to false. Additionally, any value entered for 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.
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.enable_tpm
(bool) - If true enable a virtual TPM for the virtual machine. This defaults to false.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 topackerhv1234567
. 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. The resulting VM will be housed in a randomly generated folder under %TEMP% by default. You can set thetemp_path
variable to change the location of the folder.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 <output_directory>/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.first_boot_device
(string) - When configured, determines the device or device type that is given preferential treatment when choosing a boot device.For Generation 1:
IDE
CD
orDVD
Floppy
NET
For Generation 2:
IDE:x:y
SCSI:x:y
CD
orDVD
NET
boot_order
([]string) - When configured, the boot order determines the order of the devices from which to boot.The device name must be in the form of
SCSI:x:y
, for example, to boot from the first scsi device useSCSI:0:0
.NB You should also set
first_boot_device
(e.g.DVD
).NB Although the VM will have this initial boot order, the OS can change it, for example, Ubuntu 18.04 will modify the boot order to include itself as the first boot option.
NB This only works for Generation 2 machines.
HTTP Directory configuration
Packer will create an http server serving http_directory
when it is set, a
random free port will be selected and the architecture of the directory
referenced will be available in your builder.
Example usage from a builder:
wget http://{{ .HTTPIP }}:{{ .HTTPPort }}/foo/bar/preseed.cfg
Optional:
http_directory
(string) - Path to a directory to serve using an HTTP server. The files in this directory will be available over HTTP that will be requestable from the virtual machine. This is useful for hosting kickstart files and so on. By default this is an empty string, which means no HTTP server will be started. The address and port of the HTTP server will be available as variables inboot_command
. This is covered in more detail below.http_content
(map[string]string) - Key/Values to serve using an HTTP server.http_content
works like and conflicts withhttp_directory
. The keys represent the paths and the values contents, the keys must start with a slash, ex:/path/to/file
.http_content
is useful for hosting kickstart files and so on. By default this is empty, which means no HTTP server will be started. The address and port of the HTTP server will be available as variables inboot_command
. This is covered in more detail below. Example:http_content = { "/a/b" = file("http/b") "/foo/bar" = templatefile("${path.root}/preseed.cfg", { packages = ["nginx"] }) }
http_port_min
(int) - These are the minimum and maximum port to use for the HTTP server started to serve thehttp_directory
. Because Packer often runs in parallel, Packer will choose a randomly available port in this range to run the HTTP server. If you want to force the HTTP server to be on one port, make this minimum and maximum port the same. By default the values are8000
and9000
, respectively.http_port_max
(int) - HTTP Port Maxhttp_bind_address
(string) - This is the bind address for the HTTP server. Defaults to 0.0.0.0 so that it will work with any network interface.
Shutdown configuration reference
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
(duration string | ex: "1h5m2s") - 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).
Floppy configuration reference
A floppy can be made available for your build. 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. The summary size of the listed files
must not exceed 1.44 MB. The supported ways to move large files into the OS
are using http_directory
or the file
provisioner.
Optional:
floppy_files
([]string) - A list of files to place onto a floppy disk that is attached when the VM is booted. 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_dirs
([]string) - A list of directories to place onto the floppy disk recursively. This is similar to thefloppy_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 infloppy_files
.floppy_content
(map[string]string) - Key/Values to add to the floppy disk. The keys represent the paths, and the values contents. It can be used alongsidefloppy_files
orfloppy_dirs
, which is useful to add large files without loading them into memory. If any paths are specified by both, the contents infloppy_content
will take precedence.Usage example (HCL):
floppy_files = ["vendor-data"]floppy_content = { "meta-data" = jsonencode(local.instance_data) "user-data" = templatefile("user-data", { packages = ["nginx"] })}floppy_label = "cidata"
floppy_label
(string) - Floppy Label
CD configuration reference
cd_files
([]string) - A list of files to place onto a CD that is attached when the VM is booted. This can include either files or directories; any directories will be copied onto the CD recursively, preserving directory structure hierarchy. Symlinks will have the link's target copied into the directory tree on the CD where the symlink was. File globbing is allowed.Usage example (JSON):
"cd_files": ["./somedirectory/meta-data", "./somedirectory/user-data"],"cd_label": "cidata",
Usage example (HCL):
cd_files = ["./somedirectory/meta-data", "./somedirectory/user-data"]cd_label = "cidata"
The above will create a CD with two files, user-data and meta-data in the CD root. This specific example is how you would create a CD that can be used for an Ubuntu 20.04 autoinstall.
Since globbing is also supported,
cd_files = ["./somedirectory/*"]cd_label = "cidata"
Would also be an acceptable way to define the above cd. The difference between providing the directory with or without the glob is whether the directory itself or its contents will be at the CD root.
Use of this option assumes that you have a command line tool installed that can handle the iso creation. Packer will use one of the following tools:
- xorriso
- mkisofs
- hdiutil (normally found in macOS)
- oscdimg (normally found in Windows as part of the Windows ADK)
cd_content
(map[string]string) - Key/Values to add to the CD. The keys represent the paths, and the values contents. It can be used alongsidecd_files
, which is useful to add large files without loading them into memory. If any paths are specified by both, the contents incd_content
will take precedence.Usage example (HCL):
cd_files = ["vendor-data"]cd_content = { "meta-data" = jsonencode(local.instance_data) "user-data" = templatefile("user-data", { packages = ["nginx"] })}cd_label = "cidata"
cd_label
(string) - CD Label
Communicator configuration reference
Optional common fields:
communicator
(string) - Packer currently supports three kinds of communicators:none
- No communicator will be used. If this is set, most provisioners also can't be used.ssh
- An SSH connection will be established to the machine. This is usually the default.winrm
- A WinRM connection will be established.
In addition to the above, some builders have custom communicators they can use. For example, the Docker builder has a "docker" communicator that uses
docker exec
anddocker cp
to execute scripts and copy files.pause_before_connecting
(duration string | ex: "1h5m2s") - We recommend that you enable SSH or WinRM as the very last step in your guest's bootstrap script, but sometimes you may have a race condition where you need Packer to wait before attempting to connect to your guest.If you end up in this situation, you can use the template option
pause_before_connecting
. By default, there is no pause. For example if you setpause_before_connecting
to10m
Packer will check whether it can connect, as normal. But once a connection attempt is successful, it will disconnect and then wait 10 minutes before connecting to the guest and beginning provisioning.
Optional SSH fields:
ssh_host
(string) - The address to SSH to. This usually is automatically configured by the builder.ssh_port
(int) - The port to connect to SSH. This defaults to22
.ssh_username
(string) - The username to connect to SSH with. Required if using SSH.ssh_password
(string) - A plaintext password to use to authenticate with SSH.ssh_ciphers
([]string) - This overrides the value of ciphers supported by default by Golang. The default value is [ "aes128-gcm@openssh.com", "chacha20-poly1305@openssh.com", "aes128-ctr", "aes192-ctr", "aes256-ctr", ]Valid options for ciphers include: "aes128-ctr", "aes192-ctr", "aes256-ctr", "aes128-gcm@openssh.com", "chacha20-poly1305@openssh.com", "arcfour256", "arcfour128", "arcfour", "aes128-cbc", "3des-cbc",
ssh_clear_authorized_keys
(bool) - If true, Packer will attempt to remove its temporary key from~/.ssh/authorized_keys
and/root/.ssh/authorized_keys
. This is a mostly cosmetic option, since Packer will delete the temporary private key from the host system regardless of whether this is set to true (unless the user has set the-debug
flag). Defaults to "false"; currently only works on guests withsed
installed.ssh_key_exchange_algorithms
([]string) - If set, Packer will override the value of key exchange (kex) algorithms supported by default by Golang. Acceptable values include: "curve25519-sha256@libssh.org", "ecdh-sha2-nistp256", "ecdh-sha2-nistp384", "ecdh-sha2-nistp521", "diffie-hellman-group14-sha1", and "diffie-hellman-group1-sha1".ssh_certificate_file
(string) - Path to user certificate used to authenticate with SSH. The~
can be used in path and will be expanded to the home directory of current user.ssh_pty
(bool) - Iftrue
, a PTY will be requested for the SSH connection. This defaults tofalse
.ssh_timeout
(duration string | ex: "1h5m2s") - The time to wait for SSH to become available. Packer uses this to determine when the machine has booted so this is usually quite long. Example value:10m
. This defaults to5m
, unlessssh_handshake_attempts
is set.ssh_disable_agent_forwarding
(bool) - If true, SSH agent forwarding will be disabled. Defaults tofalse
.ssh_handshake_attempts
(int) - The number of handshakes to attempt with SSH once it can connect. This defaults to10
, unless assh_timeout
is set.ssh_bastion_host
(string) - A bastion host to use for the actual SSH connection.ssh_bastion_port
(int) - The port of the bastion host. Defaults to22
.ssh_bastion_agent_auth
(bool) - Iftrue
, the local SSH agent will be used to authenticate with the bastion host. Defaults tofalse
.ssh_bastion_username
(string) - The username to connect to the bastion host.ssh_bastion_password
(string) - The password to use to authenticate with the bastion host.ssh_bastion_interactive
(bool) - Iftrue
, the keyboard-interactive used to authenticate with bastion host.ssh_bastion_private_key_file
(string) - Path to a PEM encoded private key file to use to authenticate with the bastion host. The~
can be used in path and will be expanded to the home directory of current user.ssh_bastion_certificate_file
(string) - Path to user certificate used to authenticate with bastion host. The~
can be used in path and will be expanded to the home directory of current user.ssh_file_transfer_method
(string) -scp
orsftp
- How to transfer files, Secure copy (default) or SSH File Transfer Protocol.NOTE: Guests using Windows with Win32-OpenSSH v9.1.0.0p1-Beta, scp (the default protocol for copying data) returns a a non-zero error code since the MOTW cannot be set, which cause any file transfer to fail. As a workaround you can override the transfer protocol with SFTP instead
ssh_file_transfer_protocol = "sftp"
.ssh_proxy_host
(string) - A SOCKS proxy host to use for SSH connectionssh_proxy_port
(int) - A port of the SOCKS proxy. Defaults to1080
.ssh_proxy_username
(string) - The optional username to authenticate with the proxy server.ssh_proxy_password
(string) - The optional password to use to authenticate with the proxy server.ssh_keep_alive_interval
(duration string | ex: "1h5m2s") - How often to send "keep alive" messages to the server. Set to a negative value (-1s
) to disable. Example value:10s
. Defaults to5s
.ssh_read_write_timeout
(duration string | ex: "1h5m2s") - The amount of time to wait for a remote command to end. This might be useful if, for example, packer hangs on a connection after a reboot. Example:5m
. Disabled by default.ssh_remote_tunnels
([]string) -ssh_local_tunnels
([]string) -
ssh_private_key_file
(string) - Path to a PEM encoded private key file to use to authenticate with SSH. The~
can be used in path and will be expanded to the home directory of current user.
Optional WinRM fields:
winrm_username
(string) - The username to use to connect to WinRM.winrm_password
(string) - The password to use to connect to WinRM.winrm_host
(string) - The address for WinRM to connect to.NOTE: If using an Amazon EBS builder, you can specify the interface WinRM connects to via
ssh_interface
winrm_no_proxy
(bool) - Setting this totrue
adds the remotehost:port
to theNO_PROXY
environment variable. This has the effect of bypassing any configured proxies when connecting to the remote host. Default tofalse
.winrm_port
(int) - The WinRM port to connect to. This defaults to5985
for plain unencrypted connection and5986
for SSL whenwinrm_use_ssl
is set to true.winrm_timeout
(duration string | ex: "1h5m2s") - The amount of time to wait for WinRM to become available. This defaults to30m
since setting up a Windows machine generally takes a long time.winrm_use_ssl
(bool) - Iftrue
, use HTTPS for WinRM.winrm_insecure
(bool) - Iftrue
, do not check server certificate chain and host name.winrm_use_ntlm
(bool) - Iftrue
, NTLMv2 authentication (with session security) will be used for WinRM, rather than default (basic authentication), removing the requirement for basic authentication to be enabled within the target guest. Further reading for remote connection authentication can be found here.
Boot Configuration Reference
The boot configuration is very important: boot_command
specifies the keys
to type when the virtual machine is first booted in order to start the OS
installer. This command is typed after boot_wait, which gives the virtual
machine some time to actually load.
The boot_command is an array of strings. The strings are all typed in sequence. It is an array only to improve readability within the template.
There are a set of special keys available. If these are in your boot command, they will be replaced by the proper key:
<bs>
- Backspace<del>
- Delete<enter> <return>
- Simulates an actual "enter" or "return" keypress.<esc>
- Simulates pressing the escape key.<tab>
- Simulates pressing the tab key.<f1> - <f12>
- Simulates pressing a function key.<up> <down> <left> <right>
- Simulates pressing an arrow key.<spacebar>
- Simulates pressing the spacebar.<insert>
- Simulates pressing the insert key.<home> <end>
- Simulates pressing the home and end keys.<pageUp> <pageDown>
- Simulates pressing the page up and page down keys.<menu>
- Simulates pressing the Menu key.<leftAlt> <rightAlt>
- Simulates pressing the alt key.<leftCtrl> <rightCtrl>
- Simulates pressing the ctrl key.<leftShift> <rightShift>
- Simulates pressing the shift key.<leftSuper> <rightSuper>
- Simulates pressing the ⌘ or Windows key.<wait> <wait5> <wait10>
- Adds a 1, 5 or 10 second pause before sending any additional keys. This is useful if you have to generally wait for the UI to update before typing more.<waitXX>
- Add an arbitrary pause before sending any additional keys. The format ofXX
is a sequence of positive decimal numbers, each with optional fraction and a unit suffix, such as300ms
,1.5h
or2h45m
. Valid time units arens
,us
(orµs
),ms
,s
,m
,h
. For example<wait10m>
or<wait1m20s>
.<XXXOn> <XXXOff>
- Any printable keyboard character, and of these "special" expressions, with the exception of the<wait>
types, can also be toggled on or off. For example, to simulate ctrl+c, use<leftCtrlOn>c<leftCtrlOff>
. Be sure to release them, otherwise they will be held down until the machine reboots. To hold thec
key down, you would use<cOn>
. Likewise,<cOff>
to release.{{ .HTTPIP }} {{ .HTTPPort }}
- The IP and port, respectively of an HTTP server that is started serving the directory specified by thehttp_directory
configuration parameter. Ifhttp_directory
isn't specified, these will be blank!{{ .Name }}
- The name of the VM.
Example boot command. This is actually a working boot command used to start an CentOS 6.4 installer:
In JSON:
"boot_command": [ "<tab><wait>", " ks=http://{{ .HTTPIP }}:{{ .HTTPPort }}/centos6-ks.cfg<enter>" ]
In HCL2:
boot_command = [ "<tab><wait>", " ks=http://{{ .HTTPIP }}:{{ .HTTPPort }}/centos6-ks.cfg<enter>" ]
The example shown below is a working boot command used to start an Ubuntu 12.04 installer:
In JSON:
"boot_command": [ "<esc><esc><enter><wait>", "/install/vmlinuz noapic ", "preseed/url=http://{{ .HTTPIP }}:{{ .HTTPPort }}/preseed.cfg ", "debian-installer=en_US auto locale=en_US kbd-chooser/method=us ", "hostname={{ .Name }} ", "fb=false debconf/frontend=noninteractive ", "keyboard-configuration/modelcode=SKIP keyboard-configuration/layout=USA ", "keyboard-configuration/variant=USA console-setup/ask_detect=false ", "initrd=/install/initrd.gz -- <enter>" ]
In HCL2:
boot_command = [ "<esc><esc><enter><wait>", "/install/vmlinuz noapic ", "preseed/url=http://{{ .HTTPIP }}:{{ .HTTPPort }}/preseed.cfg ", "debian-installer=en_US auto locale=en_US kbd-chooser/method=us ", "hostname={{ .Name }} ", "fb=false debconf/frontend=noninteractive ", "keyboard-configuration/modelcode=SKIP keyboard-configuration/layout=USA ", "keyboard-configuration/variant=USA console-setup/ask_detect=false ", "initrd=/install/initrd.gz -- <enter>" ]
For more examples of various boot commands, see the sample projects from our community templates page.
Optional:
boot_keygroup_interval
(duration string | ex: "1h5m2s") - Time to wait after sending a group of key pressses. The value of this should be a duration. Examples are5s
and1m30s
which will cause Packer to wait five seconds and one minute 30 seconds, respectively. If this isn't specified, a sensible default value is picked depending on the builder type.boot_wait
(duration string | ex: "1h5m2s") - The time to wait after booting the initial virtual machine before typing theboot_command
. The value of this should be a duration. Examples are5s
and1m30s
which will cause Packer to wait five seconds and one minute 30 seconds, respectively. If this isn't specified, the default is10s
or 10 seconds. To set boot_wait to 0s, use a negative number, such as "-1s"boot_command
([]string) - 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.
Integration Services
Packer will automatically attach the integration services ISO as a DVD drive for the version of Hyper-V that is running.
Generation 1 vs Generation 2
Floppy drives are no longer supported by generation 2 machines. This requires you to take another approach when dealing with preseed or answer files. Two possible options are using your own virtual DVD drives, the cd_files option, or using Packer's built in web server.
When dealing with Windows you need to enable UEFI drives for generation 2 virtual machines.
Creating an ISO From a Directory
Programs like mkisofs can be used to create an ISO from a directory. There is a windows version of mkisofs available.
Below is a working PowerShell script that can be used to create a Windows answer ISO:
$isoFolder = "answer-iso"if (test-path $isoFolder){ remove-item $isoFolder -Force -Recurse} if (test-path windows\windows-2012R2-serverdatacenter-amd64\answer.iso){ remove-item windows\windows-2012R2-serverdatacenter-amd64\answer.iso -Force} mkdir $isoFolder copy windows\windows-2012R2-serverdatacenter-amd64\Autounattend.xml $isoFolder\copy windows\windows-2012R2-serverdatacenter-amd64\sysprep-unattend.xml $isoFolder\copy windows\common\set-power-config.ps1 $isoFolder\copy windows\common\microsoft-updates.ps1 $isoFolder\copy windows\common\win-updates.ps1 $isoFolder\copy windows\common\run-sysprep.ps1 $isoFolder\copy windows\common\run-sysprep.cmd $isoFolder\ $textFile = "$isoFolder\Autounattend.xml" $c = Get-Content -Encoding UTF8 $textFile # Enable UEFI and disable Non EUFI$c | % { $_ -replace '<!-- Start Non UEFI -->','<!-- Start Non UEFI' } | % { $_ -replace '<!-- Finish Non UEFI -->','Finish Non UEFI -->' } | % { $_ -replace '<!-- Start UEFI compatible','<!-- Start UEFI compatible -->' } | % { $_ -replace 'Finish UEFI compatible -->','<!-- Finish UEFI compatible -->' } | sc -Path $textFile & .\mkisofs.exe -r -iso-level 4 -UDF -o windows\windows-2012R2-serverdatacenter-amd64\answer.iso $isoFolder if (test-path $isoFolder){ remove-item $isoFolder -Force -Recurse}
Example For Windows Server 2012 R2 Generation 2
Packer config:
{ "builders": [ { "vm_name": "windows2012r2", "type": "hyperv-iso", "disk_size": 61440, "floppy_files": [], "secondary_iso_images": [ "./windows/windows-2012R2-serverdatacenter-amd64/answer.iso" ], "http_directory": "./windows/common/http/", "boot_wait": "0s", "boot_command": ["a<wait>a<wait>a"], "iso_url": "http://download.microsoft.com/download/6/2/A/62A76ABB-9990-4EFC-A4FE-C7D698DAEB96/9600.16384.WINBLUE_RTM.130821-1623_X64FRE_SERVER_EVAL_EN-US-IRM_SSS_X64FREE_EN-US_DV5.ISO", "iso_checksum": "md5:458ff91f8abc21b75cb544744bf92e6a", "communicator": "winrm", "winrm_username": "vagrant", "winrm_password": "vagrant", "winrm_timeout": "4h", "shutdown_command": "f:\\run-sysprep.cmd", "memory": 4096, "cpus": 4, "generation": 2, "switch_name": "LAN", "enable_secure_boot": true } ], "provisioners": [ { "type": "powershell", "elevated_user": "vagrant", "elevated_password": "vagrant", "scripts": [ "./windows/common/install-7zip.ps1", "./windows/common/install-chef.ps1", "./windows/common/compile-dotnet-assemblies.ps1", "./windows/common/cleanup.ps1", "./windows/common/ultradefrag.ps1", "./windows/common/sdelete.ps1" ] } ], "post-processors": [ { "type": "vagrant", "keep_input_artifact": false, "output": "{{.Provider}}_windows-2012r2_chef.box" } ]}
autounattend.xml:
<?xml version="1.0" encoding="utf-8"?><unattend xmlns="urn:schemas-microsoft-com:unattend"> <settings pass="windowsPE"> <component name="Microsoft-Windows-International-Core-WinPE" processorArchitecture="amd64" publicKeyToken="31bf3856ad364e35" language="neutral" versionScope="nonSxS" xmlns:wcm="http://schemas.microsoft.com/WMIConfig/2002/State" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <SetupUILanguage> <UILanguage>en-US</UILanguage> </SetupUILanguage> <InputLocale>en-US</InputLocale> <SystemLocale>en-US</SystemLocale> <UILanguage>en-US</UILanguage> <UILanguageFallback>en-US</UILanguageFallback> <UserLocale>en-US</UserLocale> </component> <component name="Microsoft-Windows-Setup" processorArchitecture="amd64" publicKeyToken="31bf3856ad364e35" language="neutral" versionScope="nonSxS" xmlns:wcm="http://schemas.microsoft.com/WMIConfig/2002/State" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <!-- Start Non UEFI --> <DiskConfiguration> <Disk wcm:action="add"> <CreatePartitions> <CreatePartition wcm:action="add"> <Type>Primary</Type> <Order>1</Order> <Size>350</Size> </CreatePartition> <CreatePartition wcm:action="add"> <Order>2</Order> <Type>Primary</Type> <Extend>true</Extend> </CreatePartition> </CreatePartitions> <ModifyPartitions> <ModifyPartition wcm:action="add"> <Active>true</Active> <Format>NTFS</Format> <Label>boot</Label> <Order>1</Order> <PartitionID>1</PartitionID> </ModifyPartition> <ModifyPartition wcm:action="add"> <Format>NTFS</Format> <Label>Windows 2012 R2</Label> <Letter>C</Letter> <Order>2</Order> <PartitionID>2</PartitionID> </ModifyPartition> </ModifyPartitions> <DiskID>0</DiskID> <WillWipeDisk>true</WillWipeDisk> </Disk> </DiskConfiguration> <ImageInstall> <OSImage> <InstallFrom> <MetaData wcm:action="add"> <Key>/IMAGE/NAME </Key> <Value>Windows Server 2012 R2 SERVERSTANDARD</Value> </MetaData> </InstallFrom> <InstallTo> <DiskID>0</DiskID> <PartitionID>2</PartitionID> </InstallTo> </OSImage> </ImageInstall> <!-- Finish Non UEFI --> <!-- Start UEFI compatible <DiskConfiguration> <Disk wcm:action="add"> <CreatePartitions> <CreatePartition wcm:action="add"> <Order>1</Order> <Size>300</Size> <Type>Primary</Type> </CreatePartition> <CreatePartition wcm:action="add"> <Order>2</Order> <Size>100</Size> <Type>EFI</Type> </CreatePartition> <CreatePartition wcm:action="add"> <Order>3</Order> <Size>128</Size> <Type>MSR</Type> </CreatePartition> <CreatePartition wcm:action="add"> <Order>4</Order> <Extend>true</Extend> <Type>Primary</Type> </CreatePartition> </CreatePartitions> <ModifyPartitions> <ModifyPartition wcm:action="add"> <Order>1</Order> <PartitionID>1</PartitionID> <Label>WINRE</Label> <Format>NTFS</Format> <TypeID>de94bba4-06d1-4d40-a16a-bfd50179d6ac</TypeID> </ModifyPartition> <ModifyPartition wcm:action="add"> <Order>2</Order> <PartitionID>2</PartitionID> <Label>System</Label> <Format>FAT32</Format> </ModifyPartition> <ModifyPartition wcm:action="add"> <Order>3</Order> <PartitionID>3</PartitionID> </ModifyPartition> <ModifyPartition wcm:action="add"> <Order>4</Order> <PartitionID>4</PartitionID> <Label>Windows</Label> <Format>NTFS</Format> </ModifyPartition> </ModifyPartitions> <DiskID>0</DiskID> <WillWipeDisk>true</WillWipeDisk> </Disk> <WillShowUI>OnError</WillShowUI> </DiskConfiguration> <ImageInstall> <OSImage> <InstallFrom> <MetaData wcm:action="add"> <Key>/IMAGE/NAME </Key> <Value>Windows Server 2012 R2 SERVERSTANDARD</Value> </MetaData> </InstallFrom> <InstallTo> <DiskID>0</DiskID> <PartitionID>4</PartitionID> </InstallTo> </OSImage> </ImageInstall> Finish UEFI compatible --> <UserData> <!-- Product Key from http://technet.microsoft.com/en-us/library/jj612867.aspx --> <ProductKey> <!-- Do not uncomment the Key element if you are using trial ISOs --> <!-- You must uncomment the Key element (and optionally insert your own key) if you are using retail or volume license ISOs --> <!--<Key>D2N9P-3P6X9-2R39C-7RTCD-MDVJX</Key>--> <WillShowUI>OnError</WillShowUI> </ProductKey> <AcceptEula>true</AcceptEula> <FullName>Vagrant</FullName> <Organization>Vagrant</Organization> </UserData> </component> </settings> <settings pass="specialize"> <component name="Microsoft-Windows-Shell-Setup" processorArchitecture="amd64" publicKeyToken="31bf3856ad364e35" language="neutral" versionScope="nonSxS" xmlns:wcm="http://schemas.microsoft.com/WMIConfig/2002/State" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <OEMInformation> <HelpCustomized>false</HelpCustomized> </OEMInformation> <ComputerName>vagrant-2012r2</ComputerName> <TimeZone>Coordinated Universal Time</TimeZone> <RegisteredOwner /> </component> <component name="Microsoft-Windows-ServerManager-SvrMgrNc" processorArchitecture="amd64" publicKeyToken="31bf3856ad364e35" language="neutral" versionScope="nonSxS" xmlns:wcm="http://schemas.microsoft.com/WMIConfig/2002/State" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <DoNotOpenServerManagerAtLogon>true</DoNotOpenServerManagerAtLogon> </component> <component name="Microsoft-Windows-IE-ESC" processorArchitecture="amd64" publicKeyToken="31bf3856ad364e35" language="neutral" versionScope="nonSxS" xmlns:wcm="http://schemas.microsoft.com/WMIConfig/2002/State" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <IEHardenAdmin>false</IEHardenAdmin> <IEHardenUser>false</IEHardenUser> </component> <component name="Microsoft-Windows-OutOfBoxExperience" processorArchitecture="amd64" publicKeyToken="31bf3856ad364e35" language="neutral" versionScope="nonSxS" xmlns:wcm="http://schemas.microsoft.com/WMIConfig/2002/State" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <DoNotOpenInitialConfigurationTasksAtLogon>true</DoNotOpenInitialConfigurationTasksAtLogon> </component> <component name="Microsoft-Windows-Security-SPP-UX" processorArchitecture="amd64" publicKeyToken="31bf3856ad364e35" language="neutral" versionScope="nonSxS" xmlns:wcm="http://schemas.microsoft.com/WMIConfig/2002/State" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <SkipAutoActivation>true</SkipAutoActivation> </component> </settings> <settings pass="oobeSystem"><!-- Start Setup cache proxy during installation <component name="Microsoft-Windows-IE-ClientNetworkProtocolImplementation" processorArchitecture="amd64" publicKeyToken="31bf3856ad364e35" language="neutral" versionScope="nonSxS" xmlns:wcm="http://schemas.microsoft.com/WMIConfig/2002/State" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <POLICYProxySettingsPerUser>0</POLICYProxySettingsPerUser> <HKLMProxyEnable>true</HKLMProxyEnable> <HKLMProxyServer>cache-proxy:3142</HKLMProxyServer> </component>Finish Setup cache proxy during installation --> <component name="Microsoft-Windows-Shell-Setup" processorArchitecture="amd64" publicKeyToken="31bf3856ad364e35" language="neutral" versionScope="nonSxS" xmlns:wcm="http://schemas.microsoft.com/WMIConfig/2002/State" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <AutoLogon> <Password> <Value>vagrant</Value> <PlainText>true</PlainText> </Password> <Enabled>true</Enabled> <Username>vagrant</Username> </AutoLogon> <FirstLogonCommands> <SynchronousCommand wcm:action="add"> <CommandLine>cmd.exe /c powershell -Command "Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Force"</CommandLine> <Description>Set Execution Policy 64 Bit</Description> <Order>1</Order> <RequiresUserInput>true</RequiresUserInput> </SynchronousCommand> <SynchronousCommand wcm:action="add"> <CommandLine>C:\Windows\SysWOW64\cmd.exe /c powershell -Command "Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Force"</CommandLine> <Description>Set Execution Policy 32 Bit</Description> <Order>2</Order> <RequiresUserInput>true</RequiresUserInput> </SynchronousCommand> <SynchronousCommand wcm:action="add"> <CommandLine>cmd.exe /c winrm quickconfig -q</CommandLine> <Description>winrm quickconfig -q</Description> <Order>3</Order> <RequiresUserInput>true</RequiresUserInput> </SynchronousCommand> <SynchronousCommand wcm:action="add"> <CommandLine>cmd.exe /c winrm quickconfig -transport:http</CommandLine> <Description>winrm quickconfig -transport:http</Description> <Order>4</Order> <RequiresUserInput>true</RequiresUserInput> </SynchronousCommand> <SynchronousCommand wcm:action="add"> <CommandLine>cmd.exe /c winrm set winrm/config @{MaxTimeoutms="1800000"}</CommandLine> <Description>Win RM MaxTimeoutms</Description> <Order>5</Order> <RequiresUserInput>true</RequiresUserInput> </SynchronousCommand> <SynchronousCommand wcm:action="add"> <CommandLine>cmd.exe /c winrm set winrm/config/winrs @{MaxMemoryPerShellMB="300"}</CommandLine> <Description>Win RM MaxMemoryPerShellMB</Description> <Order>6</Order> <RequiresUserInput>true</RequiresUserInput> </SynchronousCommand> <SynchronousCommand wcm:action="add"> <CommandLine>cmd.exe /c winrm set winrm/config/service @{AllowUnencrypted="true"}</CommandLine> <Description>Win RM AllowUnencrypted</Description> <Order>7</Order> <RequiresUserInput>true</RequiresUserInput> </SynchronousCommand> <SynchronousCommand wcm:action="add"> <CommandLine>cmd.exe /c winrm set winrm/config/service/auth @{Basic="true"}</CommandLine> <Description>Win RM auth Basic</Description> <Order>8</Order> <RequiresUserInput>true</RequiresUserInput> </SynchronousCommand> <SynchronousCommand wcm:action="add"> <CommandLine>cmd.exe /c winrm set winrm/config/client/auth @{Basic="true"}</CommandLine> <Description>Win RM client auth Basic</Description> <Order>9</Order> <RequiresUserInput>true</RequiresUserInput> </SynchronousCommand> <SynchronousCommand wcm:action="add"> <CommandLine>cmd.exe /c winrm set winrm/config/listener?Address=*+Transport=HTTP @{Port="5985"} </CommandLine> <Description>Win RM listener Address/Port</Description> <Order>10</Order> <RequiresUserInput>true</RequiresUserInput> </SynchronousCommand> <SynchronousCommand wcm:action="add"> <CommandLine>cmd.exe /c netsh advfirewall firewall set rule group="remote administration" new enable=yes </CommandLine> <Description>Win RM adv firewall enable</Description> <Order>11</Order> <RequiresUserInput>true</RequiresUserInput> </SynchronousCommand> <SynchronousCommand wcm:action="add"> <CommandLine>cmd.exe /c netsh advfirewall firewall add rule name="WinRM 5985" protocol=TCP dir=in localport=5985 action=allow</CommandLine> <Description>Win RM port open</Description> <Order>12</Order> <RequiresUserInput>true</RequiresUserInput> </SynchronousCommand> <SynchronousCommand wcm:action="add"> <CommandLine>cmd.exe /c netsh advfirewall firewall add rule name="WinRM 5986" protocol=TCP dir=in localport=5986 action=allow</CommandLine> <Description>Win RM port open</Description> <Order>13</Order> <RequiresUserInput>true</RequiresUserInput> </SynchronousCommand> <SynchronousCommand wcm:action="add"> <CommandLine>cmd.exe /c net stop winrm </CommandLine> <Description>Stop Win RM Service </Description> <Order>14</Order> <RequiresUserInput>true</RequiresUserInput> </SynchronousCommand> <SynchronousCommand wcm:action="add"> <CommandLine>cmd.exe /c sc config winrm start= disabled</CommandLine> <Description>Win RM Autostart</Description> <Order>15</Order> <RequiresUserInput>true</RequiresUserInput> </SynchronousCommand> <SynchronousCommand wcm:action="add"> <CommandLine>%SystemRoot%\System32\reg.exe ADD HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Advanced\ /v HideFileExt /t REG_DWORD /d 0 /f</CommandLine> <Order>16</Order> <Description>Show file extensions in Explorer</Description> </SynchronousCommand> <SynchronousCommand wcm:action="add"> <CommandLine>%SystemRoot%\System32\reg.exe ADD HKCU\Console /v QuickEdit /t REG_DWORD /d 1 /f</CommandLine> <Order>17</Order> <Description>Enable QuickEdit mode</Description> </SynchronousCommand> <SynchronousCommand wcm:action="add"> <CommandLine>%SystemRoot%\System32\reg.exe ADD HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Advanced\ /v Start_ShowRun /t REG_DWORD /d 1 /f</CommandLine> <Order>18</Order> <Description>Show Run command in Start Menu</Description> </SynchronousCommand> <SynchronousCommand wcm:action="add"> <CommandLine>%SystemRoot%\System32\reg.exe ADD HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Advanced\ /v StartMenuAdminTools /t REG_DWORD /d 1 /f</CommandLine> <Order>19</Order> <Description>Show Administrative Tools in Start Menu</Description> </SynchronousCommand> <SynchronousCommand wcm:action="add"> <CommandLine>%SystemRoot%\System32\reg.exe ADD HKLM\SYSTEM\CurrentControlSet\Control\Power\ /v HibernateFileSizePercent /t REG_DWORD /d 0 /f</CommandLine> <Order>20</Order> <Description>Zero Hibernation File</Description> </SynchronousCommand> <SynchronousCommand wcm:action="add"> <CommandLine>%SystemRoot%\System32\reg.exe ADD HKLM\SYSTEM\CurrentControlSet\Control\Power\ /v HibernateEnabled /t REG_DWORD /d 0 /f</CommandLine> <Order>21</Order> <Description>Disable Hibernation Mode</Description> </SynchronousCommand> <SynchronousCommand wcm:action="add"> <CommandLine>cmd.exe /c wmic useraccount where "name='vagrant'" set PasswordExpires=FALSE</CommandLine> <Order>22</Order> <Description>Disable password expiration for vagrant user</Description> </SynchronousCommand> <SynchronousCommand wcm:action="add"> <CommandLine>cmd.exe /c winrm set winrm/config/winrs @{MaxShellsPerUser="30"}</CommandLine> <Description>Win RM MaxShellsPerUser</Description> <Order>23</Order> <RequiresUserInput>true</RequiresUserInput> </SynchronousCommand> <SynchronousCommand wcm:action="add"> <CommandLine>cmd.exe /c winrm set winrm/config/winrs @{MaxProcessesPerShell="25"}</CommandLine> <Description>Win RM MaxProcessesPerShell</Description> <Order>24</Order> <RequiresUserInput>true</RequiresUserInput> </SynchronousCommand> <SynchronousCommand wcm:action="add"> <CommandLine>%SystemRoot%\System32\reg.exe ADD "HKLM\System\CurrentControlSet\Services\Netlogon\Parameters" /v DisablePasswordChange /t REG_DWORD /d 1 /f</CommandLine> <Description>Turn off computer password</Description> <Order>25</Order> </SynchronousCommand> <SynchronousCommand wcm:action="add"> <CommandLine>cmd.exe /c netsh advfirewall firewall add rule name="ICMP Allow incoming V4 echo request" protocol=icmpv4:8,any dir=in action=allow</CommandLine> <Description>ICMP open for ping</Description> <Order>26</Order> <RequiresUserInput>true</RequiresUserInput> </SynchronousCommand> <!-- WITH WINDOWS UPDATES --> <SynchronousCommand wcm:action="add"> <CommandLine>cmd.exe /c IF EXIST a:\set-power-config.ps1 (C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -File a:\set-power-config.ps1) ELSE (C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -File f:\set-power-config.ps1)</CommandLine> <Order>97</Order> <Description>Turn off all power saving and timeouts</Description> <RequiresUserInput>true</RequiresUserInput> </SynchronousCommand> <SynchronousCommand wcm:action="add"> <CommandLine>cmd.exe /c IF EXIST a:\microsoft-updates.ps1 (C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -File a:\microsoft-updates.ps1) ELSE (C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -File f:\microsoft-updates.ps1)</CommandLine> <Order>98</Order> <Description>Enable Microsoft Updates</Description> <RequiresUserInput>true</RequiresUserInput> </SynchronousCommand> <SynchronousCommand wcm:action="add"> <CommandLine>cmd.exe /c IF EXIST a:\win-updates.ps1 (C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -File a:\win-updates.ps1) ELSE (C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -File f:\win-updates.ps1)</CommandLine> <Description>Install Windows Updates</Description> <Order>100</Order> <RequiresUserInput>true</RequiresUserInput> </SynchronousCommand> <!-- END WITH WINDOWS UPDATES --> </FirstLogonCommands> <OOBE> <HideEULAPage>true</HideEULAPage> <HideLocalAccountScreen>true</HideLocalAccountScreen> <HideOEMRegistrationScreen>true</HideOEMRegistrationScreen> <HideOnlineAccountScreens>true</HideOnlineAccountScreens> <HideWirelessSetupInOOBE>true</HideWirelessSetupInOOBE> <NetworkLocation>Work</NetworkLocation> <ProtectYourPC>1</ProtectYourPC> </OOBE> <UserAccounts> <AdministratorPassword> <Value>vagrant</Value> <PlainText>true</PlainText> </AdministratorPassword> <LocalAccounts> <LocalAccount wcm:action="add"> <Password> <Value>vagrant</Value> <PlainText>true</PlainText> </Password> <Group>administrators</Group> <DisplayName>Vagrant</DisplayName> <Name>vagrant</Name> <Description>Vagrant User</Description> </LocalAccount> </LocalAccounts> </UserAccounts> <RegisteredOwner /> <TimeZone>Coordinated Universal Time</TimeZone> </component> </settings> <settings pass="offlineServicing"> <component name="Microsoft-Windows-LUA-Settings" processorArchitecture="amd64" publicKeyToken="31bf3856ad364e35" language="neutral" versionScope="nonSxS" xmlns:wcm="http://schemas.microsoft.com/WMIConfig/2002/State" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <EnableLUA>false</EnableLUA> </component> </settings> <cpi:offlineImage cpi:source="wim:c:/projects/baseboxes/9600.16384.winblue_rtm.130821-1623_x64fre_server_eval_en-us-irm_sss_x64free_en-us_dv5_slipstream/sources/install.wim#Windows Server 2012 R2 SERVERDATACENTER" xmlns:cpi="urn:schemas-microsoft-com:cpi" /></unattend>
sysprep-unattend.xml:
<?xml version="1.0" encoding="utf-8"?><unattend xmlns="urn:schemas-microsoft-com:unattend"> <settings pass="generalize"> <component language="neutral" name="Microsoft-Windows-Security-SPP" processorArchitecture="amd64" publicKeyToken="31bf3856ad364e35" versionScope="nonSxS" xmlns:wcm="http://schemas.microsoft.com/WMIConfig/2002/State" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <SkipRearm>1</SkipRearm> </component> </settings> <settings pass="oobeSystem"><!-- Setup proxy after sysprep <component name="Microsoft-Windows-IE-ClientNetworkProtocolImplementation" processorArchitecture="amd64" publicKeyToken="31bf3856ad364e35" language="neutral" versionScope="nonSxS" xmlns:wcm="http://schemas.microsoft.com/WMIConfig/2002/State" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <POLICYProxySettingsPerUser>1</POLICYProxySettingsPerUser> <HKLMProxyEnable>false</HKLMProxyEnable> <HKLMProxyServer>cache-proxy:3142</HKLMProxyServer> </component>Finish proxy after sysprep --> <component language="neutral" name="Microsoft-Windows-International-Core" processorArchitecture="amd64" publicKeyToken="31bf3856ad364e35" versionScope="nonSxS" xmlns:wcm="http://schemas.microsoft.com/WMIConfig/2002/State" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <InputLocale>0809:00000809</InputLocale> <SystemLocale>en-GB</SystemLocale> <UILanguage>en-US</UILanguage> <UILanguageFallback>en-US</UILanguageFallback> <UserLocale>en-GB</UserLocale> </component> <component language="neutral" name="Microsoft-Windows-Shell-Setup" processorArchitecture="amd64" publicKeyToken="31bf3856ad364e35" versionScope="nonSxS" xmlns:wcm="http://schemas.microsoft.com/WMIConfig/2002/State" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <OOBE> <HideEULAPage>true</HideEULAPage> <HideOEMRegistrationScreen>true</HideOEMRegistrationScreen> <HideOnlineAccountScreens>true</HideOnlineAccountScreens> <HideWirelessSetupInOOBE>true</HideWirelessSetupInOOBE> <NetworkLocation>Work</NetworkLocation> <ProtectYourPC>1</ProtectYourPC> <SkipUserOOBE>true</SkipUserOOBE> <SkipMachineOOBE>true</SkipMachineOOBE> </OOBE> <UserAccounts> <AdministratorPassword> <Value>vagrant</Value> <PlainText>true</PlainText> </AdministratorPassword> <LocalAccounts> <LocalAccount wcm:action="add"> <Password> <Value>vagrant</Value> <PlainText>true</PlainText> </Password> <Group>administrators</Group> <DisplayName>Vagrant</DisplayName> <Name>vagrant</Name> <Description>Vagrant User</Description> </LocalAccount> </LocalAccounts> </UserAccounts> <DisableAutoDaylightTimeSet>true</DisableAutoDaylightTimeSet> <TimeZone>Coordinated Universal Time</TimeZone> <VisualEffects> <SystemDefaultBackgroundColor>2</SystemDefaultBackgroundColor> </VisualEffects> </component> </settings></unattend>
Warning: Please note that if you're setting up WinRM for provisioning, you'll probably want to turn it off or restrict its permissions as part of a shutdown script at the end of Packer's provisioning process. For more details on the why/how, check out this useful blog post and the associated code: https://cloudywindows.io/post/winrm-for-provisioning-close-the-door-on-the-way-out-eh/
Example For Ubuntu Vivid Generation 2
If you are running Windows under virtualization, you may need to create a
virtual switch with an External
connection type.
Packer config:
{ "variables": { "vm_name": "ubuntu-xenial", "cpus": "2", "memory": "1024", "disk_size": "21440", "iso_url": "http://releases.ubuntu.com/16.04/ubuntu-16.04.6-server-amd64.iso", "iso_checksum": "sha1:056b7c15efc15bbbf40bf1a9ff1a3531fcbf70a2" }, "builders": [ { "vm_name": "{{user `vm_name`}}", "type": "hyperv-iso", "disk_size": "{{user `disk_size`}}", "guest_additions_mode": "disable", "iso_url": "{{user `iso_url`}}", "iso_checksum": "{{user `iso_checksum`}}", "communicator": "ssh", "ssh_username": "packer", "ssh_password": "packer", "ssh_timeout": "4h", "http_directory": "./", "boot_wait": "5s", "boot_command": [ "<esc><wait10><esc><esc><enter><wait>", "set gfxpayload=1024x768<enter>", "linux /install/vmlinuz ", "preseed/url=http://{{.HTTPIP}}:{{.HTTPPort}}/hyperv-taliesins.cfg ", "debian-installer=en_US auto locale=en_US kbd-chooser/method=us ", "hostname={{.Name}} ", "fb=false debconf/frontend=noninteractive ", "keyboard-configuration/modelcode=SKIP keyboard-configuration/layout=USA ", "keyboard-configuration/variant=USA console-setup/ask_detect=false <enter>", "initrd /install/initrd.gz<enter>", "boot<enter>" ], "shutdown_command": "echo 'packer' | sudo -S -E shutdown -P now", "memory": "{{user `memory`}}", "cpus": "{{user `cpus`}}", "generation": 2, "enable_secure_boot": false } ]}
preseed.cfg:
## Options to set on the command lined-i debian-installer/locale string en_US.utf8d-i console-setup/ask_detect boolean falsed-i console-setup/layout string usd-i netcfg/get_hostname string nl-ams-basebox3d-i netcfg/get_domain string unassigned-domaind-i time/zone string UTCd-i clock-setup/utc-auto boolean trued-i clock-setup/utc boolean trued-i kbd-chooser/method select American Englishd-i netcfg/wireless_wep stringd-i base-installer/kernel/override-image string linux-serverd-i debconf debconf/frontend select Noninteractived-i pkgsel/install-language-support boolean falsetasksel tasksel/first multiselect standard, ubuntu-server## Partitioningd-i partman-auto/method string lvmd-i partman-lvm/confirm boolean trued-i partman-lvm/device_remove_lvm boolean trued-i partman-lvm/confirm boolean trued-i partman-auto-lvm/guided_size string maxd-i partman-auto/choose_recipe select atomicd-i partman/confirm_write_new_label boolean trued-i partman/choose_partition select finishd-i partman/confirm boolean trued-i partman/confirm_nooverwrite boolean true# Write the changes to disks and configure LVM?d-i partman-lvm/confirm boolean trued-i partman-lvm/confirm_nooverwrite boolean trued-i partman-partitioning/no_bootable_gpt_biosgrub boolean falsed-i partman-partitioning/no_bootable_gpt_efi boolean falsed-i partman-efi/non_efi_system boolean true# Default userd-i passwd/user-fullname string packerd-i passwd/username string packerd-i passwd/user-password password packerd-i passwd/user-password-again password packerd-i user-setup/encrypt-home boolean falsed-i user-setup/allow-password-weak boolean true# Minimum packagesd-i pkgsel/include string openssh-server ntp linux-tools-$(uname -r) linux-cloud-tools-$(uname -r) linux-cloud-tools-common# Upgrade packages after debootstrap? (none, safe-upgrade, full-upgrade)# (note: set to none for speed)d-i pkgsel/upgrade select noned-i grub-installer/only_debian boolean trued-i grub-installer/with_other_os boolean trued-i finish-install/reboot_in_progress noted-i pkgsel/update-policy select nonechoose-mirror-bin mirror/http/proxy string
Note for *nix guests: Please note that Packer requires the VM to be
running a hyper-v KVP daemon in order to detect the IP address of the guest VM.
On RHEL based machines this may require installing the package hyperv-daemons
and ensuring the hypervkvpd
service is started at boot. On Debian based
machines, you may need linux-cloud-tools-common
for hv_kvp_daemon
. Failure
to do this may cause packer to wait at Waiting for SSH to become available...
before eventually timing out.
Also note that while the operating system is still being installed by a preseed
file, it is normal to see Waiting for SSH/WinRM to be available
and
Error getting SSH/WinRM host: No ip address
error messages until the system
is actually installed and ready to be connected to.
For more information about the hyper-v daemons and supported distributions, see the Microsoft docs at https://docs.microsoft.com/en-us/windows-server/virtualization/hyper-v/supported-linux-and-freebsd-virtual-machines-for-hyper-v-on-windows