add option for debian
This commit is contained in:
@@ -1,11 +1,13 @@
|
|||||||
# Proxmox Create Template
|
# 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
|
## What it does
|
||||||
|
|
||||||
1. Looks up the newest Ubuntu release on `cloud-images.ubuntu.com` (newest LTS by default).
|
1. Looks up the newest release:
|
||||||
2. Downloads the cloud image and verifies its SHA256 checksum.
|
- **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).
|
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).
|
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`.
|
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
|
VMID=9001 STORAGE=local-zfs CHANNEL=latest ./create-template.sh
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Debian template:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
DISTRO=debian VMID=9002 ./create-template.sh
|
||||||
|
```
|
||||||
|
|
||||||
Then clone the template:
|
Then clone the template:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
@@ -40,6 +48,7 @@ qm clone 9000 <newid> --name <name> --full && qm start <newid>
|
|||||||
|
|
||||||
| Variable | Default | Description |
|
| Variable | Default | Description |
|
||||||
|-------------------|-----------------------------|-------------|
|
|-------------------|-----------------------------|-------------|
|
||||||
|
| `DISTRO` | `ubuntu` | `ubuntu` or `debian` |
|
||||||
| `VMID` | `9000` | ID of the template VM (must not exist yet) |
|
| `VMID` | `9000` | ID of the template VM (must not exist yet) |
|
||||||
| `STORAGE` | `local-lvm` | Storage for the disk and cloud-init drive |
|
| `STORAGE` | `local-lvm` | Storage for the disk and cloud-init drive |
|
||||||
| `BRIDGE` | `vmbr0` | Network bridge |
|
| `BRIDGE` | `vmbr0` | Network bridge |
|
||||||
@@ -47,7 +56,7 @@ qm clone 9000 <newid> --name <name> --full && qm start <newid>
|
|||||||
| `MEMORY` | `2048` | Memory in MiB |
|
| `MEMORY` | `2048` | Memory in MiB |
|
||||||
| `DISK_SIZE` | `20G` | Final root disk size (must be larger than the image, ~3.5G) |
|
| `DISK_SIZE` | `20G` | Final root disk size (must be larger than the image, ~3.5G) |
|
||||||
| `ARCH` | `amd64` | `amd64` or `arm64` |
|
| `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_USER` | `niklas` | Cloud-init default user |
|
||||||
| `CI_PASSWORD` | *(empty)* | Optional password; leave empty to rely on SSH keys only |
|
| `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 |
|
| `SSH_KEY_FILE` | `$HOME/.ssh/authorized_keys`| Public keys injected via cloud-init |
|
||||||
|
|||||||
+30
-9
@@ -1,10 +1,12 @@
|
|||||||
#!/usr/bin/env bash
|
#!/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.:
|
# 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
|
set -euo pipefail
|
||||||
|
|
||||||
# ---------- config ----------
|
# ---------- config ----------
|
||||||
|
DISTRO="${DISTRO:-ubuntu}" # ubuntu or debian
|
||||||
VMID="${VMID:-9000}"
|
VMID="${VMID:-9000}"
|
||||||
STORAGE="${STORAGE:-local-lvm}" # where the disk + cloud-init drive live
|
STORAGE="${STORAGE:-local-lvm}" # where the disk + cloud-init drive live
|
||||||
BRIDGE="${BRIDGE:-vmbr0}"
|
BRIDGE="${BRIDGE:-vmbr0}"
|
||||||
@@ -12,13 +14,12 @@ CORES="${CORES:-2}"
|
|||||||
MEMORY="${MEMORY:-2048}" # MiB
|
MEMORY="${MEMORY:-2048}" # MiB
|
||||||
DISK_SIZE="${DISK_SIZE:-20G}" # final root disk size (must be > image size, ~3.5G)
|
DISK_SIZE="${DISK_SIZE:-20G}" # final root disk size (must be > image size, ~3.5G)
|
||||||
ARCH="${ARCH:-amd64}" # amd64 or arm64
|
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_USER="${CI_USER:-niklas}"
|
||||||
CI_PASSWORD="${CI_PASSWORD:-}" # optional; leave empty to rely on SSH keys only
|
CI_PASSWORD="${CI_PASSWORD:-}" # optional; leave empty to rely on SSH keys only
|
||||||
SSH_KEY_FILE="${SSH_KEY_FILE:-$HOME/.ssh/authorized_keys}"
|
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)
|
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
|
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 ----------
|
# ---------- sanity checks ----------
|
||||||
die() { echo "ERROR: $*" >&2; exit 1; }
|
die() { echo "ERROR: $*" >&2; exit 1; }
|
||||||
@@ -30,6 +31,9 @@ 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"
|
pvesm status -storage "$STORAGE" &>/dev/null || die "storage '$STORAGE' doesn't exist"
|
||||||
|
|
||||||
# ---------- find newest release ----------
|
# ---------- find newest release ----------
|
||||||
|
case "$DISTRO" in
|
||||||
|
ubuntu)
|
||||||
|
BASE_URL="https://cloud-images.ubuntu.com/releases"
|
||||||
echo "==> Looking up newest Ubuntu release ($CHANNEL)..."
|
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)
|
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"
|
[[ -n "$VERSIONS" ]] || die "couldn't parse release list from $BASE_URL"
|
||||||
@@ -45,7 +49,24 @@ fi
|
|||||||
IMG_NAME="ubuntu-${VERSION}-server-cloudimg-${ARCH}.img"
|
IMG_NAME="ubuntu-${VERSION}-server-cloudimg-${ARCH}.img"
|
||||||
IMG_URL="$BASE_URL/$VERSION/release/$IMG_NAME"
|
IMG_URL="$BASE_URL/$VERSION/release/$IMG_NAME"
|
||||||
SUMS_URL="$BASE_URL/$VERSION/release/SHA256SUMS"
|
SUMS_URL="$BASE_URL/$VERSION/release/SHA256SUMS"
|
||||||
echo "==> Using Ubuntu $VERSION ($IMG_NAME)"
|
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 ----------
|
# ---------- download + verify ----------
|
||||||
WORKDIR=$(mktemp -d)
|
WORKDIR=$(mktemp -d)
|
||||||
@@ -54,10 +75,10 @@ cd "$WORKDIR"
|
|||||||
|
|
||||||
echo "==> Downloading image..."
|
echo "==> Downloading image..."
|
||||||
curl -fL --progress-bar -o "$IMG_NAME" "$IMG_URL"
|
curl -fL --progress-bar -o "$IMG_NAME" "$IMG_URL"
|
||||||
curl -fsSL -o SHA256SUMS "$SUMS_URL"
|
curl -fsSL -o SUMS "$SUMS_URL"
|
||||||
|
|
||||||
echo "==> Verifying checksum..."
|
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 ----------
|
# ---------- optional: bake in qemu-guest-agent ----------
|
||||||
QGA_VIA_SNIPPET=0
|
QGA_VIA_SNIPPET=0
|
||||||
@@ -84,7 +105,7 @@ fi
|
|||||||
# ---------- create VM ----------
|
# ---------- create VM ----------
|
||||||
echo "==> Creating VM $VMID..."
|
echo "==> Creating VM $VMID..."
|
||||||
qm create "$VMID" \
|
qm create "$VMID" \
|
||||||
--name "ubuntu-${VERSION}-template" \
|
--name "${DISTRO}-${VERSION}-template" \
|
||||||
--ostype l26 \
|
--ostype l26 \
|
||||||
--cores "$CORES" \
|
--cores "$CORES" \
|
||||||
--cpu host \
|
--cpu host \
|
||||||
@@ -148,5 +169,5 @@ echo "==> Converting to template..."
|
|||||||
qm template "$VMID"
|
qm template "$VMID"
|
||||||
|
|
||||||
echo
|
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>"
|
echo "Clone it with: qm clone $VMID <newid> --name <name> --full && qm start <newid>"
|
||||||
Reference in New Issue
Block a user