Skip to main content
Redhat Developers  Logo
  • Products

    Platforms

    • Red Hat Enterprise Linux
      Red Hat Enterprise Linux Icon
    • Red Hat AI
      Red Hat AI
    • Red Hat OpenShift
      Openshift icon
    • Red Hat Ansible Automation Platform
      Ansible icon
    • See all Red Hat products

    Featured

    • Red Hat build of OpenJDK
    • Red Hat Developer Hub
    • Red Hat JBoss Enterprise Application Platform
    • Red Hat OpenShift Dev Spaces
    • Red Hat OpenShift Local
    • Red Hat Developer Sandbox

      Try Red Hat products and technologies without setup or configuration fees for 30 days with this shared Red Hat OpenShift and Kubernetes cluster.
    • Try at no cost
  • Technologies

    Featured

    • AI/ML
      AI/ML Icon
    • Linux
      Linux Icon
    • Kubernetes
      Cloud icon
    • Automation
      Automation Icon showing arrows moving in a circle around a gear
    • See all technologies
    • Programming languages & frameworks

      • Java
      • Python
      • JavaScript
    • System design & architecture

      • Red Hat architecture and design patterns
      • Microservices
      • Event-Driven Architecture
      • Databases
    • Developer experience

      • Productivity
      • Tools
      • GitOps
    • Automated data processing

      • AI/ML
      • Data science
      • Apache Kafka on Kubernetes
    • Platform engineering

      • DevOps
      • DevSecOps
      • Red Hat Ansible Automation Platform for applications and services
    • Secure development & architectures

      • Security
      • Secure coding
  • Learn

    Featured

    • Kubernetes & cloud native
      Openshift icon
    • Linux
      Rhel icon
    • Automation
      Ansible cloud icon
    • AI/ML
      AI/ML Icon
    • See all learning resources

    E-books

    • GitOps cookbook
    • Podman in action
    • Kubernetes operators
    • The path to GitOps
    • See all e-books

    Cheat sheets

    • Linux commands
    • Bash commands
    • Git
    • systemd commands
    • See all cheat sheets

    Documentation

    • Product documentation
    • API catalog
    • Legacy documentation
  • Developer Sandbox

    Developer Sandbox

    • Access Red Hat’s products and technologies without setup or configuration, and start developing quicker than ever before with our new, no-cost sandbox environments.
    • Explore the Developer Sandbox

    Featured Developer Sandbox activities

    • Get started with your Developer Sandbox
    • OpenShift virtualization and application modernization using the Developer Sandbox
    • Explore all Developer Sandbox activities

    Ready to start developing apps?

    • Try at no cost
  • Blog
  • Events
  • Videos

Automate VM golden image builds for OpenShift with Packer

