Nazeem.Me

A blog about technology, football and all the other random stuff in my life

Adding memtest86+ to systemd-boot with Secure Boot on

Written by

in

,

·

Three steps on paper: install it, sign it, add an entry. Then systemd-boot put it at the top of the menu twice, for reasons I hadn’t expected.

I had a new RAM kit to test on my desktop, gilgalad, and wanted memtest86+ in the boot menu rather than on a USB stick. gilgalad runs CachyOS with systemd-boot as the boot loader, and Secure Boot is on with my own keys, managed by sbctl. So there were three things to do: install memtest86+, sign it so Secure Boot would load it, and add a menu entry.

Here’s the recipe that worked, then the parts that didn’t go to plan.

The recipe

1. Install the EFI build.

sudo pacman -S memtest86+-efi

On Arch and CachyOS this puts the binary at /boot/memtest86+/memtest86x64.efi. Check the filename with pacman -Ql memtest86+-efi before writing the entry. I assumed it was memtest.efi, and my first attempt stopped at the file check.

2. Add a boot entry. Create /boot/loader/entries/diag-memtest86+.conf:

title    Memtest86+
efi      /memtest86+/memtest86x64.efi

The diag- prefix matters. More on that below.

3. Sign it for Secure Boot.

sudo sbctl sign -s /boot/memtest86+/memtest86x64.efi

The -s saves the file to sbctl’s database, so the pacman hook re-signs it when the package updates. Without it, the next memtest86+ update would leave an unsigned binary that Secure Boot refuses to load.

4. Boot it once.

systemctl reboot --boot-loader-entry=diag-memtest86+.conf

This boots memtest86+ for one boot only. The next boot goes back to the normal default. You can also pick it from the menu.

The menu order that fought back

I wanted memtest86+ at the bottom of the menu, below the two CachyOS kernels. It took three attempts.

Attempt 1: a sort key. systemd-boot entries can have a sort-key line, so I added sort-key zz-memtest, expecting “zz” to sort last. It went to the top.

The reason: systemd-boot puts any entry that has a sort-key ahead of every entry that doesn’t. CachyOS’s kernel entries don’t have one. So by adding a sort key I’d guaranteed it would come first, whatever its value.

Attempt 2: no sort key. I removed the line. It stayed at the top.

Without a sort key, systemd-boot sorts entries by filename, but in descending order, so later names come first. “memtest86+” comes after “linux-cachyos” alphabetically, so it sorted first.

Attempt 3: a leading zero. I renamed it 0-memtest86+.conf, thinking a digit would sort before letters. It stayed at the top.

systemd-boot doesn’t compare names as plain text. It uses a version-style comparison, where digits count as higher than letters. Combined with the descending order, a name starting with a digit goes to the top as well.

What worked: diag-memtest86+.conf. “diag” sorts before “linux-cachyos-lts”, and in descending order that puts it last.

Filenamesort-keyPosition
memtest86+.confzz-memtestTop
memtest86+.confnoneTop
0-memtest86+.confnoneTop
diag-memtest86+.confnoneBottom

Check your default entry

Menu order shouldn’t decide what boots by default, as long as the default line in loader.conf names a real entry. Mine didn’t quite. CachyOS had written default linux-cachyos, but an entry’s id is its full filename, linux-cachyos.conf. A default that matches no entry falls back to the first entry in the menu.

For a long time that fallback hid the problem, because the first entry happened to be CachyOS. I only found it a week later, setting up CachyOS’s new recovery image on another machine. The recovery image carries a sort key, so it went to the top of the menu, and bootctl list moved (default) onto it. The next unattended boot would have gone into recovery.

The fix is one word:

sudo sed -i 's/^default linux-cachyos$/default linux-cachyos.conf/' /boot/loader/loader.conf

sdboot-manage only writes loader.conf when it’s first installed, so the change stays. After any change to the menu, run sudo bootctl list and check (default) is on the entry you expect.

Smaller gotchas

bootctl list shows stale names. As well as the entry files on disk, it lists the entries the boot loader reported at the last boot. After each rename, the old name still showed up at the bottom until I rebooted. Entries that exist on disk are listed first, in the real order.

CachyOS’s entry manager leaves it alone. CachyOS generates kernel entries with sdboot-manage. I checked the script: with REMOVE_EXISTING="no" (the default), it only removes entries for kernels that are no longer installed, and it finds those by their linux line. A memtest entry uses an efi line, so kernel updates don’t touch it.

/boot is root-only. On my install the ESP is mounted with permissions 700. Even bootctl list can’t read the entries without root.

What it was for

memtest86+ passed the new kit. The kit was faulty anyway, and it took a heavier test to show it. That’s a separate story: memtest86+ said my new RAM was fine. It wasn’t.

The memtest86+ entry stays. It’s still the quickest first check when a machine starts acting up, as long as you don’t treat a pass as the final word.