极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 11127|回复: 0

【转】openwrt的启动过程

[复制链接]
发表于 2016-6-1 23:55:12 | 显示全部楼层 |阅读模式
转载自:http://bbs.intorobot.com/thread-923-1-1.html

启动三宝

Bootloader

1.上电第一条指定一定是Flash的某个固定地址;

2.Bootloader执行powerup start,非常底层的初始化,堆栈,cpu,内存等;

3.解压kernel到内存,转到内核启动。

Kernel

1.执行arch特有的初始化,bootstaps;

2.执行通用start_kernel;

3.扫描mtd分区,以根据cmdline的要求挂载rootfs(squashfs),cmdline可以通过bootloader传递也可以内核自己设置写死,cat /proc/cmdline来查看自己的启动参数;

4.执行/etc/preinit,预初始化挂载rootfs、/proc、/sys等;

Preinit目的是为后续启动脚本做初步的check和setup,主要工作就是挂载/proc、/sys等虚拟fs以便取系统信息和控制,准备/dev目录的设备节点,启动/sbin/init守护进程等;

5.执行/sbin/init(如果没有initramfs的话),进程之母;

6.finally some kernel thread becomes the userspace init process。

Init

用户态在rootfs挂载后就开始运行了,第一个就是/sbin/init。

1. /sbin/init去/etc/inittab找到sysinit的定义继续

2. ::sysinit:[url=]/etc/init.d/rcS[/url] S boot执行(默认的命令);

3. rcS执行/etc/rc.d/的S##xxxx脚本,以start为参数,实际上都链接到了/etc/init.d/xxxx;

4. rcS遍历后,openwrt系统启动完毕。

Init.d脚本
init.d下的脚本实际上是各个功能的统一,下面是对照表:S##xxxx表示启动时执行的脚本,传start参数,K##xxxx表示reboot时执行传参stop。通过opkg安装的程序可能会自动在init.d添加脚本


S05defconfig

create config files with default values for platform (if config file is not exist), really does this on first start after OpenWRT installed (copy unexisted files from /etc/defconfig/$board/ to /etc/config/)


S10boot

starts hotplug-script, mounts filesystesm, starts .., starts syslogd, …


S39usb

mount -t usbfs none /proc/bus/usb


S40network

start a network subsystem (run /sbin/netifd, up interfaces and wifi


S45firewall

create and implement firewall rules from /etc/config/firewall


S50cron

starts crond, see → /etc/crontabs/root for configuration


S50dropbear

starts dropbear, see → /etc/config/dropbear for configuration


S50telnet

checks for root password, if non is set, /usr/sbin/telnetd gets started


S60dnsmasq

starts dnsmasq, see → /etc/config/dhcp for configuration


S95done

executes /etc/rc.local


S96led

load a LED configuration from /etc/config/system and set up LEDs (write values to /sys/class/leds/*/*)


S97watchdog

start the watchdog daemon (/sbin/watchdog)


S99sysctl

interprets /etc/sysctl.conf


K50dropbear

kill all instances of dropbear


K90network

down all interfaces and stop netifd


K98boot

stop logger daemons: /sbin/syslogd and /sbin/klogd


K99umount

writes caches to disk, unmounts all filesystems


Busybox初始化
  Init被视为守护进程,它启动镜像,修改runnning level,创建设备访问daemon,连同其他的琐碎管理。

  Init一被启动,就从/etc/inittab读出配置,以启动管理其他功能,执行相应的程序。Busybox的init是一个小型化的配置,去掉了run level、id

格式为

[ID] : [Runlevel(s)] : [Action] : [Process to execute ]

the ID field is used for controlling TTY/Console。

Openwrt的inittab:

root@OpenWrt:/sbin# cat /etc/inittab

::sysinit:/etc/init.d/rcS S boot

::shutdown:/etc/init.d/rcS K shutdown

::askconsole:/bin/ash --login

/etc/rc.d/rcS脚本
rcS的核心就是遍历执行init.d下面的所有可执行脚本,

rcS的脚本非常简单,$1, $2 是参数,$1 为S或者K,表示启动和复位的两类脚本,

for i in /etc/rc.d/$1* ; do

[ -x $i ] && $i $2

done

与windows程序不同,linux通过permission来管理文件的执行权限,

比如对于

我们的inittab中的配置::sysinit:/etc/init.d/rcS S boot,rcS解释如下:

1.Execute the following line once for every entry (file/link) in the /etc/rc.d directory that begins with "S"

2.If the file is executable, execute the file with the option "boot"

3.Repeat at step 1, replacing $i with the next filename until there are no more files to check
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则 需要先绑定手机号

Archiver|联系我们|极客工坊

GMT+8, 2024-3-29 15:29 , Processed in 0.041782 second(s), 17 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表