add option for debian
This commit is contained in:
+43
-22
@@ -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 <newid> --name <name> --full && qm start <newid>"
|
||||
Reference in New Issue
Block a user