Wednesday, August 19, 2015

Linux Boot Process



1. BIOS

Once the control goes to BIOS it will take care of two things

· Run POST operation.
•Selecting first Boot device.

POST operation: POST is a process of checking hardware availability. BIOS will have a list of all devices which are present in previous system boot. In order to check if hardware is available for the present booting or not it will send an electric pulse to each and every device in the list that it already have. If an electrical pulse is returned from that device it will come to a conclusion the hardware is working fine and ready for use. If it does not receive a single from a particular device it will treat that device as faulty or it was removed from the system. If any new hardware is attached to the system it will do the same operation to find if it’s available or not. The new list will be stored in BIOS memory for next boot.
Unlike the main RAM chip, CMOS RAM does not flush its memory when a computer is turned off. It remembers the entire configuration with the help of a battery called CMOS battery.



POST check will confirm the integrity of the following devices

◦Timer IC's
◦DMA controllers
◦CPU
◦Video ROM
◦Motherboard
◦Keyboard
◦Printer port
◦Hard Drive etc


Selecting First Boot Device: Once the POST is completed BIOS will have the list of devices available. BIOS memory will have the next steps details like what is the first boot device it has to select etc. It will select the first boot device and gives back the control to Processor (CPU). Suppose if it does not find first boot device, it will check for next boot device, if not third and so on. If BIOS do not find any boot device it will alert user stating "No boot device found".
Commands
To find BIOS details: #dmidecode -t 0



2. MBR

Once the BIOS gives control back to CPU, it will try to load MBR of the first boot device (We will consider it as HDD). MBR is a small part of Hard Disk with just a size of 512 Bytes, I repeat its just 512 Bytes. This MBR resides at the starting of HDD or end of HDD depending on manufacturer.

What is MBR?

MBR (Master Boot recorder) is a location on disk which has details about

· Primary boot loader code(This is of 446 Bytes)

· Partition table information(64 Bytes)

· Magic number(2 Bytes)

Which will be equal to 512B (446+64+2)B.

Primary Boot loader code: This code provides boot loader information and location details of actual boot loader code on the hard disk. This is helpful for CPU to load second stage of Boot loader.

