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

11.3 12.0 12.1 kea ken/TL2024 ken/inkscape-core-mods ken/tuningfonts lazarus lxqt plabs/newcss plabs/python-mods python3.11 qt5new rahul/power-profiles-daemon renodr/vulkan-addition trunk xry111/llvm18 xry111/xf86-video-removal
Last change on this file since 1ff0f0c was b804521, checked in by Xi Ruoyao <xry111@…>, 19 months ago

grub UEFI setup: realign kernel configurations

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