Assumption: you have a running proxmox server
I have shell script to create a promox template from a cloud image of choice. You have a choice either to use the shell script or run the command manually described in the shell script. By the way, there's a manual process and steps to create the vm template, but those are like too many steps in the proxmox UI.
Additionally, our objective is to automate everything :)
I will give credit to the original author, from whom this code is borrowed and then modified, once i find him/her :)
Steps
Copy the create_vm_template.sh
shell script to your proxmox server and run it.
Note: This should create a vm template named temp-debian-12
. If you want to download other cloud images, feel free to uncomment the relevant section and run the code.
Feel free to copy and modify this script to your need.
Directly download the script on your machine and run it or manually copy/modify the script to suit your need.
wget -O create_vm_template.sh https://raw.githubusercontent.com/aruntomar/homelab/main/proxmox/scripts/create_vm_template.sh
create_vm_template.sh
#!/bin/bash
#Create template
#args:
# vm_id
# vm_name
# file name in the current directory
function create_template() {
#Print all of the configuration
echo "Creating template $2 ($1)"
#Create new VM
#Feel free to change any of these to your liking
qm create $1 --name $2 --ostype l26
#Set networking to default bridge
qm set $1 --net0 virtio,bridge=vmbr0
#Set display to serial
#qm set $1 --serial0 socket --vga serial0
#Set memory, cpu, type defaults
#If you are in a cluster, you might need to change cpu type
qm set $1 --memory 2048 --cores 2 --cpu host
#Set boot device to new file
qm set $1 --scsi0 ${storage}:0,import-from="$(pwd)/$3",discard=on
#Set scsi hardware as default boot disk using virtio scsi single
qm set $1 --boot order=scsi0 --scsihw virtio-scsi-single
#Set bootdisk
qm set $1 --boot c --bootdisk scsi0
#Enable Qemu guest agent in case the guest has it available
qm set $1 --agent enabled=1,fstrim_cloned_disks=1
#Add cloud-init device
qm set $1 --ide2 ${storage}:cloudinit
#Set CI ip config
#IP6 = auto means SLAAC (a reliable default with no bad effects on non-IPv6 networks)
#IP = DHCP means what it says, so leave that out entirely on non-IPv4 networks to avoid DHCP delays
#qm set $1 --ipconfig0 "ip=dhcp"
#Import the ssh keyfile
#qm set $1 --sshkeys ${ssh_keyfile}
#If you want to do password-based auth instaed
#Then use this option and comment out the line above
#qm set $1 --cipassword password
#Add the user
#qm set $1 --ciuser ${username}
#qm set $1 --cicustom "user=snippets:snippets/cloud-init.yaml"
#Resize the disk to 8G, a reasonable minimum. You can expand it more later.
#If the disk is already bigger than 8G, this will fail, and that is okay.
#qm resize $1 scsi0 10G
#Make it a template
qm template $1
#Remove file when done
#rm $3
}
#Path to your ssh authorized_keys file
#Alternatively, use /etc/pve/priv/authorized_keys if you are already authorized
#on the Proxmox system
#export ssh_keyfile=/root/key.pub
#Username to create on VM template
#export username=<username>
#Name of your storage
export storage=local-lvm
#The images that I've found premade
#Feel free to add your own
## Debian
#Buster (10)
#wget "https://cloud.debian.org/images/cloud/buster/latest/debian-10-genericcloud-amd64.qcow2"
#create_template 900 "temp-debian-10" "debian-10-genericcloud-amd64.qcow2"
#Bullseye (11)
#wget "https://cloud.debian.org/images/cloud/bullseye/latest/debian-11-genericcloud-amd64.qcow2"
#create_template 901 "temp-debian-11" "debian-11-genericcloud-amd64.qcow2"
#Bookworm (12)
wget -c "https://cloud.debian.org/images/cloud/bookworm/latest/debian-12-genericcloud-amd64.qcow2"
create_template 9000 "temp-debian-12" "debian-12-genericcloud-amd64.qcow2"
## Ubuntu
#20.04 (Focal Fossa)
#wget "https://cloud-images.ubuntu.com/releases/focal/release/ubuntu-20.04-server-cloudimg-amd64.img"
#create_template 910 "temp-ubuntu-20-04" "ubuntu-20.04-server-cloudimg-amd64.img"
#22.04 (Jammy Jellyfish)
#wget -c "https://cloud-images.ubuntu.com/releases/22.04/release/ubuntu-22.04-server-cloudimg-amd64.img"
#create_template 8000 "temp-ubuntu-22-04" "ubuntu-22.04-server-cloudimg-amd64.img"
#23.04 (Lunar Lobster) - daily builds
#wget "https://cloud-images.ubuntu.com/lunar/current/lunar-server-cloudimg-amd64.img"
#create_template 912 "temp-ubuntu-23-04-daily" "lunar-server-cloudimg-amd64.img"
## Fedora 37
#Image is compressed, so need to uncompress first
#wget https://download.fedoraproject.org/pub/fedora/linux/releases/37/Cloud/x86_64/images/Fedora-Cloud-Base-37-1.7.x86_64.raw.xz
#xz -d -v Fedora-Cloud-Base-37-1.7.x86_64.raw.xz
#create_template 920 "temp-fedora-37" "Fedora-Cloud-Base-37-1.7.x86_64.raw"
## CentOS Stream
#Stream 8
#wget https://cloud.centos.org/centos/8-stream/x86_64/images/CentOS-Stream-GenericCloud-8-20220913.0.x86_64.qcow2
#create_template 930 "temp-centos-8-stream" "CentOS-Stream-GenericCloud-8-20220913.0.x86_64.qcow2"
#Stream 9 (daily) - they don't have a 'latest' link?
#wget https://cloud.centos.org/centos/9-stream/x86_64/images/CentOS-Stream-GenericCloud-9-20230123.0.x86_64.qcow2
#create_template 931 "temp-centos-9-stream-daily" "CentOS-Stream-GenericCloud-9-20230123.0.x86_64.qcow2"