NfsHowto.html 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. <html>
  2. <head>
  3. <title>NFS Client How-To</title>
  4. </head>
  5. <body background="backgd.gif">
  6. <hr><hr>
  7. <table width ="100%">
  8. <tr align="center" bgcolor="#e4e4e4">
  9. <td>
  10. <h1><big><font color="#3c34ec"><i>NFS Client How-To</i></font></big></h1>
  11. <p>Last Updated: June 18, 2012</p>
  12. </td>
  13. </tr>
  14. </table>
  15. <hr><hr>
  16. <table width ="100%">
  17. <tr bgcolor="#e4e4e4">
  18. <td>
  19. <h1>Table of Contents</h1>
  20. </td>
  21. </tr>
  22. </table>
  23. <center><table width ="80%">
  24. <tr>
  25. <td>
  26. <table>
  27. <tr>
  28. <td valign="top" width="22"><img height="20" width="20" src="favicon.ico"></td>
  29. <td>
  30. <a href="#nfsconfiguration">Adding NFS to the NuttX Configuration</a>
  31. </td>
  32. </tr>
  33. </table>
  34. <table>
  35. <tr>
  36. <td valign="top" width="22"><img height="20" width="20" src="favicon.ico"></td>
  37. <td>
  38. <a href="#mountinterface">Mount Interface</a>
  39. </td>
  40. </tr>
  41. </table>
  42. <table>
  43. <tr>
  44. <td valign="top" width="22"><img height="20" width="20" src="favicon.ico"></td>
  45. <td>
  46. <a href="#nfsmount">NFS Mount Command</a>
  47. </td>
  48. </tr>
  49. </table>
  50. <table>
  51. <tr>
  52. <td valign="top" width="22"><img height="20" width="20" src="favicon.ico"></td>
  53. <td>
  54. <a href="#serverconfig">Configuring the NFS server (Ubuntu)</a>
  55. </td>
  56. </tr>
  57. </table>
  58. </td>
  59. </tr>
  60. </table></center>
  61. <table width ="100%">
  62. <tr bgcolor="#e4e4e4">
  63. <td>
  64. <a name="nfsconfiguration"><h1>Adding NFS to the NuttX Configuration</h1></a>
  65. </td>
  66. </tr>
  67. </table>
  68. <p>
  69. The NFS client is easily added to your configuration:
  70. You simply need to add <code>CONFIG_NFS</code> to your <code>nuttx/.config</code> file.
  71. There are, however, a few dependencies on other system settings:
  72. </p>
  73. <ol>
  74. <li>
  75. First, there are things that you must configure in order to be able to use any file system:
  76. </li>
  77. <ul>
  78. <li>
  79. <code>CONFIG_DISABLE_MOUNTPOINT=n</code>. You must include support for mount points in the pseudo-file system.
  80. </li>
  81. </ul>
  82. <li>
  83. And there are several dependencies on the networking configuration.
  84. At a minimum, you need to have the following selections:
  85. </li>
  86. <ul>
  87. <li>
  88. <code>CONFIG_NET=y</code>. General networking support.
  89. </li>
  90. <li>
  91. <code>CONFIG_NET_UDP=y</code>. Support for UDP.
  92. </li>
  93. </ul>
  94. </ol>
  95. <table width ="100%">
  96. <tr bgcolor="#e4e4e4">
  97. <td>
  98. <a name="mountinterface"><h1>Mount Interface</h1></a>
  99. </td>
  100. </tr>
  101. </table>
  102. <p>
  103. A low-level, C-callable interface is provided to mount a file system.
  104. That interface is called <code>mount()</code> and is mentioned in the <a href="NuttxPortingGuide.html#NxFileSystem"><code>porting guide</code></a> and is prototyped in the header file <code>include/sys/mount.h</code>:
  105. </p>
  106. <ul><pre>
  107. int mount(const char *source, const char *target, const char *filesystemtype, unsigned long mountflags, const void *data);
  108. </pre></ul>
  109. <p>
  110. <b>Synopsis</b>:
  111. <code>mount()</code> attaches the filesystem specified by the <code>source</code> block device name into the root file system at the path specified by <code>target</code>.
  112. </p>
  113. <p>
  114. <b>Input Paramters</b>:
  115. <ul>
  116. <li><code>source</code>. A null-terminated string providing the fill path to a block driver in the NuttX pseudo-file system.
  117. <li><code>target</code>. The location in the NuttX pseudo-file system where the volume will be mounted.
  118. <li><code>filesystemtype</code>. A string identifying the type of file system to use.
  119. <li><code>mountflags</code>. Various flags that can be used to qualify how the file system is mounted.
  120. <li><code>data</code>. Opaque data that is passed to the file system with the mount occurs.
  121. </ul>
  122. </p>
  123. <p>
  124. <b>Returned Values</b>
  125. Zero is returned on success; -1 is returned on an error and <code>errno</code> is set appropriately:
  126. <ul>
  127. <li><code>EACCES</code>.
  128. A component of a path was not searchable or mounting a read-onlyfilesystem was attempted without giving the <code>MS_RDONLY</code> flag.
  129. </li>
  130. <li><code>EBUSY</code>.
  131. <code>source</code> is already mounted.
  132. </li>
  133. <li><code>EFAULT</code>.
  134. One of the pointer arguments points outside the user address space.
  135. </li>
  136. <li><code>EINVAL</code>.
  137. <code>source</code> had an invalid superblock.
  138. </li>
  139. <li><code>ENODEV</code>.
  140. <code>filesystemtype</code> not configured
  141. </li>
  142. <li><code>ENOENT</code>.
  143. A pathname was empty or had a nonexistent component.
  144. </li>
  145. <li><code>ENOMEM</code>.
  146. Could not allocate a memory to copy filenames or data into.
  147. </li>
  148. <li><code>ENOTBLK</code>.
  149. <code>source</code> is not a block device
  150. </li>
  151. </ul>
  152. </p>
  153. <p>
  154. This same interface can be used to mount a remote, NFS file system using some special parameters.
  155. The NFS mount differs from the <i>normal</i> file system mount in that: (1) there is no block driver for the NFS file system, and (2) special parameters must be passed as <code>data</code> to describe the remote NFS server.
  156. Thus the following code snippet might represent how an NFS file system is mounted:
  157. </p>
  158. <ul><pre>
  159. #include &lt;sys/mount.h&gt;
  160. #include &lt;nuttx/fs/nfs.h&gt;
  161. struct nfs_args data;
  162. char *mountpoint;
  163. ret = mount(NULL, mountpoint, string &quot;nfs&quot;, 0, (FAR void *)&data);
  164. </pre></ul>
  165. <p>
  166. NOTE that: (1) the block driver parameter is <code>NULL</code>.
  167. The <code>mount()</code> is smart enough to know that no block driver is needed with the NFS file system.
  168. (2) The NFS file system is identified with the simple string &quot;nfs&quot;
  169. (3) A reference to <code>struct nfs_args</code> is passed as an NFS-specific argument.
  170. </p>
  171. <p>
  172. The NFS-specific interface is described in the file <code>include/nuttx/fs/nfs.h</code>.
  173. There you can see that <code>struct nfs_args</code> is defined as:
  174. </p>
  175. <ul><pre>
  176. struct nfs_args
  177. {
  178. uint8_t addrlen; /* Length of address */
  179. uint8_t sotype; /* Socket type */
  180. uint8_t flags; /* Flags, determines if following are valid: */
  181. uint8_t timeo; /* Time value in deciseconds (with NFSMNT_TIMEO) */
  182. uint8_t retrans; /* Times to retry send (with NFSMNT_RETRANS) */
  183. uint16_t wsize; /* Write size in bytes (with NFSMNT_WSIZE) */
  184. uint16_t rsize; /* Read size in bytes (with NFSMNT_RSIZE) */
  185. uint16_t readdirsize; /* readdir size in bytes (with NFSMNT_READDIRSIZE) */
  186. char *path; /* Server's path of the directory being mount */
  187. struct sockaddr_storage addr; /* File server address (requires 32-bit alignment) */
  188. };
  189. </pre></ul>
  190. <table width ="100%">
  191. <tr bgcolor="#e4e4e4">
  192. <td>
  193. <a name="nfsmount"><h1>NFS Mount Command</h1></a>
  194. </td>
  195. </tr>
  196. </table>
  197. <p>
  198. The <a href="NuttShell.html">NuttShell (NSH)</a> also supports a command called <code>nfsmount</code>
  199. that can be used to mount a remote file system via the NSH command line.
  200. </p>
  201. <p>
  202. <b>Command Syntax:</b>
  203. </p>
  204. <ul><pre>
  205. nfsmount &lt;server-address&gt; &lt;mount-point&gt; &lt;remote-path&gt;
  206. </pre></ul>
  207. <p>
  208. <b>Synopsis</b>.
  209. The <code>nfsmount</code> command mounts a network file system in the NuttX pseudo filesystem.
  210. The <code>nfsmount</code> will use NFSv3 UDP protocol to mount the remote file system.
  211. </p>
  212. <p>
  213. <b>Command Line Arguments</b>.
  214. The <code>nfsmount</code> takes three arguments:
  215. </p>
  216. <ol>
  217. <li>
  218. The <code>&lt;server-address&gt;</code> is the IP address of the server exporting the file system you wish to mount.
  219. This implementation of NFS for the NuttX RTOS is only for a local area network, so the server and client must be in the same network.
  220. </li>
  221. <li>
  222. The <code>&lt;mount-point &gt;</code> is the location in the NuttX pseudo filesystem where the mounted volume will appear.
  223. This mount point can only reside in the NuttX pseudo filesystem.
  224. By convention, this mount point is a subdirectory under <code>/mnt</code>.
  225. The mount command will create whatever pseudo directories that may be needed to complete the full path (but the full path must not already exist).
  226. </li>
  227. <li>
  228. The <code>&lt;remote-path&gt;</code> is the file system <code>/</code> directory being exported from server.
  229. This <code>/</code> directory must have been configured for exportation on the server before when the NFS server was set up.
  230. </li>
  231. </ol>
  232. <p>
  233. After the volume has been mounted in the NuttX pseudo filesystem, it may be access in the same way as other objects in the file system.
  234. </p>
  235. <p>
  236. <b>Example</b>.
  237. Suppose that the NFS server has been configured to export the directory <code>/export/shared</code>.
  238. The the following command would mount that file system (assuming that the target also has privileges to mount the file system).
  239. </p>
  240. <ul><pre>
  241. NuttShell (NSH)
  242. nsh&gt; ls /mnt
  243. /mnt:
  244. nsh: ls: no such directory: /mnt
  245. nsh&gt; nfsmount 10.0.0.1 /mnt/nfs /export/shared
  246. nsh&gt; ls -l /mnt/nfs
  247. /mnt/nfs:
  248. drwxrwxrwx 4096 ..
  249. drwxrwxrwx 4096 testdir/
  250. -rw-rw-rw- 6 ctest.txt
  251. -rw-r--r-- 15 btest.txt
  252. drwxrwxrwx 4096 .
  253. nsh&gt; echo &quot;This is a test&quot; &gt;/mnt/nfs/testdir/testfile.txt
  254. nsh&gt; ls -l /mnt/nfs/testdir
  255. /mnt/nfs/testdir:
  256. -rw-rw-rw- 21 another.txt
  257. drwxrwxrwx 4096 ..
  258. drwxrwxrwx 4096 .
  259. -rw-rw-rw- 16 testfile.txt
  260. nsh&gt; cat /mnt/nfs/testdir/testfile.txt
  261. This is a test
  262. </pre></ul>
  263. <table width ="100%">
  264. <tr bgcolor="#e4e4e4">
  265. <td>
  266. <a name="serverconfig"><h1>Configuring the NFS server (Ubuntu)</h1></a>
  267. </td>
  268. </tr>
  269. </table>
  270. <p>
  271. Setting up the server will be done in two steps:
  272. First, setting up the configuration file for NFS, and then starting the NFS services.
  273. But first, you need to install the nfs server on Ubuntu with these two commands:
  274. </p>
  275. <ul><pre>
  276. # sudo apt-get install nfs-common</FONT>
  277. # sudo apt-get install nfs-kernel-server</FONT>
  278. </pre></ul>
  279. <p>
  280. After that, we need to make or choose the directory we want to export from the NFS server.
  281. In our case, we are going to make a new directory called <code>/export</code>.
  282. </p>
  283. <ul><pre>
  284. # sudo mkdir /export
  285. </pre></ul>
  286. <p>
  287. It is important that <code>/export</code> directory allow access to everyone (777 permissions) as we will be accessing the NFS share from the client with no authentication.
  288. </p>
  289. <ul><pre>
  290. # sudo chmod 777 /export
  291. </pre></ul>
  292. <p>
  293. When all this is done, we will need to edit the configuration file to set up an NFS server: <code>/etc/exports</code>.
  294. This file contains a list of entries;
  295. each entry indicates a volume that is shared and how it is shared.
  296. For more information for a complete description of all the setup options for this file you can check in the man pages (<code>man export</code>).</p>
  297. An entry in <code>/etc/exports</code> will typically look like this:
  298. </p>
  299. <ul><pre>
  300. directory machine1(option11,option12)
  301. </pre></ul>
  302. <p>
  303. So for our example we export <coce>/export</code> to the client 10.0.0.2 add the entry:
  304. </p>
  305. <ul><pre>
  306. /export 10.0.0.2(rw)
  307. </pre></ul>
  308. <p>
  309. In our case we are using all the default options except for the <code>ro</code> that we replaced with <code>rw</code> so that our client will have read and write access to the directory that we are exporting.
  310. </p>
  311. </p>
  312. After we do all the require configurations, we are ready to start the server with the next command:
  313. </p>
  314. <ul><pre>
  315. # sudo /etc/init.d/nfs-kernel-server start
  316. </pre></ul>
  317. </p>
  318. Note: If you later decide to add more NFS exports to the /etc/exports file, you will need to either restart NFS daemon
  319. or run command exportfs.
  320. </p>
  321. <ul><pre>
  322. # sudo /etc/init.d/nfs-kernel-server start
  323. </pre></ul>
  324. <p>Or</p>
  325. <ul><pre>
  326. # exportfs -ra
  327. </pre></ul>
  328. <p>
  329. Now we can check if the export directory and our mount point is properly set up.
  330. </p>
  331. <ul><pre>
  332. # sudo showmount -e
  333. # sudo showmount -a
  334. </pre></ul>
  335. <p>
  336. And also we can verify if NFS is running in the system with:
  337. </p>
  338. <P STYLE="margin-left: 0.49in; margin-bottom: 0in; line-height: 100%">
  339. <ul><pre>
  340. # rpcinfo &ndash;p</FONT>
  341. program vers proto port
  342. 100000 2 tcp 111 portmapper
  343. 100000 2 udp 111 portmapper
  344. 100011 1 udp 749 rquotad
  345. 100011 2 udp 749 rquotad
  346. 100005 1 udp 759 mountd
  347. 100005 1 tcp 761 mountd
  348. 100005 2 udp 764 mountd
  349. 100005 2 tcp 766 mountd
  350. 100005 3 udp 769 mountd
  351. 100005 3 tcp 771 mountd
  352. 100003 2 udp 2049 nfs
  353. 100003 3 udp 2049 nfs
  354. 300019 1 tcp 830 amd
  355. 300019 1 udp 831 amd
  356. 100024 1 udp 944 status
  357. 100024 1 tcp 946 status
  358. 100021 1 udp 1042 nlockmgr
  359. 100021 3 udp 1042 nlockmgr
  360. 100021 4 udp 1042 nlockmgr
  361. 100021 1 tcp 1629 nlockmgr
  362. 100021 3 tcp 1629 nlockmgr
  363. 100021 4 tcp 1629 nlockmgr
  364. </pre></ul>
  365. <p>
  366. Now your NFS sever is sharing <code>/export</code> directory to be accessed.
  367. </p>
  368. </body>
  369. </html>