Add interactive install.sh: newest Ubuntu LTS, storage probe, N templates with sizes

This commit is contained in:
steffen committed 2026-10-30 10:02:18 +01:00
1 parent c8f9576225
commit b92d4f6924
3 files changed
+374 -10

No files matched your search

Executable
+340
View File
@@ -0,0 +1,340 @@
#!/usr/bin/env bash
#
# prox-mal installer: builds Proxmox VM templates from the newest Ubuntu LTS
# cloud image.
#
# Run on a Proxmox node as root:
# ./install.sh
#
# The script asks for:
# 1. a base name for the templates
# 2. which storage to put the disks on (probed from `pvesm status`)
# 3. how many templates to build
# 4. the size of each template (vCPU, RAM, disk) and whether to install Docker
#
# Optional environment overrides:
# BRIDGE network bridge for net0 (default: vmbr0)
# WORKDIR where the cloud image is cached (default: /var/tmp/prox-mal)
set -euo pipefail
BRIDGE="${BRIDGE:-vmbr0}"
WORKDIR="${WORKDIR:-/var/tmp/prox-mal}"
UBUNTU_LTS_FEED="https://changelogs.ubuntu.com/meta-release-lts"
CLOUD_IMAGES="https://cloud-images.ubuntu.com"
# Default sizes offered for template 1, 2, 3 (and 3 again for any beyond):
# suffix vCPU RAM(MB) disk(GB) docker
PRESETS=(
"small 2 2048 32 n"
"medium 2 4096 50 y"
"large 4 8192 100 y"
)
# ---------------------------------------------------------------------------
# Output and prompt helpers. Prompts read from /dev/tty so the script also
# works when piped in (curl ... | bash).
# ---------------------------------------------------------------------------
info() { printf '\e[1;34m==>\e[0m %s\n' "$*"; }
warn() { printf '\e[1;33m!!\e[0m %s\n' "$*" >&2; }
die() { printf '\e[1;31mxx\e[0m %s\n' "$*" >&2; exit 1; }
# ask VAR "Question" "default" -> sets VAR to the answer, or the default if empty.
ask() {
local __var=$1 __question=$2 __default=${3:-} __answer
if [[ -n $__default ]]; then
read -r -p "$__question [$__default]: " __answer </dev/tty
else
read -r -p "$__question: " __answer </dev/tty
fi
printf -v "$__var" '%s' "${__answer:-$__default}"
}
# ask_int VAR "Question" default min -> like ask, but repeats until a whole number >= min.
ask_int() {
local __var=$1 __question=$2 __default=$3 __min=$4 __value
while true; do
ask __value "$__question" "$__default"
if [[ $__value =~ ^[0-9]+$ ]] && (( __value >= __min )); then
printf -v "$__var" '%s' "$__value"
return
fi
warn "Enter a whole number, at least $__min."
done
}
# ask_yn VAR "Question" y|n -> sets VAR to y or n.
ask_yn() {
local __var=$1 __question=$2 __default=$3 __value
while true; do
ask __value "$__question (y/n)" "$__default"
case ${__value,,} in
y|yes) printf -v "$__var" y; return ;;
n|no) printf -v "$__var" n; return ;;
esac
warn "Answer y or n."
done
}
# ---------------------------------------------------------------------------
# Preflight
# ---------------------------------------------------------------------------
preflight() {
[[ $EUID -eq 0 ]] || die "Run as root on the Proxmox node."
command -v qm >/dev/null || die "qm not found - this must run on a Proxmox node."
command -v pvesm >/dev/null || die "pvesm not found - this must run on a Proxmox node."
command -v curl >/dev/null || die "curl is required."
if ! command -v virt-customize >/dev/null; then
info "Installing libguestfs-tools (provides virt-customize)..."
apt-get update -qq && apt-get install -y libguestfs-tools
fi
}
# ---------------------------------------------------------------------------
# Image: find the newest supported Ubuntu LTS that has a cloud image, then
# download and verify it.
# ---------------------------------------------------------------------------
# Prints "codename<TAB>version" for every supported LTS, newest first.
supported_lts_releases() {
curl -fsSL "$UBUNTU_LTS_FEED" | awk '
/^Dist:/ { dist = $2 }
/^Version:/ { version = $2 }
/^Supported:/ { if ($2 == 1) found[++n] = dist "\t" version }
END { for (i = n; i >= 1; i--) print found[i] }
'
}
# Sets UBUNTU_CODENAME, UBUNTU_VERSION and IMAGE_URL to the newest LTS
# whose cloud image is actually published.
find_latest_lts() {
local codename version
while IFS=$'\t' read -r codename version; do
local url="$CLOUD_IMAGES/$codename/current/$codename-server-cloudimg-amd64.img"
if curl -fsIL "$url" >/dev/null 2>&1; then
UBUNTU_CODENAME=$codename
UBUNTU_VERSION=$version
IMAGE_URL=$url
return
fi
warn "Ubuntu $version ($codename) has no cloud image yet, trying an older LTS."
done < <(supported_lts_releases)
die "Could not find a supported Ubuntu LTS cloud image."
}
# Downloads the image into WORKDIR (reusing a cached copy if its checksum
# still matches) and sets BASE_IMAGE to its path.
download_image() {
local file="$UBUNTU_CODENAME-server-cloudimg-amd64.img"
local sums_url="$CLOUD_IMAGES/$UBUNTU_CODENAME/current/SHA256SUMS"
local expected
mkdir -p "$WORKDIR"
BASE_IMAGE="$WORKDIR/$file"
expected=$(curl -fsSL "$sums_url" | awk -v f="*$file" '$2 == f { print $1 }')
[[ -n $expected ]] || die "No checksum for $file in $sums_url"
if [[ -f $BASE_IMAGE ]] && echo "$expected $BASE_IMAGE" | sha256sum -c --status; then
info "Using cached image $BASE_IMAGE (checksum OK)."
return
fi
info "Downloading $IMAGE_URL"
curl -fL --progress-bar -o "$BASE_IMAGE" "$IMAGE_URL"
echo "$expected $BASE_IMAGE" | sha256sum -c --status \
|| die "Checksum mismatch for $BASE_IMAGE - download corrupted, delete it and retry."
}
# ---------------------------------------------------------------------------
# Storage: list storages that are active and can hold VM disks.
# ---------------------------------------------------------------------------
choose_storage() {
local -a names=() lines=()
local name type status total used avail _rest
# pvesm status columns: Name Type Status Total Used Available % (sizes in KiB)
while read -r name type status total used avail _rest; do
[[ $status == active ]] || continue
names+=("$name")
lines+=("$(printf '%-20s %-10s %8s GiB free' "$name" "$type" "$(( avail / 1024 / 1024 ))")")
done < <(pvesm status --content images 2>/dev/null | tail -n +2)
(( ${#names[@]} > 0 )) || die "No active storage with 'images' content found."
echo
info "Storage that can hold VM disks:"
local i
for i in "${!names[@]}"; do
printf ' %d) %s\n' "$(( i + 1 ))" "${lines[i]}"
done
local pick
while true; do
ask_int pick "Choose storage" 1 1
(( pick <= ${#names[@]} )) && break
warn "Pick a number between 1 and ${#names[@]}."
done
STORAGE=${names[pick - 1]}
}
# ---------------------------------------------------------------------------
# VM IDs
# ---------------------------------------------------------------------------
# True if the ID is free across the whole cluster (and not already picked
# earlier in this run).
vmid_is_free() {
local id=$1 used
for used in "${CHOSEN_IDS[@]:-}"; do
[[ $used == "$id" ]] && return 1
done
pvesh get /cluster/nextid --vmid "$id" >/dev/null 2>&1
}
# Prints the first free ID at or above the given start.
next_free_vmid() {
local id=$1
until vmid_is_free "$id"; do id=$(( id + 1 )); done
echo "$id"
}
# ---------------------------------------------------------------------------
# Template configuration
# ---------------------------------------------------------------------------
# Fills the T_* arrays with one entry per template.
configure_templates() {
local count i preset suffix cores ram disk docker id default_id=9000
T_NAME=() T_ID=() T_CORES=() T_RAM=() T_DISK=() T_DOCKER=() CHOSEN_IDS=()
echo
ask_int count "How many templates do you want" 3 1
for (( i = 0; i < count; i++ )); do
preset=${PRESETS[i < ${#PRESETS[@]} ? i : ${#PRESETS[@]} - 1]}
read -r suffix cores ram disk docker <<<"$preset"
(( i >= ${#PRESETS[@]} )) && suffix="t$(( i + 1 ))"
echo
info "Template $(( i + 1 )) of $count"
ask suffix " Name suffix" "$suffix"
default_id=$(next_free_vmid "$default_id")
while true; do
ask_int id " VM ID" "$default_id" 100
vmid_is_free "$id" && break
warn "VM ID $id is already in use."
done
ask_int cores " vCPU cores" "$cores" 1
ask_int ram " RAM (MB)" "$ram" 512
ask_int disk " Disk (GB)" "$disk" 10
ask_yn docker " Install Docker" "$docker"
T_NAME+=("$TEMPLATE_NAME-$suffix")
T_ID+=("$id")
T_CORES+=("$cores")
T_RAM+=("$ram")
T_DISK+=("$disk")
T_DOCKER+=("$docker")
CHOSEN_IDS+=("$id")
default_id=$(( id + 1 ))
done
}
print_summary() {
local i
echo
info "Ubuntu $UBUNTU_VERSION ($UBUNTU_CODENAME) on storage '$STORAGE', bridge '$BRIDGE':"
printf ' %-6s %-40s %5s %8s %8s %7s\n' ID NAME vCPU RAM DISK DOCKER
for i in "${!T_ID[@]}"; do
printf ' %-6s %-40s %5s %6sMB %6sGB %7s\n' \
"${T_ID[i]}" "${T_NAME[i]}" "${T_CORES[i]}" "${T_RAM[i]}" "${T_DISK[i]}" "${T_DOCKER[i]}"
done
echo
}
# ---------------------------------------------------------------------------
# Build
# ---------------------------------------------------------------------------
# Builds one template from its own copy of the base image, so templates never
# inherit each other's customisations.
build_template() {
local id=$1 name=$2 cores=$3 ram=$4 disk=$5 docker=$6
local image="$WORKDIR/build-$id.img"
local -a customize=(--install qemu-guest-agent)
info "[$id] Preparing image for $name"
cp "$BASE_IMAGE" "$image"
if [[ $docker == y ]]; then
customize+=(--run-command 'curl -fsSL https://get.docker.com | sh')
fi
# Empty machine-id so every clone generates its own on first boot.
customize+=(--truncate /etc/machine-id)
virt-customize -a "$image" "${customize[@]}"
info "[$id] Creating VM"
qm create "$id" \
--name "$name" \
--ostype l26 \
--cores "$cores" --sockets 1 --cpu host \
--memory "$ram" \
--net0 "virtio,bridge=$BRIDGE" \
--scsihw virtio-scsi-pci \
--scsi0 "$STORAGE:0,import-from=$image" \
--ide2 "$STORAGE:cloudinit" \
--boot order=scsi0 \
--serial0 socket --vga serial0 \
--ipconfig0 ip=dhcp \
--agent enabled=1
qm resize "$id" scsi0 "${disk}G"
qm template "$id"
rm -f "$image"
info "[$id] Template $name is ready."
}
main() {
preflight
info "Looking up the newest Ubuntu LTS..."
find_latest_lts
info "Found Ubuntu $UBUNTU_VERSION ($UBUNTU_CODENAME)."
echo
# Default "ubuntu-26-04" from version "26.04.1".
local default_name
default_name="ubuntu-$(cut -d. -f1,2 <<<"$UBUNTU_VERSION" | tr . -)"
ask TEMPLATE_NAME "Template name" "$default_name"
# Proxmox VM names must be DNS-safe: replace anything else with '-'.
TEMPLATE_NAME=$(tr -c 'A-Za-z0-9.\n-' '-' <<<"$TEMPLATE_NAME")
choose_storage
configure_templates
print_summary
local go
ask_yn go "Build these templates" y
[[ $go == y ]] || die "Aborted - nothing was created."
download_image
local i
for i in "${!T_ID[@]}"; do
build_template "${T_ID[i]}" "${T_NAME[i]}" "${T_CORES[i]}" \
"${T_RAM[i]}" "${T_DISK[i]}" "${T_DOCKER[i]}"
done
echo
info "Done. Set a user and SSH key on each clone before first boot, e.g.:"
echo " qm clone ${T_ID[0]} 200 --name my-vm --full"
echo " qm set 200 --ciuser ubuntu --sshkeys ~/.ssh/id_rsa.pub"
}
main "$@"