Skrypt do tworzenia template
Skrypt do tworzenia template, wykorzystującego mechanizm cloud-init.
#!/usr/bin/env bash
# stop after failure
set -euo pipefail
################################ CHANGE THAT SECTION ################################
template_name="ubuntu-24.04-template"
template_id="99001"
cloudimg_link="https://cloud-images.ubuntu.com/noble/current/noble-server-cloudimg-amd64.img"
shared_storage_name="local-lvm"
#####################################################################################
# Create VM with 2GB RAM, 2 cores without network interface
qm create "$template_id" \
--name "$template_name" \
--memory 2048 \
--cores 2 \
--agent enabled=1 \
--serial0 socket
# Download image
wget -O template_file.img "$cloudimg_link"
# Convert `.img` to `.qcow2`
# Do not do this for Ubuntu cloud images!
# qemu-img convert -f raw -O qcow2 template_file.img template_file.qcow2
# Increase disk size
qemu-img resize template_file.qcow2 32G
# Import disk to VM
qm importdisk "$template_id" template_file.qcow2 "$shared_storage_name"
# Mount imported disk to VM
qm set "$template_id" \
--scsihw virtio-scsi-pci \
--scsi0 "${shared_storage_name}":vm-"${template_id}"-disk-0
# Create Cloud-Init disk
qm set "$template_id" \
--ide2 "${shared_storage_name}":cloudinit
# Set boot order from imported disk
qm set "$template_id" --boot order=scsi0
# Remove old QCOW image
rm template_file.qcow2 template_file.img
# Convert VM to Template
qm template "$template_id"