-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate-ubuntu-template.sh
More file actions
66 lines (48 loc) · 1.62 KB
/
create-ubuntu-template.sh
File metadata and controls
66 lines (48 loc) · 1.62 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
#!/bin/bash
set -e
TEMPLATE_NAME="ubuntu-${RELEASE}-template"
DATA_STORE="local-lvm"
VM_BRIDGE="vmbr0"
# Check if exactly two arguments are passed
if [ "$#" -ne 2 ]; then
echo "You must enter exactly 2 arguments: release, TEMPLATE_ID"
exit 1
fi
# Assign arguments to variables
RELEASE="$1"
TEMPLATE_ID="$2"
# Array of valid src_ids
UBUNTU_RELEASES=("jammy" "focal" "bionic")
# Check if src_id is valid
VALID=false
for id in "${UBUNTU_RELEASES[@]}"; do
if [ "$RELEASE" == "$id" ]; then
VALID=true
break
fi
done
if [ "$VALID" = true ]; then
echo "Creating template for release: $RELEASE"
else
echo "Error: Invalid release. Must be one of: ${UBUNTU_RELEASES[*]}"
exit 1
fi
# Check if dest_value is greater than 9000
if [ "$TEMPLATE_ID" -gt 9000 ]; then
echo "TEMPLATE_ID: $TEMPLATE_ID is greater than 9000"
else
echo "Error: Destination Value must be greater than 9000."
exit 1
fi
# Get the ubuntu relase image
wget "https://cloud-images.ubuntu.com/${RELEASE}/current/${RELEASE}-server-cloudimg-amd64.img"
# Create a VM
qm create "${TEMPLATE_ID} "--name "${TEMPLATE_NAME}" --memory 2048 --net0 virtio,bridge=${VM_BRIDGE}
qm importdisk "$TEMPLATE_ID" "${RELEASE}-server-cloudimg-amd64.img" $DATA_STORE -format qcow2
qm set"$TEMPLATE_ID"--scsihw virtio-scsi-pci --scsi0 $DATA_STORE:"vm-$TEMPLATE_ID-disk-0"
qm set"$TEMPLATE_ID"--ide2 local:cloudinit --boot c --bootdisk scsi0 --serial0 socket --vga serial0
qm resize"$TEMPLATE_ID"scsi0 +30G
qm set"$TEMPLATE_ID"--ipconfig0 ip=dhcp
qm set"$TEMPLATE_ID"--sshkey ~/id_rsa.pub
qm cloudinit dump "$TEMPLATE_ID" user
qm template "$TEMPLATE_ID"