Article Directory
System startup process (Linux)
1. BIOS 加电自检
2. 把 MBR 加载到内存
3. 加载 grub 引导程序
4. Kernel 自身初始化
5. 启动第一个程序 systemd
BIOS
- Initialize the hardware
- find boot device
1 - CD-DVD - 光驱启动
2 - PXE - 网络启动
3 - USB - USB启动
4 - HDD (硬盘启动 - Hard Disk) MBR - 主引导记录
MBR
1 - 446B BootLoader:安装GRUB的stage1 或 GRUB2的 boot.img
2 - 64B partition table 主分区表:四个主分区
3 - 2B magic number AAH 55H 标识
GRUB2 boot boot
1 - boot.img 唯一作用是在系统启动时装载 core.img ,即装载内核
2 - core.img 是GRUB2的核心映像文件
3 - *.mod 可以动态加载的模块
4 - grub.cfg
如果 /boot 在独立分区,则:/grub2/grub.cfg
如果 /boot 不在独立分区,则: /boot/grub2/grub.cfg
kernel boot
1 - /boot/vmlinuz Linux内核映像及内核参数传递
2 - /boot/initramfs
首先:在内存中释放供内核使用的根文件系统(/boot/initramfs)
然后,挂载真正的根文件系统,然后执行 /usr/lib/systemd/systemd
systemd
1 - 读取 /etc/systemd/system
2 - default.target - 链接文件
3 - graphical.target - 图形界面
4 - 启动 图形界面用户登录 进程
Firmware and BIOS
In the early days, the BIOS was burned on the motherboard;
the BIOS on the computer motherboard was previously called firmware
[blue screen, hardware problem]
BIOS classification
1 - legacy
Legacy boot mode is currently the most commonly used BIOS boot mode, read the MBR boot record table
2 - uefi
Unified Extensible Firware Interface
translation: unified extensible firmware interface
- Is a new type of BIOS
- Compatible with traditional legacy startup methods
about legacy
Introduction to MBR
Using MBR (Main Boot Record, translation master boot record) partition table
Main boot partition: a total of 512 bytes ( 1 Byte = 8 bit
), divided into three parts
1 - 主引导程序 446B - 主引导程序所在位置
2 - 磁盘分区表 DPT 64B - 读取分区表
分区一 16B:
第1个字节:0x80 表示激活 - 激活的分区拥有控制权,四个分区只有一个是激活的
第2-4字节:分区第一个扇区的物理位置
第5个字节:主分区类型
第6-8字节:分区最后一个扇区的物理位置
第9-12字节:分区已用完的扇区数
第13-16字节:分区扇区总数
4 字节,32 位
一个主分区的扇区总数不超过:2^32个
每个扇区大小为 512 字节
单个分区最大空间为:2^32 * 512 = 2T
分区二 16B:
分区三 16B:
分区四 16B:
3 - 结束标志 2B 分区结束
MBR limitations
1 - up to four primary partitions, or up to 3 primary partitions + one extended partition
2 - maximum 2T per partition
About UEFI
kylin OS:
存在一个 ESP 分区(EFI系统分区)
uefi的引导程序是以后缀名为 .efi 的文件存放在 ESP 分区中,ESP 分区采用 fat32 文件系统
kylinos将 ESP 分区挂载到 /boot/efi ,直接用文件夹浏览器进去就可以查看到 .efi 文件
GPT
Adopt GPT (GUID Partition Table) partition table
translation: globally unique identifier disk partition table
Advantage
- Size can support up to 128 partitions
- The size of each partition can be 2^64 *512
- fault tolerance, compatibility
bootloader boot manager
After the computer reads the 446B machine code in front of the MBR, it will hand over control to the Boot Loader
The Bootloader under Linux has:
- grub
- grub2
- use
- spfdisk
The main function of BootLoader
1 - 提供菜单
用户可以选择不同的启动项
2 - 加载内核文件
直接指向可启动的程序区段来开始操作系统
3 - 转交给其他 Loader
将引导装载功能转交给其他Loader负责
kernel initialization
The kernel file is very small, only the most basic modules are kept, and stored in the hard disk as a compressed file
Kernel files are placed in /boot
the directory
BIOS - bootloader - kernel
initrd - mirror file
systemd - daemon process
- systemd is a Linux system tool used to start daemons and has become standard in most distributions
- systemd replaces initd and becomes the first process of the system (PID=1), and other processes are its child processes
- systemd, can be concurrent, can trigger process groups
- systemd - not one command, but a set of commands
- systemd - located in /etc/systemd/system
kylinos采用过多种启动方式
1 - Sysvinit
2 - upstart
3 - system
4 - systemd
1 - systemd 执行的第一个目标是: default.target;实际上,图形化系统的default.target是指向graphical.target的软链接 (.target 是一组命令)
The commonly used target corresponds to the Sysvinit run level
runlevel runlevel | name | describe |
---|---|---|
basic.target | Start the base system, which indirectly contains all local mount point units and other necessary system initialization units | |
default.target | The default startup target, usually points to multi-user.target or graphical.target | |
5 | graphical.target | A target unit dedicated to launching a graphical login interface, which contains the multi-user.target unit |
0 | halt.target | Dedicated unit to start up when the system is shut down but not switched |
0 | poweroff.target | Stop the system and cut off the power |
local-fs.target | Target unit dedicated to aggregating local filesystem mount points | |
3 | multi-user.target | For units that are dedicated to multi-user and started in command line mode, the [install] section should add the WantedBy=multi-user.target command |
1 | rescure.target | A unit that needs to be started to boot the base system and open a rescue shell |
emergency.target | Single-user mode , no service programs are running, and the file system is not mounted. This is the most basic runlevel, providing only a shell on the main console for user interaction with the system. | |
local-fs-pre.target | This target unit is automatically ordered before any local filesystem mount points that require automounting. Can be used to ensure that certain units are started before the local filesystem is mounted. | |
6 | reboot.target | Restart the system, dedicated to the units that need to be started when the system is restarted |
After mounting the file system, run each target in turn, and finally run to the target linked to default.target (the default is grapgical.target and multi-user.target), so that the user can log in to the system through the graphics or the control terminal
System boot complete
Summarize
1 - 计算机加电到完成启动,包含多个进程;
2- 系统启动需要固件支持。legacy 采用的时 MBR分区表,MBR分区表的结构决定了它支持的分区数量和大小;
3 - 当主机存在多个操作系统时,需要bootloader管理和启动系统;
4 - kylinos V10 目前采用的初始化进程是 systemd
5 - 当systemd作为启动进程进行时,它将初始化系统,也就是启动并维护各种用户空间的服务