tiny4412 busybox制作根文件系统rootfs nfs 挂载 ubuntu 14.04

转载地址:http://blog.****.net/liudijiang/article/details/50555429

首先得要有制作好的uboot和linux内核镜像zImage,先烧录到sd卡里,这个是已经做好了的工作。

 

一,ubuntu上先设置好nfs服务

1.新建一个要挂载的文件

[email protected]:~/share$ pwd

/home/liudijiang/share

我已经建好了,这是我的路径

2.安装nfs服务

[email protected]:~/share$ sudo apt-getinstall nfs-kernel-server

3.修改/etc/exports文件

[email protected]:~/share$ sudo vim/etc/exports

在文件最后添加如下内容

/home/liudijiang/share  *(rw,sync,no_root_squash,no_subtree_check)

*:允许所有的网段访问,也可以使用具体的IP

rw:挂接此目录的客户端对该共享目录具有读写权限

sync:资料同步写入内存和硬盘

no_root_squash:root用户具有对根目录的完全管理访问权限。

no_subtree_check:不检查父目录的权限。

注意,*和后面的括号不能出现空格。

然后保存退出。

4. 重启rpcbind 服务,(14.04版本是rpcbind,较老的版本好像是portmap)

[email protected]:~/share$ sudo/etc/init.d/rpcbind restart

5.重启nfs-kernel-server

[email protected]:~/share$ sudo /etc/init.d/nfs-kernel-serverrestart

 *Stopping NFS kernel daemon                                            [OK ]

 *Unexporting directories for NFS kernel daemon...                        [ OK ]

 *Exporting directories for NFS kernel daemon...                              [ OK ]

 *Starting NFS kernel daemon                                             [OK ]

6.测试nfs服务是否成功

[email protected]:~/share$ sudo mount -tnfs localhost:/home/liudijiang/share /mnt

把本地的/home/liudijiang/share文件夹挂载到/mnt上

在share文件新建和修改的东西就出现在了/mnt上了

取消挂载,sudo umount /mnt

注意,在/mnt下时,执行取消挂载不能成功的,得不在这个路径下才可以取消挂载

到这一步就已经设置好了nfs服务了,一旦开发板能和ubuntu的ip地址ping通,就可以挂载ubuntu的文件夹了。

 

二,制作busybox

busybox官网

https://busybox.net/

 

1.找到busybox-1.23.2.tar.bz2并下载下来,解压

[email protected]:~/work$ tar xfbusybox-1.23.2.tar.bz2

2.配置busybox

[email protected]:~/work/busybox-1.23.2$make menuconfig

tiny4412 busybox制作根文件系统rootfs nfs 挂载 ubuntu 14.04tiny4412 busybox制作根文件系统rootfs nfs 挂载 ubuntu 14.04

 

1)配置动态链接库和设置编译器前缀

进入Busybox Setting菜单后进入Build Options菜单

tiny4412 busybox制作根文件系统rootfs nfs 挂载 ubuntu 14.04tiny4412 busybox制作根文件系统rootfs nfs 挂载 ubuntu 14.04

tiny4412 busybox制作根文件系统rootfs nfs 挂载 ubuntu 14.04tiny4412 busybox制作根文件系统rootfs nfs 挂载 ubuntu 14.04

这里编译器是arm-linux-gcc,所以前缀填arm-linux-,这里选择动态链接库,链接的是arm-linux-gcc的库,后面要从安装arm-linux-gcc的地方考出要用到的库。

 

2)安装路径

进入Installation Options

 tiny4412 busybox制作根文件系统rootfs nfs 挂载 ubuntu 14.04tiny4412 busybox制作根文件系统rootfs nfs 挂载 ubuntu 14.04

tiny4412 busybox制作根文件系统rootfs nfs 挂载 ubuntu 14.04tiny4412 busybox制作根文件系统rootfs nfs 挂载 ubuntu 14.04


3)配置模块命令

进入Linux Module Utilities

tiny4412 busybox制作根文件系统rootfs nfs 挂载 ubuntu 14.04

 tiny4412 busybox制作根文件系统rootfs nfs 挂载 ubuntu 14.04

tiny4412 busybox制作根文件系统rootfs nfs 挂载 ubuntu 14.04tiny4412 busybox制作根文件系统rootfs nfs 挂载 ubuntu 14.04

pretty output、blacklist support、depmod选进去也行。


4)保存配置

进入Save Configuration to an Alternate File菜单

tiny4412 busybox制作根文件系统rootfs nfs 挂载 ubuntu 14.04tiny4412 busybox制作根文件系统rootfs nfs 挂载 ubuntu 14.04

