Chinaunix首页 | 论坛 | 博客
  • 博客访问: 245654
  • 博文数量: 81
  • 博客积分: 325
  • 博客等级: 一等列兵
  • 技术积分: 595
  • 用 户 组: 普通用户
  • 注册时间: 2012-03-17 21:00
文章分类
文章存档

2016年(2)

2013年(33)

2012年(47)

我的朋友

分类:

2012-11-29 18:09:08

1构造目标板的根目录及文件系统



1.1 建立一个目标板的空根目录



我们将在这里构建构建根文件系统,创建基础目录结构. 存放交叉编译后生成的目标应用程序

  (BUSYBOX ,TINYLOGIN),存放库文件等。

[arm@localhost rootfs]# mkdir my_rootfs

[arm@localhost rootfs]# pwd

/home/arm/dev_home/rootfs/my_rootfs

[arm@localhost rootfs]# cd my_rootfs

[arm@localhost my_rootfs]#



1.2 在 my_rootfs 中建立Linux 目录树



[arm@localhost my_rootfs]#mkdir bin dev etc home lib mnt proc sbin sys tmp root usr

[arm@localhost my_rootfs]#mkdir mnt/etc

[arm@localhost my_rootfs]#mkdir usr/bin usr/lib usr/sbin

[arm@localhost my_rootfs]#touch linuxrc

[arm@localhost my_rootfs]#tree

|-- bin

|-- dev

|-- etc

|-- home

|-- lib

|-- linuxrc /* 此文件为启动脚本,是一shell 脚本文件。本文后面有专门介绍 */

|-- mnt

