commit f3607940a8dcd031abdf8255003648d54978b0bb Author: Niklas Haiden Date: Sun Oct 18 13:26:59 2026 +0200 init commit diff --git a/README.md b/README.md new file mode 100644 index 0000000..ba7dde9 --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +# Proxmox Create Template + +Script to create a proxmox template VM using a Ubuntu Cloud Image. diff --git a/create-template.sh b/create-template.sh new file mode 100644 index 0000000..33f4319 --- /dev/null +++ b/create-template.sh @@ -0,0 +1,117 @@ +#!/usr/bin/env bash +# Builds a Proxmox VM template from the newest Ubuntu 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-ubuntu-template.sh +set -euo pipefail + +# ---------- config ---------- +VMID="${VMID:-9000}" +STORAGE="${STORAGE:-local-lvm}" # where the disk + cloud-init drive live +BRIDGE="${BRIDGE:-vmbr0}" +CORES="${CORES:-2}" +MEMORY="${MEMORY:-1024}" # 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}" # 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) +BASE_URL="https://cloud-images.ubuntu.com/releases" + +# ---------- 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 ---------- +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" +echo "==> Using Ubuntu $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 SHA256SUMS "$SUMS_URL" + +echo "==> Verifying checksum..." +grep " \*\?${IMG_NAME}\$" SHA256SUMS | sed 's/\*//' | sha256sum -c - || die "checksum mismatch, image is cooked" + +# ---------- optional: bake in qemu-guest-agent ---------- +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..." + virt-customize -a "$IMG_NAME" \ + --install qemu-guest-agent \ + --truncate /etc/machine-id +fi + +# ---------- create VM ---------- +echo "==> Creating VM $VMID..." +qm create "$VMID" \ + --name "ubuntu-${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 + +# ---------- convert to template ---------- +echo "==> Converting to template..." +qm template "$VMID" + +echo +echo "Done. Template $VMID = Ubuntu $VERSION" +echo "Clone it with: qm clone $VMID --name --full && qm start " \ No newline at end of file