Recently, out of boredom and possibly sheer hatred for my weekend, I decided to install Gentoo GNU/Linux. I've installed it before, and I was looking for an operating system I would really tinker with. When the time came to choose a bootloader, I was stuck and unsure of which one to pick. This indecision lead me to pick one I had never tried before, as I was in the mood for some expirimentation. So I ended up choosing EFIstub, a "bootloader" that seemed very graceful and neat conceptually, and I was right.
For those unaware, on a UEFI system, after the computer has performed its tests and checks, the firmware executes the bootloader and passes control over to it. From there, the bootloader executes the kernel (or the initramfs), which then does its thing, and passes the torch to whichever init system that distro is using, which loads all the neccesary services and programs for the computer to function as the user intends.
While the UEFI>Bootloader>Kernel>init process makes sense and works for the vast majority of systems, there are ways you can trim this process significantly. If you decided to use an initramfs or a Unified Kernel Image (UKI), you can actually tell the uefi firmware to execute one of those binaries directly, and thus using the system firmware as a bootloader! But first,
The initramfs is a compressed (usually cpio) file that contains the linux kernel and drivers necessary for the kernel to find and mount the root device, such as file system drivers, sata controller drivers, and whatnot. Once the initramfs is booted, it locates the root drive/partition, mounts it, then continues through initializing the rest of the hardware and then executing the init system.
A UKI is similar, and is best defined by well, the organization that defined it, who defined it as "a combination of an UEFI boot stub program, a Linux kernel image, an optional initrd, and further resources in a single UEFI PE file." In somewhat laymans terms, this means the kernel, initramfs discussed earlier, and other utilies like the kernel command line (which we will discuss later), are loaded into one neat executable file that can be executed either by a bootloader, such as grub, or depending on the system, by the UEFI firmware itself, For the purpose of "Stub booting". I decided to go this route.
(This guide is for OpenRC systems)
First off, once you decide to use EFI stub booting, you need to make sure your esp (efi system partition) is mounted to the /efi directory, and formatted in a fat filesystem such as fat32. This is where you'll point the UEFI firmware to for the UKI, and its important that its in a location and a file system the firmware understands. Once said partition is created, formatted and mounted, create the following directory tree: /efi/EFI/Gentoo.
Once the partitioning is done and directories created, emerge the following programs:
Once the packages have been emerged, its time to do some configuration. First, write the following to /etc/portage/package.use/uki
sys-apps/systemd-utils boot kernel-install sys-kernel/installkernel dracut uki
Only the systemd stub booter is available on gentoo as of the time of this writing, so we'll go with that, which is why the first line of that file is there. The second line tells sys-kernel/installkernel to generate an initramfs and UKI with dracut.
Once that file is saved, emerge the sys-apps/systemd-utils package.
After that's done, its now time to configure dracut with the options neccesary to generate a functional UKI. in /etc/dracut.conf.d/uki.conf, place the following text:
uefi="yes" kernel_cmdline="some-kernel-command-line-arguments"
As you can probably imagine, the kernel command line arguments are quite important, as they're the arguments passed down to the kernel that allows it to orient itself. Don't worry, aside from some user preference commands, you only really need one command line argument, the one that tells the kernel where your root partition is.
First, simply run blkid as root, and copy the uuid of whichever partition you chose as your root partition. If you cant remember which one you mounted as your root partition, simply run lsblk and find out.
Once you have the UUID (not PARTUUID) of your root partition, enter it into the kernel_cmdline section of /etc/dracut.conf.d/uki.conf with UUID replaced with ROOT, as well as other command line arguments as wanted.
An example of what it should look like, with the UUID of my root partition used as an example:
uefi="yes" kernel_cmdline=" root=UUID=7986c07d-b821-49bb-a3ab-066657166813 "
Note: The leading and trailing spaces around the arguments are important, do not forget them.
Once all this configuration has been laid out, go ahead and emerge the sys-kernel/installkernel package again, now everytime this package is emerged, such as during a kernel upgrade, an initramfs and a UKI will be created and put into the proper directory.
From here, continue on to configure the kernel and the rest of the system, you'll come back to the bootloader at the end of the installation process.
Once you've installed and compiled the kernel of your choice, its now time to actually get the UKI and the UEFI firmware ready to boot. This is done by adding th efistub use flag to the sys-kernel/installkernel package. This flag is masked for stability, because as with most things, there's always some systems that don't properly follow the standards laid out for UEFI, and as such not all computers can boot off a stub.
To unmask the efistub use flag, enter the following text into /etc/portage/package.accept_keywords/installkernel:
sys-kernel/installkernel sys-boot/uefi-mkconfig app-emulation/virt-firmware
After you unmasked the proper flags, you can then put the efistub flag into the installkernels package.use file, located at /etc/portage/package.use/installkernel.
sys-kernel/installkernel efistub
From here, reemerge the sys-kernel/installkernel package, as well as the kernel in whichever method you chose, be that manually, or a distribution kernel.
Since sys-kernel/installkernel was configured to generate UKIs, it will be placed into the /efi/EFI/Linux directory on your system. You'll then use the efibootmgr utility to add a UEFI entry to your computers firmware. (NOTE: Make sure you change the settings to this command to match your system, such as the drive name and partition number, DO NOT just copy and paste!)
$ efibootmgr --create --disk /dev/sda --part 1 --label "gentoo" --loader "\EFI\Linux\gentoo-x.y.z.efi"
Note that you will not inclue the top level /efi directory in the command, remember to use the backslash (\) when writing the file path.
From here, you can now hold your breath, cross your fingers and reboot, if you get to the tty, then congrats! your computer now technically boots without a bootloader, instead having the firmware execute the kernel directly, pat yourself on the back for your amazing computer skills.
Is this necessary? Absolutely not. Would I reccomend the average Linux user do this? Absolutely not. However, if you're up for a bit of tinkering, and some boot/kernel level shenanigans, and are willing to troubleshoot when things go wrong, I think it's a fun exercise, and hey, my computer boots slightly faster now.