This page details the procedures for installing and reinstalling GRUB 2. The information applies to most versions of GRUB 2, but is specifically written for version 1.99. GRUB 1.99 is included on Ubuntu releases 11.04 (Natty Narwhal) and later. For more information on a variety of GRUB 2 topics, please visit the GRUB2 main page. For information on converting to or from GRUB Legacy, please view the Grub2/Upgrading community doc.

The GRUB 2 bootloader is included on all currently-supported versions of the Ubuntu family. GRUB 2 can accomodate traditional computer firmware such as BIOS as well as the newer EFI/UEFI standards. It is compatible with MBR, GPT and other partitioning tables.

All references to GRUB pertain to GRUB 2. Where GRUB 0.97 is discussed, it will be referred to by version number or as GRUB Legacy.

GRUB 2 Initial Installation

GRUB 2 installation is automatically incorporated into a normal Ubuntu installation. Input required by the user for the initial setup is very simple. If the installation will use the entire disk no specific GRUB selections are necessary. If the user wants to use a specific partition for Ubuntu the user must select the Ubuntu partition and the drive on which to embed the bootloader information.

The main GRUB 2 folder locations include /boot/grub/ and /etc/grub.d/. User preferences are stored in the /etc/default/grub file. The GRUB 2 menu is built by information contained in /boot/grub/grub.cfg.

Installation Options (LiveCD)

Using the Entire Drive ( "Erase disk and install Ubuntu" ):

Install on entire drive: The user should select a drive on which Ubuntu and GRUB 2 will be installed. GRUB 2 will overwrite the drive's boot information and place the GRUB 2 files in the appropriate locations.

Installing Ubuntu to a Specific Partition ("Something Else"):

When using the "Something Else" option, you will be offered to choose the "Device for bootloader installation". Please select: * either the disk (eg /dev/sdX, not /dev/sdXY) on which the BIOS is setup to boot (recommended for normal use) * OR the partition (eg /dev/sdXY, not /dev/sdX) on which Ubuntu (/boot, else /) will be installed (only if you want to chainload it from another bootloader; if any doubt, do NOT choose this)

