Linux Start-up 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:

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:

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.lst: is the link file of grub.conf, but this name I feel more close to its function, is the menu list. A selectable kernel menu is set in the file. 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

1234567891011 default=1    # 默认启动的内核title, 0 表示是第一个timeout=5    # 默认等待时间splashimage=(hd0,0)/grub/splash.xpm.gz    # 指定菜单的背景图片的路径。为xpm格式,采用gzip压缩,只能为14bits色hiddenmenu    # 隐藏菜单title CentOS (2.6.32-358.el6.x86_64)    # 标题名,用户可自定义    root (hd0,0)    # 指定 grub 的根位置    # 指定 kernel 文件的位置,还要指出 root(系统启动后) 的位置,挂载方式 ro,这项很关键。    # 加载后会启动 init 进程。    kernel /vmlinuz-2.6.32-358.el6.x86_64 ro root=/dev/mapper/vg0-rootrd_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/rootKEYBOARDTYPE=pc KEYTABLE=us rhgb crashkernel=auto quiet rhgb quiet    # 在内核启动过程中装载根文件系统时有用    initrd /initramfs-2.6.32-358.el6.x86_64.img

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

123456789 # initramfs 是以 gzip 压缩的 cpio 格式的文件。内核启动时将他作为一个临时的根文件系统。# grub 的 stage2 将initrd加载到内存里,让后将其中的内容释放到内容中,# 内核便去执行init脚本,这时内核将控制权交给了init文件处理。# init 它也主要是加载各种存储介质相关的设备驱动程序。当所需的驱动程序加载完后,# 会创建一个根设备,然后将根文件系统rootfs以只读的方式挂载。# 这一步结束后,释放未使用的内存,转换到真正的根文件系统上面去,同时运行/sbin/init程序,# 执行系统的1号进程。此后系统的控制权就全权交给/sbin/init进程了。

How to make Initramfs?

12345678910 # 可以使用 dircut 命令,用法如下:# 只有在 initramfs 文件损毁的时候会使用到[[email protected] ~]# dracut /mnt/initramfs-`uname -r`.img `uname -r`[[email protected] ~]# du -sh /mnt/initramfs-2.6.32-358.el6.x86_64.img 16M /mnt/initramfs-2.6.32-358.el6.x86_64.img[[email protected] ~]# du -sh /boot/initramfs-2.6.32-358.el6.x86_64.img 16M /boot/initramfs-2.6.32-358.el6.x86_64.img# 详细介绍,可使用这个链接:https://zh.opensuse.org/index.php?title=SDB:Dracut&variant=zh-cn#.E5.90.AF.E7.94.A8_dracut_.E7.94.9F.E6.88.90.E7.9A.84_initramfs

3. Functionality of GRUB

1) provides menu and interactive interface

E: Enter edit mode

2) Select the kernel or system to start

Allow passing of bootstrap parameters to the kernel

Select interface to Hide

12 # 可以自启动是通过 grub 像内核传递参数。# 应用之一是:修改 root 密码(忘记密码),使用 e 选项,传递单用户指令。

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

1234567891011 [[email protected] root]# grub-md5-crypt Password: Retype password: $1$Mp7Rp1$l2wzOILNUqpUhAR7zXvQb0# 上面是生成的加密字符串# 然后将信息加入到 grub.conf 文件中,格式如下:password --md5 $1$Mp7Rp1$l2wzOILNUqpUhAR7zXvQb0# 当然加入 grub.conf 文件的位置不同,加密效果也不一样。# 加入到 title 之前的话,会加密整个菜单。# 加入到 title 指内的话,会加密对应的操作系统的入口。

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

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

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

123 [[email protected] ~]# df -H | tail -n 2/dev/sdd1518M    11M   481M   3% /mnt/boot/dev/sdd22.2G    71M   2.0G   4% /mnt/sysroot

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

Making Grub

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

12345678910111213141516 [[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     1024 Jul 24 20:58 grub-rw-r--r-- 1 root root 16542269 Jul 24 21:06 initramfs.img-rwxr-xr-x 1 root root  4043888 Jul 24 21:05 vmlinuz# 生成的 grub.conf 文件[[email protected] ~]# cat /mnt/boot/grub/grub.confdefault=0timeout=10title Mini Linux    root (hd0,0)     kernel /vmlinuzro root=/dev/sda2selinux=0 init=/bin/bash    initrd /initramfs.img

Fourth step: Generating the root file system

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

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

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

1234567891011 # 启动级别:(runlevel)# 0:关机# 1:单用户模式# 2:多用户模式,不支持 NFS 功能# 3:完全多用户模式,支持文本接口# 4:预留模式# 5:完全多用户模式,支持图形模式# 6:关机# /etc/inittab文件格式如下,这是遵循了CentOS5的风格。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:

12345678910111213141516171819 # 获取网络环境与主机类型。首先会读取网络环境设置文件"/etc/sysconfig/network",获取主机名称与默认网关等网络环境。# 打印文本欢迎信息。# 挂载/etc/fstab文件中定义的其它文件系统。# 激活swap。# 检测根文件系统,并以读写方式重新挂载# 决定是否启动SELinux和udev。# 接口设备的检测与即插即用(pnp)参数的测试。# 用户自定义模块的加载。用户可以再"/etc/sysconfig/modules/*.modules"加入自定义的模块,此时会加载到系统中。# 加载核心的相关设置。按"/etc/sysctl.conf"这个文件的设置值配置功能。# 设置系统时间(clock)。# 设置终端的控制台的字形。# 设置raid及LVM等硬盘功能。# 以方式查看检验磁盘文件系统。# 进行磁盘配额quota的转换。# 重新以读取模式载入系统磁盘。# 启动quota功能。# 启动系统随机数设备(产生随机数功能)。# 清楚启动过程中的临时文件。# 将启动信息加载到"/var/log/dmesg"文件中。

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.

1234567 # 这里面的脚本大致分为 2 类,以 S 开头的和以 K 开头的脚本。# S 代表开机时启动的,K 表示开机时不启动。# 每个文件的命名格式,例如:S01sysstat  S 开机启动 01 启动次序  sysstat 启动脚本名# 启动的定义是在每个脚本中定义的:例如#     # chkconfig: -     85         15#            默认级别 启动次序 不启动次序

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

12345 # 常用选项:# chkconfig --add SRV_SCRIPT  可以将自己写的一些脚本,让此命令统一管理。例如:chkconfig --add httpd# chkconfig --del SRV_SCRIPT   将服务删除,意思就是服务不由chkconfig这种机制来管理# chkconfig SRV_SCRIPT {on|off}    设置服务开机是否自启动,默认管理的级别是2345.# --level 2345 等,指定默认级别,可以与其他选项一起使用

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.

Original source: http://guoting.blog.51cto.com/8886857/1530109

RELATED links: How did the computer start?

Linux Start-up process

Related Article

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.