In this second part to creating an OVF template to deploy a virtual machine for Nextcloud servers, we install Ubuntu 20.04 LTS and configure the initial filesystem layout.
Template Design
In VM Template Creation part 1 we looked at the overall template design so lets first take a quick refresh at what we need to configure in Ubuntu.
Source Ubuntu Configuration
This will be a vanilla Ubuntu installation with only the filesystems having any configuration.
| Item | Configuration |
| Locale & Keyboard | English (UK) |
| Initial User | Created username and password, did not import any keys |
| Networking | IPv4 only during build, DHCP enabled, no proxy |
| Updates | Only security updates applied at the end, did not update the installer, Default repos. |
| Disk layout | Entirely custom |
| 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/netcloud | 8 GB |
| Volume Group | Logical Volume | Format | Size | Mount |
| vgMariaDB | lvMariaDB | xfs | 1.5 GB | /var/lib/mysql |
| unallocated | 0.5 GB |
All the disks will be created with GPT partitions by default since the VM is set to use EFI.
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. This was also covered in VM Template Creation part 1
Install the Operating System
Staying with the ESXi Host Client, once the VM is defined start it up by clicking the Power on button and let the VM boot from the operating system .iso chosen during VM definition.

We need to use the Console to control the VM during OS installation. The ESXi Host Client offers various methods via the Console button.

For more detailed tasks like OS installation I find it more useful to open the console in a new window which I can then resize down to something sensible. You'll also see a warning that VMware Tools is not installed which is to be expected as we've booted into Subiquity, a minimal Ubuntu instance just to run the installation sequence.
All navigation in Subiquity is through the keyboard up, down and enter keys, there is no mouse support in these installation screens.
By my counting there's 13 steps to the installation sequence. It will start by asking you to select your language for this installation.

Select your most preferred choice from the list, which will be highlighted in green and press Enter. Subiquity will then ask if you want to continue with the current version or update to the latest installer release.

I usually continue without updating since this is works well enough and I don't want to wait for it to download and restart. It also means my installation notes stay consistent too. Make your choice and press Enter. If you do choose to update then the following screens may be different for you.
The next choice is keyboard configuration

Choose the layout and any variation to match the keyboard layout you'll be using to access the VM through the console. Select Done when you've made your choice and and it'll move on to network configuration.

The Subiquity installer will try to start a network connection at this point. The default is an IPv4 DHCP connection on the first ethernet connection it can find and we want to leave this alone as we'll want all the virtual machines we build from this template to start with DHCP until we've completed the deployment playbooks that will follow. Click Done and we then move on to proxy configuration.

I don't use an HTTP proxy for internet access, or at least not at the moment anyway so I'm leaving this blank. If you need to use a proxy in your own environment then configure it here, and remember it'll apply to this system during template creation as well as all future VMs created from it. Click Done when ready.

The installer will make a reasonable pick for an Ubuntu mirror site to install from so you can just select Done on this screen. But if you have a different preference edit the URL. I've always just gone with the default on this.
The next few screens are all about creating an initial filesystem configuration and this is the Nextcloud "special sauce" as far as this template is concerned.

Since we have a specific four-disk layout for our Nextcloud installation choose the Custom storage layout option and select Done. Then we get to start building up the base filesystem layout for our template.
All known local storage on the VM will be displayed at this point so a good first check is you can actually see all the disks you were expecting. We'll move through the disks in sequence, which will be following the SCSI ID's set during the VM definition in VM Template Creation part 1. If you don't remember setting any SCSI ID's default values starting from 0:0 were assigned for as you added the disks. Do not select the Done button at the bottom of the screen until all storage configuration is complete.

We start with an empty configuration, so move down to /dev/sda and press enter to bring up the options and choose Add GPT Partition.

The first partition we want to define is for /boot. This is not mandatory and we could leave this as part of the root partition, but the advantage of creating a specific partition is it will allow Ubuntu to start even if the root partition should become filled for any reason.
It could be argued this is wasted space and reduces the number of old boot configuration we can have but it's a worthwhile price in my opinion. I now allocate 512 MB (shown at 512M in the configuration) for this, which is not overly large, and I'm using the ext4 filesystem for simplicity should an emergency repair ever be required. Click Create when ready.

Our newly defined /boot filesystem will now appear at the top of screen under File System Summary. Since we selected EFI as the boot firmware during VM definition it also helpfully adds an EFI System Partition for us as partition 1 at the crazy size of 512 MB too. As we'll see at the end of the Ubuntu installation process we only need 6.1 MB for this so go down to the bottom of the screen under Used Devices and highlight partition one and press Enter to bring up the menu for this and pick Edit.

