<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sect1 PUBLIC "-//OASIS//DTD DocBook XML V4.5//EN"
  "http://www.oasis-open.org/docbook/xml/4.5/docbookx.dtd" [
  <!ENTITY % general-entities SYSTEM "../general.ent">
  %general-entities;
]>

<sect1 id="ch-bootable-grub" role="wrap">
  <?dbhtml filename="grub.html"?>

  <sect1info condition="script">
    <productname>grub</productname>
    <productnumber>&grub-version;</productnumber>
    <address>&grub-url;</address>
  </sect1info>

  <title>使用 GRUB 设定引导过程</title>

  <sect2>
    <title>概述</title>

    <!--warning><para>Configuring GRUB incorrectly can render your system
    inoperable without an alternate boot device such as a CD-ROM.  This
    section is not required to boot your LFS system.  You may just
    want to modify your current boot loader, e.g. Grub-Legacy, GRUB2, or
    LILO.</para></warning-->
	<warning>
		<para>如果您不小心错误地配置了 GRUB，而且没有 CD-ROM
			之类的备用引导设备，可能会导致您的系统完全无法使用。
			本节不是引导您的 LFS 系统的唯一方案，
			您可能只要修改现有的启动加载器（如 Grub-Legacy、GRUB2 或 LILO）
			配置即可引导 LFS。</para>
	</warning>


    <!--para> Ensure that an emergency boot disk is ready to <quote>rescue</quote>
    the computer if the computer becomes unusable (un-bootable).  If you do not
    already have a boot device, you can create one.  In order for the procedure
    below to work, you need to jump ahead to BLFS and install
    <userinput>xorriso</userinput> from the <ulink
    url="&blfs-book;multimedia/libisoburn.html">
    libisoburn</ulink> package.</para-->
    <para>您务必保证自己拥有一个紧急引导磁盘，它在计算机不可用（无法引导）
		时能够 <quote>救援</quote> 计算机。如果您现在还没有引导设备，
		您可以执行以下命令创建一个。在运行下列命令前，您需要跳到 BLFS，
		安装包含 <command>xorriso</command> 的
		<ulink url="&blfs-book;multimedia/libisoburn.html">libisoburn
	</ulink> 软件包：</para>

<screen role="nodump"><userinput>cd /tmp 
grub-mkrescue --output=grub-img.iso 
xorriso -as cdrecord -v dev=/dev/cdrw blank=as_needed grub-img.iso</userinput></screen>

    <note>
      <!--para>
        To boot LFS on host systems that have UEFI enabled, the kernel needs to
        have been built with the CONFIG_EFI_STUB capabality described in the
        previous section.  However, LFS can be booted using GRUB2 without such
        an addition. To do this, the UEFI Mode and Secure Boot capabilities in
        the host system's BIOS need to be turned off.  For details, see <ulink
        url="http://www.linuxfromscratch.org/hints/downloads/files/lfs-uefi.txt">
        the lfs-uefi.txt hint</ulink> at
        http://www.linuxfromscratch.org/hints/downloads/files/lfs-uefi.txt.
      </para-->
	  <para>为了在使用 UEFI 的宿主系统上引导 LFS ，在构建内核时，
		  需要启用 CONFIG_EFI_STUB 功能，正如上一节所述。
		  不过，可以使用 GRUB2 在没有该功能的情况下引导 LFS，
		  前提是在系统 BIOS 设定中关闭 UEFI 模式和安全引导
		  （Secure Boot）功能。关于在 UEFI 环境下引导 LFS 的的详细信息，
		  可以参阅 <ulink url="&hints-root;lfs-uefi.txt"/>。
	  </para>
  </note>
  <note><title>译注</title>
	  <para>以上 hint 版本较老，译者维护了它的更新版本，参阅
