Linux Startup Process

Source: Internet
Author: User

The entire Linux system startup process is as follows:

 

Linux Boot Step

Start

BIOS

 

 

Grub/lilo

 

 

Kernel boot

 

 

Init

Rc. sysinit

 

 

Rc

 

Mingetty

Login

 

Shell

 

 

Logon System

 

 

I. BIOS self-check

After the computer is powered on, the BIOS first performs POST self-check, and then reads the boot block from the hard disk, floppy disk, or CDROM according to the boot sequence set in the BIOS. In Linux, the IP address oxFFFF0 in the human BIOS starts to boot. The second step of BIOS is to enable POST self-check. POST checks the hardware. The first step of BIOS is to enumerate and initialize the local device. BIOS consists of two parts: POST code and runtime services. After the POST is completed, it is cleared from the memory, but the service is kept in the memory during the BIOS runtime, which can be used by the target operating system.

During BIOS running, the active and bootable devices are searched according to the sequence defined by the CMOS settings. 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 systems boot from hard disks. The primary Boot Record (MBR) contains the primary boot loader. MBR is a 512-byte sector located in the first sector (0-way, 0-way, 1-sector) of the disk ). When the MBR is loaded into RAM, the BIOS will hand over the control to the MBR.

To view the MBR content, run the following command as the root user:

# Dd if =/dev/had of = mbr. bin bs = 512 count = 1
Read 1 + 0 blocks
Output 1 + 0 blocks
# Od-xa mbr. bin
... ...

It reads the first 512 bytes from/dev/had (the first IDE disk) and writes it to the mbr. binfile. The od command prints the contents of the binary file in hexadecimal format and ASCII format.

 

2. Start GRUB/LILO

GRUB and LILO are both boot loaders. The bootstrap loader is used to boot the operating system. When the machine directs its operating system, the BIOS reads the first 512 bytes (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 and more flexible boot loader is required.

All boot loaders work in a similar way for a common purpose, but there are many differences between LILO and GRUB:

LILO does not have an interactive command interface, but GRUB does;
LILO does not support network boot, while GRUB does;
LILO stores information about the boot operating system 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. When GRUB is used, if the configuration file is incorrectly configured, it is only forwarded to the GRUB command line interface by default.

 

3. Load the kernel

The next step is to load the kernel image to the memory. The kernel image is not an executable kernel, but a compressed kernel image. Generally, it is a zImage (compressed image, smaller than KB) or a bzImage (larger compressed image, larger than KB). It 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 high-end memory. If you have an initial RAM disk image, the system will move it to the memory and indicate it will be used later. The routine then calls the kernel and starts the kernel boot process.

 

4. Execute the init process

The init process is the starting point of all processes in the system. After the kernel completes the kernel boot, it loads the init program in the process space. Its process is 1. The Init process is the initiator and controller of all processes. In any Linux-based system, it is the first running process, so the init process ID (PID) is always 1.

The init process has the following two functions.

The first role of the init process is to play the role of ending the parent process. Because the init process will never be terminated, the system can always be sure of its existence and take it as a reference when necessary. If a process is terminated before the end of all its child processes, the init must be used as a reference. At this time, all child processes that have lost their parent processes will use init as their parent processes.

The second role of init is to run the corresponding program at a specific running level to manage various running levels. This function is defined by the/etc/inittab file.

 

Wu. initialize through the/etc/inittab File

Init executes the corresponding script based on/etc/inittab for system initialization, such as setting the keyboard, Font, loading module, and network.

1./Etc/rc. d/rc. sysinit

The init configuration file contains the following line:
Si: sysinit:/etc/rc. d/rc. sysinit

Rc. sysinit is the first script executed by init. It mainly performs system initialization. Rc. sysinit is an important script to run at each running level. It mainly includes activating swap partitions, checking disks, loading hardware modules, and other tasks that require priority. /Etc/rc. d/rc. sysinit mainly completes the same initialization work in each running mode. Including:

Set the initial $ PATH variable;
Configure the network;
Enable swap for virtual memory;
The Host Name of the coordination system;
Check the root file system for necessary repairs;
Check the quota of the root file system;
Quota of users and groups opened for the root file system;
Reload the root file system in read/write mode;
Clear the mounted file system table/etc/mtab;
Input the root file system to mtab;
Prepare the system for Loading modules;
Find related files of the module;
Check the file system for necessary repairs;
Load all other file systems;
Clear/etc/mtab,/etc/fastboot, and/etc/nologin;
Delete UUCP and lock files;
Delete outdated subsystem files;
Delete Obsolete pid files;
Set the system clock;
Activate swap partition;
Initialize the serial port;
Load the module.

 

2./Etc/rc. d/rcX. d/[KS]

After rc. sysinit is executed, init is returned and the/etc/rc. d/rc program is executed. Taking runtime Level 5 as an example, init executes the following content in the configuration file inittab:
15: 5: wait:/etc/rc. d/rc 5

This line indicates running/etc/rc with 5 as the parameter. d/rc,/etc/rc. d/rc is a shell script that accepts 5 as the parameter and executes/etc/rc. d/rc5.d directory all rc startup scripts,/etc/rc. the startup scripts in the d/rc5.d directory are actually some link files, rather than the real rc startup scripts. The real rc startup scripts are all in the/etc/rc. d/init. d directory. These rc boot scripts have similar usage. They generally accept parameters such as stat, stop, restart, and status.

The rc STARTUP script in/etc/rc. d/rc5.d is usually a link file starting with K or S. The Startup Script starting with S runs with the start parameter. If the corresponding script also has a K-header link and is already running (marked by a file under/var/lock/subsys ), stop the started daemon with the stop parameter and run the daemon again. This is done to ensure that all related daemon will be restarted when init changes the running level.

You can use chkconfig to set which daemon will run at each running level. Common daemon processes are as follows.

Amd: automatically installs the NFS daemon.
Apmd: Advanced Power Management daemon.
Arpwatch: records logs and builds a database that corresponds to the ethernet address and IP address seen on the LAN interface.
Outofs: automatic installation and management process automount, which is related to NFS and relies on NIS.
Crond: the daemon for scheduled tasks in Linux.
Named: DNS server.
Netfs: Install NFS, Samba, and Netware network file systems.
Network: Activate the script program with configured network interfaces.
Nfs: Enable the NFS service.
Portmap: RPCportmap manager, which manages RPC-based connections.
Sendmail: The Mail Server sendmail.
Smb: Samba file sharing/printing service.
Syslog: a script that enables syslog and klogd system logs to wait for processes during system boot.
Xfs: X Window server, which provides a font set for local and remote X servers.
Xinetd: supports core daemon processes of multiple network services and manages services such as wuftp, sshd, and telnet.

After these daemon processes are started, the rc program is executed and the system returns init to continue the next step.

 

3. Run/etc/ec. d/rc. local

In RHEL 4, the Operating Modes 2, 3, and 5 put/etc/rc. d/rc. local is the last in the initialization script, so you can add some commands in this file that need to be executed after other initialization tasks and before logging on. When maintaining a Linux system, the system administrator needs to modify the script for starting or shutting down the system. If the modification takes effect only when boot is started, and the modification is not significant, you can simply edit the/etc/rc. d/rc. local script. This command script program is executed at the last step of the boot process.

 

6. Run the/bin/login program

The login program will prompt the user to enter the account and password, then encode and confirm the correctness of the password. If the account and password are consistent, initialize the environment for the user and give control to shell, wait for the user to log on.

Login receives the username sent from mingetty as the username parameter, and login analyzes the username. If the user name is not root and the/etc/nologin file exists, login outputs the content of the nologin file and then exits. This is usually used to prevent non-root User Logon during system maintenance. Only terminals registered in/etc/securetty can allow the root user to log on. If this file does not exist, the root user can log on to any terminal. The/etc/usertty file is used to add access restrictions to users. If this file does not exist, there are no other restrictions.

After the username is analyzed, login searches for/etc/passwd and/etc/shadow to verify the password and set other information about the account, such as the main directory and shell used. If no main directory is specified, the main directory is set to the root directory by default. If no shell is specified, the shell type is set to/bin/bash by default.

After the Login program is successful, the last logon information (recorded in/var/log/lostlog) will be output to the corresponding terminal ), check whether the user has any new emails (in the directory of the corresponding user name of/usr/spool/mail), and then set various environment variables. For bash, the system first looks for the/etc/profile script file and executes it; then, if the user's main directory exists. execute the bash_profile file. Other configuration files may be called in these files. After all the configuration files are executed, various environment variables are also set, A familiar command line prompt will appear, and the entire startup process is now over.

Author: "Daily Yunhui"

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.