GRUB

From ArchWiki
Revision as of 22:29, 17 February 2011 by NTia89 (Talk | contribs) (Bootloader Installation for BIOS systems)

Jump to: navigation, search

This template has only maintenance purposes. For linking to local translations please use interlanguage links, see Help:i18n#Interlanguage links.


Local languages: Català – Dansk – English – Español – Esperanto – Hrvatski – Indonesia – Italiano – Lietuviškai – Magyar – Nederlands – Norsk Bokmål – Polski – Português – Slovenský – Česky – Ελληνικά – Български – Русский – Српски – Українська – עברית – العربية – ไทย – 日本語 – 正體中文 – 简体中文 – 한국어


External languages (all articles in these languages should be moved to the external wiki): Deutsch – Français – Română – Suomi – Svenska – Tiếng Việt – Türkçe – فارسی

Summary help replacing me
Covers various aspects of the next generation of the GRand Unified Bootloader (GRUB2).
Overview
Template:Boot process overview
Related
Burg - Burg is a brand-new boot loader based on GRUB2. It uses a new object format which allows it to be built in a wider range of OS, including Linux/Windows/OSX/Solaris/FreeBSD, etc. It also has a highly configurable menu system which works in both text and graphic mode.
Resources
GNU GRUB -- GNU Project
GNU GRUB Wiki

GRUB2 is the next generation of the GRand Unified Bootloader (GRUB). GRUB2 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 and provide modularity and portability [1].

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". Even though Grub2 has been worked on since February 2005 and Grub (version 0.9x) is referred to as grub-legacy in some places over the internet, this is *not* yet the case. The new Grub2 still isn't fully implemented.

Preface

GRUB2 is still under development and therefore caution should be observed. Consider yourself warned! GRUB2 may not behave as expected, features may be missing, and functionality may change. Without any specific reason to use GRUB2, users should consider installing the more stable GRUB instead.

Notes for current GRUB users

  • There are differences in the commands of 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 while drives are still numbered from 0. For example, Template:Filename would be referred to as Template:Codeline using GRUB2.

Installation

Installing GRUB2 during Arch Linux installation

  • Skip the Install Bootloader step and exit the installer.
  • Load module (for grub2 >= 1.99~rc1)
# modprobe md-mod
  • Configure the network:
# aif -p partial-configure-network
  • If you did not configure the installed system's /etc/resolv.conf file during installation (for instance, if you plan to let DHCP generate it later), you will need to copy the one generated by AIF when it configured the network:
# cp /etc/resolv.conf /mnt/etc/resolv.conf
  • From the installer's live shell, chroot to the installed system:
# mount -o bind /dev /mnt/dev
# chroot /mnt bash
  • Install the GRUB2 package as mentioned below.

Bootloader Installation for BIOS systems

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

For grub2 upto 1.98

# pacman -S grub2

For grub2 >=1.99

# pacman -S grub2-bios

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.

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

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

For grub2 upto 1.98

# grub-install --no-floppy /dev/sda

For grub2 >= 1.99 (including bzr trunk)

# sudo modprobe dm-mod (this should have to do before, as mentioned over)
# sudo grub_bios-install --boot-directory=/boot --no-floppy --recheck /dev/sda
# sudo cp /usr/share/grub/{unicode.pf2,ascii.pf2} /boot/grub/

To generate a core.img file alone without setting up grub2 in the MBR, add --grub-setup=/bin/true to grub2_bios-install -

# sudo grub_bios-install --grub-setup=/bin/true --boot-directory=/boot --no-floppy --recheck /dev/sda

You can then chainload grub2's core.img from grub-legacy as a linux kernel or a multiboot kernel.

The --no-floppy tells to not search for floppy devices which will vastly improve the command's overall execution time on many systems (it will also prevent the issue below from occuring) and where Template:Filename is the destination of the installation (in this case the MBR of the first SATA disk). If you use LVM for your Template:Filename, you can install GRUB2 on multiple physical disks.

Executing grub_bios-install without the --no-floppy flag can also lead to this when there is a problem detecting the path of the floppy device:

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.

Thus it is recommended to use the --no-floppy switch, if you don't want to have your MBR on a floppy. If you do, you also want to set floppy as the first boot device in BIOS.

GPT specific instructions

GRUB2 in BIOS-GPT configuration requires a BIOS Boot Partition to embed its core.img in the absence of 32 KN post MBR gap in GPT partitioned systems.

