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.
- Boot to the LiveCD Desktop (Ubuntu 9.10 or later).
-
Open a terminal by selecting Applications, Accessories, Terminal from the menu bar.
-
Determine the partition with the Ubuntu installation. The fdisk option "-l" is a lowercase "L".
-
sudo fdisk -l
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.
-
- 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.
-
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
- 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.
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.
- Boot to the LiveCD Desktop (Ubuntu 9.10 or later).
- Mount the partition with your Ubuntu installation.
-
From the Places menu, select and click the partition containing your Ubuntu installation to mount it.
-
- 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
-
-
-
Open a terminal by selecting Applications, Accessories, Terminal from the menu bar.
-
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.
-
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).
- If the location window is not in the format shown, click on the icon to the left to change the presentation.
-
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.
-
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
sudo grub-setup -d /media/7848138a-41a0-4eba-8aed-d1b625ac8759/boot/grub /dev/sda
-
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
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
-
Other switches are available for use with the grub-setup command. In a terminal, type grub-setup --help for a list of available options.
-
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.
-
- 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.
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.
-
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).
-
Open a terminal - Applications, Accessories, Terminal.
- 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.
- If you aren't sure, run
- Mount your normal system partition:
- Substitute the correct partition: sda1, sdb5, etc.
sudo mount /dev/sdXX /mnt # Example: sudo mount /dev/sda1 /mnt
-
Only if you have a separate boot partition:
- sdYY is the /boot partition designation (for example sdb3)
-
sudo mount /dev/sdYY /mnt/boot
- 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 - Chroot into your normal system device:
sudo chroot /mnt
- If there is no /boot/grub/grub.cfg or it's not correct, create one using
update-grub
- Reinstall GRUB 2:
-
Substitute the correct device - sda, sdb, etc. Do not specify a partition number.
grub-install /dev/sdX
-
-
Verify the install (use the correct device, for example sda. Do not specify a partition): sudo grub-install --recheck /dev/sdX
-
Exit chroot: CTRL-D on keyboard
- 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
- If you mounted a separate /boot partition:
- Unmount the LiveCD's /usr directory:
sudo umount /mnt/usr
- Unmount last device:
sudo umount /mnt
- 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
-
grub-probe -t device /boot/grub
To install GRUB 2 to the sdX partition's MBR (sda, sdb, etc.)
-
sudo grub-install /dev/sdX
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.
No comments:
Post a Comment