52midnight.com/linux

52midnight.com/sea
Installing GRUB
The Linux bootloader

   

LINUX ONLINE

HELP

Providing online help is where Linux shines. It's free, quick, easy, highly expert, and far friendlier than what you're used to from other OS's.

VENDORS

Several stores in Australia sell Linux Live CDs and install disk sets. Click here for a short list.

DISTROS

Linux is available as a large number of distributions or distros. Those below are recommended, but a full list is at  DistroWatch.com

KNOPPIX

The original and still one of the best Live Distros that runs directly from CD without being installed. Best choice for Beginners, and as a rescue disk.

UBUNTU

Enormously popular, simple Gnome desktop, very helpful forums, a good 'plain vanilla' choice for new Users.

PCLinuxOS

Very easy to install, powerful KDE desktop, friendly forums, more 'ice-cream sundae' than 'plain vanilla'.

VECTOR LINUX

"Keep it simple, keep it small" is the idea behind a fast system that still has everything needed.

PUPPY

Small, fast, many features, ideal for older machines. Can be run from a USB stick

52MIDNIGHT

Projects
Archives
Electronics
Computers
Linux

Click here for the Grub manual in a new browser window
 
Note: you will need root permissions to use Grub.

Background

GRUB (GRand Unified Bootloader) is currently the most popular bootloader on Linux platforms. The first was lilo (LInux LOader) and is still in use by those who prefer it. Work began on Grub in the late 1990s and it was released as a Gnu package in 1999. A much more complex version named Grub 2 was begun in 2002, but it far exceeds the requirements of most systems. The original, now named Grub Legacy, is still in widespread use, the last version being released in 2005 and still available. This document addresses Grub Legacy and ignores Grub 2.

The boot process

In order to use Grub effectively it is necessary to understand the basics of the boot process. When a computer is switched on or rebooted, the CPU starts executing a small section of code contained in a chip on the motherboard – the BIOS ROM. This code first displays a low-resolution splash screen and then executes a Power On Self Test or POST, which checks that essential hardware is present and functional. Some BIOSs display the results of the POST as they occur; others leave the splash screen intact.

When the POST completes successfully, the BIOS code examines the storage devices connected to the system. The sequence in which they are examined is set by the User using the BOOT Sequence menu of the BIOS Setup Utility. Typically this will be the CDROM drives, any USB sticks that are plugged in, then the system's Hard Disk Drives (HDDs), although other devices, including the network, can also be checked. The first device that contains a bootable Operating System (OS) is selected as the Boot Device, and control is passed to it.

The first sector of all storage devices is called the Boot Sector, whether or not the device is bootable. If the device is bootable, the last two bytes of the Boot Sector have the hex values 55 AA, in which case the Boot Sector contains a valid Boot Record. The Boot Record has two main parts: a short section of executable code, and optionally a Partition Table. Small, simple storage devices such as floppy disks seldom have partitions, and thus have no Partition Table. Many larger devices have only one partition, but contain a Partition Table nonetheless.

The first sector of each partition is also called a Boot Sector; thus we have the Master Boot Sector at the start of the device proper, and Partition Boot Sectors at the start of each partition (also called Volume Boot Sectors). All or none of these may contain valid Boot Records. If the device is selected as the system's Boot Device, the short section of code in the Master Boot Record must determine which of the partitions are bootable, and which is to be used as the Boot Partition. This is the job of the Boot Loader.

Grub offers two ways of selecting the partition to be booted:

  1. By using its own commandline. The User is presented with a prompt and enters the commands required to boot the system.
  2. By using a preconfigured menu located in the /boot/grub/ directory of one of the partitions.

The second is by far the most common, and requires that the User designate one of the partitions as the Grub Master Partition. This is a purely arbitrary choice, and is usually made automatically by whichever partition was last used to install an OS. If only a single OS on the device is in use, the selection and setup will be completely transparent to the User.

Users who wish to configure a multiboot installation allowing choice of two or more OS's must either trust to installation scripts to do this (seldom satisfactory) or configure the boot loader themselves. This document is addressed to the latter.

Installing Grub

It is important to distinguish between installing Grub, and using Grub to set up a device and/or partition. In a multiboot installation it is only necessary for Grub to be installed on one OS, that in the partition chosen as the Grub Master Partition. However, most Linux distros install Grub as a matter of course, usually as one of the final phases of the installation process. A few (ill-mannered) distros simply set up the new installation as the Grub Master, sometimes without even adding multiboot options. Most distros examine the other devices and partitions for bootable OS's, looking especially for Windows, and create a boot menu for the User to choose the desired OS at boot time. The User may then be offered a choice of where to write the Boot Sector code:

  • In the Master Boot Record. This will only be satisfactory if a boot menu has been created that meets the User's requirements.
  • In the Partition Boot Record. This is a better choice, but requires the User to transfer the boot instructions from the newly installed boot menu to that in the Grub Master Partition.

