Using an OVF template to deploy a virtual machine for my Nextcloud server solves a lot of problems, but of course that means I need to create the template to start with. In this first part I look at the design decisions for my Ubuntu 20.04 Nextcloud template and creating the source virtual machine.

The first stage of building a Nextcloud server on my GeekLab is to deploy a virtual machine to host it. It is possible to have multiple separate instances of Nextcloud running in a single virtual machine, but for simplicity I'm keeping each installation on it own VM so I have complete freedom to do whatever I like without worrying about impacting anything else.

Previously I have defined the VM manually in the ESXi Host Client and then manually installed the OS from an ISO image which doesn't take too long. I then ran an Ansible playbook to do the initial post-installation configuration tasks which completed in 3 minutes what would otherwise take 2 hours so this was a great timesaver.

Setting up for Nextcloud is a little more complicated and there's clearly some opportunity to automate more of this. Even though I've already decided on using Ubuntu 20.04 LTS as the operating system, there's a few different methods we can use for creating a VM, but I also have a few constraints.

VMware ESXi Constraints

I am using the free VMware ESXi hypervisor on my GeekLab server, which is fully functional as a hypervisor but does not have the advanced VMware features which I don't really need. It also cannot be managed by vSphere which is most definitely not free and is really overkill for managing a single host.  A second constraint is that although all the VMware ESXi API's exist, they're all locked as read-only so that rules out using things like Terraform with the official VMware provider or the Ansible VMware modules.

Ubuntu Constraints

Once installed and configured with a user that Ansible can connect to, we can do almost anything we like with either Ansible modules or calls to shell commands to run programs and scripts directly. But the trick is to get the OS installed without me having to do anything.

From what I've seen there are two approaches I could use for this:

  • Create a custom installer that I can manipulate by editing configuration files before booting from it
  • Use cloud-init to pass configuration info to the ubuntu installer on boot

Neither of these approach address creating the VM definition or starting it, and to use cloud-init directly from ESXi requires a paid licence so again not an option.

I might well come back to look at both of these options again in more detail once Ubuntu 24.xx is released though as I'm have a feeling that I'm missing something.

VMware OVF Templates to the rescue

The free version of VMware ESXi supports the import and export of virtual machines using OVF templates. These templates consist of a set of files and include both the VM configuration in an XML-formatted .ovf and the contents of any defined disks as .vmdk files.

This means I can create a new virtual machine with Ubuntu installed to the point of first boot just by running the VM Import wizard in the ESXi Host Client, picking a VM Name and network and telling it where the template files are. I've also seen references for a way to do this from a command line so even this could be automated too.

The one thing the template cannot do is change the configuration of the guest operating system inside it, but for that we have Ansible playbooks.

Template Design

The configuration of the virtual machines template is going to directly influence the playbooks that will be used to create the finished Nextcloud server - and this is true for any servers using these techniques, not just Nextcloud. This being a first attempt I decided to keep this as simple and generic as possible so didn't do anything beyond define the VM and complete the Ubuntu installer to create an initial filesystem layout.

With the benefit of experience there's now a number of additional steps I would complete at the template stage to both simplify and speed up the playbook execution, but this is still a very usable template and it works which is the most important thing.

Source VM Configuration

The source VM was created in a sandbox network and I named it X_u2004_Nextcloud. This is sized for test lab and will support a handful of users quite happily. The amount of CPU and RAM can easily be modified later if needed and even allocating additional disk space is relatively simple.

VM Configuration
Item Value
Compatability ESXi 7.0 U2
OS Family Ubuntu Linux (64-bit)
vCPU 2
RAM 2 GB
Disks

4 local disks

Device SCSI Size Partitioning Usage
/dev/sda 0:0 8 GB GPT Boot & Root
/dev/sdb 0:1 2 GB GPT Swap
/dev/sdc 0:2 2 GB GPT MariaDB
/dev/sdd 0:3 8 GB GPT Nextcloud
SCSI VMware Paravirtual
Network 1 VMXNET 3, Sandpit
Options

Boot Options: Firmware = EFI

Delay = 3000ms

 

The datastore I use for VMs and working data is two separate disks on a mirroring controller for resilience so I don't need to worry about creating mirrored disks at the OS filesystem level.

Source Ubuntu Configuration

I stuck with a default installation of Ubuntu for this first version and the playbooks will install and configure everything else. With the benefit of experience this probably isn't the best approach if the template is already designed with some specific use cases in mind but the thinking was it'll be quicker to adjust a playbook than to create a new template and have to recheck every step of the process. More on that in the conclusion.

Item Configuration
Locale & Keyboard English (UK)
Initial User Created username and password, did not import any keys
Updates Only security updates applied at the end, did not update the installer
Disk layout Entirely custom

 

Filesystems
Device Format Filesytem Size
/dev/sda1 vfat /boot/efi 64 MB
/dev/sda2 ext4 /boot 512 MB
/dev/sda3 btrfs / 7.5 GB
/dev/sdb1 swap   2 GB
/dev/sdc1 LVM2 vgMariaDB 2 GB
/dev/sdd1 btrfs /srv/nextcloud 8 GB

 

