#!/usr/bin/env bash # Builds a Proxmox VM template from the newest Ubuntu or Debian cloud image, with a cloud-init drive. # Run on the Proxmox host as root. Override anything below via env vars, e.g.: # VMID=9001 STORAGE=local-zfs CHANNEL=latest ./create-template.sh # DISTRO=debian VMID=9002 ./create-template.sh set -euo pipefail # ---------- config ---------- DISTRO="${DISTRO:-ubuntu}" # ubuntu or debian VMID="${VMID:-9000}" STORAGE="${STORAGE:-local-lvm}" # where the disk + cloud-init drive live BRIDGE="${BRIDGE:-vmbr0}" CORES="${CORES:-2}" MEMORY="${MEMORY:-2048}" # MiB DISK_SIZE="${DISK_SIZE:-20G}" # final root disk size (must be > image size, ~3.5G) ARCH="${ARCH:-amd64}" # amd64 or arm64 CHANNEL="${CHANNEL:-lts}" # ubuntu only: lts = newest LTS, latest = newest release incl. interim CI_USER="${CI_USER:-niklas}" CI_PASSWORD="${CI_PASSWORD:-}" # optional; leave empty to rely on SSH keys only SSH_KEY_FILE="${SSH_KEY_FILE:-$HOME/.ssh/authorized_keys}" INSTALL_QGA="${INSTALL_QGA:-1}" # bake qemu-guest-agent into the image (needs libguestfs-tools) SNIPPET_STORAGE="${SNIPPET_STORAGE:-local}" # fallback: install QGA via cloud-init vendor snippet stored here # ---------- sanity checks ---------- die() { echo "ERROR: $*" >&2; exit 1; } [[ $EUID -eq 0 ]] || die "run this as root on the Proxmox host" command -v qm >/dev/null || die "qm not found, this isn't a Proxmox host" command -v curl >/dev/null || die "curl is required" qm status "$VMID" &>/dev/null && die "VMID $VMID already exists, pick another one" pvesm status -storage "$STORAGE" &>/dev/null || die "storage '$STORAGE' doesn't exist" # ---------- find newest release ---------- case "$DISTRO" in ubuntu) BASE_URL="https://cloud-images.ubuntu.com/releases" echo "==> Looking up newest Ubuntu release ($CHANNEL)..." VERSIONS=$(curl -fsSL "$BASE_URL/" | grep -oE 'href="[0-9]{2}\.[0-9]{2}/"' | grep -oE '[0-9]{2}\.[0-9]{2}' | sort -uV) [[ -n "$VERSIONS" ]] || die "couldn't parse release list from $BASE_URL" if [[ "$CHANNEL" == "lts" ]]; then # LTS = even year, .04 VERSION=$(echo "$VERSIONS" | awk -F. '$1 % 2 == 0 && $2 == "04"' | tail -n1) else VERSION=$(echo "$VERSIONS" | tail -n1) fi [[ -n "$VERSION" ]] || die "no matching release found" IMG_NAME="ubuntu-${VERSION}-server-cloudimg-${ARCH}.img" IMG_URL="$BASE_URL/$VERSION/release/$IMG_NAME" SUMS_URL="$BASE_URL/$VERSION/release/SHA256SUMS" SUM_CMD=sha256sum ;; debian) echo "==> Looking up current Debian stable release..." RELEASE=$(curl -fsSL https://deb.debian.org/debian/dists/stable/Release) CODENAME=$(awk '/^Codename:/ {print $2; exit}' <<<"$RELEASE") VERSION=$(awk '/^Version:/ {split($2, v, "."); print v[1]; exit}' <<<"$RELEASE") [[ -n "$CODENAME" && -n "$VERSION" ]] || die "couldn't parse Debian stable Release file" BASE_URL="https://cloud.debian.org/images/cloud/${CODENAME}/latest" IMG_NAME="debian-${VERSION}-genericcloud-${ARCH}.qcow2" IMG_URL="$BASE_URL/$IMG_NAME" SUMS_URL="$BASE_URL/SHA512SUMS" SUM_CMD=sha512sum ;; *) die "DISTRO must be 'ubuntu' or 'debian', got '$DISTRO'" ;; esac echo "==> Using $DISTRO $VERSION ($IMG_NAME)" # ---------- download + verify ---------- WORKDIR=$(mktemp -d) trap 'rm -rf "$WORKDIR"' EXIT cd "$WORKDIR" echo "==> Downloading image..." curl -fL --progress-bar -o "$IMG_NAME" "$IMG_URL" curl -fsSL -o SUMS "$SUMS_URL" echo "==> Verifying checksum..." grep " \*\?${IMG_NAME}\$" SUMS | sed 's/\*//' | "$SUM_CMD" -c - || die "checksum mismatch, image is cooked" # ---------- optional: bake in qemu-guest-agent ---------- QGA_VIA_SNIPPET=0 if [[ "$INSTALL_QGA" == "1" ]]; then if ! command -v virt-customize >/dev/null; then echo "==> Installing libguestfs-tools for virt-customize..." apt-get update -qq && apt-get install -y -qq libguestfs-tools fi echo "==> Installing qemu-guest-agent into image..." # Work on a copy so a half-failed customize can't leave the image in a weird state cp "$IMG_NAME" custom.img if virt-customize -a custom.img \ --install qemu-guest-agent \ --truncate /etc/machine-id; then mv -f custom.img "$IMG_NAME" else rm -f custom.img echo "WARN: virt-customize couldn't install qemu-guest-agent (usually no network/DNS inside the libguestfs appliance)." echo " Falling back to installing it via cloud-init on first boot." QGA_VIA_SNIPPET=1 fi fi # ---------- create VM ---------- echo "==> Creating VM $VMID..." qm create "$VMID" \ --name "${DISTRO}-${VERSION}-template" \ --ostype l26 \ --cores "$CORES" \ --cpu host \ --memory "$MEMORY" \ --net0 "virtio,bridge=${BRIDGE}" \ --scsihw virtio-scsi-single \ --agent enabled=1 \ --serial0 socket \ --vga serial0 echo "==> Importing disk..." qm set "$VMID" --scsi0 "${STORAGE}:0,import-from=${WORKDIR}/${IMG_NAME},discard=on,ssd=1,iothread=1" qm set "$VMID" --boot order=scsi0 qm resize "$VMID" scsi0 "$DISK_SIZE" # ---------- cloud-init drive ---------- echo "==> Adding cloud-init drive..." qm set "$VMID" --ide2 "${STORAGE}:cloudinit" qm set "$VMID" --ciuser "$CI_USER" --ipconfig0 ip=dhcp if [[ -f "$SSH_KEY_FILE" ]]; then qm set "$VMID" --sshkeys "$SSH_KEY_FILE" else echo "WARN: no SSH key file at $SSH_KEY_FILE, skipping keys" fi if [[ -n "$CI_PASSWORD" ]]; then qm set "$VMID" --cipassword "$CI_PASSWORD" fi if [[ ! -f "$SSH_KEY_FILE" && -z "$CI_PASSWORD" ]]; then echo "WARN: no SSH key AND no password set. You won't be able to log in until you set one in the Cloud-Init tab." fi # ---------- fallback: QGA via cloud-init vendor data ---------- if [[ "$QGA_VIA_SNIPPET" == "1" ]]; then if pvesm status --content snippets 2>/dev/null | awk 'NR>1 {print $1}' | grep -qx "$SNIPPET_STORAGE"; then SNIPPET_REF="${SNIPPET_STORAGE}:snippets/qga-vendor.yaml" SNIPPET_PATH=$(pvesm path "$SNIPPET_REF") mkdir -p "$(dirname "$SNIPPET_PATH")" cat > "$SNIPPET_PATH" <<'EOF' #cloud-config packages: - qemu-guest-agent runcmd: - systemctl start qemu-guest-agent EOF # vendor data only adds to Proxmox's generated user data, so ciuser/sshkeys still apply qm set "$VMID" --cicustom "vendor=${SNIPPET_REF}" echo "==> Clones will install qemu-guest-agent on first boot (needs internet on the clone)." else echo "WARN: storage '$SNIPPET_STORAGE' doesn't allow snippets, so no guest agent fallback." echo " Enable it (add 'snippets' to the existing content list), e.g.:" echo " pvesm set $SNIPPET_STORAGE --content iso,vztmpl,backup,snippets" echo " then rerun, or just 'apt install qemu-guest-agent' inside each clone." fi fi # ---------- convert to template ---------- echo "==> Converting to template..." qm template "$VMID" echo echo "Done. Template $VMID = $DISTRO $VERSION" echo "Clone it with: qm clone $VMID --name --full && qm start "