Chinaunix首页 | 论坛 | 博客
  • 博客访问: 949404
  • 博文数量: 261
  • 博客积分: 10026
  • 博客等级: 上将
  • 技术积分: 3420
  • 用 户 组: 普通用户
  • 注册时间: 2009-02-24 12:10
个人简介

https://smart888.taobao.com/ 立观智能监控

文章分类

全部博文(261)

文章存档

2011年(1)

2010年(4)

2009年(256)

我的朋友

分类: LINUX

2009-03-13 13:15:57

根文件系统构建
      一.建立基本的目录结构
      二.交叉编译 busybox
      三.创建配置文件

其步骤如下:
1. 建立基本的目录结构
    i).在宿主机上建一个目录作为目标系统的根目录
      在/usr/local/arm/下创建根目录my_rootfs:
       # mkdir my_rootfs
       # cd my_rootfs      
[root@localhost my_rootfs]#mkdir bin dev etc home lib mnt proc sbin sys tmp root usr
[root@localhost my_rootfs]#mkdir mnt/etc
[root@localhost my_rootfs]#mkdir mnt/etc/init.d
//[root@localhost my_rootfs]#mkdir usr/bin usr/lib usr/sbin
[root@localhost my_rootfs]#touch linuxrc
        [root@localhost my_rootfs]#touch etc/inittab
        [root@localhost my_rootfs]#mkdir etc/init.d  
        [root@localhost my_rootfs]#touch etc/init.d/rcS
        [root@localhost my_rootfs]#touch etc/passwd
        [root@localhost my_rootfs]#touch etc/group
        [root@localhost my_rootfs]#touch etc/busybox.conf       
        [root@localhost my_rootfs]#touch etc/hosts
        [root@localhost my_rootfs]#touch etc/securetty
        [root@localhost my_rootfs]#touch etc/fstab
[root@localhost my_rootfs]#tree
|bin
|dev
|etc_
     |fstab   
     |init.d_
      |rcS
     |inittab
     |passwd
     |group
     |busybox.cof   
     |hosts
     |securetty
|home
|lib
|mnt__
      |etc_
           |init.d  
|proc
|sbin
|sys
|tmp
|root
|usr__
      |bin
      |lib
      |sbin
      |etc_
    |profile
    |rc.local
|linuxrc   /* 此文件为启动脚本,是一shell脚本文件。本文后面有专门介绍 */

         
    ii).在 usr 目录下建立指向 lib,bin,sbin 的符号连接
       # cd usr
       # ln -s ../lib lib
       # ln -s ../bin bin
       # ln -s ../sbin sbin
               【注】:我们使用 devfs ,所以不需手动建立 dev 目录下的设备文件
2. 交叉编译 busybox
    a.下载 busybox 源码并解压缩
         从 下载busybox
         #tar jxvf busybox-1.5.1.tar.bz2
    b. 配置
         #cd busybox-1.5.1

         #vi Makefile
修改Makefile: (大概在175行左右)
         ARCH            ?= arm
        CROSS_COMPILE   ?= /usr/local/arm/3.4.1/bin/arm-linux-
#make menuconfig
下面是需要编译进busybox的功能选项。
(1)Busybox Setting-->                           
/   i)general configure-->[*]support for devfs //设置devfs 选项                         
              ii)build options-->
/     [*]do you want to build busybox with a cross compiler?
     //设置交叉编译选项。 输入: usr/local/arm/3.4.1/bin/arm-linux-
     交叉编译工具链的路径      
                   [*] Build BusyBox as a static binary (no shared libs)
                                 //注: 如果选中该选项 , 则 busybox 编译成静态链接的可执行文件
   iii)Installation Options
                  [*] Installation Options Don't use /usr
                                //注:这个选项一定要选 , 否则 make install 后 busybox 将安装在原系统的 /usr下
   iv)Busybox Library Tunning->
     [*]Command line editing
     //在shell中命令的自动补全和命令可编辑还是挺方便的。这些功能在busybox1.5以前shell     //的'command line editing'项在shell的选项内,但是从1.5开始这个选项已经移到'Busybox      //Settings -> Busybox Library Tuning'中了,

        (2)Init Utilities-->
     [*]init
     [*]Support reading an inittab file
      //支持init读取/etc/inittab配置文件,一定要选上。
                                [*]Support running commands with a controlling-tty
                                        //使busybox在真实的串口设备中运行命令行
      //不选择可能会报错:sh: can't access tty; job control turned off   
(3)Shell-->
     [*]Choose your default shell(ash)-->
      [*]ash
      //使用“空格”键选中。只有选中了ash,这样生成的时候才会生成bin/sh文件
      //在linuxrc脚本的头一句:#!/bin/sh是由bin/sh来解释执行的。
      //不选中会报错:can not run -bin/sh  
