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>
5 ;;; This file is part of GNU Guix.
7 ;;; GNU Guix is free software; you can redistribute it and/or modify it
8 ;;; under the terms of the GNU General Public License as published by
9 ;;; the Free Software Foundation; either version 3 of the License, or (at
10 ;;; your option) any later version.
12 ;;; GNU Guix is distributed in the hope that it will be useful, but
13 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;;; GNU General Public License for more details.
17 ;;; You should have received a copy of the GNU General Public License
18 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
20 (define-module (gnu services nfs)
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)
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
33 rpcbind-configuration?
52 (define default-pipefs-directory "/var/lib/nfs/rpc_pipefs")
56 (define-record-type* <rpcbind-configuration>
57 rpcbind-configuration make-rpcbind-configuration
58 rpcbind-configuration?
59 (rpcbind rpcbind-configuration-rpcbind
61 (warm-start? rpcbind-configuration-warm-start?
64 (define rpcbind-service-type
68 (rpcbind-configuration-rpcbind config))
70 (define rpcbind-command
71 #~(list (string-append #$rpcbind "/bin/rpcbind") "-f"
72 #$@(if (rpcbind-configuration-warm-start? config) '("-w") '())))
75 (documentation "Start the RPC bind daemon.")
76 (requirement '(networking))
77 (provision '(rpcbind-daemon))
79 (start #~(make-forkexec-constructor #$rpcbind-command))
80 (stop #~(make-kill-destructor))))))
84 (list (service-extension shepherd-root-service-type
85 (compose list proc))))
86 ;; We use the extensions feature to allow other services to automatically
87 ;; configure and start this service. Only one value can be provided. We
88 ;; override it with the value returned by the extending service.
90 (extend (lambda (config values)
92 ((first . rest) first)
94 (default-value (rpcbind-configuration)))))
98 (define-record-type* <pipefs-configuration>
99 pipefs-configuration make-pipefs-configuration
100 pipefs-configuration?
101 (mount-point pipefs-configuration-mount-point
102 (default default-pipefs-directory)))
104 (define pipefs-service-type
107 (define pipefs-directory (pipefs-configuration-mount-point config))
110 (documentation "Mount the pipefs pseudo file system.")
111 (provision '(rpc-pipefs))
114 (mkdir-p #$pipefs-directory)
115 (mount "rpc_pipefs" #$pipefs-directory "rpc_pipefs")
116 (member #$pipefs-directory (mount-points))))
118 (stop #~(lambda (pid . args)
119 (umount #$pipefs-directory MNT_DETACH)
120 (not (member #$pipefs-directory (mount-points)))))))))
124 (list (service-extension shepherd-root-service-type
125 (compose list proc))))
126 ;; We use the extensions feature to allow other services to automatically
127 ;; configure and start this service. Only one value can be provided. We
128 ;; override it with the value returned by the extending service.
130 (extend (lambda (config values) (first values)))
131 (default-value (pipefs-configuration)))))
135 (define-record-type* <gss-configuration>
136 gss-configuration make-gss-configuration
138 (pipefs-directory gss-configuration-pipefs-directory
139 (default default-pipefs-directory))
140 (nfs-utils gss-configuration-gss
141 (default nfs-utils)))
143 (define gss-service-type
147 (gss-configuration-gss config))
149 (define pipefs-directory
150 (gss-configuration-pipefs-directory config))
153 #~(list (string-append #$nfs-utils "/sbin/rpc.gssd") "-f"
154 "-p" #$pipefs-directory))
157 (documentation "Start the RPC GSS daemon.")
158 (requirement '(rpcbind-daemon rpc-pipefs))
159 (provision '(gss-daemon))
161 (start #~(make-forkexec-constructor #$gss-command))
162 (stop #~(make-kill-destructor))))))
166 (list (service-extension shepherd-root-service-type
167 (compose list proc))))
168 ;; We use the extensions feature to allow other services to automatically
169 ;; configure and start this service. Only one value can be provided. We
170 ;; override it with the value returned by the extending service.
172 (extend (lambda (config values)
174 ((first . rest) first)
176 (default-value (gss-configuration)))))
180 (define-record-type* <idmap-configuration>
181 idmap-configuration make-idmap-configuration
183 (pipefs-directory idmap-configuration-pipefs-directory
184 (default default-pipefs-directory))
185 (domain idmap-configuration-domain
187 (nfs-utils idmap-configuration-nfs-utils
189 (verbosity idmap-configuration-verbosity
192 (define idmap-service-type
197 (idmap-configuration-nfs-utils config))
199 (define pipefs-directory
200 (idmap-configuration-pipefs-directory config))
202 (define domain (idmap-configuration-domain config))
204 (define (idmap-config-file config)
205 (plain-file "idmapd.conf"
210 (idmap-configuration-verbosity config))
213 (format #f "Domain = ~a\n" domain)
216 "Nobody-User = nobody\n"
217 "Nobody-Group = nogroup\n")))
219 (define idmap-command
220 #~(list (string-append #$nfs-utils "/sbin/rpc.idmapd") "-f"
221 "-p" #$pipefs-directory
222 ;; TODO: this is deprecated
223 "-c" #$(idmap-config-file config)))
226 (documentation "Start the RPC IDMAP daemon.")
227 (requirement '(rpcbind-daemon rpc-pipefs))
228 (provision '(idmap-daemon))
229 (start #~(make-forkexec-constructor #$idmap-command))
230 (stop #~(make-kill-destructor))))))
234 (list (service-extension shepherd-root-service-type
235 (compose list proc))))
236 ;; We use the extensions feature to allow other services to automatically
237 ;; configure and start this service. Only one value can be provided. We
238 ;; override it with the value returned by the extending service.
240 (extend (lambda (config values) (first values)))
241 (default-value (idmap-configuration)))))
243 (define-record-type* <nfs-configuration>
244 nfs-configuration make-nfs-configuration
246 (nfs-utils nfs-configuration-nfs-utils
248 (nfs-version nfs-configuration-nfs-version
249 (default #f)) ; string
250 (exports nfs-configuration-exports
252 (rpcmountd-port nfs-configuration-rpcmountd-port
254 (rpcstatd-port nfs-configuration-rpcstatd-port
256 (rpcbind nfs-configuration-rpcbind
258 (idmap-domain nfs-configuration-idmap-domain
259 (default "localdomain"))
260 (nfsd-port nfs-configuration-nfsd-port
262 (nfsd-threads nfs-configuration-nfsd-threads
264 (pipefs-directory nfs-configuration-pipefs-directory
265 (default default-pipefs-directory))
266 ;; List of modules to debug; any of nfsd, nfs, rpc, idmap, statd, or mountd.
267 (debug nfs-configuration-debug
270 (define (nfs-shepherd-services config)
271 "Return a list of <shepherd-service> for the NFS daemons with CONFIG."
272 (match-record config <nfs-configuration>
273 (nfs-utils nfs-version exports
274 rpcmountd-port rpcstatd-port nfsd-port nfsd-threads
275 pipefs-directory debug)
276 (list (shepherd-service
277 (documentation "Run the NFS statd daemon.")
278 (provision '(rpc.statd))
279 (requirement '(rpcbind-daemon))
281 #~(make-forkexec-constructor
282 (list #$(file-append nfs-utils "/sbin/rpc.statd")
283 ;; TODO: notification support may require a little more
284 ;; configuration work.
286 #$@(if (member 'statd debug)
287 '("--no-syslog") ; verbose logging to stderr
291 '("--port" (number->string rpcstatd-port))
293 #:pid-file "/var/run/rpc.statd.pid"))
294 (stop #~(make-kill-destructor)))
296 (documentation "Run the NFS mountd daemon.")
297 (provision '(rpc.mountd))
298 (requirement '(rpc.statd))
300 #~(make-forkexec-constructor
301 (list #$(file-append nfs-utils "/sbin/rpc.mountd")
302 #$@(if (member 'mountd debug)
305 #$@(if rpcmountd-port
306 '("--port" (number->string rpcmountd-port))
308 (stop #~(make-kill-destructor)))
310 (documentation "Run the NFS daemon.")
311 (provision '(rpc.nfsd))
312 (requirement '(rpc.statd networking))
315 (zero? (system* #$(file-append nfs-utils "/sbin/rpc.nfsd")
316 #$@(if (member 'nfsd debug)
319 "--port" #$(number->string nfsd-port)
321 '("--nfs-version" nfs-version)
323 #$(number->string nfsd-threads)))))
327 (system* #$(file-append nfs-utils "/sbin/rpc.nfsd") "0")))))
329 (documentation "Run the NFS mountd daemon and refresh exports.")
331 (requirement '(rpc.nfsd rpc.mountd rpc.statd rpcbind-daemon))
334 (let ((rpcdebug #$(file-append nfs-utils "/sbin/rpcdebug")))
336 ((member 'nfsd '#$debug)
337 (system* rpcdebug "-m" "nfsd" "-s" "all"))
338 ((member 'nfs '#$debug)
339 (system* rpcdebug "-m" "nfs" "-s" "all"))
340 ((member 'rpc '#$debug)
341 (system* rpcdebug "-m" "rpc" "-s" "all"))))
343 #$(file-append nfs-utils "/sbin/exportfs")
351 (let ((rpcdebug #$(file-append nfs-utils "/sbin/rpcdebug")))
353 ((member 'nfsd '#$debug)
354 (system* rpcdebug "-m" "nfsd" "-c" "all"))
355 ((member 'nfs '#$debug)
356 (system* rpcdebug "-m" "nfs" "-c" "all"))
357 ((member 'rpc '#$debug)
358 (system* rpcdebug "-m" "rpc" "-c" "all"))))
362 (define nfs-service-type
367 (service-extension shepherd-root-service-type nfs-shepherd-services)
368 (service-extension activation-service-type
370 (use-modules (guix build utils))
371 (system* "mount" "-t" "nfsd"
372 "nfsd" "/proc/fs/nfsd")
374 (mkdir-p "/var/lib/nfs")
375 ;; directory containing monitor list
376 (mkdir-p "/var/lib/nfs/sm")
377 ;; Needed for client recovery tracking
378 (mkdir-p "/var/lib/nfs/v4recovery")
379 (let ((user (getpw "nobody")))
380 (chown "/var/lib/nfs"
383 (chown "/var/lib/nfs/v4recovery"
387 (service-extension etc-service-type
390 ,(plain-file "exports"
393 (nfs-configuration-exports config))
395 ;; The NFS service depends on these other services. They are extended so
396 ;; that users don't need to configure them manually.
397 (service-extension idmap-service-type
400 (domain (nfs-configuration-idmap-domain config))
402 (if (member 'idmap (nfs-configuration-debug config))
404 (pipefs-directory (nfs-configuration-pipefs-directory config))
405 (nfs-utils (nfs-configuration-nfs-utils config)))))
406 (service-extension pipefs-service-type
408 (pipefs-configuration
409 (mount-point (nfs-configuration-pipefs-directory config)))))
410 (service-extension rpcbind-service-type
412 (rpcbind-configuration
413 (rpcbind (nfs-configuration-rpcbind config)))))))
415 "Run all NFS daemons and refresh the list of exported file systems.")))