24. Linux startup process and grub detailed

Source: Internet
Author: User
Tags network function



Remember the startup process for Windows? I believe we can say a general, why say a general, because Windows is the graphical interface to start, but the boot process has done, you know, on the contrary, when the Linux boot, will load these on the screen to display, then we look at the Linux boot what happened!!!


Linux startup flowchart

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M02/87/20/wKiom1fVBELjXsvaAAUkuL83t2Q304.jpg "title=" Linux boot process. jpg "alt=" wkiom1fvbeljxsvaaaukul83t2q304.jpg "/>


Simple untie machine start-up process

When the user turns on the power, the BIOS post, determines the boot device, installs the boot device boot device above the grub installed above boot linux,linux first kernel boot, through the switch, executes the INIT program, the INIT program determines the boot level, Based on the startup level of the system initialization and operation of the service, and then return to the Init startup terminal, the user through authentication successfully landed shell, this is a boot from the boot to the landing process


First step: Hardware boot Boot


650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M02/87/1E/wKioL1fVBKryShbGAADrFHTlj-4910.png "title=" Qq20160911071540.png "alt=" Wkiol1fvbkryshbgaadrfhtlj-4910.png "/>


This section simply says that the BIOS program boots and loads the Mbrbootloader program, also known as the Grub bootloader program.


When the user turns on the power, post starts self-test, detects if the hardware device is real or defective (whether it affects the normal boot), and if it does not affect the normal boot, the task is given to the BIOS


BIOS through search, install boot OK boot device


Boot entry for hard disk, BIOS go to read the first 512 bytes of hard disk to memory, find bootloader, determine grub


Step Two: Grub boot boot kernel

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M02/87/20/wKiom1fVCCiS_otBAACaCaDmT6Y903.png "title=" Qq20160911073001.png "alt=" Wkiom1fvccis_otbaacacadmt6y903.png "/>

This step is summarized even if: The GRUB program loads execution and starts booting the kernel program.

Boot Loader is a small piece of program that runs before the operating system kernel runs. Through grub boot can determine the kernel program, because the boot sector only 446 bytes, grub is just a small program installed inside, really used in the sector behind the MBR storage, we want to use the Bootloader Grub feature must read the following file, Bootloader The grub features run and load configuration options are divided into three stages


A Stage1 Stage:

The Stage1 phase is actually a program that is pre-written to the MBR in bootloader when the system is installed.

The task of the Stage1 phase is to read the contents of the hard disk 0 cylinder 0 Track 2 sector into memory and execute it, which is the entrance to the Stage1.5 phase or Stage2 stage, and directs to the Stage1.5 stage or Stage2 stage.

In this Stage1 phase, there is no ability to identify the file system.


B Stage1.5 Stage

The stage1.5 stage is the intermediate bridge between the Stage1 stage and the Stage2 stage. The stage1.5 phase has the ability to identify the boot partition file system, after which the GRUB program has the ability to access the Stage2 file under the/boot partition under the/grub directory and load Stage2 into memory execution.


C Stage2 Stage

When the Stage2 phase is executed, the configuration file grub.conf of the GRUB program is parsed first, and the System boot menu is displayed according to the configuration file. The kernel is then loaded into memory, and the RAMDisk memory virtual root file system is established through the INITRD program. Control will be forwarded to the kernel program at this time.


Part III: Kernel boot boot

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M00/87/21/wKiom1fVClews7eUAAA00sCzm-c795.png "title=" Qq20160911073927.png "alt=" Wkiom1fvclews7euaaa00sczm-c795.png "/>


This is done by creating a virtual root file system in memory to drive the device and to establish and switch to a real root filesystem.


After the kernel image is loaded into memory and the INITRD program establishes the RAMDisk memory virtual root file system, the kernel starts to drive the basic hardware and invokes the INIT program in the virtual root file system to load the driver module to initialize the relevant configuration of the various devices in the system, including CPU, I/O, storage devices, and so on. When the required drivers are loaded, a root device is created based on what is specified in the "root=xxx" section of the grub.conf configuration file, and the root file system is mounted read-only and switched to the true root filesystem, while invoking the ancestor process of the system process/sbin/ Init program, enter the system initialization phase.


Fourth Step: System initialization

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M02/87/21/wKiom1fVC3mg-U07AADipmcMw3g129.png "title=" Qq20160911074444.png "alt=" Wkiom1fvc3mg-u07aadipmcmw3g129.png "/>

This step is through the/sbin/init,init program to prepare the software run bad environment, start system services


The/etc/inittab file determines the RunLevel, then executes the system initialization script/etc/rc.sysinit, initializes the user space environment for the user, after initialization, according to the RunLevel, the system starts the corresponding level of directory start service. Turn off those services that are not available (inside s99local. /rc.local) User Auto service startup script