Set the size to a more reasonable number. The smallest it seem to like is 64 MB which is still way too big but anything less is either rejected by the installer or things break later on. Select Save when you're ready.
If you had left the boot firmware as BIOS during VM definition we have a 1 MB BIOS_GRUB partition created here for us instead.
We still need to create one more partition on /dev/sda so highlight it once more.

Press enter to bring up the menu and pick Add GPT Partition again.

This is going to be our root partition. Leave the size blank to use all remaining space which should be just under 7.5 GB. Choose btrfs for the format and set mount to /. When ready click Create.
I'm choosing btrfs by default now although it's still a topic for debate. In it's favour you can both create and shrink the partition, it's easy to create mirrored disks for resilience (although my server can do that in hardware for every VM in the datastore) and it has a nice snapshot facility for both backups and also for quickly undoing mistakes. The big downside is the copy-on-write process than enables all these makes it perform very poorly where there's frequent changes to the same file such as in a database - which we address later.
The next disk to tackle is /dev/sdb which we're going to use for a dedicated swap partition.

Select the device /dev/sdb from Available Devices, press Enter and then Add GPT Partition.

Leave the size empty to use the entire disk, set the format to swap and then highlight Create and press enter. Swap doesn't need a mount point.
Having a dedicated swap partition apparently gives better performance than using a swap file which by default gets created on the root partition as /swap.img which means it's also taking up space in root partition too. While servers really should be sized not to need swap for normal processes since it's much slower than using real memory, having at least couple of gigabytes can help in two other ways:
- If something strange happens and there's a surge in memory consumption, having a swap available can be the difference between the host slowing down but keeping going and the host running out of memory and killing processes or potentially itself. Which is bad, and very annoying if you're the one who ends up having to watch it at oh-my-god-o-clock to work out what's going on because the support team can't be bothered. Been there, done that...
- If you see the maximum amount of swap used slowly creeping up over time it can also be an indication the VM sizing isn't quite right for the workload.
Another advantage of using a dedicated disk for swap is that if I change the RAM sizing of the server it's very easy to resize the swap too should I need it. I only need to resize that disk, rather than having to mess with the root filesystem and it could probably done with the VMs applications online and functional.
The next disk is our data disk for MariaDB, which we configure in a more roundabout fashion.

As mentioned when we were creating the root filesystem in /dev/sda, btrfs isn't a good choice for databases so in practice that means ext4 or xfs. To retain the option of implementing backup snapshots which are not supported on either ext4 or xfs we're going to create this filesystem inside an LVM2 Logical Volume. To start this process navigate down to Create volume group (LVM) and press Enter. This cannot be done directly from the /dev/sdc device.

We need to give the volume group a name and a common convention is to start the name with vg. I have chosen vgMariaDB for this template as that's nice and descriptive, and of course it's what I put in the original spec too.
We only want to use the /dev/sdc device for this so just select that one only. This will format the whole of /dev/sdc as an LVM2 Physical Volume rather than creating a partition first so I'll need to look at that again. For now this is noted as a deviation from spec.
We're not going to to use disk encryption at this point since that will involve extra configuration to manage keys and starting the VM and the file data disk won't support encryption under this design either so it's pretty pointless. Besides, Nextcloud can handle it's own encryption.
Click Create when ready and we can then move on to the next step and create a Logical Volume to hold the filesystem for our database.

Under available devices you should now see vgMariaDB instead of /dev/sdc. Highlight this and press Enter to bring up the menu and select Create Logical Volume.

We also need to give our Logical Volume a name and a common convention is to start them with lv so I'm going to use lvMariaDB since this will hold the actual filesystem for our database. The volume group is just under 2GB - the entire /dev/sdc device - and I want to retain some free space for future use. I'm going to use a 75% of the space as the initial size so I'm go to set it to 1512M or 1.5 GB. This will be formatted as xfs and I'm going to set Other for the mount point and specify /var/lib/mysql which is the default installation location for MariaDB.
By keeping some spare I can very quickly extend the volume if I need to, but the main use I have in mind is being able to to create an LVM2 snapshot of he logical volume for backup purposes - the snapshot will use this unallocated space to store changes while the backup is running so it doesn't need to be full size and will be released when the snapshot is dropped.
When happy with the configuration select Create and the /var/lib/mysql filesystem will be added to the list and we can start on the last filesystem configuration. The vgMariaDB volume group will stay on the available devices list since we have some unallocated space there but we won't be using that today.

Select the /dev/sdd device and click Add GPT Partition to create our Nextcloud file system.

