Typo
[linux_from_scratch_hints.git] / OLD / lfs-uefi-20170122.txt
blobbd94f33ea86e1bc4225dda87092e93322e22d3b2
1 USING GRUB ON UEFI
3 AUTHORS: Dan McGhee, Kevin M. Buckley
5 DATE: 2017-10-22
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 Running the "make test" that the package's documentation suggests, gave a
255 rather strange error message,
257 # make test
258 ## ...
259 ## About to test thirtythree
260 ## testing efi_set_variable()
261 ## testing efi_get_variable_size()
262 ## testing efi_get_variable()
263 ## testing efi_get_variable_attributes()
264 ## testing efi_del_variable()
265 ## testing efi_append_variable()
266 ## testing efi_get_variable()
267 ## testing efi_del_variable()
268 ## About to test tentwentyfour
269 ## testing efi_set_variable()
270 ## testing efi_get_variable_size()
271 ## testing efi_get_variable()
272 ## testing efi_get_variable_attributes()
273 ## testing efi_del_variable()
274 ## testing efi_append_variable()
275 ## FAIL: "tentwentyfour"(line 197) (-1) append test failed: No space left on device
277 however at time of writing, thoughts on this haven't been forthcoming
278 from the EFIVAR community, 
280    https://github.com/rhinstaller/efivar/issues/78
282 though it doesn't appear to prevent things working as expected.
286       EFIBOOTMGR-14
287       (depends on pciutils, efivars,zlib to build and dosfstools to run.)
289 See notes to EFIVAR package as regards changes to download URI
291 Note also that compared to the 2014-10-16 hint, efibootmgr's source 
292  directory layout has changed,
294       Download:  https://github.com/rhinstaller/efibootmgr/releases/download/14/efibootmgr-14.tar.bz2
297       Compile the package:
298         tar xf /path/to/EXTRA-SRCS-7.10/efibootmgr-14.tar.bz2
299         cd efibootmgr-14/
300         make
302       Install the package:
303         install -v -D -m0755 src/efibootmgr /usr/sbin/efibootmgr
304         install -v -D -m0644 src/efibootmgr.8 \
305                 /usr/share/man/man8/efibootmgr.8
306         install -v -D -m0644 src/efibootdump.8 \
307                 /usr/share/man/man8/efibootdump.8
310       GRUB-2.02~beta3 (depends on freetype2 if grub-mkfont is desired
311                and on efibootmgr, efivars and efivarfs at run time.) 
313          Download: http://alpha.gnu.org/gnu/grub/grub-2.02~beta3.tar.xz
315       Prepare for compilation:
316          tar xf /path/to/LFS-SRCS-7.10/grub-2.02~beta3.tar.xz
317          cd grub-2.02~beta3/
318         ./configure --prefix=/usr  \
319             --sbindir=/sbin        \
320             --sysconfdir=/etc      \
321             --disable-efiemu       \
322             --enable-grub-mkfont   \
323             --with-platform=efi    \
324             --target=x86_64        \
325             --program-prefix=""    \
326             --with-bootdir="/boot" \
327             --with-grubdir="grub" \
328             --disable-werror       
330          Command explanation:
332           --enable-grub-mkfont  This creates the build dependency on
333              Freetype2. To remove this dependency do not use this switch
334              and copy unicode.pf2 from the host system to /boot/grub of the
335              LFS partition.  Alternatively, it can be downloaded from the
336              internet.
338           --program-prefix="" is a matter of convenience.  If not used,
339              "x86_64" is inserted in all the grub executables.  For
340              example, "grub-install" and "grub-mkconfig" become
341              x86_64-grub-install and x86_64-grub-mkconfig.
343           --with-platform=efi and --target=x86_64 are mandatory for the efi
344              and x86_64 build
346          The other configure options added to the ones in LFS-7.10 and
347          LFS-SVN were employed to insure that grub is built and installed
348          in the directories used in this hint.  They may be used or
349          eliminated based on individual use and preference.
352       Compile the package:
353     
354         make
356       Install the package:
358         make install
360       Post installation of the Unifont files, do a
362         grub-mkfont -o /usr/share/grub/unicode.pf2 > \
363          /usr/share/fonts/unifont/unifont.pcf
364       
366       UNIFONT 9.0.06
368 In the 2014-01-16 hint, the Unifont installtion got a mention further
369 down but as the 2017-01-22 hint's build made use of those fonts, they're
370 here
372       Download: http://unifoundry.com/pub/unifont-9.0.06/font-builds/unifont-9.0.06.pcf.gz
373       
374       Install the font files
376         mkdir -pv /usr/share/fonts/unifont
377         gunzip -c /path/to/EXTRA-SRCS-7.10/unifont-9.0.06.pcf.gz > \
378          /usr/share/fonts/unifont/unifont.pcf
381 LFS CHAPTER 7:
383      When constructing the file /etc/fstab, add the following lines:
385       /dev/sda(x)     /boot/efi    vfat     defaults            0     1
387       efivarfs       /sys/firmware/efi/efivars  efivarfs  defaults  0      1
389      where /dev/sda(x) is the EFI partion
391 Notes:
393 1) If you are going to be booting your UEFI-aware LFS system using a 
394    non-LFS Grub from your host AND if that Grub is one (eg Fedora) 
395    that allows for the kernel to be specified using that Grub's  
396    "linuxefi" attribute, so
398      linuxefi  /path/to/kernel root=/path/to/root ro
400    then you don't appear to need the /etc/fstab line, and indeed, 
401    you'll get told during the boot that the mounter knows nothing
402    about the efivars filesystem type.
404    However, LFS's efibootmgr will still be capable of interrogating
405    your UEFI environment.
407 2) If the LFS system is booted from the LFS+Hint's grub, which doesn't
408    appear to know about the "linuxefi" attribute so using
410     linux  /path/to/kernel root=/path/to/root ro
412    then, unless you have the efivars filesystem mounted, and you are
413    able to, then LFS's efibootmgr will be NOT capable of interrogating
414    your UEFI environment, and you'll be told that there is no efivars
415    filesystem
419 LFS CHAPTER 8:
421    KERNEL CONFIGURATION OPTIONS FOR EFI
423 The LFS 7.10 kernel build's "make defconfig" populated a good number of
424 the EFI-related options on my UEFI-enabled hardware, however, so as to
425 make the 2014-10-16 hint's list of settings easier to find when coming 
426 to alter/set things, here is the list of the options along with the 
427 location of the various checkboxes and the settings they should have, 
428 as seen when starting from a "make menuconfig"
430      ## CONFIG_EFI_PARTITION=y
432        Location:
433          -> Enable the block layer
434            -> Partition Types
435              [*] Advanced partition selection
436              ...
437              [*] EFI GUID Partition support
439      ## CONFIG_EFI=y
440      ## CONFIG_EFI_STUB=y
442        Location:
443          -> Processor type and features
444            [*] EFI runtime service support
445            [*]   EFI stub support
447      ## CONFIG_FB_EFI=y
449        Location:
450          -> Device Drivers
451            -> Graphics support
452              -> Frame buffer Devices
453                [*] EFI-based Framebuffer Support
455      ## CONFIG_FRAMEBUFFER_CONSOLE=y
457        Location:
458          -> Device Drivers
459            -> Graphics support
460              -> Console display driver support
461                Framebuffer Console support      (Not available on mine)
463      ## CONFIG_EFI_VARS is not set
464      ## CONFIG_EFI_RUNTIME_MAP=y
466        Location:
467          -> Firmware Drivers
468            -> EFI (Extensible Firmware Interface) Support
469              < > EFI Variable Support via sysfs
470              [*] Export efi runtime maps to sysfs
473      ## CONFIG_EFIVAR_FS=y
475         Location:
476           -> File systems
477             -> Pseudo filesystems
478               [*] EFI Variable filesystem
481 Note: 
483 The only Kernel Config setting that a 'make defconfig' didn't set on
484 the UEFI-enabled host was this one:
486      ## CONFIG_EFI_STUB=y
488 and without that setting in the kernel, attempts to boot the LFS system
489 tell you that:
491   Kernel doesn't support EFI handover
493 however, adding just that one Kernel Config setting sees you able to
494 boot into the LFS system using the host system's Grub.
498 USING GRUB TO SET UP THE BOOT PROCESS
500 INSTALLING GRUB TO THE EFI PARTITION
502   Installing GRUB to the EFI partition and creating an OS Boot Manager
503   entry is the major difference between the recipes in this hint and the
504   procedures in the LFS book.  In concept, it is not actually a divergence
505   from the concepts of the book.  The instructions there install GRUB to
506   the MBR, the MBR protected layer of a GPT disk or to a dedicated /boot
507   partition.  The recipes here install GRUB to the EFI partition and
508   generate an entry in the system's Boot Manager.  It is for the single
509   command here that this hint was written and for which all the non-LFS
510   packages were installed.
512     grub-install --target=x86_64-efi --efi-directory=/boot/efi  \
513        --bootloader-id=LFS --recheck --debug
515     --efi-directory=<EFI Mount Point> not the actual EFI partition
516     --bootloader-id=<some name> is the directory on the EFI partition to
517       which the GRUB image is written.
519   Running this command generates lots of output.  But at the end it will
520   indicate that it was successful.  This command installs the GRUB image to
521   /boot/efi/EFI/LFS/grubx64.efi and creates the entry "LFS" in the system's
522   Boot Manager.
524   To check it, inspect the contents of /boot/efi/EFI/LFS and, as root, run
525   <efibootmgr>.  The results of this command will list the Boot Order and
526   all the Boot Entries.  If the entry "LFS" does not appear, read the
527   efibootmgr man page, create an entry and change the Boot Order to what is
528   desired.
530 CONFIGURING GRUB
532   Generate grub.cfg:
534   cat > /boot/grub/grub.cfg << "EOF"
535   # Begin /boot/grub/grub.cfg
536   set default=0
537   set timeout=5
539   insmod gzio
540   insmod part_gpt
541   insmod ext2
542   set root=(hd[x], gpt[y])
543   # hd[x] is the drive of the LFS partion and gpt[y] is the partition
545   insmod efi_gop
546   insmod efi_uga
547   insmod font
548   if loadfont /grub/unicode.pf2; then
549     loadfont /grub/unicode.pf2
550     set gfxmode=auto
551     insmod gfxterm
552     set gfxpayload=keep
553     terminal_output gfxterm
554   fi
556   menuentry "GNU/Linux, Linux <kernel name>"  {
557     linux   /boot/vmlinuz-<kernel name>; root=/dev/sda[x] ro
558   }  
559   EOF
561   Note that in "menuentry" /dev/sda[x] is the device of the LFS partition.
563 FINAL DISCUSSION:
565 As stated before, the implementation of UEFI firmware and its manipulation
566 depends hugely on the manufacturer.  As of the initial writing of this
567 hint, there is no standard approach.  Therefore, while the recipes here all
568 do what is advertised, regrettably the system may not default to the grub
569 boot loader "out of the box."  In that case, reviewing References 1-3, will
570 provide information that will lead users to a solution to the situation.
571 As always, one of the best resources is the {,B}LFS mailing lists.
573 At this point, it is worth stating that there are other helpfultools:
574 gummiboot and rEFInd are two of them.  They are described as Boot Managers,
575 but in fact are a user space layer between the OS Boot Manager and the Boot
576 Loader.  Information about both is in the references.
578 REFERENCES:
580 1.  Rod's Books  A collection of web page articles that goes into great
581     detail about the concepts of  UEFI booting, partitioning and tools.
582     The below URL goes right to the efi information.  www.rodsbooks.com is
583     the main page and has many, many good articles.
584   URL:  http://www.rodsbooks.com/efi-bootloaders/index.html
586 2.   "Unified Extensible Firmware Interface-ArchWiki"
587   URL:  https://wiki.archlinux.org/index.php/
588           Unified_Extensible_Firmware_Interface
590 3.  "GRUB-ArchWiki"
591   URL:  https://wiki.archlinux.org/index.php/GRUB
593 4.  Google
596 ACKNOWLEDGEMENTS:
597   * Craig Magee <lfs-support at lists.linuxfromscratch.org> for comments and testing
598   * Pierre Labastie <http://lists.linuxfromscratch.org/listinfo/lfs-dev> for testing,
599     font manipulation and comments.
601 TODO:
602   *  Add paragraph and section numbers and TOC to make searchable
603   *  Add appendix for migration from Legacy Boot to UEFI boot
604   *  Add appendix for more options to default to GRUB
605   *  Add appendix for LVM
606   *  Add appendix for "standalone" GRUB on EFI partition independent
607      from distro
609 CHANGELOG:
610 [TBD]
611   * 2017-01-22 
612     Updated for LFS 7.10 and "extra package" updates
613     dosfstools-3.0.26 -> dosfstools-3.0.28
614     efivar-0.12       -> efivar-30
615     efibootmgr-0.9.0  -> efibootmgr-14
616     unifont-7.0.05    -> unifont-9.0.06
618 [TBD]
619   * 2014-10-16
620     Initial hint.