Detailed Documentation:

/etc/inittab file

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M00/87/1F/wKioL1fVHw_iyP9TAADW9PL__FM892.png "title=" Qq20160911090808.png "alt=" Wkiol1fvhw_iyp9taadw9pl__fm892.png "/>

This file is the default level to set the system startup


Set the default run level

(1) Operation level: a mechanism set up for the purpose of running or maintaining the system;

0-6: A total of 7 levels;

0: Shutdown, shutdown

1: Single user mode, root user, no authentication, maintenance mode;

2: Multi-user mode (multi user), will start the network function, but will not start NFS, maintenance mode;

3: Multi-user mode (multi user), full function mode, text interface;

4: Reserved level: Currently no special use purposes, but the habit of using the same 3-level function;

5: Multi-user mode (multi user), full function mode, graphical interface;

6: Restart, reboot

CENTOS5 and 6 are not the same, CentOS5 here can also set "CTRL" + "ALT" + "Del" and "UPS" "TTY" and "system initialization script", now these are already default, in this file by the detailed


System Initialization script:/etc/rc.d/rc.sysinit

(1) Set the host name;

(2) Set welcome information;

(3) Activate Udev and SELinux;

(4) Mount all file systems defined in the/etc/fstab file;

(5) Detecting the root file system and re-mounting the root filesystem in read/write mode; (re-mount refers to the root file after detection)

(6) Set the system clock;

(7) Set kernel parameters according to/etc/sysctl.conf file;

(8) Activating LVM is a soft raid device;

(9) Activate swap device;

(10) Load Drivers for additional devices (kernel load driver only loads root file system)

(11) Cleaning operation;


Script File/etc/rc.d/

Turn off/Start services at the corresponding level

[Email protected] ~]# LS/ETC/RC.D/INIT.D RC rc0.d rc1.d rc2.d rc3.d rc4.d rc5.d rc6.d rc.local rc.sysinit

The script file/ETC/RC.D/RC functions to start or stop the service when the level switches, and this script accepts arguments passed to the $runlevel variable in the script, and then reads/etc/rc$runlevel.d/k* and/etc/rc$runlevel.d/s* All files, these files are why the boot started, some services will start automatically, some services do not start the reason.

[[email protected] ~]# ls /etc/rc.d/rc3.d/k01smartd           K69rpcsvcgssd      K95rdma           s13cpuspeed          s25netfs       S82abrtdK05wdaemon          k73winbind         k99rngd           S13irqbalance        S26acpid       s90crondk10psacct          k74ntpd             S01sysstat        s13rpcbind           s26haldaemon   s95atdk10saslauthd       k75ntpdate          S02lvm2-monitor  s15mdmonitor         s26udev-post   S99firstbootK15htcacheclean    K75quota_nld        S08ip6tables     S22messagebus         S28autofs     S99localK15httpd            K84wpa_supplicant  S08iptables       s23networkmanager    s50bluetoothk30spice-vdagentd  k87restorecond      S10network       S24nfslock            S50kdumpK50dnsmasq          K89netconsole      s11auditd        s24rpcgssd            S55sshdK60nfs              K89rdisc           S11portreserve    S25blk-availability  S80postfixK61nfs-rdma         k92pppoe-server    s12rsyslog       s25cups               s82abrt-ccpp

K*: To stop the service, k##*, priority, the smaller the number, the higher the priority to shut down, the dependent service is first closed, and then closed by the dependent.

S*: To start the service, s##*, priority, the smaller the number, the more priority starts, the dependent service starts first, and the dependent service starts.

