Search My Blog

Saturday, February 4, 2012

GRUB 2 bootloader - Full tutorial

GRUB 2 bootloader - Full tutorial

This is a good Tutorial on Grub2. I am posting the beginning and some grub2 install commands for quick reference. The whole article is long and in depth and should be allot of help in getting a better understanding of Grub2...

Don

GRUB 2 bootloader is in continuous development. Some information may change over time, as features are added or removed and fixes introduced. For latest updates, please take a look at Updates section below.

This tutorial focuses on GRUB version 2, the next generation of the popular bootloader. If you are looking for the original (legacy) GRUB tutorial, please take a look at this article.

Welcome to the GRUB 2 bootloader tutorial! You must have read my GRUB legacy guide. In the last two and a half years, it alone has garnered some half a million views, proving to be quite popular and useful to computer users worldwide.

I want to recreate the same success with my GRUB 2 tutorial. My goal is to provide people running any flavor of UNIX-like operating systems or multi-booting their computers and using GRUB as their bootloader with a simple, no-nonsense, step-by-step, proven and working tutorial that should allow them to quickly, easily and painlessly control the boot sequence of their systems.

In this tutorial, you will learn how to work with GRUB 2, add and remove menu entries, customize titles and boot options, dual-boot and triple-boot operating systems, combine legacy GRUB and GRUB 2, and we will even see how Windows fits into this scheme. After that, we will learn how to recover from errors and mistakes.

Follow me.


Table of Contents


  1. Warning
  2. GRUB 2 roadmap
  3. GRUB 2 introduction
    1. New layout
    2. Sample grub.cfg file
    3. Sample /etc/grub.d/ directory
    4. Sample /etc/default/grub
  4. Basic usage
    1. How GRUB 2 works?
    2. Add new GRUB script
    3. Update GRUB
    4. Change script boot order by changing numbers
    5. Replacing default entries
    6. OS Prober
    7. Reinstall GRUB
    8. Mini summary
  5. Real life multi-boot cases
    1. Dual boot: two operating systems with GRUB 2
    2. Dual boot: two operating systems with GRUB legacy & GRUB 2 mix
    3. Dual boot: Windows 7 and Ubuntu
    4. Upgrade from GRUB legacy to GRUB 2
  6. Additional test case scenarios
    1. Triple boot
  7. Customize GRUB menu
    1. Edit /etc/default/grub
    2. Remove memtest from the menu
    3. Change titles in menu entries
    4. Change theme (boot image)
  8. GRUB 2 recovery
    1. Recover from failed boots
  9. Small problems observed
    1. Kernel crash dump mechanism
    2. Command names
  10. GRUB legacy versus GRUB 2
    1. GRUB legacy
    2. GRUB 2
    3. Verdict
  11. More reading (documents & links)
    1. Official
    2. Other useful GRUB 2 resources
  12. Conclusion
  13. Updates

Warning

Warning! GRUB 2 is still beta software. Although it already ships with Ubuntu flavors, it is not yet production quality per se. You can probably use it without any troubles, although there is a slight, remote yet possible chance of damage.

You need to be aware of this. Furthermore, whenever handling delicate tasks like the critical functions of the system, disk partitioning, boot sequence, imaging, etc, you should always be prepared for the worst. This means:

  • You must have a solid, proven backup procedure for all your data.
  • You must possess the tools and the knowledge to quickly recover from disasters. This includes being able to restore a previous system image, fix broken configurations, restore the bootloaders, and more.
  • You must be confident in what you are doing.

Now that we know this, let us proceed cheerfully and safely.

Just remember that GRUB 2 is still beta. Although the same can be claimed for Google Mail, which was beta for some six years or so, you must exercise caution. What's more, the contents and relevance of contents in this tutorial might yet change as GRUB 2 makes into the production, so stay tuned for any updates.

GRUB 2 roadmap

This is something you should consider before trying GRUB 2. When will GRUB 2 become the de facto bootloader for UNIX-like operating systems? Currently, GRUB legacy is doing fine and will continue for many more years. Given the long-term support by companies like RedHat and Novell for their server distributions, GRUB legacy is going to remain the key player for at least 5-10 years.