All the disks are created with GPT partitions by default since the VM is set to use EFI. I've also settled on always creating a partition even where I could use the raw disk such as /dev/sdc and /dev/sdd as it makes it obvious the disk is in use and what the filesystem is and also keeps options open for future changes. It does mean a bit more effort to extend but it's not something I expect to do very often, especially on sandpit hosts.

The /boot/efi partition is initially created as 512 MB, but can be edited down to 64 MB in the disk configuration screen. This is still hugely oversized but it doesn't like it being any smaller.

Using btrfs will allow the the use of snapshots for backup and it also allows disk partitions to shrink as well as expand. Unfortunately the Ubuntu installer doesn't understand btrfs sub-volumes so that's a task for configuration. Another drawback is performance with databases ranges from OK to stupidly slow so a separate LVM2 disk is used to create an XFS filesytem for MariaDB so I could still implement snapshot backups via LVM. XFS has a reputation for being fast and resilient, but the drawback with XFS filesystems is while they can easily be grown they cannot be shrunk. Using Ext4 would be an option if I needed to be able to both grow and shrink the database filesystem.

Using a dedicated disk partition for swap is apparently faster than using a swap file although for some reason Ubuntu 20.04 also creates (but doesn't activate) a swap file in the root partition.

LVM Configuration
Volume Group Logical Volume Format Size Mount
vgMariaDB lvMariaDB xfs 1.5 GB /var/lib/mysql

 

There is 0.5 GB remaining for future use such as snapshots.

Template Creation

Creating the template VM is just like creating any other virtual machine. Once you've got everything configured it is then exported as a set of files.

Define the Virtual Machine

The first step is to login to ESXi Host Client and build the virtual machine.

ESXi 7 Host Client

Login to the ESXi Host Client and then click Create/Register VM on the Host menu. The New Virtual Machine wizard will start running to guide you through the process.

Staring the new VM wizard

Select Create a new virtual machine and click Next.

Select VM name and OS

The name is used by VMware and is not the OS hostname although they could be the same. I currently use a naming convention of  prefix_hostname where prefix identifies the group the VM is assigned to which helps.

This often leads to some duplication as I use a similar approach to hostnames.

Pick the latest Compatibility for the VM unless you know it might need to be exported to an earlier release of ESXi or you're uusing an OS which only supports older versions. The Guest OS family and Guest OS version options will select an appropropriate set of defaults for tuning the VM. If you set them incorrectly ESXi will complain at you when you start the VM and apparently you might notice some odd effects. In this case we're building an Ubuntu Linux VM. Click Next when ready.

Select storage location for VM

My GeekLab currently has two VMware datastores defined:

  • LD1-VM : A pair of disks configured as mirrored pair on the disk controller, Logical Disk 1 for VMs
  • Media : A single large disk, used for media files, OS installation disks, cache for backup & archives

The storage location is where the VM definition will be held, but it's  only the default location for the virtual disk files which can be customised for each disk as they're defined in the next step. Click Next when ready.

Defining the VM hardware configuration

And now we get to define the virtual hardware on our virtual machine, using the spec we decided above which is for the initial build of a development machine. These can be changed later althought don't forget you'll probably have to do some reconfiguration of the OS as well.

I've found setting 2 cores for CPU should be the minimum - its setting a maximum so if the VM is idle it won't be consuming those two cores. There's also a detail configuration (not shown) which allows you to choose the number of CPUs to emulate. This is a topic of continued discussion, I'm currently setting 1 CPU with all the cores to match the physical host which only has a single CPU.

I'm also defining all the disks at thin provisioned since you get to chose on template import to expand some to thick provisioning which keeps my options open. I'm also using a single disk controller in this case, I have seen some systems working better with separate controllers for OS, swap and data disks but these VMs are not going to be busy enough where that becomes an issue and I could always reconfigure the systems created from this template anyway.

I'm bulding this on my sandpit network at least for the build process. The CD/DVD Drive 1 needs to be defined to use the Ubuntu 20.04 LTS ISO image I have previously uploaded to the media datastore.

Click VM Options once you're happy with the hardware configuration.

VM technical configuration options

Open the Boot Options section to modify how the VM will boot. The defaults still work of course, this is just tuning.

  • Select EFI for the boot firmware instead of BIOS. This will affect the OS install too.
  • Enable UEFI secure boot: I'll probably enable this on the v2 template, for this first one I left it off.
  • Boot delay: Set a value of 3000 milliseconds to add a brief pause on startup so you can get to boot settings if you ever need to.

You can switch between the Virtual Hardware and VM Options tabs as often as you need to. When you're happy with the settings you chosen click Next.

Review the VM definition

It's worth doing a final check of the settings you've chosen just to be sure they're all as they should be. While most settings can be modified later it can require a lot more effort to reconfigure the OS and applications in the VM as well so it's worth taking an extra minute to check now. When you're sure all is good click Finish.

VM defined

And there we have it. A new VM defined and ready to install Ubuntu which will then become a template for a series of development and test Nextcloud hosts.

In VM Template Creation part 2 I'll walk though the Ubuntu installation process and exporting the VM as an OVF template.