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

12.0 12.1 kea ken/TL2024 ken/tuningfonts lazarus plabs/newcss python3.11 rahul/power-profiles-daemon renodr/vulkan-addition trunk xry111/llvm18 xry111/xf86-video-removal
Last change on this file since 7befd975 was 7befd975, checked in by Xi Ruoyao <xry111@…>, 11 months ago

grub-setup: dosfstools is needed for creating FS on ESP

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