Step-by-Step Guide: Building Custom Proxmox Images with Packer
Tired of setting up virtual machines in Proxmox over and over? Imagine having a ready-to-go, perfectly configured VM image that you can deploy in minutes. Welcome to automation heaven!
In this guide, I'll walk you through a step-by-step approach to using Packer to create reusable VM images in your Proxmox environment. With this setup, you'll save time, reduce errors, and have a streamlined environment that’s ready whenever you are.
Why Automate with Packer?
Provisioning VMs manually isn’t just tedious; it can lead to inconsistencies that impact your entire infrastructure. Using Packer, we can automate the VM creation process, so every image is configured exactly as needed every time. This not only simplifies deployment but also ensures that all your VMs are consistent and ready to use.
What You'll Need
This guide assumes you're working with a fresh installation of Proxmox VE and have some understanding of virtualization concepts. Familiarity with command-line interfaces and basic configuration files will also be beneficial
My Environment
- Proxmox Server Version: 8.2.7
- Requirements:
- Ubuntu 24.04 Server Image - Ensure you have a clean Ubuntu 24.04 server image ready for creating virtual machines.
- Packer v1.11.2 - Download and install Packer if it's not already installed. Packer is a tool for automating the creation of VM images.
- Code Editor: Use a code editor like Visual Studio Code (VS Code) for editing configuration files and scripts.
Additional Prerequisites
- Network Access: Verify that your Proxmox server has internet access if needed for downloads or updates.
- Sufficient Storage: Ensure the Proxmox server has enough disk space for the Ubuntu image and VM builds.
- Access to Proxmox Web UI and CLI: Familiarize yourself with accessing both the Proxmox web interface and its command-line interface for tasks not supported in the GUI.
Ready to Dive In?
In this guide, you’ll not only learn how to create automated VM images with Packer, but you’ll also gain insights into best practices for maintaining a Proxmox environment. By the end, you'll have a reusable, reliable VM image that saves you time, boosts efficiency, and frees you from repetitive manual setup. Let’s get started!
Download Ubuntu Image
- The first step is to download the image onto your Proxmos server. Do this by navigating to your server. Select the appropriate storage and Download from the URL.
This will download the image on this drive
URL can be generated by navigating to the official website and copying the link.
2. Navigate to and copy the URL of the .ISO https://releases.ubuntu.com/24.04/
- Hit download and wait for it to finish
Clone the main repo for this project
Next, you'll want to clone the repo for this project. Follow the commands below
- Clone the main repository:
git clone https://github.com/phipcode/phiptechblog.git
- Navigate to the directory:
cd phiptechblog/packer/proxmox/image_provision
Generate an API Token in Proxmox
- To create an API token for Proxmox, log in to the Proxmox web interface and navigate to the "Datacenter" section.
Under the "Permissions" tab, create a new user if needed. Then, go to the "Tokens" section for that user and click "Add" to generate a new token. Name the token and set the required permissions, then copy the token value securely for use in your Packer configuration.
This token allows you to authenticate requests to the Proxmox API for automation and management tasks. Copy this password in variables.pkr.hcl
Editing the Template: Hardware and Disk Size
Once you have generated your Proxmox API token, you can customize your Packer template to suit your virtual machine requirements. Key parameters to edit include:
File to edit: ubuntu-server-focal.pkr.hcl
- Hardware Settings: Adjust the
cores
andmemory
parameters to allocate the desired number of CPU cores and memory for your VM. These settings impact the performance of the virtual machine and should be tailored based on your workload needs. - Disk Size: Modify the
disk_size
variable to define the storage capacity for your VM. Ensure that the allocated size meets the requirements of the applications you intend to run on the server. - Storage Pool: Specify the storage pool in the
storage_pool
setting to designate where the VM's disk will be stored. If you're using LVM, ensure the pool name corresponds to your LVM configuration.
Run Packer Build Command
Execute the following command exactly as shown. It will use the variables from the specified file to build your template:
packer build -var-file='./variables.pkr.hcl' .\ubuntu-server-focal.pkr.hcl
Monitor Packer Build
Next, you'll want to monitor the progress of the build to ensure everything runs smoothly.
Packer will output real-time logs, allowing you to track each step as it creates your virtual machine. Look out for any warnings or errors in the logs that might indicate issues with configuration or resource limits.
My template below took about 6 minutes to build as you can see below.
If the build completes successfully, Packer window console will close, and your new VM template will be available in Proxmox.
ubuntu-server-focal.proxmox-iso.ubuntu-server-focal-template: output will be in this color.
==> ubuntu-server-focal.proxmox-iso.ubuntu-server-focal-template: Creating VM
==> ubuntu-server-focal.proxmox-iso.ubuntu-server-focal-template: Starting VM
==> ubuntu-server-focal.proxmox-iso.ubuntu-server-focal-template: Starting HTTP server on port 8802
==> ubuntu-server-focal.proxmox-iso.ubuntu-server-focal-template: Waiting 5s for boot
==> ubuntu-server-focal.proxmox-iso.ubuntu-server-focal-template: Typing the boot command
==> ubuntu-server-focal.proxmox-iso.ubuntu-server-focal-template: Waiting for SSH to become available...
==> ubuntu-server-focal.proxmox-iso.ubuntu-server-focal-template: Connected to SSH!
==> ubuntu-server-focal.proxmox-iso.ubuntu-server-focal-template: Provisioning with shell script: C:\Users\lilga\AppData\Local\Temp\packer-shell3746226589
ubuntu-server-focal.proxmox-iso.ubuntu-server-focal-template: Waiting for cloud-init...
ubuntu-server-focal.proxmox-iso.ubuntu-server-focal-template: Waiting for cloud-init...
ubuntu-server-focal.proxmox-iso.ubuntu-server-focal-template: Waiting for cloud-init...
ubuntu-server-focal.proxmox-iso.ubuntu-server-focal-template: Waiting for cloud-init...
==> ubuntu-server-focal.proxmox-iso.ubuntu-server-focal-template:
==> ubuntu-server-focal.proxmox-iso.ubuntu-server-focal-template: WARNING: apt does not have a stable CLI interface. Use with caution in scripts.
==> ubuntu-server-focal.proxmox-iso.ubuntu-server-focal-template:
ubuntu-server-focal.proxmox-iso.ubuntu-server-focal-template: Reading package lists...
ubuntu-server-focal.proxmox-iso.ubuntu-server-focal-template: Building dependency tree...
ubuntu-server-focal.proxmox-iso.ubuntu-server-focal-template: Reading state information...
ubuntu-server-focal.proxmox-iso.ubuntu-server-focal-template: 0 upgraded, 0 newly installed, 0 to remove and 1 not upgraded.
==> ubuntu-server-focal.proxmox-iso.ubuntu-server-focal-template:
==> ubuntu-server-focal.proxmox-iso.ubuntu-server-focal-template: WARNING: apt does not have a stable CLI interface. Use with caution in scripts.
==> ubuntu-server-focal.proxmox-iso.ubuntu-server-focal-template:
==> ubuntu-server-focal.proxmox-iso.ubuntu-server-focal-template:
==> ubuntu-server-focal.proxmox-iso.ubuntu-server-focal-template: WARNING: apt does not have a stable CLI interface. Use with caution in scripts.
==> ubuntu-server-focal.proxmox-iso.ubuntu-server-focal-template:
ubuntu-server-focal.proxmox-iso.ubuntu-server-focal-template: Reading package lists...
ubuntu-server-focal.proxmox-iso.ubuntu-server-focal-template: Building dependency tree...
ubuntu-server-focal.proxmox-iso.ubuntu-server-focal-template: Reading state information...
==> ubuntu-server-focal.proxmox-iso.ubuntu-server-focal-template: Uploading files/99-pve.cfg => /tmp/99-pve.cfg
==> ubuntu-server-focal.proxmox-iso.ubuntu-server-focal-template: Provisioning with shell script: C:\Users\lilga\AppData\Local\Temp\packer-shell3952884564
==> ubuntu-server-focal.proxmox-iso.ubuntu-server-focal-template: Stopping VM
==> ubuntu-server-focal.proxmox-iso.ubuntu-server-focal-template: Converting VM to template
==> ubuntu-server-focal.proxmox-iso.ubuntu-server-focal-template: Adding a cloud-init cdrom in storage pool local-lvm
Build 'ubuntu-server-focal.proxmox-iso.ubuntu-server-focal-template' finished after 6 minutes 49 seconds.
==> Wait completed after 6 minutes 49 seconds
==> Builds finished. The artifacts of successful builds are:
--> ubuntu-server-focal.proxmox-iso.ubuntu-server-focal-template: A template was created: 250
Validation
Ensure the new template appears in your Proxmox node to validate that the build completed successfully. Navigate to the Proxmox web interface, go to Datacenter > Node > VMs, and confirm that the VM template is listed. This verifies that Packer created and stored the template correctly.
Build VM - Manually
Now you'll want to validate if the image you created is actually working so let's go and test it out.
- Right click the template and Clone
- Give it a name and a VM ID then hit the clone button
- Before starting the VM, set the IP configuration to DHCP to avoid connection issues due to default IP settings in the 172.x.x.x range. You can do this by going to Cloud-Init > IP Config and setting the IP to DHCP.
While this manual setup works for now, I'll create a follow-up guide to automate the VM provisioning process, including the DHCP configuration, so that manual steps aren’t necessary.
Start the VM
With the IP configured, you can now start the VM. Use the password specified in variables.pkr.hcl
to log in. Congratulations—you’ve successfully built and launched a VM using a Packer-created image!
Additional Configuration Steps
Changing the Default Password
To change the default password used in the VM image, follow these steps:
- Edit the SSH Password in
variables.pkr.hcl
- Open the
variables.pkr.hcl
file - Locate the
ssh_password
parameter and update it with your desired password.
ssh_password = "your_new_password"
- Update the Password in the
user-data
File:
- Open the
user-data
file. - Locate the
passwd
parameter and update it with the hashed version of your password.
To generate a SHA-512 hash, run the following command:bashCopy codemkpasswd -m sha-512
mkpasswd -m sha-512
In addition to changing the password, you can enhance security by using SSH key-based authentication, which allows for a more secure and convenient way to log in to your VM. By generating a public-private key pair, you can add the public key to the user-data
file, enabling access without the need for a password. This method not only improves security by reducing the risk of brute-force attacks but also streamlines the login process, making it especially useful for automated scripts and remote management tasks. I won't cover this here.
Found this article useful? Why not buy Phi a coffee to show your appreciation?