The process of booting up a Linux kernel can be view as 6 separate stages
Bios Selects Boot Device
On startup, power is sent to the CPU and it starts executing instructions. The first instruction it runs passes control to the BIOS (Basic Input/Output System). The BIOS then does two things:
POST operation
Selecting the First Boot Device
Bios Loads Boot Sector from the Boot Device
One the boot device has been identified, the BIOS passes control back to the CPU. The CPU will then read the first 512 bytes of the boot device to obtain the Master Boot Recorder (MBR). The MBR consists of the following information
Primary Boot Loader Code
Partition Table Information
Magic Number
GRUB Loader / Stage 2 boot Loader
If the CPU has a valid boot device, which has a valid MBR that contains the Primary boot loader code / Stage 1 Boot loader, we proceed to load the Stage 2 Boot loader, or the GRUB (Grand Unified Bootloader) into memory. The GRUB is located 30 KB away from the MBR and before the first partition.
The GRUB displays the available Kernels to the user. Once the user selects a kernel, the kernel image is then loaded into memory and control is passed to the kernel.
Nowadays GRUB2 is being used in place of GRUB. GRUB2 is the default boot loader for RHEL 7, Fedora, and Ubuntu. To read more about GRUB2
Kernel Decompression
The kernel is a compressed image, and it is store in /boot along with an initial RAM disk image, and device maps of the hard drives.
Once the kernel is loaded into the main memory, it begins decompression. After decompression, the expanded image overwrite the initial uncompressed image. Once the kernel has extracted itself, it loads systemd, which is the replacement for the old SysV init program, and turns control over to it.
At this point, the Linux kernel and systemd (or in some systems, init) are running
Low-level initialization (init, Upstart and Systemd)
Once the kernel has been decompressed, it begins the init function, which is the low level initialization of the kernel. The kernel looks at the /etc/inittab file and decides the run level. It also looks into the folder /etc/rc* and runs the startup scripts there. The files for init are stored in /etc/init
init is a daemon process which starts as soon as the computer starts and continues running till it is shutdown. init has a pid of 1. If somehow the init daemon could not start, no process will be started and the system will enter Kernel Panic.
init is going to be replaced by systemd in the future, as systemd offers a cleaner way of booting. Ubuntu used to adopt upstart as their initialization, but switched to systemd as of 9 March 2015.
The main changes of GRUB2 from GRUB are as follows:
There are 3 stages to the GRUB2 process:
* boot.img is stored in the master boot record (MBR) or in the Volume Boot Records (VBR). At installation time it is configured to load the first sector of core.img:
* Also known as stage 1.5.core.img is by default written to the sectors between the MBR and the first partition, when these sectors are free and available. Once executed, core.img will load its configuration file and any other modules and file systems needed, at installation time, it is generated from diskboot.img and configured to load stage 2 by its file path
* Also known as stage 2. Files belonging to the stage 2 are all being held in the /boot/grub
Once GRUB2 stage 2 has loaded, it presents a kernel selection menu for the user to select which operating system to boot. GRUB2 can be configured to automatically load a specified kernel after a user-defined timeout
Once boot options have been selected, GRUB2 loads the selected kernel into memory and passes control to the kernel. Alternatively, GRUB2 can pass control of the boot process to another boot loader, using chain loading.