移植 bash

来源:互联网 发布:网络配置 编辑:程序博客网 时间:2024/06/05 09:32
移植 bash
一般情况下,在ARM开发板中都会有sh,这是随busybox携带的脚本工具。如果想将shell脚本在ARM目标机中运行,则需要把脚本第一行的#!/bin/bash改为#!/bin/sh。但这样虽然可以执行脚本,但是脚本中的许多语法是不支持的,例如:
1、判断: if [ -d /usr ] ; if [ -b /dev/sda1 ]
2、数值运算:sum=$[$val1+$val2]
3、逻辑运算:and=$[ $val1 & $val2 ]
解决方法就是将bash移植到目标板。

0 移植环境:                                                                                
主机:Intel(R) Xeon(R) CPU           X5650  @ 2.67GHz
Linux version 3.2.0-32-generic (buildd@batsu) (gcc version 4.6.3 (Ubuntu/Linaro 4.6.3-1ubuntu5)
目标平台:Amlogic Meson8(S802)
android 4.4 (Linux version 3.10.33)

1 下载bash源码

地址:http://www.gnu.org/software/bash/bash.html


2 解压

将下载的bash压缩包解压,命令:

$ mkdir /home/hui.zhang/w/tmp/shell/                 # 创建bash工作目录$ cp bash-4.3.tar.gz /home/hui.zhang/w/tmp/shell/    # 复制安装包$ cd /home/hui.zhang/w/tmp/shell/                    # 进入/home/veryarm/bash目录$ tar zxvf bash-4.3.tar.gz                           # 解压

3、配置

编译之前,要进行配置,命令:

$ mkdir install                                 # 创建安装目录$ cd bash-4.3# 进入目录$ export PATH=$PATH:/opt/gcc-linaro-arm-linux-gnueabihf/bin# 添加交叉编译器路径到PATH环境变量中,最好用与编译内核用的交叉编译器版本一致$ ./configure CC=arm-linux-gnueabihf-gcc --prefix=/home/hui.zhang/w/tmp/shell/install  --host=arm-linux --target=arm-linux-gnueabihf --enable-static-link --enable-history --without-bash-malloc  --cache-file=arm-linux.cache

CC:指定交叉编译器工具
--prefix:指定安装目录
--host:运行在的主机环境
--enable-static-link:静态链接
更多配置项,见$./configure --help

4 编译

编译并安装:

$ make & make install

5 复制到目标板

装成功后在安装路径/home/hui.zhang/w/tmp/shell/istall下生成两个目录 bin 和 share,将bin中的 bash 可执行文件复制至开发板 /bin 中,并修改执行权限:

root@k200:/ #cp bash /bin
root@k200:/ #chmod +x /bin/bash

 

6 测试

在开发板中运行bash

root@k200:/ #bash 
bash-4.3#

在开发版中运行相关脚本(hello world):

root@k200:/ # cat /data/test.sh                                                
#!/system/bin/bashecho hello world!root@k200:/ # chmod 755 /data/test.sh root@k200:/ # /data/test.sh                                                    hello world!root@k200:/ # 

恭喜,移植成功!

7 问题记录:

(1)配置报错

$ ./configure CC=arm-linux-gnueabihf-gcc --prefix=/home/hui.zhang/w/tmp/shell/install  --host=arm-linux --target=arm-linux-gnueabihf --enable-static-link --enable-history --without-bash-malloc

checking whether getpgrp requires zero arguments... yes
checking whether setvbuf arguments are reversed... configure: error: cannot run test program while cross compiling
解决:交叉编译的时候测试程序是不可以在HOST上运行就会出现: error: cannot run test program while cross compiling 类似的错误,可以使用CACHEFILE解决这个问题,
查看错误位置:configure: 
9983 echo "$as_me:$LINENO: checking whether setvbuf arguments are reversed" >&5
 9984 echo $ECHO_N "checking whether setvbuf arguments are reversed... $ECHO_C" >&6
 9985 if test "${ac_cv_func_setvbuf_reversed+set}" = set; then
 9986   echo $ECHO_N "(cached) $ECHO_C" >&6
 9987 else
 9988   if test "$cross_compiling" = yes; then
 9989   { { echo "$as_me:$LINENO: error: cannot run test program while cross compiling" >&5
 9990 echo "$as_me: error: cannot run test program while cross compiling" >&2;}
 9991    { (exit 1); exit 1; }; }
 9992 else
 9993   cat >conftest.$ac_ext <<_ACEOF

checking whether getpgrp requires zero arguments... yes
checking whether setvbuf arguments are reversed... configure: error: cannot run test program while cross compiling
解决:echo ac_cv_func_setvbuf_reversed=yes>arm-linux.cache
$ ./configure CC=arm-linux-gnueabihf-gcc --prefix=/home/hui.zhang/w/tmp/shell/install  --host=arm-linux --target=arm-linux-gnueabihf --enable-static-link --enable-history --without-bash-malloc  --cache-file=arm-linux.cache

(2)段错误。前面问题搞定后,最后测试时报错:

root@k200:/ #bash segment fault 

由于bash3.1之前的版本源文件包2M多,后续的版本都将近8M,猜想3.1会精简一些,第一次移植的是bash3.1,段错误应该是这个版本源码本身问题。更换为bash-4.3.tar.gz,果然运行正常。


参考文档:
http://www.veryarm.com/563.html
http://blog.csdn.net/zh98jm/article/details/6104615
0 0