These files are linked files that are linked to individual programs in the/etc/init.d/* directory, such as the ntpd script

[Email protected] ~]# ls/etc/rc.d/rc3.d/k74ntpd-ldlrwxrwxrwx. 1 root root: 08:38/etc/rc.d/rc3.d/k74ntpd. /init.d/ntpd


So how do we set a service to shut down or open the next time it restarts the system? You can use the Chkconfig command to implement:

Checkconfig

Command format:

chkconfig [Options] service_name [On|off] Options:--add #→ Add program service--list #→ Lists whether the level of all services on the current system is closed or started--del #→ Delete a service (just delete the linked file, do not delete the original file)--level [On|off] #→ specify which levels a service corresponds to On or off


User-defined Start-up program, you can according to their own needs to execute some commands or write to the script/etc/rc.d/rc.local. You can load it automatically when you start up!

[Email protected] ~]# ll/etc/rc.d/rc3.d/s99local lrwxrwxrwx. 1 root root: 08:37/etc/rc.d/rc3.d/s99local. /rc.local


Fifth step: Start the terminal, user login shell

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M00/87/1F/wKioL1fVEcLxmKhIAACaOKZ5zJ8383.png "title=" Qq20160911081110.png "alt=" Wkiol1fveclxmkhiaacaokz5zj8383.png "/>

This step is for the user to log in to the shell process


If the level is not changed, the default execution/sbin/mingetty opens 6 plain text terminals, allowing the user to enter a user name and password. After the input is completed, call the login program and check the password. If the password is correct, read the user-specified shell from the file/etc/passwd and start the shell.



Grub detailed

When the computer is power-on self-test, the ROM BIOS loads the code in the MBR (primary boot sector, which is the first sector of the hard disk) into memory, this sector altogether 512 bytes, the first 446 bytes content holds the Grub (bootloader) key boot program, Then 64 bytes to place the hard disk partition table DPT (disk Partition table), altogether four can have four primary partitions, accounting for 64 bytes, which is why the primary partition has a maximum of four reasons, the last 2 bytes is a fixed flag 0X55AA. When the BIOS loads the bootloader into memory, it gives the control to grub, and then the remaining code of the grub completes the loading and removal of other code and the file system initialization lookup, and finally loads the kernel image file, which gives control to the real kernel to run.

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M00/87/21/wKiom1fVFunRTkoWAAAWIeqKFKQ983.png "title=" Qq20160911083235.png "alt=" Wkiom1fvfunrtkowaaawieqkfkq983.png "/>


/boot/grub/grub.conf configuration file:

[[email protected] grub]# ll /etc/grub.conf #/etc/grub.conf /boot/grub/ grub.conf for connection file lrwxrwxrwx. 1 root root 22 8 month   11 03:29 /etc/ GRUB.CONF ->&NBSP, .... /boot/grub/grub.conf[[email protected] grub]# cat /boot/grub/grub.conf# grub.conf  generated by anaconda## Note that you do not have to  Rerun grub after making changes to this file# notice:  you  have a /boot partition.  This means that#           all kernel and initrd paths are relative  to /boot/, eg.#          root  (hd0,0) #           kernel /vmlinuz-version ro root=/ dev/sda2#          initrd /initrd-[generic-]version.img#boot=/dev/ sdadefault=0  #设定默认启动菜单项, default 0 starts timeout=5  #指定菜单等待选择的时长  splashimage= (hd0,0)/grub/splash.xpm.gz   #指定菜单的背景图片的路径, in xpm format, with gzip compression hiddenmenu  #是否影藏菜单password  --md5 $1$1s9xy$ 1mugzsopc2vatkw.jvz0x/  #菜单编辑认证title  CentOS 6  (2.6.32-642.el6.x86_64)   #定义菜单项password  123456  #可以选择明文root   (hd0,0)   #本次grub查找stage2及其kernel文件所在设备分区, specifying grub root kernel / vmlinuz-2.6.32-642.el6.x86_64 ro root=uuid=240533cf-b37f-4460-974f-702bab867da5 nomodeset  Rd_no_luks  keyboardtype=pc keytable=us rd_no_md crashkernel=auto lang=zh_cn. utf-8 rd_no_lvm rd_no_dm rhgb quiet  #需要启动的内核  initrd / initramfs-2.6.32-642.el6.x86_64.img  #内核匹配的ramfs文件

I just added two passwords to the Grub protection mechanism, enter a password to edit

Generate encrypted ciphertext

[Email protected] ~]# grub-md5-crypt password:retype Password: $1$1s9xy$1mugzsopc2vatkw.jvz0x/

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M02/87/21/wKiom1fVGgSjHTPYAAAWeIkdU0M871.png "title=" Qq20160911084113.png "alt=" Wkiom1fvggsjhtpyaaaweikdu0m871.png "/>

Here's the problem.

What if grub is corrupted and the system fails to start???


Repair with CD-ROM access to emergency rescue mode

1, start with the shutdown, select Rescue installed system in the installation interface to enter rescue mode

2, follow the prompts to select the language and keyboard

3, choose whether to configure the network card, select No

4, according to the system prompt on the hard disk has been found and mounted under/mnt/sysimage, continue loaded with read-write mode

5, input chroot/mnt/sysimage change disk root directory

6,fdisk-l/DEV/SDA using FDISK to check partitions

7,GRUB-INSTALL/DEV/SDA Installing the Grub Boot program to the MBR sector of disk/DEV/SDA

8,exit

Installation Repair method for CentOS 7

SH 3.1#grub

Grub>root (hd0,0)

Grub>setup (hd0)

Grub>quit


This article from "Live by the Sea like Waves" blog, please be sure to keep this source http://zhang789.blog.51cto.com/11045979/1851675

24. Linux startup process and grub detailed

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.