Detailed explanation of Linux startup process

Infineon / Mitsubishi / Fuji / Semikron / Eupec / IXYS

Detailed explanation of Linux startup process

Posted Date: 2024-01-24

Generally speaking, the Linux startup process can be divided into three stages:

Linux startup flow chart

Step one: power on

ØIn x86 systems, map the top 64K of 1M space from 0xF0000 to 0xFFFFF to ROM.

ØWhen the computer is first powered on, it will do some reset work, set CS to 0xFFFF, and set IP to 0x0000, so the first instruction will point to 0xFFFF0, which is within the range of ROM.

ØHere, there is a JMP command that will jump to the initialization code in the ROM, so the BIOS starts the initialization work.

Step 2: BIOS startup

Firmware initialization: After the computer is turned on, the UEFI firmware will be initialized, including hardware initialization, self-test and loading of UEFI firmware drivers, etc.

Boot device selection: UEFI firmware will detect and identify bootable devices, such as hard drives, optical disks, USB devices, etc. It will select a bootable device as the boot media based on the preset boot sequence or the boot options set by the user.

UEFI firmware driver loading: UEFI firmware will load the UEFI firmware drivers on the device. These drivers are responsible for interacting with the hardware device so that the subsequent boot process can proceed normally.

UEFI application loading: UEFI firmware loads UEFI applications located on the boot media, such as the boot loader or the operating system's boot manager. These applications are usually located in the EFI system partition and exist in .efi file format.

Boot loader execution: The loaded boot loader takes over control and is responsible for loading the operating system kernel or other boot components. Common boot loaders include UEFI Shell, GRUB, rEFInd, etc.

Step 3: Linux startup

Linux startup-boot

ØWe can select the hard disk startup item to enter the OS through the BIOS interface. How does the BIOS discover that there is an OS in this hard disk?

ØThe answer is MBR (Master Boot Record),

ØMBR is placed in the first sector of the hard disk, with a total of 512 bytes.

ØCan be divided into two parts:

ØMaster boot record: where the bootloader is installed, 446 bytes,

ØPartition table: records the status of the entire hard disk partition. In addition, 64 bytes

Linux boot-boot EBR/VBR

ØWhat’s the next step after finding MBR?

Ø(1) If you find that the operating system is installed in the primary partition when looking up the partition table, then execute the boot loader code in the loaded MBR and load the boot loader in the VBR that activates the primary partition. At this point, control is handed over to VBR boot loader

Ø(2) If the operating system is not installed in the primary partition, it must be installed in the logical partition. Therefore, after searching the primary partition table, it will continue to search the extended partition table until it finds the partition where the EBR is located, and then the boot loader in the MBR will Control is given to the boot loader of the EBR

Linux startup-booting GRUB2 introduction

ØGNU GRUB (GRand Unified Bootloader referred to as "GRUB") is a multi-operating system boot program from the GNU project. GRUB is an implementation of the multi-boot specification, which allows users to have multiple operating systems in the computer at the same time and select the operating system they want to run when the computer starts. GRUB can be used to select different kernels on an operating system partition and can also be used to pass boot parameters to these kernels.

ØGenerate configuration file: grub2-mkconfig -o /boot/grub2/grub.cfg

ØInstallation: grub2-install /dev/sda

Linux startup-boot GRUB2 loading

The first thing ØGrub2 installs is boot.img. After the BIOS completes the task, boot.img will be loaded from the hard disk to 0x7c00 in the memory to run. boot.img will load core.img. If booting from the hard disk, this sector contains diskboot.img. The task of diskboot.img is to load other parts of core.img. First, the decompression program lzma_decompress.img, then kernel.img, and finally each The image corresponding to the module module. It should be noted here that it is not the Linux kernel, but the grub kernel.

ØAs the things we load become larger and larger, the 1M address space of real mode cannot be accommodated, so before actual decompression, lzma_decompress.img made an important decision, which is to call real_to_prot and switch to protected mode. , so that more things can be loaded in a larger addressing space.

Linux startup-process 0/1

Øset_task_stack_end_magic(&init_task). There is a parameter init_task, which is defined as struct task_struct init_task = INIT_TASK(init_task).

ØIt is the first process created by the system, we call it process No. 0. This is the only process not spawned by fork or kernel_thread, and is the first in the process list.

Ø1.trap_init() interrupts initialization

Ø2.mm_init() memory initialization

Ø3.sched_init() scheduling policy initialization

Ø4.vfs_caches_init() initialization based on memory file system rootfs