tiny4412 busybox制作根文件系统rootfs nfs 挂载 ubuntu 14.04tiny4412 busybox制作根文件系统rootfs nfs 挂载 ubuntu 14.04

直接ok就行

 

 

3.编译,安装

[email protected]:~/work/busybox-1.23.2$make -j16;make install -j16

[email protected]:~/work$ ls rootfs/

bin linuxrc  sbin  usr

bin sbin usr三个文件夹存放各种命令

linuxrc是一个软连接文件,连接到bin/busybox程序

 

4.检测busybox是否成功

[email protected]:~/work/rootfs$ filebin/busybox

bin/busybox: ELF 32-bit LSB  shared object, ARM, EABI5 version 1 (SYSV),dynamically linked (uses shared libs), stripped

上面显示ARM平台运行,说明安装成功了。

 

5.完善动态链接库

我们在busybox设置的是动态链接库,而且编译器是arm-linux-gcc,找到安装arm-linux-gcc的安装路径

[email protected]:~/work/rootfs$ which arm-linux-gcc

/opt/FriendlyARM/toolschain/4.5.1/bin/arm-linux-gcc

上面是我的路径,/opt/FriendlyARM/toolschain/4.5.1/arm-none-linux-gnueabi/lib,这个就是要复制出来的动态库的路径,把里面的动态链接文件复制到rootfs文件夹下

先在rootfs文件下创建其他文件夹,模仿ubuntu系统,看看他的根目录下有哪些文件夹

[email protected]:~/work/rootfs$ mkdiretc/init.d dev home proc tmp var mnt root sys opt lib –p

复制动态链接文件