The rest of this document explains how to perform the second step.

Requirements for configuring Grub

In order to configure Grub by editing its menu, the User must be able to perform the following tasks. All can be done from within Midnight Commander except for mounting and unmounting:

  1. Know his position in the Directory Tree.
  2. Change to any desired directory in the Directory Tree.
  3. Print the contents of the current directory.
  4. Copy, move or delete files.
  5. Create or delete directories.
  6. Mount devices or partitions.
  7. Unmount devices or partitions.
  8. Use a text editor to edit plain ASCII text files.

If Midnight Commander is not available, the following commands can be invoked. All have man pages which should be consulted for more information:

CommandMeaningFunction
pwdPresent Working DirectoryPrints the full path of the directory from which the command is invoked. For example, if the User is logged in as guest and is in a desktop directory named News, invoking the command will print /home/guest/Desktop/News.
Note that the mnemonic PWD is used hereinafter to denote the User's Present Working Directory, i.e. his current position in the Directory Tree.
cdChange DirectoryChanges the PWD to the User's Home Directory.
cd DIRECTORYChanges the PWD to DIRECTORY.
cd ..Changes the PWD to the Parent Directory; the one "directly above it" in the Directory Tree.
cd /Changes the PWD to the root of the Directory Tree.
lsListPrint the contents of the PWD.
ls -lPrint the contents of the PWD in long format.
ls -1Print the contents of the PWD in a single column.
ls -aPrint all contents of the PWD including hidden files.
cp SOURCE DESTCopyCopy file SOURCE to file DEST.
cp SOURCE DIRECTORYCopy file SOURCE to directory DIRECTORY. SOURCE can be one or more files.
cp DIRECTORY DESTCopy DIRECTORY to DEST.
mv SOURCE DESTMove or RenameMove or rename file SOURCE to file DEST.
mv SOURCE DIRECTORYMove or rename file SOURCE to directory DIRECTORY. One or more SOURCE files can be moved.
mv DIRECTORY DESTMove or rename DIRECTORY to DEST.
rm FILENAMERemoveDelete file FILENAME which can be one or more files.
mkdir DIRECTORYMake DirectoryCreate a directory named DIRECTORY.
rmdir DIRECTORYRemove DirectoryDelete a directory named DIRECTORY. Only works if DIRECTORY is empty.
mountMountPrints the mount list – the list of devices (real and virtual) mounted on the system.
mount | grep ^/devPrints the list of real devices mounted on the system.
mount DEVICE DIRECTORYMount DEVICE on DIRECTORY.
mount OPTIONS DEVICE DIRECTORYMount DEVICE on DIRECTORY with OPTIONS.
umount DEVICEUnmountUnmount DEVICE.
umount DIRECTORYUnmount DIRECTORY.

Text editors

If a desktop or other GUI is installed and working, GUI text editors such as Leafpad, Mousepad or Nedit can be used. If not, a console text editor must be used. The traditional Unix text editor is vi (pronounced "vee-eye") but it is a modal editor with an unusual interface. Emacs is also traditional, but is more of a religion than an editor. The best console text editor by far is nano and is often available; joe is similar but less common. Midnight Commander uses mcedit as its internal editor, but it can also be invoked from the commandline. All use keybindings somewhat different from GUI editors since they were created back when keyboards had no function keys; but all have man pages, and nano has inbuilt help at the bottom of the screen.

The Grub menu

The menu displayed by Grub at boot time is named either grub.conf or menu.lst; one is often a softlink to the other. It has two or more sections separated by blank lines: the first contains some basic settings, the rest are boot entries. Each boot entry has two or more lines: the first line begins with the keyword title and contains whatever human-readable text the User desires; it is ignored by Grub. Grub uses the rest to boot the selected partition. Consider a simple menu file with a single boot entry:

timeout 10
default 0

title sda1  20140516 Gentoo #1 Grub Master
root (hd0,0)
kernel /boot/kernel-genkernel-x86-3.7.9-gentoo root=/dev/sda1 consoleblank=0
initrd /boot/initramfs-genkernel-x86-3.7.9-gentoo

The first line specifies the timeout delay in seconds for which the menu will be displayed. If the User does not make a selection within this period, the second line displays the number of the default entry which will be booted.

