GRUB: Using a Bootloader
Software version | 1 & 2 |
Operating System | Linux |
Website | Grub Website |
Last Update | 25/04/2012 |
Introduction
GNU GRUB (acronym for “GRand Unified Bootloader”) is a boot loader program for computers. It executes when the computer is powered on, after internal control sequences and before the actual operating system, since its role is to organize the loading process. When the computer hosts several systems (known as multi-boot), it allows the user to choose which system to start.
It has multiple advantages. First, it’s free software. It also allows the management of systems other than Linux and Windows (useful for Hurd, Solaris, FreeBSD, and OpenBSD), reads configuration at boot time (no need to reinstall GRUB in the boot sector after a configuration change, unlike LILO), offers a command line to change configuration at boot, and especially has native recognition of various existing file systems. It also has a simple command language to “recover” a boot that would have gone wrong, due to incorrect partition addressing, for example.
GRUB must be able to recognize all file systems on which it might be needed to boot. For this reason, it is much larger than LILO.
It is part of the GNU project.
Restoring GRUB After a Problem
Grub 1
- Boot the PC with a Debian installation disc (net install for example)
- At the command prompt type: rescue root=/dev/hda2
Note: hda2 corresponds to the partition where the kernel is located (/boot)
- Continue the procedure by answering the various questions about your installation
- Finally, choose the restore GRUB option and follow the indicated procedure
Grub 2
Overwriting the MBR
When another boot manager has taken the place of grub-pc in the MBR:
- Boot on a system that has grub-pc by default (for example the Linux live-CD)
- Mount the disk containing /boot/grub/grub.cfg that grub-pc will need to use (for example by going to “Shortcuts/Home Folder” and clicking on the relevant disk on the left, then ok to authorize its mounting)
- Then run the following command, replacing “DiskWhereIHaveMyBoot” with your mount point and /dev/sda possibly with the disk that should contain grub-pc on the MBR:
sudo grub-install --root-directory=/media/DiskWhereIHaveMyBoot /dev/sda
- Restart by removing your live-cd or your boot USB stick.
Stages and Boot Options
Here are the different stages with options for booting in a functional mode and trying to recover from an error that prevents correct booting:
Problem Stage Boot Option
-------------------------------
BIOS |
| Linux rescue
GRUB |
| grub shell
Kernel or Initrd |
| kernel .. root=LABEL=root init=/bin/bash
init |
| kernel .. root=LABEL=root emergency
rc.sysinit |
| kernel .. root=LABEL=root single
rc scripts (K,S) |
| kernel .. root=LABEL=root 1
rc.local |
v
Setting Up a Rescue Kernel
The idea here is to implement a solution so that if the kernel crashes at boot, it can restart on an old kernel. We will use a failback method integrated into Grub. We simply need to add a few lines (/etc/grub.conf
):
# grub.conf generated by anaconda
#
# Note that you do not have to rerun grub after making changes to this file
# NOTICE: You have a /boot partition. This means that
# all kernel and initrd paths are relative to /boot/, eg.
# root (hd0,0)
# kernel /vmlinuz-version ro root=/dev/mapper/myvg-rootvol
# initrd /initrd-[generic-]version.img
#boot=/dev/sda
default=saved
fallback 0 1
timeout=5
splashimage=(hd0,0)/grub/splash.xpm.gz
hiddenmenu
title Red Hat Enterprise Linux Server (2.6.32-220.4.2.el6.x86_64)
root (hd0,0)
kernel /vmlinuz-2.6.32-220.4.2.el6.x86_64 ro root=/dev/mapper/myvg-rootvol rd_NO_LUKS KEYBOARDTYPE=pc KEYTABLE=fr LANG=en_US.UTF-8 rd_NO_MD quiet SYSFONT=latarcyrheb-sun16 rhgb crashkernel=auto rd_LVM_LV=myvg/rootvol rd_NO_DM
initrd /initramfs-2.6.32-220.4.2.el6.x86_64.img
savedefault fallback
title Red Hat Enterprise Linux Server (2.6.32-220.2.1.el6.x86_64)
root (hd0,0)
kernel /vmlinuz-2.6.32-220.2.1.el6.x86_64 ro root=/dev/mapper/myvg-rootvol rd_NO_LUKS KEYBOARDTYPE=pc KEYTABLE=fr LANG=en_US.UTF-8 rd_NO_MD quiet SYSFONT=latarcyrheb-sun16 rhgb crashkernel=auto rd_LVM_LV=myvg/rootvol rd_NO_DM
initrd /initramfs-2.6.32-220.2.1.el6.x86_64.img
savedefault fallback
title Red Hat Enterprise Linux (2.6.32-220.el6.x86_64)
root (hd0,0)
kernel /vmlinuz-2.6.32-220.el6.x86_64 ro root=/dev/mapper/myvg-rootvol rd_NO_LUKS KEYBOARDTYPE=pc KEYTABLE=fr LANG=en_US.UTF-8 rd_NO_MD quiet SYSFONT=latarcyrheb-sun16 rhgb crashkernel=auto rd_LVM_LV=myvg/rootvol rd_NO_DM
initrd /initramfs-2.6.32-220.el6.x86_64.img
savedefault
A few explanations:
- savedefault fallback: this line indicates that this is one of the kernels we will use in case of problems
- fallback 0 1: specifies that we want to boot on kernel 0 then 1 where savedefault lines are present
Installing a Boot Splash
Introduction
Looks nice, right? Want one too? Let’s go… You must first have this compiled at the kernel level:
CONFIG_FRAMEBUFFER_CONSOLE=y
CONFIG_FRAMEBUFFER_CONSOLE_ROTATION=y
Once that’s done, we can move on to the next step.
Installation
Install this:
apt-get install bootsplash bootsplash-theme-debian grub-splashimages
Configuration
GRUB Resolution
To change the size of grub terminals (getty), edit the file /boot/grub/menu.lst
:
# defoptions=
Replace it with this:
# defoptions=vga=791 quiet splash
Keep the “#”, it’s important!
Splashimages
Here’s an example to insert in /boot/grub/menu.lst
:
## Splashimages
splashimage=(hd0,1)/grub/splashimages/fiesta.xpm.gz
Replace (hd0,1) with the one for your boot disk (/boot)
Validation
Finally, you need to validate your configuration:
update-grub
If you did everything right, when returning to your configuration you should see your new parameters that haven’t moved. If they have disappeared, you made an error in your configuration.
Converting an Image for Grub
If you want to convert an image for use in grub:
convert image123.png -colors 14 -resize 640x480 grubimg.xpm
- Size must be: 640x480 pixels
- Only 14 colors
- In XPM format
Move it to the right place and then insert it into your grub:
## Splashimages
splashimage=(hd0,1)/grub/splashimages/grubimg.xpm
References
http://www.debianaddict.org/article67.html http://www.gnu.org/software/grub/manual/legacy/Booting-fallback-systems.html http://www.linuxscrew.com/2012/04/24/grub-fallback-boot-good-kernel-if-new-one-crashes/?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A+linuxscrew+%28LinuxScrew%3A+Linux+Blog%29
Last updated 25 Apr 2012, 12:08 CEST.