nfs.scm 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2016, 2017, 2020 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>
  8. ;;;
  9. ;;; This file is part of GNU Guix.
  10. ;;;
  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.
  15. ;;;
  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.
  20. ;;;
  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/>.
  23. (define-module (gnu tests nfs)
  24. #:use-module (gnu tests)
  25. #:use-module (gnu bootloader)
  26. #:use-module (gnu bootloader grub)
  27. #:use-module (gnu system)
  28. #:use-module (gnu system file-systems)
  29. #:use-module (gnu system shadow)
  30. #:use-module (gnu system vm)
  31. #:use-module (gnu services)
  32. #:use-module (gnu services base)
  33. #:use-module (gnu services nfs)
  34. #:use-module (gnu services networking)
  35. #:use-module (gnu packages onc-rpc)
  36. #:use-module (gnu packages nfs)
  37. #:use-module (guix gexp)
  38. #:use-module (guix store)
  39. #:use-module (guix monads)
  40. #:export (%test-nfs
  41. %test-nfs-server
  42. %test-nfs-root-fs))
  43. (define %base-os
  44. (operating-system
  45. (host-name "olitupmok")
  46. (timezone "Europe/Berlin")
  47. (locale "en_US.UTF-8")
  48. (bootloader (bootloader-configuration
  49. (bootloader grub-bootloader)
  50. (target "/dev/sdX")))
  51. (file-systems %base-file-systems)
  52. (users %base-user-accounts)
  53. (packages (cons*
  54. rpcbind
  55. %base-packages))
  56. (services (cons*
  57. (service rpcbind-service-type)
  58. (service dhcp-client-service-type)
  59. %base-services))))
  60. (define (run-nfs-test name socket)
  61. "Run a test of an OS running RPC-SERVICE, which should create SOCKET."
  62. (define os
  63. (marionette-operating-system
  64. %base-os
  65. #:imported-modules '((gnu services herd)
  66. (guix combinators))))
  67. (define test
  68. (with-imported-modules '((gnu build marionette))
  69. #~(begin
  70. (use-modules (gnu build marionette)
  71. (srfi srfi-64))
  72. (define marionette
  73. (make-marionette (list #$(virtual-machine os))))
  74. (define (wait-for-socket file)
  75. ;; Wait until SOCKET exists in the guest
  76. (marionette-eval
  77. `(let loop ((i 10))
  78. (cond ((and (file-exists? ,file)
  79. (eq? 'socket (stat:type (stat ,file))))
  80. #t)
  81. ((> i 0)
  82. (sleep 1)
  83. (loop (- i 1)))
  84. (else
  85. (error "Socket didn't show up: " ,file))))
  86. marionette))
  87. (mkdir #$output)
  88. (chdir #$output)
  89. (test-begin "rpc-daemon")
  90. ;; Wait for the rpcbind daemon to be up and running.
  91. (test-assert "RPC service running"
  92. (marionette-eval
  93. '(begin
  94. (use-modules (gnu services herd))
  95. ;; Ensure 'rpcinfo' can be found below.
  96. (setenv "PATH" "/run/current-system/profile/bin")
  97. (start-service 'rpcbind-daemon))
  98. marionette))
  99. ;; Check the socket file and that the service is still running.
  100. (test-assert "RPC socket exists"
  101. (and
  102. (wait-for-socket #$socket)
  103. (marionette-eval
  104. '(begin
  105. (use-modules (gnu services herd)
  106. (srfi srfi-1))
  107. (live-service-running
  108. (find (lambda (live)
  109. (memq 'rpcbind-daemon
  110. (live-service-provision live)))
  111. (current-services))))
  112. marionette)))
  113. (test-assert "Probe RPC daemon"
  114. (marionette-eval
  115. '(zero? (system* "rpcinfo" "-p"))
  116. marionette))
  117. (test-end)
  118. (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
  119. (gexp->derivation name test))
  120. (define %test-nfs
  121. (system-test
  122. (name "nfs")
  123. (description "Test some things related to NFS.")
  124. (value (run-nfs-test name "/var/run/rpcbind.sock"))))
  125. (define %nfs-os
  126. (let ((os (simple-operating-system
  127. (simple-service 'create-target-directory activation-service-type
  128. #~(begin
  129. (mkdir "/remote")
  130. (chmod "/remote" #o777)
  131. #t))
  132. (service dhcp-client-service-type)
  133. (service nfs-service-type
  134. (nfs-configuration
  135. (debug '(nfs nfsd mountd))
  136. (exports '(("/export"
  137. ;; crossmnt = This is the pseudo root.
  138. ;; fsid=0 = root file system of the export
  139. "*(ro,insecure,no_subtree_check,crossmnt,fsid=0)"))))))))
  140. (operating-system
  141. (inherit os)
  142. (host-name "nfs-server")
  143. ;; We need to use a tmpfs here, because the test system's root file
  144. ;; system cannot be re-exported via NFS.
  145. (file-systems (cons
  146. (file-system
  147. (device "none")
  148. (mount-point "/export")
  149. (type "tmpfs")
  150. (create-mount-point? #t))
  151. %base-file-systems))
  152. (services
  153. ;; Enable debugging output.
  154. (modify-services (operating-system-user-services os)
  155. (syslog-service-type config
  156. =>
  157. (syslog-configuration
  158. (inherit config)
  159. (config-file
  160. (plain-file
  161. "syslog.conf"
  162. "*.* /dev/console\n")))))))))
  163. (define (run-nfs-server-test)
  164. "Run a test of an OS running a service of NFS-SERVICE-TYPE."
  165. (define os
  166. (marionette-operating-system
  167. %nfs-os
  168. #:requirements '(nscd)
  169. #:imported-modules '((gnu services herd)
  170. (guix combinators))))
  171. (define test
  172. (with-imported-modules '((gnu build marionette))
  173. #~(begin
  174. (use-modules (gnu build marionette)
  175. (srfi srfi-64))
  176. (define marionette
  177. (make-marionette (list #$(virtual-machine os))))
  178. (mkdir #$output)
  179. (chdir #$output)
  180. (test-begin "nfs-daemon")
  181. (marionette-eval
  182. '(begin
  183. (current-output-port
  184. (open-file "/dev/console" "w0"))
  185. (chmod "/export" #o777)
  186. (with-output-to-file "/export/hello"
  187. (lambda () (display "hello world")))
  188. (chmod "/export/hello" #o777))
  189. marionette)
  190. (test-assert "nscd PID file is created"
  191. (marionette-eval
  192. '(begin
  193. (use-modules (gnu services herd))
  194. (start-service 'nscd))
  195. marionette))
  196. (test-assert "nscd is listening on its socket"
  197. (wait-for-unix-socket "/var/run/nscd/socket"
  198. marionette))
  199. (test-assert "network is up"
  200. (marionette-eval
  201. '(begin
  202. (use-modules (gnu services herd))
  203. (start-service 'networking))
  204. marionette))
  205. ;; Wait for the NFS services to be up and running.
  206. (test-assert "nfs services are running"
  207. (and (marionette-eval
  208. '(begin
  209. (use-modules (gnu services herd))
  210. (start-service 'nfs))
  211. marionette)
  212. (wait-for-file "/var/run/rpc.statd.pid" marionette)))
  213. (test-assert "nfs share is advertised"
  214. (marionette-eval
  215. '(zero? (system* (string-append #$nfs-utils "/sbin/showmount")
  216. "-e" "nfs-server"))
  217. marionette))
  218. (test-assert "nfs share mounted"
  219. (marionette-eval
  220. '(begin
  221. (and (zero? (system* (string-append #$nfs-utils "/sbin/mount.nfs4")
  222. "nfs-server:/" "/remote" "-v"))
  223. (file-exists? "/remote/hello")))
  224. marionette))
  225. (test-end)
  226. (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
  227. (gexp->derivation "nfs-server-test" test))
  228. (define %test-nfs-server
  229. (system-test
  230. (name "nfs-server")
  231. (description "Test that an NFS server can be started and exported
  232. directories can be mounted.")
  233. (value (run-nfs-server-test))))
  234. (define (run-nfs-root-fs-test)
  235. "Run a test of an OS mounting its root file system via NFS."
  236. (define nfs-root-server-os
  237. (marionette-operating-system
  238. (operating-system
  239. (inherit %nfs-os)
  240. (services
  241. (modify-services (operating-system-user-services %nfs-os)
  242. (nfs-service-type config =>
  243. (nfs-configuration
  244. (debug '(nfs nfsd mountd))
  245. ;;; Note: Adding the following line causes Guix to hang.
  246. ;(rpcmountd-port 20001)
  247. ;;; Note: Adding the following line causes Guix to hang.
  248. ;(rpcstatd-port 20002) ; FIXME: Set broadcast port AND listening port.
  249. (nfsd-port 2049)
  250. (nfs-versions '("4.2"))
  251. (exports '(("/export"
  252. "*(rw,insecure,no_subtree_check,crossmnt,fsid=root,no_root_squash,insecure,async)"))))))))
  253. #:requirements '(nscd)
  254. #:imported-modules '((gnu services herd)
  255. (guix combinators))))
  256. (define nfs-root-client-os
  257. (marionette-operating-system
  258. (operating-system
  259. (inherit (simple-operating-system (service dhcp-client-service-type)))
  260. (kernel-arguments '("ip=dhcp"))
  261. (file-systems (cons
  262. (file-system
  263. (type "nfs")
  264. (mount-point "/")
  265. (device ":/export")
  266. (options "addr=127.0.0.1,vers=4.2"))
  267. %base-file-systems)))
  268. #:requirements '(nscd)
  269. #:imported-modules '((gnu services herd)
  270. (guix combinators))))
  271. (define test
  272. (with-imported-modules '((gnu build marionette))
  273. #~(begin
  274. (use-modules (gnu build marionette)
  275. (srfi srfi-64))
  276. (mkdir #$output)
  277. (chdir #$output)
  278. (test-begin "start-nfs-boot-test")
  279. ;;; Start up NFS server host.
  280. (mkdir "/tmp/server")
  281. (define server-marionette
  282. (make-marionette (list #$(virtual-machine
  283. nfs-root-server-os
  284. ;(operating-system nfs-root-server-os)
  285. ;(port-forwardings '( ; (111 . 111)
  286. ; (2049 . 2049)
  287. ; (20001 . 20001)
  288. ; (20002 . 20002)))
  289. ))
  290. #:socket-directory "/tmp/server"))
  291. (marionette-eval
  292. '(begin
  293. (use-modules (gnu services herd))
  294. (current-output-port
  295. (open-file "/dev/console" "w0"))
  296. ;; FIXME: Instead statfs "/" and "/export" and wait until they
  297. ;; are different file systems. But Guile doesn't seem to have
  298. ;; statfs.
  299. (sleep 5)
  300. (chmod "/export" #o777)
  301. (symlink "/gnu" "/export/gnu")
  302. (start-service 'nscd)
  303. (start-service 'networking)
  304. (start-service 'nfs))
  305. server-marionette)
  306. ;;; Wait for the NFS services to be up and running.
  307. (test-assert "nfs services are running"
  308. (wait-for-file "/var/run/rpc.statd.pid" server-marionette))
  309. (test-assert "NFS port is ready"
  310. (wait-for-tcp-port 2049 server-marionette))
  311. (test-assert "NFS statd port is ready"
  312. (wait-for-tcp-port 20002 server-marionette))
  313. (test-assert "NFS mountd port is ready"
  314. (wait-for-tcp-port 20001 server-marionette))
  315. ;;; FIXME: (test-assert "NFS portmapper port is ready"
  316. ;;; FIXME: (wait-for-tcp-port 111 server-marionette))
  317. ;;; Start up NFS client host.
  318. (define client-marionette
  319. (make-marionette (list #$(virtual-machine
  320. nfs-root-client-os
  321. ;(port-forwardings '((111 . 111)
  322. ; (2049 . 2049)
  323. ; (20001 . 20001)
  324. ; (20002 . 20002)))
  325. ))))
  326. (marionette-eval
  327. '(begin
  328. (use-modules (gnu services herd))
  329. (use-modules (rnrs io ports))
  330. (current-output-port
  331. (open-file "/dev/console" "w0"))
  332. (let ((content (call-with-input-file "/proc/mounts" get-string-all)))
  333. (call-with-output-file "/mounts.new"
  334. (lambda (port)
  335. (display content port))))
  336. (chmod "/mounts.new" #o777)
  337. (rename-file "/mounts.new" "/mounts"))
  338. client-marionette)
  339. (test-assert "nfs-root-client booted")
  340. ;;; Check whether NFS client host communicated with NFS server host.
  341. (test-assert "nfs client deposited file"
  342. (wait-for-file "/export/mounts" server-marionette))
  343. (marionette-eval
  344. '(begin
  345. (current-output-port
  346. (open-file "/dev/console" "w0"))
  347. (call-with-input-file "/export/mounts" display))
  348. server-marionette)
  349. (test-end)
  350. (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
  351. (gexp->derivation "nfs-root-fs-test" test))
  352. (define %test-nfs-root-fs
  353. (system-test
  354. (name "nfs-root-fs")
  355. (description "Test that an NFS server can be started and the exported
  356. directory can be used as root file system.")
  357. (value (run-nfs-root-fs-test))))