Create a 1 MB (approx) partition using GPT fdisk or GNU Parted with no filesystem. The location of the partition does not matter but it is advisable to put it somewhere in the beginning of the disk before the /boot partition. Set the partition type to "EF02" in GPT fdisk or set "bios_grub" flag on in GNU Parted or GParted.

This partition is used by GRUB2 only in BIOS-GPT setups. No such partition type exists in case of MBR partitioning (at least not for GRUB2). This partition is also not required if the system is UEFI based, as no embedding takes place in that case. Neither GRUB-legacy nor SYSLINUX require this partition.

Note: This partition should be created before grub2_bios-install or grub-setup is run or before the Install Bootloader step of the Archlinux installer (if GRUB2 is selected as bootloader).

Bootloader Installation for UEFI systems

Note: Unless specified as EFI 1.x , EFI and UEFI terms are used interchangeably to denote UEFI 2.x firmware. Also unless stated explicitely, the instructions are general and not Mac specific. Some of them may not work or may be different in Macs. Apple's EFI implementation is neither a EFI 1.1 version nor UEFI 2.x version but includes features of both. This kind of firmware does not fall under any one UEFI version so it is not a standard EFI firmware.

GRUB2 UEFI bootloader is available in Arch Linux only from version 1.99~rc1 . To install, first Detect which UEFI firmware arch you have (i386 or x86_64).

Depending on that, install the appropriate package

For 64-bit aka x86_64 UEFI firmware

# pacman -S grub2-efi-x86_64

For 32-bit aka i386 UEFI firmware

# pacman -S grub2-efi-i386

Mount the EFI SYSTEM PARTITION in your system at /boot/efi . It should be FAT32 formatted and should be at least 200 MB in size. If you have mounted the EFISYS partition in some other mountpoint, replace /boot/efi in the below commands with that mountpoint.

# sudo mkdir -p /boot/efi/efi  # If the directory does not exist

# sudo modprobe dm-mod

The below commands assume you are using grub2-efi-x86_64 (for grub2-efi-i386, replace all x86_64 with i386 in the commands)

# sudo grub_efi_x86_64-install --boot-directory=/boot/efi/efi --bootloader-id=grub --no-floppy --recheck

If you notice carefully, there is no <device_path> option (Eg:- /dev/sda) at the end of the grub_efi_x86_64-install command unlike the case of setting up grub2 for BIOS systems. Any <device_path> provided will be ignored by the install script as UEFI bootloaders do not use MBR or Partition boot sectors at all.

Then copy the font files required to enable UEFI video mode (GOP mostly) in grub2 (otherwise no kernel boot messages will be seen in the console)

# sudo cp /usr/share/grub/{unicode.pf2,ascii.pf2} /boot/efi/efi/grub/

The grub_efi_x86_64-install automatically generates a "grub.efi" while setting up the /boot/efi/efi/grub/ directory.

If you want, you can create a custom grub2.efi app using :-

# sudo grub-mkimage -d /usr/lib/grub/x86_64-efi -O x86_64-efi -p "" -o /boot/efi/efi/grub/grub2.efi <Your_List_of_GRUB2_Modules>

Note : The -p "" option is important for creating a portable grub2.efi app.

There is no file-size restriction on grub2.efi either due to GRUB2 or due to the UEFI firmware. Therefore you can include any number of modules you want.

A "grub.cfg" created for BIOS based GRUB2 will be sufficient for the UEFI GRUB2 as long as all the paths in the config are absolute paths. The "grub.efi" uefi application can be launched using the firmware's "Boot Manager", "Boot from file" option or using the UEFI Shell.

Alternatively, you can also setup /boot/efi/efi/grub directory by copying all the files in /usr/lib/grub/x86_64-efi to /boot/efi/efi/grub and using the above grub-mkimage command to create a grub2.efi application. Just make sure you have the required modules embedded in grub2.efi to enable it to access the EFI SYSTEM PARTITION. A basic grub2.efi can be generated using the following command -

# sudo grub-mkimage -d /usr/lib/grub/x86_64-efi -O x86_64-efi -p "" -o /boot/efi/efi/grub/grub2.efi part_gpt fat iso9660 udf normal chain linux \
ls search search_fs_file search_fs_uuid search_label help boot configfile echo efi_gop

