GRUB - invoking the grub shell
Welcome to the tutorialg guide. The tutorial will proivded a user with guidance and instructions on invoking the grub shell.
A user should note that the grub shell is an emulator; it doesn’t run under the native environment, so it sometimes does something wrong. It is advised that not to trus grub shell too much. If there is anything wrong with it, a user shouldn’t hesitate to try the native GRUB environment, especially when it guesses a wrong map between BIOS drives and OS devices.
A user can use the command grub for installing GRUB under the operating systems and for a testbed when a user adds a new feature into GRUB or when fixing a bug. grub is almost the same as the Stage 2, and, in fact, it shares the source code with the Stage 2 and a user can use the same commands in grub. It is emulated by replacing BIOS calls with UNIX system calls and libc functions.
The command grub accepts the following options:
–help
Print a summary of the command-line options and exit.
–version
Print the version number of GRUB and exit.
–verbose
Print some verbose messages for debugging purpose.
–device-map=file
Use the device map file file.
–no-floppy
Do not probe any floppy drive. This option has no effect if the option –device-map is specified.
–probe-second-floppy
Probe the second floppy drive. If this option is not specified, the grub shell does not probe it, as that sometimes takes a long time. If a user specifies the device map file, the grub shell just ignores this option.
–config-file=file
Read the configuration file file instead of /boot/grub/menu.lst. The format is the same as the normal GRUB syntax.
–boot-drive=drive
This argument should be an integer (decimal, octal or hexadecimal).
–install-partition=par
Set the stage2 install_partition to par. This argument should be an integer (decimal, octal or hexadecimal).
–no-config-file
Do not use the configuration file even if it can be read.
–no-curses
Do not use the screen handling interface by the curses even if it is available.
–batch
This option has the same meaning as `–no-config-file –no-curses’.
–read-only
Disable writing to any disk.
–hold
Wait until a debugger will attach. This option is useful when a user wants to debug the startup code.
How to install GRUB via grub
The installation procedure is the same as under the native Stage 2. An installation procedure is shown below:
Installation
In order to install GRUB as the boot loader, a user will need to first install the GRUB system and utilities under the UNIX-like operating system. This can be done either from the source tarball, or as a package for the Operating System.
After that a user will need to install the boot loader on a drive (floppy or hard disk). There are two ways of doing that - either using the utility grub-install on a UNIX-like OS, or by running GRUB itself from a floppy.
A user should note that if GRUB is installed on a UNIX-like OS, then a user should ensure that they have an emergency boot disk ready, so that a user can rescue the computer if, by any chance, the hard drive becomes unusable (unbootable).
GRUB comes with boot images, which are normally put in the directory /usr/lib/grub/i386-pc. If a user does not use grub-install, then he/she needs to copy the files stage1, stage2, and *stage1_5 to the directory /boot/grub, and run the grub-set-default if a user intends to use `default saved’ in the configuration file.
Creating a GRUB boot floppy
To create a GRUB boot floppy, as user needs to take the files stage1 and stage2 from the image directory, and write them to the first and the second block of the floppy disk, respectively.
On a UNIX-like operating system, that is done with the following commands:
# cd /usr/lib/grub/i386-pc
# dd if=stage1 of=/dev/fd0 bs=512 count=1
1+0 records in
1+0 records out
# dd if=stage2 of=/dev/fd0 bs=512 seek=1
153+1 records in
153+1 records out
#
The device file name may be different. Consult the manual for your OS.
Installing GRUB natively
GRUB can currently boot GNU Mach, Linux, FreeBSD, NetBSD, and OpenBSD directly, so using it on a boot sector (the first sector of a partition) should be alright.
If a user decides to install GRUB in the native environment, which is definitely desirable, then he/she will need to create a GRUB boot disk, and reboot the computer with it.
GRUB will show the command-line interface. First, set the GRUB’s root device to the partition containing the boot directory, like this:
grub> root (hd0,0)
If a user is not sure which partition actually holds this directory, use the command find like this:
grub> find /boot/grub/stage1
This will search for the file name /boot/grub/stage1 and show the devices which contain the file.
Once a user has set the root device correctly, run the command setup:
grub> setup (hd0)
This command will install the GRUB boot loader on the Master Boot Record (MBR) of the first drive. If a user wants to put GRUB into the boot sector of a partition instead of putting it in the MBR, specify the partition into whidch a user wants to install GRUB:
grub> setup (hd0,0)
If a user installs GRUB into a partition or a drive other than the first one, a user must chain-load GRUB from another boot loader.
After using the setup command, a user will boot into GRUB without the GRUB floppy.
The command grub-specific information is described here.
What a user should be careful about is buffer cache. grub makes use of raw devices instead of filesystems that the operating systems serve, so there exists a potential problem that some cache inconsistency may corrupt the filesystems. It is recommended that:
• If a user can unmount drives to which GRUB may write any amount of data, unmount them before running grub.
• If a drive cannot be unmounted but can be mounted with the read-only flag, mount it in read-only mode. That should be secure.
• If a drive must be mounted with the read-write flag, make sure that no activity is being done on it while the command grub is running.
• Reboot the operating system as soon as possible. This is probably not required if a user follows the rules above, but reboot is the most secure way.
In addition, enter the command quit when a user finishes the installation. That is very important because quit makes the buffer cache consistent. Do not push .
If a user wants to install GRUB non-interactively, specify `–batch’ option in the command-line. Please view example as provided:
#!/bin/sh
# Use /usr/sbin/grub if you are on an older system.
/sbin/grub –batch </dev/null 2>/dev/null
root (hd0,0)
setup (hd0)
quit
EOT
The map between BIOS drives and OS devices
When a user specifies the option –device-map, the grub shell creates the device map file automatically unless it already exists. The file name /boot/grub/device.map is preferred.
If the device map file exists, the grub shell reads it to map BIOS drives to OS devices. This file consists of lines like this:
device file
device is a drive specified in the GRUB syntax, and file is an OS file, which is normally a device file.
The reason why the grub shell gives a user the device map file is that it cannot guess the map between BIOS drives and OS devices correctly in some environments. For example, if a user wants to exchange the boot sequence between IDE and SCSI in a users BIOS, it gets the order wrong.
Thus, edit the file if the grub shell makes a mistake. A user should put any comments in the file if needed, as the grub shell assumes that a line is just a comment if the first character is `#’.
Invoking grub-install
The program grub-install installs GRUB on a users drive using the grub shell. A user must specify the device name on which a user wants to install GRUB, like this:
grub-install install_device
The device name install_device is an OS device name or a GRUB device name.
grub-install accepts the following options:
–help
Print a summary of the command-line options and exit.
–version
Print the version number of GRUB and exit.
–force-lba
Force GRUB to use LBA mode even for a buggy BIOS. Use this option only if the BIOS doesn’t work properly in LBA mode even though it supports LBA mode.
–root-directory=dir
Install GRUB images under the directory dir instead of the root directory. This option is useful when a user wants to install GRUB into a separate partition or a removable disk. Here is an example in which a user has a separate boot partition which is mounted on /boot:
grub-install –root-directory=/boot hd0
–grub-shell=file
Use file as the grub shell. A user can append arbitrary options to file after the file name, like this:
grub-install –grub-shell=”grub –read-only” /dev/fd0
–recheck
Recheck the device map, even if /boot/grub/device.map already exists. A user should use this option whenever he/she wants to add/remove a disk into/from the computer.
Invoking grub-md5-crypt
The program grub-md5-crypt encrypts a password in MD5 format. This is just a frontend of the grub shell. Passwords encrypted by this program can be used with the command password.
grub-md5-crypt accepts the following options:
–help
Print a summary of the command-line options and exit.
–version
Print the version information and exit.
–grub-shell=file
Use file as the grub shell.
Invoking grub-terminfo
The program grub-terminfo generates a terminfo command from a terminfo name. The result can be used in the configuration file, to define escape sequences. Because GRUB assumes that the terminal is vt100-compatible by default, this would be useful only if a terminal is uncommon (such as vt52).
grub-terminfo accepts the following options:
–help
Print a summary of the command-line options and exit.
–version
Print the version information and exit.
A user must specify one argument to this command. For example:
grub-terminfo vt52
Invoking grub-set-default
The program grub-set-default sets the default boot entry for GRUB. This automatically creates a file named default under the GRUB directory (i.e. /boot/grub), if it is not present.
This file is used to determine the default boot entry when GRUB boots up the system when a user uses the `default saved’ in the configuration file, and to save next default boot entry when a user uses `savedefault’ in a boot entry.
grub-set-default accepts the following options:
–help
Print a summary of the command-line options and exit.
–version
Print the version information and exit.
–root-directory=dir
Use the directory dir instead of the root directory (i.e. /) to define the location of the default file. This is useful when a user mounts a disk which is used for another system.
A user must specify a single argument to grub-set-default. This argument is normally the number of a default boot entry. For example, if a user have this configuration file:
default saved
timeout 10
title GNU/Hurd
root (hd0,0)
…
title GNU/Linux
root (hd0,1)
…
and if a user wants to set the next default boot entry to GNU/Linux, a user may execute this command:
grub-set-default 1
Because the entry for GNU/Linux is `1′. Note that entries are counted from zero. So, if a user wants to specify GNU/Hurd here, then a user should specify `0′.
This feature is very useful if a user wants to test a new kernel or to make the system quite robust.
If a user follows this tutorial guide then he/she would have learnt about invoking the grub shell.













