Skip to content

Instantly share code, notes, and snippets.

@krakazyabra
Created July 10, 2024 12:56
Show Gist options
  • Select an option

  • Save krakazyabra/a76e231a3361195dc7787d4871a76431 to your computer and use it in GitHub Desktop.

Select an option

Save krakazyabra/a76e231a3361195dc7787d4871a76431 to your computer and use it in GitHub Desktop.
Proxmox create VM templates
#!/bin/bash
#Create template
#args:
# vm_id
# vm_name
# file name in the current directory
function template_exists() {
qm list | grep -w $1 &> /dev/null
}
function delete_template() {
qm destroy $1
}
function create_template() {
#Check if template exists and delete it
if template_exists $1; then
echo "Destroy existing template $1"
delete_template $1
fi
#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 qxl
#Set memory, cpu, type defaults
#If you are in a cluster, you might need to change cpu type
qm set $1 --memory 1024 --cores 1
#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-pci --onboot 1
#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 "ip6=auto,ip=dhcp" --nameserver "8.8.8.8"
#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}
#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 disk resize $1 scsi0 4G
#Add more args
qm set $1 --machine q35 --searchdomain "domain.org" --tags "vm-template"
#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/.ssh/authorized_keys
#Username to create on VM template
export username=root
#Name of your storage
export storage=local-zfs
#The images that I've found premade
#Feel free to add your own
## Debian
#Buster (10) (really old at this point)
wget "https://cloud.debian.org/images/cloud/buster/latest/debian-10-genericcloud-amd64.qcow2"
create_template 900 "cloud-debian-10" "debian-10-genericcloud-amd64.qcow2"
#Bullseye (11) (oldstable)
wget "https://cloud.debian.org/images/cloud/bullseye/latest/debian-11-genericcloud-amd64.qcow2"
create_template 901 "cloud-debian-11" "debian-11-genericcloud-amd64.qcow2"
#Bookworm (12) (stable)
wget "https://cloud.debian.org/images/cloud/bookworm/latest/debian-12-genericcloud-amd64.qcow2"
create_template 902 "cloud-debian-12" "debian-12-genericcloud-amd64.qcow2"
#Trixie (13) (testing) dailies
wget "https://cloud.debian.org/images/cloud/trixie/daily/latest/debian-13-genericcloud-amd64-daily.qcow2"
create_template 903 "cloud-debian-13-daily" "debian-13-genericcloud-amd64-daily.qcow2"
#Sid (unstable)
wget "https://cloud.debian.org/images/cloud/sid/daily/latest/debian-sid-genericcloud-amd64-daily.qcow2"
create_template 909 "cloud-debian-sid" "debian-sid-genericcloud-amd64-daily.qcow2"
## Ubuntu
#20.04 (Focal Fossa) LTS
wget "https://cloud-images.ubuntu.com/releases/focal/release/ubuntu-20.04-server-cloudimg-amd64.img"
create_template 910 "cloud-ubuntu-20-04" "ubuntu-20.04-server-cloudimg-amd64.img"
#22.04 (Jammy Jellyfish) LTS
wget "https://cloud-images.ubuntu.com/releases/22.04/release/ubuntu-22.04-server-cloudimg-amd64.img"
create_template 911 "cloud-ubuntu-22-04" "ubuntu-22.04-server-cloudimg-amd64.img"
#23.10 (Manic Minotaur)
wget "https://cloud-images.ubuntu.com/releases/23.10/release/ubuntu-23.10-server-cloudimg-amd64.img"
create_template 912 "cloud-ubuntu-23-10" "ubuntu-23.10-server-cloudimg-amd64.img"
#As 23.10 has *just released*, the next LTS (24.04) is not in dailies yet
#24.04 (Noble Numbat) LTS
wget "https://cloud-images.ubuntu.com/releases/24.04/release/ubuntu-24.04-server-cloudimg-amd64.img"
create_template 913 "cloud-ubuntu-24-04" "ubuntu-24.04-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 "cloud-fedora-37" "Fedora-Cloud-Base-37-1.7.x86_64.raw"
## Fedora 38
wget "https://download.fedoraproject.org/pub/fedora/linux/releases/38/Cloud/x86_64/images/Fedora-Cloud-Base-38-1.6.x86_64.raw.xz"
xz -d -v Fedora-Cloud-Base-38-1.6.x86_64.raw.xz
create_template 921 "cloud-fedora-38" "Fedora-Cloud-Base-38-1.6.x86_64.raw"
## Fedora 39
wget "https://download.fedoraproject.org/pub/fedora/linux/releases/39/Cloud/x86_64/images/Fedora-Cloud-Base-39-1.5.x86_64.qcow2"
create_template 922 "cloud-fedora-39" "Fedora-Cloud-Base-Generic.x86_64-39-1.5.qcow2"
## Fedora 40
wget "https://download.fedoraproject.org/pub/fedora/linux/releases/40/Cloud/x86_64/images/Fedora-Cloud-Base-Generic.x86_64-40-1.14.qcow2"
create_template 923 "cloud-fedora-40" "Fedora-Cloud-Base-Generic.x86_64-40-1.14.qcow2"
## Rocky Linux
#Rocky 8 latest
wget "http://dl.rockylinux.org/pub/rocky/8/images/x86_64/Rocky-8-GenericCloud.latest.x86_64.qcow2"
create_template 930 "cloud-rocky-8" "Rocky-8-GenericCloud.latest.x86_64.qcow2"
#Rocky 9 latest
wget "http://dl.rockylinux.org/pub/rocky/9/images/x86_64/Rocky-9-GenericCloud.latest.x86_64.qcow2"
create_template 931 "cloud-rocky-9" "Rocky-9-GenericCloud.latest.x86_64.qcow2"
## Alpine Linux
#Alpine 3.19.1
wget "https://dl-cdn.alpinelinux.org/alpine/v3.19/releases/cloud/nocloud_alpine-3.19.1-x86_64-bios-cloudinit-r0.qcow2"
create_template 940 "cloud-alpine-3.19" "nocloud_alpine-3.19.1-x86_64-bios-cloudinit-r0.qcow2"
## OpenSUSE
# Tumbleweed
wget "https://download.opensuse.org/tumbleweed/appliances/openSUSE-Tumbleweed-JeOS.x86_64-kvm-and-xen.qcow2"
create_template 950 "cloud-opensuse-tumbleweed" "openSUSE-Tumbleweed-JeOS.x86_64-kvm-and-xen.qcow2"
# Leap
wget "https://download.opensuse.org/distribution/leap/15.3/appliances/openSUSE-Leap-15.3-JeOS.x86_64-kvm-and-xen.qcow2"
create_template 951 "cloud-opensuse-leap" "openSUSE-Leap-15.3-JeOS.x86_64-kvm-and-xen.qcow2"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment