id: e24ceb1803714090935da40d9e5c0c15
parent_id: 
item_type: 1
item_id: 03095c206c9c49ba8b1f83d351453f20
item_updated_time: 1764345626455
title_diff: "[{\"diffs\":[[1,\"【总结 】 Linux系统启动流程\"]],\"start1\":0,\"start2\":0,\"length1\":0,\"length2\":17}]"
body_diff: "[{\"diffs\":[[1,\"<html>\\\r\\\n<head>\\\r\\\n  <title>【总结 】 Linux系统启动流程</title>\\\r\\\n  <basefont face=\\\"微软雅黑\\\" size=\\\"2\\\" />\\\r\\\n  <meta http-equiv=\\\"Content-Type\\\" content=\\\"text/html;charset=utf-8\\\" />\\\r\\\n  <meta name=\\\"exporter-version\\\" content=\\\"YXBJ Windows/608924 (zh-CN, DDL); Windows/10.0.0 (Win64); EDAMVersion=V2;\\\"/>\\\r\\\n  <meta name=\\\"content-class\\\" content=\\\"yinxiang.peso.superNote\\\"/>\\\r\\\n  <meta name=\\\"application-data:yinxiang.noteLayout\\\" content=\\\"{\\\"lineSpace\\\":\\\"recommend\\\",\\\"paragraphSpace\\\":\\\"recommend\\\"}\\\"/>\\\r\\\n  <style>\\\r\\\n\\tbody, td {\\\r\\\n\\t  font-family: 微软雅黑;\\\r\\\n\\t  font-size: 12pt;\\\r\\\n\\t}\\\r\\\n  </style>\\\r\\\n</head>\\\r\\\n<body>\\\r\\\n<a name=\\\"39479\\\"></a>\\\r\\\n<h1>【总结 】 Linux系统启动流程</h1>\\\r\\\n\\\r\\\n<div><span style=\\\"\\\"><div style=\\\"max-width: 700px;\\\"><p></p><div style=\\\"--en-blockquote: true; box-sizing: border-box; border-left: 3px solid #b4c0cc; background-position: initial initial; background-repeat: initial initial;\\\"><div>原文链接: <a href=\\\"http://mp.weixin.qq.com/s?__biz=Mzg5NTcwNDcyMg==&amp;mid=2247497546&amp;idx=1&amp;sn=c1f14909268d19ea4749eb0029589e7e&amp;chksm=c138331005d7f922219b3d272d1c412dcde576be05a90a1fd2c7cefced6c92da7370ccba4ffd&amp;mpshare=1&amp;scene=1&amp;srcid=0407BEG6fiCgd2SNUiXirNO2&amp;sharer_shareinfo=3cc2eaf0a77b40fac5deaea22d73dbbd&amp;sharer_shareinfo_first=3cc2eaf0a77b40fac5deaea22d73dbbd#rd\\\"><span>http://mp.weixin.qq.com/s?__biz=Mzg5NTcwNDcyMg==&amp;mid=2247497...</span></a></div></div><p></p><div style=\\\"\\\"><div><div><div><span> hukey </span><span><a style=\\\"\\\"> 巴韭特锁螺丝 </a></span><span><em></em></span></div><div style=\\\"\\\"><p style=\\\"border: 0; vertical-align: baseline; box-sizing: border-box; text-indent: 0; color: rgb(44,44,44); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-transform: none; white-space: normal; widows: 2; word-spacing: 0; -webkit-text-stroke-width: 0; background-color: rgb(255,255,255); text-decoration-style: initial; text-decoration-color: initial;\\\"><strong><span>Linux系统启动过程分析：</span></strong></p><p style=\\\"border: 0; vertical-align: baseline; box-sizing: border-box; text-indent: 0; color: rgb(44,44,44); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-transform: none; white-space: normal; widows: 2; word-spacing: 0; -webkit-text-stroke-width: 0; background-color: rgb(255,255,255); text-decoration-style: initial; text-decoration-color: initial;\\\"><span style=\\\"border: 0px; vertical-align: baseline; box-sizing: border-box; color: rgb(153, 51, 102);\\\"><span>按下电源 --&gt; BIOS自检 --&gt; 系统引导(lilo/grub) --&gt; 启动内核 --&gt; 初始化系统 --&gt; 用户登录</span></span></p><p style=\\\"border: 0; vertical-align: baseline; box-sizing: border-box; text-indent: 0; color: rgb(44,44,44); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-transform: none; white-space: normal; widows: 2; word-spacing: 0; -webkit-text-stroke-width: 0; background-color: rgb(255,255,255); text-decoration-style: initial; text-decoration-color: initial;\\\"><span></span></p><p style=\\\"border: 0; vertical-align: baseline; box-sizing: border-box; text-indent: 0; color: rgb(44,44,44); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-transform: none; white-space: normal; widows: 2; word-spacing: 0; -webkit-text-stroke-width: 0; background-color: rgb(255,255,255); text-decoration-style: initial; text-decoration-color: initial;\\\"><span style=\\\"border: 0px; vertical-align: baseline; box-sizing: border-box; color: rgb(255, 0, 0);\\\"><span>1. BIOS自检：</span></span></p><p style=\\\"border: 0; vertical-align: baseline; box-sizing: border-box; text-indent: 0; color: rgb(44,44,44); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-transform: none; white-space: normal; widows: 2; word-spacing: 0; -webkit-text-stroke-width: 0; background-color: rgb(255,255,255); text-decoration-style: initial; text-decoration-color: initial;\\\"><span>    BIOS(basic input/output system), 又称基本输入输出系统，可以视为永久的记录在ROM中的一个软件，是操作系统输入输出管理的一部分。</span><span><br/></span><span>    BIOS的功能分为两种：POST码和Runtime服务。POST阶段完成后它将从存储器中被清除,而Runtime服务会一直保留,用于目标操作系统的启动</span><span><br/></span><span>    BIOS两个阶段所做的详细工作如下：</span><span><br/></span><span>        (1)上电自检POST(power-on self test), 主要负责检测外围关键设备是否正常工作。例如：最常见的是内存松动，BIOS自检阶段就会报错。</span><span><br/></span><span>        (2)步骤1成功后，便会执行一段小程序用来枚举本地设备并对其初始化。这一步主要是根据我们在BIOS中设置的系统启动顺序来搜索用于启动系统的驱动器，</span><span><br/></span><span>        如：硬盘、光盘、U盘、软盘和网络等。</span><span><br/></span><span>        以硬盘为例：BIOS此时去读取硬盘的第一个扇区(MBR 512字节)，然后执行里面的代码。实际上这里BIOS不管第一个扇区里面是什么内容，它只是负责读取该扇区内容并执行。</span><span><br/></span><span>        至此，BIOS的任务就完成了，此后将系统启动的控制权移交MBR部分代码。</span></p><p style=\\\"border: 0; vertical-align: baseline; box-sizing: border-box; text-indent: 0; color: rgb(44,44,44); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-transform: none; white-space: normal; widows: 2; word-spacing: 0; -webkit-text-stroke-width: 0; background-color: rgb(255,255,255); text-decoration-style: initial; text-decoration-color: initial;\\\"><span></span></p><p style=\\\"border: 0; vertical-align: baseline; box-sizing: border-box; text-indent: 0; color: rgb(44,44,44); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-transform: none; white-space: normal; widows: 2; word-spacing: 0; -webkit-text-stroke-width: 0; background-color: rgb(255,255,255); text-decoration-style: initial; text-decoration-color: initial;\\\"><span style=\\\"border: 0px; vertical-align: baseline; box-sizing: border-box; color: rgb(255, 0, 0);\\\"><span>2. 系统引导：</span></span><span><br/></span><span>    MBR(master boot record): 磁盘的0柱面、0磁头、1扇区称为主引导扇区。它由三个部分组成：主引导程序(bootloader)、硬盘分区表DPT、硬盘有效标志</span><span><br/></span><span>    通常情况下，诸如：lilo/grub 这些常见的引导程序都直接安装在MBR的bootloader中。</span><span><br/></span><span></span><span><br/></span><span>    grub引导也分为两个阶段stage1和stage2</span><span><br/></span><span>        stage1是直接写入到MBR的bootloader中的，机器启动完，就将控制权交给了bootloader, bootloader中就存放着stage1的代码，它的任务很单纯，仅仅是将硬盘0头0道2扇区</span><span><br/></span><span>        读入内存。而0头0道2扇区内容是源代码中的/stage2/start.S编译后的512字节，stage1是没有识别文件系统能力的。</span><span><br/></span><span></span></p><p style=\\\"border: 0; vertical-align: baseline; box-sizing: border-box; text-indent: 0; color: rgb(44,44,44); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-transform: none; white-space: normal; widows: 2; word-spacing: 0; -webkit-text-stroke-width: 0; background-color: rgb(255,255,255); text-decoration-style: initial; text-decoration-color: initial;\\\"><span style=\\\"border: 0px; vertical-align: baseline; box-sizing: border-box; color: rgb(255, 0, 0);\\\"><span>3. 启动内核：</span></span><span><br/></span><span>    当stage2被载入内存执行，首先会去解析/boot/grub/grub.conf，然后加载内核镜像到内存中，并将控制权转交给内核。而内核会立即初始化系统中各个设备并做相关配置</span><span><br/></span><span>    关于Linux设备驱动程序的加载，一部分是直接编译进内核，另一部分驱动程序则是以模块的形式放在initrd中。</span><span><br/></span><span>    实际上Linux内核仅是包含基本的硬件驱动，在系统安装过程中会检测系统硬件信息，根据安装信息和系统硬件信息将一部分设备驱动写入initrd，这样在以后启动系统时，一部分</span><span><br/></span><span>    设备驱动就放在initrd中来加载。</span><span><br/></span><span>    另一个概念：initramfs 当内核启动时，内核将这个cpio包解开,并且将其中包含的文件系统释放到rootfs中。内核中的一部分初始化代码会放到这个文件系统中，作为用户层进程来执行，</span><span><br/></span><span>    这样带来的明显好处是精简了内核初始化代码，而且使得内核的初始化过程更容易制定。</span><span><br/></span><span></span><span><br/></span><span>    grub的stage2将initrd加载到内存里，然后将其中的内容释放到内存中，内核便去执行initrd中的init脚本，这时内核将控制权交给了init文件处理。init主要是加载各种存储介质相关的设备驱动程序，</span><span><br/></span><span>    当所需的驱动程序加载完毕后，会创建一个根设备，然后将根文件系统以只读的方式挂载。到这一步，initrd就从内存中释放掉，转换到真正的根文件系统上面去，同时运行/sbin/init程序，执行</span><span><br/></span><span>    系统的1号进程。此后系统的控制权就全权交给了/sbin/init进程。</span></p><p style=\\\"border: 0; vertical-align: baseline; box-sizing: border-box; text-indent: 0; color: rgb(44,44,44); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-transform: none; white-space: normal; widows: 2; word-spacing: 0; -webkit-text-stroke-width: 0; background-color: rgb(255,255,255); text-decoration-style: initial; text-decoration-color: initial;\\\"><span></span></p><p style=\\\"border: 0; vertical-align: baseline; box-sizing: border-box; text-indent: 0; color: rgb(44,44,44); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-transform: none; white-space: normal; widows: 2; word-spacing: 0; -webkit-text-stroke-width: 0; background-color: rgb(255,255,255); text-decoration-style: initial; text-decoration-color: initial;\\\"><span style=\\\"border: 0px; vertical-align: baseline; box-sizing: border-box; color: rgb(255, 0, 0);\\\"><span>4. 初始化系统：</span></span></p><p style=\\\"border: 0; vertical-align: baseline; box-sizing: border-box; text-indent: 0; color: rgb(44,44,44); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-transform: none; white-space: normal; widows: 2; word-spacing: 0; -webkit-text-stroke-width: 0; background-color: rgb(255,255,255); text-decoration-style: initial; text-decoration-color: initial;\\\"><span style=\\\"border: 0px; vertical-align: baseline; box-sizing: border-box; color: rgb(51, 102, 255);\\\"><span>    /sbin/init --&gt; /etc/inittab --&gt; /etc/rc.d/rc.sysinit --&gt; /etc/rc.d/* --&gt; /etc/rc.local --&gt; login界面(username/passwd) --&gt; /etc/profile.d/file --&gt; /etc/profile</span></span><span><br/></span><span></span><span style=\\\"border: 0px; vertical-align: baseline; box-sizing: border-box; color: rgb(0, 255, 0);\\\"><span> |</span></span><span><br/></span><span style=\\\"border: 0px; vertical-align: baseline; box-sizing: border-box; color: rgb(0, 255, 0);\\\"><span>                                            |--&gt;/etc/sysctl.conf</span></span><span><br/></span><span style=\\\"border: 0px; vertical-align: baseline; box-sizing: border-box; color: rgb(0, 255, 0);\\\"><span>                                            |--&gt; /etc/fstab</span></span><span><br/></span><span></span><span><br/></span><span style=\\\"border: 0px; vertical-align: baseline; box-sizing: border-box; color: rgb(51, 102, 255);\\\"><span>    --&gt; /etc/bashrc --&gt; ~/.bashrc --&gt; ~/.bash_profile</span></span></p><p style=\\\"border: 0; vertical-align: baseline; box-sizing: border-box; text-indent: 0; color: rgb(44,44,44); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-transform: none; white-space: normal; widows: 2; word-spacing: 0; -webkit-text-stroke-width: 0; background-color: rgb(255,255,255); text-decoration-style: initial; text-decoration-color: initial;\\\"><span>    /sbin/init是所有进程的父进程，当它接管了系统的控制权之后:</span><span><br/></span><span>        (1) 它首先会去读取/etc/inittab文件来执行相应的脚本进行系统的初始化,如：设置键盘，字体，装载模块，配置网络。</span><span><br/></span><span>        (2) 获取网络环境和主机类型。首先会读取网络环境配置文件'/etc/sysconfig/network'，获取主机名和默认网关等网络环境</span><span><br/></span><span>        (3)    检测与载入内存设备/proc及USB设备/sys 除了/proc外，系统会主动检测是否有usb设备，并主动加载usb驱动，尝试挂载usb文件系统</span><span><br/></span><span>        (4)    决定是否启动selinux</span><span><br/></span><span>        (5)    接口设备的检测和即插即用参数的测试</span><span><br/></span><span>        (6)    用户自定义模块的加载。用户可以在'/etc/sysconfig/modules/*.modules' 加入自定义模块,此时会加载到系统中。</span><span><br/></span><span>        (7)    加载核心相关的设置。按'/etc/sysctl.conf'这个文件的设置值配置功能。</span><span><br/></span><span>        (8) 设置系统时间。</span><span><br/></span><span>        (9) 设置终端控制台的字体。</span><span><br/></span><span>        (10) 设置raid及LVM等硬盘功能</span><span><br/></span><span>        (11) 以只读方式检测磁盘文件系统</span><span><br/></span><span>        (12) 进行磁盘配额quota的转换</span><span><br/></span><span>        (13) 重新以只读方式加入系统磁盘</span><span><br/></span><span>        (14) 启动quota功能</span><span><br/></span><span>        (15) 启动系统随机数设备</span><span><br/></span><span>        (16) 清除启动过程中的临时文件。</span><span><br/></span><span>        (17) 将启动信息加载到'/var/log/dmesg'文件中</span><span><br/></span><span></span><span><br/></span><span>    当/etc/rc.sysinit执行完毕，系统就可以顺利工作了。只是要需要启动系统所需的各种服务，这样主机才可以提供相关的网络和主机功能。</span></p><p style=\\\"border: 0; vertical-align: baseline; box-sizing: border-box; text-indent: 0; color: rgb(44,44,44); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-transform: none; white-space: normal; widows: 2; word-spacing: 0; -webkit-text-stroke-width: 0; background-color: rgb(255,255,255); text-decoration-style: initial; text-decoration-color: initial;\\\"><span></span></p><p style=\\\"border: 0; vertical-align: baseline; box-sizing: border-box; text-indent: 0; color: rgb(44,44,44); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-transform: none; white-space: normal; widows: 2; word-spacing: 0; -webkit-text-stroke-width: 0; background-color: rgb(255,255,255); text-decoration-style: initial; text-decoration-color: initial;\\\"><span>最后放一张图诠释启动过程：</span></p><div style=\\\"border: 0px; vertical-align: baseline; box-sizing: border-box; text-indent: 0px; color: rgb(44, 44, 44); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;\\\"><img src=\\\":/f82359b8b31f4f63bb878a47a3cfcb34\\\" type=\\\"image/jpeg\\\" data-filename=\\\"640.jpg\\\" style=\\\"--en-marks: %7B%22align%22%3A%22center%22%7D; border: 0px; max-width: 100%; height: auto; vertical-align: baseline; box-sizing: border-box; cursor: zoom-in; transition: transform 0.3s cubic-bezier(0.2, 0, 0.2, 1) 0s; display: flex; border-radius: 2px; width: auto;\\\" width=\\\"auto\\\"/></div><p style=\\\"border: 0; vertical-align: baseline; box-sizing: border-box; text-indent: 0; color: rgb(44,44,44); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-transform: none; white-space: normal; widows: 2; word-spacing: 0; -webkit-text-stroke-width: 0; background-color: rgb(255,255,255); text-decoration-style: initial; text-decoration-color: initial;\\\"><span></span></p><p style=\\\"-webkit-tap-highlight-color: transparent; outline: 0; max-width: 100%; clear: both; min-height: 1em; color: rgba(0,0,0,0.9); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: 0.544px; orphans: 2; text-align: center; text-indent: 0; text-transform: none; widows: 2; word-spacing: 0; -webkit-text-stroke-width: 0; white-space: normal; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; background-color: rgb(255,255,255); box-sizing: border-box; overflow-wrap: break-word;\\\"><span><img src=\\\":/013783b3ec1c43d894b4fed929d5ba38\\\" type=\\\"image/png\\\" data-filename=\\\"640.png\\\" alt=\\\"图片\\\" style=\\\"--en-marks: %7B%22align%22%3A%22center%22%7D; -webkit-tap-highlight-color: transparent; outline: 0px; max-width: 100%; box-sizing: border-box; overflow-wrap: break-word; vertical-align: bottom; height: auto; __visibility: visible; width: auto;\\\" width=\\\"auto\\\"/></span></p><div><div><div style=\\\"--en-codeblock: true; --en-meta: {&quot;title&quot;:&quot;&quot;,&quot;lang&quot;:&quot;Plain Text&quot;,&quot;theme&quot;:&quot;default&quot;,&quot;showLine&quot;:true,&quot;lineWrap&quot;:true}; box-sizing: border-box; color: rgb(51, 51, 51); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.14902); background-position: initial initial; background-repeat: initial initial;\\\"><pre><span>版权声明：本文内容来自博客园：hukey，遵循CC 4.0 BY-SA版权协议上原文接及本声明。</span><br/><span>本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行可。</span><br/><span>原文链接：https://www.cnblogs.com/hukey/p/5220536.html</span><br/><span>如有涉及到侵权，请联系，将立即予以删除处理。</span><br/><span>在此特别鸣谢原作者的创作。</span><br/><span>此篇文章的所有版权归原作者所有，与本公众号无关，商业转载建议请联系原作者，非商业转载请注明出处。</span><br/></pre></div></div></div><div><span><br/></span></div></div></div><div><div><div><div style=\\\"display: flex;\\\"><a>阅读原文</a></div></div></div></div></div></div></div></span>\\\n</div></body></html>\\u0000\"]],\"start1\":0,\"start2\":0,\"length1\":0,\"length2\":17016}]"
metadata_diff: {"new":{"id":"03095c206c9c49ba8b1f83d351453f20","parent_id":"f67288c3a0be4374b5d532b5dcc3aabc","latitude":"0.00000000","longitude":"0.00000000","altitude":"0.0000","author":"","source_url":"","is_todo":0,"todo_due":0,"todo_completed":0,"source":"joplin-desktop","source_application":"net.cozic.joplin-desktop","application_data":"","order":1764348829544,"markup_language":2,"is_shared":0,"share_id":"","conflict_original_id":"","master_key_id":"","user_data":"","deleted_time":0},"deleted":[]}
encryption_cipher_text: 
encryption_applied: 0
updated_time: 2025-11-28T21:04:04.423Z
created_time: 2025-11-28T21:04:04.423Z
type_: 13