We are going to leave the size blank to use the entire disk for the partition and this will be formatted as btrfs as this is an excellent choice for application files and documents, especially the built-in snapshot capability. The mount point is set to Other and specified as /srv/nextcloud. When you're ready select Create button.
During the preparation playbooks we're going to reconfigure this to use btrfs subvolumes, unfortunately this version of Subiquity doesn't support doing that. By default Nextcloud would install itself under /var, but I prefer to keep server applications under /srv so I can easily see what's going on. The /var filesystem really should be for the internals of keeping applications running, not application data but it's a fine distinction between the two and again its a subject of contention.
We should now be able to see the entire filesystem configuration for the virtual machine.

Check through just to be sure and then select Done.
As noted there's one variance with the spec around the LVM configuration using raw disk and not bothering with a partition but apart from that we're good. And that might well have to be a permanent change to the spec.

Subiquity is now ready to start installing Ubuntu and there is no going back once this starts. While there's no risk of data loss on a brand new virtual machine, select Continue only if you're sure with what you've selected so far.
The next step is to define an initial administration user.

This user will be able to become root via the sudo command so it is quite a powerful user and we'll be using it during the first playbook to create a dedicated Ansible user account so you need to know the credentials. Root login is disabled by default as a security measure although you can enable that later if you wish. The "Your name" field is the friendly name for this initial user and can be anything you like.
The hostname here will also be the initial hostname for for all VMs created from the template and will need to be known to the configuration playbooks as one of their first tasks will be to change it to the desired hostname for the VM being built. The username and password also need to be passed to the configuration playbooks so the password in particular should be complex. When you're happy with these setting select Done and we'll move on to SSH configuration.
For the record, this has been edited for display and is not what I'm using in my real playbooks just in case anyone gets funny ideas.

We definitely need to install OpenSSH server on here to enable our Ansible playbooks to connect so select the option. I don't want to import keys at this stage as I handle SSH configuration in the playbooks and I use a private certificate authority anyway so while I select No to the Import SSH identity option you may want to do your own thing here if you have keys in Github or Ubuntu Launchpad. Select Done when ready to move on to some advertising.

This screen is all just advertising really, we don't need any of these - not even the Nextcloud snap - so keep hitting down arrow untill you get to Done and then hit enter.

By the time you get to this screen installation is probably completed and it's downloading all the security updates since the installation ISO you used was released. I generally cancel at this point and after a few minutes force a VM restart and do the patches later so that I'm starting from a known state but for this template I let it apply all the patches and I've not noticed anything breaking in the Ansible playbooks that will follow, which is nice. You'll be waiting a while though.

Eventually the security updates will be completed and rather than put up a nice "We're finished" banner, the option to Cancel update and reboot changes to simply Reboot. Which is a bit too subtle if you ask me. But anyways, at this point Ubuntu is installed so select the Reboot option.
During the first startup Ubuntu completes a number of initialisation steps so wait a few minutes for them to complete before doing a first login.

Verify Installation
Once it's reached the Cloud-init target state it's time to do an initial login using the admin credentials we created during the installation and do a quick check that:
- We can actually login to the host via console and SSH
- use df -h to verify that we have the filesystems mounted as we specified, plus a whole load of tempfs and other internal ones.
- A quick check of top should show the RAM and swap amounts are close to what we specifed - some RAM will be lost in hypervisor overhead
- Use lscpu to verfiy we have the correct number of CPUs assigned
At this point I shut the virtual machine down and exported it to an OVF template and this is continued in VM Template Creation part 3
Future Thoughts
With the benefit of practical experience I could have made things a lot simpler for the Ansible Playbooks that will follow. Rather than having them complete all of the installation and configuration steps each time, I could have done them in the template and leave the scripts to just do final configuration and apply updates. The downside is this would mean a much tighter coupling between the template and the scripts of course, which is the reason I didn't go that route initially. Isn't hindsight wonderful?
I'll go into the detail of these in the next few articles as they occur, but to bring them all into one place there could be a post-install config to:
- Configure repositories for PHP and MariaDB installation
- Apply as many of my standard configurables as possible
- Pre-install as many applications as possible that don't depend on hostname or other configurables being finalised. Playbooks would still apply configuration though.
- Remove the machine id to force it to regenerate on restart / template import
- Create a bootstrap user with SSH key so I don't have to supply credentials for an initial login, and delete the user during configuration and replace it a different an potentially host-specific admin user.
Pre-installing the Apache2, MariaDB and PHP packages along with my standard apps will be a big timesaver, no need to wait for apt to do its thing each time. And it might allow deployments to be run in closed labs or if something has broken my internet access. cough Plusnet DNS cough.