-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate-template-vm.sh
More file actions
executable file
·97 lines (86 loc) · 2.89 KB
/
create-template-vm.sh
File metadata and controls
executable file
·97 lines (86 loc) · 2.89 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
#!/bin/bash
# Create a production VM template from Ubuntu cloud image.
# Run this on the Proxmox HOST.
#
# Prerequisites:
# - Download Ubuntu cloud image:
# wget -O /var/lib/vz/template/iso/ubuntu-24.04-cloudimg.img \
# https://cloud-images.ubuntu.com/noble/current/noble-server-cloudimg-amd64.img
# - Have your SSH public key available
#
# Configure via environment or arguments:
# TEMPLATE_VM_ID (default: 9000)
# SSH_KEY_FILE (default: ~/.ssh/id_ed25519.pub)
# BRIDGE_GATEWAY (default: 10.0.0.1)
#
# Usage: sudo ./create-template-vm.sh [vmid] [name]
set -euo pipefail
VMID="${1:-${TEMPLATE_VM_ID:-9000}}"
NAME="${2:-ubuntu-prod-template}"
STORAGE="local"
CLOUD_IMAGE="/var/lib/vz/template/iso/ubuntu-24.04-cloudimg.img"
MEMORY="2048"
CORES="2"
DISK_SIZE="20G"
SSH_KEY_FILE="${SSH_KEY_FILE:-$HOME/.ssh/id_ed25519.pub}"
GATEWAY="${BRIDGE_GATEWAY:-10.0.0.1}"
TEMP_IP="10.0.0.250/24"
CIUSER="${CLOUD_INIT_USER:-ubuntu}"
echo "=== Creating template VM $VMID ($NAME) ==="
if [ ! -f "$CLOUD_IMAGE" ]; then
echo "ERROR: Cloud image not found: $CLOUD_IMAGE" >&2
echo "Download it first:" >&2
echo " wget -O $CLOUD_IMAGE https://cloud-images.ubuntu.com/noble/current/noble-server-cloudimg-amd64.img" >&2
exit 1
fi
if [ ! -f "$SSH_KEY_FILE" ]; then
echo "ERROR: SSH key file not found: $SSH_KEY_FILE" >&2
echo "Set SSH_KEY_FILE to your public key path" >&2
exit 1
fi
if qm status "$VMID" &>/dev/null; then
echo "ERROR: VM $VMID already exists" >&2
exit 1
fi
echo "Creating VM..."
qm create "$VMID" \
--name "$NAME" \
--memory "$MEMORY" \
--cores "$CORES" \
--cpu cputype=host \
--net0 virtio,bridge=vmbr0 \
--scsihw virtio-scsi-pci \
--serial0 socket \
--vga serial0 \
--agent enabled=1
echo "Importing cloud image disk..."
qm disk import "$VMID" "$CLOUD_IMAGE" "$STORAGE" --format qcow2
echo "Attaching disk..."
qm set "$VMID" --scsi0 "${STORAGE}:${VMID}/vm-${VMID}-disk-0.qcow2,ssd=1"
echo "Resizing disk to $DISK_SIZE..."
qm disk resize "$VMID" scsi0 "$DISK_SIZE"
echo "Configuring boot and cloud-init..."
qm set "$VMID" \
--boot order=scsi0 \
--ide2 "${STORAGE}:cloudinit" \
--ciuser "$CIUSER" \
--ipconfig0 "ip=${TEMP_IP},gw=${GATEWAY}" \
--nameserver "1.1.1.1" \
--sshkeys "$SSH_KEY_FILE"
echo ""
echo "=== VM $VMID created ==="
echo ""
echo "Next steps:"
echo " 1. Start the VM: qm start $VMID"
echo " 2. Wait for boot, then SSH in: ssh ${CIUSER}@10.0.0.250"
echo " 3. Copy and run prepare-prod-template.sh:"
echo " scp scripts/prepare-prod-template.sh ${CIUSER}@10.0.0.250:/tmp/"
echo " ssh ${CIUSER}@10.0.0.250 'sudo bash /tmp/prepare-prod-template.sh'"
echo " 4. Shut down the VM: qm shutdown $VMID"
echo " 5. Convert to template: qm template $VMID"
echo ""
echo "To clone from template:"
echo " qm clone $VMID <new-vmid> --name <app-name> --full"
echo " qm set <new-vmid> --ipconfig0 ip=10.0.0.<x>/24,gw=${GATEWAY}"
echo " qm start <new-vmid>"
echo ""