Proxmox Cloud Template Deployment Guide
This guide explains a Bash script designed to automate the process of downloading cloud images and deploying them as templates in Proxmox Virtual Environment (PVE). The script supports multiple Linux distributions and handles the entire process from downloading the image to creating a template in Proxmox.
Overview
The script performs the following main tasks:
- Presents a menu to select a Linux distribution
- Downloads the appropriate cloud image
- Creates a VM in Proxmox with specific configurations
- Imports the cloud image and configures cloud-init
- Converts the VM to a template
- Cleans up temporary files
Here’s the fully documented script with detailed explanations:
#!/bin/bash
# Purpose: This script automates the deployment of cloud images as templates in Proxmox VE
# Tested on: Proxmox VE 8.2.4
# Usage: Run the script with bash, select an option from the menu, and the script handles the rest
# Clear the terminal for better visibility
clear
# Default configuration values for the VM template
# These can be modified to match your environment's requirements
MEMORY="512" # VM memory in MB
DISK_SIZE="10G" # Virtual disk size
STORAGE="local-lvm" # Proxmox storage location
NET_IFACE="vmbr0" # Network bridge interface
# Display menu header
printf "Select an image linux to deploy how a cloud template in Proxmox:\n"
PS3="Select an option [1-9]: " # Set the prompt string for select command
# Define available Linux distributions
# Each option includes specific image URL and configuration settings
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"
)
# Menu selection logic using bash select
select opt in "${OPTIONS[@]}"; do
case "${opt}" in
"Ubuntu Server Minimal 24.04 LTS")
# Configuration for Ubuntu Server
printf "\nYou selected Ubuntu Server Minimal 24.04 LTS\n"
printf "Downloading cloud image...\n"
# Image URL and checksum can be verified at:
# 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 # Unique ID for the VM in Proxmox
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 the selected cloud image
# wget is used with -O to specify the output filename
wget -O "${DST}" "${IMAGE}"
printf "Uploading cloud image to Proxmox...\n"
# VM Creation and Configuration Section
# Create the base VM with minimal configuration
qm create "${ID_VM}" --name "${VM_NAME}" --memory "${MEMORY}" \
--net0 virtio,bridge="${NET_IFACE}" --scsihw virtio-scsi-pci
# Configure VM hardware and features
# Enable QEMU guest agent and CPU optimization
qm set "${ID_VM}" --agent enabled=1,fstrim_cloned_disks=1 --cpu cputype=host
# Import the downloaded cloud image
# This attaches the image to the VM's first SCSI drive
qm set "${ID_VM}" --scsi0 "${STORAGE}":0,import-from="${PWD}"/"${DST}"
# Configure cloud-init drive and boot settings
# cloud-init is used for initial VM configuration
qm set "${ID_VM}" --ide2 "${STORAGE}":cloudinit # Attach cloud-init drive
qm set "${ID_VM}" --boot order=scsi0 # Set boot order
qm set "${ID_VM}" --serial0 socket --vga serial0 # Configure serial console
# Configure network settings
# Set to DHCP by default - can be modified for static IP
qm set "${ID_VM}" --ipconfig0 ip=dhcp
# Resize the disk to specified size
# This ensures the template has the desired disk space
qm disk resize "${ID_VM}" scsi0 "${DISK_SIZE}"
printf "Creating cloud template in Proxmox...\n"
# Convert the configured VM to a template
# This allows for creating multiple VMs from this base configuration
qm template "${ID_VM}"
printf "Cleaning up...\n"
# Clean up by removing the downloaded image
rm -Rf "${DST}"
printf "Done!\n"
Key Components Explained
1. Default Configuration
The script starts by setting default values for the VM template:
- Memory: 512MB (minimal for most distributions)
- Disk size: 10GB (can be increased based on needs)
- Storage: local-lvm (Proxmox’s default storage)
- Network interface: vmbr0 (default bridge interface)
2. Distribution Selection
The script provides a menu for selecting different Linux distributions. Each option includes:
- Distribution name and version
- Cloud image URL
- Unique VM ID (starting from 9000)
- VM name for Proxmox
3. VM Creation Process
The script uses several qm
commands to set up the VM:
qm create
: Creates the base VM with network and memory settingsqm set
: Configures various VM parameters including:- QEMU guest agent
- CPU type
- Storage configuration
- Boot order
- Serial console
- Network settings
4. Cloud-Init Integration
The script configures cloud-init support, which allows for:
- Automatic network configuration
- SSH key deployment
- User creation
- Custom scripts execution on first boot
5. Template Creation
After configuration, the VM is converted to a template using qm template
. This template can then be used to create multiple VMs with the same base configuration.
Usage Instructions
- Save the script to a file (e.g.,
deploy-cloud-template.sh
) - Make it executable:
chmod +x deploy-cloud-template.sh
- Run the script:
./deploy-cloud-template.sh
- Select a distribution from the menu
- Wait for the process to complete
Post-Deployment
After creating the template, you can:
- Create new VMs from the template using the Proxmox web interface
- Use Terraform to automate VM creation from the template
- Customize cloud-init settings for each new VM
Important Notes
- Ensure you have sufficient storage space in your Proxmox storage
- Verify network connectivity for downloading images
- Check that the Proxmox storage and network interface names match your environment
- Consider increasing memory and disk size for production use
- Regularly update templates to include security patches
References
- Proxmox Cloud-Init Support: Cloud-Init Support - Proxmox VE
- Proxmox QM Documentation: QEMU/KVM Virtual Machines