- Set of Hashicorp's Packer templates to create Microsoft Hyper-V virtual machines
- Requirements
- Requirements - Quick Start
- Scripts
- Templates Windows 2022
- Templates Ubuntu
- Ansible Provisioning
- Known issues
- I have general problem not covered here
- Infamous UEFI/Secure boot WIndows implementation
When Hyper-V host has more than one interface Packer sets {{ .HTTPIP }} variable to inproper interfacePacker version 1.3.0/1.3.1 have bug with windows-restart provisioner- Packer won't run until VirtualSwitch is created as shared
- I have problem how to find a proper WIM name in Windows ISO to pick proper version
- On Windows machines, build break during updates phase, when update cycles are interfering with each other
- Why don't you use ansible instead of shell scripts for provisioning
- About
- packer <=
1.8.4
. Do not use packer below 1.7.0 version. For previous packer versions use previous releases from this repository - Microsoft Hyper-V Server 2019 or Microsoft Windows Server 2019/2022 with Hyper-V role installed as host to build your images
- firewall exceptions for
packer
http server (look down below) - properly constructed virtual switch in Hyper-v allowing virtual machine to get IP from DHCP and contact Hyper-V server on mentioned packer ports. This is a must, if kickstart is reachable over the network.
choco install packer --version=1.8.4 -y
In root folder of a repository
packer init --upgrade config.pkr.hcl
Remove-NetFirewallRule -DisplayName "Packer_http_server" -Verbose
New-NetFirewallRule -DisplayName "Packer_http_server" -Direction Inbound -Action Allow -Protocol TCP -LocalPort 8000-9000
To adjust to your Hyper-V, please check variables below and/or in ./variables files
- (variable
vlan_id
in /variables/variables.*.pkvars.hcl) - proper VLAN ID . Look up to find your build server vEthernet setings. - (variable
switch_name
in /variables/variables.*.pkvars.hcl) - proper Hyper-V Virtual Switch name (access to Internet will be required). Make sure you're using pre-existing switch in your Hyper-V server - creation of new switch by packer, instead of reusing existing one can cause lack of Internet access, thus failing the build.
# example of mentioned variables
vlan_id = ""
switch_name = "vSwitch"
Soon to be parametrized
In building script set packet_log
variable to 1
$packer_log=1
-
all available updates will be applied (3 passes)
-
latest version of chocolatey
-
packages from a list below:
Package Version conemu latest dotnetfx latest sysinternals latest -
phase4b-docker.ps1
- Docker settings can be customisedrequiredVersion
- which version of docker module to install - defaults to 19.03.1installCompose
($true/$false) - install docker-compose from chocolatey packagesdockerLocation
- of set, will default docker images and settings there. On empty, docker location is not being set.configDockerLocation
- default place for docker's config file
Example of usage
.\phase5b-docker.ps1 -requiredVersion "19.03.1" -installCompose $true -dockerLocation "d:\docker" -configDockerLocation "C:\ProgramData\Docker\config"
-
Repositories:
Repository Package switch Epel 7/8/9 epel-release can be switched off by setting "install_epel" to false
Zabbix 6.0 zabbix-agent can be switched on by setting "install_zabbix" to true
Hyper-V SCVMM Agent can be switched off by setting "install_hyperv" to false
Neofetch neofetch can be switched off by setting "install_neofetch" to false
-
[Optional] Linux machine with separated disk for docker
-
[Optional] Linux machine for vagrant
Run hv_win2022_std.ps1
(Windows)
For Generation 2 prepare secondary.iso
with folder structure:
- ./extra/files/gen2-2022/std/Autounattend.xml => /Autounattend.xml
- ./extra/scripts/hyper-v/bootstrap.ps1 => /bootstrap.ps1
This template uses this image name in Autounattendes.xml. If youre using different ISO you'll have to adjust that part in proper file and rebuild secondary.iso
image.
<InstallFrom>
<MetaData wcm:action="add">
<Key>/IMAGE/NAME </Key>
<Value>Windows Server 2022 SERVERSTANDARD</Value>
</MetaData>
</InstallFrom>
Run hv_win2022_dc.ps1
(Windows)
For Generation 2 prepare secondary.iso
with folder structure:
- ./extra/files/gen2-2022/dc/autounattend.xml => /Autounattend.xml
- ./extra/scripts/hyper-v/bootstrap.ps1 => /bootstrap.ps1
- ./extra/scripts/hyper-v/winrm-config.ps1 => /winrm-config.ps1
This template uses this image name in Autounattendes.xml. If youre using different ISO you'll have to adjust that part in proper file and rebuild secondary.iso
image. You can use the unattend-iso-build.ps1
script in the root of the repo to rebuild the ISO incase of any additions/modifications.
<InstallFrom>
<MetaData wcm:action="add">
<Key>/IMAGE/NAME </Key>
<Value>Windows Server 2022 SERVERDATACENTER</Value>
</MetaData>
</InstallFrom>
- if required change
switch_name
parameter to switch's name you're using. In most situations packer manages it fine but there were a cases when it created new 'internal' switches without access to Internet. By design this setup will fail to download and apply updates. - if needed - change
iso_url
variable to a proper iso name - packer generates v8 machine configuration files (Windows 2016/Hyper-V 2016 as host) and v9 for Windows Server 2019/Windows 10 1809
- credentials for Windows machines: Administrator/password (removed after sysprep)
- credentials for Linux machines: root/password
- for Windows based machines adjust your settings in ./scripts/phase-2.ps1
- for Linux based machines adjust your settings in ./files/gen2-{{os}}/provision.sh and ./files/gen2-{{os}}/puppet.conf
Run hv_ubuntu2004.ps1
Run hv_ubuntu2204.ps1
I use CI with Azure DevOps for building images via Packer. In order to facilitate this I use Ansible to run the packer builds on Hyper-V.
The packer_build.yml
file in the root of the repo is the Ansible playbook that can be used. The plays are located in ./roles/packer_build/tasks/main.yml
The play will need to be modified slightly as it is catered for my own environment where its cloning private git repos and such
hashicorp/packer#7278 (comment)
Fixed in version 1.4.4. Do not use previous versions
When Hyper-V host has more than one interface Packer sets {{ .HTTPIP }} variable to inproper interface
Fixed in version 1.4.4. Do not use lower versions
No resolution so far, template needs to be changed to pass real IP address, or there should be connection between these addresses. Limiting these, end with timeout errors.**
hashicorp/packer#5023 Will be fixed in 1.4.x revision
You can use number. If you have 4 images on the list of choice - use ImageIndex
with proper Value
<ImageInstall>
<OSImage>
<InstallFrom>
<MetaData wcm:action="add">
<Key>/IMAGE/INDEX </Key>
<Value>2</Value>
</MetaData>
</InstallFrom>
<InstallTo>
<DiskID>0</DiskID>
<PartitionID>2</PartitionID>
</InstallTo>
</OSImage>
</ImageInstall>
On Windows machines, build break during updates phase, when update cycles are interfering with each other
Increase variable update_timeout
in ./variables/*.json
file - this will create longer pauses between stages, allowing cycles to complete before jumping to another one.