GRUB2 Quick Start

From Gentoo Wiki
Jump to: navigation, search
Other languages:
Deutsch • ‎English • ‎日本語 • ‎한국어

This article provides information on how to get up and running with GRUB2 in the simplest configurations. For more comprehensive information, see GRUB2. For a migration from GRUB Legacy to GRUB2, see GRUB2 Migration.

Installing GRUB2 software

The sys-boot/grub package is slotted; both grub-0.97 and grub-2.xx may be installed at the same time. However, while both versions of GRUB can be installed on the system at the same time, only one version of GRUB may be active as the system's boot loader at the same time.

To install GRUB2, first set the GRUB_PLATFORMS variable in make.conf (optional). If unset, GRUB2 will guess which platform to use on the system. It guesses "pc" (which is not UEFI; see examples below) for X86/AMD64 architecture.

FILE /etc/portage/make.confGRUB_PLATFORMS example
# Standard PC (BIOS)
GRUB_PLATFORMS="pc"
 
# UEFI on amd64
GRUB_PLATFORMS="efi-64"
 
# UEFI and PC
GRUB_PLATFORMS="efi-64 pc"

After the variable is set, emerge the software:

root #emerge --ask sys-boot/grub:2

Activating the GRUB2 boot loader

Mount /boot if applicable:

root #mount /boot

When using an EFI platform, make sure that the EFI System Partition is available at /boot/efi. This can either be through a specific mount point (at /boot/efi) or by having the boot partition be the EFI System Partition.

root #mount /boot/efi

Run the grub2-install utility to copy the relevant files to /boot/grub. On the PC platform, this also installs a boot image to the Master Boot Record (MBR) or a partition's boot sector.

To install GRUB2 to the MBR:

root #grub2-install /dev/sda
Installation finished. No error reported.

To install GRUB2 to a partition (not recommended):

root #grub2-install /dev/sda1
Installation finished. No error reported.

To install GRUB2 on an EFI capable system:

root #grub2-install --target=x86_64-efi
Installation finished. No error reported.
Important
When installing GRUB2 to an EFI capable system (like the example above) is giving troubles, make sure the GRUB_PLATFORMS variable is properly set in /etc/portage/make.conf

The grub2-install command accepts a --target option to specify which CPU/Platform to install. If unspecified, grub2-install will make a guess; on X86/AMD64 it will use "i386-pc" by default.

Automatic configuration

GRUB2 is traditionally configured by using the grub2-mkconfig program to generate a configuration file.

grub2-mkconfig generates the configuration file based on template sections located in /etc/grub.d. The default templates should cover most common boot setups.

user $ls /etc/grub.d
00_header  10_linux  20_linux_xen  30_os-prober  40_custom  41_custom  README

The behavior of these templates can be controlled by setting variables in /etc/default/grub. See the GRUB manual for more information.

Kernel naming scheme

In order for grub2-mkconfig to detect the available Linux kernel(s), their names must start with vmlinuz- or kernel-.

For example:

CODE Example kernel names that GRUB2 can detect
 /boot/vmlinuz-3.4.3
 /boot/kernel-2.6.39-gentoo
 /boot/kernel-genkernel-x86_64-3.17.1-gentoo-r1

When using an initramfs, its name should start with initramfs- or initrd- and end with .img. The version must match one of a kernel image. File names generated by genkernel will also work.

For example:

CODE Example initramfs names that GRUB2 can detect
 /boot/initramfs-3.4.3.img
 /boot/initramfs-genkernel-x86_64-2.6.39-gentoo

To generate the grub.cfg file, execute the grub2-mkconfig command like so:

root #grub2-mkconfig -o /boot/grub/grub.cfg
Generating grub.cfg ...
Found linux image: /boot/vmlinuz-3.2.9
done

Silent kernel decompression

To silence kernel decompression at boot time, edit /etc/default/grub and add quiet to the GRUB_CMDLINE_LINUX_DEFAULT parameter.

FILE /etc/default/grubSilent decompression example
GRUB_CMDLINE_LINUX_DEFAULT="quiet"

Systemd

To boot systemd while using GRUB2 make the GRUB_CMDLINE_LINUX variable look like this:

FILE /etc/default/grubSystemd example
GRUB_CMDLINE_LINUX="init=/usr/lib/systemd/systemd"

Loading another operating system

grub2-mkconfig can also generate configurations to load other operating systems. This requires the sys-boot/os-prober package.

To boot Windows, the sys-fs/ntfs3g also needs to be installed. It allows for the grub2-mkconfig utility to probe NTFS filesystems.

Manual configuration

Users do not need to use grub2-mkconfig. The grub.cfg file can be edited manually as well.

As most users have experience with GRUB Legacy format, the next example shows how to write a GRUB2 configuration file based on information from the GRUB Legacy configuration.

grub.conf (GRUB Legacy) grub.cfg (GRUB 2)
timeout 5


title Gentoo Linux 3.2.12
root (hd0,0)
kernel /boot/kernel-3.2.12-gentoo root=/dev/sda3

timeout=5


menuentry 'Gentoo Linux 3.2.12' {
root=hd0,1
linux /boot/kernel-3.2.12-gentoo root=/dev/sda3
}

See also

Please refer to the GRUB2 article for more information and an exhaustive list of resources.