DEV Community


Posted on • Updated on

Create Proxmox cloud-init templates for use with Packer

My latest project has been to re-create my homelab using cloud native principles such as Infrastructure-as-Code and Dev(Git)Ops.

After much research and deliberation, I've concluded the best way for me to achieve this is to separate my local hardware from the software which will run on it. Essentially, I'll be treating my hardware as a "local cloud", and create VM Images that I will deploy running my application stack. (Azure Stack HCI has the right idea here!)

When developing and testing on cloud providers, they always have a base image available to use as a starting off point. Not so for my local cloud!

In this article, I'll demonstrate how to create a cloud-init enabled Ubuntu 20.04 LTS base image to use on Proxmox VE.

Cloud Native Image

There are several guides out there which utilize the "proxmox-iso" packer builder to build a base image from an ISO file. Modern Linux distributions are increasingly moving away from this install method and preseed files. Rather, disk images are provided with the OS pre-installed, and configuration is performed via cloud-init. We will create a Proxmox KVM base image using Ubuntu's KVM cloud image.

NOTE: Since the Packer Proxmox builder doesn't support LXD images nor templates, we need to create a KVM template.

Proxmox Script

The Proxmox API doesn't appear to offer the full functionality provided by the native shell commands to create a template, so we will run a script via SSH.
This gist contains the script, and I'll step through it below.

Download the image

We are downloading the kvm disk image. Note that this is a qcow2 image format with an extension of .img
Promxox doesn't like this so we rename the disk image to .qcow2

Enter fullscreen mode Exit fullscreen mode

Add QEMU Guest Agent

The Ubuntu 20.04 image we are going to use does not include the qemu-guest-agent package which is needed for the Guest VM to report its IP details back to Proxmox. This is required for Packer to communicate with the VM after cloning. the template. libguestfs-tools will allow us to embed qemu-guest-agent into the image. You can also add any additional packages you'd like in your base image. Personally, I prefer to customize this base image later with packer so that the packages can live in source control.

apt update
apt install -y libguestfs-tools
virt-customize --install qemu-guest-agent -a $IMG_NAME
Enter fullscreen mode Exit fullscreen mode

Create a VM in Proxmox with required settings and convert to template

For best performance, virtio 'hardware' should be used. Additionally, cloud-init requires a serial console and cloudinit IDE (CDROM) drive. We will set the network config to DHCP so that we get an IP address. Lastly, we will expand the template disk image size so we have space to install items later. It appears packer doesn't support doing this later.

qm create $VMID --name $TEMPL_NAME --memory $MEM --net0 virtio,bridge=$NET_BRIDGE
qm importdisk $VMID $IMG_NAME $DISK_STOR
qm set $VMID --scsihw virtio-scsi-pci --scsi0 $DISK_STOR:vm-$VMID-disk-0
qm set $VMID --ide2 $DISK_STOR:cloudinit
qm set $VMID --boot c --bootdisk scsi0
qm set $VMID --serial0 socket --vga serial0
qm set $VMID --ipconfig0 ip=dhcp
qm resize $VMID scsi0 $DISK_SIZE
qm template $VMID
# Remove downloaded image
Enter fullscreen mode Exit fullscreen mode

Packer template

Now that we have our cloud-init enabled image on Proxmox, we can use Packer to create a template based off of this template.
Ensure to set the scsi_controller="virtio-scsi-pci" and qemu_agent=true.

I'd recommend adding the Proxmox variables to a var file.

packer build --var-file=./proxmox.pkvars.hcl --var "proxox_template_name=test-output-template" --var "proxmox_source_template=ubuntu2004-cloud" base.pkr.hcl
Enter fullscreen mode Exit fullscreen mode

Next Steps

Now that you've created a template using packer from the base template, you can use Terraform to deploy that VM!
I'm still working on that myself :)


Top comments (0)