Knowledge Base Glossary       Exit
(how to) Search  

Browse by Category
Knowledge Base > Operating Systems > Linux > Grub Article #4 - How To Reinstall Grub2

Grub Article #4 - How To Reinstall Grub2

This KB article is specific to Grub2. If you are working with Grub legacy, please refer to Grub Article #2 - How To Reinstall Grub.

This article covers a procedure to reinstall Grub2. It is written primarily from the perspective of BootIt BM users. When using BootIt BM as boot manager, Grub2 must be installed in a Linux partition's boot sector, not in the MBR. When Linux is booted from the BootIt BM boot menu, BootIt BM loads (chainloads) that boot sector to start Grub2, which in turn boots Linux.

The need to reinstall Grub2 can come about for several reasons, but it is usually the result of either installing Grub2 to the wrong location during installation, or changing your drive/partition configuration in some way. In most cases, the Linux installation itself will still be completely intact, and you will just need to reinstall the Grub2 boot code to the appropriate partition so that BootIt BM can boot the Linux partition again.

When to use this procedure: This procedure should be used when BootIt BM is still intact (still appears on boot), but attempting to boot Linux from the BootIt BM menu fails.

When not to use this procedure: This procedure should not be used if Grub2 has completely overwritten BootIt BM so that BootIt BM no longer appears when booting the system, and Grub appears instead.  This can happen if Grub2 gets installed to the MBR during a Linux installation, which is the default for many Linux installers. If that happens, please refer to Grub Article #3 -  How To Recover From Grub Being Installed To The MBR.

The remainder of this article details the step by step procedure. The overall procedure can be summarized as follows:

  • download and create the Grub2 boot disk
  • attempt to boot Linux from the BootIt BM menu to load the partition table
  • boot from the Grub2 boot disk
  • boot the Linux installation manually from Grub2 command prompt
  • run 'grub-install' from Linux to reinstall the Grub2 boot code to a partition
  • run 'update-grub' or 'grub2-mkconfig' from Linux to generate a new copy of grub.cfg

1. Download and create a Grub2 boot disk - this procedure will require a Grub2 boot disk (rescue disk). To ensure you have one that correlates with this article, download grub2.zip from the TeraByte Unlimited site.The grub2.zip file contains two boot images:

  grub2.iso - ISO file for burning a bootable CD/DVD

  grub2.img  - image for creating a bootable USB flash drive (UFD)

Use one of these images to create a Grub2 boot disk. If necessary, refer to the instructions in the readme.txt file included in grub2.zip.

2. Attempt to boot Linux from the BootIt BM menu to load partition table - boot the system normally up to the BootIt BM boot menu. From the boot menu, attempt to boot Linux as you normally would. This step will fail to boot Linux, but it will load the partition table with the correct partitions (as defined in Boot Edit) needed to boot Linux. This ensures that you will be able to access the Linux partition(s) from the Grub2 boot disk.

After the boot attempt, look at the View MBR window in Partition Work for the drive containing the Linux partitions. Make sure that the partitions needed to boot Linux are loaded in the MBR. If not, edit the Linux boot item in Boot Edit so that it will load the correct partitions, and then repeat this step. If the Linux partitions are not loaded in the MBR prior to booting from the Grub2 boot disk, the remainder of this procedure will not work.

3. Boot from the Grub2 boot disk (CD/DVD or UFD)this step should follow step 2, without booting anything else in between. Reboot the system directly from the Grub2 boot disk. When the boot completes, you should see a Grub text menu displayed. On that screen, type 'c' to exit to the Grub command prompt (grub >). 

From the Grub2 prompt, type the following command so that text won't scroll off screen in later steps:

grub> set pager=1

If you now run the 'set' command from the prompt, you will see that the pager variable is now set to 1.

Another useful command is 'ls', which will list the drives/partitions that Grub2 sees. These should correspond to the partitions loaded into the MBR by BootIt BM, as specified in the Boot Edit configuration for the Linux boot item (see Step 2).

4. Use the Grub2 'search' command to locate the grub.cfg file the file grub.cfg file is the configuration file for Grub2, and will be found in the /boot/grub directory (or the /boot/grub2 directory in some cases - see note about Fedora below). To find the partition containing grub.cfg, run one of the the following two commands at the prompt. Which command you type depends on whether or not you have a separate /boot partition (if not sure, try both):

 grub>  search  -f  --no-floppy  /boot/grub/grub.cfg    (use if no /boot partition)

   hd0, msdos2

 grub>  search  -f  --no-floppy  /grub/grub.cfg    (use if you have a /boot partition)

   hd0, msdos2

