Skip to content

Instantly share code, notes, and snippets.

@rgevaert
Last active February 16, 2023 04:12
Show Gist options
  • Save rgevaert/4b6a72438c94bfc069b98a4190db9962 to your computer and use it in GitHub Desktop.
Save rgevaert/4b6a72438c94bfc069b98a4190db9962 to your computer and use it in GitHub Desktop.
#!/bin/bash
export LC_CTYPE=en_US.UTF-8
export LC_ALL=en_US.UTF-8
# Must run on Proxmox VE 7 server
# Not sure how to handle a cluster - either run on each node or copy template after creating on one?
# e.g. $ ssh [email protected] < proxmox-create-cloud-template.sh
SRC_IMG="https://cloud-images.ubuntu.com/focal/current/focal-server-cloudimg-amd64-disk-kvm.img"
IMG_NAME="focal-server-cloudimg-amd64-disk-kvm.qcow2"
TEMPL_NAME="ubuntu2004-cloud"
VMID="9000"
MEM="2048"
DISK_SIZE="20G"
DISK_STOR="local-lvm"
NET_BRIDGE="vmbr0"
SSH_KEY="/root/.ssh/id_ed25519.pub"
# Install libguesetfs-tools to modify cloud image
apt update
apt install -y libguestfs-tools
# Download kvm image and rename
# Ubuntu img is actually qcow2 format and Proxmox doesn't like wrong extensions
wget -O $IMG_NAME $SRC_IMG
# Ubuntu cloud img doesn't include qemu-guest-agent required for packer to get IP details from proxmox
# Add any additional packages you want installed in the template
virt-customize --install qemu-guest-agent -a $IMG_NAME
# Create cloud-init enabled Proxmox VM with DHCP addressing
qm create $VMID --name $TEMPL_NAME --memory $MEM --net0 virtio,bridge=$NET_BRIDGE
qm importdisk $VMID $IMG_NAME $DISK_STOR
qm set $VMID --scsihw virtio-scsi-pci --scsi0 $DISK_STOR:vm-$VMID-disk-0
qm set $VMID --ide2 $DISK_STOR:cloudinit
qm set $VMID --boot c --bootdisk scsi0
qm set $VMID --serial0 socket --vga serial0
qm set $VMID --ipconfig0 ip=dhcp
qm set $VMID --scsi0 "$DISK_STOR:vm-$VMID-disk-0,discard=on,media=disk"
qm resize $VMID scsi0 $DISK_SIZE
# Convert to template
qm template $VMID
# Enable discard because thin lvm and other options suitable for me
qm set $VMID --sshkey $SSH_KEY
qm set $VMID --ciuser ansible
qm set $VMID -onboot 1
qm set $VMID -agent 1
# Remove downloaded image
rm $IMG_NAME
# Next, use packer to clone this template and customize it!
# References
# https://gist.github.com/chriswayg/43fbea910e024cbe608d7dcb12cb8466
# https://whattheserver.com/proxmox-cloud-init-os-template-creation/
# https://norocketscience.at/deploy-proxmox-virtual-machines-using-cloud-init/
# https://pve.proxmox.com/wiki/Cloud-Init_Support
# https://blog.dustinrue.com/2020/05/going-deeper-with-proxmox-cloud-init/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment