Overview: System startup process is an important content of Linux, in-depth understanding of the startup process will play a role in learning Linux. CentOS 7 has major changes, so the following is only for CentOS 5 and 6. Let’s get down to business.
Startup process:
Step 1: POST power-on self-test
The purpose of this process is to check whether the external hardware devices can work normally, such as CPU, memory devices, hard disks, and so on.
Step 2: BIOS select boot mode
BIOS for regular basic computer people should not be unfamiliar, especially those who often install the system, it is a list of options, let you choose which way to boot the system, common hard disk boot, CD-ROM, and network mode boot.
Step 3: BootLoader
This step is a bit complicated, but what it does is load the core files from the boot system and submit them to memory for execution. It lists a GRUB menu with the kernel options for our operating system, and the kernel files you select will be loaded into memory for execution.
Boot loader GRUB: Find the kernel file and provide the GRUB menu
1) Stage1, read the MBR in order to drive the partition where STAGe2 resides (kernel files and rootfs file system drivers are stored on stage2) Stage 1.5 provides file system drivers for STAGe2 (stored in 63 sectors behind sector 1 of cylinder 0) 3) Stage1 and 1.5, stage2 will be loaded (usually /boot partition). A kernel selection menu is provided, and the STAGe2 partition also has a ramdisk or ramfs file, which is a temporary root file system that contains the driver files needed for the real rootfs. So stage2 and kernel files are usually placed in a basic disk partition (typically /boot). Note: The ramdisk temporary root file system is created after the operating system is installed. After the operating system is installed, the ramdisk temporary root file system can scan the current host hard disk device model and find the relevant driver to make a temporary rootCopy the code
Code analysis:
[root@localhost testdir]# cp /boot/initramfs-2.6.32-642.el6.x86_64.img
[root@localhost testdir]# zcat initramfs - 2.6.32-642. The el6. X86_64. Img | cpio - id / / unzip it
140023 blocks
[root@localhost testdir]Since it is a temporary root file, the directory structure is also similar to our rootfs, which contains the file system drivers required by rootfs
bin dracut-004-409.el6 init initqueue-settled lib netroot pre-trigger sbin tmp
cmdline emergency initqueue initqueue-timeout lib64 pre-mount pre-udev sys usr
dev etc initqueue-finished initramfs-2.6.32-642.el6.x86_64.img mount pre-pivot proc sysroot var
[root@localhost testdir]#
Copy the code
Step 4: Load the kernel file
Using the kernel file selected above, to load it into memory to decompress, divided into the following four steps
1) Detect all hardware devices that can be identified. 2) Load the hardware driver (possibly with ramdisk/ramfs) 3) mount the root file system in read-only mode 4) Run the first application in user space: /sbin/initCopy the code
Note: Ramdisk/ramfs is the rootfs file driver of the partition where Stage2 resides. With the kernel file and the required rootfs file driver, mount rootfs in read-only mode first to avoid bugs or manual operation of the kernel file
Code analysis:
Init program initialization
1) Obtain the runlevel information according to the init configuration file, and obtain the file path of the system initialization script. (CentOS 5 init file is /etc/inittab, CentOS6 splits the /etc/inittab file into multiple files)
CentOS 5: The SysV init mode is used, which is characterized by the start of user space services, usually through scripts, dependent services will be started in serial, which also causes the CentOS 5 startup process is very slow, the configuration file is /etc/inittab CentOS 6: The Upstart method is similar to parallel startup. Configuration files: /etc/inittab,/etc/init/*.confCopy the code
2) Read the system initialization script /etc/rc.d/rc.sysinit and execute the script as follows:
(1) Set the host name (2) Set the welcome message (3) Activate udev and selinux (4) Mount the file system defined in the /etc/fstab file (5) Detect the root file system, Remount the root file system in read/write mode (6) Set the system clock (7) Activate the swap device (8) Set kernel parameters according to the /etc/sysctl.conf file (9) Activate the LVM and software RAID device (10) Load drivers for additional devices (11) Clean up the operationCopy the code
3) Run the /etc/rc.d/rc script file based on the runlevel obtained earlier
D /etc/rc.d/ contains several link files, which point to the service script writer in /etc/init.d/, according to the default run level obtained in /etc/inittab and the link file in /etc/rc#. /etc/rc#. D/the link file starts with K or S, K indicates the service to be stopped, S indicates the service to be started. And the service script has a priority,
K*:K##* : ## run order; The smaller the number, the earlier it runs; A service with a smaller number is usually a service that depends on another service S*:S##* : ## The smaller the number, the earlier it runs; A service with a smaller number is usually a dependent serviceCopy the code
[root@localhost boot]# cd /etc/rc.d
[root@localhost rc.d]# ls
init.d rc rc0.d rc1.d rc2.d rc3.d rc4.d rc5.d rc6.d rc.local rc.sysinit
[root@localhost rc.d]# cd rc.3.d
root@localhost rc.d]# cd rc3.d
[root@localhost rc3.d]# lsK01dnsmasq K35vncserver K88wpa_supplicant S08ip6tables S14nfslock S26lm_sensors S95anacron K02avahi-dnsconfd K50netconsole K89dund S08iptables S15mdmonitor S26lvm2-monitor S95atd K02NetworkManager K50snmpd K89netplugd S08mcstrans S18rpcidmapd S28autofs S97rhnsd K05conman K50snmptrapd K89pand S10network S19rpcgssd S55sshd S97rhsmcertd K05saslauthd K69rpcsvcgssd K89rdisc S11auditd S22messagebus S56cups S97yum-updatesd K05wdaemon K73ypbind K99readahead_later S12restorecond S25bluetooth S56rawdevices S98avahi-daemon K10psacct K74ipmi S00microcode_ctl S12syslog S25netfs S57vmware-tools-thinprint S99firstboot K15httpd K74nscd S03vmware-tools S13cpuspeed S25pcscd S80sendmail S99local
K15svnserve K74ntpd S04readahead_early S13irqbalance S26acpid S85gpm S99smartd
K20nfs K85mdmpd S05kudzu S13iscsi S26haldaemon S90crond
K24irda K87multipathd S07iscsid S13portmap S26hidd S90xfs
Copy the code
Note: /etc/rc.d/rc.local is a link file to /etc/rc.d/rc.local. It is not a startup file, but it is a normal file, but it has the lowest priority, so it is started last. If you want to boot up and do something, I can write it inside this script.
[root@localhost rc3.d]# find /etc/ -name *local
/etc/rc.d/rc2.d/S99local
/etc/rc.d/rc5.d/S99local
/etc/rc.d/rc3.d/S99local
/etc/rc.d/rc4.d/S99local
Copy the code
/etc/inittab file in CentOS 5
Id :3: initDefault: // Get the default runtime level; 3 indicates the run level# System initialization.Si: : sysinit: / etc/rc. D/rc sysinit / / implement/etc/rc. D/rc sysinit system initialization scripts / / according to the previous access to the default run level, implement the/etc/rc. D/rc script file l0:0:wait:/etc/rc.d/rc 0
l1:1:wait:/etc/rc.d/rc 1
l2:2:wait:/etc/rc.d/rc 2
l3:3:wait:/etc/rc.d/rc 3
l4:4:wait:/etc/rc.d/rc 4
l5:5:wait:/etc/rc.d/rc 5
l6:6:wait/etc/rc.d/rc :/etc/rc.d/rc /etc/rc.d/rc# First, run the KILL scripts.
for i in /etc/rc$runlevel.d/K* ; do// This is oneforLoop to close /etc/rc based on the default level information obtained earlierService under #.d/
check_runlevel "$i" || continue
# Check if the subsystem is already up.
subsys=${i#/etc/rc$runlevel.d/K?? }
[ -f /var/lock/subsys/$subsys -o -f /var/lock/subsys/$subsys.init ] \
|| continue
# Bring the subsystem down.
if LC_ALL=C egrep -q "^.. *init.d/functions" $i ; then
$iStop //stop closes the service!else
action $"Stopping $subsys:" $i stop
fi
done
# Now run the START scripts.
for i in /etc/rc$runlevel.d/S* ; do// This is also aforThe loop, in reverse, starts /etc/rc#.d/ The corresponding script file below
check_runlevel "$i" || continue
# Check if the subsystem is already up.
subsys=${i#/etc/rc$runlevel.d/S?? }
[ -f /var/lock/subsys/$subsys -o -f /var/lock/subsys/$subsys.init ] \
&& continue
# If we're in confirmation mode, get user confirmation
if [ -f /var/run/confirm ]; then
confirm $subsys
test$? && = 1continue
fi
update_boot_stage "$subsys"
# Bring the subsystem up.
if [ "$subsys" = "halt" -o "$subsys" = "reboot" ]; then
export LC_ALL=C
exec $iStart // Starts the servicefi
if LC_ALL=C egrep -q "^.. *init.d/functions" $i\ | | ["$subsys" = "single" -o "$subsys" = "local" ]; then
$iStart // Starts the serviceelse
action $"Starting $subsys:" $i start
fi
done
Copy the code
The following figure shows the interface when the service is started
Step 6: Start the terminal
Start the terminal based on the default run level obtained earlier, or if the run level is 5, start the graphical interfaceCopy the code
Step 7: User login
End of system startup process!Copy the code
Problem summary:
Until now, I have been puzzled by a few questions, and I have summarized them
1) The kernel file is on disk, the system has not been booted, the system has not been booted, the directory is not mounted, the first to find the boot partition, but the directory is in/directory, where to find the boot! ?
Answer: Notice that the system is not looking for/to find the grub menu, kernel files and rootfs drivers under the boot partition, because the/has not been mounted, so it cannot find the boot directory under /. Instead, it is directly going to the disk partition of boot to find the required files
[root@localhost ~]# cat /boot/grub/grub.conf //grub configuration file. Omit... Title CentOS 6 (2.6.32-642.el6.x86_64) root (hd0,0) //hd0,0 indicates the disk partition where STAGe2 is located. If hd0,0 is set to /, then the root of the kernel and initrd is set to /"/", also represents hd0, 0, which is the disk partition where stage2 is located. // vmlinuz-2.6.32.... Kernel /vmlinuz-2.6.32-642.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 // initramfs-2.6.32... Initrd /initramfs-2.6.32-642.el6.x86_64.img [root@localhost ~] /initramfs-2.6.32-642.el6.x86_64.#
Copy the code
2) Continue the above problem, even if you first load the boot partition, boot partition system where the system driver
Answer: From the following code, partition information starts on cylinder 1, so did the dog eat cylinder 0? The MBR is stored in cylinder 0, the first sector of track 0, but it occupies only 512 bytes, because cylinder 0 contains many sectors, and the following sectors are for the file system drivers of the /boot partition. Stage1 ->stage1.5->stage2 This procedure is to mount the /boot partition, and stage1.5 is to find the file system driver for the /boot partition.
[root@localhost ~]# df //df view/directory hanging in partition
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda2 7858552 5884648 1568264 79% /
/dev/sda1 295561 16787 263514 6% /boot
tmpfs 511932 0 511932 0% /dev/shm
[root@localhost ~]# fdisk -l // Check disk partition informationDisk /dev/sda: 10.7 GB, 10737418240 bytes 255 heads, 63 sectors/track 1305 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Device Boot Start End Blocks Id System /dev/sda1 * 1 38 305203+ 83 Linux // Note that the starting cylinder starts at cylinder 1. /dev/sda3 1049 1305 2064352+ 82 Linux swap/SolarisCopy the code
3) After loading the kernel, rootfs is mounted in read-only mode to avoid bugs or human error. How to write data when mounted in read-only mode?
Solution: After the kernel reads the init program, there is a system initialization script, the /etc/rc.d/rc.sysinit script, which contains the following code, where rootfs is remounted as read/write.
remount_needed() {
local state oldifs
[ "$READONLY" = "yes" ] && return 1
state=`LC_ALL=C awk '/ / / && ($3! ~ /rootfs/) { print $4 }' /proc/mounts`
oldifs=$IFS
IFS=","
for opt in $state ; do
if [ "$opt" = "rw" ]; then
IFS=$oldifs
return 1
fi
done
IFS=$oldifs
return0}#Remount the root filesystem read-write.
update_boot_stage RCmountfs
if remount_needed ; thenMount -n -o remount,rw/action $"Remounting root filesystem in read-write mode: " mount -n -o remount,rw /
fi
Copy the code
Flow chart:
As the saying goes, a picture is worth a thousand words. I will draw a picture of the startup process described above, hoping to more clearly describe the startup process of CentOS.
If you like what I write, you can follow my official account, which contains my learning resources and other benefits. : enterprise tribeCopy the code