使用busybox制作根文件系统(rootfs)

时间:2023-02-06 09:00:23

        我们知道一个linux的启动过程,包括BIOS的加电自检POST,拷贝MBR的信息(启动BootLoader),加载内核,挂载根文件安系统这几大步,在嵌入式系统的移植方面我们也要自己动手制作内核映像,根文件系统等。今天境就带大家讲讲使用busybox制作嵌入式可以移植的根文件系统。
需要的材料:
1,busybox(busybox-1.14.2)
        下载一个版本的busybox。下载地址。
        busybox是主要用来提供一些bash shell命令的工具。
2,配置文件 

        重点配置文件是在境的上一篇文章提到的几个文件。linux启动过程中的几个重要文件的详解
3,设备文件(文中会教你如何添加)
4,必要的库文件(选用)

所以今天我要的做的就是由busybox等组成的rootfs。
制作过程:
第一步:编译busybox获得shell工具。
1,在用户主目录下建立一个用于我们实验的一个目录(~/exp/mkrootfs),并转至该目录;
@ubuntu:~$ mkdir -p ~/exp/mkrootfs
@ubuntu:~$ cd ~/exp/mkrootfs
2,将刚才的下载的busybox解压至此,并转至;
@ubuntu:~/exp/mkrootfs$ tar xvjf busybox-1.14.2.tar.bz2
@ubuntu:~/exp/mkrootfs$ cd busybox-1.14.2/
3,修改makefile文件来修改我们的arch和gcc编译工具;没有安装交叉编译工具的筒子请看快速安装ubuntu交叉编译工具。
@ubuntu:~/exp/mkrootfs$vim Makefile
修改164行:ROSS_COMPILE ?=/usr/bin/arm-linux-gnueabi-
修改190行:ARCH ?= arm

4,make menuconfig

@ubuntu:~/exp/mkrootfs$ make menuconfig
如果遇到像境这样的问题:请查看文章最后面。
 5,优化我们的配置选项,在做make menuconfig之前最好,全屏打开你的terminal,这样才还显示类似编译内核一样的配置框,如下:
主要也是根据个人口味来选择优化配置,境选择如下:
   Busybox Settings  --->下的
    General Configuration  --->                 
    Build Options  --->        
    Debugging Options  --->       
    Installation Options  --->          
    Busybox Library Tuning  ---> 

这几项都稍微看下,决定对你的有需要的就选山;比如补全命令,查看历史命令等有用的都选上。
6,编译安装我们的busybox
@ubuntu:~/exp/mkrootfs$make
@ubuntu:~/exp/mkrootfs$make install
这样我们可以得到我们的一个编译结果@ubuntu:~/exp/mkrootfs/busybox-1.14.2/_install$下的bin,sbin,linuxrc等。
第二步:完成第一步也就说明我们已经完成了一半任务,现在制作我们的根文件系统正式拉开帷幕:
1,建立一个根文件系统目录。可以利用下面这个脚本快速建立一些默认的文件系统框架。
@ubuntu:~/exp/rootfs$vim helprootfs.sh 
 

#!/bin/sh
echo "------Create rootfs directons......"
mkdir rootfs
cd rootfs
echo "--------Create root,dev......"
mkdir root dev etc bin sbin mnt sys proc lib home tmp var usr
mkdir usr/sbin usr/bin usr/lib usr/modules
mkdir mnt/usb mnt/nfs mnt/etc mnt/etc/init.d
mkdir lib/modules
chmod 1777 tmp
cd ..
echo "-------make direction done---------"


保存退出。
更改可执行权限
@ubuntu:~/exp$ chmod +x helprootfs.sh 
@ubuntu:~/exp$ ./helprootfs.sh 
------Create rootfs directons......
--------Create root,dev......
-------make direction done---------

