Explain the principle of linux system startup in detail

Linux system startup process

1. First read the kernel file under boot:
[External link image transfer failed. The source site may have an anti-leech link mechanism. It is recommended to save the image and upload it directly (img-BB0wI14u-1615525326660)(en-resource://database/9240:1)]

2. The init process reads the configuration file:
Insert picture description here

pid=1, complete the transition from kernel mode to user mode

Linux kernel startup process:

Between 1 and 12, the linux kernel starts:
1. At 1, the self-extracting
kernel file of the kernel is loaded into the memory using the code kernel/arch/arm/boot/compressed , the page table data structure is established, and the address mapping is performed ,

2. Then enter the start_kernel function:
set the work related to the initialization of the architecture (x86, etc.)
Insert picture description here

Create the kernel page table, MMU, interrupt handling function, core process scheduler, clock interrupt handling mechanism, serial console, cache, memory management, system inter-process communication mechanism IPC and
then start to mount the root file system

3. Mount the root file system,
including etc, bin, sbin, lib, dev, etc., and mount it in a read-only mode.
(The five major file systems must be stored in the root file system)
Then find the init service in the file, and read the /etc/inittab file after the init service is started, and initialize according to the settings in /etc/inittab
Insert picture description here

(The kernel image is the kernel file, which refers to a form of kernel)

Detailed explanation of initramfs

Before the init process is started in the third, initramfs will be started. As an early user mode environment,
when initram is packaged, it is required to be packaged into a cpio file, which can be embedded in the kernel image or can be loaded by GRUB as a separate file during startup. The initrd.img-xxx (Ubuntu) or initramfs-xxx.img (CentOS) file in the /boot directory is the initramfs file for Linux.
[External link image transfer failed. The source site may have an anti-leech link mechanism. It is recommended to save the image and upload it directly (img-vWBQgGse-1615525326667)(en-resource://database/9250:1)]

Write a simple hello and compile it into a 32-bit program. If 64-bit cannot compile 32-bit, run the command:
[External link image transfer failed. The source site may have an anti-leech link mechanism. It is recommended to save the image and upload it directly (img-G9rpJ2Zs-1615525326669)(en-resource://database/9254:1)]

sudo apt-get install gcc-multilib
gcc -static -o helloworld -m32 helloworld.c
echo helloworld | cpio -o --format=newc > hwinitramfs
qemu -kernel arch/x86/boot/bzImage -initrd hwinitramfs -append "console=ttyS0 rdinit=helloworld" -nographic

[External link image transfer failed. The origin site may have an anti-leech link mechanism. It is recommended to save the image and upload it directly (img-pbsGC1dN-1615525326670)(en-resource://database/9252:1)]

You can see that the hello world is successfully output and is held in user mode

GRUB detailed

GNU GRUB is a multi-operating system startup program, a multi-operating system startup manager,
you can select the kernel of the operating system, or you can pass parameters to the kernel

Guess you like

Origin blog.csdn.net/qq_42882717/article/details/114693288