[email protected]:~/work/rootfs$ cp/opt/FriendlyARM/toolschain/4.5.1/arm-none-linux-gnueabi/lib/*.so* lib/ -rap

 

6.创建控制台设备文件

[email protected]:~/work/rootfs$ sudo mknoddev/console c 5 1

这个文件是字符设备,主设备号是5,次设备号是1,只能用mknod命令创建

 

7.创建etc/下必要文件

[email protected]:~/work/rootfs/etc$ tree./

./

├── fstab  必要文件,挂着文件中指定的所有文件系统

├── group 不是必要文件,要设置密码时需要

├── init.d 必要的文件夹

│   ├── rcS 必要文件

│   └── rcS.bak 必要文件

├── inittab  从busybox的example文件下复制

├──passwd  不是必要文件,要设置密码时需要

├── profile  不是必要文件,可以创建来支持命令提示格式配置,配置系统环境变量

└── shadow  不是必要文件,要设置密码时需要

1)创建etc/inittab文件

我们可以用busybox提供的示例文件复制过来

[email protected]:~/work/busybox-1.23.2/examples$ls

inittab …

将以下内容(59行左右)

# Start an "askfirst" shell onthe console (whatever that may be)

::askfirst:-/bin/sh

# Start an "askfirst" shell on/dev/tty2-4

tty2::askfirst:-/bin/sh   add "console" in line 60, commentline 62 - 64

tty3::askfirst:-/bin/sh

tty4::askfirst:-/bin/sh

修改为

# Start an "askfirst" shell onthe console (whatever that may be)

console::askfirst:-/bin/sh     #这里加上console,也就是前面在dev文件夹下创建的控制台

# Start an "askfirst" shell on/dev/tty2-4

#tty2::askfirst:-/bin/sh   add "console" in line 60, commentline 62 - 64

#tty3::askfirst:-/bin/sh

#tty4::askfirst:-/bin/sh

 

2)fstab文件

手动创建一个fstab文件,添加以下内容

tmpfs                   /dev/shm                tmpfs   defaults        0 0

devpts                  /dev/pts                devpts  defaults        0 0

sysfs                   /sys                    sysfs   defaults       0 0

proc                    /proc                   proc    defaults        0 0

第一列是挂载设备,第二列是挂载目录,第三列是挂载文件系统类型,第四列是挂载选项分别有

auto:系统自动挂载,fstab默认就是这个选项

ro:read-only

rw:read-write

defaults:rw,suid,dev,exec,auto,nouser,and async

第五列为dump选项,设置是否让备份程序dump备份文件系统,0为忽略,1为备份

第六列为fsck选项,告诉fsck程序以什么顺序检查文件系统,0为忽略

 

3)rcS文件

在init.d文件夹下创建,添加以下内容

#!/bin/sh

mount –a #挂载fstab文件中所有指定的文件系统

mkdir /dev/pts

mount -t devpts devpts /dev/pts

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

mdev –s   #创建控制台

 

/bin/hostname ubuntu #这里修改hostname

 

注意,查看一下rcS文件的权限,如果没有执行权限的话,开发板的linux内核不能运行这个文件

sudo chmod +x rcS  添加权限

 

 

4)profile文件

添加以下内容

USER="`id -un`"

LOGNAME=$USER

PS1='[\[email protected]\h \W]# '

PATH=$PATH

 

HOSTNAME=`/bin/hostname`

 

#导出环境变量

export USER LOGNAME PS1 PATH

 

5)group、passwd、shadow三个文件是要用密码登陆的时候才用到的,可以从linux系统的/etc/group、/etc/passwd 、/etc/shadow 拷贝过来

然后修改inittab文件,原来修改的地方改成:
# Start an "askfirst" shell on the console (whatever that may be)

#console::askfirst:-/bin/sh

::respawn:-/sbin/getty 115200 ttySAC0

# Start an "askfirst" shell on /dev/tty2-4

#tty2::askfirst:-/bin/sh   add "console" in line 60, commentline 62 - 64

#tty3::askfirst:-/bin/sh

#tty4::askfirst:-/bin/sh

这里用的账号密码时原来ubuntu linux上已有的账号密码

这里还要修改passwd文件的内容,将所有”/bin/bash“ 改成“/bin/sh”

这是修改shell脚本的类型,PC上linux运行的是bash shell脚本,开发板linux运行的是sh shell脚本。

不用这个也可以,不用的时候开发板开机运行直接进入命令行了,加上这些的时候,开机运行后要登陆的账号密码才能进入开发板的linux系统,纯粹是为了好玩的。。。

 

小结一下这部分:

1.      inittab,rcS,fstab关系

系统启动挂载文件系统--->

--->读取/etc/inittab文件,解析其中定义的动作

--->根据sysinit定义的process执行/etc/init.d/rcS

--->/etc/init.d/rcS中第一条命令 mount -a

--->读取/etc/fstab文件

--->根据文件列表内容逐个挂载其中的设备到指定地

 

2.profile文件

设置环境变量,根据这里设置命令提示符号相关的环境变量

用户可以根据自己需要添加环境变量

 

3./etc/init.d/rcS执行系统初始化时候,想开机启动的程序都可以写在这个文件中

 

4.group存放用户ID信息,命令提示符号格式配置需要使用这个文件

 

5.passwd和用户相关的密码

 

等会设置好电脑、ubuntu、开发板的ip后,开发板uboot设置好启动后挂载ubuntu的这个rootfs,就能够在电脑和Xshell上通用rootfs这个文件夹了。开发板linux运行起来也就是读取rootfs里面这些文件,运行这些文件的内容了。

 

三,设置电脑主机、虚拟机、开发板的ip地址

先把开发板和电脑网线连接起来,让开发板跑起来,虽然这时候什么也没动,但应该能看到开发板和电脑的网口的指示灯都是亮的

设置电脑的ip

为了开发方便,手动设置成静态ip

tiny4412 busybox制作根文件系统rootfs nfs 挂载 ubuntu 14.04tiny4412 busybox制作根文件系统rootfs nfs 挂载 ubuntu 14.04

tiny4412 busybox制作根文件系统rootfs nfs 挂载 ubuntu 14.04tiny4412 busybox制作根文件系统rootfs nfs 挂载 ubuntu 14.04

tiny4412 busybox制作根文件系统rootfs nfs 挂载 ubuntu 14.04tiny4412 busybox制作根文件系统rootfs nfs 挂载 ubuntu 14.04

保存即可

 

[email protected]:~/work/rootfs$ ifconfig

eth0     Link encap:以太网  硬件地址00:0c:29:29:30:8a 

         inet 地址:192.168.3.27  广播:192.168.3.255  掩码:255.255.255.0

         inet6 地址: fe80::20c:29ff:fe29:308a/64 Scope:Link

         UP BROADCAST RUNNING MULTICAST MTU:1500  跃点数:1

         接收数据包:2421 错误:0 丢弃:0 过载:0 帧数:0

         发送数据包:2849 错误:0 丢弃:0 过载:0 载波:0

         碰撞:0 发送队列长度:1000

         接收字节:335705 (335.7 KB)  发送字节:2430579(2.4 MB)

         中断:19 基本地址:0x2000

 

lo       Link encap:本地环回 

         inet 地址:127.0.0.1  掩码:255.0.0.0

         inet6 地址: ::1/128 Scope:Host

         UP LOOPBACK RUNNING MTU:65536  跃点数:1

         接收数据包:506 错误:0 丢弃:0 过载:0 帧数:0

         发送数据包:506 错误:0 丢弃:0 过载:0 载波:0

         碰撞:0 发送队列长度:0

         接收字节:48008 (48.0 KB)  发送字节:48008(48.0 KB)

 

设置电脑的以太网网卡,设置成静态ip

tiny4412 busybox制作根文件系统rootfs nfs 挂载 ubuntu 14.04tiny4412 busybox制作根文件系统rootfs nfs 挂载 ubuntu 14.04

现在就可以用虚拟机和电脑主机互ping一下,看看通不通

[email protected]:~/work/rootfs$ ping192.168.3.30

PING 192.168.3.30 (192.168.3.30) 56(84)bytes of data.

64 bytes from 192.168.3.30: icmp_seq=1ttl=64 time=5.70 ms

64 bytes from 192.168.3.30: icmp_seq=2ttl=64 time=0.448 ms

64 bytes from 192.168.3.30: icmp_seq=3ttl=64 time=0.474 ms

 

我已经将电脑主机的防火墙都关掉了,不关掉的时候我也能ping通,如果没ping通就把电脑防火墙关掉再试试吧。

 

接下来是设置开发板的ip,复位开发板,进入uboot命令行模式下

设置的参数的命令

liudijiang # setenv gatewayip 192.168.3.1

还有ip地址,掩码也是一样设置,服务器ip设不设置都行

设置bootargs

liudijiang # setenv bootargs root=/dev/nfs nfsroot=192.168.3.27:/home/liudijiang/work/rootfs/ ip=192.168.3.123:192.168.3.27:192.168.3.1:255.255.255.0::eth0:off init=/linuxrcconsole=ttySAC0 lcd=S70

设置bootcmd

setenv bootcmd ‘movi read kernel 040008000;movi read rootfs 0 41000000 100000;bootm 40008000 41000000’

查看结果

liudijiang # pri

baudrate=115200

bootargs=noinitrd root=/dev/nfs nfsroot=192.168.3.27:/home/liudijiang/work/rootfs/ ip=192.168.3.123:192.168.3.27:192.168.3.1:255.255.255.0::eth0:off init=/linuxrcconsole=ttySAC0 lcd=S70

bootcmd=movi read kernel 0 40008000;moviread rootfs 0 41000000 100000;bootm 40008000 41000000

bootdelay=3

ethaddr=00:40:5c:26:0a:5b

gatewayip=192.168.3.1

ipaddr=192.168.3.123

netmask=255.255.255.0

serverip=192.168.3.30

 

和NFS有关的选项:

root=/dev/nfs     --使用NFS这个设备

 

NFS文件夹具体的路径

nfsroot=192.168.3.27:/home/liudijiang/work/rootfs ip=192.168.3.123:192.168.3.27:192.168.3.1:255.255.255.0::eth0:off

        

格式说明:

nfsroot=虚拟机Linux系统IP:根文件系统绝对路径 ip=开发板IP:虚拟机Linux系统IP:网关:子网掩码::开发板的网卡设备名(一般是eth0,不是虚拟机):off

 

以上配置的结果,电脑主机、虚拟机、开发板都在192.168.3这个网段,其他网段也行。。。重新启动后会发现已经挂载到ubuntu的那个目录下了,而且ping一下。。。能挂载上,肯定能ping通了。。。

tiny4412 busybox制作根文件系统rootfs nfs 挂载 ubuntu 14.04tiny4412 busybox制作根文件系统rootfs nfs 挂载 ubuntu 14.04

tiny4412 busybox制作根文件系统rootfs nfs 挂载 ubuntu 14.04

而且我在Xshell里已经能进入开发板linux的命令行了

 

在tmp目录下新建个hello.c,这个当然是在ubuntu上用vim方便啦,然后顺便编译一下。我已经写好了,内容如下

[email protected]:~/work/rootfs/tmp$ cat hello.c

#include <stdio.h>

 

void main(void)

{

printf("hellolinux!\r\n");

}

[email protected]:~/work/rootfs/tmp$arm-linux-gcc -o hello hello.c

[email protected]:~/work/rootfs/tmp$ ls

hello  hello.c

因为是要在开发板上运行,所以是arm-linux-gcc

tiny4412 busybox制作根文件系统rootfs nfs 挂载 ubuntu 14.04tiny4412 busybox制作根文件系统rootfs nfs 挂载 ubuntu 14.04

已经ok了。。。

然而在ubuntu下运行这个hello是不可执行的,因为ubuntu是x86,得gcc编译才可以运行。