Sunday, November 22, 2009

Mastering Grub 2 The Easy Way


If you’re running Linux, there’s a good chance your distro of choice uses Grub as the default bootloader.

Grub has served well for many years, but it’s beginning to show its age.

As with all software, it doesn’t take long before the latest-and-greatest becomes old-and-haggard. Features have been piling up in Grub without much thought going into revamping the core program.

Eventually, this lead to a messy patchwork that no one really wanted to maintain. At this point, Grub2 was born.

It’s a complete rewrite from the ground up using a completely redesigned structure. This new Grub gives us powerful features like conditional statements (if/then, etc), intelligent upgrades, and some greatly improved graphics.

Getting Grub2
Currently, if you want to use Grub2, your best bet is to run Ubuntu 9.10 (Karmic Koala). If you just want to toy around and see what it can do without risking your system, I’d suggest running it inside a virtual machine as described here, but of course with Ubuntu 9.10.

menu.lst/grub.cfg
If you’ve messed with the original Grub (now called Grub legacy), you doubtless know all about menu.lst. That was the file where Grub legacy stored pretty much everything.

In Grub2, the closest equivalent is grub.cfg, also located in /boot/grub. One of the most important differences, however, is that you should not edit the grub.cfg file directly.

Some of you may be thinking “Ha! Warnings like that are for n00bs!”. Well the reason for this rule is not because the developers don’t think you’re l33t enough, it’s because that file is auto-generated by Grub2 and will be overwritten without warning as a result of things like kernel upgrades.

grub2-grubcfg

Any time the update-grub command is run, Grub2 will read its various config files and use their contents to generate grub.cfg.

These files are dealt with individually below:

/etc/default/grub
Many of Grub’s options that used to be in menu.lst are now here. This includes things like whether or not to display the Grub menu, and how long to wait before timeout.

This screenshot is the default config in Ubuntu 9.10, and is set to hide the Grub menu, disable “recovery mode” options, and instruct the kernel to include a splash screen.

grub2-etcgrub

By the way, to force Grub 2 to show the menu on boot just hold down the Shift key while Grub is starting.

/etc/grub.d/
This is where things really begin to differ from Grub legacy. Where you’d normally enter all your boot options in the menu.lst file, Grub 2 takes a very different approach.

The /etc/grub.d/ directory contains individual files, each representing a option for the boot menu. The following shows the files contained in the default install.

grub2-etcgrubd

While it’s certainly a lot different than Grub legacy, you may notice something familiar about that folder. It’s set up in a way very similar to the standard Linux startup system, SysV Init.

Each of the files shown is a shell script that executes in order. Order is determined by the numbers at the start of each filename, so the script in 00_header will always run before, say, 05_debian_theme.

/etc/grub.d/40_custom
Chances are, if you need to edit your boot menu, this is the file you’re looking for. This is where you add your custom entries to the menu.

Since this is the last numbered file in the grub.d directory (as explained in the section above), any entries you place in this file will show up at the end of the boot menu.

By default, the 40_custom file is mostly empty. In this example, I’ve added a new menu entry.

grub2-40custom

Splash Images
You can easily download the Grub 2 splash pack in Ubuntu with:

# sudo apt-get install grub2-splashimages

When it’s done, all the images will be stores as TGA files in /usr/share/images/grub/.

grub2-splashpreview

The file that specifies what splash image to use on boot is the 05_debian_theme file we saw earlier. So to set our new custom splash, we want to open /etc/grub.d/05_debian_theme as root in whatever text editor you prefer.

Look for a line similar to the highlighted one in the example below,

grub2-changesplash

And change the filename to whichever splash image you prefer. Make certain the path points to the /usr/share/images/grub/ directory.

Some older versions default to /usr/share/images/desktop-base/ and that’s not what we want.

Applying changes
As you may remember from the beginning of this guide, the grub.cfg file is auto-generated from the other config files we’ve covered.

When you’ve made changes to those files, you’ve got to tell Grub to read them and pull together a new config file.

To do that, simply run the following command as root:
# update-grub

Once it’s complete, reboot your computer (or virtual machine) to see the results. Don’t forget to hold Shift while booting, if your menu is still set to Hidden.

grub2-large\

No comments:

Post a Comment