Monday, May 28, 2012

EFI Stub - booting without a bootloader

In this post I'm detailing a quick and easy method to use EFI Stub boot introduced in the Linux kernel version 3.3.0 that provides for a faster boot without the use of a bootloader (such as grub) on a UEFI capable system.
The steps are what I did using my Gentoo installation which means I needed to customize my kernel to support this process. You may find that this step isn't necessary with some other modern distros.

We need to configure the kernel with the following:

CONFIG_EFI=y
CONFIG_RELOCATABLE=y
CONFIG_FB_EFI=y
CONFIG_FRAMEBUFFER_CONSOLE=y
CONFIG_EFI_PARTITION=y
CONFIG_EFI_VARS=m
CONFIG_EFI_STUB=y

Also it simplifies to add any kernel command-line parameters that you pass with your current bootloader to the "Built-in kernel command line". Mine are:
CONFIG_CMDLINE="root=/dev/sda6"

Also note that I don't use an initramfs, if you do you might check here for some hints: archlinux post 1090040.


Recompile and install the updated kernel. I also suggest testing the built-in parameters. In my case I just made another grub entry leaving off the "root=" entry to make sure I could still properly boot:

title=Gentoo Sources 3.4.0-gentoo built-in
root (hd0,0)
kernel /boot/kernel-3.4.0-gentoo


title=Gentoo Sources 3.4.0-gentoo
root (hd0,0)
kernel /boot/kernel-3.4.0-gentoo root=/dev/sda6

We're still booting with our bootloader but we now know the kernel command line we embedded is working fine.

We need the efibootmgr package installed if it isn't already. Check and do this now if necessary.

It's like this: "The first rule of Fight Club...", unless we boot using UEFI we cannot instruct our UEFI BIOS to do so from our hard drive. Here we make our own boot medium so this this can be accomplished. If you already have such media you can use it and skip ahead. The cool thing here is that you'll be booting into your system using your kernel, just not from the hard drive via UEFI yet.

Prepare a USB stick with a small (you'll need room for your kernel, any initramfs, and the efi shell if desired) FAT32 partition, type EF00 (GPT - I used gdisk) or ef (MBR). Mount this new partition, we'll use sde2 as the example and /mnt/temp as a temporary mount point (use whatever you normally do). Also I'm specifically using data from my own 64 bit Gentoo install - make the proper translations for your particular system:
mount /dev/sde2 /mnt/temp
cd /mnt/temp
mkdir -p efi/boot
cd efi/boot
cp /boot/kernel-3.4.0-gentoo bootx64.efi

Now we reboot, go into our UEFI BIOS and tell it to boot from using UEFI mode from our USB stick.

Wow, that was easy. We booted without a bootloader!

Now we need to get the same results from our hard drive. The same type/format partition is necessary, so either find space or make space, however you choose, for a FAT32 partition of type EF00/ef. In my case it was /dev/sda8:
mount /dev/sda8 /mnt/temp
cd /mnt/temp
mkdir -p efi/gentoo
cd efi/gentoo
cp /boot/kernel-3.4.0-gentoo gs340x64.efi

Now we have our kernel in a place the UEFI BIOS can see it.

It's time to instruct the UEFI BIOS to boot from the Kernel Stub on our hard drive (note that we must have booted via UEFI mode for the following to work - which is one reason we went through the USB stick step):
modprobe efivars
efibootmgr --create --part 8 --label "Gentoo-3.4.0" --loader '\efi\gentoo\gs340x64.efi

Be careful - efibootmgr defaults to /dev/sda which works fine in my case but see "man efibootmgr" for details before continuing.

Shut down your system,. remove the USB stick, and enjoy fast booting without a bootloader!

To learn more about EFI booting see: Rod Smith's page.



4 comments:

  1. Great post! (very useful for me). Big thank you :)

    ReplyDelete
  2. Thanks very much for your post!!!

    ReplyDelete
  3. Just curious why, given that you got the kernel to boot from the USB stick, you took the additional steps of invoking efibootmgr once you moved the kernel to the hard drive? Wouldn't it boot without that?

    ReplyDelete
  4. https://forums.gentoo.org/viewtopic-p-7965444.html#7965444
    The above link includes tips for direct UEFI booting from a /dev/nvme0n1p3 /root partition.
    I had some difficulty getting the 4.4.6-Gentoo kernel to boot to my .m2 drive.

    Otherwise this page was a useful breadcrumb in getting my week long journey to get Gentoo to boot on my ASROCK PRO4 H170M MSB with intel 128G .m2 SSD

    ReplyDelete

Note: Only a member of this blog may post a comment.