lfs-uefi: fix efivar-37 FTBFS
[linux_from_scratch_hints.git] / lfs-uefi-20170207.txt
blobf9d7c518a99289bd59eeb099a10a30b8ca10efaf
1 USING GRUB ON UEFI
3 AUTHORS: Dan McGhee, Kevin M. Buckley
5 DATE: 2017-02-07
7 LICENSE: GNU Free Documentation License Version 1.2
9 SYNOPSIS: Boot LFS by default in a UEFI Environment using GRUB
11 DESCRIPTION:
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
14 only x86_64 machines.
16 This version, hereafter referred to as "the 2017-10-22 hint", updates Dan 
17 McGhee's original, dated 2014-10-16.
19 The 2017-10-22 hint saw the UEFI packages built against an LFS 7.10 SysV
20 installation that was already being booted using the existing host system's 
21 bootloaders.
23 Where possible, changes to the 2017-01-22 hint have been made so that it
24 should be obvious where the the 2017-01-22 and 2014-10-16 hint's differ.
26 ATTACHMENTS:
27 *  None
29 PREREQUISITES:
30 Base LFS system before or after Ch. 8
31 Basic understanding of obtaining and building packages
33 HINT:
35 DISCLAIMER:  The recipes in this hint neither supplant nor supersede the
36 build instructions in a stable version of either the LFS or BLFS books.
37 They merely augment them for newer firmware.  If conflicts arise between
38 this hint and the instructions in the book, take the issue to the mailing
39 lists.  Additionally, this hint applies to only x86_64 machines packaged
40 with Windows 7 or Windows 8.  The recipes here can be used on Mac OS, but
41 have not been investigated at the initial writing of this hint.
43 The 2017-01-22 hint refers to an LFS 7.10 system, built onto an x86_64 
44 machine from within a CentOS 6 host, that had had never had a version of
45 windows installed on it, indeed the host contained three EFI directories
46 below "/boot/efi/EFI/", namely centos, redhat, and ubuntu, these having
47 been installed from CentOS 6.8, Fedora Core 20 and Ubuntu 12.04 OSes
48 respectively.
50 USE OF TERMS:  The following is a use of terms in this hint.  Further
51 information for and amplification of them can be found in References 1-3.
53 BIOS Settings:  A firmware interface accessed by the keyboard after power
54 is applied.  In it a user can change the order and way of how the computer
55 boots.
57 BIOS System:  Firmware with an MBR
59 EFI Mode:  A condition of the booted system in which the EFI partition is
60 mounted and the uefi (efi) variable support in the kernel is working
61 properly.  It results from enabling UEFI Mode in BIOS Settings.
63 EFI Mount Point:  A user defined mount point for the EFI Partition.  In
64 this hint, and in most distros, it is /boot/efi.
66 EFI Partition:  A small partition, usually before any other partitions;
67 i.e., /dev/sda1 of  200-250 Mb, formatted in FAT32 with the /boot flag, in
68 parted, or ef00 (EF00) partition type in gdisk.  (NOTE: The boot flag has a
69 different function and meaning in MBR partitioned disks.)
71 efi variables (synonymous: uefi variables):  variables through which the
72 operating system can interact with the firmware.
74 Legacy Boot Option (Legacy Boot):  A boot process in BIOS Settings that
75 disables UEFI booting and uses CIM.
77 GUID Partition Table (GPT): A partitioning scheme that uses UUID's instead
78 of cylinders to identify partitions.
81 PRELIMINARY DISCUSSION:  Additional information and more in depth
82 discussion of the following concepts can be found using References 1-3.
84 Booting LFS is no longer as simple as "grub-install  /dev/sda."  There are
85 more options and more considerations.  With the advent and proliferation of
86 UEFI firmware, a user's knowledge and philosophy of the boot
87 process requires expansion:
88     a) GPT partitioning is different from MBR partitioning.  The tool fdisk
89        is not able to manipulate GPT partitions.  Parted and gdisk (from
90        gptfdisk) are the tools to use.  Each has their pros and cons,
91        supporters and detractors.  Either one or both can be used.
92     b) UEFI firmware uses Boot Managers to select Boot Loaders like GRUB or
93        LILO.  They, themselves do not boot the machine.
94     c) The Boot Loaders are placed on the EFI partition rather than the
95        MBR.  This concept is similar and parallel to the LFS procedures of
96        using a separate /boot partition.
97     d) There are additional tools that LFS needs in order to accomplish
98        this mode of booting.
99     e) LFS can be built and booted as the instructions are written up to
100        and including LFS-7.10.  To do this on UEFI firmware, the BIOS
101        Settings must be changed to Legacy Options from UEFI Options.
103 One of the hugely discussed issues surrounding UEFI is Secure Boot.  It is
104 necessary to understand that the terms "UEFI" and "Secure Boot" are NOT
105 synonymous.  UEFI is firmware.  Secure Boot is a process of using "keys" to
106 "guarantee" the safety and authenticity of a Boot Loader.  NOTE:  To use
107 the recipes in this hint, Secure Boot must be disabled in the BIOS Boot
108 Settings.
110 Please note that the recommended order for implementing these recipes is a
111 departure from the build order in LFS.  The most convenient, and arguably
112 the most practical way, to implement the recipes here is to use them in the
113 of build of an LFS System at the end of Ch. 6. Building the BLFS and
114 non-BLFS packages has been tested both inside and outside of the chroot
115 environment.  Then, following the book, proceed through Ch. 7, returning to
116 the recipes in Ch. 8.   The recipes are presented in that order.
118 The most inconvenient way to implement these recipes is in a completely
119 functional LFS-7.10, or earlier, system.  This involves uninstalling
120 grub-2.02, removing it from its location as a result of grub-install and
121 implementing the recipes.  Migrating from Legacy Boot to UEFI boot is
122 possible.  At the initial writing of this hint, however, it is not
123 included.  References 1-3 contain more information on this subject.
125 The last consideration in implementing the recipes here is GRUB's graphical
126 terminal.  In UEFI systems, if the GRUB video mode is not initialized, no
127 kernel boot messages will appear until the kernel video takes over.  The
128 GRUB package does not supply fonts, and GRUB defaults to unicode.pf2.
129 There are two ways to supply this font.  The first is to copy unicode.pf2
130 from the host system to /boot/grub on the LFS system.  The second method
131 involves configuring grub to build grub-mkfont, and this creates a build
132 dependency of Freetype2 for GRUB.  This hint addresses both situations.   
134 Finally, as of the initial writing of this hint, there is no standard for
135 the use of UEFI and the implementation of Secure Boot.  These are hugely
136 manufacturer dependent.  This hint uses terms used in the original author's
137 hardware.  They may be different in other manufacturers' implementations.
138 However, the capabilities to do the boot setup operations contained in this
139 hint will exist on each machine.  The terms may differ, and more than one
140 operation might be needed to achieve a desired goal.  For example, someone
141 may need to disable Secure Boot and remove Secure Keys.
144 RECIPES:
145 [NOTE] The recipes are written with the assumption that the packages are
146 being built in the chroot environment before the end of Ch. 8.  They can be
147 modified, with little difficulty, to be used in a functional system.
149 CHECKING EFI-MODE
150     Before entering the chroot environment, check that the host booted in
151     EFI Mode.
153     ls /sys/firmware/efi
155     If this directory exists and is populated, the host booted in EFI Mode.
158 MOUNT EFI PARTITION
159         Determine which device is the EFI partition using gdisk or parted,
160         enter the chroot environment, create /boot/efi if needed, and
162         mount -vt vfat /dev/sda(x) /boot/efi
164         where sda(x) is the device containing the EFI partition.
166 BUILD DEPENDENCIES:
168 Install the following BLFS packages, using the instructions in the book:
169 popt and pciutils.   Build and install Freetype2 if building grub with
170 grub-mkfont enabled.
173 The BLFS 7.10 Freetype2 instructions recommend that it be built after 
174 "which" and "libpng" have been installed, so it was, however, as the 
175 recommendation for "HarfBuzz" notes that one builds Freetype2 without 
176 it first, and then do a re-install, it wasn't thought necessary to do 
177 the re-install.
179 The "libpng" install did include the "apng" patch.
182 The BLFS 7.10 instructions for popt explicitly
184   "prevents installtion of  the static versions of the libraries"
186 however the build of efivars appears to require those.
188 In order to follow the BLFS book and then explicity alter things for 
189 an installtion of UEFI-related packages, following the BLFS books
190 instructions, and then doing 
192 tar xf /path/to/BLFS-SRCS-7.10/popt-1.16.tar.gz
193 cd popt-1.16/
194 ./configure --prefix=/usr --enable-static
195 make
196 make install  
198 appears to do the right thing.
202       DOSFSTOOLS 3.0.28 (runtime dependency of efibootmgr)
203       Note: As of October 3, 2014, dosfstools was tagged "orphaned.
204       It is still functional.]
206       Download:
207         https://github.com/dosfstools/dosfstools/releases/download/v3.0.28/dosfstools-3.0.28.tar.xz
209       Build and Installation:
211         tar xf /path/to/EXTRA-SRCS-7.10/dosfstools-3.0.28.tar.xz
212         cd dosfstools-3.0.28/
213         make PREFIX=/usr SBINDIR=/usr/bin MANDIR=/usr/share/man \
214          DOCDIR=/usr/share/doc
215         make LANGUAGES="" PREFIX=/usr SBINDIR=/usr/bin MANDIR=/usr/share/man \
216          DOCDIR=/usr/share/doc install
218 Note that the LANGUAGES="" prevents the install from creating all manner
219 of directories below /use/share/man, but if you want those, leave it out.
222       EFIVAR-30 (depends on popt)
224 The 2014-10-16 hint pointed to a user's GitHub branch of the main code
225  that is now available as part of Fedora's GitHub repository.
226 Note also that the numbering scheme has changed slightly.
227 Furthermore, the EFIVAR-30's Makefiles have altered the way in which
228  the codebase is built
230       Download:
231         https://github.com/rhinstaller/efivar/releases/download/30/efivar-30.tar.bz2
233       Compile the package:
235         tar xf /path/to/EXTRA-SRCS-7.10/efivar-30.tar.bz2
236         cd efivar-devel-30/
237         cp -p Make.defaults Make.defaults.dist
238         sed 's|-O2|-Os|g' -i Make.defaults
239         cp -p src/test/Makefile src/test/Makefile.dist
240         sed 's|-rpath=$(TOPDIR)/src/|-rpath=$(libdir)|g' \
241          -i src/test/Makefile
242         make libdir="/usr/lib/" bindir="/usr/bin/" \
243          mandir="/usr/share/man/"     \
244          includedir=/usr/include/" V=1 -j1
246       Install the package:
248         make -j1 V=1 DESTDIR="${pkgdir}/" libdir="/usr/lib/" \
249          bindir="/usr/bin/" mandir="/usr/share/man"   \
250          includedir="/usr/include/" install
251   
252         install -v -D -m0755 src/test/tester /usr/bin/efivar-tester
254 Despite pathcing the src/test/Makefile, and that Makefile having a
255 "test", target, albeit one which isn't run by default, you SHOULD NOT
256 run that "make test", as it has been found to cause firmware bugs.
258 Here are the thoughts on, and the exhortation not to do, this from 
259 the EFIVAR community, 
261    https://github.com/rhinstaller/efivar/issues/78
263 Note that, rather than remove the test from the Makefile, or alter it
264 to tell the use not to run it, they've merely hidden it from normal
265 invocation.
268       EFIBOOTMGR-14
269       (depends on pciutils, efivars,zlib to build and dosfstools to run.)
271 See notes to EFIVAR package as regards changes to download URI
273 Note also that compared to the 2014-10-16 hint, efibootmgr's source 
274  directory layout has changed,
276       Download:  https://github.com/rhinstaller/efibootmgr/releases/download/14/efibootmgr-14.tar.bz2
279       Compile the package:
280         tar xf /path/to/EXTRA-SRCS-7.10/efibootmgr-14.tar.bz2
281         cd efibootmgr-14/
282         make
284       Install the package:
285         install -v -D -m0755 src/efibootmgr /usr/sbin/efibootmgr
286         install -v -D -m0644 src/efibootmgr.8 \
287                 /usr/share/man/man8/efibootmgr.8
288         install -v -D -m0644 src/efibootdump.8 \
289                 /usr/share/man/man8/efibootdump.8
292       GRUB-2.02~beta3 (depends on freetype2 if grub-mkfont is desired
293                and on efibootmgr, efivars and efivarfs at run time.) 
295          Download: http://alpha.gnu.org/gnu/grub/grub-2.02~beta3.tar.xz
297       Prepare for compilation:
298          tar xf /path/to/LFS-SRCS-7.10/grub-2.02~beta3.tar.xz
299          cd grub-2.02~beta3/
300         ./configure --prefix=/usr  \
301             --sbindir=/sbin        \
302             --sysconfdir=/etc      \
303             --disable-efiemu       \
304             --enable-grub-mkfont   \
305             --with-platform=efi    \
306             --target=x86_64        \
307             --program-prefix=""    \
308             --with-bootdir="/boot" \
309             --with-grubdir="grub" \
310             --disable-werror       
312          Command explanation:
314           --enable-grub-mkfont  This creates the build dependency on
315              Freetype2. To remove this dependency do not use this switch
316              and copy unicode.pf2 from the host system to /boot/grub of the
317              LFS partition.  Alternatively, it can be downloaded from the
318              internet.
320           --program-prefix="" is a matter of convenience.  If not used,
321              "x86_64" is inserted in all the grub executables.  For
322              example, "grub-install" and "grub-mkconfig" become
323              x86_64-grub-install and x86_64-grub-mkconfig.
325           --with-platform=efi and --target=x86_64 are mandatory for the efi
326              and x86_64 build
328          The other configure options added to the ones in LFS-7.10 and
329          LFS-SVN were employed to insure that grub is built and installed
330          in the directories used in this hint.  They may be used or
331          eliminated based on individual use and preference.
334       Compile the package:
335     
336         make
338       Install the package:
340         make install
342       Post installation of the Unifont files, do a
344         grub-mkfont -o /usr/share/grub/unicode.pf2 > \
345          /usr/share/fonts/unifont/unifont.pcf
346       
348       UNIFONT 9.0.06
350 In the 2014-01-16 hint, the Unifont installtion got a mention further
351 down but as the 2017-01-22 hint's build made use of those fonts, they're
352 here
354       Download: http://unifoundry.com/pub/unifont-9.0.06/font-builds/unifont-9.0.06.pcf.gz
355       
356       Install the font files
358         mkdir -pv /usr/share/fonts/unifont
359         gunzip -c /path/to/EXTRA-SRCS-7.10/unifont-9.0.06.pcf.gz > \
360          /usr/share/fonts/unifont/unifont.pcf
363 LFS CHAPTER 7:
365      When constructing the file /etc/fstab, add the following lines:
367       /dev/sda(x)     /boot/efi    vfat     defaults            0     1
369       efivarfs       /sys/firmware/efi/efivars  efivarfs  defaults  0      1
371      where /dev/sda(x) is the EFI partion
373 Notes:
375 1) If you are going to be booting your UEFI-aware LFS system using a 
376    non-LFS Grub from your host AND if that Grub is one (eg Fedora) 
377    that allows for the kernel to be specified using that Grub's  
378    "linuxefi" attribute, so
380      linuxefi  /path/to/kernel root=/path/to/root ro
382    then you don't appear to need the /etc/fstab line, and indeed, 
383    you'll get told during the boot that the mounter knows nothing
384    about the efivars filesystem type.
386    However, LFS's efibootmgr will still be capable of interrogating
387    your UEFI environment.
389 2) If the LFS system is booted from the LFS+Hint's grub, which doesn't
390    appear to know about the "linuxefi" attribute so using
392     linux  /path/to/kernel root=/path/to/root ro
394    then, unless you have the efivars filesystem mounted, and you are
395    able to, then LFS's efibootmgr will be NOT capable of interrogating
396    your UEFI environment, and you'll be told that there is no efivars
397    filesystem
401 LFS CHAPTER 8:
403    KERNEL CONFIGURATION OPTIONS FOR EFI
405 The LFS 7.10 kernel build's "make defconfig" populated a good number of
406 the EFI-related options on my UEFI-enabled hardware, however, so as to
407 make the 2014-10-16 hint's list of settings easier to find when coming 
408 to alter/set things, here is the list of the options along with the 
409 location of the various checkboxes and the settings they should have, 
410 as seen when starting from a "make menuconfig"
412      ## CONFIG_EFI_PARTITION=y
414        Location:
415          -> Enable the block layer
416            -> Partition Types
417              [*] Advanced partition selection
418              ...
419              [*] EFI GUID Partition support
421      ## CONFIG_EFI=y
422      ## CONFIG_EFI_STUB=y
424        Location:
425          -> Processor type and features
426            [*] EFI runtime service support
427            [*]   EFI stub support
429      ## CONFIG_FB_EFI=y
431        Location:
432          -> Device Drivers
433            -> Graphics support
434              -> Frame buffer Devices
435                [*] EFI-based Framebuffer Support
437      ## CONFIG_FRAMEBUFFER_CONSOLE=y
439        Location:
440          -> Device Drivers
441            -> Graphics support
442              -> Console display driver support
443                Framebuffer Console support      (Not available on mine)
445      ## CONFIG_EFI_VARS is not set
446      ## CONFIG_EFI_RUNTIME_MAP=y
448        Location:
449          -> Firmware Drivers
450            -> EFI (Extensible Firmware Interface) Support
451              < > EFI Variable Support via sysfs
452              [*] Export efi runtime maps to sysfs
455      ## CONFIG_EFIVAR_FS=y
457         Location:
458           -> File systems
459             -> Pseudo filesystems
460               [*] EFI Variable filesystem
463 Note: 
465 The only Kernel Config setting that a 'make defconfig' didn't set on
466 the UEFI-enabled host was this one:
468      ## CONFIG_EFI_STUB=y
470 and without that setting in the kernel, attempts to boot the LFS system
471 tell you that:
473   Kernel doesn't support EFI handover
475 however, adding just that one Kernel Config setting sees you able to
476 boot into the LFS system using the host system's Grub.
480 USING GRUB TO SET UP THE BOOT PROCESS
482 INSTALLING GRUB TO THE EFI PARTITION
484   Installing GRUB to the EFI partition and creating an OS Boot Manager
485   entry is the major difference between the recipes in this hint and the
486   procedures in the LFS book.  In concept, it is not actually a divergence
487   from the concepts of the book.  The instructions there install GRUB to
488   the MBR, the MBR protected layer of a GPT disk or to a dedicated /boot
489   partition.  The recipes here install GRUB to the EFI partition and
490   generate an entry in the system's Boot Manager.  It is for the single
491   command here that this hint was written and for which all the non-LFS
492   packages were installed.
494     grub-install --target=x86_64-efi --efi-directory=/boot/efi  \
495        --bootloader-id=LFS --recheck --debug
497     --efi-directory=<EFI Mount Point> not the actual EFI partition
498     --bootloader-id=<some name> is the directory on the EFI partition to
499       which the GRUB image is written.
501   Running this command generates lots of output.  But at the end it will
502   indicate that it was successful.  This command installs the GRUB image to
503   /boot/efi/EFI/LFS/grubx64.efi and creates the entry "LFS" in the system's
504   Boot Manager.
506   To check it, inspect the contents of /boot/efi/EFI/LFS and, as root, run
507   <efibootmgr>.  The results of this command will list the Boot Order and
508   all the Boot Entries.  If the entry "LFS" does not appear, read the
509   efibootmgr man page, create an entry and change the Boot Order to what is
510   desired.
512 CONFIGURING GRUB
514   Generate grub.cfg:
516   cat > /boot/grub/grub.cfg << "EOF"
517   # Begin /boot/grub/grub.cfg
518   set default=0
519   set timeout=5
521   insmod gzio
522   insmod part_gpt
523   insmod ext2
524   set root=(hd[x], gpt[y])
525   # hd[x] is the drive of the LFS partion and gpt[y] is the partition
527   insmod efi_gop
528   insmod efi_uga
529   insmod font
530   if loadfont /grub/unicode.pf2; then
531     loadfont /grub/unicode.pf2
532     set gfxmode=auto
533     insmod gfxterm
534     set gfxpayload=keep
535     terminal_output gfxterm
536   fi
538   menuentry "GNU/Linux, Linux <kernel name>"  {
539     linux   /boot/vmlinuz-<kernel name> root=/dev/sda[x] ro
540   }  
541   EOF
543   Note that in "menuentry" /dev/sda[x] is the device of the LFS partition.
545 FINAL DISCUSSION:
547 As stated before, the implementation of UEFI firmware and its manipulation
548 depends hugely on the manufacturer.  As of the initial writing of this
549 hint, there is no standard approach.  Therefore, while the recipes here all
550 do what is advertised, regrettably the system may not default to the grub
551 boot loader "out of the box."  In that case, reviewing References 1-3, will
552 provide information that will lead users to a solution to the situation.
553 As always, one of the best resources is the {,B}LFS mailing lists.
555 At this point, it is worth stating that there are other helpfultools:
556 gummiboot and rEFInd are two of them.  They are described as Boot Managers,
557 but in fact are a user space layer between the OS Boot Manager and the Boot
558 Loader.  Information about both is in the references.
560 REFERENCES:
562 1.  Rod's Books  A collection of web page articles that goes into great
563     detail about the concepts of  UEFI booting, partitioning and tools.
564     The below URL goes right to the efi information.  www.rodsbooks.com is
565     the main page and has many, many good articles.
566   URL:  http://www.rodsbooks.com/efi-bootloaders/index.html
568 2.   "Unified Extensible Firmware Interface-ArchWiki"
569   URL:  https://wiki.archlinux.org/index.php/
570           Unified_Extensible_Firmware_Interface
572 3.  "GRUB-ArchWiki"
573   URL:  https://wiki.archlinux.org/index.php/GRUB
575 4.  Google
578 ACKNOWLEDGEMENTS:
579   * Craig Magee <lfs-support at lists.linuxfromscratch.org> for comments and testing
580   * Pierre Labastie <http://lists.linuxfromscratch.org/listinfo/lfs-dev> for testing,
581     font manipulation and comments.
583 TODO:
584   *  Add paragraph and section numbers and TOC to make searchable
585   *  Add appendix for migration from Legacy Boot to UEFI boot
586   *  Add appendix for more options to default to GRUB
587   *  Add appendix for LVM
588   *  Add appendix for "standalone" GRUB on EFI partition independent
589      from distro
591 CHANGELOG:
592 [TBD]
593   * 2017-01-22 
594     Updated for LFS 7.10 and "extra package" updates
595     dosfstools-3.0.26 -> dosfstools-3.0.28
596     efivar-0.12       -> efivar-30
597     efibootmgr-0.9.0  -> efibootmgr-14
598     unifont-7.0.05    -> unifont-9.0.06
600 [TBD]
601   * 2014-10-16
602     Initial hint.