NFS開發環境搭建

(測試環境Ubuntu14)

安裝目標板掛載根文件系統的方法有兩種(這裏所說的服務端就是ubuntu,Ubuntu已經成功安裝了nfs服務,並且保證服務端與目標板ping 通)

(注意開發板的linux要設置爲nfs啓動和配置nfs支持)
第一種:等待開發板啓動之後去掛載,此時文件系統從Flash中啓動,然後手動的通過命令去掛載服務端的文件系統。

一、安裝

sudo apt-get install nfs-kernel-server
(安裝nfs-kernel-server時,apt會自動安裝nfs-common和portmap)
即分別安裝:
1.服務器端:sudo apt-get install portmap
2.服務器端:sudo apt-get install nfs-kernel-server
3.客戶端:sudo apt-get install nfs-common

二、配置nfs
(1) sudo vim /etc/exports     
     在末尾一行加入

    /home/book/work *(rw,sync,no_root_squash,no_subtree_check)

    或/home/book/work *(rw,sync,no_root_squash)

含義:
/home/book/work 與nfs服務客戶端共享的目錄,
*:允許所有的網段訪問,也可以使用具體的IP
rw:掛接此目錄的客戶端對該共享目錄具有讀寫權限
sync:資料同步寫入內存和硬盤
no_root_squash:root用戶具有對根目錄的完全管理訪問權限。
no_subtree_check:不檢查父目錄的權限。更多的參數說明我在文章下面貼出來了!

(2) 設置 /home/book/work目錄權限
     sudo chmod 777 work -R

(3) 重啓服務:
    #sudo /etc/init.d/portmap restart <---重啓portmap,
    #sudo /etc/init.d/nfs-kernel-server restart <---重啓nfs服務
    #showmount -e <---顯示共享出的目錄

注意:nfs是一個RPC程序,使用它前,經常要映射好端口,通過portmap設定,不過我這裏沒設。
**重啓nfs服務有2種方式:
book@ubuntu:/mnt$ sudo /etc/init.d/nfs-kernel-server restart
* Stopping NFS kernel daemon [ OK ] 
* Unexporting directories for NFS kernel daemon... [ OK ] 
* Exporting directories for NFS kernel daemon... [ OK ] 
* Starting NFS kernel daemon [ OK ] 

book@ubuntu:/mnt$ sudo service nfs-kernel-server restart 
* Stopping NFS kernel daemon [ OK ] 
* Unexporting directories for NFS kernel daemon... [ OK ] 
* Exporting directories for NFS kernel daemon... [ OK ] 
* Starting NFS kernel daemon [ OK ]

(4) 本機上試一下:

sudo mount -t nfs localhost:/home/book/work /mnt

sudo mount 127.0.0.1:/home/book/work /mnt
注:localhost爲本機linux的IP地址

這樣就把共享目錄掛到了/mnt目錄。


(5)  取消掛載用:

sudo umount /mnt

(6) 開發板測試

如果用在嵌入式設備上掛載,要加上參數-o nolock

mount -t nfs -o nolock 192.168.1.8:/home/book/work /mnt

============================================================

第二種方法:直接從nfs啓動,這樣服務端所生成的目標文件,目標端可以直接執行,不需要掛載
 這種做法就是在u-boot啓動之後,設置nfs掛載參數。首先必須掌握nfs命令。

root=/dev/nfs

  This is necessary to enable the pseudo-NFS-device. Note that it's not a
  real device but just a synonym to tell the kernel to use NFS instead of
  a real device.


