Files
proxmox/template_create.sh
T
2026-09-21 23:57:12 -05:00

206 lines
7.0 KiB
Bash
Executable File

#!/bin/bash
CUSTOM=false
BUILT_IN_CLOUDINIT=true
CIUSER="${CIUSER:-default}"
CIPASSWORD="${CIPASSWORD:-password}"
IPCONFIG="${IPCONFIG:-ip=dhcp}"
SSHKEYS="${SSHKEYS:-}"
QM_CLOUDINIT="${QM_CLOUDINIT:---ciuser default --cipassword password --ipconfig0 ip=dhcp}"
AGENT="${AGENT:-1}"
BALLOON="${BALLOON:-0}"
MEMORY="${MEMORY:-1024}"
CORES="${CORES:-1}"
BIOS_TYPE="${BIOS_TYPE:-ovmf}"
CPU_TYPE="${CPU_TYPE:-cputype=host}"
EFI_OPTS="${EFI_OPTS:-1,efitype=4m,pre-enrolled-keys=0}"
MACHINE_TYPE="${MACHINE_TYPE:-q35}"
OS_TYPE="${OS_TYPE:-l26}"
SCSI_TYPE="${SCSI_TYPE:-virtio-scsi-pci}"
CPU_SOCKETS="${CPU_SOCKETS:-1}"
BRIDGE="${BRIDGE:-vmbr0}"
[ -f .env ] && source .env
declare -A distros=()
declare -A distro_array=(
[debian12]="https://cloud.debian.org/images/cloud/bookworm/latest/debian-12-generic-amd64.qcow2"
[debian13]="https://cloud.debian.org/images/cloud/trixie/latest/debian-13-generic-amd64.qcow2"
[ubuntu2204]="https://cloud-images.ubuntu.com/minimal/releases/jammy/release/ubuntu-22.04-minimal-cloudimg-amd64.img"
[ubuntu2404]="https://cloud-images.ubuntu.com/minimal/releases/noble/release/ubuntu-24.04-minimal-cloudimg-amd64.img"
[ubuntu2604]="https://cloud-images.ubuntu.com/minimal/releases/resolute/release/ubuntu-26.04-minimal-cloudimg-amd64.img"
[centos9]="https://cloud.centos.org/centos/9-stream/x86_64/images/CentOS-Stream-GenericCloud-9-20230123.0.x86_64.qcow2"
[centos10]="https://cloud.centos.org/centos/10-stream/x86_64/images/CentOS-Stream-GenericCloud-10-20230123.0.x86_64.qcow2"
[rocky9]="https://dl.rockylinux.org/pub/rocky/9/images/x86_64/Rocky-9-GenericCloud-Base.latest.x86_64.qcow2"
[rocky10]="https://dl.rockylinux.org/pub/rocky/10/images/x86_64/Rocky-10-GenericCloud-Base.latest.x86_64.qcow2"
[fedora44]="https://ftp2.osuosl.org/pub/fedora/linux/releases/44/Cloud/x86_64/images/Fedora-Cloud-Base-Generic-44-1.7.x86_64.qcow2"
[opensuse156]="https://download.opensuse.org/repositories/Cloud:/Images:/Leap_15.6/images/openSUSE-Leap-15.6.x86_64-1.0.1-NoCloud.qcow2"
)
Help(){
echo Use this script with the following flags
echo "-v Select the VMID to use for template *REQUIRED"
echo "-s Select the PVE storage i.e. local-lvm *REQUIRED"
echo "-a Create all the templates listed below"
echo "-d Create Debian 12 & 13 templates"
echo "-u Create Ubuntu 22.04, 24.04 & 26.04 templates"
echo "-c Create CentOS 9 & 10 templates"
echo "-r Create Rocky 9 & 10 templates"
echo "-f Create Fedora 44 template"
echo "-o Create OpenSUSE 15.6 template"
echo "-k \"Custom\" image not in the above list."
echo " You will be prompted to provide the distro name"
echo " and the url for the qcow2 file."
}
assertNotEmpty() {
: "${!1:? "$1 is required, see help (-h) for more info."}"
}
download_image() {
echo "Downloading $IMAGE now"; wget -qO $IMAGE $URL
if $CUSTOM; then
xz --test $IMAGE && mv $IMAGE $IMAGE.xz && unxz $IMAGE.xz
fi
}
qcow2_modification() {
echo "Install qemu-guest-agent on first boot"; virt-customize -a $IMAGE --firstboot-install qemu-guest-agent > /dev/null
echo "Enable qemu-guest-agent service on first boot"; virt-customize -a $IMAGE --firstboot-command "systemctl enable --now qemu-guest-agent" > /dev/null
echo "Removing /etc/machine-id"; virt-customize -a $IMAGE --run-command '>/etc/machine-id' > /dev/null
}
create_template() {
# create VM, resize the disk, import disk and create Cloud Init drive. Then set default settings for vm
echo "Creating $VMID with name \"$DISTRO-template\""; qm create $VMID --agent $AGENT --balloon $BALLOON --memory $MEMORY --cores $CORES --bios $BIOS_TYPE --cpu $CPU_TYPE --efidisk0 $STORAGE:$EFI_OPTS --ide2 $STORAGE:cloudinit --machine $MACHINE_TYPE --ostype $OS_TYPE --scsihw $SCSI_TYPE --sockets $CPU_SOCKETS --name $DISTRO-template --net0 virtio,bridge=$BRIDGE > /dev/null
echo "Resizing disk"; qemu-img resize $IMAGE 10G > /dev/null
echo "Importing disk"; qm importdisk $VMID $IMAGE $STORAGE > /dev/null
echo "Setting Virtual Drive to scsi0"; qm set $VMID --scsihw virtio-scsi-pci --scsi0 $STORAGE:vm-$VMID-disk-1,discard=on > /dev/null
echo "Set bootdisk to scsi0"; qm set $VMID --boot c --bootdisk scsi0 > /dev/null
# Define Cloud-Init parameters based on env file or the user interaction
echo "Set Cloud Init params"; qm set $VMID $QM_CLOUDINIT > /dev/null
echo Converting $VMID to Template; qm template $VMID > /dev/null
echo Deleting $IMAGE file; rm $IMAGE
echo "Your new ${DISTRO^} template is ready!"
}
get_cloudinit_params() {
if [ ! -f .env ]; then
read -p "Enter username to be created: " CIUSER
read -sp "Enter password for user (optional): " CIPASSWORD
read -p "Do you want Static IP for template? (y/N) "
if [[ $REPLY =~ ^[Yy]$ ]]; then
echo "Enter IP info"
read -p " Format is \"gw=<GatewayIP>,ip=<IPv4Format/CIDR>\": " IPCONFIG
else
IPCONFIG="ip=dhcp"
fi
read -p "File path to ssh key for user (optional): " SSHKEYS
fi
}
get_custom_info() {
read -p "Enter the distro name (no spaces): " DISTRO
echo "Enter the url for the download of the vm image"
read -p "(example: https://cloud.debian.org/images/cloud/bookworm/latest/debian-12-generic-amd64.qcow2) " URL
assertNotEmpty DISTRO
assertNotEmpty URL
}
while getopts hrocafdukv:s: opt
do
case $opt in
h)
Help
exit 0
;;
a)
for key in "${!distro_array[@]}"; do
distros["$key"]="${distro_array[$key]}"
done
;;
v)
VMID=$OPTARG
;;
d)
distros[debian12]="${distro_array[debian12]}"
distros[debian13]="${distro_array[debian13]}"
;;
u)
distros[ubuntu2204]="${distro_array[ubuntu2204]}"
distros[ubuntu2404]="${distro_array[ubuntu2404]}"
distros[ubuntu2604]="${distro_array[ubuntu2604]}"
;;
f)
distros[fedora44]="${distro_array[fedora44]}"
;;
s)
STORAGE=$OPTARG
;;
k)
CUSTOM=true
;;
r)
distros[rocky9]="${distro_array[rocky9]}"
distros[rocky10]="${distro_array[rocky10]}"
;;
o)
distros[opensuse156]="${distro_array[opensuse156]}"
;;
c)
distros[centos9]="${distro_array[centos9]}"
distros[centos10]="${distro_array[centos10]}"
;;
\?)
echo "Invalid Flag"
exit 1
;;
esac
done
############## Start of Script ###########################
# check that required items are present
assertNotEmpty VMID
assertNotEmpty STORAGE
if $BUILT_IN_CLOUDINIT; then
echo "Checking for required package"; which virt-customize || apt update && apt install -y libguestfs-tools > /dev/null
fi
if $CUSTOM; then
get_custom_info
fi
get_cloudinit_params
for DISTRO in ${!distros[@]}; do
IMAGE=$DISTRO.qcow2
URL=${distros[$DISTRO]}
download_image
$BUILT_IN_CLOUDINIT && qcow2_modification
create_template
((VMID=VMID+1))
done