基于busybox构建文件系统

0. 前提

        开发机ubuntu上已经搭建好了NFS服务器,《搭建NFS服务器》

1. busybox源码下载和编译

(1)busybox是一个开源项目,各个版本差异不大,版本新旧无所谓。下载busybox可以去linuxidc等镜像网站,也可以去www.busybox.net官方网站下载

(2)修改Makefile

ARCH = arm
CROSS_COMPILE = /usr/local/arm/arm-linu/bin/arm-none-linux-gnueabi-
#如果交叉编译工具链的路径已经添加到了PATH环境变量,也可以不指定全路径

(3)make menuconfig配置(Xshell中也可以进行,如果不行应该是操作权限不够)

        因为我是新装的Ubuntu,所以make menuconfig时会报错如下:

In file included from scripts/kconfig/lxdialog/checklist.c:24:0:
    scripts/kconfig/lxdialog/dialog.h:32:20: fatal error: curses.h: No such file or directory
    compilation terminated.
    make[1]: *** [scripts/kconfig/lxdialog/checklist.o] Error 1
    make: *** [menuconfig] Error 2

        解决办法是Ubuntu需要安装libncurses5-dev:sudo apt-get install libncurses5-dev,然后就可以正常make menuconfig,然后按照以下步骤进行配置

Busybox Settings--->
	Build Options--->
		[*]Build BusyBox as a static binary(no shared libs)
	Busybox Library Tuning--->
	[*]vi-style line editing commands
	[*]Fancy shell prompts
		
Linux Module Utilities--->
	[ ]Simplified modutils
	[*]insmod
	[*]rmmod
	[*]lsmod
	[*]modprobe
	[*]depmod
 	
Linux System Utilities--->[*]mdev
	[*]Support /etc/mdev.conf
	[*]Support subdirs/symlinks
	[*]Support regular expressions substitutions when renaming dev
	[*]Support command execution at device addition/removal
	[*]Support loading of firmwares

(4)make编译

(5)make install安装(其实是执行busybox顶层目录下的install目标),在make install之前通过make menuconfig配置安装路径为/home/sijifan/mine/nfs/X210_rootfs,即把生成的根文件系统放到NFS服务器的工作目录,方便开发板挂载。

Busybox Settings--->
    Installation Options ("make install" behavior)--->
        BusyBox installation prefix

(6)设置uboot的bootargs:

#注意:指定nfsroot目录,后续往文件系统中添加文件都是添加到这个目录
setenv bootargs root=/dev/nfs nfsroot=192.168.1.100:/home/sijifan/mine/nfs/X210_rootfs ip=192.168.1.10:192.168.1.100:192.168.1.1:255.255.255.0::eth0:off  init=/linuxrc console=ttySAC2,115200 

恢复:

setenv bootargs console=ttySAC2,115200 root=/dev/mmcblk0p2 rw init=/linuxrc rootfstype=ext3

(7)配置内核支持nfs文件系统启动

1、配置网络部分,主要是使能CONFIG_IP_PNP以在2中能够看到Root file system on NFS选项
Networking support 
	Networking options 
	TCP/IP networking
          IP: kernel level autoconfiguration
          [*] IP: DHCP support
          [*] IP: BOOTP support
2、配置开启nfs服务
File systems  --->	
	Network File Systems  --->
		<*> NFS client support 
		[*] NFS client support for NFS version 3                                  
           [*] NFS client support for the NFSv3 ACL protocol extension 
		[*] NFS client support for NFS version 4 (EXPERIMENTAL) 
		[*] NFS client support for NFSv4.1 (DEVELOPER ONLY) 
		[*] Root file system on NFS        

2. bootm启动开发板,此时已经可以正常启动了,但是会显示无法打开一些文件

can't run '/etc/init.d/rcS': No such file or directory
can't open /dev/tty2: No such file or directory
can't open /dev/tty3: No such file or directory
can't open /dev/tty4: No such file or directory

3. 添加配置文件

(1)在/home/sijifan/mine/nfs/X210_rootfs 目录下创建/etc文件夹,然后创建inittab文件,文件内容如下(经典版):【到此为止已经完成了一个最简文件系统,可以进入控制台】

#first:run the system script file
::sysinit:/etc/init.d/rcS
::askfirst:-/bin/sh
::ctrlaltdel:-/sbin/reboot
#umount all filesystem
::shutdown:/bin/umount -a -r
#restart init process
::restart:/sbin/init

(2)添加/etc/init.d/rcS文件,内容如下(经典版):

#!/bin/sh
PATH=/sbin:/bin:/usr/sbin:/usr/bin

runlevel=S
prevlevel=N

umask 022

export PATH runlevel prevlevel

mount -a

echo /sbin/mdev > /proc/sys/kernel/hotplug
mdev -s

/bin/hostname -F /etc/sysconfig/HOSTNAME

ifconfig eth0 192.168.1.10

(3)创建/etc/fstab文件,内容如下:

