Compare commits

...
6 Commits
6 changed files with 413 additions and 8 deletions

No files matched your search

+1
View File
@@ -0,0 +1 @@
*.sh text eol=lf
Executable
+16
View File
@@ -0,0 +1,16 @@
#!/usr/bin/env bash
cd "$(dirname "$0")" && source ./lib.sh
VM_ID=5000
VM_NAME="AlmaLinux-9"
CPU_CORE=2
CPU_SOCKET=1
RAM=4096
DISK=30
IMAGE_URL="https://repo.almalinux.org/almalinux/9/cloud/x86_64/images/AlmaLinux-9-GenericCloud-latest.x86_64.qcow2"
PACKAGES=""
DOCKER=0
# Alma keeps root on partition 4 with XFS
GROW_CMD="growpart /dev/sda 4 && xfs_growfs /"
build_template
+38 -7
View File
@@ -1,15 +1,45 @@
# Run the image maker # prox-mal: Proxmox templates from the newest Ubuntu LTS
Open the console or ssh in to your Proxmox node.
Open the console or SSH into your Proxmox node as root:
```sh ```sh
apt install git apt install -y git
git clone https://git.skui.io/steffen/prox-mal.git git clone https://git.skui.io/steffen/prox-mal
cd prox-mal/ cd prox-mal/
./all.sh bash install.sh
``` ```
Or build a single template: `./Ubuntu-LTS-Small.sh` The installer:
1. Finds the newest supported Ubuntu LTS that has a cloud image (from `changelogs.ubuntu.com/meta-release-lts`), then downloads it and checks its SHA256 checksum.
2. Asks for a template name (default e.g. `ubuntu-26-04`).
3. Lists the active storage that can hold VM disks and lets you pick one.
4. Asks how many templates to build. For each one you set the name suffix, VM ID, vCPU, RAM, disk size and whether to install Docker. Templates 1-3 default to small / medium / large:
| Preset | vCPU | RAM | Disk | Docker |
|---|---|---|---|---|
| small | 2 | 2 GB | 32 GB | no |
| medium | 2 | 4 GB | 50 GB | yes |
| large | 4 | 8 GB | 100 GB | yes |
5. Shows a summary and asks before it creates anything.
Each template is built from its own copy of the image. Every template gets `qemu-guest-agent`, cloud-init (DHCP) and an empty machine-id. VM IDs that are already in use anywhere in the cluster are rejected.
Optional environment variables: `BRIDGE` (default `vmbr0`) and `WORKDIR` (image cache, default `/var/tmp/prox-mal`).
Requires Proxmox VE 7.2 or newer (`import-from`).
After cloning a template, set a user and key before the first boot:
```sh
qm clone 9000 200 --name my-vm --full
qm set 200 --ciuser ubuntu --sshkeys ~/.ssh/id_rsa.pub
```
## Legacy scripts
`all.sh`, `Ubuntu-LTS-*.sh`, `Ubuntu-22-04-LTS.sh` and `Debian.sh` are the old fixed-size scripts (hard-coded storage `rust`/`local-lvm` and IDs). They are kept for reference. Use `install.sh` instead.
## Fixed templates (all.sh)
The older per-distro scripts are still here. `./all.sh` builds them all, or run one, e.g. `./Ubuntu-LTS-Small.sh`.
## Templates ## Templates
@@ -18,6 +48,7 @@ Or build a single template: `./Ubuntu-LTS-Small.sh`
| Ubuntu-LTS-Small.sh | 2000 | no | | Ubuntu-LTS-Small.sh | 2000 | no |
| Ubuntu-LTS-Medium.sh | 3000 | yes | | Ubuntu-LTS-Medium.sh | 3000 | yes |
| Ubuntu-LTS-Large.sh | 4000 | yes | | Ubuntu-LTS-Large.sh | 4000 | yes |
| Alma.sh (AlmaLinux 9, for FreeIPA) | 5000 | no |
| Debian.sh (Debian 13) | 9998 | yes | | Debian.sh (Debian 13) | 9998 | yes |
| Ubuntu-22-04-LTS.sh | 9999 | yes | | Ubuntu-22-04-LTS.sh | 9999 | yes |
+1
View File
@@ -3,6 +3,7 @@
set -e set -e
cd "$(dirname "$0")" cd "$(dirname "$0")"
./Debian.sh ./Debian.sh
./Alma.sh
./Ubuntu-LTS-Small.sh ./Ubuntu-LTS-Small.sh
./Ubuntu-LTS-Medium.sh ./Ubuntu-LTS-Medium.sh
./Ubuntu-LTS-Large.sh ./Ubuntu-LTS-Large.sh
Executable
+349
View File
@@ -0,0 +1,349 @@
#!/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"
# The cloud image's root filesystem is only ~2 GB, too small for Docker.
# Grow the image by 4 GB and the root partition with it before installing.
# Classic eth0/eth1 NIC names: Proxmox cloud-init configures eth0..ethN, and
# Ubuntu 26.04 brings NICs up in the initramfs, so cloud-init can't rename them
# later and static IPs never apply on first boot.
local -a customize=(--run-command 'growpart /dev/sda 1 && resize2fs /dev/sda1'
--install qemu-guest-agent
--write '/etc/default/grub.d/99-net-ifnames.cfg:GRUB_CMDLINE_LINUX_DEFAULT="$GRUB_CMDLINE_LINUX_DEFAULT net.ifnames=0 biosdevname=0"'
--run-command 'update-grub')
info "[$id] Preparing image for $name"
cp "$BASE_IMAGE" "$image"
qemu-img resize -q "$image" +4G
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 "$@"
+8 -1
View File
@@ -5,6 +5,7 @@
# IMAGE_URL - cloud image to start from # IMAGE_URL - cloud image to start from
# PACKAGES - extra apt packages, comma separated # PACKAGES - extra apt packages, comma separated
# DOCKER=1 - install Docker from get.docker.com # DOCKER=1 - install Docker from get.docker.com
# GROW_CMD - grows the root fs (default: ext4 on sda1)
# #
# Override where it lands with environment variables: # Override where it lands with environment variables:
# STORAGE=ssd BRIDGE=vmbr1 ./Ubuntu-LTS-Small.sh # STORAGE=ssd BRIDGE=vmbr1 ./Ubuntu-LTS-Small.sh
@@ -29,6 +30,11 @@ build_template() {
local disk="$work/disk.qcow2" local disk="$work/disk.qcow2"
cp "$IMAGE_DIR/$(basename "$IMAGE_URL")" "$disk" cp "$IMAGE_DIR/$(basename "$IMAGE_URL")" "$disk"
# The cloud image's root filesystem is only ~2 GB, too small for Docker.
# Grow the image by 4 GB and the root partition with it.
qemu-img resize -q "$disk" +4G
virt-customize -a "$disk" --run-command "${GROW_CMD:-growpart /dev/sda 1 && resize2fs /dev/sda1}"
# Guest agent, extra packages, optional Docker, and an empty machine-id so clones get their own DHCP lease # Guest agent, extra packages, optional Docker, and an empty machine-id so clones get their own DHCP lease
virt-customize -a "$disk" --install "qemu-guest-agent${PACKAGES:+,$PACKAGES}" virt-customize -a "$disk" --install "qemu-guest-agent${PACKAGES:+,$PACKAGES}"
if [ "${DOCKER:-0}" = 1 ]; then if [ "${DOCKER:-0}" = 1 ]; then
@@ -36,7 +42,8 @@ build_template() {
fi fi
virt-customize -a "$disk" --truncate /etc/machine-id virt-customize -a "$disk" --truncate /etc/machine-id
qm create "$VM_ID" --name "$VM_NAME" --net0 "virtio,bridge=$BRIDGE" # cpu host: AlmaLinux 9 needs x86-64-v2 and won't boot on the old kvm64 default
qm create "$VM_ID" --name "$VM_NAME" --cpu host --net0 "virtio,bridge=$BRIDGE"
qm set "$VM_ID" --cores "$CPU_CORE" --sockets "$CPU_SOCKET" --memory "$RAM" qm set "$VM_ID" --cores "$CPU_CORE" --sockets "$CPU_SOCKET" --memory "$RAM"
qm set "$VM_ID" --scsihw virtio-scsi-pci --scsi0 "$STORAGE:0,import-from=$disk,discard=on" qm set "$VM_ID" --scsihw virtio-scsi-pci --scsi0 "$STORAGE:0,import-from=$disk,discard=on"
qm set "$VM_ID" --ide2 "$STORAGE:cloudinit" qm set "$VM_ID" --ide2 "$STORAGE:cloudinit"