Packer configuration

While we have already setup the template in the previous chapter, we still need to configure a couple of things before Packer can communicate with HyperV.  This is because we are running inside of WSL, which itself is a VM behind the scenes on HyperV, but a very special one. Such as we need to enable Packer running on WSL2 VM to access the hypervisor as packer during the build process will start a new VM. 

First thing is to have Packer cache directory on the Windows filesystem. In order to do it you need to put this into your bashrc or wherever shell you environment you are using: 

        PACKER_CACHE_DIR=/mnt/c/Users/user/.packer 

The second thing is that Packer and Vagrant will create some ssh keys to use. However, by default ssh is configured to deny any connection attempts if your private key is unprotected. You will need to fine-tune your WSL, to mount the Windows filesystem with Metadata. You can setup Linux filesystem access rights on your mounted Windows filesystem. Simple right? No worries, just inside of your WSL add the next things into your /etc/wsl.conf 

[boot] 
systemd=true 
[automount] 
options=metadata 

The systemd part is not mandatory for this exercise, but I strongly recommend to do it.Systemd is a handy tool. 

So now, you should be ready to kick off the Packer build. One more thing to remember is that your project directory will be on the Windows filesystem. Annoying, but that is what it is. To remediate it I just made a symlink from my homefolder to my Windows “project” directory, so I can find it easily afterwards. 

Before you build, you need to download the plugins which we use in Packer, so first you do: 

`packer init template.pkr.hcl` 

Now you can trigger a packer build by `packer build template.pkr.hcl`, and you should see something like this:

(base) marczis@Sonya:~/pro/winpro/vagrant/kube/base_images/ubuntu_minimal$ packer build template.pkr.hcl 
hyperv-iso.ubuntu: output will be in this color. 
==> hyperv-iso.ubuntu: Creating build directory... 
==> hyperv-iso.ubuntu: Retrieving ISO 
==> hyperv-iso.ubuntu: Trying /mnt/c/Users/marcz/Documents/pro/ubuntu-22.04.4-live-server-amd64.iso 
==> hyperv-iso.ubuntu: Trying /mnt/c/Users/marcz/Documents/pro/ubuntu-22.04.4-live-server-amd64.iso?checksum=sha256%3A45f873de9f8cb637345d6e66a583762730bbea30277ef7b32c9c3bd6700a32b2 
==> hyperv-iso.ubuntu: /mnt/c/Users/marcz/Documents/pro/ubuntu-22.04.4-live-server-amd64.iso?checksum=sha256%3A45f873de9f8cb637345d6e66a583762730bbea30277ef7b32c9c3bd6700a32b2 => /mnt/c/Users/marcz/Documents/pro/ubuntu-22.04.4-live-server-amd64.iso 
==> hyperv-iso.ubuntu: Starting HTTP server on port 8955 
==> hyperv-iso.ubuntu: Creating switch 'public_network' if required... 
==> hyperv-iso.ubuntu:     switch 'public_network' already exists. Will not delete on cleanup... 
==> hyperv-iso.ubuntu: Creating virtual machine... 
==> hyperv-iso.ubuntu: Enabling Integration Service... 
==> hyperv-iso.ubuntu: Mounting os dvd drive C:\Users\marcz\Documents\pro\ubuntu-22.04.4-live-server-amd64.iso ... 
==> hyperv-iso.ubuntu: Skipping mounting Integration Services Setup Disk... 
==> hyperv-iso.ubuntu: Mounting secondary DVD images... 
==> hyperv-iso.ubuntu: Configuring vlan... 
==> hyperv-iso.ubuntu: Determine Host IP for HyperV machine... 
==> hyperv-iso.ubuntu: Host IP for the HyperV machine: 192.168.1.25 
==> hyperv-iso.ubuntu: Attempting to connect with vmconnect... 
==> hyperv-iso.ubuntu: Starting the virtual machine... 
==> hyperv-iso.ubuntu: Waiting 5s for boot... 
==> hyperv-iso.ubuntu: Typing the boot command... 
==> hyperv-iso.ubuntu: Waiting for SSH to become available... 
==> hyperv-iso.ubuntu: Connected to SSH! 
==> hyperv-iso.ubuntu: Gracefully halting virtual machine... 
==> hyperv-iso.ubuntu: Waiting for vm to be powered down... 
==> hyperv-iso.ubuntu: Unmount/delete secondary dvd drives... 
==> hyperv-iso.ubuntu: Unmount/delete Integration Services dvd drive... 
==> hyperv-iso.ubuntu: Unmount/delete os dvd drive... 
==> hyperv-iso.ubuntu: Delete os dvd drives controller 0 location 1 ... 
==> hyperv-iso.ubuntu: Unmount/delete floppy drive (Run)... 
==> hyperv-iso.ubuntu: Compacting disks... 
    hyperv-iso.ubuntu: Compacting disk: PackerBuilder.vhdx 
    hyperv-iso.ubuntu: Disk size is unchanged 