Partition table: MBR contains 64 bytes of data which stores Partition table information such as what is the start and end of each partition, size of partition, type of partition (Whether it's a primary or extended etc). As we all know HDD support only 4 partitions, this is because of the limitation of its information in MBR. For a partition to represent in MBR, it requires 16 Bytes of space in it so at most we will get 4 partitions

Magic Number: The magic number service as validation check for MBR. If MBR gets corrupted this magic number is used to retrieve it.

Commands:
To see MBR details
[root@xxxxxxxx ~] #dd if=/dev/sda of=mbr.bin bs=512 count=1
[root@xxxxxxxx ~]# file mbr.bin

mbr.bin: x86 boot sector; GRand Unified Bootloader, stage1 version 0x3, boot drive 0x80, 1st sector stage2 0x41438, GRUB version 0.94; partition 1: ID=0x83, active, starthead 32, startsector 2048, 2097152 sectors; partition 2: ID=0x8e, starthead 170, startsector 2099200, 81786880 sectors, code offset 0x48


3. GRUB

GRUB stage 1:

The primary boot loader takes up less than 512 bytes of disk space in the MBR - too small a space to contain the instructions necessary to load a complex operating system.

Instead the primary boot loader performs the function of loading either the stage 1.5 or stage 2 boot loader.

GRUB Stage 1.5:

Stage 1 can load the stage 2 directly, but it is normally set up to load the stage 1.5.

This can happen when the /boot partition is situated beyond the 1024 cylinder head of the hard drive.

GRUB Stage 1.5 is located in the first 30 KB of Hard Disk immediately after MBR and before the first partition.

This space is utilized to store file system drivers and modules.

This enabled stage 1.5 to load stage 2 to load from any known location on the file system i.e. /boot/grub





GRUB Stage 2:

This is responsible for loading kernel from /boot/grub/grub.conf and any other modules needed

Loads a GUI interface i.e. splash image located at /grub/splash.xpm.gz with list of available kernels where you can manually select the kernel or else after the default timeout value the selected kernel will boot

The original file is /etc/grub.conf of which you can observe a symlink file at /boot/grub/grub.conf

Sample /boot/grub/grub.conf
default=0
timeout=5
splashimage=(hd0,0)/grub/splash.xpm.gz
hiddenmenu
title Red Hat Enterprise Linux Server (2.6.18-194.26.1.el5)
root (hd0,0)
kernel /vmlinuz-2.6.18-194.26.1.el5 ro root=/dev/VolGroup00/root clocksource=acpi_pm divisor=10
initrd /initrd-2.6.18-194.26.1.el5.img
title Red Hat Enterprise Linux Server (2.6.18-194.11.4.el5)
root (hd0,0)
kernel /vmlinuz-2.6.18-194.11.4.el5 ro root=/dev/VolGroup00/root clocksource=acpi_pm divisor=10
initrd /initrd-2.6.18-194.11.4.el5.img
title Red Hat Enterprise Linux Server (2.6.18-194.11.3.el5)
root (hd0,0)
kernel /vmlinuz-2.6.18-194.11.3.el5 ro root=/dev/VolGroup00/root clocksource=acpi_pm divisor=10
initrd /initrd-2.6.18-194.11.3.el5.img




4. Kernel
This can be considered the heart of operating system responsible for handling all system processes.

Kernel is loaded in the following stages:

1. Kernel as soon as it is loaded configures hardware and memory allocated to the system.

2. Next it uncompresses the initrd image (compressed using zlib into zImage or bzImage formats) and mounts it and loads all the necessary drivers.

3. Loading and unloading of kernel modules is done with the help of programs like insmod, and rmmod present in the initrd image.

4. Looks out for hard disk types be it a LVM or RAID.

5. Unmounts initrd image and frees up all the memory occupied by the disk image.

6. Then kernel mounts the root partition as specified in grub.conf as read-only.

7. Next it runs the init process

The initrd is created to make sure that drivers can load on your server.
Commands
To find kernel version in linux: #uname -r

5. Init

Looks at the /etc/inittab file to decide the Linux run level.

Following are the available run levels

0 - halt

1 – Single user mode

2 – Multiuser, without NFS

3 – Full multiuser mode

4 – unused

5 – X11

6 – Reboot

Init identifies the default initlevel from /etc/inittab and uses that to load all appropriate programs.

Execute ‘grep initdefault /etc/inittab’ on your system to identify the default run level

If you want to get into trouble, you can set the default run level to 0 or 6. Since you know what 0 and 6 means, probably you might not do that.

Typically you would set the default run level to either 3 or 5.

Next as per the fstab entry file system's integrity is checked and root partition is re-mounted as read-write (earlier it was mounted as read-only).










6. Runlevel programs
•When the Linux system is booting up, you might see various services getting started. For example, it might say “starting sendmail …. OK”. Those are the runlevel programs, executed from the run level directory as defined by your run level.
•Depending on your default init level setting, the system will execute the programs from one of the following directories.
◦Run level 0 – /etc/rc.d/rc0.d/
◦Run level 1 – /etc/rc.d/rc1.d/
◦Run level 2 – /etc/rc.d/rc2.d/
◦Run level 3 – /etc/rc.d/rc3.d/
◦Run level 4 – /etc/rc.d/rc4.d/
◦Run level 5 – /etc/rc.d/rc5.d/
◦Run level 6 – /etc/rc.d/rc6.d/
•Please note that there are also symbolic links available for these directory under /etc directly. So, /etc/rc0.d is linked to /etc/rc.d/rc0.d.
•Under the /etc/rc.d/rc*.d/ directories, you would see programs that start with S and K.
•Programs starts with S are used during startup. S for startup.
•Programs starts with K are used during shutdown. K for kill.
•There are numbers right next to S and K in the program names. Those are the sequence number in which the programs should be started or killed.
•For example, S12syslog is to start the syslog deamon, which has the sequence number of 12. S80sendmail is to start the sendmail daemon, which has the sequence number of 80. So, syslog program will be started before sendmail.


Mingetty :- sbin/mingetty

Ref:- http://www.golinuxhub.com/2014/03/step-by-step-linux-boot-process.html

No comments:

Post a Comment