Having created our virtual machine and installed Ubuntu Server 24.04 LTS we now need to complete the configuration and install the remaining tools and utilities I want in every VM and then export it as an OVF template ready to be used.
Btrfs Sub-volumes
In Create the Ubuntu Template VM we created a new virtual machine and installed a minimized Ubuntu Server 24.04 LTS onto it using a custom filesystem layout based on btrfs. For reasons best known to themselves Canonical have decided not to allow the server installer to create btrfs subvolumes for the root and home directories like they do with the desktop install. This feels like a bit of a foot-gun decision to me and means more work for template creation as I really like the ability to create snapshots of the filesystem for backups.
Since this messes with the root filesystem and the boot configuration this needs to be with the VM booted from the installer DVD and then mounting the filesystems to make the changes. This looks a bit scary at first but it's actually quite simple. The hardest part is booting to the installer.
This process is based on a answer I found (askubuntu.com?) and I'll link to it here when I find it again.
The process has 3 main steps
- Create the btrfs subvolumes and move files
- Create a chroot environment using our subvolumes
- Update the Linux config to use subvolumes
While we're doing this we are going to have files in a different place to where the Linux config is expecting them and if you make a mistak it's quite likely our template VM will be left unbootable. Fixing it is usually just a case of repeating the process with adjustments to reverse the step you goofed but it's still worth taking a snapshot of the VM first so that you can revert the changes without having to reinstall if it doesn't quite go to plan.
We want to be really sure this snapshot is consistent so this is best done with the VM stopped. In the ESXi Host Client select the Template VM and go to Actions > Snapshots > Take snapshot.

At least give it a name so you can find it if you need to revert later and click Take Snapshot. Another reason we need the VM shut down for this is we don't yet have the VMware Tools installed.
The easiest way to get into the setup again is with the Template VM stopped edit the the config and under VM Options > Boot Options tick the Force BIOS setup option boot to bios option and also set the CD/DVD drive to Datastore ISO file and select the install .iso used to create the VM.
Click Save and start the VM and it should then bring up the Boot Manager. Click on the console screen to open it or use the Console option on the menu to open in a tab if you prefer.

Select EFI VMware Virtual SATA CDROM Drive (0.0) as the boot option and hit enter and the VM will boot from installation ISO.

Select Try or Install Ubuntu Server the installer will then start even though is says Try or Install. Hit cursor up to reach the top of the language list and then up once more to the [ Help ] menu and hit return and then either pick the Enter shell or Help on SSH access options.