==> hyperv-iso.ubuntu: Exporting virtual machine... 
==> hyperv-iso.ubuntu: Collating build artifacts... 
==> hyperv-iso.ubuntu: Disconnecting from vmconnect... 
==> hyperv-iso.ubuntu: Unregistering and deleting virtual machine... 
==> hyperv-iso.ubuntu: Deleting build directory... 
==> hyperv-iso.ubuntu: Running post-processor:  (type shell-local) 
==> hyperv-iso.ubuntu (shell-local): Running local shell script: /tmp/packer-shell192936170 
    hyperv-iso.ubuntu (shell-local): ./Virtual Hard Disks/ 
    hyperv-iso.ubuntu (shell-local): ./Virtual Hard Disks/PackerBuilder.vhdx 
    hyperv-iso.ubuntu (shell-local): ./Virtual Machines/ 
    hyperv-iso.ubuntu (shell-local): ./Virtual Machines/3A007061-A58E-4C53-8464-DA85A8BF1600.vmcx 
    hyperv-iso.ubuntu (shell-local): ./Virtual Machines/3A007061-A58E-4C53-8464-DA85A8BF1600.vmgs 
    hyperv-iso.ubuntu (shell-local): ./Virtual Machines/3A007061-A58E-4C53-8464-DA85A8BF1600.VMRS 
    hyperv-iso.ubuntu (shell-local): ./Virtual Machines/box.xml 
    hyperv-iso.ubuntu (shell-local): ./metadata.json 
    hyperv-iso.ubuntu (shell-local): /home/marczis/pro/winpro/vagrant/kube/base_images/ubuntu_minimal 
    hyperv-iso.ubuntu (shell-local): ==> box: Box file was not detected as metadata. Adding it directly... 
    hyperv-iso.ubuntu (shell-local): ==> box: Adding box 'ubuntu' (v0) for provider: 
    hyperv-iso.ubuntu (shell-local):     box: Unpacking necessary files from: file:///mnt/c/Users/marcz/Documents/pro/vagrant/kube/base_images/ubuntu_minimal/ubuntu.box 
    hyperv-iso.ubuntu (shell-local): ==> box: Successfully added box 'ubuntu' (v0) for ''! 

Build 'hyperv-iso.ubuntu' finished after 11 minutes 24 seconds. 

==> Wait completed after 11 minutes 24 seconds 

==> Builds finished. The artifacts of successful builds are: 
--> hyperv-iso.ubuntu: VM files in directory: output-ubuntu 
--> hyperv-iso.ubuntu: VM files in directory: output-ubuntu 

As you can see the build process takes quite some time -  11.5 minutes in my case. However if you consider that during this time, we power on a new server, answer all installation questions, get the system configure, install some packages, and save the virtual machine state, I would say this is pretty decent result. 

At the end of the Packer template, we execute some local commands, which will pack the HyperV exported VM, create a box file out of it for Vagrant, and even add it to Vagrant. In the next section we will use Vagrant to start up virtual machines on HyperV based on templates, if you haven’t installed vagrant yet, please go ahead and install it on your WSL! 

Vagrant configuration 

Just as with Packer, Vagrant needs to be configured to run from WSL and reach HyperV. 

In your bashrc, or wherever you like you need to configure this environment variable: 

export VAGRANT_WSL_ENABLE_WINDOWS_ACCESS="1" 

This configures Vagrant to access the HyperV outside of WSL. 

 

Any questions? We are here for you!