10/18/10

How to reinstall / fix GRUB2 after installing / dual booting Windows 7 or Ubuntu 10.10


Reinstalling GRUB 2

  

There may be times when a user needs to either move or reinstall a GRUB 2 installation. GRUB 2 needs to be reinstalled when a user is presented with a blank screen with only the word "GRUB", no prompt, and no ability to enter commands. This often happens when the MBR of the booting device is altered and GRUB 2 is removed, such as when Windows is installed after Ubuntu. Additionally, if a user cannot boot into an operating system at all, even using the rescue mode mode, a complete reinstallation of GRUB 2 may be necessary.    

 

Reinstalling from LiveCD

  

If you cannot boot from GRUB 2 review the section Boot Problems and Rescue Mode. If a reinstall becomes necessary follow these instructions. Two methods are presented; both require booting from a LiveCD (Ubuntu 9.10, Karmic Koala or later version). If the first method does not work, follow the second method, which is more complex and contains more options and instructions.   

 

SIMPLEST - Copy GRUB 2 Files from the LiveCD

  

This is a quick and simple method of restoring a broken system's GRUB 2 files. The terminal is used for entering commands and the user must know the device name/partition of the installed system (sda1, sdb5, etc). The problem partition is located and mounted from the LiveCD. The files are then copied from the LiveCD libraries to the proper locations and MBR. It requires the least steps and fewer command line entries than the following methods.  

  1. Boot to the LiveCD Desktop (Ubuntu 9.10 or later).
  2. Open a terminal by selecting Applications, Accessories, Terminal from the menu bar.

  3. Determine the partition with the Ubuntu installation. The fdisk option "-l" is a lowercase "L".

    1. sudo fdisk -l
      If the user isn't sure of the partition, look for one of the appropriate size or formatting.

      Running sudo blkid may provide more information to help locate the proper partition, especially if the partitions are labeled. The device/drive is designated by sdX, with X being the device designation. sda is the first device, sdb is the second, etc. For most users the MBR will be installed to sda, the first drive on their system. The partition is designated by the Y. The first partition is 1, the second is 2. Note the devices and partitions are counted differently.

  4. Mount the partition containing the Ubuntu installation.
    sudo mount /dev/sdXY /mnt

    Example: sudo mount /dev/sda1 Note: If the user has a separate /boot partition, this must be mounted to /mnt/boot Note: If the user has a separate /home partition, this must be mounted to /mnt/home. Encrypted home partitions should work.

  5. Run the grub-install command as described below. This will reinstall the GRUB 2 files on the mounted partition to the proper location and to the MBR of the designated device.

    sudo grub-install --root-directory=/mnt/ /dev/sdX

    Example: sudo grub-install --root-directory=/mnt/ /dev/sda

  6. Reboot
  7. Refresh the GRUB 2 menu with sudo update-grub

  8. If the user wishes to explore why the system failed, refer to Post-Restoration Commands section below.

 

METHOD 2 - Copy GRUB 2 Files from the Installed Partition

 

This is a quick and simple method of restoring a broken system's GRUB 2 files. The problem partition is located and mounted from the LiveCD. The files are then copied from the broken system's /boot/grub directory to the proper locations and MBR. It requires fewer steps and fewer command line entries than the following CHROOT method. For users with separate system partitions, such as a separate /boot partition, or other special circumstances, using the CHROOT method may provide better results.

  1. Boot to the LiveCD Desktop (Ubuntu 9.10 or later).
  2. Mount the partition with your Ubuntu installation.
    1. From the Places menu, select and click the partition containing your Ubuntu installation to mount it.

      • grub2.places.devices.png

      • If the partition isn't recognized, look for one of the appropriate size or label.
      • Once mounted, the user should see Ubuntu system directories such as /boot

  3. Open a terminal by selecting Applications, Accessories, Terminal from the menu bar.

  4. Run the grub-setup -d command as described below. This will reinstall the GRUB 2 files on the mounted partition to the proper location and to the MBR of the designated device.

    1. Determine the mount point by referring to the location box in Places. Replace XXXX in the command with the UUID, or replace /media/XXXX with the correct location if the partition is not mounted in /media. The designation may be a UUID, or a label, if the user has created one (see note).

    2. If the location window is not in the format shown, click on the icon to the left to change the presentation.
      • grub2.places.location.png

    3. When ready to copy the UUID/location, by highlighting it with the mouse. Paste it into the terminal by pressing the middle mouse button. If using the keyboard, use CTRL-SHIFT to copy the location and CTRL-SHIFT-V to paste it into the terminal.

    4. The device/drive is designated by sdX, with X being the device designation. sda is the first device, sdb is the second, etc. For most users the MBR should be installed to sda, the first drive on their system.

      sudo grub-setup -d /media/XXXX/boot/grub /dev/sda
      Example:

      sudo grub-setup -d /media/7848138a-41a0-4eba-8aed-d1b625ac8759/boot/grub /dev/sda

    5. If the user gets a "error: Cannot open /boot/grub/device.map" message, add the -m switch and path to the device.map to the command as follows:

      sudo grub-setup -d /media/XXXXX/boot/grub -m /media/XXXXX/boot/grub/device.map /dev/sda
      Example:

      sudo grub-setup -d /media/7848138a-41a0-4eba-8aed-d1b625ac8759/boot/grub -m /media/7848138a-41a0-4eba-8aed-d1b625ac8759/boot/grub/device.map /dev/sda

    6. Other switches are available for use with the grub-setup command. In a terminal, type grub-setup --help for a list of available options.

    7. You may find you get a repeated "error: Cannot open /boot/grub/device.map" message. If this happens try rebooting. The UUID method may actually work but throws an error message falsely. Using the label name does not seem to evoke the same repeated error message.

  5. Reboot
  6. Refresh the GRUB 2 menu with sudo update-grub

  7. If the user wishes to explore why the system failed, refer to Post-Restoration Commands section below.

    info.png Ubuntu 9.10 introduced a new application which can easily assign labels to existing partitions. Access Disk Utility from the System, Administration menu.

 

