shell实现分为两部分:
一、shell解释器和内置命令
源码位于system/core/sh目录下,主要完成shell命令的解释查找,对于builtins.c中包含的内置命令,直接执行,对于 toolbox的扩展命令,间接调用toolbox程序完成。
|
二、toolbox扩展命令
主要完成扩展命令的执行,每一个扩展命令对应一个name_main函数,如ls命令,对应ls_main函数。同时,每一个扩展命令都由一个 system/core/toolbox/目录下面的.c文件实现。toolbox.c会根据这个目录下面的.c文件生成tools.h头文件,并在 system/core/toolbox/Android.mk 文件中为每个命令生成指向toolbox的连接。toolbox的实现结构使它扩展一个命令很容易。
假设现在我们自己想手工添加一个shell命令mycommand,只要在system/core/toolbox/目录下面新建一个 mycommand.c文件,并在里面实现一个mycommand_main函数,然后在system/core/toolbox/Android.mk 中添加mycommand.c即可。Android.mk会自动把它编译进toolbox程序,并在编译生成的Android系统/system/bin 目录下为这个命令生成一个指向toolbox的连接。
替换toolbox为busybox
接下来翻译一下网上的一篇文章,借助它,可以把Android自带的toolbox替换成busybox。
Installing Busybox command line tools
在Android系统中安装busybox命令行工具
本文简单地介绍了怎么把busybox安装到Android的文件系统中去。如果你想直接安装,可以从下面的地址下载我已经预编译好并在Android2.1系统上试验成功的busybox,然后直接跳过下面的安装步骤。
下载在Linux公社的1号 FTP服务器里,下载地址:
FTP地址:ftp://www.
用户名:www.
密码:www.muu.cc
在 2011年LinuxIDC.com\3月\Android自带的toolbox分析及扩展
下载方法见 http://www./thread-1187-1-1.html
一、编译busybox
1、下载busybox的最新版本,本文写作时最新版本是1.13.3。
下载地址:http://www./
2、解压源码:
tar jxf busybox-1.13.3.tar.bz2
3、运行menuconfig对busybox进行配置
cd busybox-1.13.3/
make menuconfig
4、在menuconfig中设置以下选项
Busybox Settings --> Build Options --> Build Busybox as a static binary (no shared libs) - Enable this option by pressing "Y"
Busybox Settings --> Build Options --> Cross compiler prefix - Set this option equal to "arm-none-linux-gnueabi-"
Busybox Settings --> Installation Options --> Don't use /usr - Enable this option by pressing "Y"
5、把交叉编译器的地址导入到环境变量:
export PATH=/opt/arm/arm-2007q3/bin:$PATH
6、编译busybox
make
二、安装busybox
把busybox安装到Android系统中去,做这几步:
1、在Android系统根目录下创建一个/bin目录。
mkdir /<path-to-Android-fs>/bin
2、把编译出来的busybox复制到/bin目录下
cp busybox /<path-to-Android-fs>/bin
|
3、把busybox安装到Android机器中
cd /bin
./busybox --install
三、把busybox作为默认shell
需要像下面这样编辑一下init.rc
1、编辑console服务,让它默认运行busybox
service console /system/bin/sh -> service console /bin/sh
2、把busybox路径加入到环境变量中
export PATH /sbin:/system/sbin:/system/bin:/system/xbin --> export PATH /bin:/sbin:/system/sbin:/system/bin:/system/xbin
注:
我使用busybox时,只是想简单地增加一些命令,把toolbox一些功能不是很全的命令替换掉,所以操作上没有上面说的那么复杂。下面是我的替代方案,可以试一下:
1、把busybox复制到/system/bin目录下。
adb push busybox /system/bin
2、把要添加的命令通过ln建立到busybox的连接。
比如,Android自带的toolbox是没有test这个命令的。我们要添加test命令就可以:
cd /system/bin
ln -s busybox test
这样,用户通过机器上的shell执行test命令时,就会调用busybox中实现test功能的applet。
对于一些原有的命令,如ls、chown等,如果不想用toolbox,也可以把它们的连接目标指向toolbox,拿chown来举例。
cd /system/bin
rm chown
ln -s busybox chown
这样做,最大的好处就是保证对系统的改动最少,又可以最大限度的扩展shell功能。
===========================================================================
英文文档
To build busybox
Download the latest version of busybox from the following website. At the time of writing the latest version was v.1.13.3.
http://www.Extract the busybox source:
|
tar jxf busybox-1.13.3.tar.bz2Configure busybox by running menuconfig
cd busybox-1.13.3/
make menuconfigIn menuconfig set the following options
Busybox Settings --> Build Options --> Build Busybox as a static binary (no shared libs) - Enable this option by pressing "Y"
Busybox Settings --> Build Options --> Cross compiler prefix - Set this option equal to "arm-none-linux-gnueabi-"
Busybox Settings --> Installation Options --> Don't use /usr - Enable this option by pressing "Y"Export path to where the cross-compiler is located on the host, for example:
export PATH=/opt/arm/arm-2007q3/bin:$PATH
Build busybox
make
Installing Busybox
--------------------------------------------------------------------------------
To install busybox in the target file-system
Create a /bin directory in the target file-system. For example:
mkdir /<path-to-Android-fs>/binCopy the busybox binary to the /bin directory in the target file-system
cp busybox /<path-to-Android-fs>/bin Install the busybox command line tools on the target by executing the following commands:
cd /bin
./busybox --install
Make the Busybox shell the default shell
--------------------------------------------------------------------------------
To make the busybox shell the default shell, edit the file "init.rc" in the target file-system as follows:
Edit the console service so that it runs the busybox shell and not the default shell by replacing:
service console /system/bin/shWith:
service console /bin/shAdd the path of the busybox command line tools to the system path variable by replacing:
export PATH /sbin:/system/sbin:/system/bin:/system/xbinWith
export PATH /bin:/sbin:/system/sbin:/system/bin:/system/xbin