3 AUTHOR: Dan McGhee, Kevin M. Buckley, and Xi Ruoyao
7 LICENSE: GNU Free Documentation License Version 1.2
9 SYNOPSIS: Boot LFS by default in a UEFI Environment using GRUB
12 This hint contains the information to direct the OS Boot Manager to default
13 to the GRUB in a UEFI environment employing EFI Mode. This hint applies to
16 This version updates Dan McGhee and Kevin M. Buckley's original, dated
19 The 2018-04-09 update saw the UEFI packages built against an LFS 8.2 systemd
20 installation that was already being booted using the existing host system's
27 * Base LFS system before or after Ch. 8
28 * Basic understanding of obtaining and building packages
32 DISCLAIMER: The recipes in this hint neither supplant nor supersede the
33 build instructions in a stable version of either the LFS or BLFS books.
34 They merely augment them for newer firmware. If conflicts arise between
35 this hint and the instructions in the book, take the issue to the mailing
36 lists. Additionally, this hint applies to only x86_64 machines packaged
37 with Windows 7 or Windows 8. The recipes here can be used on Mac OS, but
38 have not been investigated at the initial writing of this hint.
40 The 2018-04-09 hint refers to an LFS 8.2 system, built onto an x86_64
41 machine from within a LFS 7.9 host, that had had never had a version of
42 windows installed on it, indeed the host contained one EFI directories
43 below `/boot/efi/EFI/`, namely `boot`, that having been installed by
44 the vendor of the computer.
46 USE OF TERMS: The following is a use of terms in this hint. Further
47 information for and amplification of them can be found in References 1-3.
49 * Firmware Settings: An interface accessed by the keyboard after power
50 is applied but before boot. In it a user can change the order and
51 way of how the computer boots.
53 * BIOS System: Firmware with EFI Mode turned off.
55 * EFI Mode: A condition of the booted system in which the EFI partition is
56 mounted and the uefi (efi) variable support in the kernel is working
57 properly. It results from enabling UEFI Mode in the firmware settings.
59 * EFI Mount Point: A user defined mount point for the EFI Partition. In
60 this hint, and in most distros, it is `/boot/efi`.
62 * EFI Partition: A small partition, usually before any other partitions;
63 i.e., `/dev/sda1` of 200-250 Mb, formatted in FAT32 with the `boot` flag, in
64 parted, or `ef00` (EF00) partition type in gdisk. (NOTE: The `boot` flag has a
65 different function and meaning in MBR partitioned disks.)
67 * efi variables (synonymous: uefi variables): variables through which the
68 operating system can interact with the firmware.
70 * Legacy Boot Option (Legacy Boot): A boot process in the firmware settings
71 where EFI Mode is ddisabled..
73 * GUID Partition Table (GPT): A modern partitioning scheme that supports
74 large disk drives and more flexibilty than older partitioning schemes.
76 PRELIMINARY DISCUSSION: Additional information and more in depth
77 discussion of the following concepts can be found using References 1-3.
79 Booting LFS is no longer as simple as `grub-install /dev/sda`. There are
80 more options and more considerations. With the advent and proliferation of
81 UEFI firmware, a user's knowledge and philosophy of the boot
82 process requires expansion:
84 1. GPT partitioning is different from MBR partitioning.
85 Programs fdisk, cfdisk, parted, or gdisk (from
86 gptfdisk) can be used. Each has their pros and cons,
87 supporters and detractors.
88 2. UEFI mode uses Boot Managers to select Boot Loaders like GRUB or
90 3. The Boot Loaders are placed on the EFI partition rather than the
91 MBR. This concept is similar and parallel to the LFS procedures of
92 using a separate `/boot` partition.
93 4. There are additional tools that LFS needs in order to accomplish
95 5. LFS can be built and booted as the instructions are written up to
96 and including LFS-8.4. To do this, the firmware settings must be
97 changed to disable EFI mode. Different hardware vendors may use
98 different terminology or menu options to interface with EFI options.
100 Note that the only operating system that requires EFI mode is Microsoft
101 Windows. If that operating system is not installed, then it is generally
102 easier to disable EFI mode and not use the rest of this hint. Some
103 systems may also require removing the efi partition on the boot
104 drive in addition to changing firmware settings to completely disable
107 One of the hugely discussed issues surrounding UEFI is Secure Boot. It is
108 necessary to understand that the terms "UEFI" and "Secure Boot" are NOT
109 synonymous. UEFI is firmware. Secure Boot is a process of using "keys" to
110 "guarantee" the safety and authenticity of a Boot Loader. NOTE: To use
111 the procedures in this hint, Secure Boot must be disabled in the BIOS Boot
114 Please note that the recommended order for implementing these instructions is a
115 departure from the build order in LFS. The most convenient, and arguably the
116 most practical way, to implement the instructions here is to use them in the
117 build of an LFS System at the end of Ch. 6. Building the BLFS and non-BLFS
118 packages has been tested both inside and outside of the chroot environment.
119 Then, following the book, proceed through Ch. 7, returning to the instructions
120 in Ch. 8. The instructions are presented in that order.
122 The most inconvenient way to implement these procedures is in a completely
123 functional LFS-8.4, or earlier, system. This involves uninstalling
124 `GRUB-2.02`, removing it from its location as a result of `grub-install` and
125 implementing the instructions below. Migrating from Legacy Boot to UEFI boot is
126 possible. At the writing of this hint, however, it is not
127 included. References 1-3 contain more information on this subject.
129 The last consideration in implementing the instructions here is GRUB's
130 graphical terminal. In UEFI systems, if the GRUB video mode is not
131 initialized, no kernel boot messages will appear until the kernel video takes
132 over. The GRUB package does not supply fonts, and GRUB defaults to
133 `unicode.pf2`. There are two ways to supply this font. The first is to copy
134 `unicode.pf2` from the host system to `/boot/grub` on the LFS system. The
135 second method involves configuring grub to build grub-mkfont, and this creates
136 a build dependency of `FreeType` for GRUB. This hint addresses the second
139 Finally, as of the writing of this hint, there is no standard for
140 the use of UEFI and the implementation of Secure Boot. These are hugely
141 manufacturer dependent. This hint uses terms used in the original author's
142 hardware. They may be different in other manufacturers' implementations.
143 However, the capabilities to do the boot setup operations contained in this
144 hint will exist on each machine. The terms may differ, and more than one
145 operation might be needed to achieve a desired goal. For example, someone
146 may need to disable Secure Boot and remove Secure Keys.
149 [NOTE] The instructions are written with the assumption that the packages are
150 being built in the chroot environment before the end of Ch. 8. They can be
151 modified, with little difficulty, to be used in a functional system.
154 Before entering the chroot environment, check that the host booted in
159 If this directory exists and is populated, the host booted in EFI Mode.
161 CREATE AND FORMAT NEW EFI PARTITION, IF NECESSARY
162 Some firmware (for example those in Dell desktops) has a reserved,
163 invisible EFI partition for its OEM Windows. If it's the first time you
164 install another EFI booted OS on the system, create a new EFI partition
165 using `fdisk` or your prefered partition tool. Then, mark it with the
168 If you created a new EFI partition, install `dosfstools` from BLFS and
169 format the new EFI partition with:
171 mkfs.vfat /dev/sda(x)
173 where sda(x) is the device containing the EFI partition.
176 Determine which device is the EFI partition using gdisk or parted,
177 enter the chroot environment, create `/boot/efi` if needed, and
179 mount -vt vfat /dev/sda(x) /boot/efi
181 where sda(x) is the device containing the EFI partition.
185 Install BLFS packages `dosfstools` and `popt`, using the instructions in
186 the book. Build and install `FreeType` if building grub with `grub-mkfont`
189 The BLFS `FreeType` instructions recommend that it be built after
190 `which` and `libpng` have been installed, so it was, however, as the
191 recommendation for "HarfBuzz" notes that one builds `FreeType` without
192 it first, and then do a re-install, it wasn't thought necessary to do
195 The `libpng` install did include the "apng" patch.
200 * https://github.com/rhboot/efivar/releases/download/37/efivar-37.tar.bz2
202 http://svn.linuxfromscratch.org/patches/trunk/efivar/efivar-37-gcc_9-1.patch
204 Apply a patch to make some fixes required by gcc-9:
206 patch -Np1 -i ../efivar-37-gcc_9-1.patch
210 make LIBDIR=/usr/lib BINDIR=/bin
212 The meaning of the make parameter:
214 * `libdir=/usr/lib`: This option overrides the default library directory
215 of the package (`/usr/lib64`).
217 Despite the Makefile having a `test` target, albeit one which isn't run
218 by default, you SHOULD NOT run that `make test`, as it has been found
219 to cause firmware bugs. Here are the thoughts on, and the exhortation
220 not to do, this from the `efivar` community:
221 https://github.com/rhboot/efivar/issues/78 .
225 Now as the `root` user:
227 make LIBDIR=/usr/lib BINDIR=/bin install
229 Move the shared libraries to the /lib directory, and fix the symbolic
232 mv -v /usr/lib/lib{efivar,efiboot}.so.* /lib
233 ln -sfv ../../lib/$(readlink /usr/lib/libefivar.so) /usr/lib/libefivar.so
234 ln -sfv ../../lib/$(readlink /usr/lib/libefiboot.so) /usr/lib/libefiboot.so
239 * Required: `popt` and `efivar`
242 * https://github.com/rhboot/efibootmgr/archive/17/efibootmgr-17.tar.gz
244 Fix an outdated hotfix declaration causing FTBFS:
246 sed -e '/extern int efi_set_verbose/d' -i src/efibootmgr.c
250 make sbindir=/sbin EFIDIR=LFS EFI_LOADER=grubx64.efi
252 The meaning of the make parameters:
254 * `EFIDIR=LFS`: This is the distro's subdirectory name under
255 `/boot/efi/EFI`. `Make.default` file of `efibootmgr` need this variable
257 * `EFI_LOADER=grubx64.efi`: This variable is set to the default EFI boot
258 loader. It affects the default of parameter `--loader`. You can skip
259 this if you won't invoke `efibootmgr` directly and only `grub-install`
264 Now as the `root` user:
266 make sbindir=/sbin EFIDIR=LFS EFI_LOADER=grubx64.efi install
270 Linking efibootmgr dynamically against popt preduces runtime dependency
271 to libpopt.so, which resides in /usr hierarchy. If /usr is a seperate
272 mount point and you wish to use efibootmgr in case /usr is not avaliable
273 (for example, to rescue a broken system), move popt libraries as follows:
275 mv -v /usr/lib/libpopt.so.* /lib
276 ln -sfv ../../lib/$(readlink /usr/lib/libpopt.so) /usr/lib/libpopt.so
281 * http://unifoundry.com/pub/unifont/unifont-13.0.03/font-builds/unifont-13.0.03.pcf.gz
285 [NOTE] This package is not a tarball. So DON'T (and you can't)
286 `tar -xf` it and change to the unzipped directory like normal LFS/BLFS
291 mkdir -pv /usr/share/fonts/unifont &&
292 gunzip -c unifont-13.0.03.pcf.gz > \
293 /usr/share/fonts/unifont/unifont.pcf
298 * Optional: `FreeType` (for `grub-mkfont` and `unicode.pf2`)
299 * Optional: `unifont` (for `unicode.pf2`)
301 [NOTE] The 2017-02-07 hint installs `unicode.pf2` manually.
302 However, if `freetype` and `unifont` are both installed, GRUB
303 will build `unicode.pf2`, and `grub-install` will install the
304 file automatically. This is recommended in 2018-04-09 hint.
307 * https://ftp.gnu.org/gnu/grub/grub-2.04.tar.xz
309 Prepare for compilation:
311 Unset custom optimization flags, since speeding up GRUB is not very useful
312 and they are known to cause FTBFS:
314 unset {C,CXX,LD}FLAGS
316 [NOTE] Some options in 2017-02-07 hint are no longer necessary.
318 ./configure --prefix=/usr \
322 --enable-grub-mkfont \
323 --with-platform=efi \
326 The meaning of configure options:
328 * `--enable-grub-mkfont`: This ensures `grub-mkfont` to be built.
329 If `Freetype` is not installed, remove this option and then you
330 have to get `unicode.pf2` from other sources (either the host or
333 * `--with-platform=efi`: This ensures `grub` to be built for EFI.
335 If the optional dependencies are installed, `configure` should
336 output the following information at last:
339 Build-time grub-mkfont: Yes
340 With unifont from /usr/share/fonts/unifont/unifont.pcf
342 That means `unicode.pf2` would be built and used.
350 Now as the `root` user:
353 mv -v /etc/bash_completion.d/grub /usr/share/bash-completion/completions
355 MODIFICATION OF /etc/fstab
357 When constructing `/etc/fstab` in LFS chapter 8, add a line to
360 /dev/<name of EFI partition> /boot/efi vfat defaults 0 1
362 Systemd would mount `efivarfs` automatically. If using sysvinit,
363 add another line to mount `efivarfs`:
365 efivarfs /sys/firmware/efi/efivars efivarfs defaults 0 1
369 a) If you are going to be booting your UEFI-aware LFS system using a
370 non-LFS GRUB from your host AND if that GRUB is one (eg Fedora)
371 that allows for the kernel to be specified using that GRUB's
372 `linuxefi` attribute, so
374 linuxefi /path/to/kernel root=/path/to/root ro
376 then you don't appear to need the `/etc/fstab` line, and indeed,
377 you'll get told during the boot that the mounter knows nothing
378 about the efivars filesystem type. However, LFS's efibootmgr will
379 still be capable of interrogating your UEFI environment.
381 b) If the LFS system is booted from the LFS+Hint's grub, which doesn't
382 appear to know about the "linuxefi" attribute so using
384 linux /path/to/kernel root=/path/to/root ro
386 then, unless you have the efivars filesystem mounted, and you are
387 able to, then LFS's efibootmgr will be **not** capable of interrogating
388 your UEFI environment, and you'll be told that there is no `efivars`
391 KERNEL CONFIGURATION OPTIONS FOR EFI
393 The LFS kernel build's `make defconfig` populated a good number of
394 the EFI-related options on my UEFI-enabled hardware, however, so as to
395 make the 2014-10-16 hint's list of settings easier to find when coming
396 to alter/set things, here is the list of the options along with the
397 location of the various checkboxes and the settings they should have,
398 as seen when starting from a `make menuconfig`:
400 -> Enable the block layer
402 [*] Advanced partition selection [CONFIG_PARTITION_ADVANCED]
404 [*] EFI GUID Partition support [CONFIG_EFI_PARTITION]
406 -> Processor type and features
407 [*] EFI runtime service support [CONFIG_EFI]
408 [*] EFI stub support [CONFIG_EFI_STUB]
413 -> Frame buffer Devices
414 [*] EFI-based Framebuffer Support [CONFIG_FB_EFI]
419 -> Console display driver support
420 Framebuffer Console support [CONFIG_FRAMEBUFFER_CONSOLE]
423 -> EFI (Extensible Firmware Interface) Support
424 < > EFI Variable Support via sysfs [CONFIG_EFI_VARS]
425 [*] Export efi runtime maps to sysfs [CONFIG_EFI_RUNTIME_MAP]
429 -> Pseudo filesystems
430 <*/M> EFI Variable filesystem [CONFIG_EFIVAR_FS]
434 The only Kernel Config setting that a `make defconfig` didn't set on
435 the UEFI-enabled host was this one:
437 [*] EFI stub support [CONFIG_EFI_STUB]
439 and without that setting in the kernel, attempts to boot the LFS system
442 Kernel doesn't support EFI handover
444 however, adding just that one Kernel Config setting sees you able to
445 boot into the LFS system using the host system's Grub.
447 Don't select `CONFIG_EFI_VARS`, despite some outdated references may
448 suggests to select it. It is deprecated because of an 1024-byte variable
449 size limit. `efivarfs` (`CONFIG_EFIVAR_FS`) replaces its functionality,
450 and doesn't suffer from the same limit.
452 `CONFIG_FB_EFI=y` seems not necessary if you have other FB devices
453 avaliable. But without it you will lose some boot messages along
454 with the Tux logos on the FB.
456 USING GRUB TO SET UP THE BOOT PROCESS
458 INSTALLING GRUB TO THE EFI PARTITION
460 Installing GRUB to the EFI partition and creating an OS Boot Manager
461 entry is the major difference between the instructions in this hint and the
462 procedures in the LFS book. In concept, it is not actually a divergence
463 from the concepts of the book. The instructions there install GRUB to
464 the MBR, the MBR protected layer of a GPT disk or to a dedicated /boot
465 partition. The instructions here install GRUB to the EFI partition and
466 generate an entry in the system's Boot Manager. It is for the single
467 command here that this hint was written and for which all the non-LFS
468 packages were installed.
470 grub-install --bootloader-id=LFS --recheck --debug &> grub.log
472 `--bootloader-id=<some name>` is the directory on the EFI partition to
473 which the GRUB image is written.
475 Running this command generates lots of output (redirected to `grub.log`).
476 But at the end it will indicate that it was successful. This command
477 installs the GRUB image to `/boot/efi/EFI/LFS/grubx64.efi` and creates
478 the entry `LFS` in the system's Boot Manager.
480 To check it, inspect the contents of `/boot/efi/EFI/LFS` and, as root, run
481 `efibootmgr`. The results of this command will list the Boot Order and
482 all the Boot Entries. If the entry "LFS" does not appear, read the
483 efibootmgr man page, create an entry and change the Boot Order to what is
486 If GRUB was built with `freetype` and `unifont`, `unicode.pf2` should be
487 installed automatically now. Issue:
489 grep "unicode.pf2" grub.log
491 You should see something like
493 copying `/usr/share/grub/unicode.pf2' -> `/boot/grub/fonts/unicode.pf2'
495 If not, you should get `unicode.pf2` from the host system or Internet,
496 and install it into `/boot/grub/fonts`.
502 cat > /boot/grub/grub.cfg << "EOF"
503 # Begin /boot/grub/grub.cfg
510 set root=(hd[x], gpt[y])
511 # hd[x] is the drive of the LFS partion and gpt[y] is the partition
516 if loadfont /boot/grub/fonts/unicode.pf2; then
517 loadfont /boot/grub/fonts/unicode.pf2
521 terminal_output gfxterm
524 menuentry "GNU/Linux, Linux <kernel name>" {
525 linux /boot/vmlinuz-<kernel name> root=/dev/sda[x] ro
529 Note that in `menuentry`, `/dev/sda[x]` is the device of the LFS
532 [NOTE] From GRUB's perspective, the kernel files are relative
533 to the partition used. If you used a separate `/boot` partition,
534 remove `/boot` from the above linux line and path of `unicode.pf2`.
535 You will also need to change the `set root` line to point to the
540 As stated before, the implementation of UEFI firmware and its manipulation
541 depends on the manufacturer. As of the writing of this hint, there is no
542 standard approach. Therefore, while the instructionss here all do what is
543 advertised, regrettably the system may not default to the grub boot loader "out
544 of the box." In that case, reviewing References 1-3, will provide information
545 that will lead users to a solution to the situation. As always, one of the
546 best resources is the {,B}LFS mailing lists.
548 At this point, it is worth stating that there are other helpful tools:
549 systemd-boot and rEFInd are two of them. They are described as Boot Managers,
550 but in fact are a user space layer between the OS Boot Manager and the Boot
551 Loader. Information about both is in the references.
555 1. Rod's Books - A collection of web page articles that goes into great
556 detail about the concepts of UEFI booting, partitioning and tools.
557 The below URL goes right to the efi information. www.rodsbooks.com is
558 the main page and has many, many good articles.
559 URL: http://www.rodsbooks.com/efi-bootloaders/index.html
561 2. "Unified Extensible Firmware Interface - ArchWiki"
562 URL: https://wiki.archlinux.org/index.php/Unified_Extensible_Firmware_Interface
565 URL: https://wiki.archlinux.org/index.php/GRUB
567 4. Google. URL: https://google.com
571 * Craig Magee for comments and testing.
572 * Pierre Labastie for testing, font manipulation and comments.
573 * Lei Niu for comments on efivar and grub.cfg issues.
574 * Bruce Dubbs for word changes and comments.
577 * Merge the content of this hint into BLFS-10.1
578 * Add paragraph and section numbers and TOC to make searchable
579 * Add appendix for migration from Legacy Boot to UEFI boot
580 * Add appendix for more options to default to GRUB
581 * Add appendix for LVM
582 * Add appendix for "standalone" GRUB on EFI partition independent
587 Remove the reference to pciutils
588 Update to efibootmgr-17
589 Update to unifont-13.0.03
590 Install executables and shared libraries into /, instead of /usr
591 Warn about deprecated `CONFIG_EFI_VARS`
595 Update to unifont-12.1.03
596 Add efivar-37 patch to fix FTBFS with gcc-9
599 Always use spaces instead of tabs
600 Fix a typo in unifont instruction
601 "gummiboot" is now named "systemd-boot"
602 Merge Bruce's changes
605 Use linuxfromscratch.org URL for grub patch
607 Update to efibootmgr-16
609 Add instructions creating new EFI partition
612 Added grub patch for binutils-2.31
616 Use BLFS-like format for package dependency list
617 Use package name in BLFS (FreeType instead of Freetype2)
618 Deleted dosfstools-3.0.28 (replaced by dosfstools-4.1 in BLFS)
619 Updated efivar-30 to efivar-34
620 Updated unifont-9.0.06 to unifont-10.0.07
621 Updated GRUB-2.02~beta3 to GRUB-2.02
622 Modified the hint to let GRUB install unicode.pf2 automatically
623 Removed some options not necessary now
624 Fixed path to unicode.pf2 in grub.cfg for those without /boot partition
625 Copied note about seperated /boot partition for grub.cfg from LFS book
626 Removed incorrect personal email addresses (they are LFS mail lists)
628 Adapted for checkHint script (AUTHORS to AUTHOR)
631 Updated for LFS 7.10 and "extra package" updates
632 dosfstools-3.0.26 -> dosfstools-3.0.28
633 efivar-0.12 -> efivar-30
634 efibootmgr-0.9.0 -> efibootmgr-14
635 unifont-7.0.05 -> unifont-9.0.06