This is motivated primarily by the desire to help new Linux users get started. The basics are applicable to virtually any computer system. The discussion assumes the “PC” architecture originally developed by IBM using Intel and compatible processors.
The Basics:
Virtually all modern computers operate by fetching an instruction from memory, performing an operation, and returning data to memory. When the machine is first started, there must be a way to get the initial instructions into memory. This process has come to be known as “bootstrap loading” or simply “booting”.
The most basic steps in booting are:
After initial power-on tests, the CPU is sent to a location specified by the BIOS (Basic Input/Output System). While this is most often the first hard drive, it can also be the floppy disk or a CD/DVD. Devices are searched for boot code in the order specified in the BIOS setup menu. If a hard disk is specified, the system looks in the “Master Boot Record” or MBR, defined as the first 512 bytes on the drive (also known as the first sector).
At the location specified above, there must be code which tells the system what to do next. This is sometimes called the “stage I” boot code. Typically, additional stages of boot code are loaded based on the instructions in stage I. Eventually, the instructions lead to installation of the operating system “kernel”--the most basic core code which is required for all other functions.
Once the kernel is in control, it typically runs a routine which performs the remaining startup and configuration tasks. This stage can be customized using scripts and configuration files.
There are many variants of this process. For example, the initial boot code can be configured to do nothing more than point to more boot code on another drive or partition. While the path followed can become quite convoluted, the rules at each step are very specific.
More on the MBR and Boot Sectors:
As stated, the MBR is defined as the first 512 bytes on the drive. This is further broken down as follows:
| Boot code: | 446 bytes |
| Partition table: | 64 bytes |
| “Signature”: | 2 bytes |
The partition table has four entries which are pointers to the actual disk partitions—or to additional partition tables. Go here for more details on how partitions work.
A partition can also have a “boot sector” with boot code. This could be similar (or identical) to what is in the MBR. Control can be passed—eg—from the MBR code to the boot sector code. As you might have guessed, the boot sector is the first 512 bytes of a partition. (For the purists, note that the MBR (first sector on the drive) is not part of any partition. In fact, the typical drive setup has 63 sectors at the beginning which are not part of any partition.)
Configuring the Boot Process:
Early in the process, the boot code will look for a configuration file to get further instructions on what to do next. Often the location of the first configuration file is “hard-coded” into the Stage I loader. This means that this early code must be setup specifically for the system configuration.
The configuration files also specify if and how a “RAM-Disk” will be used in the startup process, and finally, will include information to be passed to the kernel when it is started. In a computer with multiple operating systems, the configuration file(s) will also include entries for the various systems, and a mechanism for choosing which is to be booted.
Linux and GRUB:
What follows is specific to Linux systems and to the “GRUB” bootloader—the most common method of booting a Linux system. Unlike operating systems such as MS Windows, Linux is designed to make it easy to have multiple operating systems installed—and to choose which one is to be used at startup. This is the commonly-heard “dual-boot”, or “multi-boot” setup. This flexibility carries the burden of having to deal with more configuration than is required with Windows.
A Typical Default Installation:The typical Linux installer will automatically install and configure the bootloader in a variety of common setups. One typical scenario involves Windows on the first partition, and Linux on the second. In Linux-speak, these are hda1 and hda2 (IDE drives) or sda1 and sda2 (SATA or SCSI drives). In GRUB-speak (just to be different), they are hd0,0 and hd0,1 (GRUB counts from zero).
In this example, GRUB Stage I would be in the MBR, and the following stages and the configuration file would be in the /boot directory of the Linux installation. (Here this is assumed to be on partition hda2). The code that tells GRUB where to find /boot is built into the Stage I code in the MBR.
Here is a simplified version of what the GRUB configuration file (typically menu.lst or grub.conf)will look like:
title Linux
root (hd0,1)
kernel /boot/vmlinuz root=/dev/hda2
initrd /boot/initrd.img
title Windows
root (hd0,0)
makeactive
chainloader +1
The first entry specifies that Linux will be found on the second partition of the first drive, where the vmlinuz (kernel) and initrd files will be found (/boot, and where the kernel should mount the filesystem once it takes control.
The second entry points to the Windows installation using the chainloading option in GRUB. Recall that boot code in the MBR can point to added boot code in the boot-sector of a partition. In this example, the boot process is directed to “NTLDR” (The Windows loader) which is in the boot sector of drive 1, partition 1. From this point, NTLDR takes control in accord with its configuration file, boot.ini.
Alternatives:There are a minimum of three alternative setups:
Leave the Windows code in the MBR and install a GRUB stage I file in the Windows partition, making an entry in boot.ini to point to it.
Put GRUB in the MBR AND in the Linux boot sector, and use the chainloading mechanism to get to Linux.
Put GRUB on a floppy disk instead of the MBR.
Multi-Boot:Using the basic rules described above, many operating systems can be installed. All that is required is the appropriate entry in the GRUB configuration file.
Installing GRUB Manually:For one reason or another, it is often necessary to install GRUB manually. Because the GRUB stage I code must be adjusted to match the specific configuration, a specific command sequence is required. Following is a common method (recommended as most reliable in the GRUB manual):
Commands issued from any Linux (eg the primary installation, or a Live CD version)
Drive and partition information assumed to be as described earlier
grub opens
the GRUB shell
root (hd0,1) tells
GRUB that the /boot directory is on drive 1, partition 2
setup (hd0) puts
GRUB stage I on the MBR of drive 1
Quirks and Gotchas:As with any complex system, there are some nuances to deal with.
First, the numbering of drives and partitions must be as they are seen by GRUB, based on information passed by the BIOS at startup. With a mixture of IDE/PATA and SATA or SCSI drives, this can be confusing. There is a device.map file to deal with this—consult the GRUB manual for more information.
Windows always assumes that it is on the first partition. If you want it elsewhere, then you can trick it using GRUB's map command.
Where Windows depends on the “active” (aka “boot”) flag to be set in the partition table, Linux (GRUB) does not care about this. It is best to just leave the Windows partition marked active. This introduces another multi-boot method which I have never used. Simply edit the partition table to change the active flags and re-boot. (Actually, it might not be quite that simple!)
Summary and References:Booting Linux with GRUB can be very simple if the simple rules are followed. Most installations will be correctly configured automatically. To create custom configurations or to trouble-shoot non-standard setups, Two references are recommended:
This Thread on JustLinux.com (and various other threads by the same author)