In the /boot/efi/efi/grub/grub.cfg file, add the following lines to enable grub2 to pass the video mode correctly to the kernel, without which you will only get a black screen (no output) but booting (actually) proceeds successfully without any system hang.

 insmod efi_gop
 insmod font

 if loadfont ${prefix}/unicode.pf2
 then
     insmod gfxterm
     set gfxmode=auto
     set gfxpayload=keep
     terminal_output gfxterm
 fi

As you can see for gfxterm (graphical terminal) to function properly, unicode.pf2 font file should exists in /boot/efi/efi/grub/ dir.

Manual Compilation

For BIOS Systems

GRUB2 for BIOS systems should be compiled as follows

./autogen.sh

./configure --with-platform=pc --prefix=/usr

make

make install

The --with-platform=pc generates grub2 for bios alone irrespective of the firmware of the build system.

For UEFI Systems

First Detect which UEFI Firmware arch you have and then follow the compile instructions below -

./autogen.sh

./configure --with-platform=efi --target=TARGET_EFI_ARCH --prefix=/usr

make

make install

The "--target" option denotes the UEFI firmware arch. for which grub2 should be compiled, not the architecture of the linux kernel grub2 may boot.

It is possible to use UEFI 64-bit firmware + GRUB2 as x86_64-EFI app loading a i686 linux kernel, as long as the kernel does not try to access UEFI Runtime Services. Vice-versa situation is also possible. But a x86_64 UEFI firmware cannot launch GRUB2 i386-efi app (unlike x86_64 Operating Systems), and a i386 UEFI firmware will not launch GRUB2 x86_64-efi app. It is important to compile GRUB2 to match the architecture of the UEFI firmware.

Configuration

