How Linux kernel image is loaded at install time despite of no actual bootloader(GRUB) present on hard drive?

Before we move further let's understand how Linux boots up in brief:


  • As soon as power button pressed, motherboard gets the power through SMPS(Switch Mode Power Supply). SMPS converts from AC to DC voltages which motherboard requires.
  • Once motherboard is up, processor gets the power and it start execution from fixed address in EEPROM/Flash drive. This address typically contains a single jump instruction with actual BIOS(Basic Input Output System) firmware code in EEPROM/Flash drive so control goes to BIOS now
  • BIOS performs the POST(Power On Self Test) which means it checks whether basic hardware devices like Hard drive, Display, Keyboard, Mouse etc. are working fine or not
  • Once the POST is finished, BIOS finds the bootable partition on hard drive and hands over the system control to bootloader(GRUB mostly) present in boot partition of hard disk
  • Bootloader loads the kernel and initrd(Iniital RAM Disk). Initrd is minimal file system required to load root file system
  • Kernel then starts off first process "init" with PID(process ID) of 1. init process looks for the current run level(generally present in 'etc/inittab' file) and according to the current run level it executes scripts present in /etc/rc<current run level>.d directory. These scripts do some initialization, module loading etc. 
  • At last system gets into mode where user can use it. 

The above process is successfully carried out because a bootloader, kernel and other required files are already files on the hard drive of the system.



What if hard drive is blank or being used very first time, no bootloader present in disk, who loads linux kernel and initrd in this case?

Answer is SYSLINUX… 
SYSLINUX is the bootloader which operates off an FAT(MS-DOS) filesystem. SYSLINUX is designed to simplify the first-time installation of Linux, and for some rescue creation and other special-purpose boot disks.

To create a bootable disk using SYSLINUX, prepare a FAT formatted disk and copy kernel image(s) to it, and then run the following command from terminal,
syslinux -i /dev/sdb
-i or --install option overwrites any previously installed bootloader and installs the SYSLINUX on a provided disk device(sdb). This will modify the boot sector on the disk and copy a file named ldlinux.sys into its root directory.

On system boots, SYSLINUX will load the kernel named with LINUX which is present on the disk drive. This is the default setting. We can change default settings by providing syslinx configuration file(syslinux.cfg).

This is the reason when you first install linux on your system with bootable USB or CD, you could find couple of syslinux files present on your bootable USB or any other medium(CD). These files are responsible to load the kernel image and initrd at very first time and once the system is up it installs actual bootloader(GRUB) on the hard disk and from the next boot normal booting process is carried out which I explained above.

Notes :
  • SYSLINUX also supports the loading of initrd 
  • Like SYSLINUX, other similar bootloaders(ISOLINUX, PXELINUX) exist with some minimal differences
  • For configuring the syslinux.cfg, go through this link

Comments

Popular posts from this blog

MBR partitioning with 'parted' utility

Replace default splash screen in Yocto

Disk Partitioning: MBR vs GPT