<ulink url="http://svn.linuxfromscratch.org/hints/trunk/lfs-uefi-20180409.txt"/>
	/>。
	  </para>
  </note>

  </sect2>

  <sect2>
    <title>GRUB 命名惯例</title>

    <!--para>GRUB uses its own naming structure for drives and partitions in
    the form of <emphasis>(hdn,m)</emphasis>, where <emphasis>n</emphasis>
    is the hard drive number and <emphasis>m</emphasis> is the partition
    number. The hard drive number starts from zero, but the partition number
    starts from one for normal partitions and five for extended partitions.
    Note that this is different from earlier versions where
    both numbers started from zero. For example, partition <filename
    class="partition">sda1</filename> is <emphasis>(hd0,1)</emphasis> to
    GRUB and <filename class="partition">sdb3</filename> is
    <emphasis>(hd1,3)</emphasis>. In contrast to Linux, GRUB does not
    consider CD-ROM drives to be hard drives. For example, if using a CD
    on <filename class="partition">hdb</filename> and a second hard drive
    on <filename class="partition">hdc</filename>, that second hard drive
    would still be <emphasis>(hd1)</emphasis>.</para-->
    <para>GRUB 使用一种独特的命名结构，为驱动器和分区命名。
		分区名的形式为 <emphasis>(hdn,m)</emphasis>，这里
		<emphasis>n</emphasis> 是硬盘驱动器编号，<emphasis>m</emphasis>
		是分区编号。硬盘驱动器编号从 0 开始，但分区号对于主分区来说从
		1 开始，而对于扩展分区来说从 5 开始。例如，分区
		<filename class="partition">sda1</filename> 在 GRUB 中的名字是
		<emphasis>(hd0, 1)</emphasis>，而
		<filename class="partition">sda3</filename> 的名字是
		<emphasis>(hd1, 3)</emphasis>。和 Linux 不同，
		GRUB 不认为 CD-ROM 驱动器属于硬盘驱动器。例如，如果在
		<filename class="partition">hdb</filename> 上有一个 CD-ROM 驱动器，
		而 <filename class="partition">hdc</filename>
		上有一个次要硬盘驱动器，则第二个驱动器仍然名为
		<emphasis>hd1</emphasis>。</para>

  </sect2>

  <sect2>
    <title>设定 GRUB 配置</title>

    <!--para>GRUB works by writing data to the first physical track of the
    hard disk.  This area is not part of any file system.  The programs
    there access GRUB modules in the boot partition.  The default location
    is /boot/grub/.</para-->
    <para>GRUB 的工作方式是，将数据写入硬盘的第一个物理磁道。
		这里不属于任何文件系统，在启动时，
		第一个物理磁道中的程序从引导分区加载 GRUB 模块，
		默认在 /boot/grub 中查找模块。</para>

    <!--para>The location of the boot partition is a choice of the user that
    affects the configuration.  One recommendation is to have a separate small
    (suggested size is 100 MB) partition just for boot information.  That way
    each build, whether LFS or some commercial distro, can access the same boot
    files and access can be made from any booted system.  If you choose to do
    this, you will need to mount the separate partition, move all files in the
    current <filename class="directory">/boot</filename> directory (e.g. the
    linux kernel you just built in the previous section) to the new partition.
    You will then need to unmount the partition and remount it as <filename
    class="directory">/boot</filename>.  If you do this, be sure to update
    <filename>/etc/fstab</filename>.</para-->
    <para>引导分区的位置由负责进行配置的用户自己决定，
		作者推荐创建一个小的（建议大小为 100 MB）分区，
		专门存放引导信息。这样，不同的 Linux 系统（无论是 LFS
		还是商业发行版）在启动时和启动后都能访问相同的引导文件。
		如果您选择这样做，您需要挂载这个单独的分区，将 /boot
		中已有的文件（例如上一节中构建的内核）移动到新的分区中。
		之后，解除该分区的挂载，并将它挂载为
		<filename class="directory">/boot</filename>，
		而且还要注意更新 <filename>/etc/fstab</filename>。</para>

    <!--para>Using the current lfs partition will also work, but configuration
    for multiple systems is more difficult.</para-->
    <para>直接使用 LFS 分区也是可以的，但这样在配置多系统启动时比较麻烦。
    </para>

    <!--para>Using the above information, determine the appropriate
    designator for the root partition (or boot partition, if a separate
    one is used). For the following example, it is assumed that the root
    (or separate boot) partition is <filename
    class="partition">sda2</filename>.</para-->
	<para>根据以上信息，确定 LFS 根分区（或 boot 分区）的名称。
		下面假设 LFS 根分区（或 boot 分区）是
		<filename class="partition">sda2</filename>。</para>

    <para>将 GRUB 文件安装到<filename
    class="directory">/boot/grub</filename> 并设定引导磁道：</para>

    <warning>
      <!--para>The following command will overwrite the current boot loader. Do not
      run the command if this is not desired, for example, if using a third party
      boot manager to manage the Master Boot Record (MBR).</para-->
      <para>以下命令会覆盖当前启动引导器，如果这不是您希望的，
		  不要运行该命令。例如，如果您使用第三方启动引导器管理主引导记录
		  （MBR）。</para>
    </warning>