On desktops, the adoption rate may be faster, but do not expect any miracles too soon. Nevertheless, it does not hurt to start exploring. Be aware that you may encounter some compatibility issues down the road, especially with more conservative distributions that do not embrace new technologies too quickly.

At the time being, GRUB 2 is only used by the Ubuntu family, which makes about a third to one half of the Linux desktop market. That makes this tutorial rather relevant, as about one in every two or three Linux home users will probably be interested in learning more about GRUB 2 and its uses.

Let's now move on to the actual mechanics.

GRUB 2 introduction

Before you dig in, I strongly advise you to read my original GRUB tutorial. This will help you understand this article better.

GRUB 2 introduces many new changes. GRUB 2 has better portability and modularity, supports non-ASCII characters, dynamic loading of modules, real memory management, and more.

All these are pretty much irrelevant for most users. What you need to know are the changes in the configuration files and the way GRUB 2 operates.

New layout

Old GRUB files were (and still are!) located under /boot/grub/, including the menu.lst file that was read during boot and which contents were displayed to the user in the form of the GRUB menu.

GRUB 2 places its files in three core locations:

/boot/grub/grub.cfg - This is the main configuration file that replaces menu.lst. Unlike menu.lst, this file cannot be edited by hand! I strongly advise against trying to tamper with this file, using chattr command or anything of the sort. Let it be.

/etc/grub.d/ - This new directory contains GRUB scripts. These scripts are building blocks from which the grub.cfg file is built. When the relevant GRUB command is executed, the scripts are read in a certain sequence and grub.cfg is created.

/etc/default/grub - This file contains the GRUB menu settings that are read by the GRUB scripts and written into grub.cfg. It is the customization part of the GRUB, similar to the old menu.lst, except the actual boot entries.

This means that if you want to change the GRUB menu, you will have to edit existing scripts or create new ones, then update the menu. This is more similar to LILO than GRUB legacy, which allow editing the menu on the fly.

Sample grub.cfg file

The file is located under /boot/grub/:

This is what the grub.cfg file looks like:

It is not really interesting, because it's just a shell script. Most people will probably not want to read its contents or be able to fully understand them. This file is still worth a quick look any time you update the GRUB menu, to make sure the correct entries are included as you expected. But only if you're comfortable with scripts!

Sample /etc/grub.d/ directory

These are the contents of the directory on a fresh installation, in this case Kubuntu:

Let's review the scripts:

00_header is the script that loads GRUB settings from /etc/default/grub, including timeout, default boot entry, and others. We will talk more about these soon.

05_debian_theme defines the background, colors and themes. The name of this script is definitely going to change to when other distributions adopt GRUB 2.

10_linux loads the menu entries for the installed distribution.

20_memtest86+ loads the memtest utility.

30_os-prober is the script that will scan the hard disks for other operating systems and add them to the boot menu.

40_custom is a template that you can use to create additional entries to be added to the boot menu.

Have you noticed the numbering in the script names? Well, this is somewhat similar to the order of Start/Kill scripts used in different runlevels. The numbering defines precedence. This means that 10_linux will be executed before 20_memtest86+ and therefore placed higher in the boot menu order.

The scripts are not very interesting. Like the grub.cfg file, they are not intended to be edited, save for 40_custom. You need to very careful when working with these scripts.

We will soon see what they look like and learn how to use them.

Sample /etc/default/grub

The file is located under /etc/default:

This directory contains many other files so do not assume it is just used for GRUB. The grub file is a text file that is parsed by the 00_header script. You can make your changes here, if you want. We will talk about these later, in the Customization section.

Now we know what the core files are. Let's see how we use them.

Basic usage

It's time to put our theoretical knowledge to some real action.

How GRUB 2 works?

Skipping on Down...

GRUB 2 recovery

Recover from failed boots

What happens if you ruin your GRUB 2? There must be a way to reinstall it and save the day? Well, as always, there's the easy way and the hard way:

Easy way: Super Grub Disk

This great tool works with GRUB 2, so no worries, place it into the CD/DVD tray, boot and restore the mangled GRUB.

