New Grub option GRUB_TOP_LEVEL

The latest grub update introduces a new feature, to explicitly select an entry of the installation (among linux, linux-lts, linux-zen, etc.) being at the top level of the menu. In case you use submenus, the selected entry is the one in the front/main page, while it is also at the top in the Advanced menu entries page.

A user may wish to use an image that is not sorted as the "latest" version as the top-level entry. For example, in Arch Linux, if a user has the LTS and regular kernels installed, "/boot/vmlinuz-linux-lts" gets sorted as the "latest" compared to "/boot/vmlinuz-linux", meaning the LTS kernel becomes the top-level entry. However, a user may wish to use the regular kernel as the top-level default with the LTS only existing as a backup.

This need can be seen in Arch Linux's AUR with two user-submitted packages (grub-linux-default-hook, grub-linux-rt-default-hook) providing an update hook which patches /etc/grub.d/10_linux to move the desired kernel to the top-level. This patch serves to solve this in a more generic way.

Introduce the GRUB_TOP_LEVEL, GRUB_TOP_LEVEL_XEN and GRUB_TOP_LEVEL_OS_PROBER variables to allow users to specify the top-level entry.

Create grub_move_to_front() as a helper function which moves entries to the front of a list. This function does the heavy lifting of moving the menu entry to the front in each script.

In 10_netbsd, since there isn't an explicit list variable, extract the items that are being iterated through into a list so that we can optionally apply grub_move_to_front() to the list before the loop.

The online grub manual is not updated with these options.
The relevant change in the manual is this:


This option should be an absolute path to a kernel image. If provided, the image specified will be made the top-level entry if it is found in the scan.


This option should be a line of output from os-prober. As GRUB_TOP_LEVEL, if provided, the image specified will be made the top-level entry if it is found in the scan.

For example (tested on my system), you can add this line in /etc/default/grub, to set Linux main kernel at the top of the menu list:


Re-create grub configuration to apply the changes:

sudo grub-mkconfig -o /boot/grub/grub.cfg

Adjust the value to your specific "absolute path to a kernel image" that you want to show at the top.

I haven't tested the os-prober option, but I assume it would be something like this (it's copied from sudo os-prober output):

GRUB_TOP_LEVEL_OS_PROBER="/dev/sda8:Archcraft (rolling):archcraft:linux"