Proxmox Cloud-Init
July 27, 2024
Cloud-Init offers a standardised way for users to rapidly stand up VMs, and is supported by many Linux operating systems.
It is often used behind the scenes by cloud providers, but can also be used locally by the end user - and can be a useful building block in larger automation projects.
Using Cloud-Init with Proxmox
To use cloud-init on Proxmox, you need to download the cloud-init image of the OS you wish to use. Customisation of the image can save effort if the image is to be repeatedly re-used.
It is recommended to:
- Set up libguestfs on the Proxmox host where you are customising the cloud-init image.
- Use the virtcustomise command (installed with libguestfs) to install the qemu-guest-agent package for guest integration.
- Set up snippets on some local storage to allow use of the cicustom configuration options if desired.
You will need to:
- Create a new VM (eg VM9000).
- Import the downloaded/customised cloud-init image to the VM.
- Set the boot and display options, including the boot disk.
- Set the cicustom options if desired.
- Convert the VM into a template.
With the template customised, you should now be able to clone it to a new VM and start it up with the desired options and packages pre-configured.
Example commands:
# download the image
wget https://cloud-images.ubuntu.com/oracular/current/oracular-server-cloudimg-amd64.img
# download libguestfs for the virt-customise tool
apt update -y && apt install libguestfs-tools -y
# configure the image with qemu-guest-agent using virt customise
virt-customize -a oracular-server-cloudimg-amd64.img --install qemu-guest-agent
# create a new VM with VirtIO SCSI controller
qm create 9000 --memory 2048 --net0 virtio,bridge=vmbr0 --scsihw virtio-scsi-pci
# import the downloaded disk to the local-lvm storage, attaching it as a SCSI drive
# note that import path must be a full path, not relative
qm set 9000 --scsi0 local-lvm:0,import-from=/root/oracular-server-cloudimg-amd64.img
# set boot and display options
qm set 9000 --ide2 local-lvm:cloudinit
qm set 9000 --serial0 socket --vga serial0
# very important - had to be updated in the image
qm set 9000 --boot c --bootdisk scsi0
# optional - cloud-init customisation options
qm cloudinit dump 9000 user > custom.yml
# note - move the custom file to an accessible snippet location
mv custom.yml /snip/snippets
# set the template to use the custom config
qm set 9000 --cicustom "user=snip:/snippets/custom.yml"
# turn the VM into a template
qm template 9000
Example cloud-init custom file:
#cloud-config
hostname: ubuntucustom
manage_etc_hosts: true
fqdn: ubuntucustom
chpasswd:
expire: False
users:
- default
user: user
password: $5$JACHLoFl$.IoaEQthA6o5CumfvNdEVIA/cMhohA.l0b/w82t.6.4
package_upgrade: true
package_reboot_if_required: true
packages:
- apache2
- cowsay
- qemu-guest-agent
runcmd:
- systemctl start qemu-guest-agent
- systemctl enable qemu-guest-agent
- echo "job done" >> /home/user/testing.txt
A video demonstration of the setup can be found here: