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

Working with GRUB

Now we're going to interact with GRUB. We will add a custom boot entry. This will be presented upon reboot.

Before you work with GRUB, always make a backup copy of your  /boot/grub/grub.conf .

We will use the vi command, which will open /boot/grub/grub.conf in the visual editor:

[root@localhost ~]# cat /boot/grub/grub.conf
# grub.conf generated by anaconda
#
# Note that you do not have to rerun grub after making changes to this file
# NOTICE: You have a /boot partition. This means that
# all kernel and initrd paths are relative to /boot/, eg.
# root (hd0,0)
# kernel /vmlinuz-version ro root=/dev/sda2
# initrd /initrd-[generic-]version.img
#boot=/dev/sda
default=0
timeout=5
splashimage=(hd0,0)/grub/splash.xpm.gz
hiddenmenu
title CentOS (2.6.32-431.el6.x86_64)
root (hd0,0)
kernel /vmlinuz-2.6.32-431.el6.x86_64 ro root=UUID=05527d71-25b6-4931-a3bb-8fe505f3fa64 rd_NO_LUKS rd_NO_LVM LANG=en_US.UTF-8 rd_NO_MD SYSFONT=latarcyrheb-sun16 crashkernel=auto KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM rhgb quiet
initrd /initramfs-2.6.32-431.el6.x86_64.img
[root@localhost ~]# vi /boot/grub/grub.conf

Now we're inside vi, we will press I on the keyboard to enter the insert mode, scroll down using the down-arrow key until we reach the last line, and then press Enter to go to a new line: 

# grub.conf generated by anaconda
#
# Note that you do not have to rerun grub after making changes to this file
# NOTICE: You have a /boot partition. This means that
# all kernel and initrd paths are relative to /boot/, eg.
# root (hd0,0)
# kernel /vmlinuz-version ro root=/dev/sda2
# initrd /initrd-[generic-]version.img
#boot=/dev/sda
default=0
timeout=5
splashimage=(hd0,0)/grub/splash.xpm.gz
hiddenmenu
title CentOS (2.6.32-431.el6.x86_64)
root (hd0,0)
kernel /vmlinuz-2.6.32-431.el6.x86_64 ro root=UUID=05527d71-25b6-4931-a3bb-8fe505f3fa64 rd_NO_LUKS rd_NO_LVM LANG=en_US.UTF-8 rd_NO_MD SYSFONT=latarcyrheb-sun16 crashkernel=auto KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM rhgb quiet
initrd /initramfs-2.6.32-431.el6.x86_64.img
~
~
~
-- INSERT --

Next, we will start our entry using the following keywords: title, root, kernel, and initrd. We will insert our own custom values, as shown here:

# grub.conf generated by anaconda
#
# Note that you do not have to rerun grub after making changes to this file
# NOTICE: You have a /boot partition. This means that
# all kernel and initrd paths are relative to /boot/, eg.
# root (hd0,0)
# kernel /vmlinuz-version ro root=/dev/sda2
# initrd /initrd-[generic-]version.img
#boot=/dev/sda
default=0
timeout=5
splashimage=(hd0,0)/grub/splash.xpm.gz
hiddenmenu
title CentOS (2.6.32-431.el6.x86_64)
root (hd0,0)
kernel /vmlinuz-2.6.32-431.el6.x86_64 ro root=UUID=05527d71-25b6-4931-a3bb-8fe505f3fa64 rd_NO_LUKS rd_NO_LVM LANG=en_US.UTF-8 rd_NO_MD SYSFONT=latarcyrheb-sun16 crashkernel=auto KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM rhgb quiet
initrd /initramfs-2.6.32-431.el6.x86_64.img
title CompTIA Linux+ (Our.Custom.Entry)
root (hd0,0)
kernel /vmlinuz-2.6.32-431.el6.x86 ro
initrd /initramfs-2.6.32-431.el6.x86_64.img
-- INSERT --

Now we will save and exit vi. We use :wq to save our change(s) and exit vi:

title CompTIA Linux+ (Our.Custom.Entry)
root (hd0,0)
kernel /vmlinuz-2.6.32-431.el6.x86 ro
initrd /initramfs-2.6.32-431.el6.x86_64.img
:wq

Based on the preceding output, here is a breakdown of our custom entry:

  • The title defines our customer boot entry. 
  • root (hd0,0) tells it to search for the first hard disk and the first partition on the first hard disk.
  • The kernel /vmlinuz-2.6.32-431.el6.x86 ro tells GRUB to look for the location of the Linux kernel. In this case, it's vmlinuz-2.6.32-431.el6.x86 ro (ro means it loads the kernel as read-only).
  • inidrd /initramfs-2.6.32-431.el6.x86_64.img specifies the initial RAM disk file to use (this aids the system boot up).

The last step is to reboot our CentOS system and be presented with the GRUB boot menu:

From the preceding output, we can see that our new custom boot entry is displayed in GRUB, which is awesome. We can interact in real time at the GRUB menu. Let's say we wanted to tag on or remove an option from one of these entries. To do this, we would simply press the E key, as shown here:

Now we can press the E key once again to edit the entry. Let's say we want to specify that the root filesystem resides in /dev/; we would do this as shown in the following screenshot:

Now, we can press the Enter key, which will save our changes, and the Esc key to return to the previous screen. Through this, we will see the new option added:

From the preceding output, we can see how easy it is to work in real-time at the GRUB boot menu and also how to add a custom boot entry in GRUB.

In GRUB, the first hard disk and the first partition are identified as (hd0, 0), whereas in the Linux shell, the first hard disk and first partition is identified as (sda1).