并将刚才我们编译的busybox生成的_install目录下的文件拷贝至此:
@ubuntu:~/exp/rootfs$ cp ../mkrootfs/busybox-1.14.2/_install/* -a ./
这样就建好了一个根文件系统的大致框架。
2、创建设备文件
mdev 是通过 init 进程来启动的,在使用 mdev 构造 /dev 目录之前,init 至少要用到设备文件/dev/console、 /dev/null ,所以需要事先建立这两个设备文件:
@ubuntu:~/exp/rootfs$ cd dev/
@ubuntu:~/exp/rootfs/dev$ sudo mknod -m 660 console c 204 64
@ubuntu:~/exp/rootfs/dev$ sudo mknod -m 660 null c 1 3
@ubuntu:~/exp/rootfs/dev$ ls -l
total 0
crw-rw---- 1 root root 204, 64 2011-04-04 10:16 console
crw-rw---- 1 root root   1,  3 2011-04-04 10:16 null

3,准备一些配置文件和系统启动时所需的文件,这些文件的作用可以查看:(看好所在的目录)
(1)在mnt/etc下添加mdev。conf;
@ubuntu:~/exp/rootfs/mnt/etc$ touch mdev.conf
(2)在rootfs下添加linurc,rootfs下不要busybox下的linuxrs;
@ubuntu:~/exp/rootfs$ vim linuxrc
将下面内容写进去:
 

#!/bin/sh
echo "Processing /linuxrc"
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"
/bin/mount -f -t cramfs -o remount,ro /dev/bon/3 /
/bin/mount -f -t ramfs ramfs /etc
echo "start init"
exec /sbin/init


(3)在mnt/etc/init.d下添加rcS;
@ubuntu:~/exp/rootfs$ cd mnt/etc/init.d/
@ubuntu:~/exp/rootfs/mnt/etc/init.d$ vim rcS
将下面内容写进去
 

#!/bin/sh
echo "Processing /etc/init.d/rcS"
echo "mount -a"
mount -a #mount上fstab文件中所有文件系统
exec /etc/rc.local


(4)在mnt/etc下添加rc.local文件;
@ubuntu:~/exp/rootfs/mnt/etc$ vim rc.local
添加下面的内容:
 

#!/bin/sh
echo "Processing /etc/rc.local"
echo "get hostname"
/bin/hostname -F /etc/hostname
echo "Starting mdev"
echo /sbin/mdev > /proc/sys/kernel/hotplug
mdev -s
echo "ifconfig eth0 192.168.1.21"
ifconfig eth0 192.168.1.21
echo "**************************************************"
echo "*                                                *"
echo "*        Linux ubuntu 2.6.32-30-generic          *"
echo "*                                                *"
echo "*           arm-linux-gnueabi-gcc 4.4.5          *"
echo "*                                                *"
echo "*                 2011-04-04                     *"
echo "*                                                *"
echo "**************************************************"


(5)在mnt/etc下添加profile文件:
@ubuntu:~/exp/rootfs/mnt/etc$ vim profile
添加下面内容:
 

#/etc/profile
echo "Processing /etc/profile"
echo "set user path"
PATH=/bin:/sbin:/usr/bin:/usr/sbin
echo "set search library path"
LD_LIBRARY_PATH=/lib:/usr/lib
echo "set PS1"
HOSTNAME=`/bin/hostname`
PS1='\u@\h:\w\$ ' #设置命令提示符为ubuntu风格
export PATH LD_LIBRARY_PATH HOSTNAME PS1


(6)改变权限;
@ubuntu:~/exp/rootfs$ chmod 775 linuxrc mnt/etc/init.d/rcS mnt/etc/rc.local mnt/etc/profile
(7)在mnt/etc下添加inittab文件:
@ubuntu:~/exp/rootfs/mnt/etc$ vim inittab
添加下面内容:
 

#/etc/inittab
::sysinit:/etc/init.d/rcS
console::askfirst:-/bin/sh    
::ctrlaltdel:/sbin/reboot
::shutdown:/bin/umount -a -r


(8)在mnt/etc下添加fstab文件:
@ubuntu:~/exp/rootfs/mnt/etc$ vim fstab
添加下面内容:
 

#/etc/fstab: static file system information.
#<File system> <mount pt>     <type>   <options>         <dump> <pass>
proc  /proc proc  defaults 0 0
sysfs /sys  sysfs defaults 0 0
mdev  /dev  ramfs defaults 0 0
none  /tmp  ramfs defaults 0 0


(9)在/etc下添加passwd文件:
@ubuntu:~/exp/rootfs/etc$ vim passwd
添加下面内容:

#username:password:User ID:Group ID:comment:home directory:shell
root:x:0:0:root:/root:/bin/sh


(10)lib库文件复制到rootfs/lib目录下(根据需要复制)此处暂且不写了。
第三步:生成CramFS文件系统镜像文件myrootfs.img
1,下载CramFS制作工具,地址
2,解压编译(位子随便,最好在有别于rootf这个目录)
@ubuntu:~/tools$ tar xvzf cramfs-1.1.tar.gz
@ubuntu:~/tools$ make 
这样就生成了我们需要的工具拉,你可以将cramfsck和mkcramfs添加到你的bin下,也可以不。
3,最后一步制作我们的镜像;
@ubuntu:~/exp$ ~/tools/cramfs-1.1/mkcramfs rootfs/ myrootfs.img
Directory data: 6424 bytes
Everything: 572 kilobytes
Super block: 76 bytes
CRC: 1245ce56
warning: gids truncated to 8 bits (this may be a security concern)

4,goodluck!如果遇到你的CramFS没有办法make,可以在互联网上搜到解决方法,其他请跟贴。

 

 

make menuconfig时候出错处理

HOSTCC scripts/basic/fixdep
HOSTCC scripts/basic/split-include
scripts/basic/split-include.c: In function ‘main’:
scripts/basic/split-include.c:133: warning: ignoring return value of ‘fgets’, declared with attribute warn_unused_result
HOSTCC scripts/basic/docproc
HOSTCC scripts/kconfig/conf.o
scripts/kconfig/conf.c: In function ‘conf_askvalue’:
scripts/kconfig/conf.c:104: warning: ignoring return value of ‘fgets’, declared with attribute warn_unused_result
scripts/kconfig/conf.c: In function ‘conf_choice’:
scripts/kconfig/conf.c:359: warning: ignoring return value of ‘fgets’, declared with attribute warn_unused_result
HOSTCC scripts/kconfig/kxgettext.o
HOSTCC scripts/kconfig/mconf.o
scripts/kconfig/mconf.c: In function ‘exec_conf’:
scripts/kconfig/mconf.c:470: warning: ignoring return value of ‘pipe’, declared with attribute warn_unused_result
scripts/kconfig/mconf.c: In function ‘show_textbox’:
scripts/kconfig/mconf.c:836: warning: ignoring return value of ‘write’, declared with attribute warn_unused_result
HOSTCC scripts/kconfig/zconf.tab.o
HOSTLD scripts/kconfig/mconf
HOSTCC scripts/kconfig/lxdialog/checklist.o
In file included from scripts/kconfig/lxdialog/checklist.c:24:
scripts/kconfig/lxdialog/dialog.h:31:20: error: curses.h: 没有那个文件或目录
In file included from scripts/kconfig/lxdialog/checklist.c:24:
scripts/kconfig/lxdialog/dialog.h:128: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘use_colors’
scripts/kconfig/lxdialog/dialog.h:129: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘use_shadow’
scripts/kconfig/lxdialog/dialog.h:131: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘attributes’
scripts/kconfig/lxdialog/dialog.h:143: error: expected ‘)’ before ‘*’ token
scripts/kconfig/lxdialog/dialog.h:146: error: expected ‘)’ before ‘*’ token
scripts/kconfig/lxdialog/dialog.h:147: error: expected ‘)’ before ‘*’ token
scripts/kconfig/lxdialog/dialog.h:148: error: expected ‘)’ before ‘*’ token
scripts/kconfig/lxdialog/dialog.h:149: error: expected ‘)’ before ‘*’ token
scripts/kconfig/lxdialog/dialog.h:151: error: expected ‘)’ before ‘*’ token
scripts/kconfig/lxdialog/checklist.c:31: error: expected ‘)’ before ‘*’ token
scripts/kconfig/lxdialog/checklist.c:59: error: expected ‘)’ before ‘*’ token
scripts/kconfig/lxdialog/checklist.c:95: error: expected ‘)’ before ‘*’ token
scripts/kconfig/lxdialog/checklist.c: In function ‘dialog_checklist’:
scripts/kconfig/lxdialog/checklist.c:117: error: ‘WINDOW’ undeclared (first use in this function)
scripts/kconfig/lxdialog/checklist.c:117: error: (Each undeclared identifier is reported only once
scripts/kconfig/lxdialog/checklist.c:117: error: for each function it appears in.)
scripts/kconfig/lxdialog/checklist.c:117: error: ‘dialog’ undeclared (first use in this function)
scripts/kconfig/lxdialog/checklist.c:117: error: ‘list’ undeclared (first use in this function)
scripts/kconfig/lxdialog/checklist.c:117: warning: left-hand operand of comma expression has no effect
scripts/kconfig/lxdialog/checklist.c:121: warning: implicit declaration of function ‘endwin’
scripts/kconfig/lxdialog/checklist.c:122: warning: implicit declaration of function ‘fprintf’
scripts/kconfig/lxdialog/checklist.c:122: warning: incompatible implicit declaration of built-in function ‘fprintf’
scripts/kconfig/lxdialog/checklist.c:122: error: ‘stderr’ undeclared (first use in this function)
scripts/kconfig/lxdialog/checklist.c:140: error: ‘COLS’ undeclared (first use in this function)
scripts/kconfig/lxdialog/checklist.c:141: error: ‘LINES’ undeclared (first use in this function)
scripts/kconfig/lxdialog/checklist.c:143: warning: implicit declaration of function ‘draw_shadow’
scripts/kconfig/lxdialog/checklist.c:143: error: ‘stdscr’ undeclared (first use in this function)
scripts/kconfig/lxdialog/checklist.c:145: warning: implicit declaration of function ‘newwin’
scripts/kconfig/lxdialog/checklist.c:146: warning: implicit declaration of function ‘keypad’
scripts/kconfig/lxdialog/checklist.c:146: error: ‘TRUE’ undeclared (first use in this function)
scripts/kconfig/lxdialog/checklist.c:148: warning: implicit declaration of function ‘draw_box’
scripts/kconfig/lxdialog/checklist.c:148: error: ‘attributes’ undeclared (first use in this function)
scripts/kconfig/lxdialog/checklist.c:149: warning: implicit declaration of function ‘wattrset’
scripts/kconfig/lxdialog/checklist.c:150: warning: implicit declaration of function ‘mvwaddch’
scripts/kconfig/lxdialog/checklist.c:152: warning: implicit declaration of function ‘waddch’
scripts/kconfig/lxdialog/checklist.c:156: warning: implicit declaration of function ‘print_title’
scripts/kconfig/lxdialog/checklist.c:159: warning: implicit declaration of function ‘print_autowrap’
scripts/kconfig/lxdialog/checklist.c:166: warning: implicit declaration of function ‘subwin’
scripts/kconfig/lxdialog/checklist.c:190: warning: implicit declaration of function ‘print_item’
scripts/kconfig/lxdialog/checklist.c:194: warning: implicit declaration of function ‘print_arrows’
scripts/kconfig/lxdialog/checklist.c:197: warning: implicit declaration of function ‘print_buttons’
scripts/kconfig/lxdialog/checklist.c:199: warning: implicit declaration of function ‘wnoutrefresh’
scripts/kconfig/lxdialog/checklist.c:201: warning: implicit declaration of function ‘doupdate’
scripts/kconfig/lxdialog/checklist.c:204: warning: implicit declaration of function ‘wgetch’
scripts/kconfig/lxdialog/checklist.c:211: error: ‘KEY_UP’ undeclared (first use in this function)
scripts/kconfig/lxdialog/checklist.c:211: error: ‘KEY_DOWN’ undeclared (first use in this function)
scripts/kconfig/lxdialog/checklist.c:221: error: ‘FALSE’ undeclared (first use in this function)
scripts/kconfig/lxdialog/checklist.c:222: warning: implicit declaration of function ‘scrollok’
scripts/kconfig/lxdialog/checklist.c:223: warning: implicit declaration of function ‘wscrl’
scripts/kconfig/lxdialog/checklist.c:232: warning: implicit declaration of function ‘wrefresh’
scripts/kconfig/lxdialog/checklist.c:282: warning: incompatible implicit declaration of built-in function ‘fprintf’
scripts/kconfig/lxdialog/checklist.c:283: warning: implicit declaration of function ‘delwin’
scripts/kconfig/lxdialog/checklist.c:287: error: ‘KEY_LEFT’ undeclared (first use in this function)
scripts/kconfig/lxdialog/checklist.c:288: error: ‘KEY_RIGHT’ undeclared (first use in this function)
make[2]: *** [scripts/kconfig/lxdialog/checklist.o] 错误 1
make[1]: *** [menuconfig] 错误 2
make: *** [menuconfig] 错误 2
@ubuntu:~/exp/mkrootfs$
解决办法:ubuntu系统中缺少一个套件 ncurses devel ,把此套件安装下即可
@ubuntu:~/exp/mkrootfs$ sudo apt-get install libncurses5-dev

 

 

 

http://www.ourunix.org/post/63.html