Skip to content

Build the Images

Using the Build Script

Interactive Mode

  1. Start a build by running the build script (./build.sh).

    This example will look for the configuration files in the config directory.

    ./build.sh
    

    The script displays a menu to initialize a virtual machine image build(s).

    The builds are supported by a JSON configuration file (project.json) that includes the details for each guest operating system.

  2. Select a guest operating system.

    Select a guest operating system:
    
    1: Linux
    2: Windows
    
    Enter q to quit or i for info.
    
  3. Select a distribution (or edition).

    Select a Linux distribution:
    
    1: AlmaLinux OS
    2: CentOS
    3: Debian
    4: Fedora Server
    5: Oracle Linux
    6: Red Hat Enterprise Linux
    7: Rocky Linux
    8: SUSE Linux Enterprise Server
    9: Ubuntu Server
    10: VMware Photon OS
    
    Enter b to go back, or q to quit.
    
  4. Select a version.

    Select a version:
    
    1: Ubuntu Server 24.04 LTS
    2: Ubuntu Server 22.04 LTS
    3: Ubuntu Server 20.04 LTS
    
    Enter b to go back, or q to quit.
    
  5. The build will start.

    Building a Ubuntu Server 24.04 LTS virtual machine image for VMware vSphere...
    
    Initializing HashiCorp Packer and required plugins...
    Starting the build of Ubuntu Server 24.04 LTS...
    vsphere-iso.linux-ubuntu: output will be in this color.
    

Demo

Command-Line Mode

You can also run the build script using command-line options to specify the operating system, distribution, version, and edition, allowing for non-interactive builds. This is useful for automation or scripting purposes.

Examples:

  • Build an Ubuntu Server 24.04 LTS image:
./build.sh --os "Linux" --dist "Ubuntu Server" --version "24.04 LTS" --auto-continue
Info

This will start the build process for Ubuntu Server 24.04 LTS without any user prompts.

  • Build a Windows Server 2022 Standard edition image:
./build.sh --os "Windows" --dist "Windows Server" --version "2022" --edition "Standard" --auto-continue

Build Script Options

You can use the following options with the script.

Option Short Form Description
--help -h, -H Display the help.
--deps -d, -D Run a dependency check before running a build.
--json -j, -J Override the default JSON configuration file.
--show -s, -S Display the command that the script uses to initialize a build.
--os Specify the operating system (Linux or Windows).
--dist Specify the distribution (e.g., Ubuntu Server).
--version Specify the version of the distribution.
--edition Specify the edition if applicable (e.g., Standard).
--auto-continue Automatically continue without user prompts.

Build Directly with Packer

You can also start a build based on a specific source for some of the virtual machine images.

For example, if you simply want to build only Microsoft Windows Server 2022 Standard Core, run the following:

Initialize the plugins:

packer init builds/windows/server/2022/.

Build the specific machine image:

packer build -force on-error=ask \
    --only vsphere-iso.windows-server-standard-core \
    -var-file="config/build.pkrvars.hcl" \
    -var-file="config/common.pkrvars.hcl" \
    -var-file="config/vsphere.pkrvars.hcl" \
    builds/windows/server/2022

Build with Environmental Variables

Use environment variables instead of providing sensitive credentials in clear-text files.

An environment variable will always take precedence over a configuration file value.

You can add the environmental variables using the set-envvars.sh script.

. ./set-envvars.sh
Tip

You must run the script as source or the shorthand ".".


Content Library

By default, the machine image artifacts are transferred to a [vSphere Content Library][vsphere-content-library] as an OVF template and the temporary machine image is destroyed. [^1]

If an item of the same name exists in the target content library, Packer will update the existing item with the new version of OVF template.

the VMware vSphere product documentation for requirements and pre-requisites. The Microsoft Windows 11 machine image is not transferred to the content library by default. It is not supported to clone an encrypted virtual machine to the content library as an OVF Template. You can adjust the common content library settings to use VM Templates.