The configuration files are /etc/default/grub and /etc/grub.d/*. These files are used to generate the /boot/grub/grub.cfg file. You can also choose to manually edit grub.cfg.

grub-mkconfig

The grub-mkconfig script can be used to generate a Template:Filename file. By default the script outputs to stdout. Note that gettext ― an optional dependency of the GRUB2 package ― is required by the grub-mkconfig script. To generate a Template:Filename file run the command:

# grub-mkconfig -o /boot/grub/grub.cfg

grub.cfg

A basic grub file uses the following options

An example configuration:

Template:File

If you choose to modify grub.cfg completely manually and won't even generate the first grub.cfg, you should know that 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
}

Dual-booting

NOTE: If you want GRUB2 to automatically search for other systems, for example as in Ubuntu. Then you may need to download Template:Package AUR from the AUR.


Using grub-mkconfig

The best way to add other entries is editing the Template:Filename. The entries in this file will be automatically added when running grub-mkconfig. After adding the new lines, run

# grub-mkconfig -o /boot/grub/grub.cfg 

to generate an updated Template:Filename.

With GNU/Linux

Assuming that the other distro is on partition Template:Filename:

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

This assumes that your Windows partition is Template:Filename.

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

If the windows bootloader is on an entirely different harddrive than grub, it may be neccessary to trick windows into believing that it is in fact the first harddrive. This was possible in the old grub with map and is now done with drivemap. Assume grub is on hd0 and windows on hd2, you need to add the following after set root

drivemap -s hd0 hd2

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 Template:Filename file with lines similar to the following:

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

Visual Configuration

In GRUB2 it is possible, by default, to change the look of the menu.

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 Template:Filename, or, as only ascii characters under the name Template:Filename. Image formats supported include tga, png and jpeg, providing the correct modules are loaded. The maximum supported resolution depends on your hardware. There are two ways of setting a tga file as background. Two sample configurations are shown below.

The new über method

Edit /etc/default/grub like this:

GRUB_GFXMODE=1024x768x32
GRUB_GFXPAYLOAD_LINUX=keep
GRUB_BACKGROUND="/boot/grub/archlinux.tga"
#GRUB_THEME="/path/to/gfxtheme"

To generate the changes, run:

grub-mkconfig -o /boot/grub/grub.cfg
The deprecated method

This is the piece of text that would be generated. You can choose to add it manually to grub.cfg:

if loadfont /usr/share/grub/unicode.pf2 ; then
  set gfxmode="1024x768x32"
  set gfxpayload=keep
  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
Note: If this example doesn't work for you try to replace Template:Codeline by Template:Codeline.
Note: To show all the modes you can use the Template:Codeline command at grub2 prompt (you need to load the vbe module before).
Note: If you have installed Grub on a separate partition, /boot/grub/archlinux.tga becomes /grub/archlinux.tga.

Menu colors

As in Grub (0.9x), you can change the menu colors in Grub2. The available colors for GRUB2 are at http://www.gnu.org/software/grub/manual/html_node/Theme-file-format.html#Theme-file-format . Here's an example:

The new über method

Edit /etc/default/grub:

GRUB_COLOR_NORMAL="light-blue/black"
GRUB_COLOR_HIGHLIGHT="light-cyan/blue"

Execute:

grub-mkconfig -o /boot/grub/grub.cfg
The deprecated method
### BEGIN /etc/grub.d/00_header ###
....
set menu_color_normal=light-blue/black
set menu_color_highlight=light-cyan/blue
....
### END /etc/grub.d/00_header ###

Hidden menu

One of the unique features of Grub2 is hiding/skipping the menu and showing it by holding "Shift" when needed. You can also adjust whether you want to see the timeout counter.

The new über method

Edit /etc/default/grub as you wish. Here's an example where the comments from the beginning of the two lines have been removed to enable the feature, the timeout has been set to five seconds and to be shown to the user:

GRUB_HIDDEN_TIMEOUT=5
GRUB_HIDDEN_TIMEOUT_QUIET=false

And run:

grub-mkconfig -o /boot/grub/grub.cfg
The deprecated method

Once again, you can choose to be an exception and do this manually as shown here:

....
set locale_dir=($root)/boot/grub/locale
set lang=en
insmod gettext
'if sleep --interruptible 5 ; then
 set timeout=0
fi
### END /etc/grub.d/00_header ###
....

Setting the framebuffer resolution

The new über method

Grub2 can set the framebuffer for both grub2 itself and the kernel. The old vga= way is deprecated. The preferred method is editing /etc/default/grub as the following sample:

GRUB_GFXMODE=1024x768x32
GRUB_GFXPAYLOAD_LINUX=keep

To generate the changes, run:

grub-mkconfig -o /boot/grub/grub.cfg

The gfxpayload property will make sure the kernel keeps the resolution.

If this method does not work for you, the deprecated vga= method will still work. Just add the vga= setting next to the "GRUB_CMDLINE_LINUX_DEFAULT=" line in "/etc/default/grub" for eg: "GRUB_CMDLINE_LINUX_DEFAULT="quiet splash vga=792" will give you a 1024x768 resolution.

You can choose one of these resolutions: 640×480, 800×600, 1024×768, 1280×1024, 1600×1200

The deprecated method

Again, you can add the configuration manually to grub.cfg as shown below.

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

Other Options

LVM

If you use LVM for your Template:Filename, 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)
# you can only set following two lines
linux /vmlinuz26 root=/dev/mapper/VolumeGroup-root ro
initrd /kernel26.img
}

Raid

Grub2 provides convenient handling of raid-volumes. You need to add

insmod raid

which allows you to address the volume natively. E.g. Template:Filename becomes

set root=(md0)

whereas a partitioned raid-volume (e.g. Template:Filename) becomes

set root=(md0,1)

Persistent block device naming

You can use UUIDs to detect partitions instead of the "old" /dev/sd* and /dev/hd* scheming. It has the advantage of detecting partitions by their unique UUIDs, which is needed by some people booting with complicated partition setups.

UUIDs are used by default in the recent versions of grub2 - there's no downside in it anyway except that you need to re-generate the grub.cfg file every time you resize or reformat your partitions. Remember this when modifying partitions with Live-CD.

The new über method

The recent versions of grub2 use UUIDs by default. You can re-enable the use of UUIDS by simply commenting the UUID line (this is also what it looks like by default):

#GRUB_DISABLE_LINUX_UUID=true

you can also just set the value as false as shown here:

GRUB_DISABLE_LINUX_UUID=false

Either way don't forget to generate the changes:

grub-mkconfig -o /boot/grub/grub.cfg
The deprecated method

To list UUIDs, from a running system:

# blkid

Replace the value of the Template:Codeline pointer on the linux 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 Template:Codeline command:

search --fs-uuid --no-floppy --set=root <UUID>

What the --no-floppy flag does has already been explained. An example boot entry using Persistent block device naming would look like:

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

Using Labels

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

# tune2fs -L a <LABEL> <PARTITION>

Then, add an entry using labels. An example of this:

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

Recall previous entry

Grub2 can remember the last entry you booted from and use this as the default entry to boot from next time. This is useful if you have multiple kernels (i.e., the current Arch one and the LTS kernel as a fallback option) or operating systems. To do this, edit Template:Codeline and change the setting of Template:Codeline:

GRUB_DEFAULT=saved

This ensures that grub will default to the saved entry. To enable saving the selected entry, add the following line to Template:Codeline:

GRUB_SAVEDEFAULT=true

Remember to regenerate your configuration file.

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 Template:Filename, 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

Pager support

GRUB2 supports pager for reading commands that provide long output (like the help command). This works only in normal shell mode and not in rescue mode. To enable pager, in GRUB2 command shell type

sh:grub> set pager=1

parttool or legacy hide/unhide =

If you have a win9x paradigm with hidden C disks GRUB legacy had the hide/unhide feature. In GRUB2 this has been replaced by parttool. For example, to boot the third C disk of three win9x installations on the CLI enter the CLI and:

parttool hd0,1 hidden+ boot-
parttool hd0,2 hidden+ boot-
parttool hd0,3 hidden- boot+
set root=hd0,3
chainloader +1
boot

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 Template:Filename 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 Template:Filename from the path, (i.e. type Template:Codeline and Template:Codeline).

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 Template:Filename as needed and then run:

# grub-install /dev/sda --no-floppy

to reinstall GRUB2 and fix the problem completely, changing Template:Filename if needed. See #Bootloader installation for details.

Combining the use of UUID's and basic scripting

--Celilo 06:04, 19 March 2010 (EDT)

If you like the idea of using UUID's to avoid unreliable bios mappings or are struggling with grub syntax, here is an example boot menu item that uses UUID's and a small script to direct grub to the proper disk partitions for your system. All you need to do is replace the UUID's in the sample with the correct UUID's for your system. (The example applies to a system with a boot and root partition. You will obviously need to modify the grub configuration if you have additional partitions.)

 menuentry "Arch Linux 64" {
   #Enter the UUID of your boot partition (this is where grub and your kernel reside)
   set uuid_grub_boot=ece0448f-bb08-486d-9864-ac3271bd8d07
   
   #Enter the UUID of the partition containing the root partition of your Arch Linux installation.            
   set uuid_os_root=c55da16f-e2af-4603-9e0b-03f5f565ec4a
   
   #(Note: this may be the same as your boot partition)
   
   #Here we set the grub "root" variable by locating the uuid of the root partition identified above   
   search --fs-uuid --no-floppy --set=root $uuid_os_root
   
   #Here we set a custom variable grub_boot by locating the uuid of the boot partition identified above          
   search --fs-uuid --no-floppy --set=grub_boot $uuid_grub_boot
   
   #Here's the magic. We test to see if the boot and root partitions have the same UUID.
   #If they do we append /boot to the $grub_boot variable. For ex. (hd0,1) becomes (hd0,1)/boot.
   if [ $uuid_grub_boot == $uuid_os_root ] ; then
      set grub_boot=$grub_boot/boot
   fi
   
   # $grub_boot now points to the correct location, so the following will properly find the kernel and initrd
   linux ($grub_boot)/vmlinuz26 root=/dev/disk/by-uuid/$uuid_os_root ro
   initrd ($grub_boot)/kernel26.img
 }

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 Template:Filename or whatever, to Template:Filename (in /boot/grub/) and for each menu entry that you would like to use grub2, at the end type Template:Codeline. This will tell grub1 to forward control to grub2. This is an ugly hack though, so I advise setting the Template:Filename'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 Template:Codeline and your root is Template:Codeline (grub2 naming). grub-setup searches for Template:Filename. Just because it's on Template:Filename, 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

References

  1. Official GRUB2 Manual - http://www.gnu.org/software/grub/manual/grub.html
  2. GRUB2 wiki page describing steps to compile for BIOS - http://grub.enbug.org/TestingOnX86
  3. GRUB2 wiki page describing steps to compile for UEFI - http://grub.enbug.org/TestingOnMacbook
  4. Wikipedia's page on BIOS Boot Partition
  5. GRUB2 Wiki page describing why BIOS Boot Partition is required.

External Links

  1. A Linux Bash Shell script to compile and install GRUB2 for BIOS from BZR Source
  2. A Linux Bash Shell script to compile and install GRUB2 for UEFI from BZR Source