首页 > 代码库 > Linux启动简单分析

Linux启动简单分析

1. 概述

从Bootloader至init程序主要过程如下

++++++++++++++    ++++++++++++++++++    ++++++++++++++++++++++++++++++++++++++++++++++++++|            | -> | kernel_entry() | -> | start_kernel() -> rest_init() -> kernel_init() |============== -> ================== -> ==================================================| BootLoader | -> |    MIPS        | -> |                    kernel                      |++++++++++++++    ++++++++++++++++++    ++++++++++++++++++++++++++++++++++++++++++++++++++ 
<style type="text/css">.csharpcode, .csharpcode pre{ font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/}.csharpcode pre { margin: 0em; }.csharpcode .rem { color: #008000; }.csharpcode .kwrd { color: #0000ff; }.csharpcode .str { color: #006080; }.csharpcode .op { color: #0000c0; }.csharpcode .preproc { color: #cc6633; }.csharpcode .asp { background-color: #ffff00; }.csharpcode .html { color: #800000; }.csharpcode .attr { color: #ff0000; }.csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em;}.csharpcode .lnum { color: #606060; }</style>

kernel_init()首先尝试执行从bootloader传递的命令参数rdinit=、init=
如果失败,会依次尝试执行/sbin/init、/etc/init、/bin/init、/bin/sh、

以下是从UBoot传递的命令行实例
如console=ttyS0,115200 root=/dev/ram rw init=/linuxrc

2. busybox

在嵌入式系统中,init程序通常就是是busybox
而linuxrc则为busybox的符号链接

busybox的init程序,即init_main函数

init_main() -> parse_inittab() -> run_actions(SYSINIT)

3. inittab

文件/etc/inittab是Linux启动非常重要的文件

参考</etc/inittab文件分析> 或 <linux /etc/inittab文件>

这里关注以下action为sysinit的行
该行指定的脚本将完成系统的初始化过程

在ttylinux中,该行为

::sysinit:/etc/rc.d/rc.sysinit
<style type="text/css">.csharpcode, .csharpcode pre{ font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/}.csharpcode pre { margin: 0em; }.csharpcode .rem { color: #008000; }.csharpcode .kwrd { color: #0000ff; }.csharpcode .str { color: #006080; }.csharpcode .op { color: #0000c0; }.csharpcode .preproc { color: #cc6633; }.csharpcode .asp { background-color: #ffff00; }.csharpcode .html { color: #800000; }.csharpcode .attr { color: #ff0000; }.csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em;}.csharpcode .lnum { color: #606060; }</style>

rc.sysinit完成的工作主要有
设置环境变量,加载文件系统。。。

其中值得关注的是启动服务

~~~~~~~~~~~~~ Part of /etc/rc.d/rc.sysinit ~~~~~~~~~~~~~# *****************************************************************************# Start the services.# *****************************************************************************for p in /etc/rc.d/rc.startup/*; do    [[ -x ${p} ]] && ${p} startdone; unset p~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<style type="text/css">.csharpcode, .csharpcode pre{ font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/}.csharpcode pre { margin: 0em; }.csharpcode .rem { color: #008000; }.csharpcode .kwrd { color: #0000ff; }.csharpcode .str { color: #006080; }.csharpcode .op { color: #0000c0; }.csharpcode .preproc { color: #cc6633; }.csharpcode .asp { background-color: #ffff00; }.csharpcode .html { color: #800000; }.csharpcode .attr { color: #ff0000; }.csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em;}.csharpcode .lnum { color: #606060; }</style>

而rc.startup目录下又包含如下脚本

$ ls -al /etc/rc.d/rc.startuptotal 8drwxr-xr-x    2 root     root          4096 Jul 29 07:33 .drwxr-xr-x    5 root     root          4096 Jul 29 07:33 ..lrwxrwxrwx    1 root     root            16 Jul 29 07:33 00.random -> ../init.d/randomlrwxrwxrwx    1 root     root            18 Jul 29 07:33 01.firewall -> ../init.d/firewalllrwxrwxrwx    1 root     root            16 Jul 29 07:33 05.syslog -> ../init.d/sysloglrwxrwxrwx    1 root     root            17 Jul 29 07:33 10.network -> ../init.d/networklrwxrwxrwx    1 root     root            14 Jul 29 07:33 20.sshd -> ../init.d/sshdlrwxrwxrwx    1 root     root            15 Jul 29 07:33 30.inetd -> ../init.d/inetdlrwxrwxrwx    1 root     root            13 Jul 29 07:33 85.gpm -> ../init.d/gpmlrwxrwxrwx    1 root     root            15 Jul 29 07:33 90.crond -> ../init.d/crond
<style type="text/css">.csharpcode, .csharpcode pre{ font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/}.csharpcode pre { margin: 0em; }.csharpcode .rem { color: #008000; }.csharpcode .kwrd { color: #0000ff; }.csharpcode .str { color: #006080; }.csharpcode .op { color: #0000c0; }.csharpcode .preproc { color: #cc6633; }.csharpcode .asp { background-color: #ffff00; }.csharpcode .html { color: #800000; }.csharpcode .attr { color: #ff0000; }.csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em;}.csharpcode .lnum { color: #606060; }</style>

可以看出ttylinux依次启动了脚本中指定的服务

Linux启动简单分析