source: postlfs/filesystems/uefi-bootloaders/grub-setup.xml@ f53a4ef8

12.2 trunk
Last change on this file since f53a4ef8 was dece8f5, checked in by Xi Ruoyao <xry111@…>, 4 weeks ago

grub-setup: Use an entity for LFS and kernel versions

Just found another occurence of the version string not updated even for
12.1 :(.

  • Property mode set to 100644
File size: 19.0 KB
Line 
1<?xml version="1.0" encoding="UTF-8"?>
2<!DOCTYPE sect1 PUBLIC "-//OASIS//DTD DocBook XML V4.5//EN"
3 "http://www.oasis-open.org/docbook/xml/4.5/docbookx.dtd" [
4 <!ENTITY % general-entities SYSTEM "../../../general.ent">
5 %general-entities;
6
7 <!ENTITY lfs-kernel-ver "6.10.5-lfs-12.2">
8]>
9
10<sect1 id="grub-setup" xreflabel="Using GRUB to Set Up the Boot Process with UEFI">
11 <?dbhtml filename="grub-setup.html"?>
12
13
14 <title>Using GRUB to Set Up the Boot Process with UEFI</title>
15
16 <sect2>
17 <title>Turn Off Secure Boot</title>
18
19 <para>
20 BLFS does not have the essential packages to support Secure Boot. To
21 set up the boot process with GRUB and UEFI in BLFS, Secure
22 Boot must be turned off from the configuration interface of the
23 firmware. Read the documentation provided by the manufacturer of your
24 system to find out how.
25 </para>
26 </sect2>
27
28 <sect2 role="kernel" id="uefi-kernel">
29 <title>Kernel Configuration for UEFI support</title>
30
31 <para>
32 Enable the following options in the kernel configuration and recompile
33 the kernel if necessary:
34 </para>
35
36 <xi:include xmlns:xi="http://www.w3.org/2001/XInclude"
37 href="grub-setup-kernel.xml"/>
38
39 <variablelist>
40 <title>The meaning of the configuration options:</title>
41
42 <varlistentry>
43 <term><parameter>CONFIG_PARTITION_ADVANCED</parameter></term>
44 <listitem>
45 <para>If it's not enabled,
46 <parameter>CONFIG_EFI_PARTITION</parameter> will be enabled
47 automatically. But when it's enabled, you must set
48 <parameter>CONFIG_EFI_PARTITION</parameter> to enabled as
49 well.</para>
50 </listitem>
51 </varlistentry>
52
53 <varlistentry>
54 <term>
55 <parameter>CONFIG_SYSFB_SIMPLEFB</parameter>,
56 <parameter>CONFIG_DRM</parameter>,
57 <parameter>CONFIG_DRM_FBDEV_EMULATION</parameter>,
58 <parameter>CONFIG_DRM_SIMPLEDRM</parameter>,
59 <parameter>CONFIG_FB</parameter>, and
60 <parameter>CONFIG_FRAMEBUFFER_CONSOLE</parameter></term>
61 <listitem>
62 <para>The combination of these options provides the Linux console
63 support on top of the UEFI framebuffer. To allow the kernel to
64 print debug messages at an early boot stage, they shouldn't be
65 built as kernel modules unless an initramfs will be used.</para>
66 </listitem>
67 </varlistentry>
68
69 </variablelist>
70
71 <indexterm zone="grub-setup uefi-kernel">
72 <primary sortas="d-uefi">UEFI</primary>
73 </indexterm>
74 </sect2>
75
76 <sect2>
77 <title>Create an Emergency Boot Disk</title>
78
79 <para>
80 Ensure that an emergency boot disk is ready to <quote>rescue</quote>
81 the system in case the system becomes un-bootable. To make an
82 emergency boot disk with GRUB for an EFI based system, find a spare
83 USB flash drive and create a
84 <systemitem class="filesystem">vfat</systemitem> file system on it.
85 Install <xref linkend="dosfstools"/> first, then
86 as the <systemitem class="username">root</systemitem> user:
87 </para>
88
89 <warning>
90 <para>
91 The following command will erase all directories and files in the
92 partition. Make sure your USB flash drive contains no data which
93 will be needed, and change <userinput>sdx1</userinput> to the
94 device node corresponding to the first partition of the USB flash
95 drive. Be careful not to overwrite your hard drive with a typo!
96 </para>
97 </warning>
98
99 <screen role="nodump"><userinput>mkfs.vfat /dev/sdx1</userinput></screen>
100
101 <para>
102 Still as the <systemitem class="username">root</systemitem> user, use
103 the <command>fdisk</command> utility to set the first partition
104 of the USB flash drive to be an <quote>EFI system</quote> partition
105 (change <userinput>sdx</userinput> to the device node corresponding
106 to your USB flash drive):
107 </para>
108
109<screen role="nodump"><userinput>fdisk /dev/sdx</userinput>
110<literal>
111Welcome to fdisk (util-linux 2.39.1).
112Changes will remain in memory only, until you decide to write them.
113Be careful before using the write command.
114
115
116Command (m for help): </literal><userinput>t</userinput>
117<literal>Partition number (1-9, default 9): </literal><userinput>1</userinput>
118<literal>Partition type or alias (type L to list all): </literal><userinput>uefi</userinput>
119<literal>Changed type of partition 'Linux filesystem' to 'EFI System'.
120
121Command (m for help): </literal><userinput>w</userinput>
122<literal>The partition table has been altered.
123Syncing disks.</literal></screen>
124
125 <para>
126 Still as the <systemitem class="username">root</systemitem> user,
127 create a mount point for the EFI partition on the USB flash drive
128 and mount it:
129 </para>
130
131<screen role="nodump"><userinput>mount --mkdir -v -t vfat /dev/sdx1 -o codepage=437,iocharset=iso8859-1 \
132 /mnt/rescue</userinput></screen>
133
134 <para>
135 Install GRUB for EFI on the partition:
136 </para>
137
138<screen role="nodump"><userinput>grub-install --target=x86_64-efi --removable \
139 --efi-directory=/mnt/rescue --boot-directory=/mnt/rescue</userinput></screen>
140
141 <para>
142 Unmount the partition:
143 </para>
144
145<screen role="nodump"><userinput>umount /mnt/rescue</userinput></screen>
146
147 <para>
148 Now the USB flash drive can be used as an emergency boot disk on any x86-64
149 UEFI platform. To learn how to select this flash drive as the boot
150 device, read the manual of your motherboard or laptop.
151 It will boot the system and show the GRUB shell. Then you
152 can type commands to boot your operating system from the hard drive.
153 </para>
154
155 </sect2>
156
157 <sect2>
158 <title>Find or Create the EFI System Partition</title>
159
160 <para>
161 On EFI based systems, the bootloaders are installed in a special FAT32
162 partition called an <emphasis>EFI System Partition</emphasis> (ESP).
163 If your system supports EFI, and a recent version of some Linux
164 distribution or Windows is pre-installed, it's likely that the ESP
165 has already been created. As the
166 <systemitem class="username">root</systemitem> user, list all the
167 partitions on your hard drive (replace <userinput>sda</userinput>
168 with the device corresponding to the appropriate hard drive):
169 </para>
170
171 <screen role="nodump"><userinput>fdisk -l /dev/sda</userinput></screen>
172
173 <para>
174 The <quote>Type</quote> column of the ESP should be
175 <literal>EFI System</literal>.
176 </para>
177
178 <para>
179 If the system or the hard drive is new, or it's the first
180 installation of a UEFI-booted OS on the system, the ESP may not exist.
181 In that case, install <xref linkend='dosfstools'/> first. Then create
182 a new partition, make a
183 <systemitem class="filesystem">vfat</systemitem> file system on it,
184 and set the partition type to <quote>EFI system.</quote> See the
185 instructions for the emergency boot device above as a reference.
186 </para>
187
188 <warning>
189 <para>
190 Some (old) UEFI implementations may require the ESP to be the first
191 partition on the disk.
192 </para>
193 </warning>
194
195 <para>
196 Now, as the <systemitem class="username">root</systemitem> user,
197 create the mount point for the ESP, and mount it (replace
198 <userinput>sda1</userinput> with the device node corresponding to
199 the ESP):
200 </para>
201
202<screen role="nodump"><userinput>mount --mkdir -v -t vfat /dev/sda1 -o codepage=437,iocharset=iso8859-1 \
203 /boot/efi</userinput></screen>
204
205 <para>
206 If you want to mount the ESP automatically during system boot,
207 as the &root; user, add an entry for the ESP into
208 <filename>/etc/fstab</filename>:
209 </para>
210
211<screen role="nodump"><userinput>cat &gt;&gt; /etc/fstab &lt;&lt; EOF</userinput>
212<literal>/dev/sda1 /boot/efi vfat codepage=437,iocharset=iso8859-1 0 1</literal>
213<userinput>EOF</userinput></screen>
214
215 </sect2>
216
217 <sect2>
218 <title>Minimal Boot Configuration with GRUB and EFI</title>
219
220 <para>
221 On UEFI based systems, GRUB works by installing an EFI application
222 (a special kind of executable) into the ESP. The EFI firmware will
223 search boot loaders in EFI applications from boot entries recorded
224 in EFI variables, and additionally a hardcoded path
225 <filename>EFI/BOOT/BOOTX64.EFI</filename>. Normally, a boot loader
226 should be installed into a custom path and the path should be recorded
227 in the EFI variables. The use of the hardcoded path should be
228 avoided if possible. However, in some cases we have to use
229 the hardcoded path:
230 </para>
231
232 <itemizedlist>
233 <listitem>
234 <para>
235 The system is not booted with EFI yet, making EFI variables
236 inaccessible.
237 </para>
238 </listitem>
239 <listitem>
240 <para>
241 The EFI firmware is 64-bit but the LFS system is 32-bit, making
242 EFI variables inaccessible because the kernel cannot invoke EFI
243 runtime services with a different virtual address length.
244 </para>
245 </listitem>
246 <listitem>
247 <para>
248 LFS is built for a Live USB, so we cannot rely on EFI variables,
249 which are stored in NVRAM or EEPROM on the local machine.
250 </para>
251 </listitem>
252 <listitem>
253 <para>
254 You are unable or unwilling to install the
255 <application>efibootmgr</application> for manipulating boot
256 entries in EFI variables.
257 </para>
258 </listitem>
259 </itemizedlist>
260
261 <para>
262 In these cases, follow these instructions to install the GRUB EFI
263 application into the hardcoded path and make a minimal boot
264 configuration. Otherwise it's better to skip ahead and set up the
265 boot configuration normally.
266 </para>
267
268 <para>
269 To install GRUB with the EFI application in the hardcoded
270 path <filename>EFI/BOOT/BOOTX64.EFI</filename>, first ensure the boot
271 partition is mounted at <filename class="directory">/boot</filename>
272 and the ESP is mounted at
273 <filename class="directory">/boot/efi</filename>. Then, as the &root;
274 user, run the command:
275 </para>
276
277 <note>
278 <para>
279 This command will overwrite
280 <filename>/boot/efi/EFI/BOOT/BOOTX64.EFI</filename>. It may break a
281 bootloader already installed there. Back it up if you are not sure.
282 </para>
283 </note>
284
285 <screen role="nodump"><userinput>grub-install --target=x86_64-efi --removable</userinput></screen>
286
287 <para>
288 This command will install the GRUB EFI application into the hardcoded path
289 <filename>/boot/efi/EFI/BOOT/BOOTX64.EFI</filename>, so the EFI
290 firmware can find and load it. The remaining GRUB files are installed
291 in the <filename class="directory">/boot/grub</filename> directory and
292 will be loaded by <filename>BOOTX64.EFI</filename> during system boot.
293 </para>
294
295 <note>
296 <para>
297 The EFI firmware usually prefers the EFI applications with a path
298 stored in EFI variables to the EFI
299 application at the hardcoded path. So you may need to invoke the
300 boot selection menu or firmware setting interface to select the
301 newly installed GRUB manually on the next boot. Read the manual of
302 your motherboard or laptop to learn how.
303 </para>
304 </note>
305
306 <para>
307 If you've followed the instructions in this section and set up a minimal boot
308 configuration, now skip ahead to
309 <quote>Creating the GRUB Configuration File.</quote>
310 </para>
311 </sect2>
312
313 <sect2>
314 <title>Mount the EFI Variable File System</title>
315
316 <para>
317 The installation of GRUB on a UEFI platform requires that the EFI Variable
318 file system, <systemitem class="filesystem">efivarfs</systemitem>, is
319 mounted. As the <systemitem class="username">root</systemitem> user,
320 mount it if it's not already mounted:
321 </para>
322
323<screen role="nodump"><userinput>mountpoint /sys/firmware/efi/efivars || mount -v -t efivarfs efivarfs /sys/firmware/efi/efivars</userinput></screen>
324
325 <note revision="systemd">
326 <para>
327 If the system is booted with UEFI and systemd,
328 <systemitem class="filesystem">efivarfs</systemitem> will be mounted
329 automatically. However, in the LFS chroot environment it still needs to
330 be mounted manually.
331 </para>
332 </note>
333
334 <para revision="sysv">
335 Now add an entry for the
336 <systemitem class="filesystem">efivarfs</systemitem> in
337 <filename>/etc/fstab</filename> so it will be mounted automatically
338 during system boot:
339 </para>
340
341<screen revision="sysv" role="nodump"><userinput>cat &gt;&gt; /etc/fstab &lt;&lt; EOF</userinput>
342<literal>efivarfs /sys/firmware/efi/efivars efivarfs defaults 0 0</literal>
343<userinput>EOF</userinput></screen>
344
345 <warning>
346 <para>
347 If the system is not booted with UEFI, the directory
348 <filename class="directory">/sys/firmware/efi</filename> will be
349 missing. In this case you should boot the system in UEFI mode with
350 the emergency boot disk or using a minimal boot configuration created as
351 above, then mount
352 <systemitem class="filesystem">efivarfs</systemitem> and continue.
353 </para>
354 </warning>
355 </sect2>
356
357 <sect2>
358 <title>Setting Up the Configuration</title>
359
360 <para>
361 On UEFI based systems, GRUB works by installing an EFI application
362 (a special kind of executable) into
363 <filename class="directory">/boot/efi/EFI/[id]/grubx64.efi</filename>,
364 where <filename class="directory">/boot/efi</filename> is the mount
365 point of the ESP, and <literal>[id]</literal> is replaced with an
366 identifier specified in the <command>grub-install</command> command
367 line. GRUB will create an entry in the EFI variables containing
368 the path <literal>EFI/[id]/grubx64.efi</literal> so the EFI firmware
369 can find <filename>grubx64.efi</filename> and load it.
370 </para>
371
372 <para>
373 <filename>grubx64.efi</filename> is very lightweight (136 KB with
374 GRUB-2.06) so it will not use much space in the ESP. A typical ESP
375 size is 100 MB (for Windows boot manager, which uses about 50 MB in
376 the ESP). Once <filename>grubx64.efi</filename> has been loaded by the
377 firmware, it will load GRUB modules from the boot partition.
378 The default location is
379 <filename class="directory">/boot/grub</filename>.
380 </para>
381
382 <para>
383 As the <systemitem class="username">root</systemitem> user, install
384 the GRUB files into <filename>/boot/efi/EFI/LFS/grubx64.efi</filename>
385 and <filename class="directory">/boot/grub</filename>. Then set up the
386 boot entry in the EFI variables:
387 </para>
388
389<screen role="nodump"><userinput>grub-install --bootloader-id=LFS --recheck</userinput></screen>
390
391 <para>
392 If the installation is successful, the output should be:
393 </para>
394
395<screen role="nodump"><literal>Installing for x86_64-efi platform.
396Installation finished. No error reported.</literal></screen>
397
398 <para>
399 Issue the <command>efibootmgr | cut -f 1</command> command to recheck the EFI boot
400 configuration. An example of the output is:
401 </para>
402
403<screen role="nodump"><literal>BootCurrent: 0000
404Timeout: 1 seconds
405BootOrder: 0005,0000,0002,0001,0003,0004
406Boot0000* ARCH
407Boot0001* UEFI:CD/DVD Drive
408Boot0002* Windows Boot Manager
409Boot0003* UEFI:Removable Device
410Boot0004* UEFI:Network Device
411Boot0005* LFS</literal></screen>
412
413 <para>
414 Note that <literal>0005</literal> is the first in the
415 <literal>BootOrder</literal>, and <literal>Boot0005</literal>
416 is <literal>LFS</literal>. This means that on the next boot, the
417 version of GRUB installed by LFS will be used to boot the system.
418 </para>
419
420 </sect2>
421
422 <sect2>
423 <title>Creating the GRUB Configuration File</title>
424
425 <indexterm zone="grub-setup grub-video">
426 <primary sortas="e-boot-grub-grub-cfg">/boot/grub/grub.cfg</primary>
427 </indexterm>
428
429 <para>
430 Generate <filename>/boot/grub/grub.cfg</filename> to configure the
431 boot menu of GRUB:
432 </para>
433
434<screen role="nodump"><userinput>cat &gt; /boot/grub/grub.cfg &lt;&lt; EOF</userinput>
435<literal># Begin /boot/grub/grub.cfg
436set default=0
437set timeout=5
438
439insmod part_gpt
440insmod ext2
441set root=(hd0,2)
442
443insmod efi_gop<!-- to editors: do not use all_video which -->
444insmod efi_uga<!-- breaks Linux simpledrm driver in QEMU -->
445if loadfont /boot/grub/fonts/unicode.pf2; then
446 terminal_output gfxterm
447fi
448
449menuentry "GNU/Linux, Linux &lfs-kernel-ver;" {
450 linux /boot/vmlinuz-&lfs-kernel-ver; root=/dev/sda2 ro
451}
452
453menuentry "Firmware Setup" {
454 fwsetup
455}</literal>
456<userinput>EOF</userinput></screen>
457
458 <para>
459 Refer to <ulink url='&lfs-root;/chapter10/grub.html'>the LFS
460 book</ulink> for the basic knowledge about the
461 <filename>grub.cfg</filename> file.
462 <literal>(hd0,2)</literal>, <literal>sda2</literal>, and
463 <literal>&lfs-kernel-ver;</literal> must match your
464 configuration.
465 </para>
466
467 <para>
468 The <command>insmod efi_gop</command> and
469 <command>insmod efi_uga</command> directives load two modules for
470 EFI-based video support. On most systems the
471 <literal>efi_gop</literal> module is enough. The
472 <literal>efi_uga</literal> module is only useful for legacy systems,
473 but it's harmless to load it anyway. The video support is needed for
474 the <command>terminal_output gfxterm</command> directive to really
475 work.
476 </para>
477
478 <para>
479 The <command>terminal_output gfxterm</command> directive changes
480 the display resolution of the GRUB menu to match your display device.
481 It will break the rendering if the <filename>unicode.pf2</filename>
482 font data file is not loaded, so it's guarded by a
483 <command>if</command> directive.
484 </para>
485
486 <note>
487 <para>
488 From GRUB's perspective, the files are relative to the partitions
489 used. If you used a separate /boot partition, remove /boot from the
490 above paths (to kernel and to <filename>unicode.pf2</filename>). You
491 will also need to change the "set root" line to point to the boot
492 partition.
493 </para>
494 </note>
495
496 <para>
497 The <literal>Firmware Setup</literal> entry can be used to enter the
498 configuration interface provided by the firmware (sometimes called
499 <quote>BIOS configuration</quote>).
500 </para>
501 </sect2>
502
503 <sect2>
504 <title>Dual-booting with Windows</title>
505
506 <para>
507 Add a menu entry for Windows into <filename>grub.cfg</filename>:
508 </para>
509
510<screen role="nodump"><userinput>cat &gt;&gt; /boot/grub/grub.cfg &lt;&lt; EOF</userinput>
511<literal># Begin Windows addition
512
513menuentry "Windows 11" {
514 insmod fat
515 insmod chain
516 set root=(hd0,1)
517 chainloader /EFI/Microsoft/Boot/bootmgfw.efi
518}</literal>
519<userinput>EOF</userinput></screen>
520
521 <para>
522 <literal>(hd0,1)</literal> should be replaced with the GRUB
523 designated name for the ESP. The <literal>chainloader</literal>
524 directive can be used to tell GRUB to run another EFI executable,
525 in this case the Windows Boot Manager. You may put more usable tools
526 in EFI executable format (for example, an EFI shell) into the ESP and
527 create GRUB entries for them, as well.
528 </para>
529
530 </sect2>
531
532</sect1>
Note: See TracBrowser for help on using the repository browser.