第十四天:Linux启动流程

一、CentOS 6 之前版本的启动流程

1.1 硬件启动POST
POST:Power-On-Self-Test,加电自检,是BIOS功能的一个主要部分。负责完成对CPU、主板、内
存、硬盘子系统、显示子系统、串并行接口、键盘等硬件情况的检测
主板的ROM:BIOS,Basic Input and Output System,保存着有关计算机系统最重要的基本输入输出
程序,系统信息设置、开机加电自检程序和系统启动自举程序等
主板的RAM:CMOS互补金属氧化物半导体,保存各项参数的设定,按次序查找引导设备,第一个有引
导程序的设备为本次启动设备
1.2 启动加载器 bootloader
1.2.1 grub 功能和组成
bootloader: 引导加载器,引导程序
Windows: ntloader,仅是启动OS
Linux:功能丰富,提供菜单,允许用户选择要启动系统或不同的内核版本;把用户选定的内核装
载到内存中的特定空间中,解压、展开,并把系统控制权移交给内核
Linux的bootloader
LILO:LInux LOader,早期的bootloader,功能单一
GRUB: GRand Unified Bootloader, CentOS 5,6 GRUB 0.97: GRUB Legacy, CentOS 7 以后使
用GRUB 2.02
GRUB 启动阶段
primary boot loader :
1st stage:MBR的前446个字节
1.5 stage:MBR 之后的扇区,让stage1中的bootloader能识别stage2所在的分区上的文件系统
secondary boot loader :2nd stage,分区文件/boot/grub/
1.2.2 CentOS 6 grub 安装
安装 grub的两种方法:
(1) grub-install 安装grub stage1和stage1_5到/dev/DISK磁盘上,并复制GRUB相关文件到 DIR/boot目
录下grub-install --root-directory=DIR /dev/DISK
(2) grub命令
#grub
grub> root (hd#,#)
grub> setup (hd#)
1.2.3 grub legacy 管理
配置文件:/boot/grub/grub.conf <-- /etc/grub.conf
stage2及内核等通常放置于一个基本磁盘分区
grub legacy 功用:
(1) 提供启动菜单、并提供交互式接口
a:内核参数
e:编辑模式,用于编辑菜单
c:命令模式,交互式接口
(2) 加载用户选择的内核或操作系统
允许传递参数给内核
可隐藏启动菜单
(3) 为菜单提供了保护机制
为编辑启动菜单进行认证
为启用内核或操作系统进行认证
 
1.3 加载 kernel
 
kernel 自身初始化过程
1. 探测可识别到的所有硬件设备
2. 加载硬件驱动程序(借助于ramdisk加载驱动)
3. 以只读方式挂载根文件系统
4. 运行用户空间的第一个应用程序:/sbin/init
Linux内核特点:
支持模块化:.ko(内核对象),如:文件系统,硬件驱动,网络协议等
支持内核模块的动态装载和卸载
内核组成部分:
核心文件:/boot/vmlinuz-VERSION-release
模块文件:/lib/modules/VERSION-release
 
1.4 init初始化
POST --> BootSequence (BIOS) --> Bootloader(MBR) --> kernel(ramdisk) --> rootfs(只读) --> init
(systemd)
init程序的类型:
SysV: init, CentOS 5之前
配置文件:/etc/inittab
Upstart: init,CentOS 6
配置文件:/etc/inittab, /etc/init/*.conf
Systemd:systemd, CentOS 7
配置文件:/usr/lib/systemd/system
/etc/systemd/system

 init  切换级别

runlevel  查看级别
who -r   查看级别
/etc/inittab    定义运行级别
 
初始化脚本 sysinit   
/etc/rc.d/rc.sysinit

 

 

 

二、systemd和启动流程   CentOS 7 以后版本启动流程
2.1 systemd 特性
Systemd:从 CentOS 7 版本之后开始用 systemd 实现init进程,系统启动和服务器守护进程管理器,
负责在系统启动或运行时,激活系统资源,服务器进程和其它进程
(1)Systemd 新特性
  系统引导时实现服务并行启动
  按需启动守护进程
  自动化的服务依赖关系管理
  同时采用socket式与D-Bus总线式激活服务
  socket与服务程序分离
  向后兼容sysv init脚本
  使用systemctl 命令管理,systemctl命令固定不变,不可扩展,非由systemd启动的服务,
  systemctl无法与之通信和控制
  系统状态快照 
(2)systemd 核心概念:unit
unit表示不同类型的systemd对象,通过配置文件进行标识和配置;文件中主要包含了系统服务、监听
socket、保存的系统快照以及其它与init相关的信息
    
service unit: 文件扩展名为.service, 用于定义系统服务
Socket unit: .socket, 定义进程间通信用的socket文件,也可在系统启动时,延迟启动服务,实现
按需启动
Target unit: 文件扩展名为.target,用于模拟实现运行级别
Device unit: .device, 用于定义内核识别的设备
Mount unit: .mount, 定义文件系统挂载点
Snapshot unit: .snapshot, 管理系统快照
Swap unit: .swap, 用于标识swap设备
Automount unit: .automount,文件系统的自动挂载点
Path unit: .path,用于定义文件系统中的一个文件或目录使用,常用于当文件系统变化时,延迟激
活服务,如:spool 目录 

 

2.2 systemctl管理系统服务service unit 
命令:systemctl COMMAND name.service
#启动:相当于service name start           systemctl start name1.service [name2.service] ...
#停止:相当于service name stop      systemctl stop name.service
#重启:相当于service name restart    systemctl restart name.service
#查看状态:相当于service name status    systemctl status name.service
#禁止自动和手动启动:          systemctl mask name.service
#取消禁止              systemctl unmask name.service
#查看某服务当前激活与否的状态:  systemctl is-active name.service
#查看service文件内容        systemctl cat sshd
#查看所有已经激活的服务:      systemctl list-units --type|-t service
#查看所有服务:            systemctl list-units --type service --all|-a
#设定某服务开机自启,相当于chkconfig name on    systemctl enable name.service
#设定某服务开机禁止启动:相当于chkconfig name off    systemctl disable name.service
#查看所有服务的开机自启状态,相当于chkconfig --list    systemctl list-unit-files --type service
#用来列出该服务在哪些运行级别下启用和禁用:chkconfig –list name    ls /etc/systemd/system/*.wants/name.service
#查看服务是否开机自启:        systemctl is-enabled name.service
#列出失败的服务          systemctl --failed --type=service
 #开机并立即启动或停止      systemctl enable --now postfix    systemctl disable  --now postfix
#查看服务的依赖关系:      systemctl list-dependencies name.service
#杀掉进程:          systemctl kill unitname

 #显示所有单元状态        systemctl 或 systemctl list-units

#只显示服务单元的状态      systemctl --type=service
#显示sshd服务单元        systemctl –l status sshd.service
#验证sshd服务当前是否活动      systemctl is-active sshd
#启动,停止和重启sshd服务      systemctl start sshd.service
                  systemctl stop sshd.service
                  systemctl restart sshd.service
#重新加载配置            systemctl reload sshd.service
#列出活动状态的所有服务单元      systemctl list-units --type=service
#列出所有服务单元        systemctl list-units --type=service --all
#查看服务单元的启用和禁用状态    systemctl list-unit-files  --type=service
#列出依赖的单元        systemctl list-dependencies sshd
验证sshd服务是否开机启动    systemctl is-enabled sshd
禁用network,使之不能自动启动,但手动可以  systemctl disable network
#启用network      systemctl enable network
#禁用network,使之不能手动或自动启动    systemctl mask network
#启用network      systemctl unmask network
 
2.3 service unit文件格式
/etc/systemd/system:系统管理员和用户使用
/usr/lib/systemd/system:发行版打包者使用 

 

Service段的常用选项:
Type:定义影响ExecStart及相关参数的功能的unit进程启动类型
simple:默认值,这个daemon主要由ExecStart接的指令串来启动,启动后常驻于内存中
forking:由ExecStart启动的程序透过spawns延伸出其他子程序来作为此daemon的主要服
务。原生父程序在启动结束后就会终止
oneshot:与simple类似,不过这个程序在工作完毕后就结束了,不会常驻在内存中
dbus:与simple类似,但这个daemon必须要在取得一个D-Bus的名称后,才会继续运作.因
此通常也要同时设定BusNname= 才行
notify:在启动完成后会发送一个通知消息。还需要配合 NotifyAccess 来让 Systemd 接收消
idle:与simple类似,要执行这个daemon必须要所有的工作都顺利执行完毕后才会执行。这
类的daemon通常是开机到最后才执行即可的服务
EnvironmentFile:环境配置文件
ExecStart:指明启动unit要运行命令或脚本的绝对路径
ExecStartPre: ExecStart前运行
ExecStartPost: ExecStart后运行
ExecStop:指明停止unit要运行的命令或脚本
Restart:当设定Restart=1 时,则当次daemon服务意外终止后,会再次自动启动此服务
RestartSec: 设置在重启服务( Restart= )前暂停多长时间。 默认值是100毫秒(100ms)。 如果未指
定时间单位,那么将视为以秒为单位。 例如设为"20"等价于设为"20s"。
PrivateTmp:设定为yes时,会在生成/tmp/systemd-private-UUID-NAME.service-XXXXX/tmp/目
Install段的常用选项:
Alias:别名,可使用systemctl command Alias.service
RequiredBy:被哪些units所依赖,强依赖
WantedBy:被哪些units所依赖,弱依赖
Also:安装本服务的时候还要安装别的相关服务
 
 
2.4 运行级别
 target units:相当于CentOS 6之前的runlevel ,unit配置文件:.target
 

 

查看依赖性:
systemctl list-dependencies graphical.target
级别切换:相当于 init N
systemctl isolate name.target 
进入默认target 
systemctl default 
获取默认运行级别: 相当于查看 /etc/inittab
systemctl get-default 
修改默认级别:相当于修改 /etc/inittab 
systemctl set-default name.target
切换至紧急救援模式:
systemctl rescue
切换至emergency模式
systemctl emergency 
 
2.5 设置内核参数 
systemd.unit=multi-user.target
systemd.unit=emergency.target
systemd.unit=rescue.target 
 
2.6 CentOS 7之后版本引导顺序
1. UEFi或BIOS初始化,运行POST开机自检
2. 选择启动设备
3. 引导装载程序, centos7是grub2,加载装载程序的配置文件:
/etc/grub.d/
/etc/default/grub
/boot/grub2/grub.cfg
4. 加载initramfs驱动模块(可以实现根文件系统的挂载)
5. 加载虚拟根中的内核
6. 虚拟根的内核初始化,Centos7使用systemd代替init,第一个进程
7. 执行initrd.target所有单元,包括挂载/etc/fstab
8. 从initramfs根文件系统切换到磁盘根目录
9. systemd执行默认target配置,配置文件/etc/systemd/system/default.target
10. systemd执行sysinit.target初始化系统及basic.target准备操作系统
11. systemd启动multi-user.target下的本机与服务器服务
12. systemd执行multi-user.target下的/etc/rc.d/rc.local
13. Systemd执行multi-user.target下的getty.target及登录服务
14. systemd执行graphical需要的服务
 
 
2.7 破解 root 密码
方法一

 方法二

 

2.8 实现 GRUB2 安全
#添加grub密码
[root@centos8 ~]#grub2-setpassword
Enter password:
Confirm password:
[root@centos8 ~]#ls -l /boot/grub2/
 
清空密码
[root@centos8 ~]#cat /dev/null > /boot/grub2/user.cfg
#或者
[root@centos8 ~]#rm -f /boot/grub2/user.cfg 
 
2.9 修复 GRUB2

 

 
2.10 故障排错实战案例 
 

 

 grub2-install /dev/sda  #BIOS环境

grub2-install #UEFI环境
2安装Kernel
#CentOS 7
mount /dev/sr0 /mnt
rpm –ivh /mnt/Packages/kernel-3.10.0-1062.el7.x86_64.rpm --force
#CentOS 8
mount /dev/sr0 /mnt
rpm -ivh /mnt/BaseOS/Packages/kernel-core-4.18.0-147.el8.x86_64.rpm --force
3修复grub配置文件
#生成grub.cfg文件
grub2-mkconfig –o /boot/grub2/grub.cfg
4 退出重启
sync
sync
exit
exit
 
3 /proc 目录和内核参数管理
/proc目录:内核把自己内部状态信息及统计信息,以及可配置参数通过proc伪文件系统加以输出
帮助:man proc
内核参数:
只读:只用于输出信息
可写:可接受用户指定“新值”来实现对内核某功能或特性的配置
/proc/sys 设置

 

 

 

 
posted @ 2024-01-27 21:13  djyhello  阅读(74)  评论(0编辑  收藏  举报