Contents

Create Proxmox cloud-init template

Overview

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

The tradition 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.

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 or Proxmox node’s GUI shell.

The Script you can found on my GitHub. There is the Link.

The below sections, I will explain this Script step by steps.

Step 1: Download the image

We are downloading the kvm disk image.

Note: This is a qcow2 image format with an extension of .img, Promxox doesn’t like this so we rename the disk image to .qcow2

1
2
3
SRC_IMG="https://cloud-images.ubuntu.com/focal/current/focal-server-cloudimg-amd64-disk-kvm.img"
IMG_NAME="focal-server-cloudimg-amd64-disk-kvm.qcow2"
wget -O $IMG_NAME $SRC_IMG

Step 2: 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.

1
2
3
apt update
apt install -y libguestfs-tools
virt-customize --install qemu-guest-agent -a $IMG_NAME

Step 3: 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.

you will need to change the user name, password, and add the ssh public key so we can connect to the VM later using Ansible and terraform. update the variables and click on Regenerate Image

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
TEMPL_NAME="ubuntu2004-cloud"
VMID="9000"
MEM="512"
DISK_SIZE="32G"
DISK_STOR="local-lvm"
NET_BRIDGE="vmbr0"
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
rm $IMG_NAME

References

  1. https://gist.github.com/chriswayg/43fbea910e024cbe608d7dcb12cb8466
  2. https://whattheserver.com/proxmox-cloud-init-os-template-creation/
  3. https://norocketscience.at/deploy-proxmox-virtual-machines-using-cloud-init/
  4. https://pve.proxmox.com/wiki/Cloud-Init%5FSupport
  5. https://blog.dustinrue.com/2020/05/going-deeper-with-proxmox-cloud-init/
  6. https://gist.github.com/mike1237/cce83a74f898b11c2cec911204568cf9