proxmox-scripts/create-proxmox-cloudinit-images.sh

225 lines
6.9 KiB
Bash
Raw Permalink Normal View History

2024-01-20 21:58:38 +02:00
#!/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"
2024-01-21 04:01:03 +02:00
SSH_KEYS_FILE="keys"
2024-01-20 21:58:38 +02:00
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*)
2024-01-21 03:52:19 +02:00
virt-customize -v -a ${IMAGE_FILE} --install qemu-guest-agent
2024-01-20 21:58:38 +02:00
;;
alma*|rhel*|centos*)
2024-01-21 03:52:19 +02:00
virt-customize -v -a ${IMAGE_FILE} --commands-from-file
virt-customize -v -a ${IMAGE_FILE} --install qemu-guest-agent
2024-01-20 21:58:38 +02:00
;;
*)
echo "Unknown OS type"
;;
esac
fi
2024-01-21 03:52:19 +02:00
case "${IMAGE_TYPE}" in
deb*|ubuntu*)
DISPLAY_TYPE="serial"
;;
alma*|rhel*|centos*)
DISPLAY_TYPE="default"
;;
*)
DISPLAY_TYPE="default"
echo "Unknown OS type"
;;
esac
2024-01-20 21:58:38 +02:00
qm create ${IMAGE_ID} --name "${IMAGE_NAME}" --memory ${IMAGE_RAM} --cores ${IMAGE_CORES} \
2024-01-21 03:52:19 +02:00
--net0 virtio,bridge=vmbr0
2024-01-27 18:47:00 +02:00
qm importdisk ${IMAGE_ID} ${IMAGE_FILE} ${IMAGE_STORE} --format qcow2
2024-05-05 05:27:58 +03:00
qm set ${IMAGE_ID} --cpu cputype=x86-64-v2-AES
2024-01-21 02:44:45 +02:00
qm set ${IMAGE_ID} --scsihw virtio-scsi-pci --scsi0 ${IMAGE_STORE}:vm-${IMAGE_ID}-disk-0
2024-01-27 18:47:00 +02:00
# qm resize ${IMAGE_ID} virtio0 +15G
2024-01-20 21:58:38 +02:00
qm set ${IMAGE_ID} --boot c --bootdisk scsi0
qm set ${IMAGE_ID} --ide2 ${IMAGE_STORE}:cloudinit
2024-01-21 03:52:19 +02:00
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
2024-01-21 04:01:03 +02:00
qm set ${IMAGE_ID} --ipconfig0 ip=dhcp
2024-01-20 21:58:38 +02:00
qm set ${IMAGE_ID} --agent enabled=1
2024-01-21 04:01:03 +02:00
qm set ${IMAGE_ID} --sshkeys ${SSH_KEYS_FILE}
2024-01-27 18:47:00 +02:00
# qm template ${IMAGE_ID}
2024-01-20 21:58:38 +02:00
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
2024-01-21 03:52:19 +02:00
if [ -f "flags/debian-12" ]
then
2024-01-20 21:58:38 +02:00
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"
2024-01-21 03:52:19 +02:00
fi
2024-01-20 21:58:38 +02:00
## 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
2024-01-21 03:52:19 +02:00
if [ -f "flags/alma-8" ]
then
2024-01-20 21:58:38 +02:00
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"
2024-01-21 03:52:19 +02:00
fi
2024-01-20 21:58:38 +02:00
## 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
2024-01-21 03:52:19 +02:00
if [ -f "flags/alma-9" ]
then
2024-01-20 21:58:38 +02:00
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"
2024-01-21 03:52:19 +02:00
fi
2024-01-20 21:58:38 +02:00
## 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
2024-01-21 03:52:19 +02:00
if [ -f "flags/ubuntu-2204" ]
then
2024-01-20 21:58:38 +02:00
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"
2024-01-21 03:52:19 +02:00
fi
2024-01-20 21:58:38 +02:00
2024-05-05 05:27:58 +03:00
## 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