Linux in a Nutshell, Fourth Edition (2024)

Like LILO, the GRUBboot loader can load other operating systems in addition to Linux.GRUB was written by Erich Boleyn to boot operating systems onPC-based hardware, and is now developed and maintained by the GNUproject. GRUB was intended to boot operating systems that conform tothe Multiboot Specification, which wasdesigned to create one booting method that would work on anyconforming PC-based operating system. In addition tomultiboot-conforming systems, GRUB can boot directly to Linux,FreeBSD, OpenBSD, and NetBSD. It can also boot other operatingsystems such as Microsoft Windows indirectly, through the use of achainloader .The chainloader loads an intermediate file, and that file loads theoperating system’s boot loader.

GRUB provides a graphical menu interface. It also provides a commandinterface that is accessible both while the system is booting (thenative command environment) and from the command line once Linux isrunning.

While LILO works perfectly well, especially ifyou usually boot the default image, GRUB has some advantages. Thegraphical menu interface shows you exactly what your choices are forbooting so you don’t have to remember them. It alsolets you easily edit an entry on the fly, or drop down into thecommand interface. In addition, if you are using the menu interfaceand something goes wrong, GRUB automatically puts you into thecommand interface so you can attempt to recover and boot manually.Another advantage of GRUB is that if you install a new kernel orupdate the configuration file, that’s all you haveto do; with LILO, you also have to remember to rerun the lilo command to reinstall the boot loader. Onthe other hand, if you are used to LILO, don’t needto see the prompts often, and have a stable system, LILO is quick andconvenient.

A GRUB installation consists of at least two and sometimes threeexecutables, known asstages . Thestages are:

Stage 1

Stage 1 is the piece of GRUB that resides in the MBR or the bootsector of another partition or drive. Since the main portion of GRUBis too large to fit into the 512 bytes of a boot sector, Stage 1 isused to transfer control to the next stage, either Stage 1.5 or Stage2.

Stage 1.5

Stage 1.5 is loaded by Stage 1 only if the hardware requires it.Stage 1.5 is filesystem-specific; that is, there is a differentversion for each filesystem that GRUB can load. The name of thefilesystem is part of the filename(e2fs_stage1_5,fat_stage1_5, etc.). Stage 1.5 loads Stage 2.

Stage 2

Stage 2 runs the main body of the GRUB code. It displays the menu,lets you select the operating system to be run, and starts the systemyou’ve chosen.

If it was compiled with netboot support, GRUB can also be used toboot over a network. We don’t describe that processhere; see the file netboot/README.netboot in theGRUB source directory for detailed information.

One of the first things to understand about GRUB is that it uses its ownnaming conventions. Drives are numbered starting from 0; thus,the first hard drive is hd0, the second hard drive is hd1, the firstfloppy drive is fd0, and so on. Partitions are also numbered from 0,and the entire name is put in parentheses. So the first partition ofthe first drive, /dev/hda1, is known as (hd0,0)to GRUB. The third partition of the second drive is (hd1,2). GRUBmakes no distinction between IDE drives and SCSI drives, so the firstdrive is hd0 whether it is IDE or SCSI.

Files are specified either by the filename or byblocklist , which is used to specify files such aschainloaders that aren’t part of a filesystem. Afilename looks like a standard Unix path specification with the GRUBdevice name prepended; for example:

(hd0,0)/grub/grub.conf

If the device name is omitted, the GRUB root device is assumed. TheGRUB root device is the disk or partition where the kernel image isstored, set with the root command.See Section 4.4 for the commanddescriptions.

When you use blocklist notation, you tell GRUB which blocks on thedisk contain the file you want. Each section of a file is specifiedas the offset on the partition where the block begins plus the numberof blocks in the section. The offset starts at 0 for the first blockon the partition. The syntax for blocklist notation is:

[device][offset]+length[,offset]+length...

In this case, too, the device name is optional for a file on the rootdevice. With blocklist notation, you can also omit the offset if itis 0. A typical use of blocklist notation is when using a chainloaderto boot Windows. If GRUB is installed in the MBR, you can chainloadWindows by setting the root device to the partition that has theWindows boot loader, making it the active partition, and then usingthe chainloader command to read the Windows bootsector:

rootnoverify (hd0,0)makeactivechainloader +1

In this example, the blocklist notation (+1) does not include eitherthe device name or the offset because we set the root device to theWindows partition, and the Windows loader begins at offset 0 of thatpartition.

GRUB also includes a devicemap. The device map is an ASCII file, usually/boot/grub/device.map. Since the operatingsystem isn’t loaded yet when you use GRUB to bootLinux (or any other operating system), GRUB knows only the BIOS drivenames. The purpose of the device map is to map the BIOS drives toLinux devices. For example:

