-
Notifications
You must be signed in to change notification settings - Fork 1
/
ubuntu-template-create.sh
137 lines (117 loc) · 4.88 KB
/
ubuntu-template-create.sh
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
#!/bin/sh
# Function to print usage
usage() {
echo "Usage: $0 [OPTIONS]"
echo ""
echo "Options:"
echo " -c, --clean Force re-download of the Ubuntu image and recreate the VM template."
echo " -p, --pass Set a custom password for the cloud-init user (default: 'password')."
echo " -i, --image Specify a custom image URL for the VM template."
echo " -s, --storage Specify the storage location for the VM template (default: 'local')."
echo " -u, --username Set a custom username for the cloud-init user (default: 'administrator')."
echo " -t, --timezone Set a timezone (default: 'Europe/London')."
echo " -n, --name Set the time of the VM (default: 'ubuntu-2204-template')."
echo " -h, --help Display this help message and exit."
echo ""
echo "This script creates a Proxmox VM template based on a specified or default Ubuntu Cloud Image."
}
# Default values
PASSWORD="password"
CLEAN=0
STORAGE="local"
IMAGE_URL="http://cloud-images.ubuntu.com/releases/22.04/release/ubuntu-22.04-server-cloudimg-amd64-disk-kvm.img"
USERNAME="administrator"
TIMEZONE="Europe/London"
NAME="ubuntu-2204-template"
# Parse command line arguments
while [ "$#" -gt 0 ]; do
case "$1" in
-c|--clean) CLEAN=1 ;;
-p|--pass) PASSWORD="$2"; shift ;;
-i|--image) IMAGE_URL="$2"; shift ;;
-s|--storage) STORAGE="$2"; shift ;;
-u|--username) USERNAME="$2"; shift ;;
-t|--timezone) TIMEZONE="$2"; shift ;;
-n|--name) NAME="$2"; shift ;;
-h|--help) usage; exit 0 ;;
*) echo "Unknown parameter passed: $1"; usage; exit 1 ;;
esac
shift
done
IMAGE_NAME=$(basename "$IMAGE_URL")
# Update system and install required tools
echo "Installing required tools..."
apt update -y && apt install -y nano wget curl libguestfs-tools
# Conditionally remove and re-download the image
if [ $CLEAN -eq 1 ] || [ ! -f "$IMAGE_NAME" ]; then
echo "Removing old image and downloading a new one..."
rm -fv "$IMAGE_NAME"
echo "Downloading Ubuntu Cloud Image..."
wget --inet4-only "$IMAGE_URL"
else
echo "Image already exists. Skipping download..."
fi
# Destroy existing VM template if it exists
echo "Checking for existing VM template..."
if qm status 9000 >/dev/null 2>&1; then
echo "Destroying existing VM template..."
qm destroy 9000 --destroy-unreferenced-disks 1 --purge 1
else
echo "No existing VM template found. Proceeding..."
fi
# Resize the image
echo "Resizing the image..."
qemu-img resize "$IMAGE_NAME" +5G
qemu-img resize "$IMAGE_NAME" +820M
# Customize the image with qemu-guest-agent, timezone, and SSH settings
echo "Customizing the image..."
virt-customize -a "$IMAGE_NAME" \
--install qemu-guest-agent,cloud-init \
--timezone $TIMEZONE \
--run-command 'sed -i "s/^PasswordAuthentication.*/PasswordAuthentication yes/" /etc/ssh/sshd_config' \
--run-command 'sed -i "s/^#PermitRootLogin.*/PermitRootLogin prohibit-password/" /etc/ssh/sshd_config' \
--run-command 'apt-get update && DEBIAN_FRONTEND=noninteractive apt-get upgrade -y && apt-get clean' \
--run-command 'rm -rf /var/lib/apt/lists/*' \
--run-command 'dd if=/dev/zero of=/EMPTY bs=1M || true' \
--run-command 'rm -f /EMPTY' \
--run-command 'cloud-init clean'
# Create the VM template
echo "Creating VM template..."
qm create 9000 --name "$NAME" --memory 4096 --cores 2 \
--net0 virtio,bridge=vmbr0,firewall=1 \
--bios ovmf --agent enabled=1 --ostype l26 --serial0 socket \
--vga serial0 --machine q35 --scsihw virtio-scsi-single \
--efidisk0 $STORAGE:9000,efitype=4m
# Import the image to VM and convert to QCOW2
echo "Importing image to VM and converting to QCOW2 format..."
IMPORT_OUTPUT=$(qm importdisk 9000 "$IMAGE_NAME" $STORAGE --format qcow2 2>&1)
DISK_NAME=$(echo "$IMPORT_OUTPUT" | grep -oP "Successfully imported disk as \'\K[^']+" | sed 's/^unused0://')
# Verify disk name was captured
if [ -z "$DISK_NAME" ]; then
echo "Failed to capture the disk name from the import operation."
exit 1
else
echo "Imported disk name: $DISK_NAME"
fi
# Add the disk to VM
echo "Adding disk to VM template..."
qm set 9000 --scsi0 "$DISK_NAME"
# Set the boot disk
echo "Setting boot disk..."
qm set 9000 --boot c --bootdisk scsi0
# Add cloud-init drive
echo "Adding cloud-init drive..."
qm set 9000 --scsi1 $STORAGE:cloudinit
# Set user/password
echo "Setting cloud-init user and password..."
qm set 9000 --ciuser $USERNAME --cipassword $PASSWORD
# Convert VM to template
echo "Converting VM to template..."
QM_TEMPLATE_OUTPUT=$(qm template 9000 2>&1)
# Check for errors and complete
if echo "$QM_TEMPLATE_OUTPUT" | grep -q "chattr: Operation not supported"; then
echo "Note: 'chattr' operation not supported on this storage. This does not impact the template functionality."
else
echo "VM template conversion completed successfully."
fi
echo "Template creation script completed."