From 24e624234b7ac782131ab2a72b0246e85e818547 Mon Sep 17 00:00:00 2001 From: steffen Date: Wed, 4 Nov 2026 20:47:19 +0100 Subject: [PATCH] Shared lib.sh builder: pvebox storage/bridge defaults, self-downloading scripts, Debian 13 --- Debian.sh | 29 +++++++----------------- README.md | 29 +++++++++++++++++++++--- Ubuntu-22-04-LTS.sh | 27 ++++++----------------- Ubuntu-LTS-Large.sh | 27 ++++++----------------- Ubuntu-LTS-Medium.sh | 27 ++++++----------------- Ubuntu-LTS-Small.sh | 27 ++++++----------------- all.sh | 9 +++++++- lib.sh | 52 ++++++++++++++++++++++++++++++++++++++++++++ 8 files changed, 122 insertions(+), 105 deletions(-) create mode 100755 lib.sh diff --git a/Debian.sh b/Debian.sh index 8c1d839..701f122 100755 --- a/Debian.sh +++ b/Debian.sh @@ -1,27 +1,14 @@ #!/usr/bin/env bash +cd "$(dirname "$0")" && source ./lib.sh + VM_ID=9998 -VM_NAME="Debian-12-Large-Docker" +VM_NAME="Debian-13-Large-Docker" CPU_CORE=4 CPU_SOCKET=2 RAM=8192 DISK=100 -#getting latest image" -wget https://cdimage.debian.org/images/cloud/bookworm/20250210-2019/debian-12-generic-amd64-20250210-2019.qcow2 -#installing qemu guest agent and remove HW ID -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 -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"" \ No newline at end of file +IMAGE_URL="https://cloud.debian.org/images/cloud/trixie/latest/debian-13-generic-amd64.qcow2" +PACKAGES="" +DOCKER=1 + +build_template diff --git a/README.md b/README.md index a995767..05f09a2 100644 --- a/README.md +++ b/README.md @@ -9,9 +9,32 @@ cd prox-mal/ ``` -## Image Medium and Large -Docker and Docker Compose comes pre installed from the Docker github repo. \ -If not wanted, you can comment out line 12 in Large and Medium.sh +Or build a single template: `./Ubuntu-LTS-Small.sh` + +## Templates + +| 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 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. + +## Where the templates land + +Defaults fit pvebox: storage `ssd`, network `vmbr1` (VM-Ekstern). Override per run: + +```sh +STORAGE=tank BRIDGE=intern ./Ubuntu-LTS-Large.sh +``` + +A script stops if its VM ID already exists. Remove the old template first: +`qm destroy 2000 --purge`
Source Doc diff --git a/Ubuntu-22-04-LTS.sh b/Ubuntu-22-04-LTS.sh index 09264aa..ff86eb6 100755 --- a/Ubuntu-22-04-LTS.sh +++ b/Ubuntu-22-04-LTS.sh @@ -1,27 +1,14 @@ #!/usr/bin/env bash +cd "$(dirname "$0")" && source ./lib.sh + VM_ID=9999 VM_NAME="Ubuntu-22-04-LTS-Large-Docker" CPU_CORE=4 CPU_SOCKET=2 RAM=8192 DISK=100 -#getting latest image" -wget https://cloud-images.ubuntu.com/jammy/current/jammy-server-cloudimg-amd64.img -#installing qemu guest agent and remove HW ID -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 -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"" \ No newline at end of file +IMAGE_URL="https://cloud-images.ubuntu.com/jammy/current/jammy-server-cloudimg-amd64.img" +PACKAGES="neofetch" +DOCKER=1 + +build_template diff --git a/Ubuntu-LTS-Large.sh b/Ubuntu-LTS-Large.sh index e70b1aa..b753e77 100755 --- a/Ubuntu-LTS-Large.sh +++ b/Ubuntu-LTS-Large.sh @@ -1,27 +1,14 @@ #!/usr/bin/env bash +cd "$(dirname "$0")" && source ./lib.sh + VM_ID=4000 VM_NAME="Ubuntu-24-04-LTS-Large-Docker" CPU_CORE=4 CPU_SOCKET=2 RAM=8192 DISK=100 -#getting latest image" -#wget https://cloud-images.ubuntu.com/noble/current/noble-server-cloudimg-amd64.img -#installing qemu guest agent and remove HW ID -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 -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"" \ No newline at end of file +IMAGE_URL="https://cloud-images.ubuntu.com/noble/current/noble-server-cloudimg-amd64.img" +PACKAGES="" +DOCKER=1 + +build_template diff --git a/Ubuntu-LTS-Medium.sh b/Ubuntu-LTS-Medium.sh index 4bc8704..80eb95b 100755 --- a/Ubuntu-LTS-Medium.sh +++ b/Ubuntu-LTS-Medium.sh @@ -1,27 +1,14 @@ #!/usr/bin/env bash +cd "$(dirname "$0")" && source ./lib.sh + VM_ID=3000 VM_NAME="Ubuntu-24-04-LTS-Medium-Docker" CPU_CORE=2 CPU_SOCKET=2 RAM=4096 DISK=50 -#getting latest image" -#wget https://cloud-images.ubuntu.com/noble/current/noble-server-cloudimg-amd64.img -#installing qemu guest agent and remove HW ID -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 -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"" \ No newline at end of file +IMAGE_URL="https://cloud-images.ubuntu.com/noble/current/noble-server-cloudimg-amd64.img" +PACKAGES="" +DOCKER=1 + +build_template diff --git a/Ubuntu-LTS-Small.sh b/Ubuntu-LTS-Small.sh index c4d4ece..1fb5450 100755 --- a/Ubuntu-LTS-Small.sh +++ b/Ubuntu-LTS-Small.sh @@ -1,27 +1,14 @@ #!/usr/bin/env bash +cd "$(dirname "$0")" && source ./lib.sh + VM_ID=2000 VM_NAME="Ubuntu-24-04-LTS-Small" CPU_CORE=2 CPU_SOCKET=2 RAM=2048 DISK=50 -#getting latest image" -wget https://cloud-images.ubuntu.com/noble/current/noble-server-cloudimg-amd64.img -#installing qemu guest agent and remove HW ID -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 -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"" \ No newline at end of file +IMAGE_URL="https://cloud-images.ubuntu.com/noble/current/noble-server-cloudimg-amd64.img" +PACKAGES="" +DOCKER=0 + +build_template diff --git a/all.sh b/all.sh index f93f46c..928f161 100755 --- a/all.sh +++ b/all.sh @@ -1,2 +1,9 @@ #!/usr/bin/env bash -apt install -y libguestfs-tools && ./Debian.sh && ./Ubuntu-LTS-Small.sh && ./Ubuntu-LTS-Medium.sh && ./Ubuntu-LTS-Large.sh #&& ./Ubuntu-22-04-LTS.sh +# 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 diff --git a/lib.sh b/lib.sh new file mode 100755 index 0000000..0d6f179 --- /dev/null +++ b/lib.sh @@ -0,0 +1,52 @@ +#!/usr/bin/env bash +# Shared template builder. Each .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" +}