Home » Linux » The Linux boot process in detail

The Linux boot process in detail

 one, BIOS power on self test
When you press the power switch to boot, the computer will first start the BIOS (basic input and output system), BIOS is generally integrated into the motherboard
BIOS's job
1., detect connection hardware, such as graphics cards, memory, disk, etc., the purpose of detection is to provide these equipment information to the operating system later
2. look for boot disk, each BIOS will have boot menu, you can set the device in the menu to start the system
For example: CD-ROM, hard disk, network and so on, this menu can set a number of options, in accordance with the set order in the device to find boot information
3. found the boot disk, and then BIOS will find the boot sector on disk to find the kernel boot system
1) try to find the first sector, that is, the master boot record MBR (Master, Boot, Record)
2) if there is no operating system on the first sector, then go to the partition marked BOOT
Whether it is MBR or BOOT partitioning, the storage operating system has only 446 bytes of space;
If the kernel of the operating system is not found in the location mentioned above, then BIOS will not be able to proceed with the boot
And our kernel tends to be more than 446 bytes, stored in other locations on the disk, since 446 can not install the kernel, but also in order to successfully find the other place in the kernel,
People came up with a solution in 446 bytes to write a small program, when BIOS tried to execute the program will start the operating system, and then by a small program to load other position. This small program is the kernel boot loader (boot loader)
Two, BOOT Loader
Linux boot loader (small program mentioned above), there are two common: Lilo, grub
Because the Lilo has a 1024 cylinder limit, and change the startup information file on disk need to restart your system to synchronize 446 bytes of content, because of the defects of Lilo, has been replaced by grub. The vast majority of Linux systems are now using grub boot loader;
Grub does not have any Lilo restrictions and is convenient to synchronize the contents of the system file with the 446 content immediately
Well, let's look at the content of grub, which exists in /boot/grub
GRUB has several important files, STAGE1, STAGE1.5, and STAGE2
STAGE1: it's only 512 bytes. It's usually in MBR. It's simple enough to load STAGE2 and give control to it when the system starts
The core of STAGE2:GRUB, all functions are implemented by it
STAGE1.5: between STAGE1 and STAGE2, is their bridge, because STAGE2 is larger, usually in a file system, but STAGE1 does not recognize the file system format,
So I need to guide STAGE1.5 in a file system based on STAGE2. file system format, STAGE1.5 also need the corresponding documents, such as: e2fs_stage1_5, fat_stage1_5, were used to identify ext and fat file system format. It is stored in the 1-63 cylinder.
The boot sequence is as follows: STAGE1->; STAGE1.5->; STAGE2,
The main configuration file is grub.conf
Inside option meaning:
A title: operating system enables the header, you can make multiple
Root: indicates which disk exists on which disk, which partition (hd0,0) represents the first hard disk, and the first partition, which can refer to /boot/grub/device.map
The name of the kernel: kernel file and some parameters that are loaded into the kernel. Or stands for read-only loading
Initrd: contains some additional drivers
Three, the kernel starts
When the kernel is started, it will query all the hardware information of the computer from the BIOS and then take charge of the devices themselves so that they can be used by the Linux
The kernel will try to drive these devices, some of which are included in the kernel, called static drives, and some are stored in modules (dynamically) in file systems
At the same time also did not mount any file system, it cannot be used in the file system module, here only drive in the kernel of the hardware driver to drive the corresponding equipment. Not included in the kernel hardware driver will need to load the file system.
The kernel attempts to mount the root file system, and the root file system contains at least /etc, /bin, /sbin, /lib, and /dev, these 5 directories
5 of them are indispensable. They can cause the system to not start
The root file system mount is mounted read-only because the Linux is still in the boot phase and is unstable and avoids breaking system data
Four, start the INIT service
After you successfully mount the root file system, you start the init service
The kernel looks for the init program in /sbin/init, /etc/init, and /bin/init order,
If the kernel error is not found, there is a parameter init=/bin/sh. If it is specified at startup, the /bin/sh is finally executed
Without kernel error reporting, it is also called quick start because it ignores a lot of boot initialization
The purpose of starting init is to initialize the system environment, and the startup of the init proves that the kernel has been successfully started, and then the Linux service environment is built by the init service
What does init do? It reads the /etc/inittab file and initializes it based on the information of that file
Three scripts will be executed: /etc/rc.d/rc.sysinit /etc/rc.d/rc /etc/rc.d/rc.local
The main function of rc.sysinit is to establish the basic environment of the system
Start the udev SELinux subsystem
Udev is responsible for generating files in /dev, and SELinux is responsible for enhancing the security of the system
Set the kernel parameter, sysctl -p, and load /etc/sysctl.conf
Set system time and set the hardware time to system time
Load keyboard and swap partition swapon -a -e
Set the hostname, mount the file system, and re mount the root for readable writing
Load dynamic driver module
USB devices and RAID & LVM
Uninstall the /initrd/ directory
The RC script sets the boot level. There are many different startup levels in Linux, and different startup levels make different services
The connection script file in /etc/rc.d/rc[0-6].d/ executes in accordance with the specified RC parameter, starting at the start of S
The first two scripts of rc.local are system critical scripts, if we want to put some other programs in the boot process
You should use the rc.local script because S99local exists in rc[2-5].d
After the execution of the three RC script, the virtual master console is built, and the user login interface is provided when the /bin/login is executed
If it is a graphical L5 level, the X window login screen will start
All the boot is finished
User environment initialization
Enter user name and password, system authentication, and then execute
/etc/profile
/etc/profile.d/*.sh $HOME/.bash_profile
/etc/bashrc
$HOME/.bashrc
Boot level
0 power off
1 single user
More than 2 user mode but with network but not telnet
3 unlimited multi-user mode TUI
4 reservations
5 graphics mode
6 restart
View start level
# runlevel
N 3
N is the last level, and 3 is the current level, that is, the machine boot is the 3 level
Handover level
Init 5
Mount file system
/etc/fstab
The first is to mount storage device physical location or label
To which directory will the second item be hung?
The third specifies the system format to mount
Fourth mount to set the state, read-only or defauls
The fifth item provides the DUMP function, which requires BACKUP flag bits in the system DUMP, and the default location is 0
The sixth setting is whether the filesystem will be check at boot time
In addition to the root file system, the necessary check is 1, other depending on the need, and the default is 0
LABEL=/boot /boot ext3 defaults 1 2

Latest