important.png Never choose any other partition! (this may break the boot of your other systems, see Bug #1049549).

g2_installing_partition.png

info.png On a system with multiple drives and OS's, the user can preserve the original bootloader by installing GRUB 2 on another drive. To accomplish this:

BIOS/MBR Notes

GRUB 2 normally writes its bootloader information to an area just beyond the Master Boot Record and before the first partition. Some software on non-Linux operating systems also used this embedded area and broke the system by overwriting the GRUB 2 information on dual-boot systems. GRUB 2 now can recognize this potential problem and avoid it.

The bootloader information does not have to be installed to the embedded area of the MBR. It may also bypass the MBR entirely and be installed to a specific partition. In doing this, the location of the GRUB 2 files are specified by using blocklists. This option is not available via an Ubuntu GUI installation but can be made via the terminal after installation. Even then this method is not as reliable as writing to the MBR and is not recommended by the GRUB developers.

BIOS/GPT Notes

If the BIOS is setup to boot the disk in Legacy/mbr mode, installing GRUB2 on a GPT (GUID Partition Table) disk requires a dedicated BIOS boot partition with a recommended size of at least 1 MiB. This partition can be created via GParted or other partitioning tools, or via the command line. It must be identified with a bios_grub flag. The necessary GPT modules are automatically included during installation when GRUB 2 detects a GPT scheme.

UEFI

GRUB 2 is compatible with EFI/UEFI and will load the necessary modules during installation. For information on this topic, please visit the UEFI community documentation.

Post-Installation

At the conclusion of a normal installation using the default entries:

To view the GRUB 2 file system and configuration options please refer to Grub 2 community documentation page.

Reinstalling GRUB 2

The user may wish to reinstall GRUB 2 for a variety of reasons, including:

When using the grub-install command, the boot information is updated and written to the designated drive, missing - but not corrupted or intentionally deleted - files are restored. Specifically the core.img, grubenv, and device.map are updated and missing modules restored. If missing, the grub folder will be recreated. The grub-install command does not generate a new GRUB 2 menu (grub.cfg).

Reinstalling GRUB 2 from a Working System

If Ubuntu is operating normally, boot into the working installation and run the following command from a terminal.

This will rewrite the MBR information to point to the current installation and rewrite some GRUB 2 files (which are already working). Since it isn't done during execution of the previous command, running sudo update-grub after the install will ensure GRUB 2's menu is up-to-date.

Fixing a Broken System

If a system has become unbootable due to a GRUB 2 failure or corrupted MBR/boot sector, there are various methods for reinstalling GRUB 2. The simplest is listed first, followed by more complicated methods.

via Boot-Repair Graphical Tool

Boot-Repair is a GUI application which can fix a variety of GRUB 2 problems. It can be used either from a LiveCD, its own CD, or during a normal Ubuntu session. In addition to providing a simple user interface, Boot-Repair also includes the ability to run a script to gather details of the system which can be used for troubleshooting help on various forums and IRC channels.

Please refer to the Boot-Repair community documentation for instructions on how to install and run this application.

via GRUB2 Rescue mode

GRUB 2 provides a rescue mode which allows the user to attempt to repair GRUB 2 from a special GRUB terminal during boot. Refer to the Grub2 Command_Line_and_Rescue_Mode section for information on GRUB terminal operations.

via the LiveCD terminal

This method uses the terminal from a LiveCD. The user must know the device name/partition of the installed system (sda1, sdb5, etc), which is then mounted from the LiveCD. GRUB 2 files are copied from the LiveCD libraries to the proper locations and MBR.

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. Boot the LiveCD, select "Try It", and open a terminal.

info.png When using a LiveCD, due to GRUB 2 changes between Ubuntu releases, it is recommended that the user boots a LiveCD of the same release (11.10, 12.04, etc) as the release to be repaired. If the user has installed a different version of GRUB 2, use a LiveCD with the same GRUB 2 version.

If necessary, use the fdisk command to help determine the partition on which Ubuntu is installed. The fdisk option "-l" is a lowercase "L". 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 '

In the following commands:

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

via Partition Files Copy

This method does not work if GRUB has not already been installed, or if GRUB files have been deleted.

important.png This method talks about 9.10, so it may be obsolete.

It consists in locating and mounting the problem boot partition 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 apartition 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 Disk Utility, introduced in Ubuntu 9.10, can easily assign labels to existing partitions. Access Disk Utility from the System, Administration menu or DASH.

via 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 64bit (not 32bit) if the system to fix is 64 bit.
  2. Open a terminal. The method varies depending on the release.
  3. Assuming the normal system partition(s) are on a software RAID (otherwise skip this step): make sure the mdadm tools are installed in the Live CD environment (e.g. by executing sudo apt-get install mdadm). Then assemble the arrays:

    sudo mdadm --assemble --scan
  4. If using LVM:

    sudo sh -ec "apt-get install lvm2; vgchange -ay"
  5. If using bcache:

    sudo sh -ec "apt-get install software-properties-common; add-apt-repository ppa:g2p/storage; apt-get update; apt-get install bcache-tools"
  6. Determine your normal system partition. The following commands may be helpful. The fdisk switch is a lowercase "L".

    sudo fdisk -l
    sudo blkid
    df -Th
  7. Mount your normal system partition. X is the drive letter. Y is the partition number:

    • Substitute the correct partition: sda1, sdb5, etc.
    sudo mount /dev/sdXY /mnt
    • Example 1: sudo mount /dev/sda1 /mnt

    • Example 2: sudo mount /dev/md1 /mnt

  8. Only if you have a separate boot partition (where sdXY is the /boot partition designation):

    sudo mount /dev/sdXY /mnt/boot
    • Example 1: sudo mount /dev/sdb6 /mnt/boot

    • Example 2: sudo mount /dev/md0 /mnt/boot

  9. Only if (some) of the system partitions are on a software RAID (otherwise skip this step): make sure the output of mdadm --examine --scan agrees with the array definitions in /etc/mdadm/mdadm.conf.

  10. Mount the critical virtual filesystems. Run the following as a single command:
    for i in /dev /dev/pts /proc /sys /run; do sudo mount -B $i /mnt/$i; done
  11. Chroot into your normal system device:
    sudo chroot /mnt
  12. Reinstall GRUB 2 (substitute the correct device with sda, sdb, etc. Do not specify a partition number):

    grub-install /dev/sdX

    If the system partitions are on a software RAID install GRUB 2 on all disks in the RAID. Example (software RAID using /dev/sda and /dev/sdb):

    grub-install /dev/sda
    grub-install /dev/sdb
  13. Recreate the GRUB 2 menu file (grub.cfg)

    update-grub
  14. Exit chroot: CTRL-D on keyboard

  1. Reboot.
    • sudo reboot

Boot repair after a Windows Upgrade on Ubuntu 14.04 (non-RAID)

Boot repair may be required when updating Windows (e.g. Windows 8 to Windows 8.1). This will be required for example if the computer has been configured such that Windows/Ubuntu is selected by entering the bios and changing the boot mode between "secure boot"/UEFI and CMS respectively. To manually repair grub (http://howtoubuntu.org/how-to-repair-restore-reinstall-grub-2-with-a-ubuntu-live-cd / http://askubuntu.com/questions/88384/how-can-i-repair-grub-how-to-get-ubuntu-back-after-installing-windows );

Purging & Reinstalling GRUB 2

The difference between using the grub-install command and purging & reinstalling GRUB 2 is that the latter completely removes all GRUB 2 files and system settings. Purging GRUB 2 is also a method to reset all configuration settings to the default values on a working system. This may correct GRUB 2 failures due to corrupted files and improper configurations as well as for missing folders/files deleted by the user. The GRUB 2 packages removed and reinstalled via this procedure include grub-pc, grub-common and, in Grub 1.99 and later, grub-gfxpayload-lists.

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.

This procedure can be accomplished very simply via Boot-Repair, or via the terminal procedure below.

Remark: the Boot-Repair procedure will also remove custom files that may block the boot. It is also able to purge and reinstall GRUB2 for UEFI / LVM / RAID systems.

via Boot-Repair Graphical Tool

* Run Boot-Repair either from liveCD or from your installed session * Click "Advanced options" --> "GRUB options" tab * Tick the "Purge GRUB and reinstall it" option --> Apply

via Terminal Commands

The commands from a working Ubuntu installation or once within the ChRoot of a LiveCD are the same.

  1. If using the LiveCD, complete the steps up to and including the sudo chroot command (Step 9) in the previous section.

Run the following commands as root. If not in a ChRoot environment, preface each command with sudo.

  1. Update the repositories to get a list of the most recent packages available. Ensure you have a working Internet connection and access to the repositories. If you do not, STOP!
    apt-get update
  2. The following command will completely remove grub-pc and grub-common. In GRUB 1.99 and later, grub-gfxpayload-lists will also be removed.

    • You will be warned you are removing the bootloader. TAB to OK and press ENTER.

    apt-get purge grub-common
  3. Reinstall GRUB 2. When you run the following command:
    • You will be given the opportunity to add kernel options. TAB to OK. 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
  4. If using the ChRoot procedure, Exit chroot then continue the ChRoot procedure at Step 13:

Post-Restoration Commands

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

Moving GRUB 2

The command to change the GRUB 2 installation device or boot files is grub-install run as root. This command allows the user to modify the installation by setting the ROOT directory, preload modules, run specific setup files and more. When executed, grub-install may run one or more other commands, such as grub-probe, grub-mkimage, and grub-setup. Here are some considerations when running grub-install:

GRUB Legacy Upgrades and Downgrades

For information on how to switch from GRUB Legacy to GRUB 2, or from GRUB 2 to GRUB Legacy, please refer to the Grub2/Upgrading community documentation.


Related Links

Grub2

Grub2/Submenus

Grub2/Upgrading

Boot-Repair

GNU GRUB Manual


CategoryBootAndPartition CategoryBootAndPartition

Grub2/Installing (last edited 2021-12-22 04:59:36 by sambrightman)