How to create a Cloud Init Image of Ubuntu

Cloud init images are great for ProxMox Templates and for launching machines via Terraform. 

This code is also available here: https://gitlab.petermatra.design/gitlab-instance-5e679b60/How-to-create-ubuntu-cloudinit-image

Ubuntu images are available here:  https://cloud-images.ubuntu.com

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

You need to install libguestfstools to customize the image.

sudo apt update -y && sudo apt install libguestfs-tools -y

 Once the tools are installed you can start customizing your image:

sudo virt-customize -a focal-server-cloudimg-amd64.img --install qemu-guest-agent
sudo virt-customize -a focal-server-cloudimg-amd64.img --run-command 'useradd pmatra'
sudo virt-customize -a focal-server-cloudimg-amd64.img --run-command 'mkdir -p /home/pmatra/.ssh'
sudo virt-customize -a focal-server-cloudimg-amd64.img --ssh-inject pmatra:file:/home/pmatra/.ssh/authorized_keys
sudo virt-customize -a focal-server-cloudimg-amd64.img --run-command 'chown -R pmatra:pmatra /home/pmatra'
sudo virt-customize -a focal-server-cloudimg-amd64.img --root-password password:PASSWORD
sudo virt-customize -a focal-server-cloudimg-amd64.img --run-command 'echo /etc/sudoers >> pmatra ALL=(ALL) NOPASSWD:ALL'

After all of that is done if you want to load it into Proxmox as a template, you need to upload it to your Proxmox Server and run this code.

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

If you want to clone the image manually you can do it this way via ProxMox Shell or SSH

sudo qm clone 9000 999 --name test-clone-cloud-init sudo qm set 999 --ipconfig0 ip=10.98.1.96/24,gw=10.98.1.1 sudo qm start 999