GRUB

From ArchWiki
Revision as of 02:25, 3 July 2010 by Rransom (Talk | contribs) (Installing GRUB2 during Arch Linux installation: remove possibly false statement; I was confused)

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 – فارسی

Template:Article summary start Template:Article summary text Template:Article summary heading Template:Article summary wiki Template:Article summary heading Template:Article summary link Template:Article summary link Template:Article summary end

GRUB2 is the next generation of the GRand Uniform 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 yet *not* 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

The GRUB2 package can be installed with pacman (and will replace Template:Package Official, 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.

Installing GRUB2 during Arch Linux installation

  • Skip the Install Bootloader step and exit the installer.
  • Configure the network:
# aif -p partial-configure-network
  • From the installer's live shell, chroot to the installed system:
# mount -o bind /dev /mnt/dev
# chroot /mnt bash
  • Install the GRUB2 package:
# pacman -Sy grub2

Bootloader Installation

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:

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

Where 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-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.

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. 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

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 +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 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/color.html. 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


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
}

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 --no-floppy --fs-uuid <UUID> --set root

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 --no-floppy --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
}

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 archroot --set root
    linux /boot/vmlinuz26 root=/dev/disk/by-label/archroot ro
    initrd /boot/kernel26.img
}

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


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" {
   set uuid_grub_boot=ece0448f-bb08-486d-9864-ac3271bd8d07        #Enter the UUID of your boot partition (this is where grub and your kernel reside)   
   set uuid_os_root=c55da16f-e2af-4603-9e0b-03f5f565ec4a          #Enter the UUID of the partition containing the root partition of your Arch Linux installation. 
                                                                  #(Note: this may be the same as your boot partition)   
   search --no-floppy --fs-uuid $uuid_os_root --set=root          #Here we set the grub "root" variable by locating the uuid of the root partition identified above
   search --no-floppy --fs-uuid $uuid_grub_boot --set=grub_boot   #Here we set a custom variable grub_boot by locating the uuid of the boot partition identified above
   #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