GRUB2

From ArchWiki

Jump to: navigation, search
Article summary
Covers various aspects of the next generation of the GRand Unified Bootloader (GRUB2).
Available in languages
English
Česky
Español
Français
Italiano
Русский
Related articles
GRUB
External links
GNU GRUB -- GNU Project
GNU GRUB Wiki

GNU GRUB is a Multiboot bootloader. It was derived from GRUB, the GRand Unified Bootloader, which was originally designed and implemented by Erich Stefan Boleyn. GRUB 2 is derived from PUPA which was a research project to investigate the next generation of GRUB. GRUB 2 has been rewritten from scratch to clean up everything for modularity and portability.

Briefly, the bootloader is the first software program that runs when a computer starts. It is responsible for loading and transferring control to the Linux kernel. The kernel, in turn, initializes the rest of the operating system.

Currently, GRUB (i.e. version 0.9x) is the de facto standard bootloader of Linux, and is expected to be superseded by GRUB2 in the near future. When this happens, "GRUB" will become "GRUB Legacy".

Contents

Preface

The next generation of the GRand Unified Bootloader (GRUB2) is still under development and therefore caution must be observed. Consider yourself warned! GRUB2 may not behave as expected: features may be missing; functionality may change. Without any specific reason to use GRUB2 instead of GRUB, users should consider installing the more stable GRUB instead.

Notes for current GRUB users

  • Commands have changed between GRUB and GRUB2. Familiarize yourself with GRUB2 commands before proceeding (e.g. "find" has been replaced with "search").
  • GRUB2 is now modular and no longer requires "stage 1.5". As a result, the bootloader itself is limited -- modules are loaded from the hard drive as needed to expand functionality (e.g. for LVM or RAID support).
  • Device naming has changed between GRUB and GRUB2. Partitions are numbered from 1 instead of 0 (drives are still numbered from 0). For example, /dev/sda1 would be referred to as (hd0,0) using GRUB and (hd0,1) using GRUB2.

Installation

The GRUB2 package can be installed with pacman (and will replace grub, if it is installed):

# pacman -S grub2

Additionally, GRUB2 must be installed to the boot sector of a drive or partition to serve as a bootloader. This is covered in the #Bootloader installation section.

Configuration

The configuration file is located at /boot/grub/grub.cfg (no longer menu.lst). Edit this file to suit your needs.

