CentOS System Administration Essentials
上QQ阅读APP看书,第一时间看更新

When is the root filesystem not the root filesystem?

We now need to break down the menu entries within the file, identifying the core components so that we can understand how they relate to the system and, most importantly, how we can correct errors.

Editing stanzas in GRUB

Each entry in the GRUB menu is known as a stanza, and each stanza will start with the title word, containing three directives as follows:

  • root
  • kernel
  • initd

The title of the stanza also becomes the displayed item in the menu. Let's consider a stanza that begins with the following title:

title CentOS 6.5 OS

The menu will display CentOS 6.5 OS as the selectable item, and it is important to note that we do not add quotes around the text as they will also be displayed to the user. This is unless, of course, you want or need to display these quotes; we are most certainly not quote unfriendly at Packt Publishing!

Adding a root entry to a stanza

Directly following the stanza title will be a line that starts with the root directive. This identifies the root filesystem to GRUB and not the OS root; in simple terms, this should point to the partition that is marked as bootable in the partition table.

We can use the fdisk or parted command to display the bootable partition. If you are using the fdisk command to display the partition information, the command would be similar to the following where we want to list the partitions of the first hard drive within the system:

# fdisk -l /dev/sda

The partition marked as bootable will be identified with an asterisk mark. If you are using the parted command to display the partition table, you will be able to identify the bootable partition by the boot flag by executing the following command:

# parted /dev/sda print

Tip

The fdisk shows the bootable partition with * and parted with the word boot.

The bootable partition can be /boot or the actual root filesystem itself /. This relates to how the system was configured as it was installed. It might often be the case that /boot will have its own partition to ease access by the bootloader. The legacy GRUB, for example, cannot access a filesystem built on Logical Volume Management (LVM); this is the default partitioning proposal in CentOS 6. The same applies to software Redundant Array of Inexpensive Disks (RAID) arrays.

Consider the following stanza:

title CentOS 6.5 OS
  root (hd0,0)

From this, we can determine that GRUB should mount the first partition on the first drive (both the drive and partition numbering starts at 0) as the bootable partition.

To summarize, the root directive in a GRUB stanza indicates the partition that the MBR marks as bootable.

Adding a kernel entry to a stanza

The directive, kernel, directs the bootloader to the target operating system kernel. The path to that kernel will be related to the GRUB root partition, or the bootable partition. If the path reads /vmlinuz.version, then this would be an indication that the kernel is located at the root of the bootable partition, whereas the path /boot/vmlinuz.version would indicate that the bootable partition is the Linux or OS root partition. The path has to include the /boot directory to be able to locate the kernel.

Following the filename of the kernel are the arguments used when loading the kernel, or more simply referred to as the kernel options. These options include, among others, the device name where the real root filesystem is located and the device name for the swap filesystem, which can be used to suspend the system, perhaps on a laptop build. An example of the OS root option would be root=/dev/sda2; this being the second partition on the first hard drive or root=/dev/mapper/vg_centos-vg_root. This indicates that the operating system root is built upon an LVM. The swap filesystem to be suspended is indicated by the resume option.

The following extract from a stanza indicates that the boot partition is /dev/sda1 (hd0,0) and the operating system root is /dev/sda2, with the swap located on /dev/sda3:

title CentOS 6.5 OS
  root (hd0,0)
  kernel /vmlinuz.version root=/dev/sda2 resume=/dev/sda3

If the OS root is also the bootable partition, the corresponding GRUB stanza would read similar to the following:

title CentOS 6.5 OS
  root (hd0,0)
  kernel /boot/vmlinuz.version root=/dev/sda1 resume=/dev/sda2

We can see that the path to the kernel is now the full operating system path and both the GRUB root and the OS root correspond to the same partition.

Given a running system where the boot process is completed and we are logged in, it is possible to view the version of the kernel with either of the following commands:

  • $ cat /proc/version
  • $ uname –r

You should look at both commands and see which one best suits your needs; the /proc/version file will give a little more information. However, the uname -r command summarizes the information well. This is your system and it is your choice.

Should we need to list the options with which the kernel was booted, we can display those options with the following command:

$ cat /proc/cmdline

By this stage, I am hoping you have a little more understanding of when the root filesystem may not actually be the root filesystem and when it can be the root filesystem. You are now ready to use this riddle anytime that you wish to confuse your colleagues. It really is a simple matter of knowing where the partition that holds the kernel is; this then becomes the root of the bootable partition. The OS root is what we normally think as of the root filesystem but this happens only once the system has completed the boot process. The kernel directive simply points to the kernel file with a path relative to the root of the boot partition along with any options that we may wish to pass through to the kernel when it is loaded.

Tip

The /proc directory is a pseudo filesystem, meaning that it is transient and resides only in the RAM. It contains up-to-date information for the currently running system. This directory is worth becoming acquainted with.

Adding an initrd entry to a stanza

Similar to the kernel directive, the initrd directive will point to the initialization RAM disk; a mini OS that is compiled with the drivers needed to access the OS root filesystem. The RAM disk loads prior to the kernel and mounts the OS root filesystem as read-only. Filesystem integrity checks are performed before handing it to the kernel to continue with the boot process and mounting as read/write. This means that the kernel does not have to have the drivers for the root filesystem internally compiled, allowing more flexibility in changes to the OS root and a more lean kernel. The RAM disk can be recompiled if the root filesystem changes or the drivers need to access the hardware change with the mkinitrd command.

Continuing with our example stanza, we can insert a line for the initrd directive to read as follows:

title CentOS 6.5 OS
  root (hd0,0)
  kernel /boot/vmlinuz.version root=/dev/sda1 resume=/dev/sda2
  initrd /boot/initramfs.version

Not wishing to be out performed by the preceding simple text, the following screenshot shows an extract from a real GRUB stanza on my CentOS 6.5 system.