tiny4412 busybox製作根文件系統rootfs nfs 掛載 ubuntu 14.04

首先得要有製作好的uboot和linux內核鏡像zImage,先燒錄到sd卡里,這個是已經做好了的工作。

 

一,ubuntu上先設置好nfs服務

1.新建一個要掛載的文件

liudijiang@ubuntu:~/share$ pwd

/home/liudijiang/share

我已經建好了,這是我的路徑

2.安裝nfs服務

liudijiang@ubuntu:~/share$ sudo apt-getinstall nfs-kernel-server

3.修改/etc/exports文件

liudijiang@ubuntu:~/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)

liudijiang@ubuntu:~/share$ sudo/etc/init.d/rpcbind restart

5.重啓nfs-kernel-server

liudijiang@ubuntu:~/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服務是否成功

liudijiang@ubuntu:~/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並下載下來,解壓

liudijiang@ubuntu:~/work$ tar xfbusybox-1.23.2.tar.bz2

2.配置busybox

liudijiang@ubuntu:~/work/busybox-1.23.2$make menuconfig

 

1)配置動態鏈接庫和設置編譯器前綴

進入Busybox Setting菜單後進入Build Options菜單



這裏編譯器是arm-linux-gcc,所以前綴填arm-linux-,這裏選擇動態鏈接庫,鏈接的是arm-linux-gcc的庫,後面要從安裝arm-linux-gcc的地方考出要用到的庫。

 

2)安裝路徑

進入Installation Options

 



3)配置模塊命令

進入Linux Module Utilities

 


pretty output、blacklist support、depmod選進去也行。


4)保存配置

進入Save Configuration to an Alternate File菜單



直接ok就行

 

 

3.編譯,安裝

liudijiang@ubuntu:~/work/busybox-1.23.2$make -j16;make install -j16

liudijiang@ubuntu:~/work$ ls rootfs/

bin linuxrc  sbin  usr

bin sbin usr三個文件夾存放各種命令

linuxrc是一個軟連接文件,連接到bin/busybox程序

 

4.檢測busybox是否成功

liudijiang@ubuntu:~/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的安裝路徑

liudijiang@ubuntu:~/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系統,看看他的根目錄下有哪些文件夾

liudijiang@ubuntu:~/work/rootfs$ mkdiretc/init.d dev home proc tmp var mnt root sys opt lib –p

複製動態鏈接文件

liudijiang@ubuntu:~/work/rootfs$ cp/opt/FriendlyARM/toolschain/4.5.1/arm-none-linux-gnueabi/lib/*.so* lib/ -rap

 

6.創建控制檯設備文件

liudijiang@ubuntu:~/work/rootfs$ sudo mknoddev/console c 5 1

這個文件是字符設備,主設備號是5,次設備號是1,只能用mknod命令創建

 

7.創建etc/下必要文件

liudijiang@ubuntu:~/work/rootfs/etc$ tree./

./

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

├── group 不是必要文件,要設置密碼時需要

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

│   ├── rcS 必要文件

│   └── rcS.bak 必要文件

├── inittab  從busybox的example文件下複製

├──passwd  不是必要文件,要設置密碼時需要

├── profile  不是必要文件,可以創建來支持命令提示格式配置,配置系統環境變量

└── shadow  不是必要文件,要設置密碼時需要

1)創建etc/inittab文件

我們可以用busybox提供的示例文件複製過來

liudijiang@ubuntu:~/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='[\u@\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




保存即可

 

liudijiang@ubuntu:~/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


現在就可以用虛擬機和電腦主機互ping一下,看看通不通

liudijiang@ubuntu:~/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通了。。。



而且我在Xshell裏已經能進入開發板linux的命令行了

 

在tmp目錄下新建個hello.c,這個當然是在ubuntu上用vim方便啦,然後順便編譯一下。我已經寫好了,內容如下

liudijiang@ubuntu:~/work/rootfs/tmp$ cat hello.c

#include <stdio.h>

 

void main(void)

{

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

}

liudijiang@ubuntu:~/work/rootfs/tmp$arm-linux-gcc -o hello hello.c

liudijiang@ubuntu:~/work/rootfs/tmp$ ls

hello  hello.c

因爲是要在開發板上運行,所以是arm-linux-gcc


已經ok了。。。

然而在ubuntu下運行這個hello是不可執行的,因爲ubuntu是x86,得gcc編譯纔可以運行。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章