CompTIA Linux+ Certification Guide
上QQ阅读APP看书,第一时间看更新

Working with GRUB2

We add a custom boot entry in GRUB2 in a slightly different way from GRUB. In GRUB2, instead of editing the actual /boot/grub/grub.cfg, we work with /etc/default/grub and /etc/grub.d. Let's do a listing of /etc/grub.d to see all of the available files:

philip@ubuntu:~$ ls -l /etc/grub.d/
total 76
-rwxr-xr-x 1 root root 9791 Apr 15 2016 00_header
-rwxr-xr-x 1 root root 6258 Mar 15 2016 05_debian_theme
-rwxr-xr-x 1 root root 12261 Apr 15 2016 10_linux
-rwxr-xr-x 1 root root 11082 Apr 15 2016 20_linux_xen
-rwxr-xr-x 1 root root 1992 Jan 28 2016 20_memtest86+
-rwxr-xr-x 1 root root 11692 Apr 15 2016 30_os-prober
-rwxr-xr-x 1 root root 1418 Apr 15 2016 30_uefi-firmware
-rwxr-xr-x 1 root root 214 Apr 15 2016 40_custom
-rwxr-xr-x 1 root root 216 Apr 15 2016 41_custom
-rw-r--r-- 1 root root 483 Apr 15 2016 README
philip@ubuntu:~$
Before you work with GRUB2, always make a backup copy of your /boot/grub/grub.cfg.

From the preceding output, we can see a number of files. Their names start with a number, and the numbers are read in sequential order. Let's say we want to add a custom boot entry in GRUB2. We are going to create a custom entry and name it /etc/grub/40_custom. We will see the following code in vi:

#!/bin/sh
exec tail -n +3 $0
# This file provides an easy way to add custom menu entries. Simply type the
# menu entries you want to add after this comment. Be careful not to change
# the 'exec tail' line above.
echo "Test Entry"
cat << EOF
menuentry "CompTIA_LINUX+" {
set root ='hd0,0'
}
EOF

From the preceding output, we can see that the syntax is a bit similar to programming. In GRUB2, it's an entire programming language. The next step is to save our changes, then run grub-mkconfig (the name implies we're talking about legacy GRUB, but we're actually referring to GRUB2). This depends on the Linux distribution. In CentOS 7, you will see commands that start with grub2:

root@ubuntu:/home/philip# grub-mkconfig
Generating grub configuration file ...
#
# DO NOT EDIT THIS FILE
#
# It is automatically generated by grub-mkconfig using templates
# from /etc/grub.d and settings from /etc/default/grub
#
### BEGIN /etc/grub.d/00_header ###
if [ -s $prefix/grubenv ]; then
set have_grubenv=true
load_env
fi

Some of the following output is omitted for the sake of brevity:

### BEGIN /etc/grub.d/40_custom ###
# This file provides an easy way to add custom menu entries. Simply type the
# menu entries you want to add after this comment. Be careful not to change
# the 'exec tail' line above.
echo "Test Entry"
cat << EOF
menuentry "CompTIA_LINUX+" {
set root ='hd0,0'
}
EOF

When we run this command, the grub-mkconfig command finds the custom entry. This then generates a new boot menu. Upon the next reboot of the system, we will see the new boot menu. We can also change options in /etc/default/grub, including options such as the default OS, the timer, and so on. Here is the content of /etc/default/grub:

root@ubuntu:/home/philip# cat /etc/default/grub
# If you change this file, run 'update-grub' afterwards to update
# /boot/grub/grub.cfg.
# For full documentation of the options in this file, see:
# info -f grub -n 'Simple configuration'
GRUB_DEFAULT=0
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"
GRUB_CMDLINE_LINUX="find_preseed=/preseed.cfg auto noprompt priority=critical locale=en_US"

Based on the preceding output, the timer value is set to 10. Also, note that there is a default value of 0. Continuing down the configuration file, we see the following code:

# Uncomment to enable BadRAM filtering, modify to suit your needs
# This works with Linux (no patch required) and with any kernel that obtains
# the memory map information from GRUB (GNU Mach, kernel of FreeBSD ...)
#GRUB_BADRAM="0x01234567,0xfefefefe,0x89abcdef,0xefefefef"
# Uncomment to disable graphical terminal (grub-pc only)
#GRUB_TERMINAL=console
# The resolution used on graphical terminal
# note that you can use only modes which your graphic card supports via VBE
# you can see them in real GRUB with the command `vbeinfo'
#GRUB_GFXMODE=640x480
# Uncomment if you don't want GRUB to pass "root=UUID=xxx" parameter to Linux
#GRUB_DISABLE_LINUX_UUID=true
# Uncomment to disable generation of recovery mode menu entries
#GRUB_DISABLE_RECOVERY="true"
# Uncomment to get a beep at grub start
#GRUB_INIT_TUNE="480 440 1"

Now, let's reboot our Ubuntu system and check out the GRUB2 boot menu:

From the preceding screenshot, we can now see our custom menu option in GRUB2. We can even scroll through the entries and edit them by pressing the key.

In GRUB2, the first hard disk starts with 0 and the first partition starts with 1, unlike in legacy GRUB.