nfs.rst 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. =================
  2. NFS Client How-To
  3. =================
  4. Adding NFS to the NuttX Configuration
  5. =====================================
  6. The NFS client is easily added to your configuration: You simply need to
  7. add ``CONFIG_NFS`` to your ``nuttx/.config`` file. There are, however, a
  8. few dependencies on other system settings:
  9. First, there are things that you must configure in order to be able to
  10. use any file system:
  11. - ``CONFIG_DISABLE_MOUNTPOINT=n``. You must include support for mount
  12. points in the pseudo-file system.
  13. And there are several dependencies on the networking configuration. At a
  14. minimum, you need to have the following selections:
  15. - ``CONFIG_NET=y``. General networking support.
  16. - ``CONFIG_NET_UDP=y``. Support for UDP.
  17. Mount Interface
  18. ===============
  19. A low-level, C-callable interface is provided to mount a file system.
  20. That interface is called ``mount()`` and is mentioned in the
  21. ```porting guide`` <NuttxPortingGuide.html#NxFileSystem>`__ and is
  22. prototyped in the header file ``include/sys/mount.h``:
  23. .. c:function:: int mount(const char *source, const char *target, const char *filesystemtype, unsigned long mountflags, const void *data)
  24. ``mount()`` attaches the filesystem specified by the
  25. ``source`` block device name into the root file system at the path
  26. specified by ``target``.
  27. :param source: A null-terminated string providing the fill path to a
  28. block driver in the NuttX pseudo-file system.
  29. :param target: The location in the NuttX pseudo-file system where the
  30. volume will be mounted.
  31. :param filesystemtype: A string identifying the type of file system to
  32. use.
  33. :param mountflags: Various flags that can be used to qualify how the
  34. file system is mounted.
  35. :param data: Opaque data that is passed to the file system with the
  36. mount occurs.
  37. :return: Zero is returned on success; -1 is returned on an
  38. error and ``errno`` is set appropriately:
  39. - ``EACCES``. A component of a path was not searchable or mounting a
  40. read-only filesystem was attempted without giving the ``MS_RDONLY``
  41. flag.
  42. - ``EBUSY``. ``source`` is already mounted.
  43. - ``EFAULT``. One of the pointer arguments points outside the user
  44. address space.
  45. - ``EINVAL``. ``source`` had an invalid superblock.
  46. - ``ENODEV``. ``filesystemtype`` not configured
  47. - ``ENOENT``. A pathname was empty or had a nonexistent component.
  48. - ``ENOMEM``. Could not allocate a memory to copy filenames or data
  49. into.
  50. - ``ENOTBLK``. ``source`` is not a block device
  51. This same interface can be used to mount a remote, NFS file system using
  52. some special parameters. The NFS mount differs from the *normal* file
  53. system mount in that: (1) there is no block driver for the NFS file
  54. system, and (2) special parameters must be passed as ``data`` to
  55. describe the remote NFS server. Thus the following code snippet might
  56. represent how an NFS file system is mounted:
  57. .. code-block:: c
  58. #include <sys/mount.h>
  59. #include <nuttx/fs/nfs.h>
  60. struct nfs_args data;
  61. char *mountpoint;
  62. ret = mount(NULL, mountpoint, string "nfs", 0, (FAR void *)&data);
  63. NOTE that: (1) the block driver parameter is ``NULL``. The ``mount()``
  64. is smart enough to know that no block driver is needed with the NFS file
  65. system. (2) The NFS file system is identified with the simple string
  66. "nfs" (3) A reference to ``struct nfs_args`` is passed as an
  67. NFS-specific argument.
  68. The NFS-specific interface is described in the file
  69. ``include/nuttx/fs/nfs.h``. There you can see that ``struct nfs_args``
  70. is defined as:
  71. .. code-block:: c
  72. struct nfs_args
  73. {
  74. uint8_t addrlen; /* Length of address */
  75. uint8_t sotype; /* Socket type */
  76. uint8_t flags; /* Flags, determines if following are valid: */
  77. uint8_t timeo; /* Time value in deciseconds (with NFSMNT_TIMEO) */
  78. uint8_t retrans; /* Times to retry send (with NFSMNT_RETRANS) */
  79. uint16_t wsize; /* Write size in bytes (with NFSMNT_WSIZE) */
  80. uint16_t rsize; /* Read size in bytes (with NFSMNT_RSIZE) */
  81. uint16_t readdirsize; /* readdir size in bytes (with NFSMNT_READDIRSIZE) */
  82. char *path; /* Server's path of the directory being mount */
  83. struct sockaddr_storage addr; /* File server address (requires 32-bit alignment) */
  84. };
  85. NFS Mount Command
  86. =================
  87. The `NuttShell (NSH) <NuttShell.html>`__ also supports a command called
  88. ``nfsmount`` that can be used to mount a remote file system via the NSH
  89. command line.
  90. **Command Syntax:**
  91. .. code-block::
  92. fsmount <server-address> <mount-point> <remote-path>
  93. **Synopsis**. The ``nfsmount`` command mounts a network file system in
  94. the NuttX pseudo filesystem. The ``nfsmount`` will use NFSv3 UDP
  95. protocol to mount the remote file system.
  96. **Command Line Arguments**. The ``nfsmount`` takes three arguments:
  97. #. The ``<server-address>`` is the IP address of the server exporting
  98. the file system you wish to mount. This implementation of NFS for the
  99. NuttX RTOS is only for a local area network, so the server and client
  100. must be in the same network.
  101. #. The ``<mount-point >`` is the location in the NuttX pseudo filesystem
  102. where the mounted volume will appear. This mount point can only
  103. reside in the NuttX pseudo filesystem. By convention, this mount
  104. point is a subdirectory under ``/mnt``. The mount command will create
  105. whatever pseudo directories that may be needed to complete the full
  106. path (but the full path must not already exist).
  107. #. The ``<remote-path>`` is the file system ``/`` directory being
  108. exported from server. This ``/`` directory must have been configured
  109. for exportation on the server before when the NFS server was set up.
  110. After the volume has been mounted in the NuttX pseudo filesystem, it may
  111. be access in the same way as other objects in the file system.
  112. **Example**. Suppose that the NFS server has been configured to export
  113. the directory ``/export/shared``. The the following command would mount
  114. that file system (assuming that the target also has privileges to mount
  115. the file system).
  116. .. code-block:: fish
  117. NuttShell (NSH)
  118. nsh> ls /mnt
  119. /mnt:
  120. nsh: ls: no such directory: /mnt
  121. nsh> nfsmount 10.0.0.1 /mnt/nfs /export/shared
  122. nsh> ls -l /mnt/nfs
  123. /mnt/nfs:
  124. drwxrwxrwx 4096 ..
  125. drwxrwxrwx 4096 testdir/
  126. -rw-rw-rw- 6 ctest.txt
  127. -rw-r--r-- 15 btest.txt
  128. drwxrwxrwx 4096 .
  129. nsh> echo "This is a test" >/mnt/nfs/testdir/testfile.txt
  130. nsh> ls -l /mnt/nfs/testdir
  131. /mnt/nfs/testdir:
  132. -rw-rw-rw- 21 another.txt
  133. drwxrwxrwx 4096 ..
  134. drwxrwxrwx 4096 .
  135. -rw-rw-rw- 16 testfile.txt
  136. nsh> cat /mnt/nfs/testdir/testfile.txt
  137. This is a test
  138. Configuring the NFS server (Ubuntu)
  139. ===================================
  140. Setting up the server will be done in two steps: First, setting up the
  141. configuration file for NFS, and then starting the NFS services. But
  142. first, you need to install the nfs server on Ubuntu with these two
  143. commands:
  144. .. code-block:: console
  145. # sudo apt-get install nfs-common
  146. # sudo apt-get install nfs-kernel-server
  147. After that, we need to make or choose the directory we want to export
  148. from the NFS server. In our case, we are going to make a new directory
  149. called ``/export``.
  150. .. code-block:: console
  151. # sudo mkdir /export
  152. It is important that ``/export`` directory allow access to everyone (777
  153. permissions) as we will be accessing the NFS share from the client with
  154. no authentication.
  155. .. code-block:: console
  156. # sudo chmod 777 /export
  157. When all this is done, we will need to edit the configuration file to
  158. set up an NFS server: ``/etc/exports``. This file contains a list of
  159. entries; each entry indicates a volume that is shared and how it is
  160. shared. For more information for a complete description of all the setup
  161. options for this file you can check in the man pages (``man export``).
  162. An entry in ``/etc/exports`` will typically look like this:
  163. .. code-block::
  164. directory machine1(option11,option12)
  165. So for our example we export ``/export`` to the client 10.0.0.2 add the
  166. entry:
  167. .. code-block::
  168. /export 10.0.0.2(rw)
  169. In our case we are using all the default options except for the ``ro``
  170. that we replaced with ``rw`` so that our client will have read and write
  171. access to the directory that we are exporting.
  172. After we do all the require configurations, we are ready to start the
  173. server with the next command:
  174. .. code-block:: console
  175. # sudo /etc/init.d/nfs-kernel-server start
  176. Note: If you later decide to add more NFS exports to the /etc/exports
  177. file, you will need to either restart NFS daemon or run command
  178. exportfs.
  179. .. code-block:: console
  180. # sudo /etc/init.d/nfs-kernel-server start
  181. Or
  182. .. code-block:: console
  183. # exportfs -ra
  184. Now we can check if the export directory and our mount point is properly
  185. set up.
  186. .. code-block:: console
  187. # sudo showmount -e
  188. # sudo showmount -a
  189. And also we can verify if NFS is running in the system with:
  190. .. code-block:: console
  191. # rpcinfo –p
  192. program vers proto port
  193. 100000 2 tcp 111 portmapper
  194. 100000 2 udp 111 portmapper
  195. 100011 1 udp 749 rquotad
  196. 100011 2 udp 749 rquotad
  197. 100005 1 udp 759 mountd
  198. 100005 1 tcp 761 mountd
  199. 100005 2 udp 764 mountd
  200. 100005 2 tcp 766 mountd
  201. 100005 3 udp 769 mountd
  202. 100005 3 tcp 771 mountd
  203. 100003 2 udp 2049 nfs
  204. 100003 3 udp 2049 nfs
  205. 300019 1 tcp 830 amd
  206. 300019 1 udp 831 amd
  207. 100024 1 udp 944 status
  208. 100024 1 tcp 946 status
  209. 100021 1 udp 1042 nlockmgr
  210. 100021 3 udp 1042 nlockmgr
  211. 100021 4 udp 1042 nlockmgr
  212. 100021 1 tcp 1629 nlockmgr
  213. 100021 3 tcp 1629 nlockmgr
  214. 100021 4 tcp 1629 nlockmgr
  215. Now your NFS sever is sharing ``/export`` directory to be accessed.