Linux Startup Process Analysis

Source: Internet
Author: User

The analysis of the Linux Startup Process can help those who want to learn more about the Linux Startup Process, and further study how Linux works next.

Linux boot process is as follows: when the user powers on the PC, BIOS boot self-check, start according to the boot device set in BIOS (usually hard disk), then start the boot device installed on the boot deviceProgramLilo or GRUB starts to guide Linux. Linux first guides the kernel, then runs the INIT program, and the INIT program calls RC. sysinit and RC programs, RC. after completing system initialization and running service tasks, sysinit and RC return Init; init starts mingetty and opens the terminal for users to log on to the system. After the user logs on to the system, the user enters shell, this completes the entire startup process from boot to login.

Next we will introduce the Linux Startup process step by step:

1) BIOS self-check

When you turn on the computer power, the computer will first load the BIOS information, BIOS information is so important that the computer must find it at the very beginning. This is because the BIOS contains CPU information, device startup sequence information, hard disk information, memory information, clock information, and PNP features. After that, the computer will be aware of which hardware device should be read.

After the computer is powered on, the BIOS first conducts self-check, that is, the so-called post (power on self test ), then, read the boot block from the hard disk, floppy disk, or CDROM according to the boot sequence set in the BIOS ". In PC, boot Linux starts from the BIOS address 0xffff0. The first step of BIOS is power-on self-check (post ). Post checks the hardware. The second step of BIOS is to enumerate and initialize the local device. Given the different usage of BIOS functions, BIOS consists of two parts: PostCodeAnd runtime services. After the post is completed, it is cleared from the memory, but the service is still kept in the memory during the BIOS runtime, which can be used by the target operating system.

To Boot an operating system, the BIOS searches active and bootable devices in the order defined by the CMOS settings during runtime. The boot device can be a floppy disk, a CD-Rom, a partition on the hard disk, a device on the network, or even a USB flash memory. Generally, Linux boot is from the hard disk. The primary Boot Record (MBR) contains the primary boot loader.

As we all know, the first sector of the 0th track on the hard disk is called MBR, that is, Master Boot Record, that is, the Master Boot Record. Its size is 512 bytes, you can store the pre-start information and partition table information. After the system finds the MBR of the hard disk specified by the bios, it will copy it to the physical memory where the 0 × 7c00 address is located. When the MBR is loaded into RAM, the BIOS will hand over the control to the MBR. In fact, the content copied to the physical memory is the boot loader, and to your computer, it is LILO or grub.

Extract MBR Information

To view the MBR content, use the following command:

# Read the content of the first 512 bytes from/dev/SDA and write it into the MBR. binfile.

Linux "> root @ farsight:/home/Linux # dd If =/dev/SDA of = MBR. Bin BS = 512 COUNT = 1

1 + 0 records in

1 + 0 records out

512 bytes (512 B) Copied, 0.000798615 seconds, 641 KB/s

# Print the content of the binary file in hexadecimal format and ASCII format

Root @ farsight:/home/Linux # OD-Xa MBR. Bin

0000000 48eb 1090 d08e 00bc b8b0 0000 d88e c08e

K h dle so P <NUL 0 8 NUL so X so @

0000020 befb 7c00 00bf b906 0200 a4f3 21ea 0006

{> NUL |? Nul ack 9 nul stx s $ J! Ack NUL

0000040 be00 07be 0438 0b75 c683 8110 Fefe 7507

Nul> Bel 8 eot u vt etx f dle Soh ~ ~ Bel u

......

This dd Command needs to be run as the root user. It reads the content of the first 512 bytes from/dev/hda (the first IDE disk) and writes it to MBR. BIN file. The OD command prints the contents of the binary file in hexadecimal format and ASCII format.

2) Start grub/lilo

Boot Loader is a small program that runs before the operating system kernel runs. Through this small program, we can initialize hardware devices and build a map of memory space to bring the system's hardware and software environment to a suitable state, in order to make all preparations for the final call to the operating system kernel.

There are several boot loaders, among which grub, Lilo, and spfdisk are common loaders.

Grub and lilo are both boot loaders that guide the operating system. When the machine directs its operating system, the BIOS reads the first 512 bytes (MBR: Master Boot Record) on the boot media ). In a single MBR, only one operating system boot record can be stored. Therefore, when multiple operating systems are required, problems may occur. Therefore, more flexible boot loader is required.

Comparison between grub and Lilo

All boot loaders work in a similar way to achieve a common purpose. However, there are many differences between Lilo and GRUB:

L lilo does not have an interactive command interface, but grub does.

L lilo does not support network boot, but grub does.

L lilo physically stores information about the operating system locations that can be booted in MBR. If you have modified the Lilo configuration file, you must rewrite the first-stage boot loader of lilo to MBR. Compared with grub, this is a more dangerous option, because wrong configuration of MBR may make the system unable to boot. If grub is used, if the configuration file is incorrectly configured, it is only forwarded to the grub command line interface by default.

Security tips:

For security, anyone who can access the boot disk/CD can bypass all the security measures mentioned in this article by using grub. conf or Lilo. conf without security settings. Especially when grub is used, it is a serious security vulnerability because it can be guided to the single-user mode. One easy way to solve this problem is to disable boot through CD and floppy disk in the BIOS of the machine, and ensure that a password is set for the bios so that others cannot modify these settings.

