Image for post
Image for post

Packer: Building Win 2016 Vagrant Image

Building Windows 2016 Vagrant Box on macOS

Recently, I wanted to play with Docker containers on the latest Windows Server, but I don’t have any Windows 2016 systems readily at my disposal. I would like to have the same sort of functionality I get with Vagrant and Linux systems, but because Windows operating system is strictly licensed, you cannot just pull it down off the Internet (at least legally), so you need to create your own image.

Getting Started

Before we jump straight into building this, we need to get the necessary tools, download the build scripts, and make some fixes to the source code.

Tools Required

For this project, we need to install the following tools: git, ChefDK, Packer, Virtualbox, and Vagrant.

cat <<-BREWFILE > Brewfile
tap chef/chef
brew 'gnu-sed', args: ['with-default-names'] # GNU Sed
brew git
brew packer
cask chefdk
cask virtualbox
cask virtualbox-extension-pack
cask vagrant
BREWFILE
brew bundle --verbose

Source Repository

With git, we need to clone the repository somewhere:

mkdir $HOME/workarea
cd $HOME/workarea
git clone git@github.com:mwrock/packer-templates.git

Fetching the Install Media

I highly recommend fetching the install media from Microsoft and saving it locally, rather than downloading this each and every time you build this project.

ISO_ARTIFACT=14393.0.160715-1616.RS1_RELEASE_SERVER_EVAL_X64FRE_EN-US.ISO
URL_PREFIX="http://care.dlservice.microsoft.com"
URL_PATH="dl/download/1/6/F/16FA20E6-4662-482A-920B-1A45CF5AAE3C/"
ISO_URL="${URL_PREFIX}/${URL_PATH}/${ISO_ARTIFACT}"
curl -o ${HOME}/Downloads ${ISO_URL}

Fixing Some Issues

We’ll need to fix some issues before we attempt this.

cd $HOME/workarea/packer-templates
pushd cookbooks/packer-template
# Remove dependency to Windows cookbook
sed -i '/depends.*windows/d' metadata.rb # GNU Sed
rm Berksfile.lock
berks
berks vendor ../../vendor/cookbooks
popd

Building The Image

If you previously downloaded the artifact, you can run this command:

ISO_ARTIFACT=14393.0.160715-1616.RS1_RELEASE_SERVER_EVAL_X64FRE_EN-US.ISO
ISO_URL=${HOME}/Downloads/${ISO_ARTIFACT}"
packer build \
-var "iso_url=${ISO_URL}" \
-force -only virtualbox-iso \
vbox-2016.json
packer build \
-force -only virtualbox-iso \
vbox-2016.json

Manually Testing the Image

Add the Windows 2016 Box

Once the build is finished, we need to add the box to our vagrant environment. The build script (vbox-2016.json) will output a Vagrant box called windows2016min-virtualbox.box.

NAMESPACE=myboxen
BOXNAME=win2016
ARTIFACT=windows2016min-virtualbox.box
vagrant box add ${ARTIFACT} --name ${NAMESPACE}/${BOXNAME}

Test the Windows 2016 Box

Now that the Vagrant box is available to our environment, we can test it out using this:

NAMESPACE=myboxen
BOXNAME=win2016
cd
mkdir winbox-test && cd winbox-test
vagrant init ${NAMESPACE}/${BOXNAME}
vagrant up
RDP_ADDR="full%20address=s:127.0.0.1:3389"
RDP_WIDTH="desktopwidth=i:1280"
RDP_HEIGHT="desktopheight=i:1024"
open "rdp://${RDP_ADDR}&${RDP_HEIGHT}&${RDP_WIDTH}"

Segue: Remote Desktop Function

If you want an easy way to run rdp from the command line, defaulting to local host to access your Windows vagrant box, you can add this to your shell environment, e.g. .bashrc or .zshrc.

rdp() {
# default variables
RDP_ADDR=${1:-'127.0.0.1:3389'}
RDP_WIDTH=${2:-'1280'}
RDP_HEIGHT=${3:-'1024'}
# formatted strings to feed MS Remote Desktop client
RDP_ADDR="full%20address=s:${RDP_ADDR}"
RDP_WIDTH="desktopwidth=i:${RDP_WIDTH}"
RDP_HEIGHT="desktopheight=i:${RDP_HEIGHT}"
open "rdp://${RDP_ADDR}&${RDP_HEIGHT}&${RDP_WIDTH}"
}
rdp localhost:3389 1920 1080

Conclusion

And Voilà, you now have a usable and disposable Windows Server 2016 test environment.

Written by

Linux NinjaPants Automation Engineering Mutant — exploring DevOps, Kubernetes, CNI, IAC

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store