Note: For Fedora, OpenSUSE, and some other distributions, the grub directory can be named grub2, rather than grub. For that reason, if neither of the search commands above work, try substituting "grub2" in place of "grub" as shown below:

grub>  search  -f  --no-floppy  /boot/grub2/grub.cfg    (use if no /boot partition)

grub>  search  -f  --no-floppy  /grub2/grub.cfg    (use if you have a /boot partition)

When the search command finds grub.cfg, it will return the drive/partition where it was found in Grub2 notation. In the example above, it returned "hd0,msdos2", which means the 2nd partition (msdos2) on the first drive (hd0). Grub2 numbers drives starting at 0, and partitions starting at 1. Note that Grub2 numbers hard drives in the same order as BootIt BM (BIOS order), so a Grub2 designation of hd0 corresponds with drive HD0 in BootIt BM. The response shown above is just an example, and can/will be different depending on your hard drive configuration and partition layout.

Note that the "msdos2" partition designation shown in the example just means that a standard, DOS- type partition table is being used. That designation can be replaced with just "2", meaning that the drive/partition designation used in subsequent commands can be either (hd0,msdos2) or (hd0,2). They are equivalent.

If both commands above give an error message such as "error: no such device: /boot/grub/grub.cfg", this means that Grub2 could not find grub.cfg on any partition, and the remainder of this procedure will not work. If the error still persists after rechecking your command line, this likely means that the Linux root and/or /boot partitions are not currently loaded in the MBR. This can happen, for example, if you are not limiting primaries in BootIt BM and you skipped Step 2 above, or if your boot item is not set up correctly in Boot Edit. To resolve this, you will need to go back to Step 2 above and ensure that the correct partitions get loaded before booting from the Grub2 boot disk.

5. Use the Grub2 'configfile' command to boot into Linux - this step will also be done from the Grub2 boot disk prompt. Using the Grub2 drive/partition designation determined in Step 4, run one of the following two commands from the Grub2 command prompt. Which command to use depends on whether or not you have a separate /boot partition (the same as in Step 4):

 configfile (hd0,msdos2)/boot/grub/grub.cfg     (use if no /boot partition)

 configfile (hd0,msdos2)/grub/grub.cfg    (use if you have a /boot partition)

Note: As noted in Step 4 above, for Fedora, OpenSUSE, and some other distributions, the grub directory will be named grub2, rather than grub. Be sure to use the same path to grub.cfg that was successful when running the search command in Step 4.

The system should now boot normally into Linux. If your Linux installation normally displays the Grub2 menu on boot, that same menu should now appear, with the same menu choices, timeout, etc. If the Grub2 menu is normally hidden (such as is typically done with Ubuntu installs), it will be hidden now also, but should continue to boot up to the your normal Linux desktop. Note that to display the hidden menu in Grub2, you can hold down a Shift key while pressing <Enter> for the 'configfile' command above.

Using the 'configfile' command is usually the quickest and simplest way to get booted into Linux from the Grub2 boot disk. However, it will not always work, depending on the the specific situation. If it doesn't work, be sure to check your 'configfile' command line for typos, etc. If it still doesn't work, please see Issue 2 in the Troubleshooting Grub2 section at the end of this article. If the alternate procedure outlined there is successful in getting Linux booted, you can then continue this procedure at Step 6 below.

6. Run  the 'grub-install' command to reinstall Grub2 - once you have booted into Linux from the Grub2 boot disk, the next step is to run the 'grub-install' command, which will install the Grub2 boot code to the Linux partition's boot sector. The 'grub-install' command must be run with root privileges. For distributions with no root account enabled (such as Ubuntu), root privileges can be obtained by prefacing a command with 'sudo', and then entering your user password when prompted.

If you do not have a separate /boot partition, Grub2 should be installed to the Linux root partition. To determine the Linux designation for the root partition, you can run the 'df' command to list the currently mounted file systems. The first one listed should be the root partition (/) as shown in the sample 'df' output below:

testvm:~# df
Filesystem           1K-blocks      Used Available Use% Mounted on
/dev/sda2              7732352    745860   6593704  11% /
tmpfs                   257396         0    257396   0% /lib/init/rw
udev                     10240       140     10100   2% /dev
tmpfs                   257396         0    257396   0% /dev/shm

With the root partition being /dev/sda2, run the 'grub-install' command as follows:

testvm:~# grub-install  --force  --recheck  /dev/sda2
  <several lines of messages>
  Installation finished. No error reported.

Note: Use 'grub2-install' instead for Fedora, OpenSUSE.

If you do have a separate boot partition, Grub2 should be installed to the /boot partition. Once again, you can use the 'df' command to list the mounted file systems. The sample 'df' output shown below shows a system with a separate /boot partition, as well as a root partition:

