Using GRUB to overcome boot problems
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
Conclusion
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.







March 29th, 2007 at 10:48 pm
How to use GRub in linux?
April 4th, 2007 at 9:04 pm
Hello
How to deal with situation when grub-install script does not recognize software raid devices /dev/md? . It says something like “…/dev/md0 does not have corresponding bios drive”
Regards
Nikolay Kabaivanov.
April 4th, 2007 at 9:55 pm
With sw RAID devices you have to instruct grub specifically on the BIOS harddisk position and partition location as with a normal ext3 partition. Thus in case of RAID1, you would manually install grub on both 1st disk [ root (hd0,0), setup (hd0) ] and immediately also on the second disk [ root (hd1,0), setup(hd1) ].
April 5th, 2007 at 1:47 am
Hi,
Can you please explain how we can reinstall grub from grub command prompt.
Thanks
Noushad.
April 5th, 2007 at 2:53 am
Noushad,
It depends what you mean by “reinstall.” If you need to repair GRUB you will probably have to boot into the rescue environment using a rescue CD. To reinstall GRUB and its various components, you will have to use a command like the following:
rpm -ivh /path/to/grub-0.95-3.5.i386.rpm –root /mnt/sysimage –force
(assuming you have access to a current copy of the grub RPM in the rescue environment). Next we need to reinstall GRUB into the master boot record of the hard drive. In the following example, I’m going to assume the /boot partition is the first partition of the first disk drive:
bash# grub
GNU GRUB version 0.95 (640K lower / 3072K upper memory)
[welcome banner snipped]
grub> root (hd0,0)
Filesystem type is ext2fs, partition type 0×83
grub> setup (hd0)
Checking if “/boot/grub/stage1″ exists… no
Checking if “/grub/stage1″ exists… yes
Checking if “/grub/stage2″ exists… yes
Checking if “/grub/e2fs_stage1_5″ exists… yes
Running “embed /grub/e2fs_stage1_5 (hd0)”… 15 sectors are embedded.
succeeded
Running “install /grub/stage1 (hd0) (hd0)1+15 p (hd0,0)/grub/stage2 /grub/grub
.conf”… succeeded
Done.
grub> quit
Now you have grub reinstalled into the master boot record of your machine.
George
April 5th, 2007 at 10:47 am
Your coverage of grub is welcome.
My preference is a bootable CD with grub on it … boots in seconds to the grub> prompt and can be used to re-install grub along with a myriad of other little tasks. On my list of “want to do’s” is putting grub onto a bootable flash for the same reason. Documented in the grub manual and there are other informal howto’s on line as well.
April 22nd, 2007 at 1:40 pm
Does anyone know if it is possible check the system time using grub and boot a particular OS depending on the time of day?
Arvind
April 24th, 2007 at 5:18 am
Arvind,
GRUB won’t do that kind of thing automatically. It doesn’t have any commands which will read the system clock. GRUB is pretty smart, but it’s not as smart as an operating system.
One possibility is to have an operating system detect the current time, modify the GRUB config file to boot the other OS, and then reboot.
Perhaps if we take a step back and look at the BIG picture you may want to use virtualization to accomplish what you need.
George
April 26th, 2007 at 3:18 am
Hello
How do i choose the Command Line insted of GUI(or vice versa) when i reboot my fedora 6?
thanks
April 27th, 2007 at 9:27 am
I have software-based RAID1 device (/dev/md0) and allowed up2date to update my kernel. Now the /boot folder on (hd1,0) has the updated kernel files while /boot on (hd0,0) does not. How do I get grub to update the /boot folder on (hd0,0) with the new kernel files and grub.conf (this partition is not available after boot-up - it is buried by /dev/md0).
Thanks.
April 28th, 2007 at 1:49 pm
Jubrits,
To go in to command line vs. GUI mode when you reboot, when GRUB starts the countdown, hit [escape] then use the ‘a’ command to add a runlevel at the end of the kernel line. If you type a 3, you will go into a text based login. A 5 at the end of the kernel line will take you into GUI mode. A better approach would be to copy a stanza in your grub.conf file and make the original stanza GUI and your copy go into text mode.
AgarTurbo,
I’m not sure how your disk is layed out exactly. If (hd1,0) and (hd0,0) are RAID1 devices, they should both have copies of the same kernels and other files.
George
May 8th, 2007 at 11:28 am
Hi George,
Thanks for your response. I thought of letting the OS boot, check the system time and then modify GRUB accordingly and then re-boot. If the default OS is Linux then this is fine, but what happens when it makes Windows the default. The next time the machine boots, I don’t think Windows can modify the grub config file to make the machine boot back into Linux.
I know (based on experience with Mandrake 9), that LILO can be set up so that on rebooting, the computer boots into a particular OS WITHOUT necessarily permanently setting that OS as the default. Can GRUB do something similar?
If I can get GRUB to do that then I can set Linux as the default OS, and then have Linux check the system time and re-boot into the desired OS according to the time of day.
What do you think?
May 8th, 2007 at 2:55 pm
Arvind,
The /boot partition doesn’t have to be ext3 for GRUB to work. So why not format /boot as vfat/fat32, that way both linux and Windows could access it. If the /boot partition is located past the C: drive, you would access it as D:. You would need to write a Windows script that could modify D:\grub\grub.conf properly (line endings should be LF, not CR-LF like Windows prefers).
George
July 3rd, 2007 at 11:55 am
…but anyway, what problems can rise the GRUB? i don’t know why my Linux don’t want to starting up. Is GRUB can help me to know it?
July 5th, 2007 at 11:14 am
hi,
I have linux insatlled on a IBM A Pro the problem is after booting it just flashes the nvidia screen continuously. like the nvidia screen with logo comes and then there is a blank. than again nvidia logo screen comes and there is a blank. can any one suggest what to do. Can GRUB solve this problem
July 5th, 2007 at 12:07 pm
GRUB can’t solve your problem, but it can help you get control of your system so you can.
The problem you are having is your system is trying to come up in runlevel 5 (X graphical login) and somehow X is misconfigured. When you boot, interrupt GRUB and use the “append” command to add a 3 to the end of the kernel line. This will cause you to enter runlevel 3 (non-graphical multiuser mode). At this point you can login as root and try to get your graphics problem corrected.
August 5th, 2007 at 4:49 am
my pc is not shutdown properly due to fluctuation in light.so it causes problem in booting.when it boots it shows no logical drive found message and swap space ok then hangs up
August 13th, 2007 at 2:19 pm
George,
This is more of a “GRUB related” question concerning the GRUB specified kernel/initrd images running and then encountering problems mounting the disk.
If there is a filesystem related module loading problem or mismatch in partition-labels, that causes the kernel to panic during mount time…
How do I navigate the apparently frozen screen up, or, how would I configure GRUB to log kernel/ramdisk errors as it tries to mount the disk? Sometimes important errors run off the screen during this critical period between GRUB-ramdisk and init. It *would* be great to boot back up in rescue mode and see what the boot messages that ran off the screen were.
Thanks!
-wayne
August 14th, 2007 at 9:51 am
Satish,
Your problem will probably require you to use the rescue environment to find out what is happening to your disks. GRUB successfully loads the kernel and ramdisk image and it sounds like init is getting started, but your disk subsystems are probably having issues. You may need to run fsck from the rescue environment to fix the problems that cause your disks not to be mounted.
Wayne,
GRUB will not log to disk. It loads the kernel into memory then has the processor execute the kernel. So when your messages stream by off of the screen, it is the kernel doing that, not GRUB. Also GRUB is not an O/S, so you can’t get it to write or log to disk (not enough stuff running yet).
I think you can type [ctl]S and [ctl]Q to get the kernel to pause/resume when it boots up. Give that a try. Also try [shift]PgUp to see if you can go back to earlier pages. I’m sure this won’t work once you get the kernel panic.
George
August 31st, 2007 at 1:00 pm
I have a seperate problem when I run setup (hd0)
this is what I get:
grub> root (hd0,0)
Filesystem type is ext2fs, partition type 0×83
grub> setup (hd0)
Checking if “/boot/grub/stage1″ exists… no
Checking if “/grub/stage1″ exists… yes
Checking if “/grub/stage2″ exists… yes
Checking if “/grub/e2fs_stage1_5″ exists… yes
Running “embed /grub/e2fs_stage1_5 (hd0)”… 15 sectors are embedded.
succeeded
Running “install /grub/stage1 (hd0) (hd0)1+15 p (hd0,0)/grub/stage2 /grub/grub
.conf”… failed
Error 22: Not such partition
How can I fix this?
August 31st, 2007 at 8:39 pm
I need some more background. How did you get to that point, Sean?
October 7th, 2007 at 7:56 pm
Your problem will probably require you to use the rescue environment to find out what is happening to your disks.
October 22nd, 2007 at 8:50 am
i have purcgased a new laptop which consists of a 160gb hdd, configured with linux as primary and the other has w95.
by mistake i have deleted both the partition.
now when i boot up i get only
grub>
on the screen.
i dont have a linux cd nor a recovery cd. I only have a W-xP cd, which i want to install in this machine.
what do i do., how do i go about it
November 6th, 2007 at 11:20 pm
I have a problem,I install linux enterprise edition -4 & I was having windows xp also on my laptop ,after a few days I formatted the drive which was containing the Linux.After this when I restarted the laptop again it is not allowing me to boot windows,only a grub comamand prompt is coming .How can I solve this problem.Please someone help me .Thanks!
November 19th, 2007 at 6:14 pm
Vivek,
The first stage of GRUB lives in the MBR (first sector) of your hard drive and it is still intact. When your system boots, the first stage of GRUB looks for the second stage of GRUB which lives in the /boot/grub directory in Fedora/RHEL. Since you reformatted your disk with Linux, GRUB can’t find the second stage (therefore your system won’t boot properly).
One way you can solve your problem is to reinstall Linux.
Another option is to boot into Windoze and use the FIXMBR command to reinstall ntloader into the MBR.
Another way is to boot into the rescue environment and use it to install GRUB onto a memory stick. You will need to configure grub to boot from the Windoze partition (look at the rootnoverify and chainloader commands for more info).
January 27th, 2008 at 7:31 am
Hi,
I have used follwin grub command to recover my overwriten MBR
grub>partnew (hd0,0) 0×0b 63 1112399
grub>partnew (hd0,0) 0×0b 1112462 1112399
when I restarted the comp i am unable to come out of bios prompt
Enter setup….
press F12 to select nework boot….
please help.
January 29th, 2008 at 2:06 am
hi,
I have few Linux ISOs on hard disk. pls tell me how to install
from hard disk.I have GRUB too.
February 10th, 2008 at 2:40 am
Hi,
I have used follwing grub command to recover my overwriten MBR
i.e.
grub>partnew (hd0,0) 0×0b 63 1112399
grub>partnew (hd0,1) 0×0b 1112462 1112399
when I restarted the comp I am unable to come out of bios prompt
It got struc at follwing Messge.
“Enter setup….”
“press F12 to select nework boot….”
please help.
February 17th, 2008 at 5:00 am
Hi,
I have used follwing grub command to recover my overwriten MBR
i.e.
grub>partnew (hd0,0) 0×0b 63 1112399
grub>partnew (hd0,1) 0×0b 1112462 1112399
when I restarted the comp, I see that its unable to come out of bios prompt
It got struc at follwing Messge.
“Enter setup….”
“press F12 to select nework boot….”
1)Plese let me whats wrong int the usage of PARTNEW command
as i mentioned above.
2) How To Recover my harddisk from here now..
Please guide me
Thanks,
Arun
February 25th, 2008 at 9:40 am
hi,
I have Windows XP and Linux installed on my machine. Today suddenly, when I booted my PC, the grub> command prompt is being shown instead of the list of the OS’s.
I don’t have a recovery CD.
What do i do?
P.S. When I tried “find /grub/grub.conf” command, nothing is returned and it shows back the grub> command line.
April 14th, 2008 at 1:27 pm
Would you tell me command to log onto windoze partition? Have 3 partitions hdo,o) hdo ,1 and then windoze for xp pro, which - cannot lopg on due to cd w/o sp1 2
Thank you
April 14th, 2008 at 3:59 pm
Arun,
How did you determine the sector addresses of your partitions? Also if both partitions you are creating are 0×0b (W95 FAT32) why are you using GRUB to try to recover Windows?
Diganth,
To install from the ISO’s, you need to first extract the anaconda kernel and initial RAM disk from the DVD. Mount the DVD using a loop interface and extract the /images/pxeboot directory. That directory contains a vmlinuz file (kernel) and an initrd.img. Put these in a directory GRUB can access (/boot?) and boot the anaconda kernel with the RAM disk using grub. The commands you will need to use are ROOT, KERNEL, and INITRD.
Sayantani,
What did you recently do to your PC since your last successful boot? At least GRUB is able to find stage2 and display the GRUB> prompt. It cannot find the GRUB config file so it cannot display a menu.
Not knowing how your machine is partitioned, there are a couple of reasons why the find command fails. The first is you deleted the grub.conf file so there is nothing to find. The second cause would be that you don’t have a separate /boot partition. If this is the case, try ‘find /boot/grub/grub.conf’ and see if that works.
Faith,
Assuming the third partition has Windows, you may want to try the following:
rootnoverify (hd0,2)
chainloader +1
To all readers,
Each system is a little different and there can be a variety of reasons why it no longer is bootable. I hope you have found my article to be useful and informative. I will not respond to any additional responses to this article asking for troubleshooting tips for a particular system boot problem.
Best wishes,
George
May 19th, 2008 at 8:52 am
IF the computer which installed redhat is frozen, what should I do? Eg. with windows, press Ctrl+Alt+Delete, in windows task manager, I can either stop the program running or shutdown. What about with linux?
September 1st, 2008 at 12:02 am
use #ps -a or pstree to identify the process
then use #kill (process id)