Boot Partition – How Linux Manages a Separate /boot Partition

bootgrubpartition

I am interested to learn about how Linux deals with a separate boot partitions. I am not interested in actually doing this, but I would like to know how this works under-the-hood.

Consider a hard drive sda, which has two partitions sda1 and sda2. Let's say that sda2 is the root partition / which contains the Linux OS.

My understanding is that the bootloader GRUB2, is mounted to /boot. When the directory /boot is on a separate partition sda2, however, how is it that this can happen before / is actually mounted?

How does the interaction between the BIOS, the Master boot record and GRUB (or the files /boot) successfully happen in this case? Is it that the data in /boot is not actually mounted to the / filesystem at this early stage?

Note: this question deals with mounting the root partition, but does not discuss a separate boot partition.

Best Answer

Here is the problem in your understanding:

My understanding is that the bootloader GRUB2, is mounted to /boot.

GRUB is not "mounted" on boot. GRUB is installed to /boot, and is loaded from code in the Master Boot Record. Here is a simplified overview of the modern boot process, assuming a GNU/Linux distribution with an MBR/BIOS (not GPT/UEFI):

  1. The BIOS loads.
  2. The BIOS loads the small piece of code that is in the Master Boot Record.
  3. GRUB does not fit in 440 bytes, the size of the Master Boot Record. Therefore, the code that is loaded actually just parses the partition table, finds the /boot partition (which I believe is determined when you install GRUB to the Master Boot Record), and parses the filesystem information. It then loads Stage 2 GRUB. (This is where the simplification comes in.)
  4. Stage 2 GRUB loads everything it needs, including the GRUB configuration, then presents a menu (or not, depending on user configuration).
  5. A boot sequence is chosen. This could be by a timeout, by the user selecting a menu entry, or by booting a command list.
  6. The boot sequence starts executing. This can do a number of things - for example, loading a kernel, chainloading to another bootloader - but let's assume that the boot sequence is standard GNU/Linux.
  7. GRUB loads the Linux kernel.
  8. GRUB loads the initial ramdisk.
  9. The initial ramdisk mounts / under /new_root (possibly cryptographically unlocking it), starts udev, starts resume-from-swap, etc.
  10. The initial ramdisk uses the pivot_root utility to set /new_root as the real /.
  11. init starts. Partitions get mounted, daemons get started, and the system boots.

Notice how the kernel is only loaded at step 7. Because of this, there is no concept of mounting until step 7. This is why /boot has to be mounted again in step 9, even though GRUB has already used it.

It may also be of use to look at the GRUB 2 section of the Wikipedia page on GRUB.

Related Question