Proxmox Ubuntu Cloud-Init Image Creation

Proxmox Ubuntu Cloud-Init Image Creation

This guide illustrates the steps to create a Proxmox Ubuntu cloud-init image.

1. Downloading the Base Ubuntu Image

Ubuntu provides base images that are regularly updated. Download the Ubuntu 20.04 Focal cloud image.

wget https://cloud-images.ubuntu.com/focal/current/focal-server-cloudimg-amd64.img

2. Install Packages

Install libguestfs-tools for modifying VM images. Then, install qemu-guest-agent into the downloaded image using virt-customize.

sudo apt update -y && sudo apt install libguestfs-tools -y
sudo virt-customize -a focal-server-cloudimg-amd64.img --install qemu-guest-agent

3. Create Proxmox Virtual Machine

Create a VM with basic resources, assign networking, import the image to storage, set boot disk, set cloud-init settings, and add a virtual serial port.

sudo qm create 9000 --name "ubuntu-2004-cloudinit-template" --memory 2048 --cores 2 --net0 virtio,bridge=vmbr0
sudo qm importdisk 9000 focal-server-cloudimg-amd64.img local-zfs
sudo qm set 9000 --scsihw virtio-scsi-pci --scsi0 local-zfs:vm-9000-disk-0
sudo qm set 9000 --boot c --bootdisk scsi0
sudo qm set 9000 --ide2 local-zfs:cloudinit
sudo qm set 9000 --serial0 socket --vga serial0
sudo qm set 9000 --agent enabled=1

4. Convert VM to a Template

After shutting down the VM, convert it to a template.

sudo qm template 9000

5. Clone the Template into a Full VM and Set Parameters

Clone the template and set SSH keys and IP address for the new VM.

sudo qm clone 9000 999 --name test-clone-cloud-init
sudo qm set 999 --sshkey ~/.ssh/id_rsa.pub
sudo qm set 999 --ipconfig0 ip=10.98.1.96/24,gw=10.98.1.1

Start the VM and log in via SSH.

sudo qm start 999
ssh ubuntu@10.98.1.96

Cleanup:

sudo qm stop 999 && sudo qm destroy 999
rm focal-server-cloudimg-amd64.img

6. Automating the Process

Automate this process by putting the above commands into a shell script and schedule it using cron.

Here's a simplified shell script to create a template.

#!/bin/bash
sudo apt update -y
sudo apt install libguestfs-tools -y
rm focal-server-cloudimg-amd64.img
wget https://cloud-images.ubuntu.com/focal/current/focal-server-cloudimg-amd64.img
sudo virt-customize -a focal-server-cloudimg-amd64.img --install qemu-guest-agent
sudo qm create 9000 --name "ubuntu-2004-cloudinit-template" --memory 2048 --cores 2 --net0 virtio,bridge=vmbr0
sudo qm importdisk 9000 focal-server-cloudimg-amd64.img local-zfs
sudo qm set 9000 --scsihw virtio-scsi-pci --scsi0 local-z