rootfs的制作(busybox)和安装

系统环境:

  操作系统:Ubuntu12.04(lsb_release -a命令查看)
  交叉编译工具链:arm-linux-gcc 4.5.1
  busybox源码包:busybox-1.25.0

1、创建根文件系统的目录

[root@localhost fs]# ls
[root@localhost fs]# mkdir myrootfs
[root@localhost fs]# cd myrootfs/
[root@localhost myrootfs]# mkdir bin dev etc lib proc sbin sys usr mnt tmp var
[root@localhost myrootfs]# mkdir usr/bin usr/lib usr/sbin lib/modules

2、创建设备文件

[root@localhost myrootfs]# cd dev/
[root@localhost dev]# mknod -m 666 console c 5 1
[root@localhost dev]# mknod -m 666 null c 1 3

3、配置/etc/

3.1 获取配置文件

     把busybox源码目录下的etc的内容拷贝到这里的/home/fs/myrootfs/etc下

cp -a /home/w/busybox-1.25.0/examples/bootfloppy/etc/* ./

 3.2 修改配置文件

    我们可以根据我们需要,对下面列出的配置文件,做相应的修改,可参考下面的参考文章。

etc/
etc/inittab
etc/profile
etc/fstab
etc/init.d/
etc/init.d/rcS

    也可以使用3.1中拷贝的默认配置文件,不过需要修改inittab文件中的

tty2::askfirst:-/bin/sh

::askfirst:-/bin/sh
修改之后表示运行/bin/sh的进程默认使用与init进程一样的控制台。

参考 :http://blog.csdn.net/velanjun/article/details/7289061

4、安装glibc库

      我们将busybox编译为静态的,可以不配置这个步骤。如果编译busybox时选择动态库方式编译,则需要查看生成的busybox使用哪些动态库,然后把它们拷贝到rootfs/lib目录下。可参考下面的方法。

     在root_fs下新建lib目录,再把arm-linux-交叉编译链下的lib文件拷贝到我们root_fs下的lib目录下。我使用cp /usr/local/arm/4.3.2/arm-none-linux-gnueabi/libc/armv4t/lib/*root_fs/lib/* -df 使用-d选项表示链接文件按照原来的链接方式拷贝,否则链接文件拷贝过来是一个副本。

5、配置busybox

在我们的根文件系统中的/bin和/sbin目录下有各种命令的应用程序,而这些程序在嵌入式系统中都是通过busybox来构建的,每一个命令实际上都是一个指向busybox的链接,busybox通过传入的参数来决定进行何种命令操作。

[root@localhost fs]# ls
busybox-1.13.3.tar.gz  etc.tar.gz  myrootfs
[root@localhost fs]# tar -xvzf busybox-1.13.3.tar.gz
[root@localhost fs]# cd busybox-1.13.3
[root@localhost busybox-1.13.3]# make menuconfig

Busybox Settings-----> build Options----->

选中Build BusyBox as a static binary(no shared libs)

 Busybox Settings-----> InstallationOptions ------------>     

选中 “don't use /usr”    选中此项可以避免busybox被安装到宿主系统的/usr目录下,破坏宿主系统

Busybox Installation Prefix(/home/fs/myrootfs)   这里要输入busybox安装的绝对路径

6、编译安装 busybox

[root@localhost busybox-1.13.3]# make ARCH=arm CROSS_COMPILE=arm-linux-
[root@localhost busybox-1.13.3]# make install

将busybox _install目录中的文件拷贝到/home/fs/myrootfs目录中。

下面给出编译过程中我遇到了下面4个问题,以及它们的解决办法。

出现如下错误:

miscutils/nandwrite.c: In function 'nandwrite_main':
miscutils/nandwrite.c:151: error: 'MTD_FILE_MODE_RAW' undeclared (first use in this function)
miscutils/nandwrite.c:151: error: (Each undeclared identifier is reported only once
miscutils/nandwrite.c:151: error: for each function it appears in.)
scripts/Makefile.build:197: recipe for target 'miscutils/nandwrite.o' failed
make[1]: *** [miscutils/nandwrite.o] Error 1
Makefile:742: recipe for target 'miscutils' failed
make: *** [miscutils] Error 2

解决办法:

MTD_FILE_MODE_RAW在/usr/include/mtd/mtd-abi.h中定义,直接在nandwrite.c中包含编译仍无法通过。将/usr/include/mtd/mtd-abi.h拷贝到busybox的include文件中,然后包含该头文件:

#include "libbb.h"
#include "mtd-abi.h"
#include

编译可以通过。

继续make,出现如下错误:

util-linux/blkdiscard.c: In function 'blkdiscard_main':
util-linux/blkdiscard.c:72: error: 'BLKSECDISCARD' undeclared (first use in this function)
util-linux/blkdiscard.c:72: error: (Each undeclared identifier is reported only once
util-linux/blkdiscard.c:72: error: for each function it appears in.)
scripts/Makefile.build:197: recipe for target 'util-linux/blkdiscard.o' failed
make[1]: *** [util-linux/blkdiscard.o] Error 1
Makefile:742: recipe for target 'util-linux' failed
make: *** [util-linux] Error 2

解决办法:

BLKSECDISCARD在/usr/include/linux/fs.h中定义,方法如上所述,将/usr/include/linux/fs.h拷贝到busybox的include文件中,然后修改blkdiscard.c中头文件包含:

修改:#include

#include "fs.h"

继续make,编译通过,但是在链接的时候出现问题:

networking/lib.a(nslookup.o): In function `print_host':
nslookup.c:(.text.print_host+0x44): warning: Using 'getaddrinfo' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
debianutils/lib.a(mktemp.o): In function `mktemp_main':
mktemp.c:(.text.mktemp_main+0x98): warning: the use of `mktemp' is dangerous, better use `mkstemp'
networking/lib.a(ipcalc.o): In function `ipcalc_main':
ipcalc.c:(.text.ipcalc_main+0x25c): warning: Using 'gethostbyaddr' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
libbb/lib.a(inet_common.o): In function `INET_resolve':
inet_common.c:(.text.INET_resolve+0x60): warning: Using 'gethostbyname' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
networking/lib.a(inetd.o): In function `reread_config_file':
inetd.c:(.text.reread_config_file+0x230): warning: Using 'getservbyname' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
networking/lib.a(netstat.o): In function `ip_port_str':
netstat.c:(.text.ip_port_str+0x50): warning: Using 'getservbyport' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
util-linux/lib.a(nsenter.o): In function `nsenter_main':
nsenter.c:(.text.nsenter_main+0x1b0): undefined reference to `setns'
collect2: ld returned 1 exit status
Note: if build needs additional libraries, put them in CONFIG_EXTRA_LDLIBS.
Example: CONFIG_EXTRA_LDLIBS="pthread dl tirpc audit pam"
Makefile:717: recipe for target 'busybox_unstripped' failed
make: *** [busybox_unstripped] Error 1 

解决办法:

make menuconfig

Linux System Utilities--->nsenter,去掉该选项,重新编译make,又出现如下错误:

networking/lib.a(nslookup.o): In function `print_host':
nslookup.c:(.text.print_host+0x44): warning: Using 'getaddrinfo' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
debianutils/lib.a(mktemp.o): In function `mktemp_main':
mktemp.c:(.text.mktemp_main+0x98): warning: the use of `mktemp' is dangerous, better use `mkstemp'
networking/lib.a(ipcalc.o): In function `ipcalc_main':
ipcalc.c:(.text.ipcalc_main+0x25c): warning: Using 'gethostbyaddr' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
libbb/lib.a(inet_common.o): In function `INET_resolve':
inet_common.c:(.text.INET_resolve+0x60): warning: Using 'gethostbyname' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
networking/lib.a(inetd.o): In function `reread_config_file':
inetd.c:(.text.reread_config_file+0x230): warning: Using 'getservbyname' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
networking/lib.a(netstat.o): In function `ip_port_str':
netstat.c:(.text.ip_port_str+0x50): warning: Using 'getservbyport' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
coreutils/lib.a(sync.o): In function `sync_main':
sync.c:(.text.sync_main+0x7c): undefined reference to `syncfs'
collect2: ld returned 1 exit status
Note: if build needs additional libraries, put them in CONFIG_EXTRA_LDLIBS.
Example: CONFIG_EXTRA_LDLIBS="pthread dl tirpc audit pam"
Makefile:717: recipe for target 'busybox_unstripped' failed
make: *** [busybox_unstripped] Error 1

解决办法:

make menuconfig

Coreutils--->sync选项去掉,重新make编译通过,生成了busybox可执行文件。

参考:http://www.cnblogs.com/softhal/p/5769121.html

这样根文件系统就做好了。

7、根文件系统安装

7.1、安装initramfs根文件系统

要使用initramfs做根文件系统,在内核中做如下配置,并重新编译内核镜像即可。
General setup --->
[*] Initial RAM filesystem and RAM disk (initramfs/initrd) support
(/rootfs_dir) Initramfs source file(s) //输入根文件系统的所在目录

7.2、将根文件系统制作 成ramdisk

[root@localhost fs]# genext2fs -b 8192 -d /home/fs/myrootfs/  ramdisk
[root@localhost fs]# gzip -9 -f ramdisk
[root@localhost fs]# ls
busybox-1.13.3         etc.tar.gz       genext2fs-1.4.1.tar.gz  ramdisk.gz
busybox-1.13.3.tar.gz  genext2fs-1.4.1  myrootfs

将ramdisk.gz下载到开发板上就可以了。

8、启动错误分析

     在系统启动过程中,加载init程序时会出现“Kernel panic - not syncing: Attempted to kill init!”错误,我发现这个错误是与使用不同版本的交叉编译器编译出的busybox可执行文件不能用有关,具体分析可参考我的这篇文章,“ARM下Kernel panic - not syncing: Attempted to kill init!错误分析”。

9、参考资料

[1] linux根文件系统的制作

http://blog.163.com/liuxin_arm/blog/static/18329142320126425338286/

你可能感兴趣的:(File,system)