【ACCESS精品源码栏目提醒】:网学会员ACCESS精品源码为您提供使用BusyBox制作linux根文件系统 - 电子电信参考,解决您在使用BusyBox制作linux根文件系统 - 电子电信学习中工作中的难题,参考学习。
操作系统:Ubuntu9.04内核版本:linux-2.6.24.7开发板:博创 arm2410s交叉编译工具:arm-linux-gcc-4.1.1BusyBox:busybox-1.11.1CramFS:CramFS-1.1注意:由于要制作的根文件系统使用 mdevBusyBox 简化版的 udev),因此,需要内 核支持 sysfs、procfs 和 ramfs(tmpfs)文件系统以及 hotplug(uevent)事件机制。
浅蓝色为命,橘红色为 代码和脚步1、准备根文件系统使用 shell 脚本 create_rootfs.sh,建根文件系统的目录框架lingdubuntu:/arm2410s vi create_rootfs.shcreate_rootfs.sh 内容如下:/bin/shecho quot------Create rootfs directons......quotmkdir rootfscd rootfsecho quot--------Create rootdev......quotmkdir root dev etc bin sbin mnt sys proc lib home tmp var usrmkdir usr/sbin usr/bin usr/lib usr/modulesmkdir mnt/usb mnt/nfs mnt/etc mnt/etc/init.dmkdir lib/moduleschmod 1777 tmpcd ..echo quot-------make direction done---------quot为 create_rootfs.sh 添加执权限lingdubuntu:/arm2410s chmod ax create_rootfs.sh执 create_rootfs.sh,建根文件系统的目录框架lingdubuntu:/arm2410s ./create_rootfs.shlingdubuntu:/arm2410s cd rootfslingdubuntu:/arm2410s/rootfs lsbin dev etc home lib mnt proc root sbin sys tmp usr var2、创建设备 文件mdev 是通过 init 进程来启动的,在使用 mdev 构造 /dev 目录之前,init 至少要用到设备文件/dev/console、 /dev/null ,所以需要事先建这两个设备文件:lingdubuntu:/arm2410s/rootfs cd devlingdubuntu:/arm2410s/rootfs/dev sudo mknod -m 660 console c 204 64sudo password for lingd:lingdubuntu:/arm2410s/rootfs/dev sudo mknod -m 660 null c 1 3lingdubuntu:/arm2410s/rootfs/dev ls -ltotal 0crw-rw---- 1 root root 5 1 2010-04-02 15:49 consolecrw-rw---- 1 root root 1 3 2010-04-02 15:50 null注意:本来 console 的主次设备号应为 5 和 1.但是因为 init 在执完/etc/init.d/rcS脚本后,就 会在一个控制台上,按照 inittab 的指示开一个 shell(或者是开gettylogin,这样用户就会看到提示输入用户名的提示符)。
在 mdev -s 未执之前,/dev 目录下只有我们创建的/dev/null 和/dev/console,也就是说,没有控制台可供 init 用来按照 inittab 的指示开一个 shell。
而在 s3c24xx 系芯片的口驱动面用 s3c2410_serial 做为设备名 (在内核
源码的“drivers/serial/s3c2410.c”文件的 949 ) ,因此,启动时可用 s3c2410_serial0、s3c2410_serial1 或 s3c2410_serial2作为 init 用来按照 inittab 的指示开一个 shell 的控制台,这我用口 0,即s3c2410_serial0(主次设备号为 204 和 64)作为控制台。
这直接 将 console 当s3c2410_serial0,所以 console 的主次设备号才会为 204 和 64。
3、准备一些配置文件和系统启动时所需的文件3.1、mdev 配置文件 mdev.confmdev 会在/etc 目录下找 mdev 的配置文件: mdev.conf. 如果该文件存在,那么在执 mdev –s 这个命时,会提示找到 mdev.conf。
我们需要 mdev 规则,所以只是 touch 生成一个空文件就 OK 。
当然也可以根据 mdev 的规则来 编写mdev.conf。
我把所有配置文件都是在/mnt/etc 下,而是/etc,后面解释这么做的原因。
lingdubuntu:/arm2410s/rootfs/dev cd ../mnt/etclingdubuntu:/arm2410s/rootfs/mnt/etc touch mdev.conflingdubuntu:/arm2410s/rootfs/mnt/etc lsinit.d mdev.conf3.2、linuxrclinuxrc 位于根文件系统的顶层目录/,这为 rootfs。
/Linuxrc 执 init 进程初始化文件。
主要工作是把已安装根文件系统中的/etc 安装为 ramfs,并拷贝/mnt/etc/目录下所有文件到/etc,这存放系统启动后的许多特殊文件;接着 Linuxrc 重新构建文件分配表 inittab;之后执系统初始化进程/sbin/init。
lingdubuntu:/arm2410s/rootfs/mnt/etc cd ../..lingdubuntu:/arm2410s/rootfs vi linuxrclinuxrc 内容如下:/bin/shecho quotProcessing /linuxrcquotecho quotmount /etc as ramfsquot/bin/mount -n -t ramfs ramfs /etc/bin/cp -a /mnt/etc/ /etcecho quotre-create the /etc/mtab entriesquot/bin/mount -f -t cramfs -o remountro /dev/bon/3 //bin/mount -f -t ramfs ramfs /etcecho quotstart initquotexec /sbin/initlingdubuntu:/arm2410s/rootfs lsbin dev etc home lib linuxrc mnt proc root sbin sys tmp usr varLinuxrc 脚本分析/bin/mount -n -t ramfs ramfs /etc这话的作用加载一个 ramfs 作为/etc 目录。
这样 /etc 就是一个可写目录。
从这个脚本可知,你的根文件系统是一个 cramfs(只读文件系统),而/etc 作为系统运配置文件的存放地点,可 能会写一些运状态在这,linuxrc 第一件事情就是将一个 ramfs mount 到/etc 只读目录中,使得/etc/目录可写,指定参数 -n的目的是告诉 mount 要写/etc/mtab 这个文件存放当前系统已挂载(mount)的文件系统清单。
因为现在/etc/目录还是只读,所以这次 mount 要写这个文件,否则会失败。
而且 mount 上后, 原/etc 会覆盖掉(原/etc 下的文件都见,umount后会重新出现),所以我们把配置文件都保存在/mnt /etc,mount 上 ramfs 到/etc后,再把配置文件拷贝到/etc。
而是直接将配置文件保存在/etc/下/bin/cp -a /mnt/etc/ /etc/etc 成为可写目录后,将所有/mnt/etc 中的配置文件拷贝到/etc/中,这说明 ramfs可能 是一个空的 ramfs,没有配置文件,或者配置文件比较。
同时也说明这个系统是一个只读系统,每次系统运中写入的配置会保。
将以前 mount 的那些信息重新写到/etc/mtab 中,命就是下面这些。
/bin/mount -f -t cramfs -o remountro /dev/bon/3 //bin/mount -f -t ramfs ramfs /etc这些命只是将这 些 mount 信息写到/etc/mtab 中,会实际去 mount 这些 blockdevice,说明你的根文件系统依然是以前的那个/dev/bon/3exec /sbin/init执根文件系统中的 init 执 程序,使其成为 1 号进程。
shell 正式运。
3.3、rcSrcS 文件位于/etc/init.d是 busybox 版 init 第一个运的脚步 (常见的 init 还有 Sys Vinit 版,其第一个执的脚步是/etc/rc.d/rc.sysinit。
/mnt/etc/init.d/rcS 完成各个文件系统的 mount,再执/mnt/etc/rc.local;通过 rcS 可以调用ifconfig 程序配置网络。
rcS 执完以后,init 就会在一个 console 上,按照 inittab 的指示开一个shell,或者是开 getty login,这样用户就会看到提示输入用户名的提示符。
/etc/init.d/rcS 文件内容如下:lingdubuntu:/arm2410s/rootfs cd mnt/etc/init.dlingdubuntu:/arm2410s/rootfs/mnt/etc/init.d vi rcS/etc/init.d/rcS 内容如下:/bin/shecho quotProcessing /etc/init.d/rcSquotecho quotmount -aquotmount -a mount 上 fstab 文件中所有文件系统exec /etc/rc.local3.4、/etc/rc.local/etc/rc.local 是被 init.d/rcS 文件调用执的特殊文件,与 Linux 系统硬件平台相关,如安装核心模块、进网络配置、运应用程序、启动图形界面等。
内容如下:lingdubuntu:/arm2410s/rootfs/mnt/etc/init.d cd ..lingdubuntu:/arm2410s/rootfs/mnt/etc vi rc.local/etc/rc.local 内容如下:/bin/shecho quotProcessing /etc/rc.localquotecho quotget hostnamequot/bin/hostname -F /etc/hostnameecho quotStarting mdevquotecho /sbin/mdev gt /proc/sys/kernel/hotplugmdev -secho quotifconfig eth0 192.168.1.21quotifconfig eth0 192.168.1.21echo quotquotecho quot quotecho quot lingd rootfs for linux 2.6.24.7 quotecho quot quotecho quot arm-linux-gcc version 4.1.1 quotecho quot quotecho quot 2010-03-30 quotecho quot quotecho quotquotlingdubuntu:/arm2410s/rootfs/mnt/etc lsinit.d rc.local在 rc.local 使用quot/bin/hostname -F /etc/hostnamequot来设置主机名设置主机名主要是为后面设置命提示符 PS1。
这条命需要一个主机名配置文件/etc/hostname,其内容如下:arm2410s3.5、/etc/profilerc.local 首先执该文件配置应用程序需要的环境变等。
lingdubuntu:/arm2410s/rootfs/mnt/etc vi profile/etc/profile 内容如下:/etc/profileecho quotProcessing /etc/profilequotecho quotset user pathquotPATH/bin:/sbin:/usr/bin:/usr/sbinecho quotset search library pathquotLD_LIBRARY_PATH/lib:/usr/libecho quotset PS1quotHOSTNAME/bin/hostnamePS1uh:w #设置命提示符为 ubuntu 风格export PATH LD_LIBRARY_PATH HOSTNAME PS1lingdubuntu:/arm2410s/rootfs/mnt/etc lsinit.d profile rc.local改变这四个文件的权限lingdubuntu:/arm2410s/rootfs/mnt/etc cd ../..lingdubuntu:/arm2410s/rootfs chmod 775 linuxrc mnt/etc/init.d/rcS mnt/etc/rc.localmnt/etc/profile3.6、/etc/inittab内核引导完成后,内核会启动初始化进程 init(用户级进程)来进 系统的各项配置。
init 是系统第一个进程,它是系统上运的所有其他进程的父进程,他会观察其子进程,并在需要时启动、停止、重启它们。
init 主要是 用来完成系统的各项配置。
init 从/etc/inittab 获取所有信息。
想解 BusyBox init 及其 inittab 基本原的,可以看这篇文章 http://blog.chinaunix.net/u3/109117/showart_2208026.html。
lingdubuntu:/arm2410s/rootfs cd mnt/etclingdubuntu:/arm2410s/rootfs/mnt/etc vi inittab/etc /inittab 内容如下:/etc/inittab::sysinit:/etc/init.d/rcSconsole::askfirst:-/bin/sh::ctrlaltdel:/sbin/reboot::shutdown:/bin/umount -a -rquotconsole::askfirst:-/bin/shquot中 的-表示的是让 busybox 开启一个登录loginshelllogin 。
shell 在执前都会读取配置文件/etc/profile 和.profile 由 BusyBox
源码的 shell/ash.c文件可知 这一点:int ash_mainint argc ATTRIBUTE_UNUSED char argv ………… if argv0 ampamp argv00 - isloginsh 1if isloginsh state 1 read_profilequot/etc/profilequotstate1: state 2 read_profilequot.profilequot …………因为我们在/etc/profile 对 PATH、LD_LIBRARY_PATH、 HOSTNAME、PS1 等环境变进修改所以 BusyBox 开启的必须是一个 login shell(这样可以保证/profile 的内容对开发板上所有 shell 都是有效的);否则/etc/profile 定义的内容将会执。
做 个小实验以这次做好的根文件系统为基础:Please press Enter to activate this console.启动开发板, 引导 linux 内核并进各系统配置后,执到这。
按下回车键,显示以下内 容:starting pid 797 tty /dev/console: -/bin/sh 在控制台/dev/console 上开启一个 loginshellProcessing /etc/profile执/etc/profile 配置文 件set user pathset search library pathset PS1rootarm2410s:/rootarm2410s:/ exit退出当前 shellPlease press Enter to activate this console.再按回车开启一个新的 login shellstarting pid 799 tty /dev/console: -/bin/sh Processing /etc/profile再次执/etc/profile 配置文 件set user pathset search library pathset PS1lingdubuntu:/arm2410s/rootfs/mnt/etc lsinit.d inittab profile rc.local3.7、/etc/fstab文件/etc/fstab 存放的是系统中的文件系统信息。
当正确的设置该 文件,则可以通过quotmount /directorynamequot命来加载一个文件系统,或 mount -a 来加载该文件中所有文件系统。
每种文件系统都对应一个独的,每中的字段都有空格或tab 键分开。
同时 fsck、mount、umount 的等命 都用该程序。
lingdubuntu:/arm2410s/rootfs/mnt/etc vi fstabfstab 文件内容如下:/etc/fstab: static file system information.ltFile systemgt ltmount ptgt lttypegt ltoptionsgt ltdumpgt ltpassgtproc /proc proc defaults 0 0sysfs /sys sysfs defaults 0 0mdev /dev ramfs defaults 0 0none /tmp ramfs defaults 0 0注意: 已单独 mount 的文件系统,就要出现在/etc/fstab 文件中,以免使用mount -a 时把先前已经 mount 上的文件系统被覆盖。
3.8、/etc/passwd/etc/passwd 文件存放着所有用户的信息,包括账号和密码。
内容如下:username:password:User ID:Group ID:comment:home directory:shellroot:x:0:0:root:/root:/bin/sh4、编译 busyboxBusyBox 下载地址:http://www.busybox.net/lingdubuntu:/arm2410s tar xjvf busy-1.11.1.tar.bz2lingdubuntu:/arm2410s cd busybox-1.11.1lingdubuntu:/arm2410s/busybox-1.11.1 vi Makefile首先修改 Makefile 将以下两项改为CROSS_COMPILE arm-linux-ARCH arm配置 busybox,修改以下选项(其他选项默认就可 以,或者根据需要再裁减一下):lingdubuntu:/arm2410s/busybox-1.11.1 make menuconfigBusybox Settings ---gtBuild Options ---gt Build BusyBox as a static binary no shared libs Build with Large File Support for accessing files gt 2 GBInstallation Options ---gt Dont use /usr 这项必选选上,否则 BusyBox 默认安装径是/usr,原/usr 下的东西可能会被覆盖 Applets links as soft-links ---gt ./_install BusyBox installation prefixBusybox Library Tuning ---gt Command line editing 1024 Maximum length of input vi-style line editing commands 15 History size History saving Tab completion Username completion Fancy shell prompts这两项必须选上,然 BusyBox 将无法识别环境变 PS1 的参数。
Linux Module Utilities ---gt insmod Enable load map -m option Symbols in load map rmmod lsmod Pretty output for 2.6.x Linux kernels modprobe Multiple options parsing Fancy alias parsing Blacklist support --- Options common to multiple modutils Support tainted module checking with new kernels Support version 2.2.x to 2.4.x Linux kernels Support version 2.6.x Linux kernels /lib/modules Default directory containing modules modules.dep Default name of modules.dep保存退出 Busyboxlingdubuntu:/arm2410s/busybox-1.11.1 makelingdubuntu:/arm2410s/busybox-1.11.1 make install编译 BusyBox 时,可能会出现以下错误:4.1、inotifyd 出错交叉编译 busybox-1.11.1 时,出现以下错误:CC miscutils/inotifyd.omiscutils/inotifyd.c:31:27: linux/inotify.h: No such file or directorymiscutils/inotifyd.c: In function inotifyd_main:miscutils/inotifyd.c:61: error: IN_ALL_EVENTS undeclared first use in this functionmiscutils/inotifyd.c:61: error: Each undeclared identifier is reported only oncemiscutils/inotifyd.c:61: error: for each function it appears in.miscutils/inotifyd.c:129: error: dereferencing pointer to incomplete typemiscutils/inotifyd.c:139: error: dereferencing pointer to incomplete typemiscutils/inotifyd.c:140: error: dereferencing pointer to incomplete typemiscutils/inotifyd.c:140: error: dereferencing pointer to incomplete typemiscutils/inotifyd.c:143: error: invalid application of sizeof to incomplete typeinotify_eventmiscutils/inotifyd.c:143: error: dereferencing pointer to incomplete typemake1: miscutils/inotifyd.o Error 1make: miscutils Error 2网上说这是 busybox 的一个 bug,解决方法:去掉对 inotifyed 的支持,具体步骤如下:# make menuconfigMiscellaneous Utilities ---gt inotifyd还有另一个 bug 是 taskset,也要将它去掉,然编译时又会出错。
Miscellaneous Utilities ---gt taskset4.2、启动时,BusyBox 提示quot/bin/sh:cant
access ttyjob console turn offquot解决方法:shell ---gt Job ConsoleCttyhack4.3、未定义 ARPHRD_INFINIBAND错误信息如下:networking/interface.c:818: error: ARPHRD_INFINIBAND undeclared here not in afunctionmake1: networking/interface.o Error 1make: networking Error 2通过查看内核源代码目录中的“include/linux/ifarp.h”文件可知“ARPHRD_INFINIBAND”的值为“32”。
然后修改“networking/interface.c”文件,在其中添加:define ARPHRD_INFINIBAND 32 / InfiniBand /默认在_install 目录中生成基本的文件系统 包括以下目录或文件 bin、 sbin、linuxrc该目录下包含基本的 shell 命.将编译好的 BusyBox 的_Install 目录下的 bin 和sbin 用 tar 命打包复制到/rootfs 目录,解压后删除打包文件。
lingdubuntu:/arm2410s/busybox-1.11.1 cd _installlingdubuntu:/arm2410s/busybox-1.11.1/_install lsbin linuxrc sbinlingdubuntu:/arm2410s/busybox-1.11.1/_install tar zcvf rootfs.tar.gz bin sbinlingdubuntu:/arm2410s/busybox-1.11.1/_install lsbin linuxrc rootfs.tar.gz sbinlingdubuntu:/arm2410s/busybox-1.11.1/_install mv rootfs.tar.gz ../../rootfslingdubuntu:/arm2410s/busybox-1.11.1/_install lsbin linuxrc sbinlingdubuntu:/arm2410s/busybox-1.11.1/_install cd ../../rootfslingdubuntu:/arm2410s/rootfs lsbin etc lib mnt root sbin tmp vardev home linuxrc proc rootfs.tar.gz sys usrlingdubuntu:/arm2410s/rootfs tar zvxf rootfs.tar.gzlingdubuntu:/arm2410s/rootfs rm rootfs.tar.gz5、 lib 库文件复制到 rootfs/lib 目录下(根据需要复制)lingdubuntu:/arm2410s/rootfs cd/opt/crosstool/gcc-4.1.1-glibc-2.3.2/arm-linux/arm-linux/liblingdubuntu:/opt/crosstool/gcc-4.1.1-glibc-2.3.2/arm-linux/arm-linux/lib cp -dR ld/arm2410s/rootfs/liblingdubuntu:/opt/crosstool/gcc-4.1.1-glibc-2.3.2/arm-linux/arm-linux/lib cp -dRlibc.so /arm2410s/rootfs/liblingdubuntu:/opt/crosstool/gcc-4.1.1-glibc-2.3.2/arm-linux/arm-linux/lib cp -dRlibcrypt /arm2410s/rootfs/liblingdubuntu:/opt/crosstool/gcc-4.1.1-glibc-2.3.2/arm-linux/arm-linux/lib cp -dRlibgcc_s.so /arm2410s/rootfs/liblingdubuntu:/opt/crosstool/gcc-4.1.1-glibc-2.3.2/arm-linux/arm-linux/lib cp -dR libm/arm2410s/rootfs/liblingdubuntu:/opt/crosstool/gcc-4.1.1-glibc-2.3.2/arm-linux/arm-linux/lib cp -dRlibpthread.so /arm2410s/rootfs/lib6、生成 CramFS 文件系统镜像文件 CramFS.img首先从 http://sourceforge.net/projects/cramfs/下载 cramfs-1.1.tar.gzlingdubuntu:/opt/crosstool/gcc-4.1.1-glibc-2.3.2/arm-linux/arm-linux/lib cd /arm2410slingdubuntu:/arm2410s tar zxvf cramfs-1.1.tar.gzlingdubuntu:/arm2410s cd cramfs-1.1lingdubuntu:/arm2410s/cramfs-1.1 lsCOPYING cramfsck.c GNUmakefile linux mkcramfs.c NOTES READMElingdubuntu:/arm2410s/cramfs-1.1 makegcc -W -Wall -O2 -g -I. mkcramfs.c -lz -o mkcramfsgcc -W -Wall -O2 -g -I. cramfsck.c -lz -o cramfsck在 cramfs-1.1 目录下会出现两个 bin 文件 mkcramfs 和 cramfscklingdubuntu:/arm2410s/cramfs-1.1 lsCOPYING cramfsck.c linux mkcramfs.c READMEcramfsck GNUmakefile mkcramfs NOTES把他们 copy 到/usr/bin:lingdubuntu:/arm2410s/cramfs-1.1 sudo cp mkcramfs cramfsck /usr/bin创建根文件系统的 cramfs 镜像lingdubuntu:/arm2410s/cramfs-1.1 mkcramfs ../rootfs CramFS01.imgDirectory data: 6020 bytesEverything: 908 kilobytesSuper block: 76 bytesCRC: 526c6bb5warning: gids truncated to 8 bits t.