#!/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 --hotplug disk,usb --tablet 0
#Set networking to default bridge
qm set $1 --net0 virtio,bridge=vmbr4000,mtu=1400
#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 1024 --cores 4 --cpu host
#Set boot device to new file
qm set $1 --scsi0 ${storage}:0,import-from="$3",discard=on,ssd=1
#Set scsi hardware as default boot disk using virtio scsi single
qm set $1 --boot order=scsi0 --scsihw virtio-scsi-pci
#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" --nameserver 10.13.14.250
#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 verystrongandsecretpasswordbaby
#Add the user, not relevant for us
# 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 8G
# Custom actions
qm set $1 --cicustom "user=local:snippets/rabbitmq.yaml"
# Update cloudinit drive
qm cloudinit update $1
#Make it a template
qm template $1
#Remove file when done, prefer not yet
#rm $3
}
#Path to your ssh authorized_keys file
export ssh_keyfile=/var/lib/vz/template/cloud-images/id_rsa_pongraczi.pub
#Name of your storage
export storage=local-zfs
## Debian
#Bookworm (11) (stable) - fix your PATH!!!!
wget "https://cloud.debian.org/images/cloud/bullseye/latest/debian-11-genericcloud-amd64.qcow2"
create_template 910 "deb11gencloud" "/var/lib/vz/template/cloud-images/debian-11-genericcloud-amd64.qcow2"