-
-
Save maxfield-allison/9ff96f67acabefa17eceabcb1aa5d193 to your computer and use it in GitHub Desktop.
#!/bin/sh | |
# Define storage variable | |
STORAGE=nas | |
# Install necessary tools | |
echo "Checking tools" | |
apt update -y && apt install nano wget curl libguestfs-tools -y | |
# Remove old image | |
echo "Removing old image..." | |
rm -fv ubuntu-22.04-server-cloudimg-amd64-disk-kvm.img | |
# Check if VM 9000 exists before attempting to destroy it | |
if qm status 9000 >/dev/null 2>&1; then | |
echo "Destroying existing VM 9000..." | |
qm destroy 9000 --destroy-unreferenced-disks 1 --purge 1 | |
else | |
echo "VM 9000 does not exist. Skipping destroy command." | |
fi | |
# Download new image | |
echo "Downloading new image..." | |
wget --inet4-only http://cloud-images.ubuntu.com/releases/22.04/release/ubuntu-22.04-server-cloudimg-amd64-disk-kvm.img | |
# Add agent to image | |
echo "Customizing image: adding qemu-guest-agent..." | |
virt-customize -a ubuntu-22.04-server-cloudimg-amd64-disk-kvm.img --install qemu-guest-agent | |
# Set timezone | |
echo "Customizing image: setting timezone..." | |
virt-customize -a ubuntu-22.04-server-cloudimg-amd64-disk-kvm.img --timezone America/Chicago | |
# Set password auth to yes | |
echo "Customizing image: enabling password authentication..." | |
virt-customize -a ubuntu-22.04-server-cloudimg-amd64-disk-kvm.img --run-command 'sed -i s/^PasswordAuthentication.*/PasswordAuthentication\ yes/ /etc/ssh/sshd_config' | |
# Allow root login with ssh-key only | |
echo "Customizing image: setting root login policy..." | |
virt-customize -a ubuntu-22.04-server-cloudimg-amd64-disk-kvm.img --run-command 'sed -i s/^#PermitRootLogin.*/PermitRootLogin\ prohibit-password/ /etc/ssh/sshd_config' | |
# Increase image size | |
echo "Resizing image..." | |
qemu-img resize ubuntu-22.04-server-cloudimg-amd64-disk-kvm.img +5G | |
qemu-img resize ubuntu-22.04-server-cloudimg-amd64-disk-kvm.img +820M | |
# Create VM | |
echo "Creating VM..." | |
qm create 9000 --name "ubuntu-2204-template" --memory 4096 --cores 2 --net0 virtio,bridge=vmbr0,tag=20,firewall=1 --bios ovmf --agent enabled=1 --ostype l26 --serial0 socket --vga serial0 --machine q35 --scsihw virtio-scsi-pci | |
# Import image to VM and convert to QCOW2 during the import | |
echo "Importing image to VM and converting to QCOW2 format..." | |
IMPORT_OUTPUT=$(qm importdisk 9000 ubuntu-22.04-server-cloudimg-amd64-disk-kvm.img $STORAGE --format qcow2 2>&1) | |
# Extract the disk name from the import output, removing the 'unused0:' prefix | |
DISK_NAME=$(echo "$IMPORT_OUTPUT" | grep -oP "Successfully imported disk as \'\K[^']+" | sed 's/^unused0://') | |
# Check if the 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 | |
# Adding disk to VM using the captured disk name | |
echo "Disk to be added: $DISK_NAME" | |
qm set 9000 --scsi0 "$DISK_NAME" | |
# Set bootdisk to the newly added disk | |
echo "Setting boot disk..." | |
qm set 9000 --boot c --bootdisk scsi0 | |
# Convert to template | |
echo "Converting VM to template..." | |
qm template 9000 | |
echo "Script completed." |
thank you for expanding on my gist! just went to use my gist after a while and discovered you had expanded it! would be amazing if you can expand it more and let users config everything right at the top of the script like timezone, what network to attach, what nodeid to use rather than 9000 maybe even what ubuntu version too!
edit: you also forgot to add in the
--scsi1 nas:cloudinit
so it isnt adding in the cloudinit drive edit2: you also forgot to add in the--efidisk0
so the is no UEFI bios to save secure keys etc into
Good suggestions and catches. I'll update it in a bit. I may have already done some of this in my scripts repo but I can't remember off the top
Yea here we go https://github.com/maxfield-allison/scripts/blob/main/ubuntu-template-create.sh
Oh wow legend!
A few thing I've spotted, so I'll fork ur repo n make my own changes.
for example the user create and and update packages, don't need to be done in the image itself,
as u can specify these in the proxmox ui per vm and it gets done when u first start ur vm.
This lets u use the template as a template for customers :)
Also timezone is hard-encoded ans the cloud init for proxmox won't run because it needs to be attached to the scsi and not the ide (ubuntu thing if ur drives are attached as scsi instead of ide)
thank you for expanding on my gist!
just went to use my gist after a while and discovered you had expanded it!
would be amazing if you can expand it more and let users config everything right at the top of the script
like timezone, what network to attach, what nodeid to use rather than 9000
maybe even what ubuntu version too!
edit: you also forgot to add in the
--scsi1 nas:cloudinit
so it isnt adding in the cloudinit driveedit2: you also forgot to add in the
--efidisk0
so the is no UEFI bios to save secure keys etc into