HOWTO: Purge and Reinstall Grub 2 from the Live CD

There are several ways to repair Grub 2. In this post, we will completely remove and then reinstall Grub 2 using an Ubuntu Desktop Installation CD, which I will refer to as the Live CD.

Note: If you can get to your normal installation without using a Live CD the chroot procedure is not necessary and you need only Steps 2-5.

I would recommend this procedure only after running meierfra’s boot info script and finding no obvious discrepancies. The reasons for this include:

  • During this procedure, you will temporarily have no bootloader on your system.
  • If you designated the wrong drive/partition during the initial install, you may make the same mistake again.
  • You will lose any customizations you have made to your Grub 2 configuration files.
  • Other methods, such as “grub-install” or “grub-install –reinstall” are less intrusive on your system. Doing the least necessary to fix a system is generally a good idea.

This thread is primarily for reference. If you need help interpreting the output of the boot info script, please start your own thread in one of the other forums. Depending on your experience level, Absolute Beginners or Installation & Upgrades might be appropriate forums.

What can I try first?

From the Normal OS (not Live CD).

  • Change the BIOS boot order. If you installed Grub 2 from another device, changing the boot order to the original installing device may find a bootable Grub installation. Once booted and in your normal Ubuntu system, run “sudo dpkg-reconfigure grub-pc” and select additional devices on which to write Grub 2. Example: If you installed to your interneal hard drive from a USB drive, change the BIOS to boot from the USB drive first. If Ubuntu now boots, run the dpkg-reconfigure command and be sure to include the internal drive in the “Grub install devices” options.
  • Reinstalling Grub 2 may be another option. A reinstall will rewrite the MBR and refresh the modules in the boot/grub folder. The “update-grub” command should be run as root after installing Grub 2, as the grub.cfg file is not generated by the installation.
  • Replace the X value (sda, sdb, etc). This is normally the drive with the Ubuntu OS. This should be the drive the computer’s BIOS boots. The command assumes the /bootfolder is not on a separate partition. These commands should not be run from a Live CD.
    Code:
    sudo grub-install --recheck /dev/sdX
    sudo update-grub

From the Live CD:

Important Note on Using a LiveCD:
Please use a LiveCD with the same version of Grub 2 as the OS. Not doing so may break, or fail to fix, Natty uses Grub 1.99RC. Lucid/Maverick use Grub 1.98. If Grub files are to be copied or installed to the actual OS, users of Lucid/Maverick should use a Lucid or Maverick LiveCD to repair Grub. Natty users should use a Natty LiveCD.

  • If the Ubuntu OS cannot be accessed through the normal boot process (see above), boot a compatible installation CD/Live CD. The Grub 2 files are by default installed in the partition’s /boot/grub folder and will be placed in this folder if the commands are run as instructed.

    Note 1: Running “update-grub” from the Live CD will not update the grub.cfg file of the normal installation. If the grub.cfg file is damaged or missing, the user should use thechroot procedure below to update the grub.cfg file.

    Note 2: In the following commands, the Ubuntu partition (sda1, sda5, etc) is mounted in the first command, but only the drive (sda, sdb, etc) is designated in the ‘grub-install’ command.

    Note 3: The middle commands are to be run only if the operating system has a separate partition specifically for /boot. This would normally not be the case.

    If you have a separate /boot partition run these two commands first. Most users do not have a separate /boot partition. sdXZ is the boot partition.

    Code:
    ### Run these two commands only if you have a separate /boot partition
    sudo mkdir /mnt/boot
    sudo mount /dev/sdXZ /mnt/boot  # Example: sudo mount /dev/sda6 /mnt/boot
    
    Code:
    sudo mount /dev/sdXY /mnt  # Example: sudo mount /dev/sda5 /mnt
    sudo grub-install --root-directory=/mnt /dev/sdX # Example: sudo grub-install --root-directory=/mnt /dev/sda

Purge & Reinstall without Chroot
If you wish to purge and reinstall Grub 2 from a normally running installation it is not necessary to use the chroot instructions. Following a normal boot (without the Live CD):

  • Complete only steps 2 through 5.
  • Include “sudo” at the start of each command. Example: sudo apt-get purge grub grub-pc grub-common

