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

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 b804521 was b804521, checked in by Xi Ruoyao <xry111@…>, 20 months ago

grub UEFI setup: realign kernel configurations

  • Property mode set to 100644
File size: 19.5 KB
RevLine 
[756a787]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>
[fda29e4]12 <date>$Date$</date>
[756a787]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>
[d5cc78a]21 BLFS does not have the essential packages to support Secure Boot. To
[c8391c3d]22 set up the boot process with GRUB and UEFI in BLFS, Secure
[756a787]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>
[d5cc78a]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
[756a787]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
[b9c353b]56 the <command>fdisk</command> utility to set the first partition
[756a787]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>
[16f7f0e]64Welcome to fdisk (util-linux 2.38.1).
[756a787]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>
[43397b0e]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'.
[756a787]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
[1977aef]92<screen role="nodump"><userinput>grub-install --target=x86_64-efi --removable --efi-directory=/mnt/rescue --boot-directory=/mnt/rescue</userinput></screen>
[756a787]93
94 <para>
95 Unmount the partition:
96 </para>
97
98<screen role="nodump"><userinput>umount /mnt/rescue</userinput></screen>
99
100 <para>
[c8391c3d]101 Now the USB flash drive can be used as an emergency boot disk on any x86-64
[d5cc78a]102 UEFI platform. It will boot the system and show the GRUB shell. Then you
[c8391c3d]103 can type commands to boot your operating system from the hard drive.
[d5cc78a]104 To learn how to select the boot device, read the manual of your
[756a787]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;
[b804521]119 [*] EFI runtime service support [CONFIG_EFI]
120 [*] EFI stub support [CONFIG_EFI_STUB]
[756a787]121Enable the block layer ---&gt;
122 Partition Types ---&gt;
[b804521]123 [*] Advanced partition selection [CONFIG_PARTITION_ADVANCED]
124 [*] EFI GUID Partition support [CONFIG_EFI_PARTITION]
[756a787]125Device Drivers ---&gt;
[62d4a239]126 Firmware Drivers ---&gt;
[b804521]127 [*] Mark VGA/VBE/EFI FB as generic system framebuffer [CONFIG_SYSFB_SIMPLEFB]
[62d4a239]128 EFI (Extensible Firmware Interface) Support ---&gt;
[b804521]129 &lt; &gt; EFI Variable Support via sysfs [CONFIG_EFI_VARS]
[756a787]130 Graphics support ---&gt;
[b804521]131 &lt;*&gt; Direct Rendering Manager [CONFIG_DRM]
[34e3e2d]132 [*] Enable legacy fbdev support for your modesetting driver [CONFIG_DRM_FBDEV_EMULATION]
[b804521]133 &lt;*&gt; Simple framebuffer driver [CONFIG_DRM_SIMPLEDRM]
[756a787]134 Frame buffer Devices ---&gt;
[b804521]135 &lt;*&gt; Support for frame buffer devices ---&gt; [CONFIG_FB]
[756a787]136 Console display driver support ---&gt;
[b804521]137 [*] Framebuffer Console support [CONFIG_FRAMEBUFFER_CONSOLE]
[756a787]138File systems ---&gt;
[7027897]139 &lt;DOS/FAT/EXFAT/NT Filesystems ---&gt;
[b804521]140 &lt;*/M&gt; VFAT (Windows-95) fs support [CONFIG_VFAT_FS]
[756a787]141 Pseudo filesystems ---&gt;
[b804521]142 &lt;*/M&gt; EFI Variable filesystem [CONFIG_EFIVAR_FS]</literal></screen>
[756a787]143
144 <variablelist>
[c8391c3d]145 <title>The meaning of the configuration options:</title>
[756a787]146
147 <varlistentry>
148 <term><parameter>CONFIG_EFI_STUB</parameter></term>
149 <listitem>
[d5cc78a]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
[756a787]153 this option.</para>
154 </listitem>
155 </varlistentry>
156
157 <varlistentry>
158 <term><parameter>CONFIG_EFI_VARS</parameter></term>
159 <listitem>
[d5cc78a]160 <para>Don't use this deprecated option because of a 1024-byte
[756a787]161 variable size limit. Its function is replaced by
162 <parameter>CONFIG_EFIVAR_FS</parameter>.</para>
163 </listitem>
164 </varlistentry>
165
166 <varlistentry>
[217e54c9]167 <term>
168 <parameter>CONFIG_SYSFB_SIMPLEFB</parameter>,
169 <parameter>CONFIG_DRM</parameter>,
[34e3e2d]170 <parameter>CONFIG_DRM_FBDEV_EMULATION</parameter>,
[9c44927]171 <parameter>CONFIG_DRM_SIMPLEDRM</parameter>,
[217e54c9]172 <parameter>CONFIG_FB</parameter>, and
173 <parameter>CONFIG_FRAMEBUFFER_CONSOLE</parameter></term>
[756a787]174 <listitem>
[34e3e2d]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>
[756a787]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>
[c8391c3d]190 On EFI based systems, the bootloaders are installed in a special FAT32
[d5cc78a]191 partition called an <emphasis>EFI System Partition</emphasis> (ESP).
[c8391c3d]192 If your system supports EFI, and a recent version of some Linux
[756a787]193 distribution or Windows is pre-installed, it's likely that the ESP
[c8391c3d]194 has already been created. As the
[756a787]195 <systemitem class="username">root</systemitem> user, list all the
196 partitions on your hard drive (replace <userinput>sda</userinput>
[d5cc78a]197 with the device corresponding to the appropriate hard drive):
[756a787]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>
[c8391c3d]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.
[d5cc78a]210 In that case, create a new partition, make a
[756a787]211 <systemitem class="filesystem">vfat</systemitem> file system on it,
[d5cc78a]212 and set the partition type to <quote>EFI system</quote>. See the
[756a787]213 instructions for the emergency boot device above as a reference.
214 </para>
215
216 <warning>
217 <para>
[c8391c3d]218 Some (old) UEFI implementations may require the ESP to be the first
[756a787]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>
[d5cc78a]234 Add an entry for the ESP in
[756a787]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
[6db606f]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
[c8391c3d]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
[6db606f]257 the hardcoded path:
258 </para>
259
260 <itemizedlist>
261 <listitem>
262 <para>
[c8391c3d]263 The system is not booted with EFI yet, making EFI variables
[6db606f]264 inaccessible.
265 </para>
266 </listitem>
267 <listitem>
268 <para>
[c8391c3d]269 The EFI firmware is 64-bit but the LFS system is 32-bit, making
[6db606f]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>
[c8391c3d]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.
[6db606f]278 </para>
279 </listitem>
280 <listitem>
281 <para>
[c8391c3d]282 You are unable or unwilling to install the
[6db606f]283 <application>efibootmgr</application> for manipulating boot
284 entries in EFI variables.
285 </para>
286 </listitem>
287 </itemizedlist>
288
289 <para>
[c8391c3d]290 In these cases, follow these instructions to install the GRUB EFI
[6db606f]291 application into the hardcoded path and make a minimal boot
[c8391c3d]292 configuration. Otherwise it's better to skip ahead and set up the
293 boot configuration normally.
[6db606f]294 </para>
295
296 <para>
[c8391c3d]297 To install GRUB with the EFI application in the hardcoded
[6db606f]298 path <filename>EFI/BOOT/BOOTX64.EFI</filename>, first ensure the boot
[b38aed5]299 partition is mounted at <filename class="directory">/boot</filename>
[c8391c3d]300 and the ESP is mounted at
301 <filename class="directory">/boot/efi</filename>. Then, as the &root;
[6db606f]302 user, run the command:
303 </para>
304
305 <note>
306 <para>
[c8391c3d]307 This command will overwrite
[6db606f]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>
[c8391c3d]316 This command will install the GRUB EFI application into the hardcoded path
[6db606f]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
[c8391c3d]319 in the <filename class="directory">/boot/grub</filename> directory and
[6db606f]320 will be loaded by <filename>BOOTX64.EFI</filename> during system boot.
321 </para>
322
323 <note>
324 <para>
[c8391c3d]325 The EFI firmware usually prefers the EFI applications with a path
326 stored in EFI variables to the EFI
[6db606f]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
[c8391c3d]330 your motherboard or laptop to learn how.
[6db606f]331 </para>
332 </note>
333
334 <para>
[c8391c3d]335 If you've followed the instructions in this section and set up a minimal boot
336 configuration, now skip ahead to
[6db606f]337 <quote>Creating the GRUB Configuration File</quote>.
338 </para>
339 </sect2>
340
[756a787]341 <sect2>
342 <title>Mount the EFI Variable File System</title>
343
344 <para>
[d5cc78a]345 The installation of GRUB on a UEFI platform requires that the EFI Variable
[c8391c3d]346 file system, <systemitem class="filesystem">efivarfs</systemitem>, is
[564a569]347 mounted. As the <systemitem class="username">root</systemitem> user,
348 mount it if it's not already mounted:
[756a787]349 </para>
350
[564a569]351<screen role="nodump"><userinput>mountpoint /sys/firmware/efi/efivars || mount -v -t efivarfs efivarfs /sys/firmware/efi/efivars</userinput></screen>
[756a787]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
[c8391c3d]357 automatically. However, in the LFS chroot environment it still needs to
[756a787]358 be mounted manually.
359 </para>
360 </note>
361
362 <para revision="sysv">
[d5cc78a]363 Now add an entry for the
[756a787]364 <systemitem class="filesystem">efivarfs</systemitem> in
[d5cc78a]365 <filename>/etc/fstab</filename> so it will be mounted automatically
[756a787]366 during system boot:
367 </para>
368
369<screen revision="sysv" role="nodump"><userinput>cat &gt;&gt; /etc/fstab &lt;&lt; EOF</userinput>
[6ca17c82]370<literal>efivarfs /sys/firmware/efi/efivars efivarfs defaults 0 0</literal>
[756a787]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
[c8391c3d]378 the emergency boot disk or using a minimal boot configuration created as
[6db606f]379 above, then mount
380 <systemitem class="filesystem">efivarfs</systemitem> and continue.
[756a787]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
[d5cc78a]390 (a special kind of executable) into
[756a787]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
[d5cc78a]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
[756a787]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
[16f7f0e]402 GRUB-2.06) so it will not use much space in the ESP. A typical ESP
[d5cc78a]403 size is 100 MB (for Windows boot manager, which uses about 50 MB in
[c8391c3d]404 the ESP). Once <filename>grubx64.efi</filename> has been loaded by the
405 firmware, it will load GRUB modules from the boot partition.
[756a787]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>
[d5cc78a]413 and <filename class="directory">/boot/grub</filename>. Then set up the
[756a787]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>
[c8391c3d]427 Issue the <command>efibootmgr | cut -f 1</command> command to recheck the EFI boot
[756a787]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>
[d5cc78a]442 Note that <literal>0005</literal> is the first in the
[756a787]443 <literal>BootOrder</literal>, and <literal>Boot0005</literal>
[8558044]444 is <literal>LFS</literal>. This means that on the next boot, the
[d5cc78a]445 version of GRUB installed by LFS will be used to boot the system.
[756a787]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
[16f7f0e]477menuentry "GNU/Linux, Linux 5.19.2-lfs-11.2" {
478 linux /boot/vmlinuz-5.19.2-lfs-11.2 root=/dev/sda2 ro
[756a787]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
[c8391c3d]488 <literal>5.19.2-lfs-11.2</literal> must match your
[756a787]489 configuration.
490 </para>
491
492 <note>
493 <para>
[c8391c3d]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
[756a787]496 above paths (to kernel and to <filename>unicode.pf2</filename>). You
[c8391c3d]497 will also need to change the "set root" line to point to the boot
[756a787]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
[9039d65]505 <quote>BIOS configuration</quote>).
[756a787]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
[16f7f0e]519menuentry "Windows 11" {
[756a787]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
[d5cc78a]529 designated name for the ESP. The <literal>chainloader</literal>
[756a787]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
[8558044]532 in EFI executable format (for example, an EFI shell) into the ESP and
[c8391c3d]533 create GRUB entries for them, as well.
[756a787]534 </para>
535
536 </sect2>
537
538</sect1>
Note: See TracBrowser for help on using the repository browser.