Building a Hyper - V image with Packer can be a complex yet rewarding process. As a Packer supplier, I've had the opportunity to work with numerous clients to streamline their image - building workflows. In this blog, I'll share a comprehensive guide on how to build a Hyper - V image using Packer, covering everything from the basics to advanced techniques.
Understanding Packer and Hyper - V
Before diving into the process, it's essential to understand what Packer and Hyper - V are. Packer is an open - source tool developed by HashiCorp that automates the creation of machine images for multiple platforms, including Hyper - V. Hyper - V is Microsoft's native hypervisor, which allows users to create and manage virtual machines on Windows Server and Windows 10.
Using Packer to build Hyper - V images offers several advantages. It provides a consistent and repeatable way to create images, reducing the chances of human error. It also allows for version control of image configurations, making it easier to manage changes over time.
Prerequisites
To build a Hyper - V image with Packer, you'll need the following:
- Packer Installation: Download and install Packer from the official HashiCorp website. Make sure to add Packer to your system's PATH so that you can run it from the command line.
- Hyper - V Enabled: Enable the Hyper - V feature on your Windows machine. You can do this through the "Turn Windows features on or off" option in the Control Panel.
- Base ISO Image: You'll need an ISO image of the operating system you want to install on the virtual machine. For example, if you're building a Windows Server 2019 image, download the official ISO from Microsoft.
- Knowledge of JSON: Packer uses JSON files to define image - building configurations. Familiarize yourself with JSON syntax if you're not already.
Creating a Packer Configuration File
The heart of the image - building process with Packer is the configuration file. This file, usually named template.json, defines the steps and settings for creating the Hyper - V image.
Here's a basic example of a template.json file for building a Windows Server 2019 Hyper - V image:
{
"builders": [
{
"type": "hyperv-iso",
"iso_url": "path/to/your/windows_server_2019.iso",
"iso_checksum": "sha256:your_checksum",
"vm_name": "windows_server_2019_image",
"disk_size": 60000,
"communicator": "winrm",
"winrm_username": "Administrator",
"winrm_password": "your_password",
"winrm_timeout": "1h",
"shutdown_command": "shutdown /s /t 0"
}
],
"provisioners": [
{
"type": "powershell",
"inline": [
"Install - WindowsFeature - Name Web - Server",
"Set - Service - Name W3SVC - StartupType Automatic"
]
}
]
}
In this configuration:
- The
builderssection defines the type of builder (in this case,hyperv - iso) and the settings for the virtual machine, such as the ISO URL, disk size, and communication protocol. - The
provisionerssection contains the commands to be executed on the virtual machine after the operating system is installed. Here, we're installing the Web Server role and setting the World Wide Web Publishing Service to start automatically.
Building the Hyper - V Image
Once you've created the template.json file, you can start building the Hyper - V image using the following steps:
- Navigate to the Directory: Open a command prompt or PowerShell window and navigate to the directory where your
template.jsonfile is located. - Validate the Configuration: Run the following command to validate the Packer configuration file:
packer validate template.json
If the configuration is valid, Packer will return a success message. Otherwise, it will display the errors you need to fix.
3. Build the Image: If the validation is successful, run the following command to start building the image:
packer build template.json
Packer will then start the process of creating the virtual machine, installing the operating system, and running the provisioning commands. This process may take some time, depending on your system's resources and the complexity of the configuration.
Advanced Techniques
Using Variables
Instead of hard - coding values in the template.json file, you can use variables to make the configuration more flexible. For example, you can define variables for the ISO URL, password, and disk size.
Here's how you can modify the previous template.json file to use variables:
{
"variables": {
"iso_url": "path/to/your/windows_server_2019.iso",
"iso_checksum": "sha256:your_checksum",
"winrm_password": "your_password",
"disk_size": "60000"
},
"builders": [
{
"type": "hyperv-iso",
"iso_url": "{{user `iso_url`}}",
"iso_checksum": "{{user `iso_checksum`}}",
"vm_name": "windows_server_2019_image",
"disk_size": "{{user `disk_size`}}",
"communicator": "winrm",
"winrm_username": "Administrator",
"winrm_password": "{{user `winrm_password`}}",
"winrm_timeout": "1h",
"shutdown_command": "shutdown /s /t 0"
}
],
"provisioners": [
{
"type": "powershell",
"inline": [
"Install - WindowsFeature - Name Web - Server",
"Set - Service - Name W3SVC - StartupType Automatic"
]
}
]
}
You can then pass the variable values when running the packer build command:


packer build -var 'iso_url=path/to/new/iso.iso' -var 'winrm_password=new_password' template.json
Post - Processing
Packer allows you to perform post - processing steps after the image is built. For example, you can export the Hyper - V virtual machine to a different format or upload it to a cloud storage service.
Here's an example of adding a post - processor to export the virtual machine as a VHDX file:
{
"builders": [
{
"type": "hyperv-iso",
//... existing builder settings...
}
],
"provisioners": [
{
"type": "powershell",
//... existing provisioner settings...
}
],
"post - processors": [
{
"type": "hyperv-export",
"output": "output/windows_server_2019_image_{{.BuildName}}.vhdx"
}
]
}
Different Types of Packers
As a Packer supplier, we offer a variety of packers for different applications. You can explore our range of packers, including Compression Open Hole Packer, Hydraulic Packer, and Tension Packers. These packers are designed to meet the specific needs of various industries, ensuring reliable and efficient performance.
Conclusion
Building a Hyper - V image with Packer is a powerful way to automate the creation of consistent and repeatable virtual machine images. By following the steps outlined in this blog, you can create your own Hyper - V images tailored to your specific requirements.
If you're interested in learning more about our Packer products or have any questions about the image - building process, we encourage you to reach out for a procurement discussion. Our team of experts is ready to assist you in finding the best solutions for your business needs.
References
- HashiCorp Packer Documentation
- Microsoft Hyper - V Documentation
