#!/usr/bin/env bash function createImage() { IMAGE_FILE_URL="$1" IMAGE_ID="$2" IMAGE_FILE="$3" IMAGE_NAME="$4" IMAGE_RAM="$5" IMAGE_CORES="$6" IMAGE_STORE="$7" IMAGE_TYPE="$8" SSH_KEYS_FILE="keys" stat "${IMAGE_FILE}" if [ "$?" -gt "0" ] then wget "${IMAGE_FILE_URL}" -O "${IMAGE_FILE}" apt update apt install -y libguestfs-tools case "${IMAGE_TYPE}" in deb*|ubuntu*) virt-customize -v -a ${IMAGE_FILE} --install qemu-guest-agent ;; alma*|rhel*|centos*) virt-customize -v -a ${IMAGE_FILE} --commands-from-file virt-customize -v -a ${IMAGE_FILE} --install qemu-guest-agent ;; *) echo "Unknown OS type" ;; esac fi case "${IMAGE_TYPE}" in deb*|ubuntu*) DISPLAY_TYPE="serial" ;; alma*|rhel*|centos*) DISPLAY_TYPE="default" ;; *) DISPLAY_TYPE="default" echo "Unknown OS type" ;; esac qm create ${IMAGE_ID} --name "${IMAGE_NAME}" --memory ${IMAGE_RAM} --cores ${IMAGE_CORES} \ --net0 virtio,bridge=vmbr0 qm importdisk ${IMAGE_ID} ${IMAGE_FILE} ${IMAGE_STORE} --format qcow2 qm set ${IMAGE_ID} --cpu cputype=x86-64-v2-AES qm set ${IMAGE_ID} --scsihw virtio-scsi-pci --scsi0 ${IMAGE_STORE}:vm-${IMAGE_ID}-disk-0 # qm resize ${IMAGE_ID} virtio0 +15G qm set ${IMAGE_ID} --boot c --bootdisk scsi0 qm set ${IMAGE_ID} --ide2 ${IMAGE_STORE}:cloudinit case "${DISPLAY_TYPE}" in serial) qm set ${IMAGE_ID} --serial0 socket --vga serial0 ;; default) qm set ${IMAGE_ID} --vga std ;; *) qm set ${IMAGE_ID} --vga std ;; esac qm set ${IMAGE_ID} --ipconfig0 ip=dhcp qm set ${IMAGE_ID} --agent enabled=1 qm set ${IMAGE_ID} --sshkeys ${SSH_KEYS_FILE} # qm template ${IMAGE_ID} rm -vf "${IMAGE_FILE}" } function getDownloadSpeed() { local url="$1" local duration=10 # Set the duration for the download test (in seconds) local speed=$(curl -o /dev/null -s -w '%{speed_download}\n' --max-time $duration "$url") echo "$speed" } LOCAL_STORE="$( head -1 store-name)" if [ -z "${LOCAL_STORE}" ] then echo "Missing proxmox vm store name" exit 1 else echo "Using proxmox vm store: ${LOCAL_STORE}" fi USE_LOCAL_IMAGES="0" ## Debian 12 (BookWorm) DEBIAN_12_IMAGE_URL="https://cloud.debian.org/images/cloud/bookworm/latest/debian-12-generic-amd64.qcow2" DEBIAN_12_LOCAL_IMAGE_URL="https://www.ngtech.co.il/static/cloud-images/debian-12-generic-amd64.qcow2" DEBIAN_12_TEMPLATE_ID="9000" DEBIAN_12_IMAGE_FILENAME="debian-12.qcow2" DEBIAN_12_TEMPLATE_NAME="debian-12-cloudinit-template" DEBIAN_12_TEMPLATE_RAM="2048" DEBIAN_12_TEMPLATE_CORES="2" DEBIAN_12_TEMPLATE_STORE="${LOCAL_STORE}" ## Download Speed test comparison between debian and local mirror REMOTE_DOWNLOAD_SPEED=$(getDownloadSpeed "${DEBIAN_12_IMAGE_URL}" ) LOCAL_DOWNLOAD_SPEED=$(getDownloadSpeed "${DEBIAN_12_LOCAL_IMAGE_URL}" ) echo "${REMOTE_DOWNLOAD_SPEED}" echo "${LOCAL_DOWNLOAD_SPEED}" if [ "${LOCAL_DOWNLOAD_SPEED}" -gt "${REMOTE_DOWNLOAD_SPEED}" ]; then USE_LOCAL_IMAGES="1" fi if [ "${USE_LOCAL_IMAGES}" -eq "1" ] then DEBIAN_12_IMAGE_URL="${DEBIAN_12_LOCAL_IMAGE_URL}" fi if [ -f "flags/debian-12" ] then createImage "${DEBIAN_12_IMAGE_URL}" "${DEBIAN_12_TEMPLATE_ID}" \ "${DEBIAN_12_IMAGE_FILENAME}" "${DEBIAN_12_TEMPLATE_NAME}" \ "${DEBIAN_12_TEMPLATE_RAM}" "${DEBIAN_12_TEMPLATE_CORES}" "${DEBIAN_12_TEMPLATE_STORE}" "deb" fi ## AlmaLinux 8 ALMA_8_IMAGE_URL="https://repo.almalinux.org/almalinux/8/cloud/x86_64/images/AlmaLinux-8-GenericCloud-latest.x86_64.qcow2" ALMA_8_LOCAL_IMAGE_URL="https://www.ngtech.co.il/static/cloud-images/AlmaLinux-8-GenericCloud-latest.x86_64.qcow2" ALMA_8_TEMPLATE_ID="9001" ALMA_8_IMAGE_FILENAME="alma-8.qcow2" ALMA_8_TEMPLATE_NAME="alma-8-cloudinit-template" ALMA_8_TEMPLATE_RAM="2048" ALMA_8_TEMPLATE_CORES="2" ALMA_8_TEMPLATE_STORE="${LOCAL_STORE}" if [ "${USE_LOCAL_IMAGES}" -eq "1" ] then ALMA_8_IMAGE_URL="${ALMA_8_LOCAL_IMAGE_URL}" fi if [ -f "flags/alma-8" ] then createImage "${ALMA_8_IMAGE_URL}" "${ALMA_8_TEMPLATE_ID}" \ "${ALMA_8_IMAGE_FILENAME}" "${ALMA_8_TEMPLATE_NAME}" \ "${ALMA_8_TEMPLATE_RAM}" "${ALMA_8_TEMPLATE_CORES}" "${ALMA_8_TEMPLATE_STORE}" "alma" fi ## AlmaLinux 9 ALMA_9_IMAGE_URL="https://repo.almalinux.org/almalinux/9/cloud/x86_64/images/AlmaLinux-9-GenericCloud-latest.x86_64.qcow2" ALMA_9_LOCAL_IMAGE_URL="https://www.ngtech.co.il/static/cloud-images/AlmaLinux-9-GenericCloud-latest.x86_64.qcow2" ALMA_9_TEMPLATE_ID="9002" ALMA_9_IMAGE_FILENAME="alma-9.qcow2" ALMA_9_TEMPLATE_NAME="alma-9-cloudinit-template" ALMA_9_TEMPLATE_RAM="2048" ALMA_9_TEMPLATE_CORES="2" ALMA_9_TEMPLATE_STORE="${LOCAL_STORE}" if [ "${USE_LOCAL_IMAGES}" -eq "1" ] then ALMA_9_IMAGE_URL="${ALMA_9_LOCAL_IMAGE_URL}" fi if [ -f "flags/alma-9" ] then createImage "${ALMA_9_IMAGE_URL}" "${ALMA_9_TEMPLATE_ID}" \ "${ALMA_9_IMAGE_FILENAME}" "${ALMA_9_TEMPLATE_NAME}" \ "${ALMA_9_TEMPLATE_RAM}" "${ALMA_9_TEMPLATE_CORES}" "${ALMA_9_TEMPLATE_STORE}" "alma" fi ## Ubuntu 22.04 UBUNTU_2204_IMAGE_URL="https://cloud-images.ubuntu.com/releases/22.04/release/ubuntu-22.04-server-cloudimg-amd64-disk-kvm.img" UBUNTU_2204_LOCAL_IMAGE_URL="https://www.ngtech.co.il/static/cloud-images/ubuntu-22.04-server-cloudimg-amd64-disk-kvm.img" UBUNTU_2204_TEMPLATE_ID="9003" UBUNTU_2204_IMAGE_FILENAME="ubuntu-2204.qcow2" UBUNTU_2204_TEMPLATE_NAME="ubuntu-2204-cloudinit-template" UBUNTU_2204_TEMPLATE_RAM="2048" UBUNTU_2204_TEMPLATE_CORES="2" UBUNTU_2204_TEMPLATE_STORE="${LOCAL_STORE}" if [ "${USE_LOCAL_IMAGES}" -eq "1" ] then UBUNTU_2204_IMAGE_URL="${UBUNTU_2204_LOCAL_IMAGE_URL}" fi if [ -f "flags/ubuntu-2204" ] then createImage "${UBUNTU_2204_IMAGE_URL}" "${UBUNTU_2204_TEMPLATE_ID}" \ "${UBUNTU_2204_IMAGE_FILENAME}" "${UBUNTU_2204_TEMPLATE_NAME}" \ "${UBUNTU_2204_TEMPLATE_RAM}" "${UBUNTU_2204_TEMPLATE_CORES}" "${UBUNTU_2204_TEMPLATE_STORE}" "alma" fi ## Ubuntu 24.04 UBUNTU_2404_IMAGE_URL="https://cloud-images.ubuntu.com/releases/24.04/release/ubuntu-24.04-server-cloudimg-amd64.img" UBUNTU_2404_LOCAL_IMAGE_URL="https://www.ngtech.co.il/static/cloud-images/ubuntu-24.04-server-cloudimg-amd64.img" UBUNTU_2404_TEMPLATE_ID="9004" UBUNTU_2404_IMAGE_FILENAME="ubuntu-2404.qcow2" UBUNTU_2404_TEMPLATE_NAME="ubuntu-2404-cloudinit-template" UBUNTU_2404_TEMPLATE_RAM="2048" UBUNTU_2404_TEMPLATE_CORES="2" UBUNTU_2404_TEMPLATE_STORE="${LOCAL_STORE}" if [ "${USE_LOCAL_IMAGES}" -eq "1" ] then UBUNTU_2404_IMAGE_URL="${UBUNTU_2404_LOCAL_IMAGE_URL}" fi if [ -f "flags/ubuntu-2404" ] then createImage "${UBUNTU_2404_IMAGE_URL}" "${UBUNTU_2404_TEMPLATE_ID}" \ "${UBUNTU_2404_IMAGE_FILENAME}" "${UBUNTU_2404_TEMPLATE_NAME}" \ "${UBUNTU_2404_TEMPLATE_RAM}" "${UBUNTU_2404_TEMPLATE_CORES}" "${UBUNTU_2404_TEMPLATE_STORE}" "alma" fi