Ø5.start_kernel()->rest_init() other aspects of initialization

The first major job of Ørest_init is to create a second process using kernel_thread(kernel_init, NULL, CLONE_FS). This is process No. 1. Process No. 1 has "epoch-making" significance for the operating system.

Linux boot-ramdisk

The Øinit program is on the file system, and the file system must be on a storage device, such as a hard disk. Linux requires a driver to access storage devices. If the number of storage systems is very limited, the driver can be placed directly in the kernel. However, there are many file system formats, and if they are all put into the kernel, the kernel will be too large.

ØWhat to do?

ØWe had to get a memory-based file system first. Memory access does not require a driver, this is ramdisk. At this time, the ramdisk is the root file system.

ØRun /init on the ramdisk, and when it finishes running, you will be in user mode. /init This program will first load the driver according to the type of storage system. With the driver, you can set up the real root file system. With a true root filesystem, /init on the ramdisk starts init on the filesystem.

Linux startup-init introduction

ØThe previous 0/1 processes all belong to kernel threads, and the one with ps pid=1 is the init process.

Ø if (ramdisk_execute_command) {

Øret = run_init_process(ramdisk_execute_command);



Øif (!try_to_run_init_process("/sbin/init") ||

Ø!try_to_run_init_process("/etc/init") ||

Ø !try_to_run_init_process("/bin/init") ||

Ø!try_to_run_init_process("/bin/sh")) return 0

ØIt will try to run "/init" on ramdisk, or "/sbin/init" "/etc/init" "/bin/init" "/bin/sh" on a normal file system. Different versions of Linux will choose different files to start, but as long as one is up, it will be fine.

ØInit type:

ØSysV: Before CentOS 5, configuration file /etc/inittab

ØUpstart: CentOS 6, configuration files/etc/inittab,/etc/init/*.conf

ØSystemd:CnetOS7, configuration file/usr/lib/system/syste,/etc/systemd/system

Linux boot-runlevel

Linux boot-fstab

ØAfter any hardware device is connected, the operating system uses the hardware and needs to be mounted. Windows just "mounts" automatically, Linux needs to be done manually. In Linux systems, for example, each time you mount /dev/sdb1 (such as a USB disk device file), you need to manually use the mount command. Of course, every time you restart or turn on the hard disk, the hard disk is usually automatically mounted, and the automatic mounting information is recorded in the /etc/fstab file.

Ø Every time the system starts, it will read the configuration content in /etc/fstab and automatically mount the devices and partitions recorded in the file.

Ø The first column: device file or UUID or label (see the difference between the three below)

Ø Second column: Mount point of the device (empty directory)

Ø The third column: the format of the partition file system (you can use the special parameter auto to automatically identify the partition format of the partition)

Ø The fourth column: file system parameters, formatting options

Ø The fifth column: dump backup settings (0 means no dump backup, 1 means dump backup every day, 2 means dump backup on an irregular date)

Ø Sixth column: Disk check setting (actually a check sequence, 0 means no check, 1 means the first check, 2 follows. Generally, the root directory is 1, if the numbers are the same, they will be checked at the same time)

Linux startup-user login

Generally speaking: there are three ways for users to log in

1. Command line login

2.SSH login

3. Graphical login

Linux is a multi-tasking and multi-user operating system that allows multiple people to work online at the same time. But everyone must enter a username and password to authenticate themselves and ultimately log in. But when logging in, should it be used by users in a graphical interface or in pure command line mode? This is determined by the terminal, which means that the terminal needs to be loaded before logging in.

On modern Linux, the console terminal is no longer the same as its original meaning. Its device is mapped on /dev/console. All information output by the kernel is output to the console terminal, while information output by other user programs is output to a virtual terminal or Pseudo terminal.

To summarize:

/dev/console: console terminal

/dev/ttyN: virtual terminal, ctrl+alt+f (1-6) switches to the virtual terminal

/dev/ttySN: serial terminal

/dev/pts/N: Pseudo terminal. The command line terminal opened under the graphical terminal connected to ssh and other tools is the pseudo terminal.

Linux startup-user switching

Linux default provides six command window terminals for us to log in to.

By default, we log in to the first window, which is tty1. The six windows are tty1, tty2...tty6. You can press Ctrl + Alt + F1 ~ F6 to switch them.


ØFor my VM Virtual BOX ctrl+alt + F1 is the graphical terminal, ctrl+alt + F2~F6 command line terminal

Linux startup process mind map

Review Editor: Tang Zihong

#Detailed #explanation #Linux #startup #process