2

When booting, is the Linux Kernel first loaded to the memory or the first the root file system is mounted? I have this doubt because the compressed kernel image is usually located in /boot/vmlinuz. So, to access the kernel image the root file system should be mounted. But since root file system will reside on the storage, how is it read from the storage?

sawdust
  • 16,103
  • 3
  • 40
  • 50
Poojan
  • 3,312
  • 4
  • 20
  • 23
  • See [`initrd`](http://en.wikipedia.org/wiki/Initrd). – Jonathon Reinhart Nov 11 '13 at 07:11
  • Thanks @JonathonReinhart. So, GRUB or LILO are able to interact with the storage and load initrd and kernel image files into memory? So, basically they can interact with the root file system to read these files from the root directory? – Poojan Nov 11 '13 at 07:20
  • 3
    *"to access the kernel image the root file system should be mounted"* -- A bootloader does not have to adhere to formal kernel procedures. It's only a **transient program** that performs quick & dirty operations. So the filesystem (or sometimes a raw device) is simply read, there is no elaborate `mount` procedure. Also a "root filesystem" is a kernel concept, not a bootloader concept. The bootloader is reading the kernel image from a filesystem on the **boot device**. There is no requirement that this boot device or filesystem has to be the kernel's rootfs. – sawdust Nov 11 '13 at 08:17

1 Answers1

0

Linux Kernel first loaded to the memory or the first the root file system is mounted?

To begin with, BIOS loads bootloader (assumimg GRUB) making use of its built-in routines to access the disk right from initial 512 Bytes of MBR of your disk and executes it. The code is loaded into RAM and executed, which doesn't require any file system understanding and ideally its a RAW code. In the next stage, boot loader loads /boot/grub/grub.cfg file and passes contents for execution. Below is my Linux GRUB config,

menuentry 'Ubuntu, with Linux 3.5.0-37-generic' --class ubuntu --class gnu-linux 
    --class gnu --class os {
    ...
    search --no-floppy --fs-uuid --set=root 925c0ccb-1532-4078-bee0-07acff8bc917
    linux   /vmlinuz-3.5.0-37-generic root=/dev/mapper/my_vg-root ro   quiet 
            splash $vt_handoff
    initrd  /initrd.img-3.5.0-37-generic
}
 Figure: 1


Linux:/boot/grub$ sudo blkid -U 925c0ccb-1532-4078-bee0-07acff8bc917
/dev/sda1


Linux:/boot/grub$ df -h 
Filesystem              Size  Used Avail Use% Mounted on
/dev/mapper/my_vg-root  455G  126G  307G  29% /
udev                    1.8G  4.0K  1.8G   1% /dev
tmpfs                   708M  980K  707M   1% /run
none                    5.0M     0  5.0M   0% /run/lock
none                    1.8G  408K  1.8G   1% /run/shm
/dev/sda1               228M  131M   85M  61% /boot

From above figure: 1, boot loader searches for a block Id number 925c0ccb-1532-4078-bee0-07acff8bc917 which is my boot device /dev/sda1 and mounted to /boot as shown in the figures. Therefore, at this final stage GRUB loads Linux kernel & initrd images looked under block device which is /boot to RAM using BIOS disk utilities. Usually BIOS runs in real mode of processor which can address only 2^20 bytes of RAM restricting to ~1MB. Now the kernel & initrd weigh above 1MB, loading them to RAM first kernel to 1MB, jump to protected mode and pass the loaded kernel to high memory and free the first 1MB of real mode. Once kernel is under run and initramfs loaded into RAM will finds the location of the file system it itself is on and passes that to the kernel as its root / file system.

Read Tim Jones article for more information on this.

Sunil Bojanapally
  • 12,528
  • 4
  • 33
  • 46