OpenWrt啟動過程分析

來源:互聯網
上載者:User

總結一下OpenWrt的啟動流程:1.CFE->2.linux->3./etc/preinit->4./sbin/init ->5./etc/inittab ->6./etc/init.d/rcS->7./etc/rc.d/S* ->8.

 OpenWrt是一個開放的linux平台,主要用於帶wifi的無線路由上。

類似於Ubuntu、Red Hat、之類的linux發行版本,它也有一套自己的啟動流程。下面,我就以我的Linksys 的WRT54G為平台介紹一下,OpenWrt的啟動流程。

首先,CFE(common firmware environment)--它就是一個bootloader,類似u-boot,redboot之類,有點broadcom公司禦用之嫌--

最先啟動。它的任務只是創造一個簡單的環境,讓系統先運行起來。除了能夠跳轉到特定地址上啟動作業系統(如Linux)外,

它還能讓你download東西到上面,比如download一個linux,然後啟動它。

另外,值得一提的是,CFE在啟動之後會有1,2秒的時間等待由tftp上傳的核心並燒寫到flash上,

這就給一些作業系統損壞但CFE還能工作的"磚頭"板一個起死回生的機會。請注意一旦linux啟動之後,將由linux全部接管系統,

這時候就沒CFE什麼事情了。唯一的瓜葛是CFE傳遞給核心一個命令列的參數,這個可以在linux啟動起來之後用下面的命令查看:

 

root@OpenWrt:/# cat /proc/cmdline

console=ttyS0,115200 mtdparts=spi_flash:1m(u-boot)ro,3m(kernel),-(rootfs)

 

之後,linux系統啟動起來了。它使用類似如下的指令碼命令來解析cmdline:

for x in $(cat /proc/cmdline); do

case $x in

init=*)

init=${x#init=}

;;

root=*)

ROOT=${x#root=}

case $ROOT in

LABEL=*)

ROOT="/dev/disk/by-label/${ROOT#LABEL=}"

;;

UUID=*)

ROOT="/dev/disk/by-uuid/${ROOT#UUID=}"

;;

esac

;;

rootflags=*)

ROOTFLAGS="-o ${x#rootflags=}"

;;

cryptopts=*)

cryptopts="${x#cryptopts=}"

;;

nfsroot=*)

NFSROOT="${x#nfsroot=}"

;;

nfsopts=*)

NFSOPTS="-o ${x#nfsopts=}"

;;

boot=*)

BOOT=${x#boot=}

;;

resume=*)

RESUME=${x#resume=}

;;

noresume)

NORESUME=y

;;

quiet)

quiet=y

;;

ro)

readonly=y

;;

rw)

readonly=n

;;

debug)

debug=y

exec >/tmp/initramfs.debug 2>&1

set -x

;;

break=*)

break=${x#break=}

;;

break)

break=premount

;;

esac

done

對於OpenWrt這個cmdline的意思就是:root=/dev/mtdblock2 檔案系統在第三個flash分區上(mtdblock0,1,2);

rootfstype=squashfs,jffs2 檔案系統類型是squashfs和jffs2(為什麼兩種類型,目前還不清楚,

不過可以確定OpenWrt使用了較為複雜的檔案系統,實現了squashfs的壓縮和jffs2的可寫 ); 

init=/etc/preinit 執行該初始化,noinitrd console=ttyS0,115200

沒有initrd和console口設定。

 

init=/etc/preinit 是linux會執行的初始化,具體內容如下:

root@OpenWrt:/# cat /etc/preinit

#!/bin/sh

# Copyright (C) 2006 OpenWrt.org

export PATH=/bin:/sbin:/usr/bin:/usr/sbin

. /etc/diag.sh

 

failsafe_ip() {

ifconfig $ifname 192.168.1.1 netmask 255.255.255.0 broadcast 192.168.1.2 55 up

}

 

failsafe() {

[ -n "$ifname" ] && grep "$ifname" /proc/net/dev >/dev/null && {

failsafe_ip

netmsg 192.168.1.255 "Entering Failsafe!"

telnetd -l /bin/login <> /dev/null 2>&1

}

lock /tmp/.failsafe

ash --login

}

 

mount proc /proc -t proc

mount sysfs /sys -t sysfs

 

size=$(awk '/MemTotal:/ {l=5242880;mt=($2*1024);print((s=mt/2)<l)&&(mt>l)?mt-l:s }' /proc/meminfo)

mount tmpfs /tmp -t tmpfs -o size=$size,nosuid,nodev,mode=1777

 

if grep devfs /proc/filesystems > /dev/null; then

mount devfs /dev -t devfs

M0=/dev/pty/m0

M1=/dev/pty/m1

HOTPLUG=/sbin/hotplug-call

 

elif [ -x /sbin/hotplug2 ]; then

mount -t tmpfs tmpfs /dev -o size=512K

mknod /dev/console c 5 1

/sbin/hotplug2 --coldplug --set-rules-file /etc/hotplug2-init.rules

/sbin/hotplug2 --no-coldplug --persistent --set-rules-file /etc/hotplug2 -init.rules &

M0=/dev/ptmx

M1=/dev/ptmx

HOTPLUG=

 

elif [ -x /sbin/udevd ]; then

mount -n -t tmpfs -o mode=0755 udev /dev

/sbin/udevd --daemon

/sbin/udevtrigger

/sbin/udevsettle

M0=/dev/pty/ptmx

M1=/dev/pty/ptmx

HOTPLUG=

fi

 

mkdir -p /dev/pts /dev/shm

mount devpts /dev/pts -t devpts

 

# the shell really doesn't like having stdin/out closed

# that's why we use /dev/pty/m0 and m1 as replacement

# for /dev/console if there's no serial console available

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.