|  `-- etc

|-- proc

|-- sbin

|-- sys

|-- tmp

|-- root

`-- usr

    |-- bin

    |-- lib

    `-- sbin



权限参照你的linux 工作站即可,基础目录介绍参见本文参考资料(未尾) 。

需要说明的一点就是etc  目录存放配置文件,这个目录通常是需要修改的,所以在linuxrc 脚本当中将etc  目录

挂载为ramfs 文件系统,然后将mnt/etc  目录中的所有配置文件拷贝到etc  目录当中,这在下一节的linuxrc 脚本

文件当中会有体现。



1.3 创建 linuxrc 文件



1. 创建linuxrc,加入如下内容:

[arm@localhost my_rootfs]#vi linuxrc

#!/bin/sh


----------------------- 页面 64-----------------------

#挂载/etc 为ramfs, 并从/mnt/etc 下拷贝文件到/etc  目录当中

echo "mount /etc as ramfs"

/bin/mount -n -t ramfs ramfs /etc

/bin/cp -a /mnt/etc/* /etc



echo "re-create the /etc/mtab entries"

# re-create the /etc/mtab entries

/bin/mount -f -t cramfs -o remount,ro /dev/mtdblock/2 /



#mount some file system

echo "------------mount /dev/shm as tmpfs"

/bin/mount -n -t tmpfs tmpfs /dev/shm



#挂载/proc 为proc 文件系统

echo "------------mount /proc as proc"

/bin/mount -n -t proc none /proc



#挂载/sys 为sysfs 文件系统

echo "------------mount /sys as sysfs"

/bin/mount -n -t sysfs none /sys



exec /sbin/init



2. 修改权限

[arm@localhost my_rootfs]#chmod 775 linuxrc

[arm@localhost my_rootfs]#ls linuxrc -al

-rwxrwxr-x   1 root root  533 Jun 4 11:19 linuxrc



当编译内核时,指定命令行参数如下

Boot options  ---> Default kernel command string: 我的命令行参数如下

         noinitrd root=/dev/mtdblock2 init=/linuxrc console=ttySAC0,115200



其中的init 指明kernel 执行后要加载的第一个应用程序,缺省为/sbin/init ,此处指定为/linuxrc



2 移植 Busybox



2.1 下载busybox



从下载busybox-1.1.3 到/tmp  目录当中,并解压.



2.2 进入解压后的目录,配置Busybox

[arm@localhost busybox-1.1.3]$ make menuconfig

Busybox Settings  --->

                  General Configuration   --->

                           [*] Support for devfs

                  Build Options  --->

                           [*] Build BusyBox as a static binary (no shared libs)

                                    /* 将busybox 编译为静态连接,少了启动时找动态库的麻烦 */

                           [*] Do you want to build BusyBox with a Cross Compiler?

                           (/usr/local/arm/3.3.2/bin/arm-linux-) Cross Compiler prefix


----------------------- 页面 65-----------------------

                                    /* 指定交叉编译工具路径 */

                  Init Utilities  --->

                           [*] init

                           [*] Support reading an inittab file

                              /* 支持init 读取/etc/inittab 配置文件,一定要选上 */

                  Shells  --->

                  Choose your default shell (ash)  --->

                             /* (X) ash 选中ash ,这样生成的时候才会生成bin/sh 文件

                             * 看看我们前头的linuxrc 脚本的头一句:

                             * #!/bin/sh 是由bin/sh 来解释执行的

                       */

                           [*] ash



Coreutils  --->

                           [*] cp

                           [*] cat

                           [*] ls

                           [*] mkdir

                           [*] echo (basic SuSv3 version taking no options)

                           [*] env

                           [*] mv

                           [*] pwd

                           [*] rm

                           [*] touch



Editors  --->     [*] vi



Linux System Utilities  --->

                           [*] mount

                           [*] umount

                           [*] Support loopback mounts

                           [*] Support for the old /etc/mtab file



Networking Utilities  --->

                           [*] inetd

                                    /*

                                     * 支持inetd 超级服务器

                                     * inetd 的配置文件为/etc/inetd.conf 文件,

                                     * "在该部分的4: 相关配置文件的创建"一节会有说明

                              */



2.3 编译并安装 Busybox



[arm@localhost busybox-1.1.3]$ make TARGET_ARCH=arm CROSS=arm-linux- \

PREFIX=/home/arm/dev_home/rootfs/my_rootfs/ all install



PREFIX 指明安装路径:就是我们根文件系统所在路径。



*这里需要注意一点的是,只要install busybox ,我们根文件系统下先前建好的linuxrc 就会被覆盖为一同名二进

制文件。

所以要事先备份我们自己的linuxrc ,在安装完busybox 后,将linuxrc 复制回去就好。


----------------------- 页面 66-----------------------

3 移植 TinyLogin



3.1 下载



从 下载tinylogin-1.4 到/tmp  目录当中,并解压.



3.2 修改tinyLogin 的Makefile



[arm@localhost tinylogin-1.4]$ vi Makefile

修改记录如下:



指明静态编译,不连接动态库

DOSTATIC = true



指明tinyLogin 使用自己的算法来处理用户密码

USE_SYSTEM_PWD_GRP = false

USE_SYSTEM_SHADOW = false



3.3 编译并安装



[root@localhost tinylogin-1.4]# make CROSS=arm-linux- PREFIX=/home/arm/dev_home/rootfs/my_rootfs all install



PREFIX 指明根文件路径



4 相关配置文件的创建



进入mnt/etc 中, 这里是我们存放配置文件的路径

[arm@localhost my_rootfs]$ cd mnt/etc



4.1 创建帐号及密码文件



[arm@localhost etc]$ cp /etc/passwd .

[arm@localhost etc]$ cp /etc/shadow .

[arm@localhost etc]$ cp /etc/group .



这3 个文件是从你工作站当中拷贝过来的,删除其中绝大部分不需要的用户,经过删减后的上述3 个文件如下。

那么现在root 的登陆密码

和你工作站上的登陆口令一致了,这可能透露你工作站的信息



[arm@localhost etc]$ cat passwd

root:x:0:0:root:/root:/bin/sh /* 改为/bin/sh */

bin:x:1:1:bin:/bin:/sbin/nologin

daemon:x:2:2:daemon:/sbin:/sbin/nologin



[arm@localhost etc]$ cat shadow

root:$1$2LG20u89$UCEEUzBhElYpKMNZQPU.e1:13303:0:99999:7:::

bin:*:13283:0:99999:7:::

daemon:*:13283:0:99999:7:::



[arm@localhost etc]$ cat group

root:x:0:root

bin:x:1:root,bin,daemon


----------------------- 页面 67-----------------------

daemon:x:2:root,bin,daemon



4.2 创建 profile 文件



[arm@localhost etc]$ vi profile

# Set search library path

# 这条语句设置动态库的搜索路径,极其重要!!!

echo "Set search library path int /etc/profile"

export LD_LIBRARY_PATH=/lib:/usr/lib



# Set user path

echo "Set user path in /etc/profile"

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

export PATH



4.4 创建fstab 文件



[arm@localhost etc]$ vi fstab

none       /proc      proc   defaults       0 0

none       /dev/pts    devpts  mode=0622     0 0

tmpfs      /dev/shm     tmpfs  defaults     0 0



4.5 创建 inetd.conf 配置文件



    此处只是一个经过修改的示例配置文件,用于代理监听telnetd 的23 端口。

    读者可以根据自己需求进行修改。该配置文件可以从busybox 的examples  目录中获得



[arm@localhost etc]$ vi inetd.conf

#

#ftp             stream       tcp     nowait  root  /usr/sbin/ftpd

telnet            stream     tcp      nowait  root  /usr/sbin/telnetd



5 移植 inetd



5.1 inetd 的选择及获取



    Busybox1.1.3 提供了inetd 支持。如果读者使用的是较低版本的不提供inetd 的Busybox ,那么可以考虑使

用netkit 套件来提供网络服务。强烈建议使用高版本的Busybox 。此节后半部分介绍如果编译布署netkit 当中的

inetd 。



5.1.1 获取 inetd



    Netkit 套件可以从ftp://ftp.uk.linux.org/pub/linux/Networking/netkit/下载。

    其中netkit-base-0.17 中包括inetd 程序。下载netkit-base-0.17 到/tmp  目录并解压。



5.2 编译inetd



5.2.1 修改 configure文件



    开始配置netkit-base 之前需要先修改configure 脚本以免它在主机上执行测试程序。

[arm@localhost netkit-base-0.17]# vi configure


----------------------- 页面 68-----------------------

将每一      出现的 ./__conftest || exit 1 ;

修改成:

# ./__conftest || exit 1 ;



5.2.2 编译 



[arm@localhost netkit-base-0.17]$ CC=arm-linux-gcc ./configure

[arm@localhost netkit-base-0.17]$ make



5.3 配置 inetd



5.3.1 拷贝 inetd到根文件系统的 usr/sbin 目录中



[arm@localhost netkit-base-0.17]$ cp inetd/inetd /home/arm/dev_home/rootfs/my_rootfs/usr/sbin/



拷贝inetd 的配置文件inetd.conf 到根文件系统的/mnt/etc  目录中

[arm@localhost netkit-base-0.17]$ cp etc.sample/inetd.conf /home/arm/dev_home/rootfs/my_rootfs/mnt/etc



5.3.2 根据需要,修改 inetd.conf配置文件

例如:支持telnetd 的inetd.conf 配置文件如下

#

  telnet        stream      tcp     nowait root   /usr/sbin/telnetd



5.3.3 拷贝配置文件

etc.sample  目录下有许多网络相关配置文件,其中有一些需要拷贝到根文件系统的etc  目录当中,记录如下:

[arm@localhost netkit-base-0.17]$ cd etc.sample/

[arm@localhost etc.sample]$ cp host.conf /home/arm/dev_home/rootfs/my_rootfs/mnt/etc/

[arm@localhost etc.sample]$ cp hosts /home/arm/dev_home/rootfs/my_rootfs/mnt/etc/

[arm@localhost etc.sample]$ cp networks /home/arm/dev_home/rootfs/my_rootfs/mnt/etc/

[arm@localhost etc.sample]$ cp protocols /home/arm/dev_home/rootfs/my_rootfs/mnt/etc/

[arm@localhost etc.sample]$ cp resolv.conf /home/arm/dev_home/rootfs/my_rootfs/mnt/etc/

[arm@localhost etc.sample]$ cp services /home/arm/dev_home/rootfs/my_rootfs/mnt/etc/



以上重要配置文件说明如下:

host.conf:在系统中同时存在着DNS 域名解析和/etc/hosts 的主机表机制时,由文件/etc/host.conf 来说明了解析器

的查询顺序



hosts:记录主机名到IP 地址的映射



protocols:记录常用网络协议及端口别名关系,网络应用程序依赖于此文件



resolv.conf:指定DNS 服务器



services:记录知名网络服务及端口,网络编程依赖于此文件


----------------------- 页面 69-----------------------

6 移植 thttpd Web服务器



6.1 下载



   从 下载thttpd 到/tmp  目录当中,并解压.



6.2 编译thttpd



[arm@localhost thttpd-2.25b]$ CC=arm-linux-gcc ./configure --host=arm-linux

[arm@localhost thttpd-2.25b]$ vi Makefile

指定静态链接二进制文件

LDFLAGS =       -static



[arm@localhost thttpd-2.25b]$ make LDFLAGS="-static"



6.3 配置



6.3.1 拷贝 thttpd二进制可执行文件到根文件系统/ usr/sbin/ 目录中



[arm@localhost thttpd-2.25b]$ cp thttpd /home/arm/dev_home/rootfs/my_rootfs/usr/sbin/



6.3.2 修改 thttpd配置文件



[arm@localhost thttpd-2.25b]$ vi contrib/redhat-rpm/thttpd.conf

# This section overrides defaults

dir=/etc/thttpd/html            #指明WebServer 存放网页的根目录路径

chroot

user=root                       # 以root 身份运       thttpd

logfile=/etc/thttpd/log/thttpd.log # 日志文件路径

pidfile=/etc/thttpd/run/thttpd.pid #pid 文件路径



拷贝thttpd.conf 配置文件到根文件系统的mnt/etc/ 目录,

系统加载后,linuxrc 脚本会自动将mnt/etc/下的所有文件拷贝到/etc  目录中。

[arm@localhost thttpd-2.25b]$ cp contrib/redhat-rpm/thttpd.conf  /home/arm/dev_home/rootfs/my_rootfs/mnt/etc/



6.3.3 转移到根文件系统目录,创建相应的文件



[arm@localhost etc]$ cd /home/arm/dev_home/rootfs/my_rootfs

[arm@localhost my_rootfs]$ cd mnt/etc/



创建thttpd  目录

[arm@localhost etc]$ mkdir thttpd

[arm@localhost etc]$ cd thttpd

thttpd  目录下的目录结构

|-- html

| `-- index.html  Web Server 网页根目录下的默认HTML 文件