testvm:~# df
Filesystem           1K-blocks      Used Available Use% Mounted on
/dev/sda3              7487288    696068   6410884  10% /
tmpfs                   257396         0    257396   0% /lib/init/rw
udev                     10240       144     10096   2% /dev
tmpfs                   257396         0    257396   0% /dev/shm
/dev/sda2               241113     18387    210276   9% /boot

With the /boot partition being /dev/sda2, run the  'grub-install' command as follows:

testvm:~# grub-install  --force  --recheck  /dev/sda2
  <several lines of messages>
  Installation finished. No error reported.

Note: Use 'grub2-install' instead for Fedora, OpenSUSE.

If you do not get the "Installation finished. No error reported" message after running 'grub-install', this means that the command failed. In that case, check your command line for typing errors, or other mistakes. The --force option is absolutely required to install Grub2 to any partition boot sector (but not to the MBR)The 'grub-install' command will always fail without it.  The --recheck option generates a new copy of /boot/grub/device.map, based on the current drive configuration.

Note (for Ubuntu installs only): If the 'grub-install' command above results in a "source_dir doesn't exist" error message, this is indicative of an issue encountered by some users after installing Ubuntu 12.10 and 13.04 (other recent versions possible), where the Ubuntu installer fails to install Grub2 to a partition, despite following all steps correctly. Users encountering this issue have been able to correct the situation by reinstalling the 'grub-pc' package using the following command:

testvm:~# apt-get install --reinstall  grub-pc

After reinstalling the 'grub-pc' package, repeat the 'grub-install' command above. 

7. Run  the 'update-grub' command OR the 'grub2-mkconfig' command to update the grub.cfg file

On many Linux distributions, the 'update-grub' command is available to generate an updated copy of /boot/grub/grub.cfg, which is used at boot time to display the Grub2 menu. The file won't always need to be updated, but it won't hurt to run the command in any case. This command also requires root privileges (or use sudo), and is run without any options: 

testvm:~# update-grub

On Fedora, OpenSUSE, and and some other distributions, the 'update-grub' command is not available. However, the same result can be obtained by using the 'grub2-mkconfig' command as shown below. To use this command, you must also know the path to the grub.cfg file, which for Fedora is /boot/grub2/grub.cfg. The command is executed as follows (this also requires root privileges or sudo):

testvm"~# grub2-mkconfig -o /boot/grub2/grub.cfg

Note: On the distributions that do have the 'update-grub' command available, that command is actually just a script that runs 'grub-mkconfig -o /boot/grub/grub.cfg'. Also note that this command can be named either 'grub-mkconfig' or 'grub2-mkconfig', depending on the distribution.

8. Reboot into BootIt BM and boot Linux from the boot menu

If all steps were completed successfully, the Linux installation should now be bootable from the BootIt BM menu. If not, look at the Properties screen in Partition Work for the partition that you are booting Linux from. It should say "Bootable: Yes" in the Additional Information section, which indicates that the Grub2 boot code has been installed to that partition. If not, the troubleshooting section below may be of help.

 

Troubleshooting Grub2

Issue 1: Installing Grub2 to a partition boot sector fails: Note: This problem is fixed as of Ubuntu 11.10 and later, as well as in other distributions using Grub2 of approximately the same, or later, release date. If working with an older distribution, the following is a description of the problem, and how to work around it.

In Ubuntu versions 10.04, 10.10, and 11.04, as well as in other distribution releases of approximately the same release date range, the Grub2 boot loader will not install to either a volume in an extended partition, or to a primary partition, if there is an unoccupied primary partition slot before the partition that Grub2 is being installed to. There is no failure message during the installation.

The end result will be that BootIt BM will show "Bootable: No" (because the Grub boot sector never got installed to the partition) on the Properties screen for the volume or partition from which it is trying to boot. Attempting to boot the Linux installation from the boot menu will fail with the error message "The Partition is not bootable". Attempting to correct this by installing Grub2 manually after the installation with 'grub-install' will also fail, even though there is no failure message.

If you are affected by this problem, a known workaround is to ensure that all primary partition slots that come before the partition that Grub2 is installed to (typically this is your root partition) are filled. To help clarify the point, here are 2 examples:

Example 1. The root partition is /dev/sda2, and there is is no partition in /dev/sda1. The result will be that Grub2 will not get installed to /dev/sda2 during installation, and Linux will not boot from the BootIt BM menu. There will be no failure message during installation. To correct, make the root partition /dev/sda1, or fill /dev/sda1 with another partition.

