by George Hacker
Troubleshooting boot problems can be a very frustrating and challenging process. Sometimes their repair requires the Linux rescue environment, but what can be done if rescue media is not handy? If the system gets past BIOS and loads the GRUB boot loader, often there is much that can be done to get it up and running again.
Basic GRUB usage
Each time a Linux system boots, GRUB flashes by on the screen while it performs a brief countdown. The default countdown on many systems is five seconds but the actual countdown period can be adjusted. GRUB loads the Linux kernel and initial RAM disk image into system memory then fires the system up. When everything goes well and GRUB is finished with its task, the system successfully boots into Linux.
Press any key to enter the menu Booting Fedora Core (2.6.19-1.2911.fc6) in 5 seconds...
Occasionally, GRUB requires some simple intervention. Perhaps a new kernel isn’t working correctly with your system or maybe you want to bring the system into a different runlevel than the default. Hit the ESCAPE key to interrupt the GRUB countdown and bring up GRUB’s main menu.
GNU GRUB version 0.97 (640K lower / 3072K upper memory) +---------------------------------------------------------------------+ | Fedora Core (2.6.19-1.2911.fc6) | | Fedora Core (2.6.19-1.2895.fc6) | | Other | | | | | | | | | | | +---------------------------------------------------------------------+ Use the ^ and v keys to select which entry is highlighted. Press enter to boot the selected OS, 'e' to edit the commands before booting, 'a' to modify the kernel arguments before booting, or 'c' for a command-line.
At the GRUB main menu a different boot menu item can be selected using the up arrow and down arrow keys. Once the desired menu item has been highlighted, its existing kernel parameters can be changed or additional parameters can be passed to the kernel using the ‘a’ command.
[ 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. ESC at any time cancels. ENTER at any time accepts your changes.] grub append> ro root=LABEL=/ rhgb quiet 1
Using the ‘e’ command to edit a GRUB menu item
Although the ‘a’ command meets most GRUB editing needs, sometimes more extensive changes are required to get a system up and running. This is where the ‘e’ command comes in handy.
At the GRUB main menu, select the menu option you want to correct then boot from. When you use the ‘e’ command, a simple editing menu will appear allowing you to select the line you wish to edit.
GNU GRUB version 0.97 (640K lower / 3072K upper memory) +---------------------------------------------------------------------+ | root (hd0,2) | | kernel /vmlinuz-2.6.19-1.2911.fc6 ro root=LABEL=/ rhgb quiet | | initrd /initrd-2.6.19-1.2911.fc6.img | | | | | | | +---------------------------------------------------------------------+ Use the ^ and v keys to select which entry is highlighted. Press 'b' to boot, 'e' to edit the selected command in the boot sequence, 'c' for a command-line, 'o' to open a new line after ('O' for before) the selected line, 'd' to remove the selected line, or escape to go back to the main menu.
Use the arrow keys to select the particular line you want to modify, then type ‘e’ again to edit that line with the arrow keys, BACKSPACE, INSERT, or DELETE key. Changes are accepted by hitting ENTER or they can be thrown away by hitting ESCAPE. Once the correction has been accepted, typing ‘b’ will cause GRUB to start the boot sequence using the newly-made adjustments.
[ 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. ESC at any time cancels. ENTER at any time accepts your changes.] grub edit> initrd /initrd-2.6.19-1.2895.fc6.img
Note – All editing changes at boot time are temporary. As smart as GRUB is, it isn’t an operating system so it won’t update the hard drive and fix its configuration file – /boot/grub/grub.conf. That task must be done using a text editor once the system has successfully booted.
For example, let’s suppose an old IDE disk drive has been replaced with a shiny new SATA drive. The new drive has been partitioned, file systems have been formatted, and all of the old data has been copied to the new drive. An initial RAM disk image has been created, but someone forgot to update the /boot/grub/grub.conf configuration file to point to the new RAM disk.
No need for the rescue CD–interrupt the boot sequence and use the ‘e’ command to edit the initrd line in the current GRUB menu item. Point it to the new and improved RAM disk image. Once the system is up and running, the original, obsolete file that got us into this mess in the first place, /boot/grub/grub.conf, can be edited and corrected permanently.
Ultimate GRUB control – the ‘c’ command
The majority of boot problems can be solved with the ‘a’ and ‘e’ GRUB commands, but these two modes of GRUB are just the beginning of what GRUB can do. At boot time, the ‘c’ command accesses the GRUB command line, or shell. Thankfully, rebooting is not necessary to get to the GRUB shell. Log into Linux as root and execute /sbin/grub from the BASH prompt.
GNU GRUB version 0.97 (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>
Many boot-time GRUB command questions can be answered by using the ‘help’ command. Used by itself, ‘help’ displays a list of the most useful GRUB commands. When used with an argument, it displays a brief synopsis and description of the command specified.
grub> help blocklist FILE boot cat FILE chainloader [--force] FILE clear color NORMAL [HIGHLIGHT] configfile FILE device DRIVE DEVICE displayapm displaymem find FILENAME geometry DRIVE [CYLINDER HEAD SECTOR [ halt [--no-apm] help [--all] [PATTERN ...] hide PARTITION initrd FILE [ARG ...] kernel [--no-mem-option] [--type=TYPE] makeactive map TO_DRIVE FROM_DRIVE md5crypt module FILE [ARG ...] modulenounzip FILE [ARG ...] pager [FLAG] partnew PART TYPE START LEN parttype PART TYPE quit reboot root [DEVICE [HDBIAS]] rootnoverify [DEVICE [HDBIAS]] serial [--unit=UNIT] [--port=PORT] [-- setkey [TO_KEY FROM_KEY] setup [--prefix=DIR] [--stage2=STAGE2_ terminal [--dumb] [--no-echo] [--no-ed terminfo [--name=NAME --cursor-address testvbe MODE unhide PARTITION uppermem KBYTES vbeprobe [MODE] grub> help help help: help [--all] [PATTERN ...] Display helpful information about builtin commands. Not all commands aren't shown without the option `--all'. grub>
As you can see, many GRUB commands take file names as arguments. The full syntax for GRUB file names is (DEVICE,PARTITION)/PATH/NAME. DEVICE can use ‘fd0′ to specify the floppy disk. In this case PARTITION isn’t used because floppies usually contain a single file system on the entire media. Most GRUB scenarios involve access to a hard drive. Hard drives have GRUB names such as ‘hd0′ or ‘hd1′ regardless of their type–IDE vs. SCSI vs. SATA. Most of the time hard drive path names require the PARTITION number to be specified. GRUB counts partitions starting at zero so the Linux partition /dev/sda1 would be represented in GRUB as (hd0,0), /dev/sda2 would be (hd0,1) and so on.
Let’s see if we can put this new GRUB pathname knowledge to good use. The ‘cat’ GRUB command is similar to its Linux counterpart–it displays the contents of text files to the screen. When used with large files, the GRUB ‘cat’ command displays the file one screen at a time much like the Linux command ‘less’.
grub> cat (hd0,2)/sample-file Here is a small sample file of text located at /boot/sample-file. It is not very interesting. grub>
Typing full GRUB pathnames gets old fast–especially if you need to view many files while looking for useful information. The ‘root’ GRUB command instructs GRUB to search a single drive or partition when the (DEVICE,PARTITION) portion of file pathnames is omitted.
grub> root (hd0,2) Filesystem type is ext2fs, partition type 0x83 grub> cat /sample-file Here is a small sample file of text located at /boot/sample-file. It is not very interesting. grub>
Like the BASH shell, GRUB has command and filename completion that is invoked by using the TAB key. The filename in the above example could have been specified using /sa[TAB]. If more than one file starts with “sa” the TAB key will complete the filename as much as it can then display a list of all filenames that start with the given string. GRUB is smart enough to put a trailing slash at the end of directories when using filename completion.
Let’s put what we know together and use GRUB to help us determine which partition contains the root filesystem:
grub> root (hd0,1) Filesystem type is ext2fs, partition type 0x83 grub> cat /etc/passwd Error 15: File not found grub> root (hd0,2) Filesystem type is ext2fs, partition type 0x83 grub> cat /etc/passwd root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin adm:x:3:4:adm:/var/adm:/sbin/nologin lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin ... grub>
If hd0 in the example is an IDE drive, Linux refers to the root partition as /dev/hda3. This information can be passed as to the kernel as the ‘root=/dev/hda3′ parameter when we boot our system.
GRUB has a command that makes finding pathnames in partitions even easier. Amazingly enough, it’s called ‘find’. Although this command is easier to use, it can often take a very long time to execute. It will display all of the partitions that contain the filename specified.
grub> find /etc/passwd (hd0,2) grub>
GRUB will automatically invoke its shell at boot time if it cannot locate its configuration file. This happens when the configuration file is accidentally renamed or deleted. The ‘configfile’ command can be used to load an alternate configuration file that conforms to the /boot/grub/grub.conf GRUB syntax and commands. For example, the following sequence of commands will locate a backup copy of the grub.conf configuration file, assuming we have a backup copy. Before the backup configuration file can be used, its name and location must be determined:
grub> find /grub/grub.conf Error 15: File not found grub> find /grub/grub.conf.bak (hd0,2) grub> configfile (hd0,2)/grub/grub.conf.bak
We’ve explored the three basic modes of the GRUB boot loader: a – append mode, e – edit mode, and c – command mode. Also, we’ve examined some of the more useful GRUB shell commands. Don’t wait for a boot-time mishap to practice using these commands. Experiment with them when you have some spare time. Try some of the GRUB commands that weren’t discussed in this article–on a non-production server of course. Don’t fear the GRUB boot loader; embrace it and harness its power for good. Someday your system’s health may depend on it.