Skip to content

Instantly share code, notes, and snippets.

@ironwolphern
Last active December 26, 2024 22:17
Show Gist options
  • Save ironwolphern/9c2c5841a1382a3fc28fdc57408dd4a0 to your computer and use it in GitHub Desktop.
Save ironwolphern/9c2c5841a1382a3fc28fdc57408dd4a0 to your computer and use it in GitHub Desktop.
This script will download a cloud image and deploy it as a template in Proxmox VE
#!/bin/bash
# This script will download a cloud image and deploy it as a template in Proxmox VE
# Tested on Proxmox VE 8.2.4
clear
# Default values for cloud template
MEMORY="512"
DISK_SIZE="10G"
STORAGE="local-lvm"
NET_IFACE="vmbr0"
# Menu to select cloud template to deploy
printf "Select an image linux to deploy how a cloud template in Proxmox:\n"
PS3="Select an option [1-9]: "
OPTIONS=("Ubuntu Server Minimal 24.04 LTS" "Debian 12" "CentOS Stream 9" "Fedora Server 40" "Arch Linux 2024.08.01" "OpenSUSE Leap Micro 6.0" "Alpine Linux 3.20.2" "Flatcar 3975.2.0" "Quit")
select opt in "${OPTIONS[@]}"; do
case "${opt}" in
"Ubuntu Server Minimal 24.04 LTS")
printf "\nYou selected Ubuntu Server Minimal 24.04 LTS\n"
printf "Downloading cloud image...\n"
# https://cloud-images.ubuntu.com/minimal/releases/noble/release/SHA256SUMS
IMAGE="https://cloud-images.ubuntu.com/minimal/releases/noble/release/ubuntu-24.04-minimal-cloudimg-amd64.img"
DST="ubuntu-24.04-minimal-cloudimg-amd64.img"
ID_VM=9000
VM_NAME="ubuntu-server-minimal-24.04"
break
;;
"Debian 12")
printf "\nYou selected Debian 12\n"
printf "Downloading cloud image...\n"
# https://cloud.debian.org/images/cloud/bookworm/latest/SHA512SUMS
IMAGE="https://cloud.debian.org/images/cloud/bookworm/latest/debian-12-nocloud-amd64.qcow2"
DST="debian-12-nocloud-amd64.qcow2"
ID_VM=9001
VM_NAME="debian-12"
break
;;
"CentOS Stream 9")
printf "\nYou selected CentOS Stream 9\n"
printf "Downloading cloud image...\n"
# https://cloud.centos.org/centos/9-stream/x86_64/images/CentOS-Stream-GenericCloud-9-latest.x86_64.qcow2.SHA256SUM
IMAGE="https://cloud.centos.org/centos/9-stream/x86_64/images/CentOS-Stream-GenericCloud-9-latest.x86_64.qcow2"
DST="CentOS-Stream-GenericCloud-9-latest.x86_64.qcow2"
ID_VM=9002
VM_NAME="centos-Stream-9"
break
;;
"Fedora Server 40")
printf "\nYou selected Fedora Server 40\n"
printf "Downloading cloud image...\n"
# https://download.fedoraproject.org/pub/fedora/linux/releases/40/Cloud/x86_64/images/Fedora-Cloud-40-1.14-x86_64-CHECKSUM
IMAGE="https://download.fedoraproject.org/pub/fedora/linux/releases/40/Cloud/x86_64/images/Fedora-Cloud-Base-Generic.x86_64-40-1.14.qcow2"
DST="Fedora-Cloud-Base-Generic.x86_64-40-1.14.qcow2"
ID_VM=9003
VM_NAME="fedora-server-40"
break
;;
"Arch Linux 2024.08.01")
printf "\nYou selected Arch Linux 2024.08.01\n"
printf "Downloading cloud image...\n"
# https://geo.mirror.pkgbuild.com/images/latest/Arch-Linux-x86_64-cloudimg.qcow2.SHA256
IMAGE="https://geo.mirror.pkgbuild.com/images/latest/Arch-Linux-x86_64-cloudimg.qcow2"
DST="Arch-Linux-x86_64-cloudimg.qcow2"
ID_VM=9004
VM_NAME="arch-linux-2024.08.01"
break
;;
"OpenSUSE Leap Micro 6.0")
printf "\nYou selected OpenSUSE Leap Micro 6.0\n"
printf "Downloading cloud image...\n"
# https://download.opensuse.org/distribution/leap-micro/6.0/appliances/openSUSE-Leap-Micro.x86_64-Default-qcow.qcow2.sha256
IMAGE="https://download.opensuse.org/distribution/leap-micro/6.0/appliances/openSUSE-Leap-Micro.x86_64-Default-qcow.qcow2"
DST="openSUSE-Leap-Micro.x86_64-Default-qcow.qcow2"
ID_VM=9005
VM_NAME="opensuse-leap-micro-6.0"
break
;;
"Alpine Linux 3.20.2")
printf "\nYou selected Alpine Linux 3.20.2\n"
printf "Downloading cloud image...\n"
# https://dl-cdn.alpinelinux.org/alpine/v3.20/releases/cloud/nocloud_alpine-3.20.2-x86_64-bios-cloudinit-r0.asc
IMAGE="https://dl-cdn.alpinelinux.org/alpine/v3.20/releases/cloud/nocloud_alpine-3.20.2-x86_64-bios-cloudinit-r0.qcow2"
DST="nocloud_alpine-3.20.2-x86_64-bios-cloudinit-r0.qcow2"
ID_VM=9006
VM_NAME="alpine-linux-3.20.2"
break
;;
"Flatcar 3975.2.0")
printf "\nYou selected Flatcar 3975.2.0\n"
printf "Downloading cloud image...\n"
# https://stable.release.flatcar-linux.net/amd64-usr/current/flatcar_production_qemu_image.img.DIGESTS
IMAGE="https://stable.release.flatcar-linux.net/amd64-usr/current/flatcar_production_qemu_image.img"
DST="flatcar_production_qemu_image.img"
ID_VM=9007
VM_NAME="flatcar-3975.2.0"
break
;;
"Quit")
exit 0
;;
*)
printf "\nInvalid option, try again.\n"
;;
esac
done
# Download image
wget -O "${DST}" "${IMAGE}"
printf "Uploading cloud image to Proxmox...\n"
# Create a VM with the cloud image and convert to template
qm create "${ID_VM}" --name "${VM_NAME}" --memory "${MEMORY}" --net0 virtio,bridge="${NET_IFACE}" --scsihw virtio-scsi-pci
# Set agent and cpu parameters
qm set "${ID_VM}" --agent enabled=1,fstrim_cloned_disks=1 --cpu cputype=host
# Import cloud image to VM
qm set "${ID_VM}" --scsi0 "${STORAGE}":0,import-from="${PWD}"/"${DST}"
# Set cloud-init parameters
qm set "${ID_VM}" --ide2 "${STORAGE}":cloudinit
qm set "${ID_VM}" --boot order=scsi0
qm set "${ID_VM}" --serial0 socket --vga serial0
# Set network parameters
qm set "${ID_VM}" --ipconfig0 ip=dhcp
# Set disk size
qm disk resize "${ID_VM}" scsi0 "${DISK_SIZE}"
printf "Creating cloud template in Proxmox...\n"
# Convert VM to template
qm template "${ID_VM}"
printf "Cleaning up...\n"
# Remove downloaded image
rm -Rf "${DST}"
printf "Done!\n"
# Then you can create a VM from this templates with terraform and cloud-init
############
# Reference:
# https://pve.proxmox.com/wiki/Cloud-Init_Support
# https://pve.proxmox.com/pve-docs/chapter-qm.html#qm_cloud_init
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment