nfs.scm 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2016 John Darrington <jmd@gnu.org>
  3. ;;; Copyright © 2018, 2019, 2020 Ricardo Wurmus <rekado@elephly.net>
  4. ;;; Copyright © 2020, 2021 Maxim Cournoyer <maxim.cournoyer@gmail.com>
  5. ;;;
  6. ;;; This file is part of GNU Guix.
  7. ;;;
  8. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  9. ;;; under the terms of the GNU General Public License as published by
  10. ;;; the Free Software Foundation; either version 3 of the License, or (at
  11. ;;; your option) any later version.
  12. ;;;
  13. ;;; GNU Guix is distributed in the hope that it will be useful, but
  14. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. ;;; GNU General Public License for more details.
  17. ;;;
  18. ;;; You should have received a copy of the GNU General Public License
  19. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  20. (define-module (gnu services nfs)
  21. #:use-module (gnu)
  22. #:use-module (gnu services shepherd)
  23. #:use-module (gnu packages onc-rpc)
  24. #:use-module (gnu packages linux)
  25. #:use-module (gnu packages nfs)
  26. #:use-module (guix)
  27. #:use-module (guix records)
  28. #:use-module (srfi srfi-1)
  29. #:use-module (ice-9 match)
  30. #:use-module (gnu build file-systems)
  31. #:export (rpcbind-service-type
  32. rpcbind-configuration
  33. rpcbind-configuration?
  34. pipefs-service-type
  35. pipefs-configuration
  36. pipefs-configuration?
  37. idmap-service-type
  38. idmap-configuration
  39. idmap-configuration?
  40. gss-service-type
  41. gss-configuration
  42. gss-configuration?
  43. nfs-service-type
  44. nfs-configuration
  45. nfs-configuration?))
  46. (define default-pipefs-directory "/var/lib/nfs/rpc_pipefs")
  47. (define-record-type* <rpcbind-configuration>
  48. rpcbind-configuration make-rpcbind-configuration
  49. rpcbind-configuration?
  50. (rpcbind rpcbind-configuration-rpcbind
  51. (default rpcbind))
  52. (warm-start? rpcbind-configuration-warm-start?
  53. (default #t)))
  54. (define rpcbind-service-type
  55. (let ((proc
  56. (lambda (config)
  57. (define rpcbind
  58. (rpcbind-configuration-rpcbind config))
  59. (define rpcbind-command
  60. #~(list (string-append #$rpcbind "/sbin/rpcbind") "-f"
  61. #$@(if (rpcbind-configuration-warm-start? config) '("-w") '())))
  62. (shepherd-service
  63. (documentation "Start the RPC bind daemon.")
  64. (requirement '(networking))
  65. (provision '(rpcbind-daemon))
  66. (start #~(make-forkexec-constructor #$rpcbind-command))
  67. (stop #~(make-kill-destructor))))))
  68. (service-type
  69. (name 'rpcbind)
  70. (extensions
  71. (list (service-extension shepherd-root-service-type
  72. (compose list proc))))
  73. ;; We use the extensions feature to allow other services to automatically
  74. ;; configure and start this service. Only one value can be provided. We
  75. ;; override it with the value returned by the extending service.
  76. (compose identity)
  77. (extend (lambda (config values)
  78. (match values
  79. ((first . rest) first)
  80. (_ config))))
  81. (default-value (rpcbind-configuration))
  82. (description "Run the RPC Bind service, which provides a facility to map
  83. ONC RPC program numbers into universal addresses. Many NFS related services
  84. use this facility."))))
  85. (define-record-type* <pipefs-configuration>
  86. pipefs-configuration make-pipefs-configuration
  87. pipefs-configuration?
  88. (mount-point pipefs-configuration-mount-point
  89. (default default-pipefs-directory)))
  90. (define pipefs-service-type
  91. (let ((proc
  92. (lambda (config)
  93. (define pipefs-directory (pipefs-configuration-mount-point config))
  94. (shepherd-service
  95. (documentation "Mount the pipefs pseudo file system.")
  96. (provision '(rpc-pipefs))
  97. (start #~(lambda ()
  98. (mkdir-p #$pipefs-directory)
  99. (mount "rpc_pipefs" #$pipefs-directory "rpc_pipefs")
  100. (member #$pipefs-directory (mount-points))))
  101. (stop #~(lambda (pid . args)
  102. (umount #$pipefs-directory MNT_DETACH)
  103. (not (member #$pipefs-directory (mount-points)))))))))
  104. (service-type
  105. (name 'pipefs)
  106. (extensions
  107. (list (service-extension shepherd-root-service-type
  108. (compose list proc))))
  109. ;; We use the extensions feature to allow other services to automatically
  110. ;; configure and start this service. Only one value can be provided. We
  111. ;; override it with the value returned by the extending service.
  112. (compose identity)
  113. (extend (lambda (config values)
  114. (match values
  115. ((first . rest) first)
  116. (_ config))))
  117. (default-value (pipefs-configuration))
  118. (description "Mount the pipefs file system, which is used to transfer
  119. NFS-related data between the kernel and user-space programs."))))
  120. (define-record-type* <gss-configuration>
  121. gss-configuration make-gss-configuration
  122. gss-configuration?
  123. (pipefs-directory gss-configuration-pipefs-directory
  124. (default default-pipefs-directory))
  125. (nfs-utils gss-configuration-gss
  126. (default nfs-utils)))
  127. (define gss-service-type
  128. (let ((proc
  129. (lambda (config)
  130. (define nfs-utils
  131. (gss-configuration-gss config))
  132. (define pipefs-directory
  133. (gss-configuration-pipefs-directory config))
  134. (define gss-command
  135. #~(list (string-append #$nfs-utils "/sbin/rpc.gssd") "-f"
  136. "-p" #$pipefs-directory))
  137. (shepherd-service
  138. (documentation "Start the RPC GSS daemon.")
  139. (requirement '(rpcbind-daemon rpc-pipefs))
  140. (provision '(gss-daemon))
  141. (start #~(make-forkexec-constructor #$gss-command))
  142. (stop #~(make-kill-destructor))))))
  143. (service-type
  144. (name 'gss)
  145. (extensions
  146. (list (service-extension shepherd-root-service-type
  147. (compose list proc))))
  148. ;; We use the extensions feature to allow other services to automatically
  149. ;; configure and start this service. Only one value can be provided. We
  150. ;; override it with the value returned by the extending service.
  151. (compose identity)
  152. (extend (lambda (config values)
  153. (match values
  154. ((first . rest) first)
  155. (_ config))))
  156. (default-value (gss-configuration))
  157. (description "Run the @dfn{global security system} (GSS) daemon, which
  158. provides strong security for protocols based on remote procedure calls (ONC
  159. RPC)."))))
  160. (define-record-type* <idmap-configuration>
  161. idmap-configuration make-idmap-configuration
  162. idmap-configuration?
  163. (pipefs-directory idmap-configuration-pipefs-directory
  164. (default default-pipefs-directory))
  165. (domain idmap-configuration-domain
  166. (default #f))
  167. (nfs-utils idmap-configuration-nfs-utils
  168. (default nfs-utils))
  169. (verbosity idmap-configuration-verbosity
  170. (default 0)))
  171. (define idmap-service-type
  172. (let ((proc
  173. (lambda (config)
  174. (define nfs-utils
  175. (idmap-configuration-nfs-utils config))
  176. (define pipefs-directory
  177. (idmap-configuration-pipefs-directory config))
  178. (define domain (idmap-configuration-domain config))
  179. (define (idmap-config-file config)
  180. (plain-file "idmapd.conf"
  181. (string-append
  182. "\n[General]\n"
  183. "Verbosity = "
  184. (number->string
  185. (idmap-configuration-verbosity config))
  186. "\n"
  187. (if domain
  188. (format #f "Domain = ~a\n" domain)
  189. "")
  190. "\n[Mapping]\n"
  191. "Nobody-User = nobody\n"
  192. "Nobody-Group = nogroup\n")))
  193. (define idmap-command
  194. #~(list (string-append #$nfs-utils "/sbin/rpc.idmapd") "-f"
  195. "-p" #$pipefs-directory
  196. ;; TODO: this is deprecated
  197. "-c" #$(idmap-config-file config)))
  198. (shepherd-service
  199. (documentation "Start the RPC IDMAP daemon.")
  200. (requirement '(rpcbind-daemon rpc-pipefs))
  201. (provision '(idmap-daemon))
  202. (start #~(make-forkexec-constructor #$idmap-command))
  203. (stop #~(make-kill-destructor))))))
  204. (service-type
  205. (name 'idmap)
  206. (extensions
  207. (list (service-extension shepherd-root-service-type
  208. (compose list proc))))
  209. ;; We use the extensions feature to allow other services to automatically
  210. ;; configure and start this service. Only one value can be provided. We
  211. ;; override it with the value returned by the extending service.
  212. (compose identity)
  213. (extend (lambda (config values) (first values)))
  214. (default-value (idmap-configuration))
  215. (description "Run the idmap daemon, which provides a mapping between user
  216. IDs and user names. It is typically required to access file systems mounted
  217. via NFSv4."))))
  218. (define-record-type* <nfs-configuration>
  219. nfs-configuration make-nfs-configuration
  220. nfs-configuration?
  221. (nfs-utils nfs-configuration-nfs-utils
  222. (default nfs-utils))
  223. (nfs-versions nfs-configuration-nfs-versions
  224. (default '("4.2" "4.1" "4.0")))
  225. (exports nfs-configuration-exports
  226. (default '()))
  227. (rpcmountd-port nfs-configuration-rpcmountd-port
  228. (default #f))
  229. (rpcstatd-port nfs-configuration-rpcstatd-port
  230. (default #f))
  231. (rpcbind nfs-configuration-rpcbind
  232. (default rpcbind))
  233. (idmap-domain nfs-configuration-idmap-domain
  234. (default "localdomain"))
  235. (nfsd-port nfs-configuration-nfsd-port
  236. (default 2049))
  237. (nfsd-threads nfs-configuration-nfsd-threads
  238. (default 8))
  239. (nfsd-tcp? nfs-configuration-nfsd-tcp?
  240. (default #t))
  241. (nfsd-udp? nfs-configuration-nfsd-udp?
  242. (default #f))
  243. (pipefs-directory nfs-configuration-pipefs-directory
  244. (default default-pipefs-directory))
  245. ;; List of modules to debug; any of nfsd, nfs, rpc, idmap, statd, or mountd.
  246. (debug nfs-configuration-debug
  247. (default '())))
  248. (define (nfs-shepherd-services config)
  249. "Return a list of <shepherd-service> for the NFS daemons with CONFIG."
  250. (match-record config <nfs-configuration>
  251. (nfs-utils nfs-versions exports
  252. rpcmountd-port rpcstatd-port nfsd-port nfsd-threads
  253. nfsd-tcp? nfsd-udp?
  254. pipefs-directory debug)
  255. (list (shepherd-service
  256. (documentation "Mount the nfsd pseudo file system.")
  257. (provision '(/proc/fs/nfsd))
  258. (start #~(lambda ()
  259. (mount "nfsd" "/proc/fs/nfsd" "nfsd")
  260. (member "/proc/fs/nfsd" (mount-points))))
  261. (stop #~(lambda (pid . args)
  262. (umount "/proc/fs/nfsd" MNT_DETACH)
  263. (not (member "/proc/fs/nfsd" (mount-points))))))
  264. (shepherd-service
  265. (documentation "Run the NFS statd daemon.")
  266. (provision '(rpc.statd))
  267. (requirement '(/proc/fs/nfsd rpcbind-daemon))
  268. (start
  269. #~(make-forkexec-constructor
  270. (list #$(file-append nfs-utils "/sbin/rpc.statd")
  271. ;; TODO: notification support may require a little more
  272. ;; configuration work.
  273. "--no-notify"
  274. #$@(if (member 'statd debug)
  275. '("--no-syslog") ; verbose logging to stderr
  276. '())
  277. "--foreground"
  278. #$@(if rpcstatd-port
  279. #~("--port" #$(number->string rpcstatd-port))
  280. '()))
  281. #:pid-file "/var/run/rpc.statd.pid"))
  282. (stop #~(make-kill-destructor)))
  283. (shepherd-service
  284. (documentation "Run the NFS mountd daemon.")
  285. (provision '(rpc.mountd))
  286. (requirement '(/proc/fs/nfsd rpc.statd))
  287. (start
  288. #~(make-forkexec-constructor
  289. (list #$(file-append nfs-utils "/sbin/rpc.mountd")
  290. "--foreground"
  291. #$@(if (member 'mountd debug)
  292. '("--debug" "all")
  293. '())
  294. #$@(if rpcmountd-port
  295. #~("--port" #$(number->string rpcmountd-port))
  296. '()))))
  297. (stop #~(make-kill-destructor)))
  298. (shepherd-service
  299. (documentation "Run the NFS daemon.")
  300. (provision '(rpc.nfsd))
  301. (requirement '(/proc/fs/nfsd rpc.statd networking))
  302. (start
  303. #~(lambda _
  304. (zero? (apply system* #$(file-append nfs-utils "/sbin/rpc.nfsd")
  305. (list
  306. #$@(if (member 'nfsd debug)
  307. '("--debug")
  308. '())
  309. "--port" #$(number->string nfsd-port)
  310. #$@(map (lambda (version)
  311. (string-append "--nfs-version=" version))
  312. nfs-versions)
  313. #$(number->string nfsd-threads)
  314. #$(if nfsd-tcp?
  315. "--tcp"
  316. "--no-tcp")
  317. #$(if nfsd-udp?
  318. "--udp"
  319. "--no-udp"))))))
  320. (stop
  321. #~(lambda _
  322. (zero?
  323. (system* #$(file-append nfs-utils "/sbin/rpc.nfsd") "0")))))
  324. (shepherd-service
  325. (documentation "Run the NFS mountd daemon and refresh exports.")
  326. (provision '(nfs))
  327. (requirement '(/proc/fs/nfsd rpc.nfsd rpc.mountd rpc.statd rpcbind-daemon))
  328. (start
  329. #~(lambda _
  330. (let ((rpcdebug #$(file-append nfs-utils "/sbin/rpcdebug")))
  331. (cond
  332. ((member 'nfsd '#$debug)
  333. (system* rpcdebug "-m" "nfsd" "-s" "all"))
  334. ((member 'nfs '#$debug)
  335. (system* rpcdebug "-m" "nfs" "-s" "all"))
  336. ((member 'rpc '#$debug)
  337. (system* rpcdebug "-m" "rpc" "-s" "all"))))
  338. (zero? (system*
  339. #$(file-append nfs-utils "/sbin/exportfs")
  340. "-r" ; re-export
  341. "-a" ; everthing
  342. "-v" ; be verbose
  343. "-d" "all" ; debug
  344. ))))
  345. (stop
  346. #~(lambda _
  347. (let ((rpcdebug #$(file-append nfs-utils "/sbin/rpcdebug")))
  348. (cond
  349. ((member 'nfsd '#$debug)
  350. (system* rpcdebug "-m" "nfsd" "-c" "all"))
  351. ((member 'nfs '#$debug)
  352. (system* rpcdebug "-m" "nfs" "-c" "all"))
  353. ((member 'rpc '#$debug)
  354. (system* rpcdebug "-m" "rpc" "-c" "all"))))
  355. #t))
  356. (respawn? #f)))))
  357. (define %nfs-activation
  358. (with-imported-modules '((guix build utils))
  359. #~(begin
  360. (use-modules (guix build utils))
  361. ;; directory containing monitor list
  362. (mkdir-p "/var/lib/nfs/sm")
  363. ;; Needed for client recovery tracking
  364. (mkdir-p "/var/lib/nfs/v4recovery")
  365. (let ((user (getpw "nobody")))
  366. (chown "/var/lib/nfs"
  367. (passwd:uid user)
  368. (passwd:gid user))
  369. (chown "/var/lib/nfs/v4recovery"
  370. (passwd:uid user)
  371. (passwd:gid user)))
  372. #t)))
  373. (define nfs-service-type
  374. (service-type
  375. (name 'nfs)
  376. (extensions
  377. (list
  378. (service-extension shepherd-root-service-type nfs-shepherd-services)
  379. (service-extension activation-service-type (const %nfs-activation))
  380. (service-extension etc-service-type
  381. (lambda (config)
  382. `(("exports"
  383. ,(plain-file "exports"
  384. (string-join
  385. (map string-join
  386. (nfs-configuration-exports config))
  387. "\n"))))))
  388. ;; The NFS service depends on these other services. They are extended so
  389. ;; that users don't need to configure them manually.
  390. (service-extension idmap-service-type
  391. (lambda (config)
  392. (idmap-configuration
  393. (domain (nfs-configuration-idmap-domain config))
  394. (verbosity
  395. (if (member 'idmap (nfs-configuration-debug config))
  396. 10 0))
  397. (pipefs-directory (nfs-configuration-pipefs-directory config))
  398. (nfs-utils (nfs-configuration-nfs-utils config)))))
  399. (service-extension pipefs-service-type
  400. (lambda (config)
  401. (pipefs-configuration
  402. (mount-point (nfs-configuration-pipefs-directory config)))))
  403. (service-extension rpcbind-service-type
  404. (lambda (config)
  405. (rpcbind-configuration
  406. (rpcbind (nfs-configuration-rpcbind config)))))))
  407. (description
  408. "Run all NFS daemons and refresh the list of exported file systems.")))