busybox制作ramdisk

来源:互联网 发布:php mysql select db 编辑:程序博客网 时间:2024/05/22 09:41

一、 BusyBox 

BusyBox 是标准 Linux 工具的一个单个可执行实现。BusyBox 包含了一些简单的工具,例如 cat 和 echo,还包含了一些更大、更复杂的工具,例如 grep、find、mount 以及 telnet。有些人将 BusyBox 称为 Linux 工具里的瑞士军刀.简单的说BusyBox就好像是个大工具箱,它集成压缩了 Linux 的许多工具和命令。 
嵌入式根目录下的bin、sbin和usr目录以及linuxrc通常就是BusyBox。在构建嵌入式linux的根文件系统的时候,会根据目标平台配置BusyBox源码,编译生成这三个目录和一个文件。根目录下的目录和文件需要自行建立或者从BusyBox的example中拷贝修改。 二、 嵌入式中的BusyBox的启动流程 (/bin/buysbox)
脚本执行顺序如下: 

/sbin/init -> /etc/inittab -> /etc/init.d/rcS 


1、在kernel/init/main.c的init函数中有代码      if (execute_command) 
      execve(execute_command,argv_init,envp_init); 
      execve("/sbin/init",argv_init,envp_init); 
bootloader会给内核的main函数 init=/linuxrc 这个参数,于是就有了 
execute_command = "/linuxrc",busybox中_install目录下的 linuxrc 是busybox的一个软链接,指向/bin/busybox,而/sbin/init也是/bin/busybox的符号链接,因此这个linuxrc基本没什么用处,我们不要这个linuxrc脚本可以直接通过/sbin/init执行busybox的初始化,或者重写linuxrc,添加自己的一些初始化的东西。 2、BusyBox初始化 

BusyBox是目标板系统上执行的第一个应用程序。当BusyBox知道调用它的目的是要执行init,它会立即跳转到init进程。 

BusyBox的 init进程依次进行以下工作: 

1)为init设置信号处理进程。 

2)初始化控制台。

3)解析inittab文件(/etc/inittab)。

4)执行系统初始化命令。 

5)执行所有阻塞的(会导致init暂停的)inittab命令(动作类型:wait)。 

6)执行所有仅执行一次的inittab命令(动作类型:once)。 7)一旦完成以上工作,init进程便会循环执行以下工作: 

8)执行所有终止时必须重新启动的inittab命令(动作类型:respawn)。 

9)执行所有中止时必须重新启动但启动前必须前询问用户的inittab命令(动作类 型:askfirst)。 


其中,inittab文件的每一行的格式为:id:runlevel:action:process。process字段用来指定所执行程序的路径, 包括命令行选项。action字段用来指定可应用到process的动作。BusyBox init能够识别的inittab动作类型如下表所示: 
动作 结果 

sysinit------为init提供初始化命令行的路径 

respawn-----每当相应的进程终止执行便重新启动 

askfirst-------类似respawn,不过它的主要用途是减少系统上执行 的终端应用程序的数量。它将会促使init在控制台上显示“Please press Enter to activate this console.”的信息,并在重新启动进程之前等待用户按下Enter键 
wait---------告诉init必须等到相应的进程完成才能继续执行 

once-------仅执行相应的进程一次,而且不会等待它完成

ctrlaltdel------当按下Ctrl-Alt-Delete组合键时,执行相 应的进程 

shutdown-----当系统关机时,执行相应的进程 

restart -----当init重新启动时,执行相应的进程。通常此处所执行 的进程就是init本身 
下面就是一个典型的嵌入式busybox的inittab文件的例子: 

 

可见busybox分析完inittab脚本中会执行rcS脚本,下面就是一个rcS脚本的例子: 

echo "export system param...." export PATH=/sbin:/bin: 

export HOSTNAME=micro2440  
echo "init ip address....." 
/sbin/ifconfig eth0 192.168.254.177  
echo "Mount all filesystems (of the given types) mentioned in fstab." mount –a 
其中mount –a会加载文件/etc/fstab文件中的选项,这个目录的编写主要是安排哪些目录挂载哪些次级文件系统:例如: 

proc           /proc        proc   defaults        0     0 

tmpfs         /tmp        tmpfs  defaults        0     0 

sysfs          /sys         sysfs  defaults        0     0 

上面的rcS文件中不但加载了在etc/fstab中定义的文件系统(mount -a),还对系统的环境变量、ifconfig以及打印欢迎信息进行设置,这样比较省事儿,还有一种做法是在rcS中运行/usr/etc/rc.local脚本,该脚本的主要工作是安装核心模块、进行网络配置、运行应用程序、启动图形界面等,这里的rc.local脚本只进行ifconfig,打印欢迎信息,并执行/usr/etc/profile,profile文件时对应用程序的环境变量进行设置: 
/sbin/init -> /etc/inittab -> /etc/init.d/rcS->/usr/etc/rc.local->/usr/etc/profile  