Example 2. The extended partition is /dev/sda3, and the root partition is /dev/sda5 in the extended partition.  Partition slot /dev/sda1 contains BootIt BM, but partition slot /dev/sda2 is empty. The result will be that Grub2 will not get installed to /dev/sda5 during installation, and Linux will not boot from the BootIt BM menu. There will be no failure message during installation. To correct, make the extended partition /dev/sda2, or fill /dev/sda2 with another partition.

Issue 2: Step 5 fails to boot into Linux from the Grub2 boot disk: For this situation, it is suggested that you try an alternate method to boot Linux from the Grub2 boot disk. This method will require the 3 commands described below, and they should be done in the order shown. The first 2 commands will make use of the Tab completion feature in Grub2 (pressing Tab key for filename completion).

The reason for using Tab completion is that the vmlinuz and initrd files will be named differently depending on the distribution, as well as how recently the kernel had been updated. For example, the vmlinuz file could be named vmlinuz-2.6.31-19-generic for one distribution, and vmlinuz-2.6.33-14-custom for another, etc. However, the common thread for all distributions is that these 2 files will always begin with 'vmlinuz' and 'initrd' respectively.

This method will require that you know the Linux designation for the root partition so that it can be used on the 'linux' command line below. To determine the root partition, run the following command from the Grub2 prompt:

search  -f  --no-floppy  /etc/fstab

  hd0, msdos2    (this is /dev/sda2 in Linux)

The example above returned hd0,msdos2. Since the file /etc/fstab will always be located on the root partition, this result indicates that hd0,msdos2 is the root partition. To use that on the 'linux' command line, you need to convert that from Grub2 notation to Linux notation. The Grub2 notation of hd0,msdos2 would be equivalent to Linux /dev/sda2 (1st drive, 2nd partition). As another example, a Grub2 notation of hd2,msdos1 would translate to /dev/sdc1 (3rd drive, 1st partition).

The 3 Grub2 commands required to boot Linux are listed below, followed by a more detailed explanation. Please note that the commands below are for when there is no /boot partition. If there is a /boot partition, the "/boot" would be removed from the path. Also note that the specific 'vmlinuz' and 'initrd' file names below will be different, depending on the Linux distribution, its' version, and its' update level. That is why Tab completion is used in the procedure below:

  • grub>  linux (hd0,msdos2)/boot/vmlinuz-2.6.31-19-generic root=/dev/sda2
  • grub>  initrd (hd0,msdos2)/boot/initrd.img-2.6.31-19-generic
  • grub>  boot

The 'linux' command -  this example will use (hd0, mdsos2) as the root partition, and /dev/sda2 as the Linux designation for the root partition.

grub>  linux (hd0,msdos2)/boot/vmlinuz   (now press <Tab>)

At this point in the command, press the <Tab> key. That uses the file name completion feature in Grub2, and should complete the vmlinuz file name to something similar to below:

grub>  linux (hd0,msdos2)/boot/vmlinuz-2.6.31-19-generic

Type a <space>, and then add the root= boot parameter to the end of the command line, using the Linux designation for the root partition determined above. The command line should then look similar to below:

grub>  linux (hd0,msdos2)/boot/vmlinuz-2.6.31-19-generic root=/dev/sda2

Press <Enter> to execute the command. As of this writing, the 'linux' command does not return anything when successful.

The 'initrd' command - this will use (hd0,msdos2) as the root partition.

grub>  initrd (hd0,msdos2)/boot/initrd   (now press <Tab>)

At this point in the command, press the <Tab> key. That uses the file name completion feature in Grub2, and should complete the initrd.img file name to something similar to below:

grub>  initrd (hd0,msdos2)/boot/initrd.img-2.6.31-19-generic

Press <Enter> to execute the command. As of this writing, the 'initrd' command does not return anything when successful.

The 'boot' command (assumes the first 2 commands were successful).

grub>  boot

The 'boot' command should boot Linux up to the normal user interface. If successful, you can continue with Step 6. If the boot fails, and you got no error messages from the Grub2 commands in this step, then the most likely cause would be using the wrong root partition (root=/dev/xyz) in the 'linux' command above.

Issue 3: How to access a hidden Grub2 menu (such as with Ubuntu): In some situations, you may want to access a hidden Grub2 boot menu, such as when booting into Linux manually from the Grub2 boot disk. That can be done by holding down a Shift key while pressing <Enter> for the 'configfile' command in Step 5 above, or when pressing <Enter> to boot from BootIt BM. Note that when booting Linux from BootIt BM, it will be less problematic if you use the right Shift key for this (because the left Shift key is used to do a simulated boot).

 


How helpful was this article to you?


powered by Lore