Chroot
Since you will boot from the Live CD, commands executed in the normal manner would act on the Live CD’s temporary system files and not the files on your real installation’s partition. By ‘chroot-ing’ into your Ubuntu partition, the commands will act on your installation and remain after you remove the CD and reboot. Use a LiveCD with the same version of Grub 2 (see note near the top of this post).

Procedure: *
Boot to the Live CD Desktop and open a terminal (Applications, Accessories, Terminal).

* If you can boot to your normal Ubuntu installation and wish to purge and reinstall Grub 2, see the section “Purge & Reinstall without Chroot“.

A note about copying: Copying these commands is easier and more accurate than typing. To paste into a linux terminal, highlight the text with your mouse, then click in the terminal with your middle mouse button to paste the contents. It’s that simple!. If you prefer to use the keyboard, paste into the terminal with CTRL-SHIFT-V.

Important: All instances of sdX and sdXY must be replaced with the correct drive/partition for your system. (sda, sdb, sda1, sda5, sdb5, etc). X is the drive letter. Y is the partition number. The first drive is “a” and the first partition is “1”. Example: sda1

1. Chroot into your real system. The following set of commands will mount the necessary system files to allow the chroot and place you in a terminal where the commands will work on your real installation. Note the middle two commands are to be run ONLY IF you have a separate partition specifically for /boot. Copying the resolv.conf file may help establish an Internet connection.

Note: The “for i in … line is a command that needs to be run. It is an efficient way to mount multiple items using a single line of code. I have added a section at the end of this post showing what the command does. *

Change sdXY to correct value (for example, sda5, sdb1, etc.).

Mounting Instructions:
When mounting the partitions, use A (separate /boot partition), B (Wubi only), or C (normal installtion).
Once you have completed the mounting instructions in the section that applies to your installation, go to Continue.

  1. Only If You Have a Separate /boot partition.
    If you have a separate /boot partition run these commands. Most users do not have a separate /boot partition and should go to the normal partition section. sdXY is the main partition. sdXZ is the boot partition.

    Code:
    sudo mkdir /mnt/temp /mnt/temp/boot  
    sudo mount /dev/sdXY /mnt/temp  # Mount the main Ubuntu partition. Example: sudo mount /dev/sda5 /mnt/temp
    sudo mount /dev/sdXZ /mnt/temp/boot  # Mount the /boot partition. Example: sudo mount /dev/sda6 /mnt/temp/boot
  2. Only If You Are Running WUBI (Ubuntu inside Windows)
    It is possible to chroot into a Wubi install by first mounting your Windows partition, and then mounting the Windows Wubi file, root.disk. The user must determine the value of the Window partition (sda1, sda2, etc). This section only applies to users booting an Ubuntu OS installed within Windows.

    Code:
    sudo mkdir /mnt/windows /mnt/temp  # Make the mount points
    sudo mount /dev/sdXY /mnt/windows  # Mount the Windows partition. Example: sudo mount /dev/sda1 /mnt/windows
    sudo mount -o loop /mnt/windows/ubuntu/disks/root.disk /mnt/temp  # Mount the root.disk file.
  3. If You Have a Normal Installation. No separate /boot partition, not a Windows/Wubi installation.
    Code:
    sudo mkdir /mnt/temp 
    sudo mount /dev/sdXY /mnt/temp  # Example: sudo mount /dev/sda5 /mnt/temp

Continue from A, B or C.
At this point the Ubuntu installation should be mounted on /mnt/temp. The next step is to mount certain items in preparation of the chroot. The “for i in …” is a command that will mount these items and must be run.

Code:
for i in /dev /dev/pts /proc /sys; do sudo mount -B $i /mnt/temp$i;  done
sudo cp /etc/resolv.conf /mnt/temp/etc/resolv.conf  # May be required to connect to the Internet.
sudo chroot /mnt/temp

