Creating a Proxmox Debian cloud-init Template
Background
Finally have some time to document and publish this blog due to recent Philippines long weekend. This is the first part of the blog, as it is necessary to learn how to create template that will be used in provisioning VM and will be automatically created and deploy with Opentofu/Terraform (part 2).
What set apart a cloud-init image vs. a fully pledge image, basically it is a clone of a miminal image. A clean installed system, that can be installed on the fly, unlike the traditional image where you have to install it using GUI or manually. With cloud-init, hostname, user/password, network and ssh-keys can be set with the config file.
Table of Contents
Download the base image
On this part you can change the image to your desired distro, but for this lab we’ll be using Debian latest base image - https://cloud.debian.org/images/cloud/.
1wget https://cloud.debian.org/images/cloud/bookworm/20240717-1811/debian-12-generic-amd64-20240717-1811.qcow2
Install qemu-guest-agent
Debian cloud-init images doesn’t include qemu-guest-agent by default. To enable it we need virt-customize tool.
Install package
1apt install libguestfs-tools -y
Then install qemu-guest-agent to the image.
1virt-customize -a debian-12-generic-amd64-20240717-1811.qcow2 --install qemu-guest-agent
Create Proxmox virtual machine
Note: Value here can be changed, take note on the VM name as it will be used in part 2.
Create a VM with VMID=1002, VM name with “debian-20240717-cloudinit-template”, with basic resources (2 core, 2048Mi ram), with a virtio adapter network.
1qm create 1002 --name "debian-20240717-cloudinit-template" --memory 2048 --cores 2 --net0 virtio,bridge=vmbr0
Import the image to storage (storage pool will depends on you setup) and setting disk 0 to use the image.
1qm importdisk 1002 debian-12-generic-amd64-20240717-1811.qcow2 tags-nvme-thin-pool1
2qm set 1002 --scsihw virtio-scsi-pci --scsi0 tags-nvme-thin-pool1:vm-1002-disk-0
Set boot to disk and mounting cloudinit to ide1.
1qm set 1002 --boot c --bootdisk scsi0
2qm set 1002 --ide1 tags-nvme-thin-pool1:cloudinit
Set tty to serial0.
1qm set 1002 --serial0 socket --vga serial0
Enable qemu-guest-agent.
1qm set 1002 --agent enabled=1
Covert VM to Template
There are two ways to covert it to template.
Option 1: Using the terminal
1qm template 1002
Option 2: GUI Navigate to Proxmox gui, notice that VM 1002 is listed in the VM list. Click on the VM and click ‘More’ option and select ‘Convert to template’.
At this point you can proceed to Part 2.
To unconvert to template, navigate to dir /etc/pve/qemu-server. Set template equals to 0.
1vim /etc/pve/qemu-server/1001.conf
2
3agent: enabled=1
4boot: c
5bootdisk: scsi0
6cores: 2
7ide1: tags-nvme-thin-pool1:vm-1002-cloudinit,media=cdrom
8memory: 2048
9meta: creation-qemu=8.1.5,ctime=1724425025
10name: debian-20240717-cloudinit-template
11net0: virtio=BC:24:11:90:D0:08,bridge=vmbr0
12scsi0: tags-nvme-thin-pool1:base-1002-disk-0,size=2G
13scsihw: virtio-scsi-pci
14serial0: socket
15smbios1: uuid=1c1c60fa-62c4-426d-969e-6ebe18ca1d07
16template: 0
17vga: serial0
18vmgenid: 53e2d08a-c57f-4539-abf9-6863e2635ded
Optional Starting the VM
Let first add ssh public key, most cloud-init image has disabled user/password login.
1qm set 1002 --sshkey ~/.ssh/id_rsa.pub
Set network for the VM.
1qm set 1002 --ipconfig0 ip=192.168.254.102/24,gw=192.168.254.254
To start the VM.
1qm start 1002
To stop the VM.
1qm stop 1002
2```bash
3To destroy the VM.
4```bash
5qm destroy 1002