(fd0) /dev/fd0(hd0) /dev/hda

Installing GRUB

InstallingGRUB involves two stages. First, you install the GRUB files on yoursystem, either by compiling and installing the source tarball or froma package. That puts the GRUB files in the correct locations on yoursystem. The second step is to install the GRUB software as your bootmanager. This is the step we describe in this section.

If you installed GRUB as part of your Linux installation, thedistribution’s installation program took care ofboth stages of installing GRUB, and you’ll mostlikely see the GRUB menu when you boot Linux. If youdidn’t install GRUB as part of your Linuxinstallation, you have two choices. The easiest way to install GRUBis with the grub-install shellscript that comes with GRUB. If grub-install doesn’t work, orif you want to do the installation manually, you can run thegrub command and issue theinstallation commands yourself.

The following sections describe how to create a GRUB boot floppy andhow to install GRUB.

Creating a GRUB boot floppy

You can create a GRUB boot floppy for everyday use or to have for anemergency. The following instructions make a floppy that boots to theGRUB command line:

  1. From the directory where GRUB was installed (e.g.,/usr/share/grub/i386-pc), use the dd command to write the filestage1 to the floppy:

    % dd if=stage1 of=/dev/fd0 bs=512 count=1

    This command writes one block, with a blocksize of 512, from theinput file stage1 to the floppy device/dev/fd0.

  2. Now write the file stage2 to the floppy,skipping over the first block (seek=1) so you don’toverwrite stage1:

    % dd if=stage2 of=/dev/fd0 bs=512 seek=1

Put together, the process looks like this:

% dd if=stage1 of=/dev/fd0 bs=512 count=11+0 records in1+0 records out% dd if=stage2 of=/dev/fd0 bs=512 seek=1254+1 records in254+1 records out

The boot floppy is now ready to boot to the GRUB command line.

You can also make a boot floppy that boots to the GRUB menu:

  1. Create a GRUB configuration file(/boot/grub/grub.conf) if youdon’t already have one. The configuration file isdescribed later in Section 4.3.2.

  2. Create a filesystem on your floppy disk. For example:

    $ mke2fs /dev/fd0
  3. Mount the floppy drive and create the directory/boot/grub:

    % mount /mnt% mkdir /mnt/boot% mkdir /mnt/boot/grub
  4. Copy the stage1, stage2,and grub.conf GRUB images from/boot/grub on your Linux partition to/mnt/boot/grub.

  5. Run the grub command. This exampleassumes the command is in /sbin/grub, but itmight be in /usr/sbin/grub on your system:

    $ /sbin/grub --batch <<EOTroot (fd0)setup (fd0)quitEOT

You should now be able to boot to the GRUB menu from the floppy diskyou just created.

Using grub-install

GRUB comes with a shell script, grub-install, which uses the GRUB shell toautomate the installation. The command syntax is:

grub-install options install-device

where install-device is the name of the deviceon which you want to install GRUB, specified as either the GRUBdevice name (e.g., (hd0)) or the system device (e.g.,/dev/hda). For example, you might issuefollowing the command (as root):

# grub-install /dev/hda

This command installs GRUB into the MBR of the first hard drive. Thegrub-install options are:

--force-lba

Force GRUB to use LBA mode, to allow booting from partitions beyondcylinder 1024.

--grub-shell= file

Specify that file is to be used as the GRUBshell. You might want to use this option to append options togrub. For example:

% grub-install --grub-shell="grub --read-only" /dev/fd0
-h, --help

Print a help message on standard output and exit.

--recheck

Force probing of a device map. You should run grub-install with this option if you add orremove a disk from your system. The device map is found at/boot/grub/device.map.

--root-directory= dir

Install GRUB images in the directory dir insteadof the GRUB root directory.

-v, --version

Print the GRUB version number to standard output and exit.

Installing from the GRUB command line

To install GRUB from the native command environment, make a GRUB bootfloppy as described previously. You will use that floppy to boot tothe GRUB command line to do the installation. If you know whichpartition holds the GRUB files, you’re all set.Otherwise, you can find the partition with the find command:

grub> find /boot/grub/stage1(hd0,0)

Here, the files are on (hd0,0). Use that information to set the GRUBroot device:

grub> root (hd0,0)

Run the setup command to installGRUB. To install GRUB on the MBR, run setup as follows:

grub> setup (hd0)

If you are going to chainload Linux and want to install GRUB on theboot sector of the Linux partition, run setup like this:

grub> setup (hd0,0)

The GRUB Configuration File

