nfs.scm 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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. (define-record-type* <pipefs-configuration>
  83. pipefs-configuration make-pipefs-configuration
  84. pipefs-configuration?
  85. (mount-point pipefs-configuration-mount-point
  86. (default default-pipefs-directory)))
  87. (define pipefs-service-type
  88. (let ((proc
  89. (lambda (config)
  90. (define pipefs-directory (pipefs-configuration-mount-point config))
  91. (shepherd-service
  92. (documentation "Mount the pipefs pseudo file system.")
  93. (provision '(rpc-pipefs))
  94. (start #~(lambda ()
  95. (mkdir-p #$pipefs-directory)
  96. (mount "rpc_pipefs" #$pipefs-directory "rpc_pipefs")
  97. (member #$pipefs-directory (mount-points))))
  98. (stop #~(lambda (pid . args)
  99. (umount #$pipefs-directory MNT_DETACH)
  100. (not (member #$pipefs-directory (mount-points)))))))))
  101. (service-type
  102. (name 'pipefs)
  103. (extensions
  104. (list (service-extension shepherd-root-service-type
  105. (compose list proc))))
  106. ;; We use the extensions feature to allow other services to automatically
  107. ;; configure and start this service. Only one value can be provided. We
  108. ;; override it with the value returned by the extending service.
  109. (compose identity)
  110. (extend (lambda (config values) (first values)))
  111. (default-value (pipefs-configuration)))))
  112. (define-record-type* <gss-configuration>
  113. gss-configuration make-gss-configuration
  114. gss-configuration?
  115. (pipefs-directory gss-configuration-pipefs-directory
  116. (default default-pipefs-directory))
  117. (nfs-utils gss-configuration-gss
  118. (default nfs-utils)))
  119. (define gss-service-type
  120. (let ((proc
  121. (lambda (config)
  122. (define nfs-utils
  123. (gss-configuration-gss config))
  124. (define pipefs-directory
  125. (gss-configuration-pipefs-directory config))
  126. (define gss-command
  127. #~(list (string-append #$nfs-utils "/sbin/rpc.gssd") "-f"
  128. "-p" #$pipefs-directory))
  129. (shepherd-service
  130. (documentation "Start the RPC GSS daemon.")
  131. (requirement '(rpcbind-daemon rpc-pipefs))
  132. (provision '(gss-daemon))
  133. (start #~(make-forkexec-constructor #$gss-command))
  134. (stop #~(make-kill-destructor))))))
  135. (service-type
  136. (name 'gss)
  137. (extensions
  138. (list (service-extension shepherd-root-service-type
  139. (compose list proc))))
  140. ;; We use the extensions feature to allow other services to automatically
  141. ;; configure and start this service. Only one value can be provided. We
  142. ;; override it with the value returned by the extending service.
  143. (compose identity)
  144. (extend (lambda (config values)
  145. (match values
  146. ((first . rest) first)
  147. (_ config))))
  148. (default-value (gss-configuration)))))
  149. (define-record-type* <idmap-configuration>
  150. idmap-configuration make-idmap-configuration
  151. idmap-configuration?
  152. (pipefs-directory idmap-configuration-pipefs-directory
  153. (default default-pipefs-directory))
  154. (domain idmap-configuration-domain
  155. (default #f))
  156. (nfs-utils idmap-configuration-nfs-utils
  157. (default nfs-utils))
  158. (verbosity idmap-configuration-verbosity
  159. (default 0)))
  160. (define idmap-service-type
  161. (let ((proc
  162. (lambda (config)
  163. (define nfs-utils
  164. (idmap-configuration-nfs-utils config))
  165. (define pipefs-directory
  166. (idmap-configuration-pipefs-directory config))
  167. (define domain (idmap-configuration-domain config))
  168. (define (idmap-config-file config)
  169. (plain-file "idmapd.conf"
  170. (string-append
  171. "\n[General]\n"
  172. "Verbosity = "
  173. (number->string
  174. (idmap-configuration-verbosity config))
  175. "\n"
  176. (if domain
  177. (format #f "Domain = ~a\n" domain)
  178. "")
  179. "\n[Mapping]\n"
  180. "Nobody-User = nobody\n"
  181. "Nobody-Group = nogroup\n")))
  182. (define idmap-command
  183. #~(list (string-append #$nfs-utils "/sbin/rpc.idmapd") "-f"
  184. "-p" #$pipefs-directory
  185. ;; TODO: this is deprecated
  186. "-c" #$(idmap-config-file config)))
  187. (shepherd-service
  188. (documentation "Start the RPC IDMAP daemon.")
  189. (requirement '(rpcbind-daemon rpc-pipefs))
  190. (provision '(idmap-daemon))
  191. (start #~(make-forkexec-constructor #$idmap-command))
  192. (stop #~(make-kill-destructor))))))
  193. (service-type
  194. (name 'idmap)
  195. (extensions
  196. (list (service-extension shepherd-root-service-type
  197. (compose list proc))))
  198. ;; We use the extensions feature to allow other services to automatically
  199. ;; configure and start this service. Only one value can be provided. We
  200. ;; override it with the value returned by the extending service.
  201. (compose identity)
  202. (extend (lambda (config values) (first values)))
  203. (default-value (idmap-configuration)))))
  204. (define-record-type* <nfs-configuration>
  205. nfs-configuration make-nfs-configuration
  206. nfs-configuration?
  207. (nfs-utils nfs-configuration-nfs-utils
  208. (default nfs-utils))
  209. (nfs-versions nfs-configuration-nfs-versions
  210. (default '("4.2" "4.1" "4.0")))
  211. (exports nfs-configuration-exports
  212. (default '()))
  213. (rpcmountd-port nfs-configuration-rpcmountd-port
  214. (default #f))
  215. (rpcstatd-port nfs-configuration-rpcstatd-port
  216. (default #f))
  217. (rpcbind nfs-configuration-rpcbind
  218. (default rpcbind))
  219. (idmap-domain nfs-configuration-idmap-domain
  220. (default "localdomain"))
  221. (nfsd-port nfs-configuration-nfsd-port
  222. (default 2049))
  223. (nfsd-threads nfs-configuration-nfsd-threads
  224. (default 8))
  225. (nfsd-tcp? nfs-configuration-nfsd-tcp?
  226. (default #t))
  227. (nfsd-udp? nfs-configuration-nfsd-udp?
  228. (default #f))
  229. (pipefs-directory nfs-configuration-pipefs-directory
  230. (default default-pipefs-directory))
  231. ;; List of modules to debug; any of nfsd, nfs, rpc, idmap, statd, or mountd.
  232. (debug nfs-configuration-debug
  233. (default '())))
  234. (define (nfs-shepherd-services config)
  235. "Return a list of <shepherd-service> for the NFS daemons with CONFIG."
  236. (match-record config <nfs-configuration>
  237. (nfs-utils nfs-versions exports
  238. rpcmountd-port rpcstatd-port nfsd-port nfsd-threads
  239. nfsd-tcp? nfsd-udp?
  240. pipefs-directory debug)
  241. (list (shepherd-service
  242. (documentation "Mount the nfsd pseudo file system.")
  243. (provision '(/proc/fs/nfsd))
  244. (start #~(lambda ()
  245. (mount "nfsd" "/proc/fs/nfsd" "nfsd")
  246. (member "/proc/fs/nfsd" (mount-points))))
  247. (stop #~(lambda (pid . args)
  248. (umount "/proc/fs/nfsd" MNT_DETACH)
  249. (not (member "/proc/fs/nfsd" (mount-points))))))
  250. (shepherd-service
  251. (documentation "Run the NFS statd daemon.")
  252. (provision '(rpc.statd))
  253. (requirement '(/proc/fs/nfsd rpcbind-daemon))
  254. (start
  255. #~(make-forkexec-constructor
  256. (list #$(file-append nfs-utils "/sbin/rpc.statd")
  257. ;; TODO: notification support may require a little more
  258. ;; configuration work.
  259. "--no-notify"
  260. #$@(if (member 'statd debug)
  261. '("--no-syslog") ; verbose logging to stderr
  262. '())
  263. "--foreground"
  264. #$@(if rpcstatd-port
  265. '("--port" (number->string rpcstatd-port))
  266. '()))
  267. #:pid-file "/var/run/rpc.statd.pid"))
  268. (stop #~(make-kill-destructor)))
  269. (shepherd-service
  270. (documentation "Run the NFS mountd daemon.")
  271. (provision '(rpc.mountd))
  272. (requirement '(/proc/fs/nfsd rpc.statd))
  273. (start
  274. #~(make-forkexec-constructor
  275. (list #$(file-append nfs-utils "/sbin/rpc.mountd")
  276. "--foreground"
  277. #$@(if (member 'mountd debug)
  278. '("--debug" "all")
  279. '())
  280. #$@(if rpcmountd-port
  281. '("--port" (number->string rpcmountd-port))
  282. '()))))
  283. (stop #~(make-kill-destructor)))
  284. (shepherd-service
  285. (documentation "Run the NFS daemon.")
  286. (provision '(rpc.nfsd))
  287. (requirement '(/proc/fs/nfsd rpc.statd networking))
  288. (start
  289. #~(lambda _
  290. (zero? (apply system* #$(file-append nfs-utils "/sbin/rpc.nfsd")
  291. (list
  292. #$@(if (member 'nfsd debug)
  293. '("--debug")
  294. '())
  295. "--port" #$(number->string nfsd-port)
  296. #$@(map (lambda (version)
  297. (string-append "--nfs-version=" version))
  298. nfs-versions)
  299. #$(number->string nfsd-threads)
  300. #$(if nfsd-tcp?
  301. "--tcp"
  302. "--no-tcp")
  303. #$(if nfsd-udp?
  304. "--udp"
  305. "--no-udp"))))))
  306. (stop
  307. #~(lambda _
  308. (zero?
  309. (system* #$(file-append nfs-utils "/sbin/rpc.nfsd") "0")))))
  310. (shepherd-service
  311. (documentation "Run the NFS mountd daemon and refresh exports.")
  312. (provision '(nfs))
  313. (requirement '(/proc/fs/nfsd rpc.nfsd rpc.mountd rpc.statd rpcbind-daemon))
  314. (start
  315. #~(lambda _
  316. (let ((rpcdebug #$(file-append nfs-utils "/sbin/rpcdebug")))
  317. (cond
  318. ((member 'nfsd '#$debug)
  319. (system* rpcdebug "-m" "nfsd" "-s" "all"))
  320. ((member 'nfs '#$debug)
  321. (system* rpcdebug "-m" "nfs" "-s" "all"))
  322. ((member 'rpc '#$debug)
  323. (system* rpcdebug "-m" "rpc" "-s" "all"))))
  324. (zero? (system*
  325. #$(file-append nfs-utils "/sbin/exportfs")
  326. "-r" ; re-export
  327. "-a" ; everthing
  328. "-v" ; be verbose
  329. "-d" "all" ; debug
  330. ))))
  331. (stop
  332. #~(lambda _
  333. (let ((rpcdebug #$(file-append nfs-utils "/sbin/rpcdebug")))
  334. (cond
  335. ((member 'nfsd '#$debug)
  336. (system* rpcdebug "-m" "nfsd" "-c" "all"))
  337. ((member 'nfs '#$debug)
  338. (system* rpcdebug "-m" "nfs" "-c" "all"))
  339. ((member 'rpc '#$debug)
  340. (system* rpcdebug "-m" "rpc" "-c" "all"))))
  341. #t))
  342. (respawn? #f)))))
  343. (define %nfs-activation
  344. (with-imported-modules '((guix build utils))
  345. #~(begin
  346. (use-modules (guix build utils))
  347. ;; directory containing monitor list
  348. (mkdir-p "/var/lib/nfs/sm")
  349. ;; Needed for client recovery tracking
  350. (mkdir-p "/var/lib/nfs/v4recovery")
  351. (let ((user (getpw "nobody")))
  352. (chown "/var/lib/nfs"
  353. (passwd:uid user)
  354. (passwd:gid user))
  355. (chown "/var/lib/nfs/v4recovery"
  356. (passwd:uid user)
  357. (passwd:gid user)))
  358. #t)))
  359. (define nfs-service-type
  360. (service-type
  361. (name 'nfs)
  362. (extensions
  363. (list
  364. (service-extension shepherd-root-service-type nfs-shepherd-services)
  365. (service-extension activation-service-type (const %nfs-activation))
  366. (service-extension etc-service-type
  367. (lambda (config)
  368. `(("exports"
  369. ,(plain-file "exports"
  370. (string-join
  371. (map string-join
  372. (nfs-configuration-exports config))
  373. "\n"))))))
  374. ;; The NFS service depends on these other services. They are extended so
  375. ;; that users don't need to configure them manually.
  376. (service-extension idmap-service-type
  377. (lambda (config)
  378. (idmap-configuration
  379. (domain (nfs-configuration-idmap-domain config))
  380. (verbosity
  381. (if (member 'idmap (nfs-configuration-debug config))
  382. 10 0))
  383. (pipefs-directory (nfs-configuration-pipefs-directory config))
  384. (nfs-utils (nfs-configuration-nfs-utils config)))))
  385. (service-extension pipefs-service-type
  386. (lambda (config)
  387. (pipefs-configuration
  388. (mount-point (nfs-configuration-pipefs-directory config)))))
  389. (service-extension rpcbind-service-type
  390. (lambda (config)
  391. (rpcbind-configuration
  392. (rpcbind (nfs-configuration-rpcbind config)))))))
  393. (description
  394. "Run all NFS daemons and refresh the list of exported file systems.")))