sections in this module City College of San Francisco - CS260A
Linux System Administration

Module: StartupShutdown III
module list

grub2

RH7 has upgraded to grub2, which is considerably more complex than grub1. It is complex enough, in fact, that a complete treatment is impossible. For the interested reader, the documentation in /usr/share/doc/grub2-tools-x, where x is the version, seems very complete. In this section we will highlight and tell you how to do the things most commonly needed for customization in grub2.

The most annoying thing about grub2 is learning where things are. The information about it is spread around in several places and, annoyingly, named inconsistently. Sometimes the grub files are named grub and sometimes they are named grub2. I cannot explain this except to say that someone is an idiot not paying attention.

grub2 basics

At the basic level, grub2 consists of a handful of images that are combined when it is installed as the boot loader to give it basic functionality: besides the image that is installed in the boot sector (the boot.img file, which is similar to grub1's stage1 file), there is a core image plus images to load the kernel, do a cd boot, do a disk boot, etc. This forms the basic boot loader. 

At boot time, grub2 reads its configuration file and adds more pieces (modules) as configured in that file. There are a lot of modules that can be loaded: these modules add functionality to access other filesystems, etc. If you want to take a look, list /boot/grub2/arch, where arch is the architecture version of grub (probably i386-pc). The winning feature here is an ability to access many, many types of filesystems, and even do a pxe or network boot. Also, functionality can now be added to grub2 by adding new module(s).

Because of this fluid configuration, the grub2 configuration file at /boot/grub2/grub.cfg is no longer static  - it is generated. To modify the grub2 configuration file you modify its parameters in other files then rebuild the configuration file using the program grub2-mkconfig(1). NOTE: grub2-mkconfig sends its output to standard output. You will have to save it or use the commandline option to specify where the output should go. See the man page.

The configuration file is built in a few pieces:

This configuration file is generated by running several scripts in /etc/grub.d which you should not muck with unless you know a lot more than I do. The only exception to this is the 40_custom file where you can is set a global password. This is described in an upcoming section. Later in this section we describe grub2 configuration options incorporated into the output of grub2-mkconfig. These options are kept in yet another location.

There is one more issue we should cover briefly - the root issue. Just like in grub1, grub2 has its own root, which is how grub can find its configuration file. If you recall, the root in grub1 was set like this

root (hd0,0)

to indicate the first hard disk (#0) and the first partition (#0)

in grub2, the root is set somewhat differently

set root='hd0,msdos1'

to indicate the first hard disk (#0), the first MSDOS partition on that disk (#1). note that in grub2 partitions are no longer 0-based, but the disks still are.

grub2 configuration options

A handful of global configuration parameters can be set in the configuration file /etc/default/grub. These will be incorporated into the output of grub2-mkconfig the next time it is run. The ones similar to those we discussed for grub1 are discussed here.

GRUB_TIMEOUT=n

this is how long to wait (in secs) before booting the default configuration. The default is 5. 0 boots immediately (no chance for interaction), and -1 waits indefinitely.

GRUB_TIMEOUT_STYLE=x

where x is

hidden - the menu and the countdown message are hidden

menu - the menu is displayed during the countdown

countdown - only a countdown message is displayed.

GRUB_DEFAULT=x

here x can be an integer, in which case it denotes the x-th menu entry (0-based). It can also be a menu entry id, which is difficult to configure by hand as the menu id often contains a UUID string in it. It can also be the word saved, in which case it relies on the environment block, which is grub's mechanism for keeping information between boots. The file /boot/grub2/grubenv contains the environment block, which is loaded into grub's configuration in response to the loadenv command and saved in the file in response to the saveenv command.

The environment block has a variable set in it by default to indicate the saved entry. This entry is set to the menu entry id of the most recent kernel when a kernel is installed. Assuming all the pieces are put together correctly (which is probably the case unless someone has been modifying it by hand), the most recent kernel should be the default.

GRUB_SAVEDEFAULT

Assuming the environment block is being used, you can set this option to true, and grub will save the entry chosen at the current boot to be used in the next boot. With this setting, if you decide to back out to a recent kernel and select that kernel to boot when the system comes up, it will become the default.

This is all kind of confusing, so here are some examples of the use of these parameters.

GRUB_TIMEOUT=-1
GRUB_TIMEOUT_STYLE=menu
GRUB_DEFAULT=saved
no GRUB_SAVEDEFAULT
Here, there is no automatic boot. The menu will be displayed until the user selects an item and boots the system. The default configuration is whatever is in the environment block (by default the most-recently installed kernel.)
GRUB_TIMEOUT=5
GRUB_TIMEOUT_STYLE=hidden
GRUB_DEFAULT=saved
GRUB_SAVEDEFAULT=true
Here there is no menu displayed, nor a countdown message. The last saved configuration is booted. If the user chooses a different configuration to boot this time (hitting a key to display the menu), that configuration becomes the default for next time.

Remember, changing these parameters has no effect unless you rebuild grub.cfg using grub2-mkconfig.

Prev This page was made entirely with free software on linux:  
Kompozer, the Mozilla Project
and Openoffice.org      
Next

Copyright 2014 Greg Boyd - All Rights Reserved.