mirror of
https://github.com/RogerSik/proxmox-vm-template.git
synced 2026-11-17 13:34:24 +01:00
Compare commits
11
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
239334c6e5 | ||
|
|
cf0765982f | ||
|
|
8b17d0e365 | ||
|
|
54abefdfcb | ||
|
|
983a3e6e6f | ||
|
|
8b8b783e7d | ||
|
|
2994d7224a | ||
|
|
66863b50b2 | ||
|
|
775d4a2989 | ||
|
|
dc5d541205 | ||
|
|
f537f9fafd |
No files matched your search
@@ -0,0 +1,14 @@
|
|||||||
|
name: shellcheck
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches: [main]
|
||||||
|
pull_request:
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
shellcheck:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v4
|
||||||
|
- name: Run shellcheck
|
||||||
|
run: shellcheck *.sh
|
||||||
Vendored
+1
-1
@@ -3,7 +3,7 @@
|
|||||||
"editor.formatOnSave": true,
|
"editor.formatOnSave": true,
|
||||||
"editor.formatOnPaste": false,
|
"editor.formatOnPaste": false,
|
||||||
"files.associations": {
|
"files.associations": {
|
||||||
".ssh-keys.pub": "plaintext"
|
"ssh-keys.pub": "plaintext"
|
||||||
},
|
},
|
||||||
"[shellscript]": {
|
"[shellscript]": {
|
||||||
"editor.defaultFormatter": "foxundermoon.shell-format"
|
"editor.defaultFormatter": "foxundermoon.shell-format"
|
||||||
|
|||||||
@@ -0,0 +1,43 @@
|
|||||||
|
# Changelog
|
||||||
|
|
||||||
|
All notable changes to this project are documented in this file.
|
||||||
|
|
||||||
|
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
|
||||||
|
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
||||||
|
|
||||||
|
## [1.0.0] - 2026-11-06
|
||||||
|
|
||||||
|
First tagged release.
|
||||||
|
|
||||||
|
### Added
|
||||||
|
|
||||||
|
- Interactive selection of the last four Ubuntu LTS releases, or select one by codename as argument
|
||||||
|
- Only download the cloud image if a newer version exists
|
||||||
|
- Verify the SHA256 checksum of the downloaded cloud image
|
||||||
|
- Install `qemu-guest-agent` into the image and reset `/etc/machine-id`
|
||||||
|
- Suggest the next free VM ID and reject IDs that are invalid or already in use
|
||||||
|
- Validate the VM name as a DNS name
|
||||||
|
- Set OS type `l26` and CPU type `x86-64-v2-AES`
|
||||||
|
- Resize the disk to 32G
|
||||||
|
- Serial console (`serial0`) as default display, as expected by Ubuntu cloud images
|
||||||
|
- Discard and SSD emulation on the disk
|
||||||
|
- Hotplug for disk, network, USB, memory and CPU
|
||||||
|
- Destroy the incomplete VM if a step fails after it was created
|
||||||
|
- shellcheck GitHub Actions workflow
|
||||||
|
- README with requirements, usage and description of the created VM
|
||||||
|
|
||||||
|
### Changed
|
||||||
|
|
||||||
|
- Abort on the first error (`set -euo pipefail`), errors are printed to stderr
|
||||||
|
- Check for root and a non-empty `ssh-keys.pub` before asking any questions
|
||||||
|
- Install `libguestfs-tools` only if `virt-customize` is missing
|
||||||
|
- Ask for VM ID and name before downloading the image
|
||||||
|
- Use a unique temporary file for the modified image
|
||||||
|
|
||||||
|
### Fixed
|
||||||
|
|
||||||
|
- The "(latest)" marker was shown next to the oldest instead of the newest LTS release
|
||||||
|
- Missing root or `ssh-keys.pub` exited with status 0
|
||||||
|
- VS Code file association for `ssh-keys.pub`
|
||||||
|
|
||||||
|
[1.0.0]: https://github.com/RogerSik/proxmox-vm-template/releases/tag/v1.0.0
|
||||||
@@ -1,3 +1,35 @@
|
|||||||
# proxmox-vm-template
|
# proxmox-vm-template
|
||||||
|
|
||||||
A simple script to download and create a cloud init ready Ubuntu LTS vm.
|
A simple script to download and create a cloud-init ready Ubuntu LTS VM on Proxmox VE.
|
||||||
|
|
||||||
|
## Requirements
|
||||||
|
|
||||||
|
- Proxmox VE host, script runs as `root`
|
||||||
|
- Storage `local-lvm` and bridge `vmbr0`
|
||||||
|
- `libguestfs-tools` (installed automatically if missing)
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
|
||||||
|
```bash
|
||||||
|
cp ssh-keys.pub.dist ssh-keys.pub
|
||||||
|
# add your public SSH keys to ssh-keys.pub
|
||||||
|
|
||||||
|
./ubuntu-lts.sh # interactive release selection
|
||||||
|
./ubuntu-lts.sh noble # select release by codename
|
||||||
|
```
|
||||||
|
|
||||||
|
The script asks for a VM ID (defaults to the next free ID) and a VM name.
|
||||||
|
|
||||||
|
## What it does
|
||||||
|
|
||||||
|
1. Downloads the current Ubuntu cloud image to `/tmp` (only if newer) and verifies its SHA256 checksum
|
||||||
|
2. Installs `qemu-guest-agent` into a copy of the image and resets `/etc/machine-id`
|
||||||
|
3. Creates the VM:
|
||||||
|
- 2 cores, 1024 MB memory, CPU type `x86-64-v2-AES`
|
||||||
|
- 32G disk with discard and SSD emulation
|
||||||
|
- Serial console, QEMU guest agent enabled
|
||||||
|
- Hotplug for disk, network, USB, memory and CPU
|
||||||
|
- Cloud-init user `devops` with keys from `ssh-keys.pub`, DHCP for IPv4 and IPv6
|
||||||
|
- Starts on boot
|
||||||
|
|
||||||
|
If a step fails after the VM was created, the incomplete VM is destroyed again.
|
||||||
+106
-44
@@ -1,18 +1,36 @@
|
|||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
|
set -euo pipefail
|
||||||
|
|
||||||
DIR=$(
|
DIR=$(
|
||||||
cd "$(dirname "$0")" || exit 1
|
cd "$(dirname "$0")" || exit 1
|
||||||
pwd
|
pwd
|
||||||
)
|
)
|
||||||
|
|
||||||
|
if [ "$EUID" -ne 0 ]; then
|
||||||
|
echo "Error: Please run as root" >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ ! -s "$DIR/ssh-keys.pub" ]; then
|
||||||
|
echo "Error: $DIR/ssh-keys.pub doesn't exist or is empty" >&2
|
||||||
|
echo "Copy ssh-keys.pub.dist to ssh-keys.pub and add your public keys" >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
if ! command -v virt-customize >/dev/null; then
|
||||||
|
echo "Install libguestfs-tools"
|
||||||
|
apt-get install -y libguestfs-tools
|
||||||
|
fi
|
||||||
|
|
||||||
mapfile -t LTS_ENTRIES < <(
|
mapfile -t LTS_ENTRIES < <(
|
||||||
curl -s https://changelogs.ubuntu.com/meta-release-lts | awk '
|
curl -fsS https://changelogs.ubuntu.com/meta-release-lts | awk '
|
||||||
/^Dist:/ { dist=$2 }
|
/^Dist:/ { dist=$2 }
|
||||||
/^Version:/ { ver=$2; print dist " " ver }
|
/^Version:/ { ver=$2; print dist " " ver }
|
||||||
' | tail -4 | tac
|
' | tail -4 | tac
|
||||||
)
|
)
|
||||||
|
|
||||||
if [ ${#LTS_ENTRIES[@]} -eq 0 ]; then
|
if [ ${#LTS_ENTRIES[@]} -eq 0 ]; then
|
||||||
echo "Error: Could not fetch LTS release list from Ubuntu"
|
echo "Error: Could not fetch LTS release list from Ubuntu" >&2
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
@@ -23,78 +41,123 @@ for entry in "${LTS_ENTRIES[@]}"; do
|
|||||||
LTS_VERSIONS+=("${entry##* }")
|
LTS_VERSIONS+=("${entry##* }")
|
||||||
done
|
done
|
||||||
|
|
||||||
LAST_LTS_RELEASE="${LTS_RELEASES[-1]}"
|
LATEST_LTS_RELEASE="${LTS_RELEASES[0]}"
|
||||||
|
|
||||||
if [ -n "$1" ]; then
|
if [ -n "${1:-}" ]; then
|
||||||
SELECTED_RELEASE="$1"
|
SELECTED_RELEASE="$1"
|
||||||
VALID=0
|
VALID=0
|
||||||
for r in "${LTS_RELEASES[@]}"; do
|
for r in "${LTS_RELEASES[@]}"; do
|
||||||
[ "$r" = "$SELECTED_RELEASE" ] && VALID=1 && break
|
if [ "$r" = "$SELECTED_RELEASE" ]; then
|
||||||
|
VALID=1
|
||||||
|
break
|
||||||
|
fi
|
||||||
done
|
done
|
||||||
if [ "$VALID" -eq 0 ]; then
|
if [ "$VALID" -eq 0 ]; then
|
||||||
echo "Error: '$SELECTED_RELEASE' is not a valid LTS release."
|
echo "Error: '$SELECTED_RELEASE' is not a valid LTS release." >&2
|
||||||
printf "Available: "
|
printf "Available: " >&2
|
||||||
for i in "${!LTS_RELEASES[@]}"; do
|
for i in "${!LTS_RELEASES[@]}"; do
|
||||||
printf "%s (%s) " "${LTS_RELEASES[$i]}" "${LTS_VERSIONS[$i]}"
|
printf "%s (%s) " "${LTS_RELEASES[$i]}" "${LTS_VERSIONS[$i]}" >&2
|
||||||
done
|
done
|
||||||
echo ""
|
echo "" >&2
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
else
|
else
|
||||||
echo ""
|
echo ""
|
||||||
echo "Available Ubuntu LTS versions:"
|
echo "Available Ubuntu LTS versions:"
|
||||||
for i in "${!LTS_RELEASES[@]}"; do
|
for i in "${!LTS_RELEASES[@]}"; do
|
||||||
if [ "${LTS_RELEASES[$i]}" = "$LAST_LTS_RELEASE" ]; then
|
if [ "${LTS_RELEASES[$i]}" = "$LATEST_LTS_RELEASE" ]; then
|
||||||
echo " $((i+1))) ${LTS_VERSIONS[$i]} (${LTS_RELEASES[$i]}) (latest)"
|
echo " $((i + 1))) ${LTS_VERSIONS[$i]} (${LTS_RELEASES[$i]}) (latest)"
|
||||||
else
|
else
|
||||||
echo " $((i+1))) ${LTS_VERSIONS[$i]} (${LTS_RELEASES[$i]})"
|
echo " $((i + 1))) ${LTS_VERSIONS[$i]} (${LTS_RELEASES[$i]})"
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
echo ""
|
echo ""
|
||||||
read -rp "Select version [1-${#LTS_RELEASES[@]}, default: 1]: " SELECTION
|
read -rp "Select version [1-${#LTS_RELEASES[@]}, default: 1]: " SELECTION
|
||||||
SELECTION="${SELECTION:-1}"
|
SELECTION="${SELECTION:-1}"
|
||||||
if ! [[ "$SELECTION" =~ ^[0-9]+$ ]] || [ "$SELECTION" -lt 1 ] || [ "$SELECTION" -gt "${#LTS_RELEASES[@]}" ]; then
|
if ! [[ "$SELECTION" =~ ^[0-9]+$ ]] || [ "$SELECTION" -lt 1 ] || [ "$SELECTION" -gt "${#LTS_RELEASES[@]}" ]; then
|
||||||
echo "Error: Invalid selection"
|
echo "Error: Invalid selection" >&2
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
SELECTED_RELEASE="${LTS_RELEASES[$((SELECTION-1))]}"
|
SELECTED_RELEASE="${LTS_RELEASES[$((SELECTION - 1))]}"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
NEXT_VM_ID=$(pvesh get /cluster/nextid)
|
||||||
|
read -rp "Enter VM ID [default: $NEXT_VM_ID]: " VM_ID
|
||||||
|
VM_ID="${VM_ID:-$NEXT_VM_ID}"
|
||||||
|
if ! [[ "$VM_ID" =~ ^[1-9][0-9]{2,}$ ]]; then
|
||||||
|
echo "Error: VM ID must be a number >= 100" >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
if qm status "$VM_ID" >/dev/null 2>&1; then
|
||||||
|
echo "Error: VM ID $VM_ID is already in use" >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
read -rp "Enter VM Name: " VM_NAME
|
||||||
|
if ! [[ "$VM_NAME" =~ ^[a-zA-Z0-9]([a-zA-Z0-9.-]*[a-zA-Z0-9])?$ ]]; then
|
||||||
|
echo "Error: VM name must be a valid DNS name" >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
IMAGE_NAME="$SELECTED_RELEASE-server-cloudimg-amd64.img"
|
||||||
|
IMAGE_URL="https://cloud-images.ubuntu.com/$SELECTED_RELEASE/current"
|
||||||
|
MODIFIED_IMAGE=$(mktemp "/tmp/$SELECTED_RELEASE-server-cloudimg-amd64.modified.XXXXXX.img")
|
||||||
|
CHECKSUMS=$(mktemp)
|
||||||
|
VM_CREATED=0
|
||||||
|
|
||||||
|
cleanup() {
|
||||||
|
local exit_code=$?
|
||||||
|
rm -f "$MODIFIED_IMAGE" "$CHECKSUMS"
|
||||||
|
if [ "$exit_code" -ne 0 ] && [ "$VM_CREATED" -eq 1 ]; then
|
||||||
|
echo "Error: Setup failed, destroying incomplete VM $VM_ID" >&2
|
||||||
|
qm destroy "$VM_ID" --purge || true
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
trap cleanup EXIT
|
||||||
|
|
||||||
echo ""
|
echo ""
|
||||||
echo "This script will download and setup a VM with Ubuntu LTS: $SELECTED_RELEASE"
|
echo "This script will download and setup a VM with Ubuntu LTS: $SELECTED_RELEASE"
|
||||||
echo ""
|
echo ""
|
||||||
|
|
||||||
if [ "$EUID" -ne 0 ]; then
|
echo "Check and re-download image $IMAGE_NAME if there is a newer img file"
|
||||||
echo "Please run as root"
|
wget -N "$IMAGE_URL/$IMAGE_NAME" -P /tmp/
|
||||||
exit
|
|
||||||
|
echo "Verify checksum of $IMAGE_NAME"
|
||||||
|
curl -fsS "$IMAGE_URL/SHA256SUMS" -o "$CHECKSUMS"
|
||||||
|
EXPECTED_HASH=$(awk -v img="*$IMAGE_NAME" '$2 == img { print $1 }' "$CHECKSUMS")
|
||||||
|
if [ -z "$EXPECTED_HASH" ]; then
|
||||||
|
echo "Error: No checksum found for $IMAGE_NAME" >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
if ! echo "$EXPECTED_HASH /tmp/$IMAGE_NAME" | sha256sum -c --quiet -; then
|
||||||
|
echo "Error: Checksum mismatch, removing /tmp/$IMAGE_NAME" >&2
|
||||||
|
rm -f "/tmp/$IMAGE_NAME"
|
||||||
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [ ! -f "$DIR/ssh-keys.pub" ]; then
|
echo "Create copy of $IMAGE_NAME for image modification"
|
||||||
echo "Warning ssh-keys.pub doesn't exists"
|
cp "/tmp/$IMAGE_NAME" "$MODIFIED_IMAGE"
|
||||||
echo "Will abort now"
|
|
||||||
exit
|
|
||||||
fi
|
|
||||||
|
|
||||||
echo "Check and re-download Image $SELECTED_RELEASE-server-cloudimg-amd64.img if there is a newer img file"
|
echo "Install qemu-guest-agent and reset machine-id on Ubuntu image"
|
||||||
wget -N "https://cloud-images.ubuntu.com/$SELECTED_RELEASE/current/$SELECTED_RELEASE-server-cloudimg-amd64.img" -P /tmp/
|
virt-customize -a "$MODIFIED_IMAGE" \
|
||||||
|
--install qemu-guest-agent \
|
||||||
|
--truncate /etc/machine-id
|
||||||
|
|
||||||
echo "Enter VM ID"
|
echo "Create Proxmox VM from Ubuntu Cloud Image"
|
||||||
read -r VM_ID
|
qm create "$VM_ID" \
|
||||||
echo "Enter VM Name"
|
--name "$VM_NAME" \
|
||||||
read -r VM_NAME
|
--ostype l26 \
|
||||||
|
--cpu x86-64-v2-AES \
|
||||||
echo "Ensure libguestfs-tools is installed"
|
--memory 1024 --balloon 0 \
|
||||||
apt install libguestfs-tools -y
|
--cores 2 \
|
||||||
|
--numa 1 \
|
||||||
echo "Create copy of $SELECTED_RELEASE-server-cloudimg-amd64 for image modification"
|
--net0 virtio,bridge=vmbr0 \
|
||||||
cp -r "/tmp/$SELECTED_RELEASE-server-cloudimg-amd64.img" "/tmp/$SELECTED_RELEASE-server-cloudimg-amd64.modified.img"
|
--scsihw virtio-scsi-pci \
|
||||||
|
--serial0 socket --vga serial0 \
|
||||||
echo "Install qemu-guest-agent on Ubuntu image"
|
--hotplug disk,network,usb,memory,cpu
|
||||||
virt-customize -a "/tmp/$SELECTED_RELEASE-server-cloudimg-amd64.modified.img" --install qemu-guest-agent
|
VM_CREATED=1
|
||||||
|
qm set "$VM_ID" --scsi0 local-lvm:0,import-from="$MODIFIED_IMAGE",discard=on,ssd=1
|
||||||
echo "Create Proxmox VM image from Ubuntu Cloud Image"
|
qm resize "$VM_ID" scsi0 32G
|
||||||
qm create "$VM_ID" --memory 1024 --balloon 0 --cores 2 --net0 virtio,bridge=vmbr0 --scsihw virtio-scsi-pci
|
|
||||||
qm set "$VM_ID" --scsi0 local-lvm:0,import-from="/tmp/$SELECTED_RELEASE-server-cloudimg-amd64.modified.img"
|
|
||||||
qm set "$VM_ID" --agent enabled=1,fstrim_cloned_disks=1
|
qm set "$VM_ID" --agent enabled=1,fstrim_cloned_disks=1
|
||||||
|
|
||||||
echo "Create Cloud-Init Disk and configure boot"
|
echo "Create Cloud-Init Disk and configure boot"
|
||||||
@@ -103,11 +166,10 @@ qm set "$VM_ID" --boot order=scsi0
|
|||||||
|
|
||||||
echo "Configure vm"
|
echo "Configure vm"
|
||||||
qm set "$VM_ID" \
|
qm set "$VM_ID" \
|
||||||
--name "$VM_NAME" \
|
|
||||||
--onboot 1 \
|
--onboot 1 \
|
||||||
--ciuser "devops" \
|
--ciuser "devops" \
|
||||||
--sshkeys "$DIR/ssh-keys.pub" \
|
--sshkeys "$DIR/ssh-keys.pub" \
|
||||||
--ipconfig0 "ip=dhcp,ip6=dhcp"
|
--ipconfig0 "ip=dhcp,ip6=dhcp"
|
||||||
|
|
||||||
echo "Cleanup: delete modified cloudimg"
|
echo ""
|
||||||
rm -f "/tmp/$SELECTED_RELEASE-server-cloudimg-amd64.modified.img"
|
echo "VM $VM_ID ($VM_NAME) with Ubuntu $SELECTED_RELEASE created successfully"
|
||||||
Reference in new issue
Block a user