The title line of the boot entry contains the text visible in the menu. In this case it shows the name of the partition (sda1), the date(20140516), the OS (Gentoo #1) and identifies the partition as the Grub Master. The second root line identifies the Boot Partition using Grub's own device/partition terminology (refer to the manual), the third kernel line contains the kernel invocation line including the parameters to be passed to the kernel, and the fourth initrd line specifies the location of the Init RAMdisk that is loaded and executed to perform hardware detection prior to loading the kernel proper.

These lines will typically be created by the installation script and written to the /boot/grub/grub.conf file in the installation partition. It is only necessary for the User to copy them into the grub.conf file of the Grub Master Partition as a new boot entry.

Editing the Grub menu with a GUI editor

The advantage of GUI editors is that text can be copied and pasted between two windows, one containing the new grub.conf file, the other the Grub Master partition grub.conf file. Here are two example files, followed by the edited Master file containing the new entry. Assume that the system is running the Grub Master OS in sda3. Note the first entry for chainloading and booting Windows XP, and that sda6 was empty until the new installation was performed:

timeout 3
splashimage=(hd0,3)/boot/grub/splash.xpm.gz
default 1

title sda1  20121027 Windows XP
root (hd0,0)
makeactive
chainloader +1
boot

title sda2  20140724 Gentoo #4 Working
root (hd0,1)
kernel /boot/kernel-genkernel-x86-3.7.9-gentoo root=/dev/sda2 consoleblank=0 net.ifnames=0
initrd /boot/initramfs-genkernel-x86-3.7.9-gentoo

title sda3  20130404 Gentoo #1  Grub master
root (hd0,2)
kernel /boot/kernel-genkernel-x86-3.7.9-gentoo root=/dev/sda3 consoleblank=0 net.ifnames=0
initrd /boot/initramfs-genkernel-x86-3.7.9-gentoo

title sda6  Empty partition

title sda7  130520 Gentoo #2 Nvidia setup
root (hd0,6)
kernel /boot/kernel-genkernel-x86-3.7.10-gentoo root=/dev/sda7 consoleblank=0 net.ifnames=0 rootfstype=ext3
initrd /boot/initramfs-genkernel-x86-3.7.10-gentoo

title sda8  130426 Gentoo #3 X + Nvidia
root (hd0,7)
kernel /boot/kernel-genkernel-x86-3.7.10-gentoo root=/dev/sda8 consoleblank=0 net.ifnames=0 rootfstype=ext3
initrd /boot/initramfs-genkernel-x86-3.7.10-gentoo

Assume further that PCLinuxOS has been newly installed on sda6, and that sda6 has been mounted on /mnt/sda6/:

># mount /dev/sda6 /mnt/sda6

Here is the file /mnt/sda6/boot/grub/grub.conf:

timeout 10
splashimage=(hd0,5)/boot/grub/splash.xpm.gz
default 0

title PCLinuxOS 2014 KDE
root (hd0,5)
kernel /boot/linuz-3.7.9-pclos root=/dev/sda6
initrd /boot/initrd-pclos-3.7.9

Open both of these files in the GUI editor, then copy the boot entry form the second and paste it into the first in place of the existing entry for sda6:

timeout 3
splashimage=(hd0,3)/boot/grub/splash.xpm.gz
default 1

title sda1  20121027 Windows XP
root (hd0,0)
makeactive
chainloader +1
boot

title sda2  20140724 Gentoo #4 Working
root (hd0,1)
kernel /boot/kernel-genkernel-x86-3.7.9-gentoo root=/dev/sda2 consoleblank=0 net.ifnames=0
initrd /boot/initramfs-genkernel-x86-3.7.9-gentoo

title sda3  20130404 Gentoo #1  Grub master
root (hd0,2)
kernel /boot/kernel-genkernel-x86-3.7.9-gentoo root=/dev/sda3 consoleblank=0 net.ifnames=0
initrd /boot/initramfs-genkernel-x86-3.7.9-gentoo

title PCLinuxOS 2014 KDE
root (hd0,0)
kernel /boot/linuz-3.7.9-pclos root=/dev/sda6
initrd /boot/initrd-pclos-3.7.9

title sda7  130520 Gentoo #2 Nvidia setup
root (hd0,6)
kernel /boot/kernel-genkernel-x86-3.7.10-gentoo root=/dev/sda7 consoleblank=0 net.ifnames=0 rootfstype=ext3
initrd /boot/initramfs-genkernel-x86-3.7.10-gentoo

title sda8  130426 Gentoo #3 X + Nvidia
root (hd0,7)
kernel /boot/kernel-genkernel-x86-3.7.10-gentoo root=/dev/sda8 consoleblank=0 net.ifnames=0 rootfstype=ext3
initrd /boot/initramfs-genkernel-x86-3.7.10-gentoo

Now edit the title line to suit, and the default line to make it the default boot OS:

timeout 3
splashimage=(hd0,3)/boot/grub/splash.xpm.gz
default 3

title sda1  20121027 Windows XP
root (hd0,0)
makeactive
chainloader +1
boot

title sda2  20140724 Gentoo #4 Working
root (hd0,1)
kernel /boot/kernel-genkernel-x86-3.7.9-gentoo root=/dev/sda2 consoleblank=0 net.ifnames=0
initrd /boot/initramfs-genkernel-x86-3.7.9-gentoo

title sda3  20130404 Gentoo #1  Grub master
root (hd0,2)
kernel /boot/kernel-genkernel-x86-3.7.9-gentoo root=/dev/sda3 consoleblank=0 net.ifnames=0
initrd /boot/initramfs-genkernel-x86-3.7.9-gentoo

title sda6  20140518 PCLinuxOS 2014 KDE
root (hd0,0)
kernel /boot/linuz-3.7.9-pclos root=/dev/sda6
initrd /boot/initrd-pclos-3.7.9

title sda7  130520 Gentoo #2 Nvidia setup
root (hd0,6)
kernel /boot/kernel-genkernel-x86-3.7.10-gentoo root=/dev/sda7 consoleblank=0 net.ifnames=0 rootfstype=ext3
initrd /boot/initramfs-genkernel-x86-3.7.10-gentoo

title sda8  130426 Gentoo #3 X + Nvidia
root (hd0,7)
kernel /boot/kernel-genkernel-x86-3.7.10-gentoo root=/dev/sda8 consoleblank=0 net.ifnames=0 rootfstype=ext3
initrd /boot/initramfs-genkernel-x86-3.7.10-gentoo

Reboot the system and it should end up in the new PCLOS installation.

Editing the Grub menu with a console editor

The same procedure as above should be followed. If the software allows text to be cut and pasted between two instances of the editor running in separate console windows, the sequence is identical. If not, the new grub.conf file can be copied to the Grub Master /boot/grub/ directory using a different name (e.g. grub.sda6) and then read into the Master file using the appropriate text editor command.

A simpler method – chainloading

The following is based on my own understanding of Grub and a brief experiment. I see no reason why it should not work, but Readers of this document should treat it as a suggestion until they have either verified or disproved it.

In the example above, Windows was booted using a chainloader which, as the name suggests, simply passes control to the Partition Boot Sector on the assumption that it has been correctly set up with boot code. If this is done for each partition, either at the end of installation by the installation script, or by using Grub as explained in the manual, then each partition can be booted by chainloading. The only caveat, based on my own experience, is that the makeactive command which sets the partition boot flag should only be issued for Primary Partitions. Logical partitions do not have boot flags, and issuing the command on them hangs the boot process. Given these considerations, the above example becomes:

timeout 3
splashimage=(hd0,3)/boot/grub/splash.xpm.gz
default 3

title sda1  20121027 Windows XP
root (hd0,0)
makeactive
chainloader +1
boot

title sda2  20140724 Gentoo #4 Working
root (hd0,1)
makeactive
chainloader +1
boot

title sda3  20130404 Gentoo #1  Grub master
root (hd0,2)
makeactive
chainloader +1
boot

title sda6  20140518 PCLinuxOS 2014 KDE
root (hd0,5)
chainloader +1
boot

title sda7  130520 Gentoo #2 Nvidia setup
root (hd0,6)
chainloader +1
boot

title sda8  130426 Gentoo #3 X + Nvidia
root (hd0,7)
chainloader +1
boot
 
 

LINUX ONLINE

HELP

Providing online help is where Linux shines. It's free, quick, easy, highly expert, and far friendlier than what you're used to from other OS's.

VENDORS

Several stores in Australia sell Linux Live CDs and install disk sets. Click here for a short list.

DISTROS

Linux is available as a large number of distributions or distros. Those below are recommended, but a full list is at  DistroWatch.com

KNOPPIX

The original and still one of the best Live Distros that runs directly from CD without being installed. Best choice for Beginners, and as a rescue disk.

UBUNTU

Enormously popular, simple Gnome desktop, very helpful forums, a good 'plain vanilla' choice for new Users.

PCLinuxOS

Very easy to install, powerful KDE desktop, friendly forums, more 'ice-cream sundae' than 'plain vanilla'.

VECTOR LINUX

"Keep it simple, keep it small" is the idea behind a fast system that still has everything needed.

PUPPY

Small, fast, many features, ideal for older machines. Can be run from a USB stick

52MIDNIGHT

Projects
Archives
Electronics
Computers
Linux