Based on https://gist.github.com/chriswayg/b6421dcc69cb3b7e41f2998f1150e1df
Ubuntu 20.04 Focal Fossa
TEMPLATE_VM_ID=20001
TEMPLATE_NAME=ubuntu2004-template
TEMPLATE_MEM=2048
TEMPLATE_DISK=8G
TEMPLATE_NET=virtio
TEMPLATE_NET_BRIDGE=vmbr0
TEST_VM_ID=201
ISO_SRC_PATH=https://cloud-images.ubuntu.com/focal/current/focal-server-cloudimg-amd64.img
ISO_DEST_PATH=/mnt/pve/proxmox-images/template/iso/focal-server-cloudimg-amd64.img
VM_STORAGE_NAME=proxmox-vms
PATH_TO_SSH_PUBKEY=~/.ssh/id_rsa.pub
# Download ISO
if [ ! -e "${ISO_DEST_PATH}" ]; then
curl -o "${ISO_DEST_PATH}" "${ISO_SRC_PATH}"
fi
# Create a VM
qm create "${TEMPLATE_VM_ID}" --name "${TEMPLATE_NAME}" --memory $TEMPLATE_MEM --net0 $TEMPLATE_NET,bridge=$TEMPLATE_NET_BRIDGE
# Import the disk in qcow2 format (as unused disk)
qm importdisk "${TEMPLATE_VM_ID}" "${ISO_DEST_PATH}" "${VM_STORAGE_NAME}" -format qcow2
# Attach the disk to the vm using VirtIO SCSI
qm set "${TEMPLATE_VM_ID}" --scsihw virtio-scsi-pci --scsi0 "/mnt/pve/${VM_STORAGE_NAME}/images/${TEMPLATE_VM_ID}/vm-${TEMPLATE_VM_ID}-disk-0.qcow2"
qm set "${TEMPLATE_VM_ID}" --ide2 ${VM_STORAGE_NAME}:cloudinit --boot c --bootdisk scsi0 --serial0 socket --vga serial0
# The initial disk is only 2GB
qm resize "${TEMPLATE_VM_ID}" scsi0 ${TEMPLATE_DISK}
# Set networking to DHCP
qm set "${TEMPLATE_VM_ID}" --ipconfig0 ip=dhcp
# user authentication for 'ubuntu' user
qm set "${TEMPLATE_VM_ID}" --sshkey "${PATH_TO_SSH_PUBKEY}"
# check the cloud-init config
qm cloudinit dump "${TEMPLATE_VM_ID}" user
# Output should look like:
# #cloud-config
# hostname: ubuntu20.04-template
# manage_etc_hosts: true
# ssh_authorized_keys:
# - ssh-ed25519 <public ssh key info here>
# chpasswd:
# expire: False
# users:
# - default
# package_upgrade: true
# Create template
qm template $TEMPLATE_VM_ID
To test:
# Create a linked clone
qm clone $TEMPLATE_VM_ID $TEST_VM_ID --name ubuntu2004-1
qm start $TEST_VM_ID
# Create a clone on a different VM host
TEST_VM_ID=202
TEST_VM_NAME=ubuntu2004-2
TARGET_REMOTE_VMHOST=vmhost2
qm clone $TEMPLATE_VM_ID $TEST_VM_ID --name $TEST_VM_NAME --target $TARGET_REMOTE_VMHOST
Agent
sudo apt -y install qemu-guest-agent
Docker stuff
sudo apt update
sudo apt -y install \
apt-transport-https \
ca-certificates \
curl \
gnupg \
lsb-release
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
echo \
"deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt update
sudo apt -y install docker-ce docker-ce-cli containerd.io