busybox制作根文件系统.docx
《busybox制作根文件系统.docx》由会员分享,可在线阅读,更多相关《busybox制作根文件系统.docx(21页珍藏版)》请在冰豆网上搜索。
![busybox制作根文件系统.docx](https://file1.bdocx.com/fileroot1/2022-12/30/49d8e911-62db-48f6-ac35-163e39726baa/49d8e911-62db-48f6-ac35-163e39726baa1.gif)
busybox制作根文件系统
操作系统:
Ubuntu9.04
内核版本:
linux-2.6.24.7
开发板:
博创arm2410s
交叉编译工具:
arm-linux-gcc-4.1.1
BusyBox:
busybox-1.11.1
CramFS:
CramFS-1.1
注意:
由于要制作的根文件系统使用了mdev(BusyBox简化版的udev),因此,需要内核支持sysfs、procfs和ramfs(tmpfs)文件系统以及hotplug(uevent)事件机制。
浅蓝色为命令,橘红色为代码和脚步
1、准备根文件系统
使用shell脚本create_rootfs.sh,建立根文件系统的目录框架
lingd@ubuntu:
~/arm2410s$vicreate_rootfs.sh
create_rootfs.sh内容如下:
#!
/bin/sh
echo"------Createrootfsdirectons......"
mkdirrootfs
cdrootfs
echo"--------Createroot,dev......"
mkdirrootdevetcbinsbinmntsysproclibhometmpvarusr
mkdirusr/sbinusr/binusr/libusr/modules
mkdirmnt/usbmnt/nfsmnt/etcmnt/etc/init.d
mkdirlib/modules
chmod1777tmp
cd..
echo"-------makedirectiondone---------"
为create_rootfs.sh添加执行权限
lingd@ubuntu:
~/arm2410s$chmoda+xcreate_rootfs.sh
执行create_rootfs.sh,建立根文件系统的目录框架
lingd@ubuntu:
~/arm2410s$./create_rootfs.sh
lingd@ubuntu:
~/arm2410s$cdrootfs
lingd@ubuntu:
~/arm2410s/rootfs$ls
bin dev etc home lib mnt proc root sbin sys tmp usr var
2、创建设备文件
mdev是通过init进程来启动的,在使用mdev构造/dev目录之前,init至少要用到设备文件/dev/console、/dev/null ,所以需要事先建立这两个设备文件:
lingd@ubuntu:
~/arm2410s/rootfs$cddev
lingd@ubuntu:
~/arm2410s/rootfs/dev$sudomknod-m660consolec20464
[sudo]passwordforlingd:
lingd@ubuntu:
~/arm2410s/rootfs/dev$sudomknod-m660nullc13
lingd@ubuntu:
~/arm2410s/rootfs/dev$ls-l
total0
crw-rw----1rootroot5,12010-04-0215:
49console
crw-rw----1rootroot1,32010-04-0215:
50null
注意:
本来console的主次设备号应为5和1.但是因为init在执行完/etc/init.d/rcS脚本后,就会在一个控制台上,按照inittab的指示开一个shell(或者是开getty+login,这样用户就会看到提示输入用户名的提示符)。
在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.conf
mdev会在/etc目录下找mdev的配置文件:
mdev.conf.如果该文件不存在,那么在执行mdev–s这个命令时,会提示找不到mdev.conf。
我们不需要mdev规则,所以只是touch生成一个空文件就OK了。
当然也可以根据mdev的规则来编写mdev.conf。
我把所有配置文件都是在/mnt/etc下,而不是/etc,后面解释这么做的原因。
lingd@ubuntu:
~/arm2410s/rootfs/dev$cd../mnt/etc
lingd@ubuntu:
~/arm2410s/rootfs/mnt/etc$touchmdev.conf
lingd@ubuntu:
~/arm2410s/rootfs/mnt/etc$ls
init.d mdev.conf
3.2、linuxrc
linuxrc位于根文件系统的顶层目录/,这里为rootfs。
/Linuxrc执行init进程初始化文件。
主要工作是把已安装根文件系统中的/etc安装为ramfs,并拷贝/mnt/etc/目录下所有文件到/etc,这里存放系统启动后的许多特殊文件;接着Linuxrc重新构建文件分配表inittab;之后执行系统初始化进程/sbin/init。
lingd@ubuntu:
~/arm2410s/rootfs/mnt/etc$cd../..
lingd@ubuntu:
~/arm2410s/rootfs$vilinuxrc
linuxrc内容如下:
#!
/bin/sh
echo"Processing/linuxrc"
echo"mount/etcasramfs"
/bin/mount-n-tramfsramfs/etc
/bin/cp-a/mnt/etc/*/etc
echo"re-createthe/etc/mtabentries"
/bin/mount-f-tcramfs-oremount,ro/dev/bon/3/
/bin/mount-f-tramfsramfs/etc
echo"startinit"
exec/sbin/init
lingd@ubuntu:
~/arm2410s/rootfs$ls
bin dev etc home lib linuxrc mnt proc root sbin sys tmp usr var
Linuxrc脚本分析
/bin/mount-n-tramfsramfs/etc
这句话的作用加载一个ramfs作为/etc目录。
这样/etc就是一个可写目录。
从这个脚本可知,你的根文件系统是一个cramfs(只读文件系统),而/etc作为系统运行配置文件的存放地点,可能会写一些运行状态在这里,linuxrc第一件事情就是将一个ramfsmount到/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-tcramfs-oremount,ro/dev/bon/3/
/bin/mount-f-tramfsramfs/etc
这些命令只是将这些mount信息写到/etc/mtab中,不会实际去mount这些blockdevice,说明你的根文件系统依然是以前的那个/dev/bon/3
exec/sbin/init
执行根文件系统中的init执行程序,使其成为1号进程。
shell正式运行。
3.3、rcS
rcS文件位于/etc/init.d,是busybox版init第一个运行的脚步(常见的init还有SysVinit版,其第一个执行的脚步是/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文件内容如下:
lingd@ubuntu:
~/arm2410s/rootfs$cdmnt/etc/init.d
lingd@ubuntu:
~/arm2410s/rootfs/mnt/etc/init.d$vircS
/etc/init.d/rcS内容如下:
#!
/bin/sh
echo"Processing/etc/init.d/rcS"
echo"mount-a"
mount-a#mount上fstab文件中所有文件系统
exec/etc/rc.local
3.4、/etc/rc.local
/etc/rc.local是被init.d/rcS文件调用执行的特殊文件,与Linux系统硬件平台相关,如安装核心模块、进行网络配置、运行应用程序、启动图形界面等。
内容如下:
lingd@ubuntu:
~/arm2410s/rootfs/mnt/etc/init.d$cd..
lingd@ubuntu:
~/arm2410s/rootfs/mnt/etc$virc.local
/etc/rc.local内容如下:
#!
/bin/sh
echo"Processing/etc/rc.local"
echo"gethostname"
/bin/hostname-F/etc/hostname
echo"Startingmdev"
echo/sbin/mdev>/proc/sys/kernel/hotplug
mdev-s
echo"ifconfigeth0192.168.1.21"
ifconfigeth0192.168.1.21
echo"**************************************************"
echo"* *"
echo"* lingd rootfsforlinux2.6.24.7 *"
echo"* *"
echo"* arm-linux-gccversion4.1.1 *"
echo"* *"
echo"* 2010-03-30 *"
echo"* *"
echo"**************************************************"
lingd@ubuntu:
~/arm2410s/rootfs/mnt/etc$ls
init.d rc.local
在rc.local使用了"/bin/hostname-F/etc/hostname"来设置主机名(设置主机名主要是为了后面设置命令提示符PS1)。
这条命令需要了一个主机名配置文件/etc/hostname,其内容如下:
arm2410s
3.5、/etc/profile
rc.local首先执行该文件配置应用程序需要的环境变量等。
lingd@ubuntu:
~/arm2410s/rootfs/mnt/etc$viprofile
/etc/profile内容如下:
#/etc/profile
echo"Processing/etc/profile"
echo"setuserpath"
PATH=/bin:
/sbin:
/usr/bin:
/usr/sbin
echo"setsearchlibrarypath"
LD_LIBRARY_PATH=/lib:
/usr/lib
echo"setPS1"
HOSTNAME=`/bin/hostname`
PS1='\u@\h:
\w\$'#设置命令提示符为ubuntu风格
exportPATHLD_LIBRARY_PATHHOSTNAMEPS1
lingd@ubuntu:
~/arm2410s/rootfs/mnt/etc$ls
init.d profile rc.local
改变这四个文件的权限
lingd@ubuntu:
~/arm2410s/rootfs/mnt/etc$cd../..
lingd@ubuntu:
~/arm2410s/rootfs$chmod775linuxrcmnt/etc/init.d/rcSmnt/etc/rc.localmnt/etc/profile
3.6、/etc/inittab
内核引导完成后,内核会启动初始化进程init(用户级进程)来进行系统的各项配置。
init是系统第一个进程,它是系统上运行的所有其他进程的父进程,他会观察其子进程,并在需要时启动、停止、重启它们。
init主要是用来完成系统的各项配置。
init从/etc/inittab获取所有信息。
想了解BusyBoxinit及其inittab基本原理的,可以看这篇文章cdmnt/etc
lingd@ubuntu:
~/arm2410s/rootfs/mnt/etc$viinittab
/etc/inittab内容如下:
#/etc/inittab
:
:
sysinit:
/etc/init.d/rcS
console:
:
askfirst:
-/bin/sh
:
:
ctrlaltdel:
/sbin/reboot
:
:
shutdown:
/bin/umount-a-r
"console:
:
askfirst:
-/bin/sh"中的-表示的是让busybox开启一个登录(login)shell,loginshell在执行前都会读取配置文件/etc/profile和.profile。
由BusyBox源码的shell/ash.c文件可知这一点:
intash_main(intargcATTRIBUTE_UNUSED,char**argv)
{
…………
if(argv[0]&&argv[0][0]=='-')
isloginsh=1;
if(isloginsh){
state=1;
read_profile("/etc/profile");
state1:
state=2;
read_profile(".profile");
…………
}
因为我们在/etc/profile对PATH、LD_LIBRARY_PATH、HOSTNAME、PS1等环境变量进行了修改,所以BusyBox开启的必须是一个loginshell(这样可以保证/profile的内容对开发板上所有shell都是有效的);否则/etc/profile定义的内容将不会执行。
做个小实验(以这次做好的根文件系统为基础):
PleasepressEntertoactivatethisconsole. #启动开发板,引导linux内核并进行各系统配置后,执行到这里。
按下回车键,显示以下内容:
startingpid797,tty'/dev/console':
'-/bin/sh' #在控制台/dev/console上开启一个loginshell
Processing/etc/profile #执行/etc/profile配置文件
setuserpath
setsearchlibrarypath
setPS1
root@arm2410s:
/#
root@arm2410s:
/#exit #退出当前shell
PleasepressEntertoactivatethisconsole. #再按回车开启一个新的loginshell
startingpid799,tty'/dev/console':
'-/bin/sh'
Processing/etc/profile #再次执行/etc/profile配置文件
setuserpath
setsearchlibrarypath
setPS1
lingd@ubuntu:
~/arm2410s/rootfs/mnt/etc$ls
init.d inittab profile rc.local
3.7、/etc/fstab
文件/etc/fstab存放的是系统中的文件系统信息。
当正确的设置了该文件,则可以通过"mount/directoryname"命令来加载一个文件系统,或mount-a来加载该文件中所有文件系统。
每种文件系统都对应一个独立的行,每行中的字段都有空格或tab键分开。
同时fsck、mount、umount的等命令都利用该程序。
lingd@ubuntu:
~/arm2410s/rootfs/mnt/etc$vifstab
fstab文件内容如下:
#/etc/fstab:
staticfilesysteminformation.
#
proc /procproc defaults00
sysfs/sys sysfsdefaults00
mdev /dev ramfsdefaults00
none /tmp ramfsdefaults00
注意:
已单独mount了的文件系统,就不要出现在/etc/fstab文件中,以免使用mount-a时把先前已经mount上的文件系统被覆盖了。
3.8、/etc/passwd
/etc/passwd文件存放着所有用户的信息,包括账号和密码。
内容如下:
#username:
password:
UserID:
GroupID:
comment:
homedirectory:
shell
root:
x:
0:
0:
root:
/root:
/bin/sh
4、编译busybox
BusyBox下载地址:
lingd@ubuntu:
~/arm2410s$tarxjvfbusy-1.11.1.tar.bz2
lingd@ubuntu:
~/arm2410s$cdbusybox-1.11.1
lingd@ubuntu:
~/arm2410s/busybox-1.11.1$viMakefile
首先修改Makefile,将以下两项改为
CROSS_COMPILE=arm-linux-
ARCH =arm
配置busybox,修改以下选项(其他选项默认就可以了,或者根据需要再裁减一下):
lingd@ubuntu:
~/arm2410s/busybox-1.11.1$makemenuconfig