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.
If you cannot boot from GRUB 2 review the section Rescue Mode ("grub rescue>") Booting.
Methods of Reinstalling
There are various methods; some require booting from a LiveCD. The simplest method is presented first. If the first method does not work, follow the second or subsequent methods, which are more complex and contain more options and instructions.
As GRUB 2 has changed with each release, please try to use a LiveCD that has the same version of Grub2 as the installed version. However, since this is a reinstall it 'should' be possible to use older (or newer) versions too.
Use Boot-Repair Graphical Tool
To reinstall GRUB, just use the following graphical tool : Boot-Repair.
Boot-repair can be used either from a live-CD or from a normal session.
http://pix.toile-libre.org/upload/original/1299426403.png
Copy LiveCD Files
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. If for example Windows is on sda1 and Ubuntu is on sda5, and Windows has overwritten the MBR, then the target for grub installation will be /dev/sda5, and the MBR in the boot sector of sda will be re written for grub.
This operation will write to the MBR and restore the modules and core.img to /boot/grub. It will not replace or restore grub.cfg or fix corrupted files.
1. Boot the LiveCD Desktop.
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
In Grub 1.99, introduced with Ubuntu 11.04, Natty Narwhal, a new switch is available which more clearly defines where the grub folder is placed. The command above will still work with Grub 1.99, but the following command is preferred by the developers. The target directory in the command is the command into which the grub folder will be installed. By default, and without the switch, the location is /boot/grub. In these instructions, since the Ubuntu partition is mounted on /mnt, the target would be /mnt/boot/grub.
*
sudo grub-install --boot-directory=/mnt/boot /dev/sdX
Example: sudo grub-install --boot-directory=/mnt /dev/sda
* Reboot
*
Refresh the GRUB 2 menu with sudo update-grub
*
If the user wishes to explore why the system failed, refer to Post-Restoration Commands section below.
Copy Partition Files
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.
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-arrow to select and CTRL-C 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.
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. The CD should be the same release and architecture (32/64 bit).
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
sudo mount /dev/sdYY /mnt/boot
* Example: sudo mount /dev/sdb6 /mnt/boot
6. Mount the critical virtual filesystems. Run the following as a single command:
for i in /dev /dev/pts /proc /sys; do sudo mount -B $i /mnt$i; done
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):
grub-install --recheck /dev/sdX
11.
Exit chroot: CTRL-D on keyboard
12. Unmount virtual filesystems. Run the following as a single command:
for i in /sys /proc /dev/pts /dev; do sudo umount /mnt$i; done
13. If you mounted a separate /boot partition:
*
sudo umount /mnt/boot
14. Unmount the LiveCD's /usr directory:
sudo umount /mnt/usr
15. Unmount last device:
sudo umount /mnt
16. Reboot.
sudo reboot
Purge & Reinstall
This procedure also 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.
If certain GRUB 2 files become corrupted or are removed by the user, a simple reinstall of GRUB 2 may not return the system to a bootable condition. To completely restore the GRUB 2 filesystem, it may be necessary to completely remove and reinstall the grub-pc and grub-common packages (and grub-gfxpayload-lists in Grub 1.99/Natty).
important.png During this operation the user will be temporarily left with no bootloader. Ensure you have a working Internet connection and access to the repositories before purging the GRUB 2 packages.
1. Boot to the LiveCD Desktop. The CD should be the same release and architecture (32/64 bit).
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
sudo mount /dev/sdYY /mnt/boot
*
Example: sudo mount /dev/sda6 /mnt/boot
6. Mount the critical virtual filesystems:
for i in /dev /dev/pts /proc /sys; do sudo mount -B $i /mnt$i; done
7. Chroot into your normal system device:
chroot /mnt
8. You should now be at a "root" prompt. The use of "sudo" is no longer required. Ensure you have a working Internet connection and access to the repositories. If you do not, STOP!
apt-get update
9.
Purge grub-pc and grub-common (and grub-gfxpayload-lists in Grub 1.99/Natty). You will be warned you are removing the bootloader. TAB to OK.
apt-get purge grub-common
10. Reinstall GRUB 2. You will be given the opportunity to add kernel options. TAB to OK. Next you will be given the opportunity to select the devices/drives on which to install GRUB 2. Select the appropriate drive(s) with the SPACEBAR. Normally you do NOT want to select any partitions. TAB to OK after selecting the drive(s), then press ENTER.
apt-get install grub-pc
11.
Exit chroot:
exit
* You should be back at your normal command prompt.
12. Unmount virtual filesystems*:
for i in /sys /proc /dev/pts /dev; do sudo umount /mnt$i; done
13. Unmount the Ubuntu partition:
sudo umount /dev/sdXY
14. 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
o
sudo update-grub
To look for the bootloader location.
o
grub-probe -t device /boot/grub
To install GRUB 2 to the sdX partition's MBR (sda, sdb, etc.)
o
sudo grub-install /dev/sdX
To recheck the installation. (sda, sdb, etc.)
o
sudo grub-install --recheck /dev/sdX
Purge & Reinstall
This procedure also 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.
If certain GRUB 2 files become corrupted or are removed by the user, a simple reinstall of GRUB 2 may not return the system to a bootable condition. To completely restore the GRUB 2 filesystem, it may be necessary to completely remove and reinstall the grub-pc and grub-common packages (and grub-gfxpayload-lists in Grub 1.99/Natty).
important.png During this operation the user will be temporarily left with no bootloader. Ensure you have a working Internet connection and access to the repositories before purging the GRUB 2 packages.
1. Boot to the LiveCD Desktop. The CD should be the same release and architecture (32/64 bit).
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
sudo mount /dev/sdYY /mnt/boot
*
Example: sudo mount /dev/sda6 /mnt/boot
6. Mount the critical virtual filesystems:
for i in /dev /dev/pts /proc /sys; do sudo mount -B $i /mnt$i; done
7. Chroot into your normal system device:
chroot /mnt
8. You should now be at a "root" prompt. The use of "sudo" is no longer required. Ensure you have a working Internet connection and access to the repositories. If you do not, STOP!
apt-get update
9.
Purge grub-pc and grub-common (and grub-gfxpayload-lists in Grub 1.99/Natty). You will be warned you are removing the bootloader. TAB to OK.
apt-get purge grub-common
10. Reinstall GRUB 2. You will be given the opportunity to add kernel options. TAB to OK. Next you will be given the opportunity to select the devices/drives on which to install GRUB 2. Select the appropriate drive(s) with the SPACEBAR. Normally you do NOT want to select any partitions. TAB to OK after selecting the drive(s), then press ENTER.
apt-get install grub-pc
11.
Exit chroot:
exit
* You should be back at your normal command prompt.
12. Unmount virtual filesystems*:
for i in /sys /proc /dev/pts /dev; do sudo umount /mnt$i; done
13. Unmount the Ubuntu partition:
sudo umount /dev/sdXY
14. 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
o
sudo update-grub
To look for the bootloader location.
o
grub-probe -t device /boot/grub
To install GRUB 2 to the sdX partition's MBR (sda, sdb, etc.)
o
sudo grub-install /dev/sdX
To recheck the installation. (sda, sdb, etc.)
o
sudo grub-install --recheck /dev/sdX