How to Create Hyper-V Images With Packer
In this posts, Matt Wrock will discuss how you can use Taliesin Sisson's PR #2576 in order to build Hyper-V images with Hyper-V.
Join the DZone community and get the full member experience.
Join For FreeI've been playing with some new significant changes to my Packer build process. This includes replacing the Boxstarter based install with a Chef cookbook and also using a native Hyper-V builder. I'll be blogging about the Chef stuff later. This post will discuss how to use Taliesin Sisson's PR #2576 to build Hyper-V images with Hyper-V.
The State of Hyper-V Builders
Packer currently comes bundled with builders for the major cloud providers, some local hypervisors like VirtualBox, VMware, Parallels, and QEMU as well as OpenStack and Docker. There is no built-in Hyper-V builder (the native hypervisor on Windows).
The packer ecosystem does provide a plugin model for supporting third-party builders. In early 2015, it was announced that MSOpenTech had built a usable Hyper-V builder plugin and there were hopes to pull that into Packer core. This never happened. I personally see this like two technology asteroids rocketing past each other. The Hyper-V builder came in on a version of GO that Packer did not yet support but by the time it did, Packer and Hyper-V had moved on.
I started playing with Packer in July of 2015 and when I tried this builder on Windows 10 (a technical preview at the time) it just did not work. Likely this is because some things in Hyper-V, like its virtual machine file format, had completely changed. Hard to say but as a Packer newcomer and wanting to just get an image built I quickly moved away from using a Hyper-V builder.
Converting VirtualBox to Hyper-V
After ditching the hope of building Hyper-V images with Packer, I resurrected my daughter's half-busted laptop to become my VirtualBox Packer builder. It worked great.
I also quickly discovered that I could simply convert the VirtualBox images to VHD format and create a Vagrant Hyper-V provider box without Hyper-V. I blogged about this procedure here and I think it is still a good option for creating multiple providers on a single platform.
It's great to take the exact same image that provisions a VirtualBox VM to also provision a Hyper-V VM. However, its sometimes a pain to have to switch over to a different environment. My day-to-day dev environment uses Hyper-V and, ideally, this is where I would develop and test Packer builds as well.
A Hyper-V Builder That Works
So early this year I started hearing mumblings of a new PR to Packer for an updated Hyper-V builder. My VirtualBox setup worked fine and I needed to produce both VirtualBox and Hyper-V providers anyways so I was not highly motivated to try out this new builder.
I spoke at Hashiconf about creating Windows Vagrant boxes with Packer. It sure would be nice not to have to bring my VirtualBox rig and just use a Hyper-V builder on my personal laptop.
So, I finally took this PR for a spin last week and I was pretty amazed when it just worked. One thing I have noticed in contemporary DevOps tooling is that the chances of the tooling working on Windows is sketchy and as for Hyper-V? Good luck! No one uses it in the communities where I mingle (except for me, it sometimes seems). If few are testing the tooling and most building the tooling are not familiar with Windows environment nuances, it's not a scenario optimized for success.
Using PR #2576 to Build Hyper-V Images
For those unfamiliar with working with Go source builds, getting the PR built and working is probably the biggest blocker to getting started. It's really not that bad at all. Hand here is a step-by-step walkthrough to building the PR:
- Install golang using Chocolatey: cinst golang -y. This puts Go in c:\tools\go.
- Create a directory for Go development: c:\dev\go and set $env:gopath to that path.
- From that path, run go get github.com/mitchellh/packer, which will put packer's master branch in c:\dev\go\src\github.com\mitchellh\packer.
- Navigate to that directory and add a git remote to Taliesin Sisson's PR branch: git remote add hyperv https://github.com/taliesins/packer.
- Run git fetch hyperv and then git checkout hyperv. Now, the code for this PR is on the disk.
- Build it with go build -o bin/packer.exe.
- Now, the built packer.exe is at C:\dev\go\src\github.com\mitchellh\packer\bin\packer.exe.
You can now run C:\dev\go\src\github.com\mitchellh\packer\bin\packer.exe build and this builder will be available!
Things to Know
If you have used the VirtualBox builder, this builder is really not much different at all. The only thing that surprised and tripped me up a bit at first is that unless you configure it differently, the builder will create a new switch to be used by the VMs it creates. This switch may not be able to access the internet and your build might break. You can easily avoid this and use an existing switch by using the switch_name setting.
A Working Template
As I mentioned above, I've been working on using Chef instead of Boxstarter to provision the packer image. I've been testing this building a Windows Server 2016 TP5 image. Here is the Hyper-V template. The builder section is as follows:
"builders": [
{
"type": "hyperv-iso",
"guest_additions_mode": "disable",
"iso_url": "{{ user `iso_url` }}",
"iso_checksum": "{{ user `iso_checksum` }}",
"iso_checksum_type": "md5",
"ram_size_mb": 2048,
"communicator": "winrm",
"winrm_username": "vagrant",
"winrm_password": "vagrant",
"winrm_timeout": "12h",
"shutdown_command": "C:/Windows/Panther/Unattend/packer_shutdown.bat",
"shutdown_timeout": "15m",
"switch_name": "internal_switch",
"floppy_files": [
"answer_files/2016/Autounattend.xml",
"scripts/winrm.ps1"
]
}
]
Documentation
"builders": [
{
"type": "hyperv-iso",
"guest_additions_mode": "disable",
"iso_url": "{{ user `iso_url` }}",
"iso_checksum": "{{ user `iso_checksum` }}",
"iso_checksum_type": "md5",
"ram_size_mb": 2048,
"communicator": "winrm",
"winrm_username": "vagrant",
"winrm_password": "vagrant",
"winrm_timeout": "12h",
"shutdown_command": "C:/Windows/Panther/Unattend/packer_shutdown.bat",
"shutdown_timeout": "15m",
"switch_name": "internal_switch",
"floppy_files": [
"answer_files/2016/Autounattend.xml",
"scripts/winrm.ps1"
]
}
]
Fortunately, this PR includes updated documentation for the builder. You can view it in markdown here.
Published at DZone with permission of Matt Wrock, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments