Linux system startup process

Source: Internet
Author: User


When we use the Linux operating system, we just need to press the power button, wait, and then enter the account and password to use the Linux operating system. So what does the operating system do before you press the power supply to enter your account number and password? Let's talk about the actions that took place during this period.


Let's take the CentOS6 series as an example to explain the Linux startup process.

First, the Linux startup process detailed

Is the general process of initiating the process:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/41/1D/wKiom1PQxc6SML8nAAFcDmK4XC0648.jpg "title=" Qidong.jpg "alt=" Wkiom1pqxc6sml8naafcdmk4xc0648.jpg "/>


Second, post power-on self-test

The POST (poweronselftest) first checks each device. After completion will look for the device that has the boot record, find and read into the operating system boot record, and then give the system control to the boot record, and by the boot record to complete the system's smooth start.


Third, MBR guidance

MBR (Master Boot record) MBR records are typically on disk 0 tracks 1 sectors, a total of 512 bytes. The first 446 bytes are bootloder, after 4*16 64 bytes is the storage partition information, the last 2 bytes is the checksum information, is generally 55AA.


Iv. GRUB (GRand Unified Bootloader), loading kernel

Is the first 446 bytes in the MBR, is a bootloader, its role is to select the kernel to start.

1, the composition of the GRUB program:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/41/21/wKioL1PQ4efw0SE_AAJbTpZuDV4737.jpg "title=" Grub.jpg "alt=" Wkiol1pq4efw0se_aajbtpzudv4737.jpg "/>

It is mainly composed of Device.map,menulst,stage1,stage2, and a series of stage1_5. My understanding of these parts is this:

    • Device.map: The root partition of the kernel file is stored

    • Menu.lis: is the link file of frub.conf, but this name I feel more close to its function, is the menu list. The kernel menu that you can select is set up. stored in the Stage2.

    • Stage: For the Grub boot program is too large, so the 2-segment boot, the first paragraph is stored in the MBR, the second paragraph is stored in the kernel file system, the first paragraph after the completion of the boot can find the second paragraph. However, the second paragraph is stored in the kernel file system, there is no format file system, how can access to the second paragraph of the menu.lst it?? It is necessary to use the middle-tier stage1_5, which assists the Stage1 field in accessing the Stage2 segment.  The stage1_5 is typically located in the 63 sectors after the Stage1 field. This is why there are multiple stage1_5 because the Stage2 file system that can be used in memory is not deterministic.


2. grub.conf file parameter meaning

default=1    #  The default boot kernel title, 0  represents the first timeout=5    #   Default Wait Time splashimage= (hd0,0)/grub/splash.xpm.gz    #  the path to the background picture of the specified menu. For XPM format, with gzip compression, only 14bits color hiddenmenu    #  hidden menu title centos  (2.6.32-358. el6.x86_64)     #  title name, user customizable root  (hd0,0)     #  specify   The root location of the grub  #  specifies the location of the  kernel  file, as well as the location of the  root (after system boot)  , the Mount mode  ro, which is critical. The  init  process starts after the #  is loaded. Kernel /vmlinuz-2.6.32-358.el6.x86_64 ro root=/dev/mapper/vg0-root rd_no_luks rd_no_ Dm lang=en_us. Utf-8 rd_lvm_lv=vg0/swap rd_no_md sysfont=latarcyrheb-sun16 crashkernel=auto rd_lvm_ Lv=vg0/root  keyboardtype=pc keytable=us rhgb crashkernel=auto quiet rhgb  quiet#  useful initrd /initramfs-2.6.32-358.el6.x86_64.img When loading the root file system during kernel boot

What is the nature of initramfs-2.6.32-358.el6.x86_64.img????

# Initramfs is a file in cpio format with gzip compression. The kernel starts as a temporary root file system. # Grub's stage2 will load the INITRD into memory, let the content be released into the content, the kernel will execute the init script, when the kernel gives control to the init file processing. # init It is also primarily loaded with various storage media related device drivers. When the required driver is loaded, # creates a root device and then mounts the root file system Rootfs as read-only. # at the end of this step, release unused memory, switch to the real root filesystem and run the/sbin/init program, # # # # # # # # # # # # # # # # # # # # # 1th Thereafter, the control of the system was fully delegated to the/sbin/init process.

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/41/21/wKioL1PQ7u_RXEQAAAHfgGw63dE269.jpg "title=" 3.jpg " alt= "Wkiol1pq7u_rxeqaaahfggw63de269.jpg"/>


3. Functionality of GRUB

1) provides menu and interactive interface

E: Enter edit mode

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/41/22/wKiom1PQ8uLRTFq2AAFFyUCSOSo430.jpg "title=" 4.jpg " alt= "Wkiom1pq8ulrtfq2aaffyucsoso430.jpg"/>

2) Select the kernel or system to start

Allow passing of bootstrap parameters to the kernel

Select interface to Hide

# can be self-booting through grub like kernel pass parameters. # One of the applications is to change the root password (forget the password) and use the E option to pass the single user instruction.

3) Provide protection for editing functions

Enable kernel files

Select run the specified kernel to enter the password first

Passing parameters

Use the e command to enter the password first

[[email protected] root]# grub-md5-crypt password:retype Password: $1$mp7rp1$l2wzoilnuqpuhar7zxvqb0# above is the generated encrypted string # The information is then added to the grub.conf file in the following format: Password--md5 $1$mp7rp1$l2wzoilnuqpuhar7zxvqb0# Of course, the location of the grub.conf file is different, the encryption effect is not the same. # before adding to title, the entire menu will be encrypted. # when added to the title, the entry of the corresponding operating system will be encrypted.

4. How to install Grub

1) Use the Grub-install command

# Grub-install [--root-directory=/path/to/somewhere] DEVICE

2) Grub interaction is a command

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/41/22/wKiom1PQ_HiCHYTqAALwLfHE0OI236.jpg "title=" 5.jpg " alt= "Wkiom1pq_hichytqaalwlfhe0oi236.jpg"/>

Here we make homemade a lite grub, which of course starts with just one bash. The example is done under a VMware virtual machine.

The first step: Prepare a new disk, divided into 2 districts

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/41/22/wKiom1PRAY_CbjOxAAFQGeXx0nE979.jpg "title=" 8.jpg " alt= "Wkiom1pray_cbjoxaafqgexx0ne979.jpg"/>

The second step: simulate the boot directory at startup, and the root directory, and mount it. As follows

[Email protected] ~]# Df-h | Tail-n 2/dev/sdd1 518M 11M 481M 3%/mnt/boot/dev/sdd2 2.2G 71M 2.0G 4%/mnt/sysro Ot

Step three: Make grub, and generate the Grub configuration file

Making Grub

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/41/22/wKiom1PRA4njlzRSAANnZuPPwug409.jpg "title=" 9.jpg " alt= "Wkiom1pra4njlzrsaannzuppwug409.jpg"/>

Copy the kernel files and Initramfs files to the/mnt/boot directory and generate the grub.conf file

[Email protected] ~]# cp/boot/vmlinuz-2.6.32-358.el6.x86_64/mnt/boot/vmlinuz[[email protected] ~]# cp/boot/ Initramfs-2.6.32-358.el6.x86_64.img/mnt/boot/initramfs.img[[email protected] ~]# ll/mnt/boot/total 20106drwxr-xr-x 2 root root 20:58 grub-rw-r--r--1 root root 16542269 Jul 21:06 initramfs.img-rwxr-xr-x 1 root root 40 43888 Jul 21:05 vmlinuz# generated grub.conf file [[email protected] ~]# Cat/mnt/boot/grub/grub.confdefault=0timeout=10title Mi Ni Linuxroot (hd0,0) kernel/vmlinuz ro root=/dev/sda2 selinux=0 init=/bin/bashinitrd/initramfs.img

Fourth step: Generating the root file system

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/41/22/wKiom1PRBtWiGC9QAAJM8RoL0uA295.jpg "title=" 10.jpg "alt=" Wkiom1prbtwigc9qaajm8rol0ua295.jpg "/>

Fifth step: Copy using commands and libraries to the corresponding root directory

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/41/22/wKioL1PRDKij5REwAAM4ghH8fUw006.jpg "title=" 11.jpg "alt=" Wkiol1prdkij5rewaam4ghh8fuw006.jpg "/>

Use the disk image you just made as the startup disk for another virtual machine.

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/41/22/wKioL1PRD2KC4J8-AAFGpnnDVLM992.jpg "title=" 12.jpg "alt=" Wkiol1prd2kc4j8-aafgpnndvlm992.jpg "/>

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/41/22/wKioL1PRD3LSg0DtAALqgLLfnXg176.jpg "title=" 13.jpg "alt=" Wkiol1prd3lsg0dtaalqgllfnxg176.jpg "/>


V. Start the INIT process

The init process is the first process initiated by the system. Other processes are forked out with this process control, which means that Init is the "ancestor" of other processes, pid=1. Of course, the pid=0 process we call the idle process belongs to the kernel.

Things to do after Init starts:

1. Read/etc/inittab to read the default level hypothesis: Read to the default level is 3

/etc/init/*.conf

# Boot level: (runlevel) # 0: Shutdown # 1: Single user mode # 2: Multiuser mode, NFS not supported # 3: Full multi-user mode, support for Text interface # 4: Reserved Mode # 5: Full multiuser mode, support graphics mode # 6: Shutdown #/etc/inittab file format as Under, it was followed by the CentOS5 style. Id:3:initdefault:

2. Initialize the script/etc/rc.d/rc.sysinit to initialize the system script

This everyone has the opportunity to read, the main role of the script is as follows:

# Get the network environment and host type. The network environment settings file "/etc/sysconfig/network" is read first to obtain a network environment such as host name and default gateway. # Promise Text Welcome message. # mount other file systems defined in the/etc/fstab file. # Activate swap# to detect the root file system and re-mount it in read/write mode # Decide whether to start SELinux and Udev. # Interface Device detection with Plug and Play (PNP) parameter testing. # user-defined module loading. The user can then "/etc/sysconfig/modules/*.modules" to add a custom module, which will be loaded into the system. # Load the relevant settings for the core. Press "/etc/sysctl.conf" to configure the function for the setting value of this file. # Set the system time (clock). # Set the font style for the console of the terminal. # set up HDD features such as RAID and LVM. # View the test disk file system in a way. # The conversion of disk quota quota. # Load the system disk again in read mode. # Start the quota feature. # Start the system random number device (generate random number function). # clear the temporary files during startup. # Load the boot information into the "/VAR/LOG/DMESG" file.

3. Then execute the/ETC/RC.D/RC script

When this script runs, it reads all scripts under/etc/rc.d/rc3.d/to control when the system starts or shuts down those services. 650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/41/44/wKiom1PSA-mDbCDSAAZ9kiqbBP4645.jpg "title=" 14.jpg "alt=" Wkiom1psa-mdbcdsaaz9kiqbbp4645.jpg "/>

# This script is roughly divided into 2 categories, beginning with S and starting with a K script. # S represents the start-up, K means that the machine does not start when it is switched on.            # each file naming format, for example: S01sysstat S boot start 01 boot sequence Sysstat startup script name # The startup definition is defined in each script: for example, # # Chkconfig:-85 15# Default level start order does not start

These scripts are self-booting and can be managed by the Chkconfig command.

# Common options: # chkconfig--add Srv_script You can write some scripts to make this command unified management. For example: Chkconfig--add httpd# chkconfig--del srv_script The service is removed, meaning that the service is not managed by chkconfig this mechanism # chkconfig Srv_script {On|off} settings The service boot is self-booting, the default level of management is 2345.#--level 2345, etc., specifying the default level, which can be used with other options


4. Execute the/etc/rc.d/rc.local script, which is the last script launched during startup.

The/bin/login logged-on user is executed at the end. This completes the system startup process.


This article is from the "Upstream Cold" blog, please be sure to keep this source http://guoting.blog.51cto.com/8886857/1530109

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.