With the summer hiatus coming to an end I've picked up on my last round of deployment automation with an update to the Nextcloud VM deployer to use LUKS encryption of the filesystems for the MariaDB database and Nextcloud itself and also updated to use Ubuntu 24.04 LTS. I need the server to boot without me having to type in passwords and for that I'm using clevis and tang so I don't need to store any decrytion keys on VM itself. This was working nicely untill I configured the MariaDB file system to use LVM2.
This has taken a few evenings to fix, I will write up the full how-to in due course.
I've used LUKS with clevis for data disks on the VMs for a while, all of them using btrfs filesystems inside the LUKS partition and clevis has just worked. I've taken to using btrfs as it performs well, it can grow and shrink if needed but the big advantage is supports near-instant snapshots so the Nextcloud server only needs to be paused for a second or so to get an application-consistent backup - the backup server then takes a backup of the snapshot. But btrfs does not work well with databases or applications that makes frequent writes to the same files, so I need to use something else.
My preferences for the MariaDB filesytem is XFS or ye goode olde ext4. But neither of these support snapshots which I may want in future, but that can be done via LVM2 Logical Volumes. So we end up with a more complicated setup
| XFS - /var/lib/mysql |
| LVM2 Logical Volume - lvMariaDB |
| LVM2 Volume Group - vgMariaDB |
| LVM2 Physical Volume - db_crypt |
| LUKS partition - /dev/sdd1 |
| Virtual Disk |
I won't go into the full setup here, but the issue wasn't the building but rather it refused to automatically decrypt during boot. The decryption and mounting of the disks is controlled by two files, /etc/fstab and /etc/crypttab. To improve resilience I confgure the crypttab file to use UUIDs to recognise devices which makes it less readable but means if I goof adding more disks later and the normal device names change it won't mess up.
A simple config is something like this:
File: /etc/fstab
/dev/mapper/app_crypt /srv/nextcloud btrfs defaults,nofail,_netdev 0 2
/dev/mapper/db_crypt /var/lib/mysql xfs defaults,nofail,_netdev 0 2
File: /etc/crypttab
app_crypt UUID=01234567-0123-0123-0123-000000005dc1 none luks,_netdev
db_crypt UUID=01234567-0123-0123-0123-000000005dd1 none luks,_netdev
During boot each disk should be identified as a LUKS volume, crypttab is consulted and volume opened with clevis triggered to assemble the key using the tang servers and make the opened volume appear under /dev/mapper. The LUKS option isn't really needed, the _netdev tells it to wait untill the network is available since it needs to make a call to the configured tang servers to assemble the unlocking key.
For some reason when the db_crypt volume is formatted as an LVM2 physcial volume (PV) this is ignored during startup and eventually systemd gives up with "Timed out waiting for device dev-mapper-vgMariaDB\x2dlvMariaDB.device - /dev/mapper/vgMariaDB-lvMariaDB" error.
During the boot I see the call to clevis-luks-askpass.path when it unlocks app_crypt, but it doesn't do the same for db_crypt which explains why it cannot then see the LVM2 config. If I use a key file (replace none with the path to a file) then it works fine, so this is definitely a clevis triggering issue rather than a fundamental problem with config or LUKS+LVM2. And I couldn't find anything in the various bits of documentation on the inetwebs so I decided to strip out the clevis config step by step and see if that gave any other errors.
The first step was to remove the _netdev from crypttab for the db_crypt volume and reboot.
File: /etc/crypttab
app_crypt UUID=01234567-0123-0123-0123-000000005dc1 none luks,_netdev
db_crypt UUID=01234567-0123-0123-0123-000000005dd1 none luks
On the console screen I saw the usual prompt message for the app_crypt decryption which was immediately supplied by clevis, and then two prompts for db_crypt which it had not done before. And it then mounted the volumes.
So I'm calling that a win. I don't know why it's doing that - _netdev is only supposed to be a flag to say wait for network. I then found that _netdev isn't needed for the app_crypt volume either but equally it doesn't seem to cause any issues if it's added. For consistency I've removed if from both entries and not had any issues since.