Hard way: Manual fix from live CD

You will need a live CD that ships with GRUB 2, like Ubuntu or Kubuntu. Boot into the live session, mount the hard disk and install GRUB 2 to the MBR.

This is the sequence of commands you require (assuming disk = /dev/sda). You will need to mount the partition of your installed distribution (e.g. Ubuntu) containing the /boot directory. It may also be a separate partition on your system, depending on your setup.

After it is mounted, you will have to rerun the grub-install command.

mount /dev/sda1 /mnt/
grub-install --root-directory=/mnt /dev/sda

If this does not work, you will have to go through a much longer, more complicated procedure, as explained in the official GRUB 2 Ubuntu Wiki documentation.

Use fdisk to locate the right root device, then mount it as before. For the sake of this section, let's assume that /dev/sda1 is a dedicated /boot partition and /dev/sda2 is the root partition of your distro.

sudo mount /dev/sda2 /mnt

If you have a separate /boot partition, you will have to mount it too:

sudo mount /dev/sda1 /mnt/boot

Next, remount the rest of your devices using --bind option. For more details about how the mount command works, please consult the man page.

sudo mount --bind /dev /mnt/dev

Effectively, you now have a root system under mount. You can now change the root from your real one (/) to /mnt. This is done using the chroot command.

sudo chroot /mnt

This means that command executed in this terminal will refer to /mnt/. Now, reconfigure the GRUB package:

dpkg-reconfigure grub-pc

You will have to tell GRUB which device to use. You can change the selection using Spacebar. The devices shown will reflect that in the device.map file, so if you only have one, there should be no issues.

Once this step is done, you can exit from the chroot environment. This is done by pressing Ctrl + D keys. After that, unmount the devices, first the /dev, then the rest:

sudo umount /mnt/dev
sudo umount /mnt

Now you can reboot. GRUB 2 should be in place.

Small problems observed


Read More...
http://www.dedoimedo.com/computers/grub-2.html


Grub2 info
BIOS Implementation Test Suite
How to add XP / Vista / Windows install iso to GRUB2 multiboot? - Hak5 Forums
Mastering Grub 2 The Easy Way
supergrub disk 2 - Google Search
Super Grub(2) Disk (English)
Boot Problems Open Source Tools | Super Grub Disk, Super Grub2 Disk and Rescatux
Rescatux | Boot Problems Open Source Tools
Rescatux | Boot Problems Open Source Tools

Grub2 and os-prober
DonsDeals: My brand new Debian 6 grub menu had no mention of WinXP:O
os-prober - Google Search
DonsDeals: Automatically adding other distributions to Fedora Grub using os-prober « Hedayat’s Blog

Grub Boot Loader
install grub boot loader to cd - Google Search
GNU GRUB - Obtaining GRUB
Index of ftp://alpha.gnu.org/gnu/grub/
GRUB bootloader - Full tutorial
GRUB bootloader - Full tutorial
GRUB bootloader - Full tutorial
How to make a Grub floppy/CD that boots all systems in a PC - JustLinux Forums
GRUB - bootloader
GRUB 2 bootloader - Full tutorial
8.4. Using GRUB to Set Up the Boot Process
GNU GRUB - Documentation
Index of ftp://ftp.gnu.org/gnu/grub/
GNU GRUB - GNU Project - Free Software Foundation (FSF)
install grub2 on usb - Google Search
install grub2 on usb hard drive - Google Search
How to install GRUB2 on USB drive from within Windows - reboot.pro
MultiBoot USB with Grub2 (boot directly from iso files) | panticz.de
Boot Multiple ISO from USB via Grub2 using Linux | USB Pen Drive Linux
Install GRUB2 on a USB Flash Drive using Linux | USB Pen Drive Linux
How to boot from an USB stick without BIOS support using Grub2?
[SOLVED] Grub 2 - USB External HDD - Ubuntu Forums
Tails - Download, verify and install
How To Boot An ISO With GRUB2 (The Easy Way!) ~ Web Upd8: Ubuntu / Linux blog
installmodes – unetbootin
howitworks – unetbootin

No comments: