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

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

grub-setup: Simplify the command mounting ESP

Add the fstab entry before mounting the partition, so we can omit device
node path and mount option. Also use --mkdir option so we don't need
mkdir -pv.

By the way bump the kernel version and LFS version in example grub.cfg.

  • Property mode set to 100644
File size: 17.8 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
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>
[d5cc78a]18 BLFS does not have the essential packages to support Secure Boot. To
[c8391c3d]19 set up the boot process with GRUB and UEFI in BLFS, Secure
[756a787]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>
[d5cc78a]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
[756a787]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
[b9c353b]53 the <command>fdisk</command> utility to set the first partition
[756a787]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>
[16f7f0e]61Welcome to fdisk (util-linux 2.38.1).
[756a787]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>
[43397b0e]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'.
[756a787]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
[1977aef]89<screen role="nodump"><userinput>grub-install --target=x86_64-efi --removable --efi-directory=/mnt/rescue --boot-directory=/mnt/rescue</userinput></screen>
[756a787]90
91 <para>
92 Unmount the partition:
93 </para>
94
95<screen role="nodump"><userinput>umount /mnt/rescue</userinput></screen>
96
97 <para>
[c8391c3d]98 Now the USB flash drive can be used as an emergency boot disk on any x86-64
[d5cc78a]99 UEFI platform. It will boot the system and show the GRUB shell. Then you
[c8391c3d]100 can type commands to boot your operating system from the hard drive.
[d5cc78a]101 To learn how to select the boot device, read the manual of your
[756a787]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
[6af847b]115 <xi:include xmlns:xi="http://www.w3.org/2001/XInclude"
116 href="grub-setup-kernel.xml"/>
[756a787]117
118 <variablelist>
[c8391c3d]119 <title>The meaning of the configuration options:</title>
[756a787]120
[2266a90]121 <varlistentry>
122 <term><parameter>CONFIG_PARTITION_ADVANCED</parameter></term>
123 <listitem>
124 <para>If it's not enabled,
125 <parameter>CONFIG_EFI_PARTITION</parameter> will be enabled
126 automatically. But when it's enabled, you must set
127 <parameter>CONFIG_EFI_PARTITION</parameter> to enabled as
128 well.</para>
129 </listitem>
130 </varlistentry>
131
[756a787]132 <varlistentry>
[217e54c9]133 <term>
134 <parameter>CONFIG_SYSFB_SIMPLEFB</parameter>,
135 <parameter>CONFIG_DRM</parameter>,
[34e3e2d]136 <parameter>CONFIG_DRM_FBDEV_EMULATION</parameter>,
[9c44927]137 <parameter>CONFIG_DRM_SIMPLEDRM</parameter>,
[217e54c9]138 <parameter>CONFIG_FB</parameter>, and
139 <parameter>CONFIG_FRAMEBUFFER_CONSOLE</parameter></term>
[756a787]140 <listitem>
[34e3e2d]141 <para>The combination of these options provides the Linux console
142 support on top of the UEFI framebuffer. To allow the kernel to
143 print debug messages at an early boot stage, they shouldn't be
144 built as kernel modules unless an initramfs will be used.</para>
[756a787]145 </listitem>
146 </varlistentry>
147
148 </variablelist>
149
[10b89821]150 <indexterm zone="grub-setup uefi-kernel">
151 <primary sortas="d-uefi">UEFI</primary>
152 </indexterm>
[756a787]153 </sect2>
154
155 <sect2>
156 <title>Find or Create the EFI System Partition</title>
157
158 <para>
[c8391c3d]159 On EFI based systems, the bootloaders are installed in a special FAT32
[d5cc78a]160 partition called an <emphasis>EFI System Partition</emphasis> (ESP).
[c8391c3d]161 If your system supports EFI, and a recent version of some Linux
[756a787]162 distribution or Windows is pre-installed, it's likely that the ESP
[c8391c3d]163 has already been created. As the
[756a787]164 <systemitem class="username">root</systemitem> user, list all the
165 partitions on your hard drive (replace <userinput>sda</userinput>
[d5cc78a]166 with the device corresponding to the appropriate hard drive):
[756a787]167 </para>
168
169 <screen role="nodump"><userinput>fdisk -l /dev/sda</userinput></screen>
170
171 <para>
172 The <quote>Type</quote> column of the ESP should be
173 <literal>EFI System</literal>.
174 </para>
175
176 <para>
[c8391c3d]177 If the system or the hard drive is new, or it's the first
178 installation of a UEFI-booted OS on the system, the ESP may not exist.
[7befd975]179 In that case, install <xref linkend='dosfstools'/> first. Then create
180 a new partition, make a
[756a787]181 <systemitem class="filesystem">vfat</systemitem> file system on it,
[d5cc78a]182 and set the partition type to <quote>EFI system</quote>. See the
[756a787]183 instructions for the emergency boot device above as a reference.
184 </para>
185
186 <warning>
187 <para>
[c8391c3d]188 Some (old) UEFI implementations may require the ESP to be the first
[756a787]189 partition on the disk.
190 </para>
191 </warning>
192
193 <para>
[0821043]194 As the &root; user, add an entry for the ESP in
195 <filename>/etc/fstab</filename>, so it will be mounted automatically
196 during system boot (replace
[756a787]197 <userinput>sda1</userinput> with the device node corresponding to
198 the ESP):
199 </para>
200
[0821043]201<screen role="nodump"><userinput>cat &gt;&gt; /etc/fstab &lt;&lt; EOF</userinput>
202<literal>/dev/sda1 /boot/efi vfat codepage=437,iocharset=iso8859-1 0 1</literal>
203<userinput>EOF</userinput></screen>
[756a787]204
205 <para>
[0821043]206 Still as the &root; user,
207 create the mount point for the ESP and mount it:
[756a787]208 </para>
209
[0821043]210<screen role="nodump"><userinput>mount /boot/efi --mkdir</userinput></screen>
[756a787]211
212 </sect2>
213
[6db606f]214 <sect2>
215 <title>Minimal Boot Configuration with GRUB and EFI</title>
216
217 <para>
218 On UEFI based systems, GRUB works by installing an EFI application
219 (a special kind of executable) into the ESP. The EFI firmware will
220 search boot loaders in EFI applications from boot entries recorded
221 in EFI variables, and additionally a hardcoded path
222 <filename>EFI/BOOT/BOOTX64.EFI</filename>. Normally, a boot loader
223 should be installed into a custom path and the path should be recorded
[c8391c3d]224 in the EFI variables. The use of the hardcoded path should be
225 avoided if possible. However, in some cases we have to use
[6db606f]226 the hardcoded path:
227 </para>
228
229 <itemizedlist>
230 <listitem>
231 <para>
[c8391c3d]232 The system is not booted with EFI yet, making EFI variables
[6db606f]233 inaccessible.
234 </para>
235 </listitem>
236 <listitem>
237 <para>
[c8391c3d]238 The EFI firmware is 64-bit but the LFS system is 32-bit, making
[6db606f]239 EFI variables inaccessible because the kernel cannot invoke EFI
240 runtime services with a different virtual address length.
241 </para>
242 </listitem>
243 <listitem>
244 <para>
[c8391c3d]245 LFS is built for a Live USB, so we cannot rely on EFI variables,
246 which are stored in NVRAM or EEPROM on the local machine.
[6db606f]247 </para>
248 </listitem>
249 <listitem>
250 <para>
[c8391c3d]251 You are unable or unwilling to install the
[6db606f]252 <application>efibootmgr</application> for manipulating boot
253 entries in EFI variables.
254 </para>
255 </listitem>
256 </itemizedlist>
257
258 <para>
[c8391c3d]259 In these cases, follow these instructions to install the GRUB EFI
[6db606f]260 application into the hardcoded path and make a minimal boot
[c8391c3d]261 configuration. Otherwise it's better to skip ahead and set up the
262 boot configuration normally.
[6db606f]263 </para>
264
265 <para>
[c8391c3d]266 To install GRUB with the EFI application in the hardcoded
[6db606f]267 path <filename>EFI/BOOT/BOOTX64.EFI</filename>, first ensure the boot
[b38aed5]268 partition is mounted at <filename class="directory">/boot</filename>
[c8391c3d]269 and the ESP is mounted at
270 <filename class="directory">/boot/efi</filename>. Then, as the &root;
[6db606f]271 user, run the command:
272 </para>
273
274 <note>
275 <para>
[c8391c3d]276 This command will overwrite
[6db606f]277 <filename>/boot/efi/EFI/BOOT/BOOTX64.EFI</filename>. It may break a
278 bootloader already installed there. Back it up if you are not sure.
279 </para>
280 </note>
281
282 <screen role="nodump"><userinput>grub-install --target=x86_64-efi --removable</userinput></screen>
283
284 <para>
[c8391c3d]285 This command will install the GRUB EFI application into the hardcoded path
[6db606f]286 <filename>/boot/efi/EFI/BOOT/BOOTX64.EFI</filename>, so the EFI
287 firmware can find and load it. The remaining GRUB files are installed
[c8391c3d]288 in the <filename class="directory">/boot/grub</filename> directory and
[6db606f]289 will be loaded by <filename>BOOTX64.EFI</filename> during system boot.
290 </para>
291
292 <note>
293 <para>
[c8391c3d]294 The EFI firmware usually prefers the EFI applications with a path
295 stored in EFI variables to the EFI
[6db606f]296 application at the hardcoded path. So you may need to invoke the
297 boot selection menu or firmware setting interface to select the
298 newly installed GRUB manually on the next boot. Read the manual of
[c8391c3d]299 your motherboard or laptop to learn how.
[6db606f]300 </para>
301 </note>
302
303 <para>
[c8391c3d]304 If you've followed the instructions in this section and set up a minimal boot
305 configuration, now skip ahead to
[6db606f]306 <quote>Creating the GRUB Configuration File</quote>.
307 </para>
308 </sect2>
309
[756a787]310 <sect2>
311 <title>Mount the EFI Variable File System</title>
312
313 <para>
[d5cc78a]314 The installation of GRUB on a UEFI platform requires that the EFI Variable
[c8391c3d]315 file system, <systemitem class="filesystem">efivarfs</systemitem>, is
[564a569]316 mounted. As the <systemitem class="username">root</systemitem> user,
317 mount it if it's not already mounted:
[756a787]318 </para>
319
[564a569]320<screen role="nodump"><userinput>mountpoint /sys/firmware/efi/efivars || mount -v -t efivarfs efivarfs /sys/firmware/efi/efivars</userinput></screen>
[756a787]321
322 <note revision="systemd">
323 <para>
324 If the system is booted with UEFI and systemd,
325 <systemitem class="filesystem">efivarfs</systemitem> will be mounted
[c8391c3d]326 automatically. However, in the LFS chroot environment it still needs to
[756a787]327 be mounted manually.
328 </para>
329 </note>
330
331 <para revision="sysv">
[d5cc78a]332 Now add an entry for the
[756a787]333 <systemitem class="filesystem">efivarfs</systemitem> in
[d5cc78a]334 <filename>/etc/fstab</filename> so it will be mounted automatically
[756a787]335 during system boot:
336 </para>
337
338<screen revision="sysv" role="nodump"><userinput>cat &gt;&gt; /etc/fstab &lt;&lt; EOF</userinput>
[6ca17c82]339<literal>efivarfs /sys/firmware/efi/efivars efivarfs defaults 0 0</literal>
[756a787]340<userinput>EOF</userinput></screen>
341
342 <warning>
343 <para>
344 If the system is not booted with UEFI, the directory
345 <filename class="directory">/sys/firmware/efi</filename> will be
346 missing. In this case you should boot the system in UEFI mode with
[c8391c3d]347 the emergency boot disk or using a minimal boot configuration created as
[6db606f]348 above, then mount
349 <systemitem class="filesystem">efivarfs</systemitem> and continue.
[756a787]350 </para>
351 </warning>
352 </sect2>
353
354 <sect2>
355 <title>Setting Up the Configuration</title>
356
357 <para>
358 On UEFI based systems, GRUB works by installing an EFI application
[d5cc78a]359 (a special kind of executable) into
[756a787]360 <filename class="directory">/boot/efi/EFI/[id]/grubx64.efi</filename>,
361 where <filename class="directory">/boot/efi</filename> is the mount
362 point of the ESP, and <literal>[id]</literal> is replaced with an
363 identifier specified in the <command>grub-install</command> command
[d5cc78a]364 line. GRUB will create an entry in the EFI variables containing
365 the path <literal>EFI/[id]/grubx64.efi</literal> so the EFI firmware
[756a787]366 can find <filename>grubx64.efi</filename> and load it.
367 </para>
368
369 <para>
370 <filename>grubx64.efi</filename> is very lightweight (136 KB with
[16f7f0e]371 GRUB-2.06) so it will not use much space in the ESP. A typical ESP
[d5cc78a]372 size is 100 MB (for Windows boot manager, which uses about 50 MB in
[c8391c3d]373 the ESP). Once <filename>grubx64.efi</filename> has been loaded by the
374 firmware, it will load GRUB modules from the boot partition.
[756a787]375 The default location is
376 <filename class="directory">/boot/grub</filename>.
377 </para>
378
379 <para>
380 As the <systemitem class="username">root</systemitem> user, install
381 the GRUB files into <filename>/boot/efi/EFI/LFS/grubx64.efi</filename>
[d5cc78a]382 and <filename class="directory">/boot/grub</filename>. Then set up the
[756a787]383 boot entry in the EFI variables:
384 </para>
385
386<screen role="nodump"><userinput>grub-install --bootloader-id=LFS --recheck</userinput></screen>
387
388 <para>
389 If the installation is successful, the output should be:
390 </para>
391
392<screen role="nodump"><literal>Installing for x86_64-efi platform.
393Installation finished. No error reported.</literal></screen>
394
395 <para>
[c8391c3d]396 Issue the <command>efibootmgr | cut -f 1</command> command to recheck the EFI boot
[756a787]397 configuration. An example of the output is:
398 </para>
399
400<screen role="nodump"><literal>BootCurrent: 0000
401Timeout: 1 seconds
402BootOrder: 0005,0000,0002,0001,0003,0004
403Boot0000* ARCH
404Boot0001* UEFI:CD/DVD Drive
405Boot0002* Windows Boot Manager
406Boot0003* UEFI:Removable Device
407Boot0004* UEFI:Network Device
408Boot0005* LFS</literal></screen>
409
410 <para>
[d5cc78a]411 Note that <literal>0005</literal> is the first in the
[756a787]412 <literal>BootOrder</literal>, and <literal>Boot0005</literal>
[8558044]413 is <literal>LFS</literal>. This means that on the next boot, the
[d5cc78a]414 version of GRUB installed by LFS will be used to boot the system.
[756a787]415 </para>
416
417 </sect2>
418
419 <sect2>
420 <title>Creating the GRUB Configuration File</title>
421
422 <indexterm zone="grub-setup grub-video">
423 <primary sortas="e-boot-grub-grub-cfg">/boot/grub/grub.cfg</primary>
424 </indexterm>
425
426 <para>
427 Generate <filename>/boot/grub/grub.cfg</filename> to configure the
428 boot menu of GRUB:
429 </para>
430
431<screen role="nodump"><userinput>cat &gt; /boot/grub/grub.cfg &lt;&lt; EOF</userinput>
432<literal># Begin /boot/grub/grub.cfg
433set default=0
434set timeout=5
435
436insmod part_gpt
437insmod ext2
438set root=(hd0,2)
439
[0cef3a67]440insmod all_video
[756a787]441if loadfont /boot/grub/fonts/unicode.pf2; then
442 terminal_output gfxterm
443fi
444
[0821043]445menuentry "GNU/Linux, Linux 6.4.10-lfs-12.0" {
446 linux /boot/vmlinuz-6.4.10-lfs-12.0 root=/dev/sda2 ro
[756a787]447}
448
449menuentry "Firmware Setup" {
450 fwsetup
451}</literal>
452<userinput>EOF</userinput></screen>
453
454 <para>
455 <literal>(hd0,2)</literal>, <literal>sda2</literal>, and
[8e843925]456 <literal>6.1.11-lfs-11.3</literal> must match your
[756a787]457 configuration.
458 </para>
459
460 <note>
461 <para>
[c8391c3d]462 From GRUB's perspective, the files are relative to the partitions
463 used. If you used a separate /boot partition, remove /boot from the
[756a787]464 above paths (to kernel and to <filename>unicode.pf2</filename>). You
[c8391c3d]465 will also need to change the "set root" line to point to the boot
[756a787]466 partition.
467 </para>
468 </note>
469
470 <para>
471 The <literal>Firmware Setup</literal> entry can be used to enter the
472 configuration interface provided by the firmware (sometimes called
[9039d65]473 <quote>BIOS configuration</quote>).
[756a787]474 </para>
475 </sect2>
476
477 <sect2>
478 <title>Dual-booting with Windows</title>
479
480 <para>
481 Add a menu entry for Windows into <filename>grub.cfg</filename>:
482 </para>
483
484<screen role="nodump"><userinput>cat &gt;&gt; /boot/grub/grub.cfg &lt;&lt; EOF</userinput>
485<literal># Begin Windows addition
486
[16f7f0e]487menuentry "Windows 11" {
[756a787]488 insmod fat
489 insmod chain
490 set root=(hd0,1)
491 chainloader /EFI/Microsoft/Boot/bootmgfw.efi
492}</literal>
493<userinput>EOF</userinput></screen>
494
495 <para>
496 <literal>(hd0,1)</literal> should be replaced with the GRUB
[d5cc78a]497 designated name for the ESP. The <literal>chainloader</literal>
[756a787]498 directive can be used to tell GRUB to run another EFI executable,
499 in this case the Windows Boot Manager. You may put more usable tools
[8558044]500 in EFI executable format (for example, an EFI shell) into the ESP and
[c8391c3d]501 create GRUB entries for them, as well.
[756a787]502 </para>
503
504 </sect2>
505
506</sect1>
Note: See TracBrowser for help on using the repository browser.