1. 程式人生 > >NFS開發環境搭建

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種方式:
[email protected]
:/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 ] 

[email protected]:/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下載一樣的效果