METHOD 3 - CHROOT

 

This method of installation uses the chroot command to gain access to the broken system's files. Once the chroot command is issued, the LiveCD treats the broken system's / as its own. Commands run in a chroot environment will affect the broken systems filesystems and not those of the LiveCD.

  1. Boot to the LiveCD Desktop (Ubuntu 9.10 or later). Please note that the Live CD must be the same as the system you are fixing - either 32-bit or 64-bit (if not then the chroot will fail).

  2. Open a terminal - Applications, Accessories, Terminal.

  3. Determine your normal system partition - (the switch is a lowercase "L")

    sudo fdisk -l

    • If you aren't sure, run

      df -Th. Look for the correct disk size and ext3 or ext4 format.

  4. Mount your normal system partition:
    • Substitute the correct partition: sda1, sdb5, etc.

    sudo mount /dev/sdXX /mnt # Example: sudo mount /dev/sda1 /mnt

  5. Only if you have a separate boot partition:

    • sdYY is the /boot partition designation (for example sdb3)
    • sudo mount /dev/sdYY /mnt/boot

  6. Mount the critical virtual filesystems:

    sudo mount --bind /dev  /mnt/dev
    sudo mount --bind /dev/pts  /mnt/dev/pts
    sudo mount --bind /proc /mnt/proc
    sudo mount --bind /sys  /mnt/sys

  7. Chroot into your normal system device:

    sudo chroot /mnt

  8. If there is no /boot/grub/grub.cfg or it's not correct, create one using

    update-grub

  9. Reinstall GRUB 2:
    • Substitute the correct device - sda, sdb, etc. Do not specify a partition number.

    grub-install /dev/sdX

  10. Verify the install (use the correct device, for example sda. Do not specify a partition): sudo grub-install --recheck /dev/sdX

  11. Exit chroot: CTRL-D on keyboard

  12. Unmount virtual filesystems:

    sudo umount /mnt/dev/pts
    sudo umount /mnt/dev
    sudo umount /mnt/proc
    sudo umount /mnt/sys

    • If you mounted a separate /boot partition:

      sudo umount /mnt/boot

  13. Unmount the LiveCD's /usr directory:

    sudo umount /mnt/usr

  14. Unmount last device:

    sudo umount /mnt

  15. Reboot.

    sudo reboot

 

Post-Restoration Commands

 

Once the user can boot to a working system, try to determine why the system failed to boot. The following commands may prove useful in locating and/or fixing the problem.

  • To refresh the available devices and settings in /boot/grub/grub.cfg

    • sudo update-grub

    To look for the bootloader location.
    • grub-probe -t device /boot/grub

    To install GRUB 2 to the sdX partition's MBR (sda, sdb, etc.)

    • sudo grub-install /dev/sdX

    To recheck the installation. (sda, sdb, etc.)

    sudo grub-install --recheck /dev/sda

If you need help on this, just leave a comment or follow me @bithack3r and send me a driect message.

 

Posted via email from BitHacker's Posterous

No comments:

Post a Comment

Post a Comment