<screen role="nodump"><userinput>grub-install /dev/sda</userinput></screen>

<!-- This does not seem to be true any more
    <note><para><application>grub-install</application> is a script and calls another
    program, grub-probe, that may fail with a message "cannot stat `/dev/root'".
    If so, create a temporary symbolic link from your root partition to /dev/root:</para>

<screen role="nodump"><userinput>ln -sv /dev/sda2 /dev/root</userinput></screen>

    <para>The symbolic link will only be present until the system is rebooted.
    The link is only needed for the installation procedure.
    </para></note>
-->
  </sect2>

  <sect2 id="grub-cfg">
    <title>创建 GRUB 配置文件</title>

	<para>生成 <filename>/boot/grub/grub.cfg</filename>：</para>

    <screen revision="sysv"><userinput>cat &gt; /boot/grub/grub.cfg &lt;&lt; "EOF"
<literal># Begin /boot/grub/grub.cfg
set default=0
set timeout=5

insmod ext2
set root=(hd0,2)

menuentry "GNU/Linux, Linux &linux-version;-lfs-&version;" {
        linux   /boot/vmlinuz-&linux-version;-lfs-&version; root=/dev/sda2 ro
}</literal>
EOF</userinput></screen>

    <screen revision="systemd"><userinput>cat &gt; /boot/grub/grub.cfg &lt;&lt; "EOF"
<literal># Begin /boot/grub/grub.cfg
set default=0
set timeout=5

insmod ext2
set root=(hd0,2)

menuentry "GNU/Linux, Linux &linux-version;-lfs-&versiond;" {
        linux   /boot/vmlinuz-&linux-version;-lfs-&versiond; root=/dev/sda2 ro
}</literal>
EOF</userinput></screen>


    <note><para>从 <application>GRUB</application>的视角来看，
			内核文件的位置相对于它使用的分区。如果您使用了单独的 /boot
			分区，需要从上面的 <emphasis>linux</emphasis> 行删除 /boot，
			然后修改 <emphasis>set root</emphasis> 行，
			指向 /boot 分区。</para></note>

    <!--para>GRUB is an extremely powerful program and it provides a tremendous
    number of options for booting from a wide variety of devices, operating
    systems, and partition types.  There are also many options for customization
    such as graphical splash screens, playing sounds, mouse input, etc.  The
    details of these options are beyond the scope of this introduction.</para-->
    <para>GRUB 是一个很强大的程序，它提供了非常多的选项，
		可以支持多种设备、操作系统和分区类型，还有很多用于定制启动屏幕、
		声音、鼠标输入等的选项。这些选项的细节超过了本书的范围，
		不予讨论。</para>

    <!--caution><para>There is a command, <application>grub-mkconfig</application>, that
    can write a configuration file automatically.  It uses a set of scripts in
    /etc/grub.d/ and will destroy any customizations that you make.  These scripts
    are designed primarily for non-source distributions and are not recommended for
    LFS.  If you install a commercial Linux distribution, there is a good chance
    that this program will be run.  Be sure to back up your grub.cfg file.</para></caution-->
	<caution><para>有一个命令 <application>grub-mkconfig</application>
			被用于自动创建配置文件。它使用 /etc/grub.d
			中的脚本创建新配置文件，这会覆盖您手动编写的 grub.cfg。
			这些脚本主要是为非源代码发行版设计的，
			在 LFS 中不推荐使用。但是，如果您安装了商业发行版，
			它很可能在发行版中被运行，记得备份 grub.cfg 以防它被覆盖。
	</para></caution>

   </sect2>

</sect1>
