This post documents the steps needed to create a minified debian cloud image template, which you can easily clone, and have a running VM in seconds with no configuration needed.
This includes common packages, ssh-keys, IP information.
The end result of this project is...
You clone a template, and start it.
The cloned VM automatically starts up, updates itself, and starts running, with common packages installed, and your ssh details pre-configured.
A slimmed down distribution, without excess (My end result was a 400M image.)
No need to edit the VM after cloning, other then adding CPU/RAM, or other hardware as needed.
These, are special images provided, which includes support for cloud-init. For this example, we will specifically be leveraging the "genericcloud" image, which is suitable for our VMs.
This- image does not include many bare metal hardware drivers, which aren't needed, since the focus of this post is creating a VM template.
At this point, we have a cloud init image. However, I personally like to customize my images, to install a few basic tools, as well as the qemu-guest-agent.
You will need to install the libguestfs-tools package, via your package manager. If you don't wish to install the tools on your proxmox machine, feel free to customize the image on your desktop, a LXC, VM, etc.
By default, the cloud images uses the hostname as the DHCP identifier. This- works fine.... when it doesn't send the default template name for every cloned image.
A simple fix, is to just update the logic to use the hardware / mac address.
For me- setting the dhcp-identifier to use the MAC address, works extremely well, as proxmox gives each clone a randomly generated MAC address when cloning.
One more step- we need to add a step to reset the machine-id. If this step is left out, machines will each acquire the same IP address when using DHCP (Regardless if your MAC is different)
Here is the list of CLI commands used to download, and customize the image.
# The ID of the Template/VM.VM_ID=139# Desired proxmox storage for the new image.STORAGE="ceph-block"# Download Image
wgethttps://cloud.debian.org/images/cloud/bookworm/latest/debian-12-genericcloud-amd64.qcow2
# Install tools to manage images
apt-getinstalllibguestfs-tools
# Install packages
virt-customize-adebian-12-genericcloud-amd64.qcow2--installqemu-guest-agent,curl,wget,nano,rsync,htop
# Set the DHCP client identifier to use hardware address.
virt-customize-adebian-12-genericcloud-amd64.qcow2--run-command"sed -i 's|send host-name = gethostname();|send dhcp-client-identifier = hardware;|' /etc/dhcp/dhclient.conf"# Reset Machine-ID
virt-customize-adebian-12-genericcloud-amd64.qcow2--run-command"echo -n > /etc/machine-id"# Compress the image
qemu-imgconvert-Oqcow2-c-opreallocation=offdebian-12-genericcloud-amd64.qcow2debian-12-genericcloud-amd64-shrink.qcow2
# Import the image to our template.
qmimportdisk$VM_IDdebian-12-genericcloud-amd64-shrink.qcow2$STORAGE
This step is questionably optional, because you don't need to convert this to a template, in order to clone it.
Info
If you wish to use a linked-clone, you DO need to convert your VM to a template.
Warning
Without converting to a template, you will need to change the MAC address for every clone.
(aka, you should make this a template, if you plan on cloning it)
To convert this to a template- Right click on the VM, and select, Convert to template.
After you have converted it to a template, you will no longer be able to start/stop or snapshot this machine. You would need to clone it first, and then start the clone.
One final configuration item- Edit the network interface on your template. Ensure its MAC Address to 00:00:00:00:00:00 (This will cause a random MAC address to be generated for every new clone)
Warning
If you miss this step- all of your cloned VMs will have the same MAC address.
This, will likely cause a lot of problems. Make sure to do this step!
Once you start your VM, it will automatically start the process of downloading updates, and configuring itself.
And- lastly, you will be left, with a login prompt.
If- you followed through with the step to update the base-image, to add qemu-guest-agent, you will also be able to see IP details via the proxmox web interface.
Lastly- since we specified the username, and ssh-keys via the CloudInit, we can also log into this VM.
Looking at ceph- my base image is only using 1.3G of total disk space.
If- we look at a VM cloned using Full Clone- we can see, it is using 1.4G of disk space. The parent field is also empty (as it was a full copy)
Looking at a disk from a Linked clone, we can see, it is also using 1.3/1.4G of disk space, but, you can see the "layering" feature is enabled, and it has a parent specified.
Now- you might be saying- That doesn't sound right at all.
And, I would agree. I would assume it is working as intended. But- I am out of time to determine how to find the actual child's usage over the parent.
# Get base image children.
root@kube01:~#rbdchildrenceph-block/base-139-disk-0
ceph-block/vm-141-disk-0
# Get base image details
root@kube01:~#rbdinfoceph-block/base-139-disk-0
rbdimage'base-139-disk-0':
size16GiBin4096objects
order22(4MiBobjects)snapshot_count:1id:773ac59ea6a2db
block_name_prefix:rbd_data.773ac59ea6a2db
format:2features:layering,exclusive-lock,object-map,fast-diff,deep-flatten
op_features:
flags:
create_timestamp:TueJul216:56:022024access_timestamp:TueJul217:00:412024modify_timestamp:TueJul216:56:022024# Get image details for the specific snapshot the child is using
root@kube01:~#rbdinfoceph-block/base-139-disk-0@__base__
rbdimage'base-139-disk-0':
size16GiBin4096objects
order22(4MiBobjects)snapshot_count:1id:773ac59ea6a2db
block_name_prefix:rbd_data.773ac59ea6a2db
format:2features:layering,exclusive-lock,object-map,fast-diff,deep-flatten
op_features:
flags:
create_timestamp:TueJul216:56:022024access_timestamp:TueJul217:00:412024modify_timestamp:TueJul216:56:022024protected:True
# Get child image details.
root@kube01:~#rbdinfoceph-block/vm-141-disk-0
rbdimage'vm-141-disk-0':
size16GiBin4096objects
order22(4MiBobjects)snapshot_count:0id:7744398791b79f
block_name_prefix:rbd_data.7744398791b79f
format:2features:layering,exclusive-lock,object-map,fast-diff,deep-flatten
op_features:
flags:
create_timestamp:TueJul217:12:122024access_timestamp:TueJul217:12:122024modify_timestamp:TueJul217:12:122024parent:ceph-block/base-139-disk-0@__base__
overlap:16GiB
Help, all of my machines are getting the same IP address from DHCP.¶
Edit the hardware of your template. Set the Network Device's MAC address to 00:00:00:00:00:00
New machines will receive a random mac address.
(If- you missed the step above, and you have already cloned a dozen machines, you can "clear" the mac address field of the VMs NIC(s), and a new address will be automatically configured.)