Utoljára aktív 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.

Revízió 4626bde79b6d01744d6bc749a064eb87b2b5d2f4

create-pve-template.sh Eredeti
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"
8SKIP_DISK_SETUP=false
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 --skip-disk) SKIP_DISK_SETUP=true ;;
23 esac
24done
25
26# Récupérer les clés SSH avec décodage propre
27SSH_KEYS=$(curl -s https://gist.vorpax.dev/vorpax/ssh/raw/HEAD/ssh-keys.pub | sed 's/%20/ /g; s/%0A/\n/g; s/%3D/=/g; s/%2F/\//g; s/%40/@/g; s/%3A/:/g')
28
29cd /tmp
30
31if [ "$SKIP_DISK_SETUP" = false ]; then
32 echo "Downloading and importing disk..."
33 wget -O "${TEMPLATE_NAME}_disk" "$TEMPLATE_URL"
34
35 qm create "$VMID" \
36 --name "$TEMPLATE_NAME" \
37 --memory 3072 \
38 --cores 3 \
39 --net0 virtio,bridge=vmbr0 \
40 --serial0 socket \
41 --vga serial0
42
43 qm importdisk "$VMID" "${TEMPLATE_NAME}_disk" "$STORAGE"
44else
45 echo "Skipping disk setup (--skip-disk flag)"
46fi
47
48# Créer le dossier des clés SSH
49SSH_KEYS_DIR="ssh_keys_${VMID}"
50mkdir -p "$SSH_KEYS_DIR"
51
52# Écrire chaque clé dans un fichier unique
53KEY_INDEX=0
54while IFS= read -r key; do
55 [ -z "$key" ] && continue
56 SSH_KEY_FILE="${SSH_KEYS_DIR}/key_$(date +%s)_${KEY_INDEX}.pub"
57 echo "$key" > "$SSH_KEY_FILE"
58 qm set "$VMID" --sshkey "$SSH_KEY_FILE"
59 ((KEY_INDEX++))
60done <<< "$SSH_KEYS"
61
62qm set "$VMID" \
63 --scsihw virtio-scsi-pci \
64 --scsi0 "${STORAGE}:vm-${VMID}-disk-0" \
65 --ide2 "${STORAGE}:cloudinit" \
66 --boot c --bootdisk scsi0 \
67 --serial0 socket \
68 --vga serial0 \
69 --net0 virtio,bridge=vmbr0 \
70 --ciuser packer \
71 --searchdomain example.com
72
73qm template "$VMID"
74
75echo "Template $VMID ready for Packer"