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>
6 ;;; This file is part of GNU Guix.
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.
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.
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/>.
21 (define-module (gnu services nfs)
23 #:use-module (gnu services shepherd)
24 #:use-module (gnu packages onc-rpc)
25 #:use-module (gnu packages linux)
26 #:use-module (gnu packages nfs)
28 #:use-module (guix records)
29 #:use-module (srfi srfi-1)
30 #:use-module (ice-9 match)
31 #:use-module (gnu build file-systems)
32 #:export (rpcbind-service-type
34 rpcbind-configuration?
53 (define default-pipefs-directory "/var/lib/nfs/rpc_pipefs")
57 (define-record-type* <rpcbind-configuration>
58 rpcbind-configuration make-rpcbind-configuration
59 rpcbind-configuration?
60 (rpcbind rpcbind-configuration-rpcbind
62 (warm-start? rpcbind-configuration-warm-start?
65 (define rpcbind-service-type
69 (rpcbind-configuration-rpcbind config))
71 (define rpcbind-command
72 #~(list (string-append #$rpcbind "/sbin/rpcbind") "-f"
73 #$@(if (rpcbind-configuration-warm-start? config) '("-w") '())))
76 (documentation "Start the RPC bind daemon.")
77 (requirement '(networking))
78 (provision '(rpcbind-daemon))
80 (start #~(make-forkexec-constructor #$rpcbind-command))
81 (stop #~(make-kill-destructor))))))
85 (list (service-extension shepherd-root-service-type
86 (compose list proc))))
87 ;; We use the extensions feature to allow other services to automatically
88 ;; configure and start this service. Only one value can be provided. We
89 ;; override it with the value returned by the extending service.
91 (extend (lambda (config values)
93 ((first . rest) first)
95 (default-value (rpcbind-configuration))
96 (description "Run the RPC Bind service, which provides a facility to map
97 ONC RPC program numbers into universal addresses. Many NFS related services
98 use this facility."))))
102 (define-record-type* <pipefs-configuration>
103 pipefs-configuration make-pipefs-configuration
104 pipefs-configuration?
105 (mount-point pipefs-configuration-mount-point
106 (default default-pipefs-directory)))
108 (define pipefs-service-type
111 (define pipefs-directory (pipefs-configuration-mount-point config))
114 (documentation "Mount the pipefs pseudo file system.")
115 (provision '(rpc-pipefs))
118 (mkdir-p #$pipefs-directory)
119 (mount "rpc_pipefs" #$pipefs-directory "rpc_pipefs")
120 (member #$pipefs-directory (mount-points))))
122 (stop #~(lambda (pid . args)
123 (umount #$pipefs-directory MNT_DETACH)
124 (not (member #$pipefs-directory (mount-points)))))))))
128 (list (service-extension shepherd-root-service-type
129 (compose list proc))))
130 ;; We use the extensions feature to allow other services to automatically
131 ;; configure and start this service. Only one value can be provided. We
132 ;; override it with the value returned by the extending service.
134 (extend (lambda (config values)
136 ((first . rest) first)
138 (default-value (pipefs-configuration))
139 (description "Mount the pipefs file system, which is used to transfer
140 NFS-related data between the kernel and user-space programs."))))
144 (define-record-type* <gss-configuration>
145 gss-configuration make-gss-configuration
147 (pipefs-directory gss-configuration-pipefs-directory
148 (default default-pipefs-directory))
149 (nfs-utils gss-configuration-gss
150 (default nfs-utils)))
152 (define gss-service-type
156 (gss-configuration-gss config))
158 (define pipefs-directory
159 (gss-configuration-pipefs-directory config))
162 #~(list (string-append #$nfs-utils "/sbin/rpc.gssd") "-f"
163 "-p" #$pipefs-directory))
166 (documentation "Start the RPC GSS daemon.")
167 (requirement '(rpcbind-daemon rpc-pipefs))
168 (provision '(gss-daemon))
170 (start #~(make-forkexec-constructor #$gss-command))
171 (stop #~(make-kill-destructor))))))
175 (list (service-extension shepherd-root-service-type
176 (compose list proc))))
177 ;; We use the extensions feature to allow other services to automatically
178 ;; configure and start this service. Only one value can be provided. We
179 ;; override it with the value returned by the extending service.
181 (extend (lambda (config values)
183 ((first . rest) first)
185 (default-value (gss-configuration))
186 (description "Run the @dfn{global security system} (GSS) daemon, which
187 provides strong security for protocols based on remote procedure calls (ONC
192 (define-record-type* <idmap-configuration>
193 idmap-configuration make-idmap-configuration
195 (pipefs-directory idmap-configuration-pipefs-directory
196 (default default-pipefs-directory))
197 (domain idmap-configuration-domain
199 (nfs-utils idmap-configuration-nfs-utils
201 (verbosity idmap-configuration-verbosity
204 (define idmap-service-type
209 (idmap-configuration-nfs-utils config))
211 (define pipefs-directory
212 (idmap-configuration-pipefs-directory config))
214 (define domain (idmap-configuration-domain config))
216 (define (idmap-config-file config)
217 (plain-file "idmapd.conf"
222 (idmap-configuration-verbosity config))
225 (format #f "Domain = ~a\n" domain)
228 "Nobody-User = nobody\n"
229 "Nobody-Group = nogroup\n")))
231 (define idmap-command
232 #~(list (string-append #$nfs-utils "/sbin/rpc.idmapd") "-f"
233 "-p" #$pipefs-directory
234 ;; TODO: this is deprecated
235 "-c" #$(idmap-config-file config)))
238 (documentation "Start the RPC IDMAP daemon.")
239 (requirement '(rpcbind-daemon rpc-pipefs))
240 (provision '(idmap-daemon))
241 (start #~(make-forkexec-constructor #$idmap-command))
242 (stop #~(make-kill-destructor))))))
246 (list (service-extension shepherd-root-service-type
247 (compose list proc))))
248 ;; We use the extensions feature to allow other services to automatically
249 ;; configure and start this service. Only one value can be provided. We
250 ;; override it with the value returned by the extending service.
252 (extend (lambda (config values) (first values)))
253 (default-value (idmap-configuration))
254 (description "Run the idmap daemon, which provides a mapping between user
255 IDs and user names. It is typically required to access file systems mounted
258 (define-record-type* <nfs-configuration>
259 nfs-configuration make-nfs-configuration
261 (nfs-utils nfs-configuration-nfs-utils
263 (nfs-versions nfs-configuration-nfs-versions
264 (default '("4.2" "4.1" "4.0")))
265 (exports nfs-configuration-exports
267 (rpcmountd-port nfs-configuration-rpcmountd-port
269 (rpcstatd-port nfs-configuration-rpcstatd-port
271 (rpcbind nfs-configuration-rpcbind
273 (idmap-domain nfs-configuration-idmap-domain
274 (default "localdomain"))
275 (nfsd-port nfs-configuration-nfsd-port
277 (nfsd-threads nfs-configuration-nfsd-threads
279 (nfsd-tcp? nfs-configuration-nfsd-tcp?
281 (nfsd-udp? nfs-configuration-nfsd-udp?
283 (pipefs-directory nfs-configuration-pipefs-directory
284 (default default-pipefs-directory))
285 ;; List of modules to debug; any of nfsd, nfs, rpc, idmap, statd, or mountd.
286 (debug nfs-configuration-debug
289 (define (nfs-shepherd-services config)
290 "Return a list of <shepherd-service> for the NFS daemons with CONFIG."
291 (match-record config <nfs-configuration>
292 (nfs-utils nfs-versions exports
293 rpcmountd-port rpcstatd-port nfsd-port nfsd-threads
295 pipefs-directory debug)
296 (list (shepherd-service
297 (documentation "Mount the nfsd pseudo file system.")
298 (provision '(/proc/fs/nfsd))
300 (mount "nfsd" "/proc/fs/nfsd" "nfsd")
301 (member "/proc/fs/nfsd" (mount-points))))
303 (stop #~(lambda (pid . args)
304 (umount "/proc/fs/nfsd" MNT_DETACH)
305 (not (member "/proc/fs/nfsd" (mount-points))))))
307 (documentation "Run the NFS statd daemon.")
308 (provision '(rpc.statd))
309 (requirement '(/proc/fs/nfsd rpcbind-daemon))
311 #~(make-forkexec-constructor
312 (list #$(file-append nfs-utils "/sbin/rpc.statd")
313 ;; TODO: notification support may require a little more
314 ;; configuration work.
316 #$@(if (member 'statd debug)
317 '("--no-syslog") ; verbose logging to stderr
321 #~("--port" #$(number->string rpcstatd-port))
323 #:pid-file "/var/run/rpc.statd.pid"))
324 (stop #~(make-kill-destructor)))
326 (documentation "Run the NFS mountd daemon.")
327 (provision '(rpc.mountd))
328 (requirement '(/proc/fs/nfsd rpc.statd))
330 #~(make-forkexec-constructor
331 (list #$(file-append nfs-utils "/sbin/rpc.mountd")
333 #$@(if (member 'mountd debug)
336 #$@(if rpcmountd-port
337 #~("--port" #$(number->string rpcmountd-port))
339 (stop #~(make-kill-destructor)))
341 (documentation "Run the NFS daemon.")
342 (provision '(rpc.nfsd))
343 (requirement '(/proc/fs/nfsd rpc.statd networking))
346 (zero? (apply system* #$(file-append nfs-utils "/sbin/rpc.nfsd")
348 #$@(if (member 'nfsd debug)
351 "--port" #$(number->string nfsd-port)
352 #$@(map (lambda (version)
353 (string-append "--nfs-version=" version))
355 #$(number->string nfsd-threads)
365 (system* #$(file-append nfs-utils "/sbin/rpc.nfsd") "0")))))
367 (documentation "Run the NFS mountd daemon and refresh exports.")
369 (requirement '(/proc/fs/nfsd rpc.nfsd rpc.mountd rpc.statd rpcbind-daemon))
372 (let ((rpcdebug #$(file-append nfs-utils "/sbin/rpcdebug")))
374 ((member 'nfsd '#$debug)
375 (system* rpcdebug "-m" "nfsd" "-s" "all"))
376 ((member 'nfs '#$debug)
377 (system* rpcdebug "-m" "nfs" "-s" "all"))
378 ((member 'rpc '#$debug)
379 (system* rpcdebug "-m" "rpc" "-s" "all"))))
381 #$(file-append nfs-utils "/sbin/exportfs")
389 (let ((rpcdebug #$(file-append nfs-utils "/sbin/rpcdebug")))
391 ((member 'nfsd '#$debug)
392 (system* rpcdebug "-m" "nfsd" "-c" "all"))
393 ((member 'nfs '#$debug)
394 (system* rpcdebug "-m" "nfs" "-c" "all"))
395 ((member 'rpc '#$debug)
396 (system* rpcdebug "-m" "rpc" "-c" "all"))))
400 (define %nfs-activation
401 (with-imported-modules '((guix build utils))
403 (use-modules (guix build utils))
405 ;; directory containing monitor list
406 (mkdir-p "/var/lib/nfs/sm")
407 ;; Needed for client recovery tracking
408 (mkdir-p "/var/lib/nfs/v4recovery")
409 (let ((user (getpw "nobody")))
410 (chown "/var/lib/nfs"
413 (chown "/var/lib/nfs/v4recovery"
418 (define nfs-service-type
423 (service-extension shepherd-root-service-type nfs-shepherd-services)
424 (service-extension activation-service-type (const %nfs-activation))
425 (service-extension etc-service-type
428 ,(plain-file "exports"
431 (nfs-configuration-exports config))
433 ;; The NFS service depends on these other services. They are extended so
434 ;; that users don't need to configure them manually.
435 (service-extension idmap-service-type
438 (domain (nfs-configuration-idmap-domain config))
440 (if (member 'idmap (nfs-configuration-debug config))
442 (pipefs-directory (nfs-configuration-pipefs-directory config))
443 (nfs-utils (nfs-configuration-nfs-utils config)))))
444 (service-extension pipefs-service-type
446 (pipefs-configuration
447 (mount-point (nfs-configuration-pipefs-directory config)))))
448 (service-extension rpcbind-service-type
450 (rpcbind-configuration
451 (rpcbind (nfs-configuration-rpcbind config)))))))
453 "Run all NFS daemons and refresh the list of exported file systems.")))