Proxmox Template with Cloud Image and Cloud Init

marksie1988

Steven Marks

Posted on June 3, 2023

Proxmox Template with Cloud Image and Cloud Init

Using Cloud images and Cloud init with Proxmox is the quickest, most efficient way to deploy servers at this time. Cloud images are small cloud certified that have Cloud init pre-installed and ready to accept configuration.

Cloud images and Cloud init also work with Proxmox and if you combine this with Terraform you have a fully automated deployment model.

Guide

Download image

First you will need to choose an Ubuntu Cloud Image

Rather than downloading this, copy the URL.

Then SSH into your Proxmox server and run wget with the URL you just copied, similar to below:

wget https://cloud-images.ubuntu.com/jammy/current/jammy-server-cloudimg-amd64.img
Enter fullscreen mode Exit fullscreen mode

This will download the image onto your proxmox server ready for use.

Install packages

The qemu-guest-agent is not installed on the cloud-images, so we need a way to inject that into out image file. This can be done with a great tool called virt-customize this is installed with the package libguestfs-tools. libguestfs is a set of tools for accessing and modifying virtual machine (VM) disk images.

Install:

sudo apt update -y && sudo apt install libguestfs-tools -y
Enter fullscreen mode Exit fullscreen mode

Install qemu-guest-agent into the downloaded image:

sudo virt-customize -a jammy-server-cloudimg-amd64.img --install qemu-guest-agent --run-command 'systemctl enable qemu-guest-agent.service'
Enter fullscreen mode Exit fullscreen mode

You can also install other packages at this point.

Adding users to the image (Optional)

It is possible to also add a user and SSH keys with the virt-customize. This is useful for automation such as terraform.

sudo virt-customize -a jammy-server-cloudimg-amd64.img --run-command 'useradd simone'
sudo virt-customize -a jammy-server-cloudimg-amd64.img --run-command 'mkdir -p /home/simone/.ssh'
sudo virt-customize -a jammy-server-cloudimg-amd64.img --ssh-inject simone:file:id_ed25519.pub
sudo virt-customize -a jammy-server-cloudimg-amd64.img --run-command 'chown -R simone:simone /home/simone'
Enter fullscreen mode Exit fullscreen mode
  1. Adds the user to the image
  2. Makes the SSH Key directory
  3. Injects the SSH Key. simone is the user the key will apply to, file:id_ed25519.pub is the file on the local host where the SSH Key is located
  4. Makes sure the user simone owns the home folder

Create a virtual machine from the image

Now we need to create a new virtual machine:

qm create 9000 --memory 2048 --core 2 --name jammy-template --net0 virtio,bridge=vmbr0
Enter fullscreen mode Exit fullscreen mode

Import the downloaded Ubuntu disk to the correct storage:

qm importdisk 9000 jammy-server-cloudimg-amd64.img local-lvm
Enter fullscreen mode Exit fullscreen mode

Attach the new disk as a SCSI drive on the SCSI Controller:

qm set 9000 --scsihw virtio-scsi-pci --scsi0 local-lvm:vm-9000-disk-0
Enter fullscreen mode Exit fullscreen mode

Add cloud init drive:

qm set 9000 --ide2 local-lvm:cloudinit
Enter fullscreen mode Exit fullscreen mode

Make the could init drive bootable and restrict BIOS to boot from disk only:

qm set 9000 --boot c --bootdisk scsi0
Enter fullscreen mode Exit fullscreen mode

Add serial console:

qm set 9000 --serial0 socket --vga serial0
Enter fullscreen mode Exit fullscreen mode

Turn on guest agent:

qm set 9000 --agent enabled=1
Enter fullscreen mode Exit fullscreen mode

DO NOT POWER ON THE VM
{: .prompt-warning }

Convert the VM to a Template

Now, Create a template from the image you just created:

qm template 9000
Enter fullscreen mode Exit fullscreen mode

Clone the template to a VM

Now you have a fully functioning template, which can be cloned as much as you want. But it makes sense to set some of the settings.

First, clone the VM (here we are cloning the template with ID 9000 to a new VM with ID 999):

sudo qm clone 9000 999 --name test-cloud-init
Enter fullscreen mode Exit fullscreen mode

Next, set the SSH keys (if you didn't add yours earlier) and IP address:

sudo qm set 999 --sshkey ~/.ssh/id_rsa.pub
sudo qm set 999 --ipconfig0 ip=10.10.1.20/24,gw=10.10.1.1
Enter fullscreen mode Exit fullscreen mode

It’s now ready to start up!

sudo qm start 999
Enter fullscreen mode Exit fullscreen mode

You should be able to log in without any problems (after trusting the SSH fingerprint). Note that the username is ubuntu, for the key set here. If you added your own user earlier, you can use that instead

ssh ubuntu@10.10.1.1
Enter fullscreen mode Exit fullscreen mode

Once happy with the template, you can stop the VM and clean up the resources:

sudo qm stop 999 && sudo qm destroy 999
rm jammy-server-cloudimg-amd64.img
Enter fullscreen mode Exit fullscreen mode

References

https://registry.terraform.io/modules/sdhibit/cloud-init-vm/proxmox/latest/examples/ubuntu_single_vm

Closing

Hopefully this information was useful for you, If you have any questions about this article and share your thoughts head over to my Discord.

💖 💪 🙅 🚩
marksie1988
Steven Marks

Posted on June 3, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related