(4)Coreutils-->
    [*]cp
    [*]cat
    [*]ls
    [*]mkdir
    [*]echo
    [*]env
    [*]mv
    [*]pwd
    [*]rm
    [*]touch
(5)Editor--> [*]vi
(6)Linux System Utilities-->
    [*]mount               
    [*]umount
    [*]Support loopback mounts
    [*]Support for the old/etc/mtab file
(7)Networking Utilities -->
    [*]inetd
    //支持inetd超级服务器。inetd 的配置文件为/etc/inetd.conf文件
     c. 再选择你需要的命令
                       保存退出
                    编译: #make
                    安装: #make install
     d. 复制文件到根文件系统中:
              #cp    -ad    _install/sbin    usr/local/arm/my_rootfs/
       #cp    -ad    _install/bin     usr/local/arm/my_rootfs/
3. 创建配置文件
---------------------------------------------------------------------------------
文件系统配置流程:
1).linuxrc
执行init 进程初始化文件。主要工作是把已安装根文件系统中的/etc 安装为ramfs,
并拷贝/mnt/etc/目录下所有文件到/etc,这里存放系统启动后的许多特殊文件
2).inittab
重新构建文件分配表
3).mnt/etc/init.d/rcS
完成各个文件系统的 mount;通过rcS 可以调用 dhcp 程序配置网络

4).执行/sbin/init
init 就会在一个 console 上,按照 inittab 的指示开一个 shell,或者是开 getty + login,这样用户就会看到提示输入用户名的
提示符。

5). usr/etc/rc.local
是被init.d/rcS 文件调用执行的特殊文件,与Linux 系统硬件平台相关,
如安装核心模块、进行网络配置、运行应用程序、启动图形界面等

6).profile 首先执行该文件配置应用程序需要的环境变量等
---------------------------------------------------------------------------------

(1).编写linuxrc 文件(路径/my_rootfs/)
执行init 进程初始化文件。主要工作是把已安装根文件系统中的/etc 安装为ramfs,
并拷贝/mnt/etc/目录下所有文件到/etc,这里存放系统启动后的许多特殊文件
-----------------------------------------------------------------
#!/bin/sh
#echo "mount /etc as ramfs"
/bin/mount -n -t ramfs ramfs /etc ##加载一个ramfs作为/etc目录。这样/etc就是一个可写目录
/bin/cp -a /mnt/etc/* /etc         ##/etc成为可写目录后,将所有/mnt/etc中的配置文件拷贝到/etc/中,这说明你的ramfs可能                                   ##是一个空的ramfs,没有配置文件,或者配置文件比较老。 同时也说明你这个系统是一个只                                   ##读系统,每次系统运行中写入的配置不会保留

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的那些信息重新写到/etc/mtab中
/bin/mount -f -t ramfs ramfs /etc


#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

/bin/mount -f -t ramfs ramfs /etc

#可先不添加该语句
echo "yaffs is mounted"
/bin/mount -t yaffs /dev/mtdblock/1 /mnt/yaffs
#

exec /sbin/init ## 执行根文件系统中的init执行程序,使其成为1号进程。shell正式运行
---------------------------------------------------------------------
(2). 权限修改
chmod 775 linuxrc

(3). 编写 inittab文件(路径:/my_rootfs/etc/)
---------------------------------------------------------------------
console::sysinit:/etc/init.d/rcS //指定系统初始化脚本文件
ttySAC0::respawn:/sbin/getty -L ttyUSB0 115200 vt100 //使命令行运行在真实的串口设备中

::askfirst:-/bin/sh        //指定一个登陆的shell (注意,这里如果用getty或minigetty代替sh,那么shell就会等待用户登录,               //而不会直接进入shell。)

/*
tty2::askfirst:-/bin/sh   
tty3::askfirst:-/bin/sh
tty4::askfirst:-/bin/sh
tty0::askfirst:-/bin/sh
*/
  
::restart:/sbin/init      //指定系统重启时执行的初始化程序

::once:/usr/etc/rc.local   //指定首次执行的脚本文件       

::ctrlaltdel:/sbin/reboot    //指定当按下ctrl+alt+del键时执行的命令,嵌入式中不用
::shutdown:/bin/umount -a -r //指定关机时执行的操作
::shutdown:/sbin/swapoff -a
----------------------------------------------------------------------

(4). rcS文件(路径:/my_rootfs/etc/init.d/)
----------------------------------------------------------------------
#!/bin/sh

/bin/mount -a

exec /usr/etc/rc.local
-----------------------------------------------------------------------
修改rcS文件权限
chmod 775 rcS   //不更改权限会报错:Cannot run '/etc/init.d/rcS': Permission denied


-----------------------------------------------------------------------
-----------------------------------------------------------------------
(5) rc.local文件(路径:/my_rootfs/usr/etc/)中添加:
#!/bin/sh
.usr/etc/profile
echo "Hello wpb3dm"