# /etc/fstab: static file system information.
#
# Use 'vol_id --uuid' to print the universally unique identifier for a
# device; this may be used with UUID= as a more robust way to name devices
# that works even if disks are added and removed. See fstab(5).
#
# 	<file system> 	<mount point> 	<type> 	<options> 	<dump> 	<pass>
	proc 			/proc 			proc 	defaults 	0 		0
	sysfs 			/sys 			sysfs 	defaults 	0 		0
	tmpfs 			/var 			tmpfs 	defaults 	0 		0
	tmpfs 			/tmp 			tmpfs 	defaults 	0 		0
	tmpfs 			/dev 			tmpfs 	defaults 	0 		0

(4)找不着挂载点(注意和上一步的fstab文件的<mount point>对比),报错如下:

mount: mounting proc on /proc failed: No such file or directory
mount: mounting sysfs on /sys failed: No such file or directory
mount: mounting tmpfs on /var failed: No such file or directory
mount: mounting tmpfs on /tmp failed: No such file or directory
mount: mounting tmpfs on /dev failed: No such file or directory

        解决办法就是在/home/sijifan/mine/nfs/X210_rootfs目录下创建/proc,/sys,/var,/tmp,/dev。然后重新启动开发板,神奇的是:在/home/sijifan/mine/nfs/X210_rootfs目录下的/proc和/sys文件夹下是没有内容的,但是开发板端的/proc和/sys下是有内容的。

(5)创建/etc/sysconfig/HOSTNAME,内容随便填,比如sijifan210

(6)创建/etc/profile,内容如下:(这里完成后就会显示hostname)

# Ash profile
# vim: syntax=sh

# No core files by default
ulimit -S -c 0 > /dev/null 2>&1

USER="`id -un`"
LOGNAME=$USER
PS1='[\u@\h \W]\# '
PATH=$PATH

HOSTNAME=`/bin/hostname`

export USER LOGNAME PS1 PATH

(7)配置使系统有登录界面

        1> 首先,在inittab中修改,去掉/bin/sh,换上/bin/login或者/sbin/getty,这两个东西的具体区别不祥,但是在busybox中都是链接文件,指向busybox。

#::askfirst:-/bin/sh
::sysinit:/bin/login

        2> 然后直接复制ubuntu系统中的/etc/passwd和/etc/shadow文件,前者用来存储密码的设置,后者用来存储加密后的密码。

        3> 将两个文件中的root一行留下,其余删除,然后创建/root;最后将passwd文件中的/bin/bash改为/bin/sh,因为busybox中使用的是shell脚本。

root:x:0:0:root:/root:/bin/sh

        root后面的/bin/sh表示root用户登录后执行的第一个程序,补充一句,busybox是没有普通用户的。

        4> 指定系统使用的串口

s3c2410_serial2::sysinit:/bin/login

        5> 用getty方式登录,inittab文件做以下修改即可:

#s3c2410_serial2::sysinit:/bin/login
s3c2410_serial2::respawn:/sbin/getty -L s3c2410_serial2 115200 vt100

4. 拷贝动态链接库

        首先,arm-2009q3这个交叉编译工具链的动态链接库在/usr/local/arm/arm-2009q3/arm-none-linux-gnueabi/libc/lib目录下。

        然后,复制动态链接库到/home/sijifan/mine/nfs/X210_rootfs/lib目录下。复制时要注意参数用-rdf,主要目的就是符号链接复制过来还是符号链接。

cp lib/*so* /home/sijifan/mine/nfs/X210_rootfs/lib -rdf

        再然后,使用strip工具去掉库中符号信息(执行这一步之后lib文件从7.5M减少到5.9M)

arm-linux-strip *so*

        最后交叉编译测试程序

静态链接:arm-linux-gcc hello.c -o hello_satic -static
动态链接:arm-linux-gcc hello.c -o hello_dynamic

5. 制作ext2格式的文件系统镜像并烧录启动

(1)制作ext2格式的镜像,大小为1024*10240个字节即10M,可以通过修改count改变大小,确保能够放下自己制作的文件系统。退回到/home/sijifan/mine/nfs/目录,创建rootfs文件夹,然后执行如下操作

dd if=/dev/zero of=rootfs.ext2 bs=1024 count=10240
losetup  /dev/loop1 rootfs.ext2
mke2fs -m 0 /dev/loop1 10240
mount -t ext2 /dev/loop1 ./rootfs/      #挂载到rootfs目录下

(2)然后进入rootfs文件夹,向./rootfs中复制内容,用cp ../X210_rootfs/* ./ -rf

cp ../X210_rootfs/* ./ -rf
cd ..
umount /dev/loop1       #取消挂载
losetup -d /dev/loop1

(3)/home/sijifan/mine/nfs目录下生成的rootfs.ext2即文件系统镜像

6. 烧写文件系统镜像

(1)使用fastboot烧录制作好的rootfs.ext2到开发板inand中

fastboot flash system rootfs.ext2

(2)烧录完成后重启系统,设置bootargs为:

set bootargs console=ttySAC2,115200 root=/dev/mmcblk0p2 rw init=/linuxrc rootfstype=ext2

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值