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.lis: is the link file of grub.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 # Default boot kernel title, 0 indicates the first
- timeout=5 # Default Wait time
- Splashimage= (hd0,0)/grub/splash.xpm.gz # Specifies the path to the background picture of the menu. In XPM format with gzip compression, only 14bits color
- Hiddenmenu # Hidden Menu
- Title CentOS (2.6.32-358.el6.x86_64) # header name, user customizable
- Root (hd0,0) # Specifies the root location of grub
- # Specify the location of the kernel file, and also point out the location of root (after system boot), mount the way Ro, this is critical.
- # The INIT process is started after loading.
- 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 when loading the root file system during kernel boot
- Initrd/initramfs-2.6.32-358.el6.x86_64.img
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 INITRD into memory, allowing the content to be released into the content,
- # The Kernel then executes 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 has finished loading,
- # Creates a root device and then mounts the root file system Rootfs as read-only.
- # After this step, release unused memory, switch to the real root filesystem and run the/sbin/init program,
- # Implementation of the system's process number 1th. Thereafter, the control of the system was fully delegated to the/sbin/init process.
How to make Initramfs?
- # can use the dircut command as follows:
- # only in initramfs file will be used to when it is damaged,
- [[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
- # details, this link can be used: 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
- # 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
- # then add the information to the grub.conf file in the following format:
- Password--md5 $1$mp7rp1$l2wzoilnuqpuhar7zxvqb0
- # of course, the location of the grub.conf files 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
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
- [Email protected] ~]# Df-h | Tail-n 2
- /DEV/SDD1 518M 11M 481M 3%/mnt/boot
- /DEV/SDD2 2.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
- [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 20106
- Drwxr-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 4043888 Jul 21:05 vmlinuz
- # The generated grub.conf file
- [Email protected] ~]# cat/mnt/boot/grub/grub.conf
- default=0
- timeout=Ten
- Title Mini Linux
- Root (hd0,0)
- Kernel/vmlinuz ro root=/dev/sda2 selinux=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
- # Boot level: (runlevel)
- # 0: Turn off the machine
- # 1: Single-user mode
- # 2: Multi-user mode, NFS feature not supported
- # 3: Full multi-user mode, support for text interface
- # 4: Reserved mode
- # 5: Full multi-user mode, support graphics mode
- # 6: Turn off the machine
- #/etc/inittab file format as follows, this is 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
- # Detect root filesystem 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.
- # 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 boot script
- # The definition of the startup 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 of your own scripts and let this command be managed uniformly. 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} Sets whether the service is booting from the start, the default level of management is 2345.
- #--level 2345, specifying the default level, 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.
Blog Link: http://guoting.blog.51cto.com/8886857/1530109
CentOS6 Start Flowchart Text anatomy