-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

ramdisk-initrd的需要添加的目录

dev目录下需要添加一些设备节点

mknod tty -c 5 0

mknod console -c  51

mknod null c 1 3

mknod tty0 c 4 0

mknod tty1 c 4 1

mknod tty2 c 4 2

mknod tty3 c 4 3

mknod tty4 c 4 4 

mknod tty5 c 4 5

mknod ttyS0 4 64

mknod ttyS1 4 65

#以上可以参考ubuntu的dev节点

mkdir /proc 

mkdir /sys

mkdir /tmp

为了fstab的mount


文件/etc/inittab

可以参考busybox的example的写法

# /etc/inittab init(8) configuration for BusyBox
#
# Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
#
#
# Note, BusyBox init doesn't support runlevels.  The runlevels field is
# completely ignored by BusyBox init. If you want runlevels, use sysvinit.
#
#
# Format for each entry: <id>:<runlevels>:<action>:<process>
#
# <id>: WARNING: This field has a non-traditional meaning for BusyBox init!
#
#       The id field is used by BusyBox init to specify the controlling tty for
#       the specified process to run on.  The contents of this field are
#       appended to "/dev/" and used as-is.  There is no need for this field to
#       be unique, although if it isn't you may have strange results.  If this
#       field is left blank, then the init's stdin/out will be used.
#
# <runlevels>: The runlevels field is completely ignored.
#
# <action>: Valid actions include: sysinit, respawn, askfirst, wait, once,
#                                  restart, ctrlaltdel, and shutdown.
#
#       Note: askfirst acts just like respawn, but before running the specified
#       process it displays the line "Please press Enter to activate this
#       console." and then waits for the user to press enter before starting
#       the specified process.
#
#       Note: unrecognized actions (like initdefault) will cause init to emit
#       an error message, and then go along with its business.
#
# <process>: Specifies the process to be executed and it's command line.
#
# Note: BusyBox init works just fine without an inittab. If no inittab is
# found, it has the following default behavior:
#         ::sysinit:/etc/init.d/rcS
#         ::askfirst:/bin/sh
#         ::ctrlaltdel:/sbin/reboot
#         ::shutdown:/sbin/swapoff -a
#         ::shutdown:/bin/umount -a -r
#         ::restart:/sbin/init
#         tty2::askfirst:/bin/sh
#         tty3::askfirst:/bin/sh
#         tty4::askfirst:/bin/sh
#
# Boot-time system configuration/initialization script.
# This is run first except when booting in single-user mode.
#
::sysinit:/etc/init.d/rcS


# /bin/sh invocations on selected ttys
#
# Note below that we prefix the shell commands with a "-" to indicate to the
# shell that it is supposed to be a login shell.  Normally this is handled by
# login, but since we are bypassing login in this case, BusyBox lets you do
# this yourself...
#
# Start an "askfirst" shell on the console (whatever that may be)
::askfirst:-/bin/sh
# Start an "askfirst" shell on /dev/tty2-4
tty2::askfirst:-/bin/sh
tty3::askfirst:-/bin/sh
tty4::askfirst:-/bin/sh


# /sbin/getty invocations for selected ttys
tty4::respawn:/sbin/getty 38400 tty5
tty5::respawn:/sbin/getty 38400 tty6


# Example of how to put a getty on a serial line (for a terminal)
#::respawn:/sbin/getty -L ttyS0 9600 vt100
#::respawn:/sbin/getty -L ttyS1 9600 vt100
#
# Example how to put a getty on a modem line.
#::respawn:/sbin/getty 57600 ttyS2


# Stuff to do when restarting the init process
::restart:/sbin/init


# Stuff to do before rebooting
::ctrlaltdel:/sbin/reboot
::shutdown:/bin/umount -a -r
::shutdown:/sbin/swapoff -a


文件/etc/init.d/rcS

#! /bin/sh

echo "export system param..."
export PATH=/sbin:/bin:
export HOSTNAME=vtc1010-kylin
echo "Mount all filesystems mentioned in fstab"
mount -o rw,remount /
/bin/mount -a


文件/etc/fstab

proc            /proc   proc    defaults    0   0
tmpfs           /tmp    tmpfs   defaults    0   0
none            /tmp    ramfs   defaults    0   0
sysfs           /sys    sysfs   defaults    0   0


目录结构:

├── bin
│   └── busybox

├── dev

├── etc
│   ├── fstab
│   ├── init.d
│   │   └── rcS
│   └── inittab
├── proc
├── sbin
├── sys
├── tmp
└── usr
    ├── bin
    └── sbin



 




 
0 0