From dda3541f973769a259639128443eb8e4d6c9f342 Mon Sep 17 00:00:00 2001 From: Niklas Haiden Date: Mon, 2 Nov 2026 23:45:31 +0100 Subject: [PATCH] add option for debian --- README.md | 17 +++++++++--- create-template.sh | 65 ++++++++++++++++++++++++++++++---------------- 2 files changed, 56 insertions(+), 26 deletions(-) diff --git a/README.md b/README.md index a7a038a..82a78b0 100644 --- a/README.md +++ b/README.md @@ -1,11 +1,13 @@ # Proxmox Create Template -Script to create a Proxmox template VM using the newest Ubuntu Cloud Image, with a cloud-init drive. +Script to create a Proxmox template VM using the newest Ubuntu or Debian Cloud Image, with a cloud-init drive. ## What it does -1. Looks up the newest Ubuntu release on `cloud-images.ubuntu.com` (newest LTS by default). -2. Downloads the cloud image and verifies its SHA256 checksum. +1. Looks up the newest release: + - **Ubuntu**: from `cloud-images.ubuntu.com` (newest LTS by default) + - **Debian**: current stable (from `deb.debian.org`), `genericcloud` image from `cloud.debian.org` +2. Downloads the cloud image and verifies its checksum (SHA256 for Ubuntu, SHA512 for Debian). 3. Optionally bakes `qemu-guest-agent` into the image with `virt-customize` (installs `libguestfs-tools` if missing). 4. Creates the VM (virtio NIC, virtio-scsi-single, `cpu host`, guest agent enabled, serial console). 5. Imports the image as `scsi0` (discard, ssd, iothread) and resizes it to `DISK_SIZE`. @@ -30,6 +32,12 @@ Override any setting via environment variables: VMID=9001 STORAGE=local-zfs CHANNEL=latest ./create-template.sh ``` +Debian template: + +```bash +DISTRO=debian VMID=9002 ./create-template.sh +``` + Then clone the template: ```bash @@ -40,6 +48,7 @@ qm clone 9000 --name --full && qm start | Variable | Default | Description | |-------------------|-----------------------------|-------------| +| `DISTRO` | `ubuntu` | `ubuntu` or `debian` | | `VMID` | `9000` | ID of the template VM (must not exist yet) | | `STORAGE` | `local-lvm` | Storage for the disk and cloud-init drive | | `BRIDGE` | `vmbr0` | Network bridge | @@ -47,7 +56,7 @@ qm clone 9000 --name --full && qm start | `MEMORY` | `2048` | Memory in MiB | | `DISK_SIZE` | `20G` | Final root disk size (must be larger than the image, ~3.5G) | | `ARCH` | `amd64` | `amd64` or `arm64` | -| `CHANNEL` | `lts` | `lts` = newest LTS, `latest` = newest release incl. interim | +| `CHANNEL` | `lts` | Ubuntu only: `lts` = newest LTS, `latest` = newest release incl. interim. Debian always uses stable | | `CI_USER` | `niklas` | Cloud-init default user | | `CI_PASSWORD` | *(empty)* | Optional password; leave empty to rely on SSH keys only | | `SSH_KEY_FILE` | `$HOME/.ssh/authorized_keys`| Public keys injected via cloud-init | diff --git a/create-template.sh b/create-template.sh index da6aabb..a52abde 100644 --- a/create-template.sh +++ b/create-template.sh @@ -1,10 +1,12 @@ #!/usr/bin/env bash -# Builds a Proxmox VM template from the newest Ubuntu cloud image, with a cloud-init drive. +# 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-ubuntu-template.sh +# 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}" @@ -12,13 +14,12 @@ 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}" # lts = newest LTS, latest = newest release incl. interim +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 -BASE_URL="https://cloud-images.ubuntu.com/releases" # ---------- sanity checks ---------- die() { echo "ERROR: $*" >&2; exit 1; } @@ -30,22 +31,42 @@ qm status "$VMID" &>/dev/null && die "VMID $VMID already exists, pick another on 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" +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" + 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)" + 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) @@ -54,10 +75,10 @@ cd "$WORKDIR" echo "==> Downloading image..." curl -fL --progress-bar -o "$IMG_NAME" "$IMG_URL" -curl -fsSL -o SHA256SUMS "$SUMS_URL" +curl -fsSL -o SUMS "$SUMS_URL" echo "==> Verifying checksum..." -grep " \*\?${IMG_NAME}\$" SHA256SUMS | sed 's/\*//' | sha256sum -c - || die "checksum mismatch, image is cooked" +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 @@ -84,7 +105,7 @@ fi # ---------- create VM ---------- echo "==> Creating VM $VMID..." qm create "$VMID" \ - --name "ubuntu-${VERSION}-template" \ + --name "${DISTRO}-${VERSION}-template" \ --ostype l26 \ --cores "$CORES" \ --cpu host \ @@ -148,5 +169,5 @@ echo "==> Converting to template..." qm template "$VMID" echo -echo "Done. Template $VMID = Ubuntu $VERSION" +echo "Done. Template $VMID = $DISTRO $VERSION" echo "Clone it with: qm clone $VMID --name --full && qm start " \ No newline at end of file