nfs.rst 9.8 KB

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