<?xml version="1.0" encoding="ISO-8859-1"?>
<!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>Using GRUB to Set Up the Boot Process</title>

  <sect2>
    <title>Introduction</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>


    <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>

<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>
    </note>

  </sect2>

  <sect2>
    <title>GRUB Naming Conventions</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>

  </sect2>

  <sect2>
    <title>Setting Up the Configuration</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>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>Using the current lfs partition will also work, but configuration
    for multiple systems is more difficult.</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>Install the GRUB files into <filename
    class="directory">/boot/grub</filename> and set up the boot track:</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>
    </warning>

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

    <note>
      <para>If the system has been booted using UEFI,
      <command>grub-install</command> will try to install files for the
      <emphasis>x86_64-efi</emphasis> target, but those files
      have not been installed in chapter 6. If this is the case, add
      <option>--target i386-pc</option> to the command above.</para>
    </note>

<!-- 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>Creating the GRUB Configuration File</title>

    <para>Generate <filename>/boot/grub/grub.cfg</filename>:</para>

    <screen arch="default" 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 arch="default" 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>

    <screen arch="ml_32,ml_x32,ml_all" 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;-multilib" {
        linux   /boot/vmlinuz-&linux-version;-lfs-&version;-multilib root=/dev/sda2 ro
}</literal>
EOF</userinput></screen>

    <screen arch="ml_32,ml_x32,ml_all" 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;-multilib" {
        linux   /boot/vmlinuz-&linux-version;-lfs-&versiond;-multilib root=/dev/sda2 ro
}</literal>
EOF</userinput></screen>


    <note><para>From <application>GRUB</application>'s perspective, the
    kernel files are relative to the partition used.  If you
    used a separate /boot partition, remove /boot from the above
    <emphasis>linux</emphasis> line.  You will also need to change the
    <emphasis>set root</emphasis> line to point to the boot partition.
    </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>

    <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>

   </sect2>

</sect1>