nfsroot=[<server-ip>:]<root-dir>[,<nfs-options>]

  If the `nfsroot' parameter is NOT given on the command line,
  the default "/tftpboot/%s" will be used.

  <server-ip>    Specifies the IP address of the NFS server.
        The default address is determined by the `ip' parameter
        (see below). This parameter allows the use of different
        servers for IP autoconfiguration and NFS.

  <root-dir>    Name of the directory on the server to mount as root.
        If there is a "%s" token in the string, it will be
        replaced by the ASCII-representation of the client's
        IP address.

  <nfs-options>    Standard NFS options. All options are separated by commas.
        The following defaults are used:
            port        = as given by server portmap daemon
            rsize        = 4096
            wsize        = 4096
            timeo        = 7
            retrans        = 3
            acregmin    = 3
            acregmax    = 60
            acdirmin    = 30
            acdirmax    = 60
            flags        = hard, nointr, noposix, cto, ac


ip=<client-ip>:<server-ip>:<gw-ip>:<netmask>:<hostname>:<device>:<autoconf>

  This parameter tells the kernel how to configure IP addresses of devices
  and also how to set up the IP routing table. It was originally called
  `nfsaddrs', but now the boot-time IP configuration works independently of
  NFS, so it was renamed to `ip' and the old name remained as an alias for
  compatibility reasons.

  If this parameter is missing from the kernel command line, all fields are
  assumed to be empty, and the defaults mentioned below apply. In general
  this means that the kernel tries to configure everything using
  autoconfiguration.

  The <autoconf> parameter can appear alone as the value to the `ip'
  parameter (without all the ':' characters before) in which case auto-
  configuration is used.

  <client-ip>    IP address of the client.

          Default:  Determined using autoconfiguration.

  <server-ip>    IP address of the NFS server. If RARP is used to determine
        the client address and this parameter is NOT empty only
        replies from the specified server are accepted.

        Only required for for NFS root. That is autoconfiguration
        will not be triggered if it is missing and NFS root is not
        in operation.

        Default: Determined using autoconfiguration.
                 The address of the autoconfiguration server is used.

  <gw-ip>    IP address of a gateway if the server is on a different subnet.

        Default: Determined using autoconfiguration.

  <netmask>    Netmask for local network interface. If unspecified
        the netmask is derived from the client IP address assuming
        classful addressing.

        Default:  Determined using autoconfiguration.

  <hostname>    Name of the client. May be supplied by autoconfiguration,
          but its absence will not trigger autoconfiguration.

          Default: Client IP address is used in ASCII notation.

  <device>    Name of network device to use.

        Default: If the host only has one device, it is used.
             Otherwise the device is determined using
             autoconfiguration. This is done by sending
             autoconfiguration requests out of all devices,
             and using the device that received the first reply.

  <autoconf>    Method to use for autoconfiguration. In the case of options
                which specify multiple autoconfiguration protocols,
        requests are sent using all protocols, and the first one
        to reply is used.

        Only autoconfiguration protocols that have been compiled
        into the kernel will be used, regardless of the value of
        this option.

                  off or none: don't use autoconfiguration (default)
          on or any:   use any protocol available in the kernel
          dhcp:        use DHCP
          bootp:       use BOOTP
          rarp:        use RARP
          both:        use both BOOTP and RARP but not DHCP
                       (old option kept for backwards compatibility)

                Default: any

nfs命令格式
在u-boot界面,輸入print就可以顯示如下參數

bootargs=noinitrd root=/dev/mtdblock3 init=/linuxrc console=ttySAC0
然後根據nfs命令格式,具體格式解析要看nfs命令介紹。
nfsroot=[<server-ip>:]<root-dir>[,<nfs-options>]  ip=<client-ip>:<server-ip>:<gw-ip>:<netmask>:<hostname>:<device>:<autoconf>
將啓動參數設置爲
set bootargs noinitrd root=/dev/nfs nfsroot=192.168.1.24:/home/linux/root_fs/first_fs/ ip=192.168.1.12:192.168.1.24:192.168.1.1:255.255.255.0::eth0:off init=/linuxrc console=ttySAC0
當文件系統啓動之後,服務器端的文件會共享過來


============================================================

apt-cache search nfs
nfs 30000000 192.168.1.8:/home/book/work/fs.yaffs2 

這和用tftp下載一樣的效果









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