GRUB2: A Minimalistic How-To

First of all, it was like: “WTF”. Now, it is like: “OMG, it’s not so bad”…

Introduction

The GRUB (or GRUB 2) is a replacement for the deprecated GRUB Legacy. A first timer may find the configuration complex and become easily confused. However, it is not that way, and the “new” GRUB is not the pain in the as someone may expect it to be…

This example is based on a general Gentoo linux distribution installed on a general (desktop/laptop) PC, thus different distros may differ a little. No encryption, no LVM, no RAID, or other “wild” options are considered here, only a generic x86 (32- or 64-bit) computer.

GRUB Emergence

If the latest GRUB (version 2.00 or higher) is not installed, it can be done as follows:

emerge -qa sys-boot/grub

GRUB Setup

It requires a few steps only. Moreover, some of them are optional and some of them are for backup purposes only.

Platform

It can be decided to manually set GRUB_PLATFORMS in /etc/portage/make.conf. Otherwise, GRUB will guess the platform itself.

nano /etc/portage/make.conf
# Standard PC (BIOS)
GRUB_PLATFORMS="pc"

Installation

Assuming the target disk is /dev/sda, the GRUB installation is performed as follows:

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

If everything goes fine (as in the previous example), the installation is finished without any troubles.

Old Config Backup

Backup your old grub.conf (menu.lst) file:

cp /boot/grub/grub.conf /boot/grub/grub.conf.BACKUP

Kernel Naming Specifics

GRUB requires to have the kernels named in one of the following formats: /boot/vmlinuz-VERSION or /boot/kernel-VERSION, e.g. kernel-3.10.7-gentoo-r1. If you use initramfs, then /boot/initramfs-VERSION.img or /boot/initrd-VERSION.img, e.g. /boot/initramfs-genkernel-x86_64-3.10.7-gentoo-r1.

Config File Generation

Having the correct kernel naming, the GRUB configuration file can be generated:

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

Found linux image: /boot/kernel-3.10.7-gentoo-r1

done

If everything goes fine (as in the previous example), the configuration file is correctly generated and can be verified by:

cat /boot/grub/grub.cfg
That’s pretty much it. No pain at all 🙂

Source.

Of course, GRUB provides many more configuration options and settings. And some of them can be found as follows…

Setup of Some Advanced Features

There are many features which can be found, for instance here. This how-to deals with a very limitted group of them.

Themes or Backgrounds

GRUB can be easily modified in the /etc/default/grub file to utilize the framebuffer graphical display. For themes, the following line, needs to be changed.

GRUB_THEME="/boot/grub/themes/starfield/theme.txt

The startfield is the predefined theme which is commented, thus not used by default.

If the background change is required only, the following line needs to be update accordingly:

GRUB_BACKGROUND="/boot/grub/mybackground.png"

Each change of the default configuration file, i.e. /etc/default/grub, needs to be applied to the /boot/grub/grub.cfg file.

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

After the computers restart, the selected theme/background is diplayed.

Memtest

Edit /etc/grub.d/40_custom script and add:

menuentry "Memtest 86+" { 
linux16 /memtest86+.bin
}

The bin images can be found here.

And again, regenerate the grub.cfg by:

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

ISO images

The ISO image can be natively booted without the need to burn a CD or DVD. For a permanent and automatic entry to grub.cfg, a custom script could be added into /etc/grub.d. Edit:

nano /etc/grub.d/50_isofile
#!/bin/sh
exec tail -n +3 $0

menuentry "SYSRESCUECD" {
	loopback loop /sysrecuecd-x86-3.8.0.iso
	linux  (loop)/isolinux/rescue64 nomodeset vga=791 docache isoloop=/sysrecuecd-x86-3.8.0.iso
	initrd (loop)/isolinux/initram.igz
}

And make the script executable:

chmod +x /etc/grub.d/50_isofile

And again, regenerate the grub.cfg by:

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

Resume/SWAP

Edit the /etc/default/grub and modify accordingly:

nano /etc/default/grub
GRUB_CMDLINE_LINUX="resume=/dev/sdXY"

And again, regenerate the grub.cfg.

Windows Partition Boot

For instance, the /etc/grub.d/40_custom file can be utilized for this by adding the following:

menuentry ‘Windows 7′ {
set root=’(hd0,msdos2)’
chainloader +1
}

where hd0 refers to the first disk (/dev/sda) and msdos2 to the second partition on it (/dev/sda2).
Now, regenerate the grub.cfg.

Probing

To have GRUB automatically find different partitions and assign as the root partition, use the following commands.

emerge sys-boot/os-prober

The partitions needs to be mounted under Linux first.

Advertisements
This entry was posted in Linux, Server and tagged , , , . Bookmark the permalink.