Compare commits
2
Commits
dev
..
24e624234b
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
24e624234b | ||
|
|
3306959dcd |
No files matched your search
@@ -1 +0,0 @@
|
|||||||
*.sh text eol=lf
|
|
||||||
@@ -1,26 +1,14 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
cd "$(dirname "$0")" && source ./lib.sh
|
||||||
|
|
||||||
VM_ID=9998
|
VM_ID=9998
|
||||||
VM_NAME="Debian-12-Large-Docker"
|
VM_NAME="Debian-13-Large-Docker"
|
||||||
CPU_CORE=4
|
CPU_CORE=4
|
||||||
CPU_SOCKET=2
|
CPU_SOCKET=2
|
||||||
RAM=8192
|
RAM=8192
|
||||||
DISK=100
|
DISK=100
|
||||||
#getting latest image"
|
IMAGE_URL="https://cloud.debian.org/images/cloud/trixie/latest/debian-13-generic-amd64.qcow2"
|
||||||
wget https://cdimage.debian.org/images/cloud/bookworm/20250210-2019/debian-12-generic-amd64-20250210-2019.qcow2
|
PACKAGES=""
|
||||||
#installing qemu guest agent and remove HW ID
|
DOCKER=1
|
||||||
virt-customize -a debian-12-generic-amd64-20250210-2019.qcow2 --install qemu-guest-agent,neofetch
|
|
||||||
virt-customize -a debian-12-generic-amd64-20250210-2019.qcow2 --run-command "curl -fsSL https://get.docker.com -o get-docker.sh
|
build_template
|
||||||
sh get-docker.sh"
|
|
||||||
virt-customize -a debian-12-generic-amd64-20250210-2019.qcow2 --run-command "echo -n > /etc/machine-id"
|
|
||||||
qm create $VM_ID --name $VM_NAME --net0 virtio,bridge=vmbr0
|
|
||||||
qm set $VM_ID --cores "$CPU_CORE" --sockets "$CPU_SOCKET" --memory "$RAM"
|
|
||||||
qm importdisk "$VM_ID" debian-12-generic-amd64-20250210-2019.qcow2 local-lvm
|
|
||||||
qm set "$VM_ID" --scsihw virtio-scsi-pci --scsi0 local-lvm:vm-"$VM_ID"-disk-0
|
|
||||||
qm set "$VM_ID" --ide2 local-lvm:cloudinit
|
|
||||||
qm set "$VM_ID" --boot c --bootdisk scsi0
|
|
||||||
qm set "$VM_ID" --serial0 socket --vga serial0
|
|
||||||
qm resize $VM_ID scsi0 "$DISK"G
|
|
||||||
qm set "$VM_ID" --ipconfig0 ip=dhcp
|
|
||||||
qm set "$VM_ID" --agent enabled=1
|
|
||||||
qm template "$VM_ID"
|
|
||||||
rm debian-12-generic-amd64-20250210-2019.qcow2 #clean iso
|
|
||||||
echo "Template er ferdig med id "$VM_ID""
|
|
||||||
@@ -1,41 +1,40 @@
|
|||||||
|
|
||||||
# prox-mal: Proxmox templates from the newest Ubuntu LTS
|
# Run the image maker
|
||||||
|
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 -y git
|
apt install git
|
||||||
git clone https://git.skui.io/steffen/prox-mal
|
git clone https://git.skui.io/steffen/prox-mal.git
|
||||||
cd prox-mal/
|
cd prox-mal/
|
||||||
bash install.sh
|
./all.sh
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
The installer:
|
Or build a single template: `./Ubuntu-LTS-Small.sh`
|
||||||
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 |
|
## Templates
|
||||||
|---|---|---|---|---|
|
|
||||||
| 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.
|
| Script | VM ID | Docker |
|
||||||
|
|---|---|---|
|
||||||
|
| Ubuntu-LTS-Small.sh | 2000 | no |
|
||||||
|
| Ubuntu-LTS-Medium.sh | 3000 | yes |
|
||||||
|
| Ubuntu-LTS-Large.sh | 4000 | yes |
|
||||||
|
| Debian.sh (Debian 13) | 9998 | yes |
|
||||||
|
| Ubuntu-22-04-LTS.sh | 9999 | yes |
|
||||||
|
|
||||||
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.
|
Each script only sets its sizes and image; the build itself is in `lib.sh`. Set `DOCKER=0`
|
||||||
|
in a script to skip Docker. The image is cached in `/var/lib/vz/template/cloud` and each
|
||||||
|
script works on its own copy, so they can run in any order.
|
||||||
|
|
||||||
Optional environment variables: `BRIDGE` (default `vmbr0`) and `WORKDIR` (image cache, default `/var/tmp/prox-mal`).
|
## Where the templates land
|
||||||
Requires Proxmox VE 7.2 or newer (`import-from`).
|
|
||||||
|
Defaults fit pvebox: storage `ssd`, network `vmbr1` (VM-Ekstern). Override per run:
|
||||||
|
|
||||||
After cloning a template, set a user and key before the first boot:
|
|
||||||
```sh
|
```sh
|
||||||
qm clone 9000 200 --name my-vm --full
|
STORAGE=tank BRIDGE=intern ./Ubuntu-LTS-Large.sh
|
||||||
qm set 200 --ciuser ubuntu --sshkeys ~/.ssh/id_rsa.pub
|
|
||||||
```
|
```
|
||||||
|
|
||||||
## Legacy scripts
|
A script stops if its VM ID already exists. Remove the old template first:
|
||||||
`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.
|
`qm destroy 2000 --purge`
|
||||||
|
|
||||||
<details>
|
<details>
|
||||||
<summary>Source Doc </summary>
|
<summary>Source Doc </summary>
|
||||||
|
|||||||
Regular → Executable
+8
-20
@@ -1,26 +1,14 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
cd "$(dirname "$0")" && source ./lib.sh
|
||||||
|
|
||||||
VM_ID=9999
|
VM_ID=9999
|
||||||
VM_NAME="Ubuntu-22-04-LTS-Large-Docker"
|
VM_NAME="Ubuntu-22-04-LTS-Large-Docker"
|
||||||
CPU_CORE=4
|
CPU_CORE=4
|
||||||
CPU_SOCKET=2
|
CPU_SOCKET=2
|
||||||
RAM=8192
|
RAM=8192
|
||||||
DISK=100
|
DISK=100
|
||||||
#getting latest image"
|
IMAGE_URL="https://cloud-images.ubuntu.com/jammy/current/jammy-server-cloudimg-amd64.img"
|
||||||
wget https://cloud-images.ubuntu.com/jammy/current/jammy-server-cloudimg-amd64.img
|
PACKAGES="neofetch"
|
||||||
#installing qemu guest agent and remove HW ID
|
DOCKER=1
|
||||||
virt-customize -a jammy-server-cloudimg-amd64.img --install qemu-guest-agent,neofetch
|
|
||||||
virt-customize -a jammy-server-cloudimg-amd64.img --run-command "curl -fsSL https://get.docker.com -o get-docker.sh
|
build_template
|
||||||
sh get-docker.sh"
|
|
||||||
virt-customize -a jammy-server-cloudimg-amd64.img --run-command "echo -n > /etc/machine-id"
|
|
||||||
qm create $VM_ID --name $VM_NAME --net0 virtio,bridge=vmbr0
|
|
||||||
qm set $VM_ID --cores "$CPU_CORE" --sockets "$CPU_SOCKET" --memory "$RAM"
|
|
||||||
qm importdisk "$VM_ID" jammy-server-cloudimg-amd64.img local-lvm
|
|
||||||
qm set "$VM_ID" --scsihw virtio-scsi-pci --scsi0 local-lvm:vm-"$VM_ID"-disk-0
|
|
||||||
qm set "$VM_ID" --ide2 local-lvm:cloudinit
|
|
||||||
qm set "$VM_ID" --boot c --bootdisk scsi0
|
|
||||||
qm set "$VM_ID" --serial0 socket --vga serial0
|
|
||||||
qm resize $VM_ID scsi0 "$DISK"G
|
|
||||||
qm set "$VM_ID" --ipconfig0 ip=dhcp
|
|
||||||
qm set "$VM_ID" --agent enabled=1
|
|
||||||
qm template "$VM_ID"
|
|
||||||
rm jammy-server-cloudimg-amd64.img #clean iso
|
|
||||||
echo "Template er ferdig med id "$VM_ID""
|
|
||||||
Regular → Executable
+8
-20
@@ -1,26 +1,14 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
cd "$(dirname "$0")" && source ./lib.sh
|
||||||
|
|
||||||
VM_ID=4000
|
VM_ID=4000
|
||||||
VM_NAME="Ubuntu-24-04-LTS-Large-Docker"
|
VM_NAME="Ubuntu-24-04-LTS-Large-Docker"
|
||||||
CPU_CORE=4
|
CPU_CORE=4
|
||||||
CPU_SOCKET=2
|
CPU_SOCKET=2
|
||||||
RAM=8192
|
RAM=8192
|
||||||
DISK=100
|
DISK=100
|
||||||
#getting latest image"
|
IMAGE_URL="https://cloud-images.ubuntu.com/noble/current/noble-server-cloudimg-amd64.img"
|
||||||
#wget https://cloud-images.ubuntu.com/noble/current/noble-server-cloudimg-amd64.img
|
PACKAGES=""
|
||||||
#installing qemu guest agent and remove HW ID
|
DOCKER=1
|
||||||
virt-customize -a noble-server-cloudimg-amd64.img --install qemu-guest-agent
|
|
||||||
virt-customize -a noble-server-cloudimg-amd64.img --run-command "curl -fsSL https://get.docker.com -o get-docker.sh
|
build_template
|
||||||
sh get-docker.sh"
|
|
||||||
virt-customize -a noble-server-cloudimg-amd64.img --run-command "echo -n > /etc/machine-id"
|
|
||||||
qm create $VM_ID --name $VM_NAME --net0 virtio,bridge=vmbr0
|
|
||||||
qm set $VM_ID --cores "$CPU_CORE" --sockets "$CPU_SOCKET" --memory "$RAM"
|
|
||||||
qm importdisk "$VM_ID" noble-server-cloudimg-amd64.img rust
|
|
||||||
qm set "$VM_ID" --scsihw virtio-scsi-pci --scsi0 rust:vm-"$VM_ID"-disk-0
|
|
||||||
qm set "$VM_ID" --ide2 rust:cloudinit
|
|
||||||
qm set "$VM_ID" --boot c --bootdisk scsi0
|
|
||||||
qm set "$VM_ID" --serial0 socket --vga serial0
|
|
||||||
qm resize $VM_ID scsi0 "$DISK"G
|
|
||||||
qm set "$VM_ID" --ipconfig0 ip=dhcp
|
|
||||||
qm set "$VM_ID" --agent enabled=1
|
|
||||||
qm template "$VM_ID"
|
|
||||||
rm noble-server-cloudimg-amd64.img clean iso
|
|
||||||
echo "Template er ferdig med id "$VM_ID""
|
|
||||||
Regular → Executable
+8
-20
@@ -1,26 +1,14 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
cd "$(dirname "$0")" && source ./lib.sh
|
||||||
|
|
||||||
VM_ID=3000
|
VM_ID=3000
|
||||||
VM_NAME="Ubuntu-24-04-LTS-Medium-Docker"
|
VM_NAME="Ubuntu-24-04-LTS-Medium-Docker"
|
||||||
CPU_CORE=2
|
CPU_CORE=2
|
||||||
CPU_SOCKET=2
|
CPU_SOCKET=2
|
||||||
RAM=4096
|
RAM=4096
|
||||||
DISK=50
|
DISK=50
|
||||||
#getting latest image"
|
IMAGE_URL="https://cloud-images.ubuntu.com/noble/current/noble-server-cloudimg-amd64.img"
|
||||||
#wget https://cloud-images.ubuntu.com/noble/current/noble-server-cloudimg-amd64.img
|
PACKAGES=""
|
||||||
#installing qemu guest agent and remove HW ID
|
DOCKER=1
|
||||||
virt-customize -a noble-server-cloudimg-amd64.img --install qemu-guest-agent
|
|
||||||
virt-customize -a noble-server-cloudimg-amd64.img --run-command "curl -fsSL https://get.docker.com -o get-docker.sh
|
build_template
|
||||||
sh get-docker.sh"
|
|
||||||
virt-customize -a noble-server-cloudimg-amd64.img --run-command "echo -n > /etc/machine-id"
|
|
||||||
qm create $VM_ID --name $VM_NAME --net0 virtio,bridge=vmbr0
|
|
||||||
qm set $VM_ID --cores "$CPU_CORE" --sockets "$CPU_SOCKET" --memory "$RAM"
|
|
||||||
qm importdisk "$VM_ID" noble-server-cloudimg-amd64.img rust
|
|
||||||
qm set "$VM_ID" --scsihw virtio-scsi-pci --scsi0 rust:vm-"$VM_ID"-disk-0
|
|
||||||
qm set "$VM_ID" --ide2 rust:cloudinit
|
|
||||||
qm set "$VM_ID" --boot c --bootdisk scsi0
|
|
||||||
qm set "$VM_ID" --serial0 socket --vga serial0
|
|
||||||
qm resize $VM_ID scsi0 "$DISK"G
|
|
||||||
qm set "$VM_ID" --ipconfig0 ip=dhcp
|
|
||||||
qm set "$VM_ID" --agent enabled=1
|
|
||||||
qm template "$VM_ID"
|
|
||||||
#rm noble-server-cloudimg-amd64.img clean iso
|
|
||||||
echo "Template er ferdig med id "$VM_ID""
|
|
||||||
Regular → Executable
+8
-20
@@ -1,26 +1,14 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
cd "$(dirname "$0")" && source ./lib.sh
|
||||||
|
|
||||||
VM_ID=2000
|
VM_ID=2000
|
||||||
VM_NAME="Ubuntu-24-04-LTS-Small"
|
VM_NAME="Ubuntu-24-04-LTS-Small"
|
||||||
CPU_CORE=2
|
CPU_CORE=2
|
||||||
CPU_SOCKET=2
|
CPU_SOCKET=2
|
||||||
RAM=2048
|
RAM=2048
|
||||||
DISK=50
|
DISK=50
|
||||||
#getting latest image"
|
IMAGE_URL="https://cloud-images.ubuntu.com/noble/current/noble-server-cloudimg-amd64.img"
|
||||||
wget https://cloud-images.ubuntu.com/noble/current/noble-server-cloudimg-amd64.img
|
PACKAGES=""
|
||||||
#installing qemu guest agent and remove HW ID
|
DOCKER=0
|
||||||
virt-customize -a noble-server-cloudimg-amd64.img --install qemu-guest-agent
|
|
||||||
virt-customize -a noble-server-cloudimg-amd64.img --run-command "curl -fsSL https://get.docker.com -o get-docker.sh
|
build_template
|
||||||
sh get-docker.sh"
|
|
||||||
virt-customize -a noble-server-cloudimg-amd64.img --run-command "echo -n > /etc/machine-id"
|
|
||||||
qm create $VM_ID --name $VM_NAME --net0 virtio,bridge=vmbr0
|
|
||||||
qm set $VM_ID --cores "$CPU_CORE" --sockets "$CPU_SOCKET" --memory "$RAM"
|
|
||||||
qm importdisk "$VM_ID" noble-server-cloudimg-amd64.img rust
|
|
||||||
qm set "$VM_ID" --scsihw virtio-scsi-pci --scsi0 rust:vm-"$VM_ID"-disk-0
|
|
||||||
qm set "$VM_ID" --ide2 rust:cloudinit
|
|
||||||
qm set "$VM_ID" --boot c --bootdisk scsi0
|
|
||||||
qm set "$VM_ID" --serial0 socket --vga serial0
|
|
||||||
qm resize $VM_ID scsi0 "$DISK"G
|
|
||||||
qm set "$VM_ID" --ipconfig0 ip=dhcp
|
|
||||||
qm set "$VM_ID" --agent enabled=1
|
|
||||||
qm template "$VM_ID"
|
|
||||||
#rm noble-server-cloudimg-amd64.img clean iso
|
|
||||||
echo "Template er ferdig med id "$VM_ID""
|
|
||||||
@@ -1 +1,9 @@
|
|||||||
apt install -y libguestfs-tools && chmod +x Ubuntu-L* && chmod +x Debian.sh && ./Debian.sh && ./Ubuntu-LTS-Small.sh && ./Ubuntu-LTS-Medium.sh && ./Ubuntu-LTS-Large.sh #&& ./Ubuntu-22-04-LTS.sh
|
#!/usr/bin/env bash
|
||||||
|
# Builds every template. Stops at the first failure.
|
||||||
|
set -e
|
||||||
|
cd "$(dirname "$0")"
|
||||||
|
./Debian.sh
|
||||||
|
./Ubuntu-LTS-Small.sh
|
||||||
|
./Ubuntu-LTS-Medium.sh
|
||||||
|
./Ubuntu-LTS-Large.sh
|
||||||
|
#./Ubuntu-22-04-LTS.sh
|
||||||
-340
@@ -1,340 +0,0 @@
|
|||||||
#!/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 "$@"
|
|
||||||
@@ -0,0 +1,52 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
# Shared template builder. Each <distro>.sh sets the variables below and calls build_template.
|
||||||
|
#
|
||||||
|
# VM_ID VM_NAME CPU_CORE CPU_SOCKET RAM DISK - template hardware (DISK in GB)
|
||||||
|
# IMAGE_URL - cloud image to start from
|
||||||
|
# PACKAGES - extra apt packages, comma separated
|
||||||
|
# DOCKER=1 - install Docker from get.docker.com
|
||||||
|
#
|
||||||
|
# Override where it lands with environment variables:
|
||||||
|
# STORAGE=ssd BRIDGE=vmbr1 ./Ubuntu-LTS-Small.sh
|
||||||
|
set -euo pipefail
|
||||||
|
|
||||||
|
STORAGE="${STORAGE:-ssd}" # pvebox: ssd (SSD raidz1) or tank (SAS raidz1)
|
||||||
|
BRIDGE="${BRIDGE:-vmbr1}" # vmbr1 = VM-Ekstern (VLAN 4), intern = Proxmox NAT, vmbr0 = Lab
|
||||||
|
IMAGE_DIR="${IMAGE_DIR:-/var/lib/vz/template/cloud}"
|
||||||
|
|
||||||
|
build_template() {
|
||||||
|
if qm status "$VM_ID" >/dev/null 2>&1; then
|
||||||
|
echo "VM $VM_ID finnes allerede - slett den først: qm destroy $VM_ID --purge" >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
command -v virt-customize >/dev/null || apt install -y libguestfs-tools
|
||||||
|
|
||||||
|
# Download once into a cache (only again when the image changed upstream);
|
||||||
|
# customize a copy so every script starts from a clean image.
|
||||||
|
mkdir -p "$IMAGE_DIR"
|
||||||
|
wget -q --show-progress -N -P "$IMAGE_DIR" "$IMAGE_URL"
|
||||||
|
local work; work=$(mktemp -d)
|
||||||
|
local disk="$work/disk.qcow2"
|
||||||
|
cp "$IMAGE_DIR/$(basename "$IMAGE_URL")" "$disk"
|
||||||
|
|
||||||
|
# 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}"
|
||||||
|
if [ "${DOCKER:-0}" = 1 ]; then
|
||||||
|
virt-customize -a "$disk" --run-command "curl -fsSL https://get.docker.com | sh"
|
||||||
|
fi
|
||||||
|
virt-customize -a "$disk" --truncate /etc/machine-id
|
||||||
|
|
||||||
|
qm create "$VM_ID" --name "$VM_NAME" --net0 "virtio,bridge=$BRIDGE"
|
||||||
|
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" --ide2 "$STORAGE:cloudinit"
|
||||||
|
qm set "$VM_ID" --boot order=scsi0
|
||||||
|
qm set "$VM_ID" --serial0 socket --vga serial0
|
||||||
|
qm resize "$VM_ID" scsi0 "${DISK}G"
|
||||||
|
qm set "$VM_ID" --ipconfig0 ip=dhcp
|
||||||
|
qm set "$VM_ID" --agent enabled=1
|
||||||
|
qm template "$VM_ID"
|
||||||
|
|
||||||
|
rm -rf "$work"
|
||||||
|
echo "Template er ferdig med id $VM_ID ($VM_NAME) på $STORAGE, nett $BRIDGE"
|
||||||
|
}
|
||||||
Reference in new issue
Block a user