If the commands ran successfully, the terminal prompt should include “root” ( root@ubuntu:/# ), which indicates you are in the chroot environment.

2. Confirm an Internet connection.*** This is necessary to retrieve the necessary Grub packages.

Code:

apt-get update   # ***
  • *** Unless an Internet connection can be established, do not run the Section 3 commands – the packages will not download and you will be left without a bootloader! For an alternative procedure, see “Reconfiguring Grub 2 without an Internet Connection” near the end of this post.
  • Internet failure means that your computer cannot connect to the Internet. If any repositories update you may safely continue to Section 3.

3. Purge Grub 2 packages. The next command will remove grubgrub-pc (Grub 2) and grub-common.* Here is what you will do:

  1. Press ENTER to continue.
  2. Read the warning during the install about removing the bootloader. TAB to highlight “<Yes>” and press ENTER.

* If you are sure you have never had Grub legacy on the current installation you may omit “grub” from the next command (apt-get purge grub-pc grub-common).

Code:
 
apt-get purge grub grub-pc grub-common

4. Reinstall the grub packages. Here is what will happen:

  1. You will be given the opportunity to add extra kernel options to the kernel line. If you don’t know, you probably don’t need them ; TAB to highlight “<OK>” and press ENTER.
  2. Read the installation notes. TAB to “<OK>” to continue.
  3. When presented with the device option, use the UP/DN keys to select the correct drive (sdX).
    • Make sure the installation drive [*] /dev/sdX has an asterisk next to it ( example: [*] /dev/sda ). If it doesn’t, highlight it and press the SPACE bar to select it.
    • Do not select a partition ( example: [ ] /dev/sda5 , etc).
  4. TAB to “<OK>” and press ENTER. When it has finishing the installation, you should have Grub 2 installed.
Code:
apt-get install grub-common grub-pc

5. Update the Grub 2 files . This command shouldn’t be necessary, but it won’t hurt to update Grub once more before exiting.

Code:
 
update-grub 

6. Exit the chroot environment.

Code:
exit

If you have successfully exited chroot, the terminal prompt should return to the Ubuntu Live CD prompt ( ubuntu@ubuntu:~$ )

7. Unmount what you previously mounted.*

Code:
for i in /dev/pts /dev /proc /sys; do sudo umount /mnt/temp$i ; done

* If you mounted a separate /boot partition, run this instead:

Code:
for i in /dev/pts /dev /proc /sys /boot; do sudo umount /mnt/temp$i ; done

8. Reboot your system.

Reconfiguring Grub 2 without an Internet Connection.

  1. Complete Sections 1 and 2 above. If the update-grub command fails to connect to the Internet after chrooting, use the procedures below while still in the chrootenvironment.

    *** Caution: Running the Step 3 commands above without an Internet connection will leave you without a bootloader!

  2. If you entered the chroot without copying the /etc/resolv.conf file, you can try to correct the Internet problem by doing so now. The following commands will exit chroot, copy the additional file, and then return to the chroot environment.
    Code:
    exit  
    sudo cp /etc/resolv.conf /mnt/temp/etc/resolv.conf
    sudo chroot /mnt/temp 
    update-grub

    If you can now connect to the Internet, return to Step 3. With no connection, run only the remaining commands below.

  3. Review the notes in Step 4 above, as you will be presented with several screens awaiting user input.
    Code:
    dpkg-reconfigure grub-pc
  4. Update the Grub 2 files . This command shouldn’t be necessary, but it won’t hurt to update Grub once more before exiting.
    Code:
     
    update-grub 
  5. Exit the chroot environment:
    Code:
    exit

    If you have successfully exited chroot, the terminal prompt should return to the Ubuntu Live CD prompt ( ubuntu@ubuntu:~$ )

  6. Unmount what you previously mounted.
    Code:
    for i in /dev/pts /dev /proc /sys ; do sudo umount /mnt/temp$i ; done
  7. Reboot your system.

* Explanation of the “for i in /dev /dev/pts /proc /sys; do sudo mount -B $i /mnt/temp$i; done
This command is the equivalent of:

Code:
sudo mount --bind /dev /mnt/temp/dev
sudo mount --bind /dev/pts /mnt/temp/dev/pts
sudo mount --bind /proc /mnt/temp/proc
sudo mount --bind /sys /mnt/temp/sys
__________________
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s