|-- log

| `-- thttpd.log 创建一个空文件就可

`-- run

    `-- thttpd.pid 创建一个空文件就可



html  目录下的index.html 文件内容如下:


----------------------- 页面 70-----------------------





        Welcome to here^^





       

               

                        Welcome to here^^!!!

               


       








7 建立根目录文件系统包



7.1 建立CRAMFS包



7.1.1 下载 cramfs 工具



从 下载源代码包.

把下载包拷贝到dev_home/tools 下.

[arm@localhost tools]$tar -xzvf cramfs-1.1.tar.gz

[arm@localhost tools]$cd cramfs-1.1

[arm@localhost tools]$make

[arm@localhost tools]$su root

[root@localhost tools]$cp mkcramfs /usr/bin

[arm@localhost tools]$exit

注意:如果你的系统中已经安装了mkcramfs 工具, 则在/usr/bin  目录下是一个软link, 请先删除该文件之后, 再拷

贝该mkcramfs 到/usr/bin 下.



7.1.2 制作 cramfs包



[arm@localhost tools]$mkcramfs my_rootfs my_rootfs.cramfs



7.1.3 写 cramfs包到 Nand Flash



[arm@localhost tools]$su root

[root@localhost tools]$cp my_rootfs.cramfs /tftpboot/



打开minicom, 进       ARM 板的终端模式:

CRANE2410 #



8 参考资料



1. linux  目录结构介绍





2. <>

    中文名:<<构建嵌入式Linux 系统>>

作者介绍:本文由尚观科技老师和同学生(刘勇,孙贺,聂强,聂大鹏 ,牛须乐,孙磊)共同创作



阅读(663) | 评论(0) | 转发(0) |
0

上一篇:ARM9 2410移植之应用程序的移植

下一篇:GO !

给主人留下些什么吧!~~