1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2016, 2017, 2020, 2021 Ludovic Courtès <ludo@gnu.org>
3 ;;; Copyright © 2016 John Darrington <jmd@gnu.org>
4 ;;; Copyright © 2017 Mathieu Othacehe <m.othacehe@gmail.com>
5 ;;; Copyright © 2017 Tobias Geerinckx-Rice <me@tobias.gr>
6 ;;; Copyright © 2018 Clément Lassieur <clement@lassieur.org>
7 ;;; Copyright © 2019, 2020 Ricardo Wurmus <rekado@elephly.net>
9 ;;; This file is part of GNU Guix.
11 ;;; GNU Guix is free software; you can redistribute it and/or modify it
12 ;;; under the terms of the GNU General Public License as published by
13 ;;; the Free Software Foundation; either version 3 of the License, or (at
14 ;;; your option) any later version.
16 ;;; GNU Guix is distributed in the hope that it will be useful, but
17 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;;; GNU General Public License for more details.
21 ;;; You should have received a copy of the GNU General Public License
22 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
24 (define-module (gnu tests nfs)
25 #:use-module (gnu tests)
26 #:use-module (gnu bootloader)
27 #:use-module (gnu bootloader grub)
28 #:use-module (gnu system)
29 #:use-module (gnu system file-systems)
30 #:use-module (gnu system shadow)
31 #:use-module (gnu system vm)
32 #:use-module (gnu services)
33 #:use-module (gnu services base)
34 #:use-module (gnu services nfs)
35 #:use-module (gnu services networking)
36 #:use-module (gnu packages admin)
37 #:use-module (gnu packages onc-rpc)
38 #:use-module (gnu packages nfs)
39 #:use-module (guix gexp)
40 #:use-module (guix store)
41 #:use-module (guix monads)
48 (host-name "olitupmok")
49 (timezone "Europe/Berlin")
50 (locale "en_US.UTF-8")
52 (bootloader (bootloader-configuration
53 (bootloader grub-bootloader)
54 (targets '("/dev/sdX"))))
55 (file-systems %base-file-systems)
56 (users %base-user-accounts)
61 (service rpcbind-service-type)
62 (service dhcp-client-service-type)
65 (define (run-nfs-test name socket)
66 "Run a test of an OS running RPC-SERVICE, which should create SOCKET."
68 (marionette-operating-system
70 #:imported-modules '((gnu services herd)
74 (with-imported-modules '((gnu build marionette))
76 (use-modules (gnu build marionette)
80 (make-marionette (list #$(virtual-machine os))))
82 (define (wait-for-socket file)
83 ;; Wait until SOCKET exists in the guest
86 (cond ((and (file-exists? ,file)
87 (eq? 'socket (stat:type (stat ,file))))
93 (error "Socket didn't show up: " ,file))))
96 (test-runner-current (system-test-runner #$output))
97 (test-begin "rpc-daemon")
99 ;; Wait for the rpcbind daemon to be up and running.
100 (test-assert "RPC service running"
103 (use-modules (gnu services herd))
105 ;; Ensure 'rpcinfo' can be found below.
106 (setenv "PATH" "/run/current-system/profile/bin")
108 (start-service 'rpcbind-daemon))
111 ;; Check the socket file and that the service is still running.
112 (test-assert "RPC socket exists"
114 (wait-for-socket #$socket)
117 (use-modules (gnu services herd)
120 (live-service-running
122 (memq 'rpcbind-daemon
123 (live-service-provision live)))
124 (current-services))))
127 (test-assert "Probe RPC daemon"
129 '(zero? (system* "rpcinfo" "-p"))
134 (gexp->derivation name test))
139 (description "Test some things related to NFS.")
140 (value (run-nfs-test name "/var/run/rpcbind.sock"))))
144 (let ((os (simple-operating-system
145 (simple-service 'create-target-directory activation-service-type
148 (chmod "/remote" #o777)
150 (service dhcp-client-service-type)
151 (service nfs-service-type
153 (debug '(nfs nfsd mountd))
154 (exports '(("/export"
155 ;; crossmnt = This is the pseudo root.
156 ;; fsid=0 = root file system of the export
157 "*(ro,insecure,no_subtree_check,crossmnt,fsid=0)"))))))))
160 (host-name "nfs-server")
161 ;; We need to use a tmpfs here, because the test system's root file
162 ;; system cannot be re-exported via NFS.
166 (mount-point "/export")
168 (create-mount-point? #t))
171 ;; Enable debugging output.
172 (modify-services (operating-system-user-services os)
173 (syslog-service-type config
175 (syslog-configuration
180 "*.* /dev/console\n")))))))))
182 (define (run-nfs-server-test)
183 "Run a test of an OS running a service of NFS-SERVICE-TYPE."
185 (marionette-operating-system
187 #:requirements '(nscd)
188 #:imported-modules '((gnu services herd)
189 (guix combinators))))
191 (with-imported-modules '((gnu build marionette))
193 (use-modules (gnu build marionette)
197 (make-marionette (list #$(virtual-machine os))))
199 (test-runner-current (system-test-runner #$output))
200 (test-begin "nfs-daemon")
204 (open-file "/dev/console" "w0"))
205 (chmod "/export" #o777)
206 (with-output-to-file "/export/hello"
207 (lambda () (display "hello world")))
208 (chmod "/export/hello" #o777))
211 (test-assert "nscd PID file is created"
214 (use-modules (gnu services herd))
215 (start-service 'nscd))
218 (test-assert "nscd is listening on its socket"
219 (wait-for-unix-socket "/var/run/nscd/socket"
222 (test-assert "network is up"
225 (use-modules (gnu services herd))
226 (start-service 'networking))
229 ;; Wait for the NFS services to be up and running.
230 (test-assert "nfs services are running"
231 (and (marionette-eval
233 (use-modules (gnu services herd))
234 (start-service 'nfs))
236 (wait-for-file "/var/run/rpc.statd.pid" marionette)))
238 (test-assert "nfs share is advertised"
240 '(zero? (system* (string-append #$nfs-utils "/sbin/showmount")
244 (test-assert "nfs share mounted"
247 (and (zero? (system* (string-append #$nfs-utils "/sbin/mount.nfs4")
248 "nfs-server:/" "/remote" "-v"))
249 (file-exists? "/remote/hello")))
253 (gexp->derivation "nfs-server-test" test))
255 (define %test-nfs-server
258 (description "Test that an NFS server can be started and exported
259 directories can be mounted.")
260 (value (run-nfs-server-test))))
263 (define (run-nfs-full-test)
264 "Run a test of an OS mounting its root file system via NFS."
265 (define nfs-root-server-os
266 (let ((os (simple-operating-system)))
267 (marionette-operating-system
272 (service static-networking-service-type
275 (addresses (list (network-address
277 (value "10.0.2.15/24")))))))
278 (simple-service 'export activation-service-type
281 (chmod "/export" #o777)))
282 (service nfs-service-type
285 (nfs-versions '("4.2"))
286 (exports '(("/export"
287 "*(rw,insecure,no_subtree_check,\
288 crossmnt,fsid=root,no_root_squash,insecure,async)")))))
289 (modify-services (operating-system-user-services os)
290 (syslog-service-type config
292 (syslog-configuration
297 "*.* /dev/console\n"))))))))
298 #:requirements '(nscd)
299 #:imported-modules '((gnu services herd)
300 (guix combinators)))))
302 (define nfs-root-client-os
303 (marionette-operating-system
304 (simple-operating-system
305 (service static-networking-service-type
309 (list (network-address
311 (value "10.0.2.16/24")))))))
312 (service nfs-service-type
315 (nfs-versions '("4.2"))))
316 (simple-service 'export activation-service-type
319 (chmod "/export" #o777))))
320 #:requirements '(nscd)
321 #:imported-modules '((gnu services herd)
322 (guix combinators))))
325 (with-imported-modules '((gnu build marionette))
327 (use-modules (gnu build marionette)
330 (test-runner-current (system-test-runner #$output))
331 (test-begin "start-nfs-boot-test")
333 ;;; Start up NFS server host.
334 (mkdir "/tmp/server")
335 (define server-marionette
337 (cons* #$(virtual-machine
338 (operating-system nfs-root-server-os)
340 '("-device" "e1000,netdev=n1,mac=52:54:00:12:34:56"
341 "-netdev" "socket,id=n1,listen=:1234"))
342 #:socket-directory "/tmp/server"))
344 ;;; Wait for the NFS services to be up and running.
345 (test-assert "nfs services are running"
346 (wait-for-file "/var/run/rpc.statd.pid" server-marionette))
348 (test-assert "NFS port is ready"
349 (wait-for-tcp-port 2049 server-marionette))
351 ;;; Start up NFS client host.
352 (mkdir "/tmp/client")
353 (define client-marionette
355 (cons* #$(virtual-machine
356 (operating-system nfs-root-client-os)
358 '("-device" "e1000,netdev=n2,mac=52:54:00:12:34:57"
359 "-netdev" "socket,id=n2,connect=127.0.0.1:1234"))
360 #:socket-directory "/tmp/client"))
362 (test-assert "NFS port is ready"
363 (wait-for-tcp-port 2049 client-marionette))
367 (use-modules (rnrs io ports))
369 (open-file "/dev/console" "w0"))
371 (system* (string-append #$nfs-utils "/sbin/mount.nfs")
372 "10.0.2.15:/export" "/export" "-v")
373 (let ((content (call-with-input-file "/proc/mounts"
375 (call-with-output-file "/export/mounts"
377 (display content port))))))
380 ;;; Check whether NFS client host communicated with NFS server host.
381 (test-assert "nfs client deposited file"
382 (wait-for-file "/export/mounts" server-marionette))
387 (open-file "/dev/console" "w0"))
388 (call-with-input-file "/export/mounts" display))
393 (gexp->derivation "nfs-full-test" test))
395 (define %test-nfs-full
398 (description "Test that an NFS server can be started and the exported
399 directory can be used by another machine.")
400 (value (run-nfs-full-test))))