"Reprint" Linux system startup process

Source: Internet
Author: User

Original:Linux system startup process

POST (Power On Self test/)-->bootloader (MBR)-->kernel (Hardware probe, load driver, mount root file system,/sbin/init)-->init (/etc/ Inittab: Set default level, System init script, start and close the corresponding level of service, start terminal)
1th Step

After the computer power is switched on, the CPU default execution 0ffffh:0000h instruction (8086 is so, 386 should be similar), and this memory address should be stored BIOS ROM . The system will have a procedure for inspecting the internal devices, which is done by a program that is commonly called post (Power On Self test/), which is also a function of the BIOS program. The complete self-test includes tests on CPUs, 640K of basic memory, expanded memory above 1M, ROM, motherboard, CMOS memory, serial and display cards, soft and hard plate systems and keyboards. If a problem is found during the self-test, the system will give a message or whistle warning.

If there are no problems, the BIOS will search for a valid boot drive such as soft, hard drive and CDROM, network server, etc. in the boot order of the system CMOS setup, read the operating system Boot record (BootLoader), and then give the system control to the boot record. The boot record completes the system startup. If a bootable device has not been found, the start declaration fails.

2nd Step

In the previous step, the BIOS found the MBR of the hard disk (the 0 track 0 sector size of the hard disk is 512 bytes, the area cannot be assigned to any partition), and then look for bootloader in the MBR (currently more commonly used have Lilo and Grub,lilo are not commonly used, Bootloader occupies 446 bytes in the MBR, so it must be short and short, the next 64 bytes is the partition table information, the last 2 bytes are used to indicate whether the MBR is valid, and then load the bootloader into memory to begin execution, The main function of bootloader is to find kernel files from hard disk and load kernel files into memory execution.

The three parts of the MBR are as follows:

    • 第1-446 bytes: BootLoader.
    • 第447-510 Bytes: Partitioned Tables (Partition table).
    • 第511-512 bytes: Master boot Record signature (0x55 and 0xAA).

Expand:

The partition table is only 64 bytes long, and it is divided into four items, each 16 bytes. Therefore, a hard disk can only be divided into four primary partitions, also known as the "main partition."

16 bytes per primary partition, consisting of 6 parts:

    • 1th byte: If 0x80, it means that the primary partition is an active partition and control is transferred to this partition. Only one of the four primary partitions is active.
    • 第2-4个 Bytes: The physical location of the first sector of the primary partition (cylinder, head, sector area code, and so on).
    • 5th byte: Primary partition type.
    • 第6-8个 Bytes: The physical location of the last sector of the primary partition.
    • 第9-12 byte: The logical address of the first sector of the primary partition.
    • 第13-16 Bytes: The total number of sectors for the primary partition.

Questions? Where is the kernel file? How does grub find the kernel file?

The kernel file (VMLINUZ-2.6.18-308.EL5) is located under the/boot partition (when we partition the drive, the/boot is partitioned separately), then there is a question, this time/is not mounted, and how to find the kernel file from the hard disk?

At this point the Grub configuration file/boot/grub/grub.conf can see root (hd0,0), this line is actually the location of the specified boot directory

Kernel/vmlinuz-2.6.18-308.el5 ro root=label=/Here Specifies the location of the kernel file, and the front/not the real root, but refers to the location of the boot directory, then its full path is (hd0,0)/ Vmlinuz-2.6.18-308.el5, while here (hd0,0) refers to the 1th partition of the 1th hard drive, Grub recognizes the beginning of HD when it recognizes the hard drive.

Summary: GRUB is not the file system to find the kernel files, because the kernel is not started so there is no file system, but direct access to the hard disk 1th hard disk 1th partition (MBR exists partition table) to find the kernel file.

And then there's a problem. How does grub identify the file systems of these partitions in the partition table? And look at the files under the/boot/grub directory:

[[email protected] grub]# LL Total 257-rw-r--r--1 root root 2013-01-05 device.map-rw-r--r--1 root root 7584 2013-01-05 e2fs_stage1_5-rw-r--r--1 root root 7456 2013-01-05 fat_stage1_5-rw-r--r--1 root root 6720 2013-01-05 ffs_stage1_5-rw-------1 root root 562 2013-01-05 grub.conf-rw-r--r--1 root root 6720 2013-01-05 iso9660_stage1_5-rw-r--r --1 root root 8192 2013-01-05 jfs_stage1_5lrwxrwxrwx 1 root root one 2013-01-05 menu.lst./grub.conf-rw-r--r--1 Root root 6880 2013-01-05 minix_stage1_5-rw-r--r--1 root root 9248 2013-01-05 reiserfs_stage1_5-rw-r--r--1 root Root 55808 2009-03-13 splash.xpm.gz-rw-r--r--1 root root 2013-01-05 stage1-rw-r--r--1 root root 104988 2013 -01-05 Stage2-rw-r--r--1 root root 7072 2013-01-05 ufs2_stage1_5-rw-r--r--1 root root 6272 2013-01-05 Vstafs_sta Ge1_5-rw-r--r--1 root root 8904 2013-01-05 xfs_stage1_5          

In fact grub Boot is staged:

The 1th stage of the BIOS loading of the MBR inside the grub (belongs to the 1th stage of the file), because only grub occupies only 446 bytes so can not achieve too much functionality, so there is this stage of the file to load phase 1.5 of the file (/boot/grub file).

Phase 1.5 phase of this stage is to load the recognition file system program, to identify the file system, do not load the file system can not be recognized, and then can not find the boot directory, because GRUB is not recognized LVM, so you cannot set the/boot partition as LVM, so you must Boot separate partition.

2nd Stage This is the process of starting the kernel, and then the boot kernel.

3rd Step

In the previous step, grub successfully found the kernel file, loaded the kernel into memory, and loaded the/boot/initrd-2.6.18-308.el5.img file as well, what did the file do?

So first look at what the kernel does in this step:

    1. Probing hardware
    2. Load Driver
    3. Mount the root file system
    4. Executes the first program/sbin/init

The BIOS checks the hardware, and the kernel initializes the hardware, then the hardware is probed first (step 1th), knowing what hardware to load the hardware driver (step 2nd), otherwise there is no way to direct the hardware to work, the key is where the kernel to find the driver (the driver is the hard disk, is the kernel module. KO exists) and this time the root file system is not mounted, what should I do? That can ②③ swap, first mount the root file system, and then load the driver, then there is a problem, I do not load the driver and how to drive the hard work? This is caught in the first egg or the first chicken problem, how to solve?

This time the file/boot/initrd-2.6.18-308.el5.img (the file is a. gz compressed file) comes in handy, this file is also the grub loaded in memory, built into a virtual root file system, which contains a hardware driver () , this file can be expanded as follows:

[Root@server1 Boot]# CP Initrd-2.6.18-308.el5.img ~[root@server1 Boot]# Cd[root@server1 ~]# lsinitrd-2.6.18-308.el5.img[root@server1 ~]# MV Initrd-2.6.18-308.el5.img Initrd-2.6.18-308.el5.img.gz[root@server1 ~]# gzip-d Initrd-2.6.18-308.el5.img.gz[root@server1 ~]# lsinitrd-2.6.18-308.el5.img[root@server1 ~]# file initrd-2.6.18-308.el5.imginitrd-2.6.18-308.el5.img:ascii Cpio Archive (SVR4 withNo CRC) You can see that this is an archive file for Cpio [root@server1 ~]# mkdir Test[root@server1 ~]# MV Initrd-2.6.18-308.el5.img Test[root@server1 ~]# CD Test[root@server1 Test]# lsinitrd-2.6.18-308.el5.img[root@server1 Test]# Cpio-id < initrd-2.6.18-308.el5.img use Cpio to expand the file12111 blocks[root @server1 test]# lsbin Dev etc Init initrd-2.6. 18-308.el5.img lib proc sbin sys sysroot[root @server1 test]# mv Initrd-2.6.18-308.el5.img. /[root @server1 test]# ls can you see it's like a real root? bin Dev etc init lib proc sbin Sys Sysroot[root @server1 test]# ls lib/ You can see this directory contains the Ext3.ko kernel module, the module can drive the hard drive to work Ata_piix.ko Dm-mod.ko Ext3.ko Mptbase.ko scsi_mod.kodm- log.ko dm-raid45.ko firmware/mptscsih.ko Scsi_transport_spi.kodm-mem-cache.ko dm-region_ Hash.ko Jbd.ko Mptspi.ko Sd_mod.kodm-message.ko ehci-hcd.ko Libata.ko Ohci-hcd.ko uhci-hcd.ko[root  @server1 test] #                

At this point the kernel utilizes the virtual root filesystem's Ext3.ko kernel module, drives the hard disk, then mounts the real root file system, then whether the virtual root file system still has the function, it may also mount the/proc file system and so on operation.

The last step in this phase starts with the kernel to start the first program/sbin/init, and the rest of the work is completed after the INIT process is started.

4th step

The INIT process first reads the/etc/inittab file and executes it sequentially according to the contents of the Inittab file

Set the default level for system operation (Id:3:initdefault:)

Execute the System initialization script file (si::sysinit:/etc/rc.d/rc.sysinit)

Perform the corresponding service to start or close at this runlevel (L3:3:WAIT:/ETC/RC.D/RC 3)

Start 6 Virtual Terminals:

    • L0:0:WAIT:/ETC/RC.D/RC 0
    • L1:1:WAIT:/ETC/RC.D/RC 1
    • L2:2:WAIT:/ETC/RC.D/RC 2
    • L3:3:WAIT:/ETC/RC.D/RC 3
    • L4:4:WAIT:/ETC/RC.D/RC 4
    • L5:5:WAIT:/ETC/RC.D/RC 5
    • L6:6:WAIT:/ETC/RC.D/RC 6

"Reprint" Linux system startup process

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.