Master GRUB 2 on Linux: Configure, Recover, and Customize
The Grand Unified Boot Loader - Grub 2 has been the default boot system for Ubuntu since version 9.10 and for other distributions with more recent versions, especially on Kernel 2.6 and 3.x. Meet the powerful GRUB 2.
GRUB 2 was completely rewritten with the goal of allowing great flexibility and performance gains. Among the various improvements, the main ones are:
- Support for scripts with conditional expressions and functions;
- Dynamic module loading;
- Emergency Mode (Rescue Mode);
- Custom Menus;
- Theme support;
- Support for graphical menu with "splash" screen;
- Load ISO images of Live CDs;
- New configuration file structure;
- Support for non-x86 platforms (e.g. PowerPC);
- Universal UUID support (Universally Unique IDentifier) – identifier used in distributed systems.
Upgrading from Grub 1 to Grub 2
Upgrading from Grub 1 to Grub 2 is relatively easy. First you must install the Grub 2 package with the command:
# sudo apt-get install grub-pc
After installation, Grub 2 will present a test menu with the kernel images found and also allows the menu to be edited.
Once the menu is defined, Grub2 can be installed permanently with the command:
# sudo upgrade-from-grub-legacy /boot/grub/grub.cfg
Main Changes from Grub 1 to Grub 2
It is important that you know the important changes from Grub 1 to Grub 2, namely:
- The file /boot/grub/menu.lst was replaced by /boot/grub/grub.cfg;
- The main configuration file is /etc/default/grub;
- The partition numbering system was changed;
- Searches for other operating systems are done automatically every time update-grub is executed and are placed in the menu;
Configuration changes have no effect until the update-grub command is executed.
Normally /boot/grub/grub.cfg should not be edited manually because it is overwritten during Grub 2 package updates, when a kernel is added or removed, or when the user runs the update-grub command. This command rebuilds the boot menu from other configuration files and rewrites /boot/grub/grub.cfg.
Creating a Default Configuration in Grub
The grub-mkconfig utility can also be used to create a configuration for Grub:
# grub-mkconfig -o /boot/grub/grub.cfg
The main configuration file for menu presentation is located in /etc/default/grub.
The /etc/default/grub File
This file contains the main information for Grub 2 to build the menu. It defines which will be the default menu option, the wait times for user action, whether the menu will be presented in graphical mode or text mode, among other options.
Example of /etc/default/grub:
GRUB_DEFAULT=0
#GRUB_SAVEDEFAULT = true
#GRUB_HIDDEN_TIMEOUT=0
GRUB_HIDDEN_TIMEOUT_QUIET=true
GRUB_TIMEOUT=10
GRUB_DISTRIBUTOR='lsb_release -i -s 2> /dev/null || echo Debian'
GRUB_CMDLINE_LINUX_DEFAULT="quiet splash"
GRUB_CMDLINE_LINUX=""
#GRUB_TERMINAL=console
#GRUB_GFXMODE=640x480
#GRUB_DISABLE_LINUX_RECOVERY="true"
#GRUB_INIT_TUNE="480 440 1"
Grub Configuration File
See the Grub option comments:
GRUB_DEFAULT
Defines which will be the default menu option of Grub. The GRUB_DEFAULT parameter can be a numeric value, the menu option name, or the word "saved".
If the value is numeric, it indicates the menu position in /boot/grub/grub.cfg. It can also indicate the menu name "Ubuntu 2.6.31".
If the value is "saved", the default will be set by the grub-set-default or grub-reboot command.
GRUB_SAVEDEFAULT
If this parameter is "true", it tells Grub that the default menu option will always be the last selected operating system in the menu.
GRUB_HIDDEN_TIMEOUT
This parameter defines the time in seconds that Grub will wait for user action until the default menu option is chosen automatically. No menu will be displayed unless the user presses a key (usually ESC).
If set to zero, the default operating system will load automatically without waiting for any intervention.
GRUB_HIDDEN_TIMEOUT_QUIET
When this option is "true", no countdown timer will be displayed. If "false", a countdown timer will be displayed with the time defined in GRUB_HIDDEN_TIMEOUT.
GRUB_TIMEOUT
This option will only be effective if the GRUB_HIDDEN_TIMEOUT directive is commented out, or if active, when the user presses a key during the GRUB_HIDDEN_TIMEOUT wait time.
GRUB_TIMEOUT defines the menu wait time until the user makes their choice. Otherwise, it will execute the default option.
If the value is -1, Grub will wait for the user indefinitely.
GRUB_DISTRIBUTOR
Determines the system description that will be used in the menu. If nothing is set, Grub will use the system default, which is usually set by the command:
# lsb_release -i -s 2> /dev/null || echo Debian
SUSE LINUX
GRUB_CMDLINE_LINUX_DEFAULT
This directive passes parameters to the Kernel during normal system boot.
GRUB_CMDLINE_LINUX
This directive passes parameters to the Kernel during system boot, both for normal kernel boot or in "recovery" mode.
GRUB_TERMINAL
This option, if uncommented, disables the graphical menu.
GRUB_GFXMODE
Defines the resolution of the graphical menu terminal.
GRUB_DISABLE_LINUX_RECOVERY
Disables Kernel emergency mode if set to "true".
GRUB_INIT_TUNE
Defines a "sound" that Grub will make before presenting the menu. The format is [note pitch] [duration].
Example: Third Degree Immediate Contact Theme Gravar="480 900 2 1000 2 800 2 400 2 600 3"
The /etc/grub.d directory
The scripts in this directory are read during the update-grub command execution and their instructions are incorporated into the /boot/grub/grub.cfg file.
The order of items in the grub menu is determined by the order of the scripts in this directory. The files are read according to the numeral preceding their name, in ascending order.
00_header
Configures initial parameters, such as the graphical mode, default menu option. These settings are usually imported from /etc/default/grub.
05_debian_theme
Configures the background screen, text colors, etc.
10_hurd
Used to locate Hurd Kernels. Not used in most distributions.
10_linux
Identifies the Kernels on the root device and creates the menu entries. All kernel images existing in /boot will be placed in the menu.
20_memtest86+
Looks for the image at /boot/memtest86+.bin used for memory testing. To remove this entry, it is necessary to remove the image in the /boot directory.
30_os-prober
This script looks for Linux and other operating systems existing on the disk and creates the menu entries. The variables in this file determine how the names will appear in the menu.
40_custom
Allows adding custom entries to the grub menu.
The settings are
usually made in the /etc/default/grub file.
It is not common to directly edit the scripts in the /etc/grub.d directory.
Any change made to /boot/grub/grub.cfg is overwritten when the
update-grub command is executed.
System Boot Sequence
Grub2 is executed before any operating system. It will usually boot the operating system directly by setting it as default or if there is only one operating system present.
If there is more than one operating system, it will present the menu for selection.
The default menu timeout is 10s. If the user does nothing, it will start loading the default menu option.
The countdown can be paused if any key is pressed. The user can force the menu display by pressing the SHIFT key during grub2 boot.
Partition Names in Grub 2
The partition naming system in grub2 was simplified compared to grub1. Device counting remains starting from zero.
A device sda will be named hd0, as well as a device sdb will be called hd1. However the partitions follow the "normal" numbering, so partition sda5 will be named hd0,5 and sda3 will be hd0,3.
Learn much more about Linux in our online course. You can enroll here. If you already have an account, or want to create one, just log in or create your user here.
Do you like it? Share
That was just one piece. The course teaches the whole path — from your first command to exam day.