Post

Proxmox cloud-init templates

Automate virtual machine provisioning with cloud templates and cloud init with proxmox.

Introduction

Cloud images are optimized images of a Linux distribution that are design to run in cloud environments. There are generally updates faster then the ISO images, and are slimmed down with some built in support for automated provisioning using cloud init.

Listed below are the official download links for some of the more common cloud images.

DistroReleaseDownloadChecksum
Debain12 Bookworm💿🔑
Debian11 Bullseye (Backports)💿🔑
Debian11 Bullseye💿🔑
Debian10 Buster💿🔑
Ubuntu20.04 LTS Focal Fossa💿🔑
Ubuntu18.04 LTS Bionic Beaver💿🔑
RockyLinux 9💿🔑
RockyLinux 8💿🔑
AlamaLinux 8💿🔑
AlamaLinux 9💿🔑
Kali2022.4💿🔑
Fedora38💿🔑


Creating your first VM template with a cloud image.

Its generally not good practice to install packages on your hypervisor. Downloads, checksum checking, and installing extra packages can be done on a separate PC and the final disk image can then be transferred to the hypervisor using SCP.

  • Download one of the distributions cloud images. Example below:
    1
    
     wget https://cloud.debian.org/images/cloud/bookworm/latest/debian-12-generic-amd64.qcow2
    
  • [Optional] I prefer my images to have a few essential packages installed, especially the qemu-guest-agent that is used by Proxmox. To do so you will need libguestfs-tools installed.
    1
    
     sudo apt install --no-install-recommends --no-install-suggests libguestfs-tools -y
    
  • Then use virt-cutomize to install your preferred packages.
    1
    2
    3
    4
    
     # Then add the required packages before importing the image. 
     CLOUD_IMAGE=debian-12-generic-amd64.qcow2
    
     sudo virt-customize -a $CLOUD_IMAGE --install qemu-guest-agent,lnav,ca-certificates,apt-transport-https,net-tools,dnsutils
    
  • If you have perform the above on a seperate PC then scp the modified disk image to the target hypervisor.
  • On proxmox start by setting variables to make the steps more generic and repeatable.
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    
    CLOUD_IMAGE=focal-server-cloudimg-amd64.img
    DNS1=1.1.1.1
    DNS2=8.8.8.8
    DNSSEARCH=karubits.com
    STORAGE=nvme-2tb
    TEMPLATE_NAME=debian-12-cloud-template
    VM_TEMPLATE_ID=9001
    VM_USER=karubits
      
    # Using read prevents the password from been saved in bash history
    read -rs -p "Enter password: " VM_PASSWORD
    
  • Create the initial template with the minimum values. You can adjust these to suit your environment.
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    
    qm create $VM_TEMPLATE_ID \
      --name $TEMPLATE_NAME \
      --core 2 \
      --memory 2048 \
      --net0 virtio,bridge=vmbr0 \
      --ide2 $STORAGE:cloudinit \
      --serial0 socket \
      --vga serial0 \
      --onboot 1 \
      --agent 1,fstrim_cloned_disks=1 \
      --tablet 0 \
      --ostype l26
    
  • Import the downloaded disk image into the new VM template.
    1
    2
    3
    
    qm importdisk $VM_TEMPLATE_ID \
        $CLOUD_IMAGE \
        $STORAGE
    
  • Add the virtual disk controller and attache the imported disk to the template.
    1
    2
    3
    4
    5
    
    qm set $VM_TEMPLATE_ID \
      --scsihw virtio-scsi-pci \
      --scsi0 $STORAGE:vm-$VM_TEMPLATE_ID-disk-0,discard=on,ssd=1 \
      --boot c \
      --bootdisk scsi0
    
  • Configure the default values for your cloud-init template. (This can also be done on the Promxox UI)
    1
    2
    3
    4
    5
    6
    
    qm set $VM_TEMPLATE_ID \
      --nameserver="$DNS1 $DNS2" \
      --searchdomain=$DNSSEARCH \
      --ipconfig0=ip=dhcp \
      --ciuser=$VM_USER \
      --cipassword=$VM_PASSWORD
    
  • Lastly, convert the VM into a template with the following command.
    1
    
    qm template $VM_TEMPLATE_ID
    

Create a new VM from the template for testing

  • Before cloning the VM I use a variable to find the next available VM ID as the clone command requires you to set an ID. Then take a full a clone of the new template.
    1
    2
    3
    4
    5
    
    NEXT_VM=$(pvesh get /cluster/nextid)
    
    qm clone $VM_TEMPLATE_ID $NEXT_VM \
      --full=true \
      --name=wow-so-quick-to-deploy
    
  • As the default image size is very small (e.g. 247mb for Debian), expand the disk to make the VM useful and then start the VM:
    1
    2
    
    qm resize $NEXT_VM scsi0 +15G
    qm start $NEXT_VM
    

References

  1. Proxmox Wiki - Cloud-Init Support
  2. Proxmox VE - How to build an Ubuntu 22.04 Template (Updated Method) - Video
  3. Proxmox VE – How to build an Ubuntu 22.04 Template (Updated Method) - Blog
This post is licensed under CC BY 4.0 by the author.

© Karubits. Some rights reserved.

Follow your curiosity.