tests: nfs-server: Use marionette helper procedures.
[jackhill/guix/guix.git] / gnu / services / nfs.scm
CommitLineData
d6a07ee6
JD
1;;; GNU Guix --- Functional package management for GNU
2;;; Copyright © 2016 John Darrington <jmd@gnu.org>
981ce389 3;;; Copyright © 2018, 2019, 2020 Ricardo Wurmus <rekado@elephly.net>
d6a07ee6
JD
4;;;
5;;; This file is part of GNU Guix.
6;;;
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.
11;;;
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.
16;;;
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/>.
19
20(define-module (gnu services nfs)
21 #:use-module (gnu)
22 #:use-module (gnu services shepherd)
23 #:use-module (gnu packages onc-rpc)
eb419bc9 24 #:use-module (gnu packages linux)
907eeac2 25 #:use-module (gnu packages nfs)
d6a07ee6
JD
26 #:use-module (guix)
27 #:use-module (guix records)
25c8c8cd 28 #:use-module (srfi srfi-1)
eb419bc9
JD
29 #:use-module (ice-9 match)
30 #:use-module (gnu build file-systems)
d6a07ee6
JD
31 #:export (rpcbind-service-type
32 rpcbind-configuration
eb419bc9
JD
33 rpcbind-configuration?
34
35 pipefs-service-type
36 pipefs-configuration
37 pipefs-configuration?
38
39 idmap-service-type
40 idmap-configuration
41 idmap-configuration?
42
43 gss-service-type
44 gss-configuration
907eeac2
RW
45 gss-configuration?
46
47 nfs-service-type
48 nfs-configuration
49 nfs-configuration?))
eb419bc9
JD
50
51
52(define default-pipefs-directory "/var/lib/nfs/rpc_pipefs")
53
54\f
d6a07ee6
JD
55
56(define-record-type* <rpcbind-configuration>
57 rpcbind-configuration make-rpcbind-configuration
58 rpcbind-configuration?
59 (rpcbind rpcbind-configuration-rpcbind
60 (default rpcbind))
61 (warm-start? rpcbind-configuration-warm-start?
62 (default #t)))
63
64(define rpcbind-service-type
ba1808d5
RW
65 (let ((proc
66 (lambda (config)
67 (define rpcbind
68 (rpcbind-configuration-rpcbind config))
d6a07ee6 69
ba1808d5
RW
70 (define rpcbind-command
71 #~(list (string-append #$rpcbind "/bin/rpcbind") "-f"
72 #$@(if (rpcbind-configuration-warm-start? config) '("-w") '())))
d6a07ee6 73
ba1808d5
RW
74 (shepherd-service
75 (documentation "Start the RPC bind daemon.")
76 (requirement '(networking))
77 (provision '(rpcbind-daemon))
d6a07ee6 78
ba1808d5
RW
79 (start #~(make-forkexec-constructor #$rpcbind-command))
80 (stop #~(make-kill-destructor))))))
81 (service-type
82 (name 'rpcbind)
83 (extensions
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.
89 (compose identity)
90 (extend (lambda (config values)
91 (match values
92 ((first . rest) first)
93 (_ config))))
94 (default-value (rpcbind-configuration)))))
eb419bc9
JD
95
96\f
97
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)))
103
104(define pipefs-service-type
25c8c8cd
RW
105 (let ((proc
106 (lambda (config)
107 (define pipefs-directory (pipefs-configuration-mount-point config))
eb419bc9 108
25c8c8cd
RW
109 (shepherd-service
110 (documentation "Mount the pipefs pseudo file system.")
111 (provision '(rpc-pipefs))
eb419bc9 112
25c8c8cd
RW
113 (start #~(lambda ()
114 (mkdir-p #$pipefs-directory)
115 (mount "rpc_pipefs" #$pipefs-directory "rpc_pipefs")
116 (member #$pipefs-directory (mount-points))))
eb419bc9 117
25c8c8cd
RW
118 (stop #~(lambda (pid . args)
119 (umount #$pipefs-directory MNT_DETACH)
120 (not (member #$pipefs-directory (mount-points)))))))))
121 (service-type
122 (name 'pipefs)
123 (extensions
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.
129 (compose identity)
130 (extend (lambda (config values) (first values)))
131 (default-value (pipefs-configuration)))))
eb419bc9
JD
132
133\f
134
135(define-record-type* <gss-configuration>
136 gss-configuration make-gss-configuration
137 gss-configuration?
5d4ba498 138 (pipefs-directory gss-configuration-pipefs-directory
eb419bc9
JD
139 (default default-pipefs-directory))
140 (nfs-utils gss-configuration-gss
141 (default nfs-utils)))
142
143(define gss-service-type
a6bdca6b
RW
144 (let ((proc
145 (lambda (config)
146 (define nfs-utils
147 (gss-configuration-gss config))
148
149 (define pipefs-directory
150 (gss-configuration-pipefs-directory config))
151
152 (define gss-command
153 #~(list (string-append #$nfs-utils "/sbin/rpc.gssd") "-f"
154 "-p" #$pipefs-directory))
155
156 (shepherd-service
157 (documentation "Start the RPC GSS daemon.")
158 (requirement '(rpcbind-daemon rpc-pipefs))
159 (provision '(gss-daemon))
160
161 (start #~(make-forkexec-constructor #$gss-command))
162 (stop #~(make-kill-destructor))))))
163 (service-type
164 (name 'gss)
165 (extensions
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.
171 (compose identity)
172 (extend (lambda (config values)
173 (match values
174 ((first . rest) first)
175 (_ config))))
176 (default-value (gss-configuration)))))
eb419bc9
JD
177
178\f
179
180(define-record-type* <idmap-configuration>
181 idmap-configuration make-idmap-configuration
182 idmap-configuration?
5d4ba498 183 (pipefs-directory idmap-configuration-pipefs-directory
eb419bc9
JD
184 (default default-pipefs-directory))
185 (domain idmap-configuration-domain
5d4ba498
RW
186 (default #f))
187 (nfs-utils idmap-configuration-nfs-utils
2a23942e
RW
188 (default nfs-utils))
189 (verbosity idmap-configuration-verbosity
190 (default 0)))
eb419bc9
JD
191
192(define idmap-service-type
af32ea35
RW
193 (let ((proc
194 (lambda (config)
eb419bc9 195
af32ea35
RW
196 (define nfs-utils
197 (idmap-configuration-nfs-utils config))
eb419bc9 198
af32ea35
RW
199 (define pipefs-directory
200 (idmap-configuration-pipefs-directory config))
eb419bc9 201
af32ea35 202 (define domain (idmap-configuration-domain config))
eb419bc9 203
af32ea35
RW
204 (define (idmap-config-file config)
205 (plain-file "idmapd.conf"
206 (string-append
207 "\n[General]\n"
2a23942e
RW
208 "Verbosity = "
209 (number->string
210 (idmap-configuration-verbosity config))
211 "\n"
af32ea35
RW
212 (if domain
213 (format #f "Domain = ~a\n" domain)
214 "")
215 "\n[Mapping]\n"
216 "Nobody-User = nobody\n"
217 "Nobody-Group = nogroup\n")))
eb419bc9 218
af32ea35
RW
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)))
eb419bc9 224
af32ea35
RW
225 (shepherd-service
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))))))
231 (service-type
232 (name 'idmap)
233 (extensions
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.
239 (compose identity)
240 (extend (lambda (config values) (first values)))
241 (default-value (idmap-configuration)))))
907eeac2
RW
242
243(define-record-type* <nfs-configuration>
244 nfs-configuration make-nfs-configuration
245 nfs-configuration?
246 (nfs-utils nfs-configuration-nfs-utils
247 (default nfs-utils))
248 (nfs-version nfs-configuration-nfs-version
249 (default #f)) ; string
250 (exports nfs-configuration-exports
251 (default '()))
252 (rpcmountd-port nfs-configuration-rpcmountd-port
253 (default #f))
254 (rpcstatd-port nfs-configuration-rpcstatd-port
255 (default #f))
256 (rpcbind nfs-configuration-rpcbind
257 (default rpcbind))
258 (idmap-domain nfs-configuration-idmap-domain
259 (default "localdomain"))
260 (nfsd-port nfs-configuration-nfsd-port
261 (default 2049))
262 (nfsd-threads nfs-configuration-nfsd-threads
263 (default 8))
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
268 (default '())))
269
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))
280 (start
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.
285 "--no-notify"
286 #$@(if (member 'statd debug)
287 '("--no-syslog") ; verbose logging to stderr
288 '())
289 "--foreground"
290 #$@(if rpcstatd-port
291 '("--port" (number->string rpcstatd-port))
292 '()))
293 #:pid-file "/var/run/rpc.statd.pid"))
294 (stop #~(make-kill-destructor)))
295 (shepherd-service
296 (documentation "Run the NFS mountd daemon.")
297 (provision '(rpc.mountd))
298 (requirement '(rpc.statd))
299 (start
300 #~(make-forkexec-constructor
301 (list #$(file-append nfs-utils "/sbin/rpc.mountd")
5fdc5b2d 302 "--foreground"
907eeac2
RW
303 #$@(if (member 'mountd debug)
304 '("--debug" "all")
305 '())
306 #$@(if rpcmountd-port
307 '("--port" (number->string rpcmountd-port))
308 '()))))
309 (stop #~(make-kill-destructor)))
310 (shepherd-service
311 (documentation "Run the NFS daemon.")
312 (provision '(rpc.nfsd))
313 (requirement '(rpc.statd networking))
314 (start
315 #~(lambda _
316 (zero? (system* #$(file-append nfs-utils "/sbin/rpc.nfsd")
317 #$@(if (member 'nfsd debug)
318 '("--debug")
319 '())
320 "--port" #$(number->string nfsd-port)
321 #$@(if nfs-version
322 '("--nfs-version" nfs-version)
323 '())
324 #$(number->string nfsd-threads)))))
325 (stop
326 #~(lambda _
327 (zero?
328 (system* #$(file-append nfs-utils "/sbin/rpc.nfsd") "0")))))
329 (shepherd-service
330 (documentation "Run the NFS mountd daemon and refresh exports.")
331 (provision '(nfs))
332 (requirement '(rpc.nfsd rpc.mountd rpc.statd rpcbind-daemon))
333 (start
334 #~(lambda _
335 (let ((rpcdebug #$(file-append nfs-utils "/sbin/rpcdebug")))
336 (cond
337 ((member 'nfsd '#$debug)
338 (system* rpcdebug "-m" "nfsd" "-s" "all"))
339 ((member 'nfs '#$debug)
340 (system* rpcdebug "-m" "nfs" "-s" "all"))
341 ((member 'rpc '#$debug)
342 (system* rpcdebug "-m" "rpc" "-s" "all"))))
343 (zero? (system*
344 #$(file-append nfs-utils "/sbin/exportfs")
345 "-r" ; re-export
346 "-a" ; everthing
347 "-v" ; be verbose
348 "-d" "all" ; debug
349 ))))
350 (stop
351 #~(lambda _
352 (let ((rpcdebug #$(file-append nfs-utils "/sbin/rpcdebug")))
353 (cond
354 ((member 'nfsd '#$debug)
355 (system* rpcdebug "-m" "nfsd" "-c" "all"))
356 ((member 'nfs '#$debug)
357 (system* rpcdebug "-m" "nfs" "-c" "all"))
358 ((member 'rpc '#$debug)
359 (system* rpcdebug "-m" "rpc" "-c" "all"))))
360 #t))
361 (respawn? #f)))))
362
363(define nfs-service-type
364 (service-type
365 (name 'nfs)
366 (extensions
367 (list
368 (service-extension shepherd-root-service-type nfs-shepherd-services)
369 (service-extension activation-service-type
370 (const #~(begin
371 (use-modules (guix build utils))
372 (system* "mount" "-t" "nfsd"
373 "nfsd" "/proc/fs/nfsd")
374
375 (mkdir-p "/var/lib/nfs")
376 ;; directory containing monitor list
377 (mkdir-p "/var/lib/nfs/sm")
378 ;; Needed for client recovery tracking
379 (mkdir-p "/var/lib/nfs/v4recovery")
380 (let ((user (getpw "nobody")))
381 (chown "/var/lib/nfs"
382 (passwd:uid user)
383 (passwd:gid user))
384 (chown "/var/lib/nfs/v4recovery"
385 (passwd:uid user)
386 (passwd:gid user)))
387 #t)))
388 (service-extension etc-service-type
389 (lambda (config)
390 `(("exports"
391 ,(plain-file "exports"
392 (string-join
393 (map string-join
394 (nfs-configuration-exports config))
395 "\n"))))))
396 ;; The NFS service depends on these other services. They are extended so
397 ;; that users don't need to configure them manually.
398 (service-extension idmap-service-type
399 (lambda (config)
400 (idmap-configuration
401 (domain (nfs-configuration-idmap-domain config))
402 (verbosity
403 (if (member 'idmap (nfs-configuration-debug config))
404 10 0))
405 (pipefs-directory (nfs-configuration-pipefs-directory config))
406 (nfs-utils (nfs-configuration-nfs-utils config)))))
407 (service-extension pipefs-service-type
408 (lambda (config)
409 (pipefs-configuration
410 (mount-point (nfs-configuration-pipefs-directory config)))))
411 (service-extension rpcbind-service-type
412 (lambda (config)
413 (rpcbind-configuration
414 (rpcbind (nfs-configuration-rpcbind config)))))))
415 (description
416 "Run all NFS daemons and refresh the list of exported file systems.")))