TODO: Mention grub-mkconfig script, which automatically generates a grub.cfg via helper scripts in /etc/grub.d.
  • (hdX,Y) is the partition Y on disk X, partition numbers starting at 1, disk numbers starting at 0
  • set default=N is the default boot entry that is chosen after timeout for user action
  • set timeout=M is the time M to wait in seconds for a user selection before default is booted
  • menuentry "title" {entry options} is a boot entry titled title
  • set root=(hdX,Y) sets the boot partition, where the kernel and GRUB modules are stored (boot need not be a separate partition, and may simply be a directory under the "root" partition (/)

An example configuration:

File: /boot/grub/grub.cfg
# Config file for GRUB2 - The GNU GRand Unified Bootloader
# /boot/grub/grub.cfg

# DEVICE NAME CONVERSIONS
#
#  Linux           Grub
# -------------------------
#  /dev/fd0        (fd0)
#  /dev/sda        (hd0)
#  /dev/sdb2       (hd1,2)
#  /dev/sda3       (hd0,3)
#

# Timeout for menu
set timeout=5

# Set default boot entry as Entry 0
set default=0

# (0) Arch Linux
menuentry "Arch Linux" {
set root=(hd0,1)
linux /vmlinuz26 root=/dev/sda3 ro
initrd /kernel26.img
}

## (1) Windows
#menuentry "Windows" {
#set root=(hd0,3)
#chainloader +1
#}

If you do not have a separate boot partition, /boot must prefix entries in grub.cfg. Example:

# (0) Arch Linux
menuentry "Arch Linux" {
set root=(hd0,1)
linux /boot/vmlinuz26 root=/dev/sda1 ro
initrd /boot/kernel26.img
}

LVM

If you use LVM for your /boot, add the following before menuentry lines:

insmod lvm

and specify your root in the menuentry as:

set root=(lvm_group_name-lvm_logical_boot_partition_name)

Example:

# (0) Arch Linux
menuentry "Arch Linux" {
insmod lvm
set root=(VolumeGroup-lv_boot)
linux /vmlinuz26 root=/dev/mapper/VolumeGroup-root ro
initrd /kernel26.img
}

Persistent block device naming

To list UUIDs, from a running system:

$ blkid

Replace the root line with the following:

linux /vmlinuz26 root=/dev/disk/by-uuid/<UUID> ro

However, you still have to set Grub2's notion of a root partition. In order to do that, use the search command, with something like:

search --fs-uuid <UUID> --set root

Putting everything together, a complete boot entry using no partition number would look like:

menuentry "Arch Linux" {
    search --fs-uuid 355ccb5c-99e1-400d-b612-451f9247e35e --set root
    linux /boot/vmlinuz26 root=/dev/disk/by-uuid/355ccb5c-99e1-400d-b612-451f9247e35e ro
    initrd /boot/kernel26.img
}

It is also possible to use labels, i.e. human-readable strings attached to filesystems, by using the --label option to search. First of all, label your existing partition:

# tune2fs -L archroot /dev/sda2

Then, add an entry using labels:

menuentry "Arch Linux, session texte" {
    search --label archroot --set root
    linux /boot/vmlinuz26 root=/dev/disk/by-label/archroot ro
    initrd /boot/kernel26.img
}

Dual-booting

These are the two most common ways of configuring the grub.cfg file. For more complex uses, feel free to add descriptions here.

With GNU/Linux

Assuming that the other distro is on partition sda2:

menuentry "Other Linux" {
set root=(hd0,2)
linux /boot/vmlinuz (add other options here as required)
initrd /boot/initrd.img (if the other kernel uses/needs one)
}

With Windows

Add this at the end of your /boot/grub/grub.cfg. This assumes that your Windows partition is sda3.

# (2) Windows XP
menuentry "Windows XP" {
set root=(hd0,3)
chainloader +1
}

With Windows via EasyBCD and NeoGRUB

Since EasyBCD's NeoGRUB currently does not understand the GRUB2 menu format, chainload to it by replacing the contents of your C:\NST\menu.lst file with lines similar to the following:

default 0
timeout 1
title       Chainload into GRUB v2
root        (hd0,7)
kernel      /boot/grub/core.img

Bootloader installation

GRUB2 may be installed from a live environment, or directly from a running Arch install.

In most cases, installing GRUB2 would is as easy as running the grub-install command as root:

# grub-install /dev/sda

where /dev/sda is the destination of the installation (in this case the MBR of the first SATA disk). If you use LVM for your /boot, you can install GRUB2 on multiple physical disks.

If this fails with the error:

grub-probe: error: Cannot get the real path of '/dev/fd0'
Auto-detection of a filesystem module failed.
Please specify the module with the option '--modules' explicitly.

Try adding --recheck to the arguments as follows:

# grub-install --recheck /dev/sda

Using the command shell

Since the MBR is too small to store all GRUB2 modules, only the menu and a few basic commands reside there. The majority of GRUB2 functionality remains in modules in /boot/grub, which are inserted as needed. In error conditions (e.g. if the partition layout changes) GRUB2 may fail to boot. When this happens, a command shell may appear.

GRUB2 offers multiple shells/prompts. If there is a problem reading the menu but the bootloader is able to find the disk, you will likely be dropped to the "normal" shell:

sh:grub>

If there is a more serious problem (e.g. GRUB cannot find required files), you may instead be dropped to the "rescue" shell:

grub rescue>

The rescue shell is a restricted subset of the normal shell, offering much less functionality. If dumped to the rescue shell, first try inserting the "normal" module, then starting the "normal" shell:

grub rescue> set prefix=(hdX,Y)/boot/grub
grub rescue> insmod (hdX,Y)/boot/grub/normal.mod
rescue:grub> normal
TODO: Describe how one might use the command shell.

Using the rescue console

See #Using the command shell first. If unable to activate the standard shell, one possible solution is to boot using a live CD or some other rescue disk to correct configuration errors and reinstall GRUB. However, such a boot disk is not always available (nor necessary); the rescue console is surprisingly robust.

The available commands in GRUB rescue include "insmod", "ls", "set", and "unset". This example uses "set" and "insmod". "set" modifies variables and "insmod" inserts new modules to add functionality.

Before starting, the user must know the location of their /boot partition (be it a separate partition, or a subdirectory under their root):

grub rescue> set prefix=(hdX,Y)/boot/grub

where X is the physical drive number and Y is the partition number.

To expand console capabilities, insert the "linux" module:

grub rescue> insmod (hdX,Y)/boot/grub/linux.mod
Note: With a separate boot partition, omit /boot from the path, (i.e. type set prefix=(hdX,Y)/grub and insmod (hdX,Y)/grub/linux.mod).

This introduces the "linux" and "initrd" commands, which should be familiar (see #Configuration).

An example, booting Arch Linux:

set root=(hd0,5)
linux /boot/vmlinuz26 root=/dev/sda5
initrd /boot/kernel26.img
boot

With a separate boot partition, again change the lines accordingly:

set root=(hd0,5)
linux /vmlinuz26 root=/dev/sda6
initrd /kernel26.img
boot

After successfully booting the Arch Linux installation, users can correct grub.cfg as needed and then run:

# grub-install /dev/sda

to reinstall GRUB2 and fix the problem completely, changing /dev/sda if needed. See #Bootloader installation for details.

Tips and tricks

Installing GRUB2 during Arch Linux installation

You need the network configured first.

Skip the Install Bootloader step and exit. Then:

# mount -o bind /dev /mnt/dev
# chroot /mnt bash

Install the GRUB2 package and follow the #Bootloader installation section above.

Background image and bitmap fonts

GRUB2 comes with support for background images and bitmap fonts in pf2 format. The unifont font is included in the grub2 package under the filename unicode.pf2, or, as only ascii characters under the name ascii.pf2. Image formats supported include tga, png and jpeg, providing the correct modules are loaded. The maximum supported resolution depends on your hardware. A configuration sample setting a tga file as background is shown below.

if loadfont /usr/share/grub/unicode.pf2 ; then
  set gfxmode="1024x768x32"
  insmod gfxterm
  insmod vbe
  terminal_output gfxterm
  if terminal_output gfxterm; then true ; else
     terminal gfxterm
  fi
fi
insmod tga
background_image /boot/grub/archlinux.tga

Menu colors

To change the colors in GRUB2 you would specify one option in /boot/grub/grub.cfg:

set menu_color_normal=light-blue/black
set menu_color_highlight=light-cyan/blue

These are the default colors for Arch's release of GRUB-legacy. The available colors for GRUB2 are at http://www.gnu.org/software/grub/manual/html_node/color.html.

Hidden menu

For hiding menu put that code in grub.cfg after picture initialization but before menuentries (e.g. background_image /boot/grub/archlinux.tga).

  set timeout=5
  echo -n "Press ESC to see the menu... "
if sleep --verbose --interruptible 5 ; then
  set timeout=0
fi

Setting the framebuffer resolution

To change the framebuffer resolution in grub2, add a line similar to this to the linux line in grub.cfg:

video=vesafb:mode=1024x768-32 vga=790

In the preceeding statement, the format mode=<resolution>-<colordepth> vga=<fbresolution> is used where fbresolution follows the following scheme:

+-------------------------------------------------+
     | 640x480    800x600    1024x768   1280x1024
 ----+--------------------------------------------
 256 | 0x301=769  0x303=771  0x305=773   0x307=775
 32K | 0x310=784  0x313=787  0x316=790   0x319=793
 64K | 0x311=785  0x314=788  0x317=791   0x31A=794
 16M | 0x312=786  0x315=789  0x318=792   0x31B=795
+-------------------------------------------------+

Make sure you add the following somewhere, (insmod statements are usually found at the top of the grub.cfg file):

insmod vbe

Troubleshooting

Any troubleshooting should be added here.

msdos-style error message

grub-setup: warn: This msdos-style partition label has no post-MBR gap; embedding won't be possible!
grub-setup: warn: Embedding is not possible. GRUB can only be installed in this setup by using blocklists.
            However, blocklists are UNRELIABLE and its use is discouraged.
grub-setup: error: If you really want blocklists, use --force.

This error may occur when you try installing GRUB2 in a VMware container. Read more about it here. Hopefully a fix will be provided soon.

It also happens when the first partition starts just after the MBR, without the usual space of 60-something block before the first partition.

Other

I couldn't figure out how to uninstall grub1, and install grub2 to the MBR, as it isn't being booted by default. It is still booting grub1. So, an easy work-around, is rename menu.lst.pacsave or whatever, to menu.lst (in /boot/grub/) and for each menu entry that you would like to use grub2, at the end type "chainloader +1". This will tell grub1 to forward control to grub2. This is an ugly hack though, so I advise setting the menu.lst's timout as 0, otherwise the total timeout would be grub1's time out + grub2's which, for me would equal more than 18 seconds, which is quite a bit.

P.S. hopefully someone figures out how to pry grub1's dead fingers off of my MBR, and place grub2 on it :) .

In my case it had to do with my boot partition. Say boot-partition is (hd0,1) and your root is (hd0,3) (grub2 naming). grub-setup searches for (hd0,3)/boot/grub/core.img. Just because it's on (hd0,1)/grub/core.img, it is unable to find it. So I copied the grub-folder to my root partition and everything worked fine:

E.g. (as root:)

# mount /boot
# cp -a /boot/grub /
# umount /boot
# mv /grub /boot/
# grub-install /dev/sda
Personal tools