Zuletzt aktiv 3 weeks ago

Working version of my proxmox template creation script. It'll be used to provision Packer with necessary templates in order to build it's image.

Änderung 3edd47dda5d9bd04cf151d95b2ca8a3cbd2c114d

create-pve-template.sh Originalformat
1#!/bin/bash
2set -e
3
4TEMPLATE_URL="https://cloud-images.ubuntu.com/plucky/current/plucky-server-cloudimg-amd64.img"
5VMID=9000
6TEMPLATE_NAME="ubuntu-25.04-cloudimg-temp"
7STORAGE="local-lvm"
8SSH_KEYS=$(curl -s https://gist.vorpax.dev/vorpax/ssh/raw/HEAD/ssh-keys.pub)
9
10# positional args first (optional)
11[ -n "$1" ] && VMID="$1"
12[ -n "$2" ] && TEMPLATE_NAME="$2"
13[ -n "$3" ] && STORAGE="$3"
14
15# overrides (still optional)
16while [[ $# -gt 3 ]]; do
17 shift
18 case "$1" in
19 --vmid=*) VMID="${1#*=}" ;;
20 --template=*) TEMPLATE_NAME="${1#*=}" ;;
21 --storage=*) STORAGE="${1#*=}" ;;
22 esac
23done
24
25cd /tmp
26wget -O "${TEMPLATE_NAME}_disk" "$TEMPLATE_URL"
27
28qm create "$VMID" \
29 --name "$TEMPLATE_NAME" \
30 --memory 3072 \
31 --cores 3 \
32 --net0 virtio,bridge=vmbr0 \
33 --serial0 socket \
34 --vga serial0
35
36qm importdisk "$VMID" "${TEMPLATE_NAME}_disk" "$STORAGE"
37
38# Créer le dossier des clés SSH
39SSH_KEYS_DIR="ssh_keys_${VMID}"
40mkdir -p "$SSH_KEYS_DIR"
41
42# Écrire chaque clé dans un fichier unique
43KEY_INDEX=0
44while IFS= read -r key; do
45 [ -z "$key" ] && continue
46 SSH_KEY_FILE="${SSH_KEYS_DIR}/key_$(date +%s)_${KEY_INDEX}.pub"
47 echo "$key" > "$SSH_KEY_FILE"
48 qm set "$VMID" --sshkey "$SSH_KEY_FILE"
49 ((KEY_INDEX++))
50done <<< "$SSH_KEYS"
51
52qm set "$VMID" \
53 --scsihw virtio-scsi-pci \
54 --scsi0 "${STORAGE}:vm-${VMID}-disk-0" \
55 --ide2 "${STORAGE}:cloudinit" \
56 --boot c --bootdisk scsi0 \
57 --serial0 socket \
58 --vga serial0 \
59 --net0 virtio,bridge=vmbr0 \
60 --ciuser packer \
61 --searchdomain example.com
62
63qm template "$VMID"
64
65echo "Template $VMID ready for Packer"