GRUB uses a configuration file that setsup the menu interface. The configuration file is calledgrub.conf and is found with the other GRUB filesin the /boot/grub directory.grub.conf is also known asmenu.lst, and at least on some distributions(e.g., Red Hat 8), menu.lst is a symbolic linkto grub.conf.

The configuration file begins with a section containing globalcommands that apply to all boot entries, followed by an entry foreach Linux image or other operating system that you want to be ableto boot. Here is an example of a global section (a hash sign, #, begins a comment):

default=0 # default to the first entrytimeout=20 # set the timeout to 20 secondssplashimage=(hd0,0)/grub/splash.xpm.gz # the splash image displayed with # the menu

Certain GRUB commands are available only in the global section of theconfiguration file, for use with the GRUB menu. These commands aredescribed in the following list. All other commands can be usedeither in the configuration file or on the command line and aredescribed later in Section 4.4.

default num

Set the default menu entry to num. The defaultentry is started if the user does not make a selection before thetimeout time. Menu entries are numbered from 0. If no default isspecified, the first entry (0) is used as the default.

fallback num

Specify the entry to be used if for any reason the default entry haserrors. If this command is specified and the defaultdoesn’t work, GRUB boots the fallback entryautomatically instead of waiting for user input.

hiddenmenu

Specify that the menu is not to be displayed. The user can press Escbefore the end of the timeout period to have the menu displayed;otherwise the default entry is booted at the end of the timeout.

timeout time

Specify the timeout period, in seconds. The timeout is the amount oftime GRUB waits for user input before booting the default entry.

title name

Start a new boot entry with specified name.

Following the global section, the configuration file includes anentry for each boot image. An entry begins with a title command that specifies the text thatwill appear on the menu for that entry when the system boots. Atypical boot entry might look like this one from a Red Hat 8.0system:

title Red Hat Linux (2.4.18-14)root (hd0,1)kernel /vmlinuz-2.4.18-14 ro root=LABEL=/ hdc=ide-scsiinitrd /initrd-2.4.18-14.img

This entry provides the information GRUB needs to boot to Linux. Whenthe menu is displayed, it will include an entry that says:

Red Hat Linux (2.4.18-14)

The GRUB root is on the second partition of the first hard drive(hd0,1). The kernel commandspecifies which Linux kernel to run and passes some parameters to thekernel, and the initrd command setsup an initial RAM disk.

The configuration file also provides somesecurity features, such as theability to set passwords and to lock certain entries so only the rootuser can boot them. The configuration file can be set up so that apassword is required to run interactively (i.e., for editing menuentries or using the command interface) or simply to protect certainmenu entries while leaving other entries available to all users. Seethe explanation of the password andlock commands in Section 4.4.

In addition to providing a password feature, GRUB provides thecommand md5crypt to encryptpasswords in MD5 format, and a corresponding Linux command, grub-md5-crypt. grub-md5-crypt is a shell script that acts asa frontend to the grub shell,calling md5crypt. Passwordsencrypted either directly with md5crypt or with grub-md5-crypt can be used with the password command to set up a GRUB password.grub-md5-crypt has three possibleoptions:

--help

Print help message and exit.

--grub-shell= file

Specify that file is to be used as the GRUBshell.

--version

Print version information and exit.

Using the Menu Interface

The most common way to use GRUB iswith the menu interface. The Stage 2 loader reads the configurationfile grub.conf and displays the menu. If atimeout is set in the configuration file, GRUB displays a countdownat the bottom of the window showing how much time is left before itboots to the default entry. Move the cursor to an entry and pressEnter to boot; or, press e to editthe command line for that entry, ato modify the kernel arguments, or cto go to the command-line interface to issue commands manually.

If you go to the command line, you can return to the menu at any timeby pressing Esc.

Selecting a and e are similar, except that a displays only the kernel command line and lets you appendoptions to it, while e displays theentire boot entry for you to edit. In either case, the availableediting commands are similar to those available on the shell commandline. When you are through editing, press Esc to return to the mainmenu. Your changes take effect for this session only; theconfiguration file is not permanently changed.

One common use for editing a kernelcommand is to boot to single-user mode. To do that, select a from the menu and append the word“single” to the end of thekernel command. Then press Esc toreturn to the menu and select the entry.

The GRUB Shell

Inaddition to using the command line from within the GRUB menuinterface (or booting directly to the command line), you can run aGRUB shell directly from the Linux command line with the grub command. For the most part, using thegrub shell is the same as running inthe native command-line environment. The major difference is that theshell uses operating system calls to emulate the BIOS calls that thenative environment uses. That can lead to some differences inbehavior.

The syntax of the grub command is:

grub [options]

For example:

% grub --no-floppy

The grub command-line options are:

--batch

Turn on batch mode for noninteractive use. Equivalent to grub --no-config-file --no-curses--no-pager.

--boot-drive= drive

Use drive as the Stage 2 boot drive, specifiedas a decimal, hexadecimal, or octal integer. The default ishexadecimal 0x0.

--config-file= file

Use file as the GRUB configuration file. Thedefault is /boot/grub/grub.conf.

--device-map= file

Use file for the device map. The value offile is usually/boot/grub/device.map.

--help

Display a help message to standard output and exit.

--hold

Wait for a debugger to attach before starting grub.

--install-partition= partition

Use partition as the Stage 2 installationpartition, specified as a decimal, hexadecimal, or octal number. Thedefault is hexadecimal 0x20000.

--no-config-file

Run without reading the configuration file.

--no-curses

Don’t use the curses interface for managing the cursor onthe screen.

--no-floppy

Don’t probe for a floppy drive. This option isignored if --device-map is alsospecified.

--no-pager

Don’t use the internal pager.

--preset-menu

Use a preset menu, for example if your system has no console and youneed to get a serial terminal set up to see messages. To use thisoption, compile GRUB with the --enable-preset-menu= fileoption and create a menu file. See the GRUB documentation for moreinformation.

--probe-second-floppy

Probe the second floppy drive (which is not probed by default). Thisoption is ignored if --device-map is also specified.

--read-only

Do not write to any disk drives.

--verbose

Print verbose messages.

--version

Print version information and exit.

When you run grub, you will seesomething like this:

 GRUB version 0.92 (640K lower / 3072K upper memory) [ Minimal BASH-like line editing is supported. For the first word, TAB lists possible command completions. Anywhere else TAB lists the possible completions of a device/filename. ]grub>

You can now enter commands at the“grub>” prompt. Press Tab to geta brief help message, listing all the commands:

grub> Possible commands are: blocklist boot cat chainloader cmp color configfile debug device displayapm displaymem dump embed find fstest geometry halt help hide impsprobe initrd install ioprobe kernel lock makeactive map md5crypt module modulenounzip pager partnew parttype password pause quit read reboot root rootnoverify savedefault serial setkey setup terminal testload testvbe unhide uppermem vbeprobe

Using Tab is a quick way to remind yourself of the commands, but itcan be confusing to see them all run together and wrapping acrosslines. You can also run the helpcommand, which lists the most frequently used commands and theirsyntax:

grub> helpblocklist FILE bootcat FILE chainloader [--force] FILEcolor NORMAL [HIGHLIGHT] configfile FILEdevice DRIVE DEVICE displayapmdisplaymem find FILENAMEgeometry DRIVE [CYLINDER HEAD SECTOR [ halt [--no-apm]help [--all] [PATTERN ...] hide PARTITIONinitrd FILE [ARG ...] kernel [--no-mem-option] [--type=TYPE]makeactive map TO_DRIVE FROM_DRIVEmd5crypt module FILE [ARG ...]modulenounzip FILE [ARG ...] pager [FLAG]partnew PART TYPE START LEN parttype PART TYPEquit rebootroot [DEVICE [HDBIAS]] rootnoverify [DEVICE [HDBIAS]]serial [--unit=UNIT] [--port=PORT] [-- setkey [TO_KEY FROM_KEY]setup [--prefix=DIR] [--stage2=STAGE2_ terminal [--dumb] [--timeout=SECS] [--testvbe MODE unhide PARTITIONuppermem KBYTES vbeprobe [MODE]

You can add the --all option tosee all the commands.

To get help for a specific command, add the command name (e.g.,help read). help treats the text you enter as a pattern;therefore, if you enter help find,you’ll get help for the find command, but if you enter help module, you’ll get helpfor both module and modulenounzip.

Linux in a Nutshell, Fourth Edition (2024)

References

Top Articles
Latest Posts
Article information

Author: Kimberely Baumbach CPA

Last Updated:

Views: 5671

Rating: 4 / 5 (61 voted)

Reviews: 92% of readers found this page helpful

Author information

Name: Kimberely Baumbach CPA

Birthday: 1996-01-14

Address: 8381 Boyce Course, Imeldachester, ND 74681

Phone: +3571286597580

Job: Product Banking Analyst

Hobby: Cosplaying, Inline skating, Amateur radio, Baton twirling, Mountaineering, Flying, Archery

Introduction: My name is Kimberely Baumbach CPA, I am a gorgeous, bright, charming, encouraging, zealous, lively, good person who loves writing and wants to share my knowledge and understanding with you.