Let's take GRUB as an example. The System reads grub configuration information (generally menu. lst or grub. lst) in the memory and starts different operating systems according to the configuration information.

3) load the kernel

When the kernel image is loaded into the memory, the kernel stage begins. The kernel image is not an executable kernel, but a compressed kernel image. Generally, it is a zimage (compressed image, smaller than KB) or bzimage (larger compressed image, larger than KB), which is compressed using zlib in advance. In front of this kernel image is a routine that implements a small amount of hardware settings, decompress the kernel contained in the kernel image, and then put it into the high-end memory, if an initial RAM disk image exists, it will be moved to the memory and marked for future use. This routine then calls the kernel and starts the kernel boot process.

In the grub command, we can use the initrd image to boot a specific kernel. The method is as follows:

Grub> kernel/bzImage-2.6.14.2

[Linux-bzimage, set = 0x1400, size = 0x29672e]

Grub> initrd/initrd-2.6.14.2.img

[Linux-initrd @ 0x5f13000, 0xcc199 bytes]

Grub> boot

Uncompressing Linux... OK, booting the kernel.

Based on the path of the kernel image set by grub, the system reads the memory image and decompress it. At this time, the screen will usually output the "Uncompressing Linux" prompt. After the kernel is decompressed, the screen outputs "OK, booting the kernel ". If you do not know the name of the kernel to boot, you only need to use/and press the tab key to display the list of kernels and initrd images.

The system places the decompressed kernel in the memory, and calls the start_kernel () function to start a series of initialization functions and initialize various devices to complete the establishment of the Linux core environment. So far, the Linux kernel has been established, and the Linux-based program should be able to run normally.

3) execute the INIT process

The INIT process is the starting point of all processes. After kernel boot is completed, the kernel loads the INIT program in the current thread (process) space. Its Process number is 1. The INIT process is the initiator and controller of all processes. In any UNIX-based system (such as Linux), The INIT process ID (PID) is always 1. If there is a problem with init, the rest of the system will collapse.

The INIT process has two functions:

L play the role of ending the parent process: All orphan processes will be taken over by the INIT process. Run the PS-AF command to list the processes whose parent process ID (ppid) is 1.

L run the corresponding program at a specific running level to manage various running levels. This function is defined by the/etc/inittab file. After the kernel is loaded, the first program to run is/sbin/init, which reads the/etc/inittab file and initializes the file based on the file.

5) use the/etc/inittab file for initialization.

The INIT process initializes the system according to/etc/inittab, such as setting the keyboard, Font, loading module, and network. The main function of Linux is to set the running level of Linux. The setting format is ": ID: 5: initdefault:", which indicates that Linux needs to run on level 5. The Linux operating level is set as follows:

0: Shutdown

1: single-user mode

2: multi-user mode without network support

3: multi-user mode with network support

4: reserved, not used

5. Multi-user mode with network support and X-window support

6. reboot the system.

For RedHat, execute in the following order:

A) execute/etc/rc. d/rc. sysinit (the first script executed by init)

After the running level is set, the first user-layer file executed by Linux is/etc/rc. d/RC. the sysinit script program does a lot of work, including setting path and network configuration (/etc/sysconfig/network) start swap partition, set/proc, input the root file system to the mtab, use the system to prepare for loading the module, find the relevant files of the module, and check the file system, to perform necessary repairs, load all other file systems, and clear several/etc files, such as/etc/mtab,/etc/fastboot and/etc/nologin, delete uucp lock files, delete outdated subsystem files, delete outdated PID files, set system clock, and enable Switching initialize the serial port and mount the module. If you are interested, you can check the RC. sysinit file in/etc/rc. d. The script in it is enough for a few days.

B) run/etc/rc. d/rcX. d [Ks].

Terminate a service starting with K (used to disable a service), and then start a service starting with S (used to start a service). For each running level, at/etc/rc. the sub-Directory D contains a corresponding sub-directory. RcX. D is used to name sub-directories at the running level. X indicates the number at the running level. In each sub-directory of the running level, a symbolic link is established to the command script program in the/etc/rc. d/init. d sub-directory. The name of the link has a number after K and S, indicating the execution order, the small number of first execution such as K01tog-pegasus, s00microcode_ctl. When executing a script starting with K, the system will pass the stop parameter, while the script system starting with S will pass the start parameter.

C) run/etc/rc. d/rc. Local.

In RedHat Running Mode 2, 3, 5, put/etc/rc. d/RC. local is the last file in the initialization script, so you can add some commands that need to be executed before logging on to this file.

If you open this file, there is a sentence in it. After reading it, you will see the role of this command at a Glance:

# This script will be executed * after * all the other init scripts.

# You can put your own initialization stuff in here if you don't want to do the full sys V style init stuff.

RC. Local is a place for users to personalize Linux after all initialization work. You can put what you want to set and start here.

6) execute/bin/login

The login program will prompt the user to enter the account and password, then encode and confirm the correctness of the password. If the two are the same, initialize the environment for the user and give control to shell, that is, the user logs on.

Original text from [than the Internet], reproduced Please retain the original link: http://soft.chinabyte.com/ OS /232/12129232.shtml

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.