Benoit J - My mostly tech blog

Rebuilding My Server - Proxmox Cloud Init Image template

Published on 2024-08-28

Categories: tech
Tags: selfhosting virtualization

On my previous post, I mentioned migrating my server's VM platform to Proxmox. Here I'm describing how I got cloud init templates to quickly provision Linux Ubuntu VMs without requiring to go through an GUI installation process.

Cloud init

According to cloud-init.io, Cloud init is:

Cloud images are operating system templates and every instance starts out as an identical clone of every other instance. It is the user data that gives every cloud instance its personality and cloud-init is the tool that applies user data to your instances automatically.

Okay…

Maybe if I explain how I create a VM from a cloud-init image:

  1. Clone the cloud-init Ubuntu server template
  2. In Proxmox UI, supply key information like name, IP/network
  3. Enlarge the main disk and tweak resources allocated
  4. Start the VM

No lengthy installation from ISO, no additional OS specific GUI. All cloud-init capable OS are configured the same way.

A starting point

TechnoTim has a really good YT video and blog post describing the initial cloud-init template creation for Ubuntu cloud image and Proxmox.

This is a good starting point for my own needs, and here is what I did in addition to the instructions described in the blog post.

my setup

Data logical drives are usually local and local-lvm, mines are files and vm.

During template creation

Right before the qm template command.

set user and ssh key

I never log as root. Always as a specific user, my-user in this case.

1
qm set 8000 --ciuser my-user

Since I never set a password on these servers, I need to connect using ssh keys.

1
qm set 8000 --sshkeys ~/my-sshkey.pub

Configure the qemu-agent for better Proxmox integration

The qemu-agent allows to get more detailed information about the VM, like IP address, OS, and also allows better control over it’s running state.

Proxmox does not support adding packages from their UI, but since this is a standard cloud-init “protocol”, we can override some of the cloud-init configuration files to achieve that.

Since Proxmox use meta, user, network cloud init files, and we can’t change these without breaking the UI, this leaves us the vendor configuration to override.

Here /pvepool/files matches the files logical storage defined in Proxmox.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17

cat > /pvepool/files/snippets/civendor-agent.yaml <<EOT
#cloud-config

packages:
  - qemu-guest-agent

# making sure qemu-guest-agent is running
runcmd:
    - systemctl start qemu-guest-agent

timezone: "America/Toronto"
EOT

qm set 8000 --cicustom vendor=files:snippets/civendor-agent.yaml

qm set 8000 --agent enabled=1,freeze-fs-on-backup=1,fstrim_cloned_disks=1

Creating a new VM

Since I prefer CLI as it’s more reproducible, I captured the commands I used to provision my VMs.

The same operations can be done in the Proxmox UI though.

Clone the template

1
qm clone 8000 <some free id number> --name <some name> --full

Allocate more CPU/RAM

1
qm set <vm id> --sockets <nb cpu> --cores <nb cores> --memory <nb megs>

Enlarge the hard drive

The drive created is too small, extend it based on needs

1
qm resize <vm id> scsi0 <nb gig>G

Set proper nic

In my setup, I have vlan, which are separate vmbr0.<vlan> network interface.

1
qm set <vm id> --net0 virtio,bridge=vmbr0,tag=<vlan>

Configure the network

1
qm set <vm id> --ipconfig0 ip=<ip>/<netmask>,gw=<ip gw>

craftering

For comments, use email or Mastodon

Don't forget to subscribe to my RSS feed!