# download ubuntu-cloud image
# NOTE: ubuntu-server won't properly work with cloud-init for some reason
wget https://cloud-images.ubuntu.com/focal/current/focal-server-cloudimg-amd64.img

# NOTE: no need for sudo because we already login as root

# install image customization tool
apt update -y && apt install libguestfs-tools -y

# install qemu-guest-agent inside of image
virt-customize -a focal-server-cloudimg-amd64.img --install qemu-guest-agent

# (optionally) edit cloud.cfg
# I usually add "package_update: true" and "packages: \n - nano \n - curl \n - wget"
# more examples: https://cloudinit.readthedocs.io/en/latest/topics/examples.html
export EDITOR=nano
virt-edit -a focal-server-cloudimg-amd64.img /etc/cloud/cloud.cfg

# NOTE: proxmox docs for further steps: https://pve.proxmox.com/wiki/Cloud-Init_Support

# create VM (number=100) with 2 cores, 2GB of RAM, and default networking
qm create 100 --memory 2048 --cores 2 --net0 virtio,bridge=vmbr0

# import image as a disk
# NOTE: you might want to change "local-lvm" to "local-zfs"
qm importdisk 100 focal-server-cloudimg-amd64.img local-lvm
qm set 100 --scsihw virtio-scsi-pci --scsi0 local-lvm:vm-100-disk-0

# set up cloud-init
qm set 100 --ide2 local-lvm:cloudinit
qm set 100 --serial0 socket --vga serial0

# set boot order
qm set 100 --boot c --bootdisk scsi0

# enable qemu-guest-agent
qm set 100 –-agent 1

# convert VM into a template
qm template 100