SDB:The Boot Manager Grub
Version: 8.1 -
İçindekiler
- 1 Situation
- 2 Background and Structure
- 2.1 The Boot Process and the Involved Grub Components
- 2.2 Possibilities of the Grub Boot Menu
- 2.3 Configuring Grub with YaST2
- 2.4 The Menu File
- 2.5 Hard Disk and Partition Designations
- 2.6 Menu File Example
- 2.7 The Map File
- 2.8 The File /etc/grub.conf
- 2.9 Manual installation of GRUB
- 2.10 Documentation
Situation
You want to learn about the utilization possibilities and the structure of the boot manager Grub, the default boot manager from SuSE Linux 8.1.
Background and Structure
The name Grub is an acronym for Grand Unified Bootloader. Similar to the boot manager LILO, Grub mainly consists of two stages: the first stage (size: 512 Byte) that is written to the MBR or the boot sector of a partition and the larger second stage containing the actual program code that is loaded afterwards.
The main difference from LILO is that Grub directly supports several file systems (including all file systems that can be used in SuSE Linux as well as vfat). Therefore it is not necessary to reinstall Grub after the Grub configuration file is modified. When the system is booted, Grub reads the contents of the menu file.
Even if a new kernel (/boot/vmlinuz) is installed or the initial ramdisk (/boot/initrd) is modified, Grub will be able to find them by itself. Grub does not care about the physical position of the kernel, as long as the partition and the path of the kernel to be booted is specified correctly in the menu file. Therefore, the boot loader does not need to be reinstalled after procedures such as the compilation of the kernel.
Another major advantage of Grub is the possibility to edit all boot parameters prior to booting. This is very useful in case errors crept into the menu file.
Furthermore, it is even possible to enter the boot commands in a sort of prompt. Thus, even operating systems for which no entry was made in the boot menu can be booted. Grub enables the specification of the position and the name of the kernel and the initrd prior to booting.
The following section provides a short summary of the Grub boot process and the configuration with YaST2. Additionally, the structure of the actual configuration file (the file /boot/grub/menu.lst) is explained. Furthermore, attention is drawn to the file /boot/grub/device.map, which contains the mapping of the mass storage devices to the BIOS device names, and the file /etc/grub.conf, which is mainly used by YaST2. The manual installation of Grub is also touched on briefly. The last part describes the possibilities of the Grub boot menu.
The Boot Process and the Involved Grub Components
When the computer is booted, two components of Grub are involved:
- Loading of stage 1 of Grub
Following the installation of SuSE Linux, the first stage of Grub is saved in the MBR, in the boot sector of a hard disk partition, or in the boot sector of a floppy disk. The first stage must be very small, since the MBR or the boot sectors only have limited space. Therefore, the only task of this component is to load the second stage of Grub.
- Loading of stage 2 of Grub
The second stage of Grub provides the actual boot loader functions, including the selection menu (as well as the hidden Grub shell; see below) and the program code that transfers the system control to the operating system. In SuSE Linux, the stage 2 file is located under /boot/grub/stage2. When the boot loader is installed, the physical location of stage 2 on the hard disk is entered in the stage 1 file.
Possibilities of the Grub Boot Menu
When Grub appears on the screen after booting, you can simply select a menu entry to start the respective operating system (just as in the boot manager LILO). Boot parameters can be added for Linux systems. However, Grub offers even more possibilities. You can exit the splash screen with ESC and edit individual menu entries with "e" (edit).
Usually, the parameters only need to be modified in the event of an emergency, for instance if an operating system can not be booted. The modifications only apply this time and will not be adopted permanently.
In order to edit a menu entry, first press ESC. Then select the entry you want to modify with the cursor keys and press "e". If, for example, you selected the "Linux" entry, the following lines will be displayed:
kernel (hd0,4)/vmlinuz root=/dev/hda7 vga=791 initrd (hd0,4)/initrd
Select a line with the cursor keys. Press "e" once more to edit the line. In this way you can correct things such as the partition and path specifications. Press Enter to return to the menu, from where you can boot the entry with "b". Additional possibilities are displayed in a help text at the bottom of the screen.
Another possibility for experts, which however is only needed once in a while: by pressing "c" (for "command line"), you can enter the Grub shell, which is a kind of input prompt. Enter "help" to view a list of available commands. Press "ESC" to return to the boot menu.
Using the Grub shell, you can boot operating systems that are not included in the Grub menu. This option is very useful if you want to test a self-compiled kernel for which no entry was made in the Grub menu.
In order to boot a system with the Grub Shell, commands such as the following can be entered manually (in this example for booting a Linux system with the boot partition /dev/hda5 and the root partition /dev/hda7):
grub> kernel (hd0,4)/vmlinuz root=/dev/hda7 vga=791
[Linux-bzImage, setup=0x1400, size=0xe9a45]
grub> initrd (hd0,4)/initrd [Linux-initrd @ 0x369000, 0x863c9 bytes]
grub> boot
The syntax can be seen in the file /boot/grub/menu.lst on your system. More information is provided in the section "The Menu File" further below. Important: When you are finished, do not forget to enter "boot" to start the system.
A practical hint: Since Grub can directly access the Linux file system when booting the system, the TAB key can be used to autocomplete file names (as in the bash). This is quite useful e.g. if you do not know the exact file name. Another useful feature: If you do not know in which partition the Linux kernel is located, you can search for the kernel with the following commands:
find /boot/vmlinuz
or
find /vmlinuz
In our example, these commands would produce the following output:
(hd0,4)
Of course you can also use the command find to search for any other file (such as the initrd).
Furthermore, you can even view the contents of individual files with the command cat. For example, you can view the contents of a Grub configuration file or any other file. Even files that cannot be accessed in the running system by Linux users without root permissions can be viewed!
Since this is not always desirable from the security standpoint, a password can be set in the file /boot/grub/menu.lst. In this way, operating systems can still be booted from the menu, but the interactive options ("e" and "c") are protected against unauthorized use. These will only be released after pressing "p" and entering the password. In order to set a password, please refer to the section Configuring Grub with YaST2.
Configuring Grub with YaST2
The easiest way to configure Grub is to use YaST2. Start the YaST2 Control Center and go to
- System -->
- Bootloader configuration
Under the menu item Modify current configuration, you can modify global settings as well as the options for booting the individual operating systems. Following the selection of this menu, you can first determine where to install Grub: in the MBR of the first hard disk, on a floppy disk, in the boot sector or root partition, or in a different partition.
Press Next to proceed to a dialog that enables you to configure the timeout for the boot prompt and set a password for Grub. Please note that this password protects the execution of special Grub commands at the boot prompt. It does not affect the selection of operating systems (more information on special Grub commands at the boot prompt is presented in the section "Possibilities of the Grub Boot Menu" further below in this article).
Additional options are available under Expert settings.
Press Next to proceed to the configuration of the sections table. Each section represents an entry in the boot menu. You can add or delete sections and determine a default section. Press Edit to rename a section or modify kernel parameters (under Expert settings).
Press Next to return to the configuration of the sections.
Select Save current configuration and press Finish in order to save the modifications to the hard disk.
Under Expert manual configuration, you can manually edit the Grub configuration files /boot/grub/menu.lst and /etc/grub.conf. More information on this issue is presented in the following sections.
Select Restore original configuration in order to restore the settings proposed during the installation.
The Menu File
After the installation, the menu file is the most important configuration file. In SuSE Linux, it is located under /boot/grub/menu.lst. This file contains information on all partitions and operating systems that can be booted with the menu.
Hard Disk and Partition Designations
Please note that Grub uses its own designations for hard disks and partitions instead of the common Linux device designations (such as /dev/hda1). The floppy disk drive is designated as fd0, all hard disks are designated as hd0, hd1, and so on. The partition designation is appended, separated by a comma: hd0,0 is the first partition on the first hard disk. Important: the device and partition numbering starts with zero.
Examples:
(fd0) Floppy disk drive (hd0) First hard disk in the system (the boot hard disk) (hd1) Second hard disk (hd0,0) First partition on the first hard disk
The partition numbers 0 to 3 represent the four primary partitions. Logical partitions are numbered from 4:
(hd0,0) First primary partition on the first hard disk (hd0,1) Second primary partition (hd0,2) Third primary partition (hd0,3) Extended partition (hd0,4) First logical partition (hd0,5) Second logical partition (hd0,6) Third logical partition etc.
Another important point: When assigning hard disk labels, Grub does not distinguish between IDE, SCSI, or other devices. All hard disks detected by the BIOS or other controllers (SCSI, RAID, etc.) are numbered according to the boot sequence. (hd0,0) is always the first hard disk in the boot sequence set in the BIOS, regardless of the type (IDE, SCSI, or RAID).
Example: In a system with two IDE hard disks and two SCSI hard disks, the boot sequence in the BIOS is set as "IDE before SCSI". Thus, Grub will use the following labels:
(hd0) First IDE hard disk (hd1) Second IDE hard disk (hd2) First SCSI hard disk (hd3) Second SCSI hard disk
For more details please refer to the section "The Map File".
Menu File Example
The following example shows the structure of the menu file /boot/grub/menu.lst. In this example, /dev/hda5 is the Linux boot partition, /dev/hda7 is the root partition, and /dev/hda1 contains a Windows operating system.
gfxmenu (hd0,4)/message color white/green black/light-gray default 0 timeout 8
title linux kernel (hd0,4)/vmlinuz root=/dev/hda7 vga=791 initrd (hd0,4)/initrd title windows root (hd0,0) makeactive chainloader +1 title floppy root (fd0) chainloader +1 title failsafe kernel (hd0,4)/vmlinuz.shipped root=/dev/hda7 ide=nodma apm=off acpi=off vga=normal nosmp maxcpus=0 3 initrd (hd0,4)/initrd.shipped
The entries have the following meaning:
- As you can easily guess, the first two lines cover the configuration of the splash menu: the background image is located in /dev/hda5 and has the name "message". Foreground: white, background: green, selection: black, background of the selection: light gray.
- The entry "default 0" in the third line indicates that the first menu entry ("title linux") is the default selection for booting.
- Line 4: The timeout is 8 seconds.
Regarding the entries of the operating systems that can be booted:
- The first entry ("title linux") boots SuSE Linux.
- The Linux kernel is located in the first logical partition of the first hard disk (hd0,4) (the boot partition in this example); the file name is vmlinuz. Kernel parameters (such as the specification of the root partition, vga, etc.) are appended directly.
Attention: The root partition must be specified as a Linux device name, since it is sent to the Linux kernel as a parameter. - Information on the position of the initrd: The initrd is also located in the first logical partition on the first hard disk.
- The next section starts Windows from the first partition of the hard disk (hd0,0). To be on the safe side, the option "makeactive" is set in the following line, as Windows can only be started from a visible partition that is set active.
The entry "chainloader +1" causes the first sector of the indicated partition to be read and executed. - The subsequent section can be used to start an operating system from a floppy disk without performing any changes in the BIOS.
- The final section starts Linux in the failsafe mode.
As stated above, changes to this menu will be adopted automatically the next time the system is booted.
For more specific information on booting DOS/Windows operating systems please refer to the articles SDB:Booting Windows from the Second HD
The Map File
The map file is the second important configuration file of Grub. However, usually this file does not need to be modified.
Let us return to the subject of how Grub numbers the hard disks in a mixed system. In a system in which hard disks are connected to different controllers (e.g. if both IDE hard disks and SCSI hard disks are used), Grub attempts to determine the boot sequence with a special procedure (as Grub is not able to peer into the BIOS). Instead, Grub must calculate whether the system is booted from the IDE hard disk or the SCSI hard disk.
Grub saves the result to a file called /boot/grub/device.map. In our example, this file has the following content (the boot sequence in the BIOS being "IDE before SCSI"):
(fd0) /dev/fd0 (hd0) /dev/hda (hd1) /dev/hdb (hd2) /dev/sda (hd3) /dev/sdb
Important: If the file content is edited manually (due to boot problems), you must subsequently execute
grub --batch --device-map=/boot/grub/device.map </etc/grub.conf
in order to reinstall Grub. The parameter --device-map specifies that Grub is to interpret the existing map file. The other commands are to be adopted from the file /etc/grub.conf, which is introduced in the following section.
The File /etc/grub.conf
The file /etc/grub.conf contains parameters for the command "grub". For example, it could have the following content:
root (hd0,4) install /grub/stage1 d (hd0) /grub/stage2 0x8000 (hd0,4)/grub/menu.lst quit
The entry root mounts the partition containing the Grub files. The command install causes Grub to be installed. In this example, the stage 1 file is installed in the MBR of the first hard disk (d (hd0)). The stage 2 file is to be loaded to the memory address 0x8000 (/grub/stage2 0x8000). The entry (hd0,4)/grub/menu.lst indicates the position of the menu file.
Manual installation of GRUB
There are two possibilities to install GRUB from the Linux command line: the "setup" command and the "install" command. Although the "setup" command is not utilized in SuSE Linux (e.g. by YaST), its use will be briefly described.
Command setup
Enter the command "grub" to display the GRUB command line prompt where further commands will be entered:
grub>
Hint: If your computer is not equipped with a floppy drive, use the option "--no-floppy" when starting GRUB to skip the search for a floppy drive.
In the following example we assume that the boot partition is located at /dev/hda5 and that GRUB must be installed on the first disk's MBR.
grub> root (hd0,4) Filesystem type is ext2fs, partition type 0x83
grub> setup (hd0) Checking if "/boot/grub/stage1" exists... yes Checking if "/boot/grub/stage2" exists... yes Checking if "/boot/grub/e2fs_stage1_5" exists... yes Running "embed /boot/grub/e2fs_stage1_5 (hd0)"... 23 sectors are embedded. succeeded Running "install /boot/grub/stage1 (hd0) (hd0)1+23 p (hd0,4)/boot/grub/stage2 /boot/grub/menu.lst"... succeeded Done.
grub> quit
The first command includes the location of the boot partition, thus indicating where the boot loader files are located. The second command installs GRUB on the MBR. For this purpose, a file called stage1_5 is used to grant GRUB access to the file system on the boot partition.
Hint: To install GRUB on a partition's boot sector (e.g. /dev/hda5) instead of on the MBR, use the command setup (hd0,4).
A further hint: Similarly to the bash shell, the GRUB shell supports the tab completion function. Thus, when you enter "root (" and press TAB, a device list (e.g. hd0) is displayed. When you enter "root (hd0," followed by TAB, a partition list is displayed.
The third command closes the GRUB shell.
Command install
The second approach to install GRUB uses the install command:
grub> root (hd0,4) Filesystem type is ext2fs, partition type 0x83
grub> install --stage2=/boot/grub/stage2 /boot/grub/stage1 d (hd0) /boot/grub/stage2 0x8000 (hd0,4)/boot/grub/menu.lst
grub> quit
The "install" command, which is also used by YaST, is included in the file /etc/grub.conf, too. The syntax of this command is explained above.
Hint: Since the install command is already included in the file /etc/grub.conf, the easiest procedure consists of starting GRUB in "batch" mode in a Linux shell and having the file /etc/grub.conf read out:
grub --batch --device-map=/boot/grub/device.map </etc/grub.conf
Documentation
Extensive documentation is available athttp://www.gnu.org/software/grub/manual/grub.html. The same information can be accessed in the installed system in a shell with info grub. An FAQ is available at http://www.gnu.org/software/grub/grub-faq.en.html. The man pages offer additional assistance: man grub und man grub-install.
SDB:The Boot Loader Concept in SuSE Linux 8.1
SDB:Setting a Password for the Boot Manager GRUB
SDB:Uninstalling the Boot Manager GRUB from the MBR
SDB:Booting Windows from the Second HD
SDB:Booting Multiple Windows Installations from One Hard Disk
<keyword>grub,boot,bootmanager,bootloader</keyword>