-----------------------------------------------------------------------
修改rcS文件权限
chmod 775 rc.local   //不更改权限会报错:/usr/etc/rc.local: Permission denied

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

(6).fstab 文件(路径:/my_rootfs/etc/)
----------------------------------------------------------------------
none       /proc      proc   defaults 0 0
none       /dev/pts   devpts mode=0622 0 0
tmpfs      /dev/shm   tmpfs defaults 0 0
----------------------------------------------------------------------
(7). 在Profile中添加(/usr/etc)
#!/bin/sh
PATH=/bin:/sbin:/usr/bin:/usr/sbin    #设置命令工具所在位置

----------------------------------------------------------------------
/* securetty文件:限制root直接登录的配置文件,只对telnet有效
                   console
                   ttyS0
                   ttySAC0
                   tts/0
                   tty1
                   tty2
                   vc/1
                   vc/2
*/
----------------------------------------------------------------------

(8). 修改/my_rootfs/etc/下面的Passwd & group & busybox.conf等文件
             i)从宿主机上拷贝 passwd 和 group 文件过来删掉其中多余的用户信息
   #cp /etc/passwd .
   #cp /etc/group .
   #vi passwd //保留前三行,其中,把第一行末尾root:后面改为root:/bin/sh
   #vi group //保留前三行
             //ii)busybox.conf 的内容:
             //      [SUID]
             //     su = ssx root.root
             //     passwd = ssx root.root
             //       more = ssx root.root
/* ii). 须securetty添加:
                   console
                   ttyS0
                   ttySAC0
                   tts/0
                   tty1
                   tty2
                   vc/1
                   vc/2
*/

(9)文件配置:
   (i)把 (4) 生成的文件(rcS)拷贝到 /mnt/etc/init.d/目录(先在/mnt/etc/目录新建格init.d目录)
[root@localhost my_rootfs]#cp -rf etc/init.d/rcS mnt/etc/init.d
   (ii)把 (3),(6)生成的文件(inittab和 fastab )放到 /mnt/etc/目录
   [root@localhost my_rootfs]#cp -rf etc/inittab mnt/etc/
   [root@localhost my_rootfs]#cp -rf etc/fastab   mnt/etc/
   (iii)把交叉编译器3.4.1下的lib文件的所有内容拷贝到my_rootfs下面覆盖原来的lib文件夹  
[root@localhost arm]cp -rf 3.4.1/lib my_rootfs  

(10). 创建串口设备文件,在/dev目录(即my_rootfs/dev/).
      mknod -m 666 /dev/ttySAC0 c 204 64

//* 在命令行输入: cat /proc/devices显示如下:
Character devices:
1 mem
2 pty
3 ttyp
204 S3C2410 tty
10 misc
108 ppp
180 usb
Block devices:
8 sd
31 mtdblock
找到主设备好为204的设备
*/

4. 生成cramfs文件
./mkfs.cramfs ./my_rootfs       ./root.cramfs
   烧写到第2部分(rootfs部分)

===============================================================================
===============================================================================

********************************************************************************
错误日志:
********************************************************************************
一、【错误】: -sh: can't access tty; job control turned off

【解决方法】:
在为S3C2410或其他平台编译运行busybox的时候,可能会遇到 can't access tty; job control turned off 的警告,
    其实这个问题的实质是busybox系统没有使用真正的串口设备,而是使用虚拟控制台。所以我们可以按照以下步骤来解决:(以s3c2410为例)

1. 在配置busybox时; makemenuconfig;
在init菜单里选择Support running commands with a controlling-tty,是busybox在真实的串口设备中运行命令行;
2. 修改/etc/inittab文件,使命令行运行在真实的串口设备中,
在/etc/inittab写入
console::sysinit:-/etc/rcS
ttySAC0::respawn:/sbin/getty -L ttyUSB0 115200 vt100   //普通串口用ttyS0,USB转串口的用ttyUSB0
3. 设置好上面的信息后,需要创建串口设备文件(当然你可以使用devfs,不推荐), 在命令行输入: cat /proc/devices
显示如下:

Character devices:

1 mem

2 pty

3 ttyp

204 S3C2410 tty

10 misc

108 ppp

180 usb

Block devices:

8 sd

31 mtdblock

找到主设备好为204的设备,然后在dev目录下面:创建设备文件mknod -m 666 /dev/ttySAC0 c 204 64

二、【错误】:Cannot run '/etc/init.d/rcS': Permission denied
【解决方法】:修改rcS文件权限
       chmod 775 rcS  

三、【错误】:Cannot run '/etc/init.d/rcS'。No such file or directory.

改用busybox-1.5.1试试,1.1.3可能有点问题。

三、
【错误】:re-create the /etc/mtab entries
mount: cannot read /etc/mtab: No such file or directory
【解决方法】:?????

阅读(2306) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~