Reduce Your Virtual Machine Build Time Using Hashicorp Packer: [Part 6 - Packer Build]

packer Feb 08, 2024

Introduction:


In part 5, we created the remaining configuration scripts that we'll use to create the Windows Server 2022 golden image.  At this point, your AutomatedSandboxFramework directory should resemble the screenshot below.

 

Here's what we'll accomplish by the end of this post:

  1. Complete the variables.pkrvars.hcl file.
  2. Complete the Win2022.pkr.hcl file.  We'll review each of the provisioners as well.
  3. Build the Windows Server 2022 golden image.

Let's get started.


 


Step 1: Complete the template file


We're going to use several variables for the template file.  Let's define those in our HCL template file.  Add the following lines above the #Source block... line in the Win2022.pkr.hcl file.

variable "box_output" {}
variable "boot_wait" {}
variable "disk_size" {}
variable "iso_checksum" {}
variable "iso_url" {}
variable "memsize" {}
variable "numvcpus" {}
variable "vm_name" {}
variable "winrm_password" {}
variable "winrm_username" {}

Next, we'll add the provisions to the build block.

As a refresher, Provisioners use built-in and third-party software to install and configure the machine image after booting.  They prepare the golden image by applying additional configurations we've defined in the scripts created in part 5.  Place each one of the code blocks within the build block brackets.

1. Install VMware Tools

provisioner "powershell" {

only = ["vmware-iso.winsrv2022"]

pause_before = "1m0s"
scripts = ["scripts/vmware-tools.ps1"]
}

2. Execute setup.ps1 

provisioner "powershell" {
scripts = ["scripts/setup.ps1"]
}

3. Copy autounattend.xml to C:\Windows\Panther\

provisioner "file" {
source = "scripts/autounattend.xml"
destination = "C:/Windows/Panther/autounattend.xml"
}

4. Copy WinRMCert.ps1 to C:\Windows\Setup\Scripts\WinRMCert.ps1

provisioner "file" {
source = "scripts/WinRMCert.ps1"
destination = "C:/Windows/Setup/Scripts/WinRMCert.ps1"
}

5. Copy SetupComplete.cmd to C:\Windows\Setup\Scripts\SetupComplete.cmd

provisioner "file" {
source = "scripts/SetupComplete.cmd"
destination = "C:/Windows/Setup/Scripts/SetupComplete.cmd"
}

6. Install Windows updates.

provisioner "powershell" {
scripts = ["scripts/install-windows-updates.ps1"]
}

7. Restart Windows

provisioner "windows-restart" {
restart_timeout = "30m"
}

8. Install any remaining Windows updates.

provisioner "powershell" {
scripts = ["scripts/install-windows-updates.ps1"]
}

9. Restart Windows

provisioner "windows-restart" {
restart_timeout = "30m"
}

10. Execute Cleanup.ps1

provisioner "powershell" {
pause_before = "1m0s"
scripts = ["scripts/cleanup.ps1"]
}

11. Create the vagrant box output

post-processor "vagrant" {
compression_level = 6
output = "${var.box_output}"
}

Step 2: Define Variables


Now, we're ready to define the variables.  Open variables.pkrvars.hcl and add the following. FYI, your iso_checksum value may be different than mine.  You can use the get-filehash powershell command to get the correct checksum for your ISO.  When complete, it should match the screenshot below.  If you change the password or username, it'll need to be updated in your autounattend.xml file as well.

1. Set the location of the box file.  This is your golden image. 

box_output = "output/win2022_gui.box"

2. Time to wait before sending the spacebar input during boot.

boot_wait = "2s"

3. VM Disk size in megabytes

disk_size = "40960"

4. ISO Checksum 

iso_checksum = "4f1457c4fe14ce48c9b2324924f33ca4f0470475e6da851b39ccbf98f44e7852"

5. ISO location

iso_url = "C:/AUTOMATEDSANDBOXFRAMEWORK/ISO/20348.169.210806-2348.fe_release_svc_refresh_SERVER_EVAL_x64FRE_en-us.iso"

6. VM Memory Size in megabytes

memsize = "4096"

7. Number of virtual CPUs

numvcpus = "4"

8. Image name

vm_name = "Win2022_20324"

9. Winrm password

winrm_password = "packer"

10. Winrm username

winrm_username = "Administrator"

Next, we'll build the image.

Step 3: Packer Build


In this step, we'll create the win2022_gui.box file.  This file contains the golden image that can be used with Vagrant to create virtual machines using VMware.

  1. Open VMware Workstation Pro.
  2. Open Windows Terminal and navigate to the C:\AutomatedSandboxFramework\Win2022 directory.
  3. Run the following command.
    1. packer build -var-file="variables.pkvars.hcl" Win2022.pkr.hcl

Packer will create a temporary virtual machine, install the latest updates, apply the configuration scripts, shut down and delete the virtual machine, and then compact the associated files for the box.  When complete, you'll have an output folder containing the win2022_gui.box file.  This process will take 20 - 50 minutes to complete, depending on your hardware resources.

Conclusion:


This image makes it easy to create virtual machines with the same initial configuration.  Packer has numerous other use cases, such as building images for other hypervisors and cloud environments.  I hope that by following this series, you can see how useful it can be for reducing errors and time when building your virtual machines. 

Check out my course, Automated Sandbox Fundamentals, to learn more about how you can combine Packer, Vagrant, and Ansible to automate your sandbox builds.  

 

Thanks for stopping by.