-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate-cloud-init-template.sh
More file actions
308 lines (268 loc) · 9.71 KB
/
create-cloud-init-template.sh
File metadata and controls
308 lines (268 loc) · 9.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
#!/bin/bash
#
# PRXDash Cloud-Init Template Creator
# ===================================
# Creates a standardized cloud-init VM template on Proxmox VE
#
# Usage: ./create-cloud-init-template.sh [OPTIONS]
#
# Options:
# -i, --vmid Template VMID (default: 9000)
# -n, --name Template name (default: cloud-init-template)
# -s, --storage Storage pool (default: local-lvm)
# -b, --bridge Network bridge (default: vmbr0)
# -d, --distro Distribution: ubuntu24, ubuntu22, debian12 (default: ubuntu24)
# -h, --help Show this help
#
set -e
# ============================================
# Default Configuration
# ============================================
VMID="${VMID:-9000}"
TEMPLATE_NAME="${TEMPLATE_NAME:-cloud-init-template}"
STORAGE="${STORAGE:-local-lvm}"
BRIDGE="${BRIDGE:-vmbr0}"
DISTRO="${DISTRO:-ubuntu24}"
MEMORY=1024
CORES=1
DISK_SIZE="4G"
# Cloud Image URLs
declare -A CLOUD_IMAGES=(
["ubuntu24"]="https://cloud-images.ubuntu.com/noble/current/noble-server-cloudimg-amd64.img"
["ubuntu22"]="https://cloud-images.ubuntu.com/jammy/current/jammy-server-cloudimg-amd64.img"
["debian12"]="https://cloud.debian.org/images/cloud/bookworm/latest/debian-12-generic-amd64.qcow2"
)
declare -A DISTRO_NAMES=(
["ubuntu24"]="Ubuntu 24.04 LTS"
["ubuntu22"]="Ubuntu 22.04 LTS"
["debian12"]="Debian 12 Bookworm"
)
declare -A TEMPLATE_NAMES=(
["ubuntu24"]="ubuntu-24.04-cloud-init"
["ubuntu22"]="ubuntu-22.04-cloud-init"
["debian12"]="debian-12-cloud-init"
)
# ============================================
# Functions
# ============================================
print_header() {
echo ""
echo "╔══════════════════════════════════════════════════════════════╗"
echo "║ PRXDash Cloud-Init Template Creator ║"
echo "╚══════════════════════════════════════════════════════════════╝"
echo ""
}
print_step() {
echo "→ $1"
}
print_success() {
echo "✓ $1"
}
print_error() {
echo "✗ ERROR: $1" >&2
exit 1
}
show_help() {
head -20 "$0" | tail -15
exit 0
}
check_root() {
if [[ $EUID -ne 0 ]]; then
print_error "This script must be run as root on a Proxmox node"
fi
}
check_vmid_available() {
if qm status "$VMID" &>/dev/null; then
print_error "VMID $VMID already exists. Use -i to specify a different ID."
fi
}
# ============================================
# Parse Arguments
# ============================================
while [[ $# -gt 0 ]]; do
case $1 in
-i|--vmid)
VMID="$2"
shift 2
;;
-n|--name)
TEMPLATE_NAME="$2"
shift 2
;;
-s|--storage)
STORAGE="$2"
shift 2
;;
-b|--bridge)
BRIDGE="$2"
shift 2
;;
-d|--distro)
DISTRO="$2"
shift 2
;;
-h|--help)
show_help
;;
*)
print_error "Unknown option: $1"
;;
esac
done
# Validate distro
if [[ -z "${CLOUD_IMAGES[$DISTRO]}" ]]; then
print_error "Unknown distribution: $DISTRO. Use: ubuntu24, ubuntu22, debian12"
fi
CLOUD_IMAGE_URL="${CLOUD_IMAGES[$DISTRO]}"
DISTRO_NAME="${DISTRO_NAMES[$DISTRO]}"
# Use OS-specific template name if not overriden by user
if [[ "$TEMPLATE_NAME" == "cloud-init-template" ]]; then
TEMPLATE_NAME="${TEMPLATE_NAMES[$DISTRO]}"
fi
# ============================================
# Main Script
# ============================================
print_header
check_root
check_vmid_available
echo "Configuration:"
echo " VMID: $VMID"
echo " Name: $TEMPLATE_NAME"
echo " Distribution: $DISTRO_NAME"
echo " Storage: $STORAGE"
echo " Bridge: $BRIDGE"
echo " Memory: ${MEMORY}MB"
echo " CPUs: $CORES"
echo " Disk: $DISK_SIZE"
echo ""
# Determine image filename
IMAGE_FILE="/tmp/cloud-init-${DISTRO}.img"
# Step 1: Download cloud image
print_step "Downloading $DISTRO_NAME cloud image..."
if [[ -f "$IMAGE_FILE" ]]; then
echo " Using cached image: $IMAGE_FILE"
else
wget -q --show-progress -O "$IMAGE_FILE" "$CLOUD_IMAGE_URL"
fi
print_success "Cloud image ready"
print_step "Installing qemu-guest-agent and configuring image..."
if ! command -v virt-customize &>/dev/null; then
print_error "virt-customize is not installed! Run 'apt update && apt install -y libguestfs-tools' on the Proxmox host first."
fi
# Ensure virt-customize works reliably on Proxmox by disabling libvirt backend
export LIBGUESTFS_BACKEND=direct
print_step "Running virt-customize (this might take a few minutes)..."
virt-customize -a "$IMAGE_FILE" \
--update \
--install qemu-guest-agent \
--run-command 'systemctl enable qemu-guest-agent' \
--run-command 'rm -f /etc/ssh/sshd_config.d/60-cloudimg-settings.conf 2>/dev/null || true' \
--run-command 'rm -f /etc/ssh/sshd_config.d/50-cloud-init.conf 2>/dev/null || true' \
--run-command 'echo "PasswordAuthentication yes" > /etc/ssh/sshd_config.d/99-pve.conf' \
--run-command 'sed -i "s/#PasswordAuthentication yes/PasswordAuthentication yes/" /etc/ssh/sshd_config 2>/dev/null || true' \
--run-command 'sed -i "s/PasswordAuthentication no/#PasswordAuthentication no/" /etc/ssh/sshd_config 2>/dev/null || true' \
--run-command 'echo "XKBMODEL=\"pc105\"" > /etc/default/keyboard' \
--run-command 'echo "XKBLAYOUT=\"de\"" >> /etc/default/keyboard' \
--run-command 'echo "XKBVARIANT=\"\"" >> /etc/default/keyboard' \
--run-command 'echo "XKBOPTIONS=\"\"" >> /etc/default/keyboard' \
--run-command 'dpkg-reconfigure -f noninteractive keyboard-configuration 2>/dev/null || true' \
--run-command 'mkdir -p /etc/cloud/cloud.cfg.d' \
--run-command 'cat << EOF > /etc/cloud/cloud.cfg.d/99-pve.cfg
datasource_list: [ NoCloud, ConfigDrive ]
ssh_pwauth: true
chpasswd: { expire: False }
keyboard:
layout: de
runcmd:
- [ systemctl, daemon-reload ]
- [ systemctl, enable, --now, qemu-guest-agent ]
EOF'
print_success "Image customization complete"
# Step 3: Create the VM
print_step "Creating VM $VMID..."
qm create "$VMID" \
--name "$TEMPLATE_NAME" \
--ostype l26 \
--machine q35 \
--bios ovmf \
--cpu host \
--cores "$CORES" \
--memory "$MEMORY" \
--balloon "$MEMORY" \
--net0 "virtio,bridge=$BRIDGE,firewall=1" \
--scsihw virtio-scsi-single \
--agent enabled=1 \
--serial0 socket \
--vga virtio \
--keyboard de \
--tags "cloud-init,template"
print_success "VM created"
# Step 4: Import disk
print_step "Importing disk to $STORAGE..."
# Note: LVM-thin only supports raw format, Proxmox will auto-convert qcow2 to raw
IMPORT_OUTPUT=$(qm importdisk "$VMID" "$IMAGE_FILE" "$STORAGE" 2>&1)
echo "$IMPORT_OUTPUT"
# Extract the imported disk volume name (works for both directory and LVM storage)
# Directory storage output: "successfully imported disk 'local:9000/vm-9000-disk-0.raw'"
# LVM storage output: "successfully imported disk 'local-lvm:vm-9000-disk-0'"
IMPORTED_DISK=$(echo "$IMPORT_OUTPUT" | grep -oP "successfully imported disk '\K[^']+")
if [[ -z "$IMPORTED_DISK" ]]; then
# Fallback: try to find the unused disk via qm config
IMPORTED_DISK=$(qm config "$VMID" | grep -oP 'unused0:\s*\K\S+')
fi
if [[ -z "$IMPORTED_DISK" ]]; then
print_error "Could not determine imported disk name"
fi
echo " Detected disk volume: $IMPORTED_DISK"
print_success "Disk imported"
# Step 5: Attach disk and configure boot
print_step "Configuring disk and boot order..."
qm set "$VMID" \
--scsi0 "${IMPORTED_DISK},discard=on,ssd=1" \
--boot order=scsi0 \
--efidisk0 "$STORAGE:1,efitype=4m,pre-enrolled-keys=1"
# Resize disk to target size
qm disk resize "$VMID" scsi0 "$DISK_SIZE"
print_success "Disk configured and resized to $DISK_SIZE"
# Step 6: Add Cloud-Init drive
print_step "Adding Cloud-Init drive..."
qm set "$VMID" --ide2 "$STORAGE:cloudinit"
print_success "Cloud-Init drive added"
# Step 7: Configure Cloud-Init defaults
print_step "Setting Cloud-Init defaults..."
qm set "$VMID" \
--citype nocloud \
--ipconfig0 "ip=dhcp"
print_success "Cloud-Init configured (DHCP default, supports static IP)"
# Step 8: Enable QEMU Guest Agent
print_step "Enabling QEMU Guest Agent..."
qm set "$VMID" --agent enabled=1
print_success "QEMU Guest Agent enabled"
# Step 9: Convert to template
print_step "Converting VM to template..."
qm template "$VMID"
print_success "VM converted to template"
# ============================================
# Summary
# ============================================
echo ""
echo "╔══════════════════════════════════════════════════════════════╗"
echo "║ Template Created! ║"
echo "╚══════════════════════════════════════════════════════════════╝"
echo ""
echo "Template Details:"
echo " VMID: $VMID"
echo " Name: $TEMPLATE_NAME"
echo " Distro: $DISTRO_NAME"
echo " Storage: $STORAGE"
echo ""
echo "Usage in PRXDash:"
echo " - Select this template when creating new VMs"
echo " - Configure Cloud-Init: user, SSH keys, network"
echo ""
echo "Manual Clone Example:"
echo " qm clone $VMID NEW_VMID --name my-new-vm --full"
echo ""
echo "Static IP Configuration:"
echo " qm set NEW_VMID --ipconfig0 'ip=192.168.1.100/24,gw=192.168.1.1'"
echo ""