November 7, 2025
Giovanni Fontana
Related topics:
Automation and managementKubernetesVirtualization
Related products:
Red Hat OpenShiftRed Hat OpenShift Virtualization

    In any virtualized environment, maintaining consistency across virtual machines (VMs) is a major challenge. Golden images (pre-configured VM templates) are the industry-standard solution. They ensure every new VM comes with the correct OS settings, security patches, and monitoring tools baked in. But how do you create and manage these images without tedious manual work?

    This is where Packer comes into the picture. Packer is a tool that automates the creation of identical machine images for multiple platforms from a single template. In this article, we'll show you how to use Packer with the KVM plugin to build golden images specifically for Red Hat OpenShift Virtualization. 

    Packer

    Packer allows you to create images from a single source template, described using either HashiCorp Configuration Language (HCL) or JSON. It provides a large collection of plugins to create machines and images for different platforms, such AWS, Azure, VMware, KVM, and others. Refer to the Packer Integrations page for more information. 

    Red Hat OpenShift Virtualization

    Red Hat OpenShift Virtualization, built on KubeVirt, is a feature of Red Hat OpenShift that allows you to run and manage traditional virtual machines alongside containers on a single, hybrid cloud platform. This unification of both containerized and virtualized workloads simplifies management and enables a consistent operational experience across your entire infrastructure.

    Packer + OpenShift Virtualization: Better together

    Through the KVM plugin and with a few simple commands, you can make your golden image ready on OpenShift Virtualization. The GitHub repository includes a collection of examples for creating images for various operating systems, including Linux (Fedora, RHEL 9) and Windows.

    Later in this article, we will dig into one of these examples, but the general workflow is simple. You define a Packer template (HCL) that specifies the operating system, configuration scripts, and other customizations. Packer then uses this template to build the VM image automatically using KVM. The output will be a qcow2 image file for you to upload to OpenShift Virtualization and use as a golden image for your VM provisioning. 

    Once Packer completes the qcow2 file creation, uploading it to OpenShift and setting it as a bootable image are steps easily automated as part of a pipeline or with automation tools like Red Hat Ansible Automation Platform or Terraform. 

    Key features and benefits:

    • Automation: Eliminate tedious manual image creation, saving time and preventing configuration drift.
    • Customization: Easily embed specific software, user configurations, and security settings directly into the image.
    • Performance optimization: Build images with the best drivers for your environment, like using virtio drivers for Windows on KVM to achieve near-native performance.
    • Reduced risk: Automating the image creation process ensures a standardized and less error-prone approach compared to manual methods.
    • Simple integration: The build process outputs a qcow2 file that can upload to OpenShift Virtualization with a few simple commands, perfect for CI/CD pipelines.

    Getting started

    The project's GitHub repository provides detailed instructions on how to get started. Here's a quick overview of the steps involved:

    1. Clone the repository: Obtain a local copy of the project.
    2. Install Packer and KVM: Install Packer on your system.
    3. Choose an example: The repository includes examples for different operating systems.
    4. Build the image: Run the Packer build command to create your custom VM image.
    5. Upload to OpenShift: Use the virtctl and oc command-line tool to upload the image to OpenShift Virtualization.

    Let's walk through the Windows Server 2019 example to see how the automation comes together. The windows2019.pkr.hcl Packer template defines the process and orchestrates the entire build.

    The source block defines the crucial first step, where Packer creates and attaches a virtual CD-ROM to the new VM. This CD contains all the files needed for an unattended installation:

    # This creates a virtual CD with our automation files and drivers.
    cd_files = [
      "./autounattend.xml",
      "./scripts/Configure-WinRM.ps1",
      "./virtio/"
    ]
    cd_label = "PACKERDRV" # A label for our driver CD

    The Windows installer then uses the autounattend.xml file from this virtual CD as its answer file to automate the setup. Inside this XML file, we point the installer to the virtio drivers, also located on the virtual CD (which typically mounts as the E: drive). This allows Windows to use the high-performance KVM-native storage and network drivers right from the start.

    <DriverPaths>
        <PathAndCredentials wcm:action="add" wcm:keyValue="1">
            <Path>E:\virtio\viostor\2k19\amd64</Path>
        </PathAndCredentials>
        <PathAndCredentials wcm:action="add" wcm:keyValue="2">
            <Path>E:\virtio\NetKVM\2k19\amd64</Path>
        </PathAndCredentials>        
    </DriverPaths>

    After the OS installation is complete, the autounattend.xml file has one more critical job. The FirstLogonCommands section automatically runs our PowerShell script.

    <FirstLogonCommands>
        <SynchronousCommand wcm:action="add">
            <CommandLine>powershell.exe -ExecutionPolicy Bypass -File E:\Configure-WinRM.ps1</CommandLine>
            <Description>Install and Configure SSH</Description>
            <Order>1</Order>
        </SynchronousCommand>
    </FirstLogonCommands>    

    This script configures Windows Remote Management (WinRM), which opens a communication channel back to Packer. Once Packer can connect to the VM via WinRM, it takes over to run post-install configurations. In this example, it executes scripts to install Windows updates before finalizing the qcow2 image.

    From image to OpenShift VM

    Once Packer creates the qcow2 file, the final step is to make it available in OpenShift Virtualization:

    1. Upload the image: First, you upload the qcow2 file, which creates a Persistent Volume Claim (PVC) that serves as a bootable volume for your new virtual machines.
    2. Create a template (optional): For better reusability, you can create a custom VM template that points to this new bootable volume. This allows developers and operators to provision new, fully configured VMs with just a few clicks from the OpenShift console.

    For organizations managing multiple OpenShift clusters, distributing and managing these golden images can be streamlined using Red Hat Advanced Cluster Management (RHACM). Red Hat Advanced Cluster Management policies can automate the distribution of these templates and their associated resources across your entire fleet, ensuring consistency and saving significant operational effort. 

    The repository's README.md file provides detailed instructions for this, including an example of how to use Red Hat Advanced Cluster Management policies to distribute these images across multiple clusters.

    Simplify your VM images on OpenShift 

    By automating the VM image creation process, teams improve efficiency, consistency, and scalability. Whether you're a developer, a system administrator, or a DevOps engineer, the powerful combination of Packer and OpenShift Virtualization can help you streamline your workflows and make the most of your platform.

    If you're ready to streamline how you manage VM images on OpenShift, dive into the openshift-virt-packer repository. Clone it, run an example, and start building your own automated image pipeline today with Packer and OpenShift Virtualization. You can also find more information in the Packer and OpenShift Virtualization - Managing VMs documentation.

    To learn more about Red Hat OpenShift Virtualization, check out the 15 reasons to adopt Red Hat OpenShift Virtualization e-book and explore how to build a migration plan with Red Hat experts through the Virtualization Migration Assessment. 

    Related Posts

    • Windows image-building service for OpenShift Virtualization

    • OpenShift Virtualization unlocks cloud-native power for VMs

    • Live migrating VMs with OpenShift Virtualization

    • Storage considerations for OpenShift Virtualization

    • Why I switched from VMware to OpenShift Virtualization

    Recent Posts

    • Automate VM golden image builds for OpenShift with Packer

    • Setting up Intel TDX VMs with Trustee on OpenShift

    • Building and running Request Tracker as a quadlet container

    • Use OpenShift Lightspeed with locally served LLMs to drive security-focused, cost-efficient enterprise solutions for Red Hat products

    • 3 MCP servers you should be using (safely)

    What’s up next?

    The cost of human error and the advantages of automation - Share Image

    The cost of human error and the advantages of automation

    Red Hat
    Red Hat Developers logo LinkedIn YouTube Twitter Facebook

    Platforms

    • Red Hat AI
    • Red Hat Enterprise Linux
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform
    • See all products

    Build

    • Developer Sandbox
    • Developer tools
    • Interactive tutorials
    • API catalog

    Quicklinks

    • Learning resources
    • E-books
    • Cheat sheets
    • Blog
    • Events
    • Newsletter

    Communicate

    • About us
    • Contact sales
    • Find a partner
    • Report a website issue
    • Site status dashboard
    • Report a security problem

    RED HAT DEVELOPER

    Build here. Go anywhere.

    We serve the builders. The problem solvers who create careers with code.

    Join us if you’re a developer, software engineer, web designer, front-end designer, UX designer, computer scientist, architect, tester, product manager, project manager or team lead.

    Sign me up

    Red Hat legal and privacy links

    • About Red Hat
    • Jobs
    • Events
    • Locations
    • Contact Red Hat
    • Red Hat Blog
    • Inclusion at Red Hat
    • Cool Stuff Store
    • Red Hat Summit
    © 2025 Red Hat

    Red Hat legal and privacy links

    • Privacy statement
    • Terms of use
    • All policies and guidelines
    • Digital accessibility

    Report a website issue