If you select Help on SSH access option it'll give you the ssh connection details together with a 20-character password if you'd rather use a different terminal.
From our template design we can see we have the following filesystems:
| Device | Format | Filesytem | Size |
| /dev/sda1 | vfat | /boot/efi | 128 MB |
| /dev/sda2 | ext4 | /boot | 384 MB |
| /dev/sda3 | btrfs | / | 7.5 GB |
We will need to reference this as we need to mount the VM filesytems into our installer shell session.
In our shell we are looking at the filesystem of the installer not our template VM. The first step is to mount the root filesystem of our template VM so we can work with it - we'll use the /mnt directory for this - and then create the sub-volumes and move things around
We now need to move all the other folders from the root of our template VM to the @ subvolume apart from the @home subvolume. Since there's no "move all except" I've found it's safer to give a whole series of mv commands
...
and so on until all that's left in /mnt is @ and @home
If you wish give a sync command to make sure any caches are flushed and everything is written to disk and then move out of the /mnt directory and unmount the template VM disk
The next step is to create a complete filesystem setup on /mnt using a mix of our template VM subvolumes and our running installer shell and then starting a chroot process to run the reconfiguration. While we don't need to touch the home directories for this, if you wanted to include them anyway use mount -o subvol=@home /dev/sda3 /mnt/home.
We now need to make the changes to the boot config to reference the subvolumes. The first step is to modify /etc/fstab so it mounts the root and home sub-volumes correctly.
Original fstab for root
...
# / was on /dev/sda3 during curtin installation
/dev/disk/by-uuid/abcdabcd-abcd-abcd-abcd-abcdabcd123 / btrfs defaults 0 1
...
Modified fstab for root and home subvolumes
...
# / was on /dev/sda3 during curtin installation
/dev/disk/by-uuid/abcdabcd-abcd-abcd-abcd-abcdabcd123 / btrfs defaults,subvol=@ 0 1
/dev/disk/by-uuid/abcdabcd-abcd-abcd-abcd-abcdabcd123 / btrfs defaults,subvol=@home 0 1
...
And then finally we can update the bootloader config and the update-grub script will do this for us.
This is pretty much a pass/fail at this point. You either have VM running with btrfs subvolumes @ for root and @home for the home directories and all is good, or you've crashed with a kernel panic or stuck in the emergency shell. While fixing is possible it's probably easier just to revert to the snapshot you took at the start of this step and try again.
Once the template VM has rebooted login and check the filesystems with df -hT and we should now see something like this with /dev/sda apparently mounted twice:
The final step should be to shut down the template VM and take another snapshot - it's up to you if you want to keep any previous ones.
Configuration
With Ubuntu installed we can now customise our virtual machine ready to become our official template. While the minimized installs are not intended to have humans regularly logging in and doing stuff it's worth installing a couple of tools to help us poor fleshies troubleshoot, plus the standard packages I add to all my Geeklab VMs and were previously done by the playbooks. As before either prefix everything with sudo or just become root with sudo -i and live life on the edge.
ping
There's two different versions, the iputils edition is my preference as it's more linux specific and does the same as the alternatives.
Install: apt install iputils-ping
vi
An editor for the config files is always going to be useful. While vi is horrible in so many ways it's small, simple and does the job and eventually you get used to it.
Install: apt install vim
Config: Create a .vimrc containing the following line in /root and /etc/skel. It's also worth copying to the build user under /home/[builder] setting the appropriate user and group too.
set nocompatible
This will make it a bit friendlier and allow the use of cursor keys while in edit mode.
VMware Tools
The ESXi Host Client shows that VMware Tools 12389 is installed on the VM but not active which appears to be a bug (or a hallucination as people seem to be calling it when computers make stuff up) although the VMware drivers are present and being used.
Install: apt install open-vm-tools
Once installed the ESXi Host Client should then report version open-vm-tools 12.4.5 (or maybe later) is installed and running and also show the OS hostname and IP addresses for the VM too under General Information > Networking/
apcupsd
My Geeklab server has a CyberPower UPS to allow for a graceful shutdown should the mains power fail. It supports the same signalling protocol as the APC units so I use apcupsd on each VM to trigger a shutdown based on how much battery is left. And unlike the APC unit I had before it produces clean sine output that doesn't freak out the server PSU.
Install: apt install apcupsd
Config: The deploy playbooks will configure the UPS daemon so for now disable it with systemctl disable apcupsd
Bacula Client
I also use Bacula to handle backups so to save time during deployment it makes sense to preinstall this now.
Install: apt install bacula-client
Config: The deploy playbooks will configure this too so for now disable it with systemctl disable bacula-fd
Build User
Another advantage of using a template VM for deployments is we can create specific users for use by the deployment scripts with key authentication for additional security which also makes the playbooks much simpler too.
This is based on using a user called ansibledeploy, and the user we created during Ubuntu installation and more importantly it's group being called builder. If you use different names you'll need to use them instead.
All of this config will be removed during deployment and we'll create a new user for ongoing maintenance so that we don't leave a hard-coded back door into all systems built using this template.
Preparation:
Create an SSH key pair to be used by the ansible playbooks if you don't already have one. You will need the private key for later to use with the deploy runbooks. Don't do this on the actual template VM!
Config:
Create the build user (e.g. ansibledeploy) with the same permissions as the install user and you can use the same group.
While it's a member of the sudo group we can also enable it to use sudo without prompting for a password by creating a file such as /etc/sudoers.d/ansibledeploy with the following content:
# Grant initial build user, which is SSH only without password, sudo access
ansibledeploy ALL=(ALL:ALL) NOPASSWD: ALL
The final step is to enable login via an SSH key by adding the public key we created earlier to the authorized keys file for our build user
Open (or cat) the public key file (ansibledeploy.pub?) you created earlier and copy the contents and then paste into authorized_keys file and save it
Check that it works by starting a new SSH session using the key file. For example
You should now have full access to the VM without having been prompted for a password.
Prepare for Export
Once you are sure everything is set correctly and the template is good it's time to prepare for export. These steps will need to be repeated after every startup of the VM itself as some of the parameters will be regenerated.
I would suggest you run an apt update to update all installed packages now as there's been some fixes since 24.04 was released. Whether or not to always do an update after changing the template is a different question. On the one hand this reduces the time to deploy a VM if there's fewer updates to apply, on the other it's possible that playbooks could be affected if a package they rely on has a significant change so you should at least do a complete test after any change.
It might also be worth maintaining an info file about the template as /etc/TemplateInfo.txt or something similar.
Each Ubuntu host has a unique identifier that gets used in various places, the critical one for this being the DHCP process and can lead to the IP address changing during the deploy process which is extremely inconvenient. If we blank the machine ID in our template it'll get regenerated on the first boot of VMs derived from out template. This also means if we start the template VM for changes it'll also get regenerated so it's very important to reset this every time before shutting down our template VM.
The final bit of administriva is to update the comments box in the ESXi Host Client as that will also be included in the OVF Export. I find something like this help me keep track of what VMs were created for, especially if they were for a sandpit or project that's lingering for a while.
Ubuntu Server 24.04 LTSTemplate: X_U2404-base-2, 12 Oct 24
Purpose:
Minimal install with open-vm-tools, apcupsd, bacula-fd
btrfs @ and @home configured.
/etc/machine-id is blanked
If you've been creating VM snapshots it's also worth reviewing if they're still needed. The export process ignores them so it's probably not worth keeping any once you've had a successful test.
Create Template with OVF Export
My first export attempt used the ESXi Host Client Actions > Export option and this successfully created and downloaded an OVF template. But I had a lot of issues importing it using the OVFtool later. Using the OVFtool to do the export as well proved to be much more successful. To save typing I created a script to do each export to a dedicated export testing directory and with all the parameters set.
When executed this script will export the VM specified in OVF_SOURCE_NAME to the directory specifed in OVF_TARGET and will probably take several minutes to run. The VM should be offline when you run this.
#!/bin/bash#################### Create an OVF from an existing VM using the VMware OVFtool## Version: 0.20# Date: 28 Sept 2024#
# Deployment parameters
# Hardcode during script development, take a parameters later.
OVF_TOOL_LOCATION="~/Projects/automation/ovftool/ovftool"
ESX_HOST=esxhost.labs.example.comESX_USER=esxuser
#ESX_PASS=esxpass#ESX_PASS=$1
OVF_SOURCE_NAME="X_U2404-base-2"
OVF_TARGET="/home/user/Projects/automation/VMtemplates/export"
# ExecuteOVF_PARAM="--targetType=OVF vi://$ESX_USER:$ESX_PASS@$ESX_HOST/$OVF_SOURCE_NAME $OVF_TARGET"
echo "Sending: $OVF_PARAM"echo ""~/Projects/automation/ovftool/ovftool $OVF_PARAM
Putting your ESXi credentials into a script is not exactly brilliant and even a command line parameter has risks, so uncomment whichever ESX_PASS statement you think is the least worst. It's probably worth setting permissions on this script to 0700 so you're the only one to see it and don't check it in to a repo with real credentials in it. At some point I will update this to take credentials from a config file. Just call it inspirational rather than production-ready code.
The key part is the OVF_TARGET needs to be a full path to wherever you want the exported VM to be placed.
In the next article we'll look at how to do the import using an Ansible playbook