nfs.scm 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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>
  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 admin)
  36. #:use-module (gnu packages onc-rpc)
  37. #:use-module (gnu packages nfs)
  38. #:use-module (guix gexp)
  39. #:use-module (guix store)
  40. #:use-module (guix monads)
  41. #:export (%test-nfs
  42. %test-nfs-server
  43. %test-nfs-full))
  44. (define %base-os
  45. (operating-system
  46. (host-name "olitupmok")
  47. (timezone "Europe/Berlin")
  48. (locale "en_US.UTF-8")
  49. (bootloader (bootloader-configuration
  50. (bootloader grub-bootloader)
  51. (targets '("/dev/sdX"))))
  52. (file-systems %base-file-systems)
  53. (users %base-user-accounts)
  54. (packages (cons*
  55. rpcbind
  56. %base-packages))
  57. (services (cons*
  58. (service rpcbind-service-type)
  59. (service dhcp-client-service-type)
  60. %base-services))))
  61. (define (run-nfs-test name socket)
  62. "Run a test of an OS running RPC-SERVICE, which should create SOCKET."
  63. (define os
  64. (marionette-operating-system
  65. %base-os
  66. #:imported-modules '((gnu services herd)
  67. (guix combinators))))
  68. (define test
  69. (with-imported-modules '((gnu build marionette))
  70. #~(begin
  71. (use-modules (gnu build marionette)
  72. (srfi srfi-64))
  73. (define marionette
  74. (make-marionette (list #$(virtual-machine os))))
  75. (define (wait-for-socket file)
  76. ;; Wait until SOCKET exists in the guest
  77. (marionette-eval
  78. `(let loop ((i 10))
  79. (cond ((and (file-exists? ,file)
  80. (eq? 'socket (stat:type (stat ,file))))
  81. #t)
  82. ((> i 0)
  83. (sleep 1)
  84. (loop (- i 1)))
  85. (else
  86. (error "Socket didn't show up: " ,file))))
  87. marionette))
  88. (test-runner-current (system-test-runner #$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. (gexp->derivation name test))
  119. (define %test-nfs
  120. (system-test
  121. (name "nfs")
  122. (description "Test some things related to NFS.")
  123. (value (run-nfs-test name "/var/run/rpcbind.sock"))))
  124. (define %nfs-os
  125. (let ((os (simple-operating-system
  126. (simple-service 'create-target-directory activation-service-type
  127. #~(begin
  128. (mkdir "/remote")
  129. (chmod "/remote" #o777)
  130. #t))
  131. (service dhcp-client-service-type)
  132. (service nfs-service-type
  133. (nfs-configuration
  134. (debug '(nfs nfsd mountd))
  135. (exports '(("/export"
  136. ;; crossmnt = This is the pseudo root.
  137. ;; fsid=0 = root file system of the export
  138. "*(ro,insecure,no_subtree_check,crossmnt,fsid=0)"))))))))
  139. (operating-system
  140. (inherit os)
  141. (host-name "nfs-server")
  142. ;; We need to use a tmpfs here, because the test system's root file
  143. ;; system cannot be re-exported via NFS.
  144. (file-systems (cons
  145. (file-system
  146. (device "none")
  147. (mount-point "/export")
  148. (type "tmpfs")
  149. (create-mount-point? #t))
  150. %base-file-systems))
  151. (services
  152. ;; Enable debugging output.
  153. (modify-services (operating-system-user-services os)
  154. (syslog-service-type config
  155. =>
  156. (syslog-configuration
  157. (inherit config)
  158. (config-file
  159. (plain-file
  160. "syslog.conf"
  161. "*.* /dev/console\n")))))))))
  162. (define (run-nfs-server-test)
  163. "Run a test of an OS running a service of NFS-SERVICE-TYPE."
  164. (define os
  165. (marionette-operating-system
  166. %nfs-os
  167. #:requirements '(nscd)
  168. #:imported-modules '((gnu services herd)
  169. (guix combinators))))
  170. (define test
  171. (with-imported-modules '((gnu build marionette))
  172. #~(begin
  173. (use-modules (gnu build marionette)
  174. (srfi srfi-64))
  175. (define marionette
  176. (make-marionette (list #$(virtual-machine os))))
  177. (test-runner-current (system-test-runner #$output))
  178. (test-begin "nfs-daemon")
  179. (marionette-eval
  180. '(begin
  181. (current-output-port
  182. (open-file "/dev/console" "w0"))
  183. (chmod "/export" #o777)
  184. (with-output-to-file "/export/hello"
  185. (lambda () (display "hello world")))
  186. (chmod "/export/hello" #o777))
  187. marionette)
  188. (test-assert "nscd PID file is created"
  189. (marionette-eval
  190. '(begin
  191. (use-modules (gnu services herd))
  192. (start-service 'nscd))
  193. marionette))
  194. (test-assert "nscd is listening on its socket"
  195. (wait-for-unix-socket "/var/run/nscd/socket"
  196. marionette))
  197. (test-assert "network is up"
  198. (marionette-eval
  199. '(begin
  200. (use-modules (gnu services herd))
  201. (start-service 'networking))
  202. marionette))
  203. ;; Wait for the NFS services to be up and running.
  204. (test-assert "nfs services are running"
  205. (and (marionette-eval
  206. '(begin
  207. (use-modules (gnu services herd))
  208. (start-service 'nfs))
  209. marionette)
  210. (wait-for-file "/var/run/rpc.statd.pid" marionette)))
  211. (test-assert "nfs share is advertised"
  212. (marionette-eval
  213. '(zero? (system* (string-append #$nfs-utils "/sbin/showmount")
  214. "-e" "nfs-server"))
  215. marionette))
  216. (test-assert "nfs share mounted"
  217. (marionette-eval
  218. '(begin
  219. (and (zero? (system* (string-append #$nfs-utils "/sbin/mount.nfs4")
  220. "nfs-server:/" "/remote" "-v"))
  221. (file-exists? "/remote/hello")))
  222. marionette))
  223. (test-end))))
  224. (gexp->derivation "nfs-server-test" test))
  225. (define %test-nfs-server
  226. (system-test
  227. (name "nfs-server")
  228. (description "Test that an NFS server can be started and exported
  229. directories can be mounted.")
  230. (value (run-nfs-server-test))))
  231. (define (run-nfs-full-test)
  232. "Run a test of an OS mounting its root file system via NFS."
  233. (define nfs-root-server-os
  234. (let ((os (simple-operating-system)))
  235. (marionette-operating-system
  236. (operating-system
  237. (inherit os)
  238. (services
  239. (cons*
  240. (service static-networking-service-type
  241. (list
  242. (static-networking
  243. (addresses (list (network-address
  244. (device "ens5")
  245. (value "10.0.2.15/24")))))))
  246. (simple-service 'export activation-service-type
  247. #~(begin
  248. (mkdir-p "/export")
  249. (chmod "/export" #o777)))
  250. (service nfs-service-type
  251. (nfs-configuration
  252. (nfsd-port 2049)
  253. (nfs-versions '("4.2"))
  254. (exports '(("/export"
  255. "*(rw,insecure,no_subtree_check,\
  256. crossmnt,fsid=root,no_root_squash,insecure,async)")))))
  257. (modify-services (operating-system-user-services os)
  258. (syslog-service-type config
  259. =>
  260. (syslog-configuration
  261. (inherit config)
  262. (config-file
  263. (plain-file
  264. "syslog.conf"
  265. "*.* /dev/console\n"))))))))
  266. #:requirements '(nscd)
  267. #:imported-modules '((gnu services herd)
  268. (guix combinators)))))
  269. (define nfs-root-client-os
  270. (marionette-operating-system
  271. (simple-operating-system
  272. (service static-networking-service-type
  273. (list
  274. (static-networking
  275. (addresses
  276. (list (network-address
  277. (device "ens5")
  278. (value "10.0.2.16/24")))))))
  279. (service nfs-service-type
  280. (nfs-configuration
  281. (nfsd-port 2049)
  282. (nfs-versions '("4.2"))))
  283. (simple-service 'export activation-service-type
  284. #~(begin
  285. (mkdir-p "/export")
  286. (chmod "/export" #o777))))
  287. #:requirements '(nscd)
  288. #:imported-modules '((gnu services herd)
  289. (guix combinators))))
  290. (define test
  291. (with-imported-modules '((gnu build marionette))
  292. #~(begin
  293. (use-modules (gnu build marionette)
  294. (srfi srfi-64))
  295. (test-runner-current (system-test-runner #$output))
  296. (test-begin "start-nfs-boot-test")
  297. ;;; Start up NFS server host.
  298. (mkdir "/tmp/server")
  299. (define server-marionette
  300. (make-marionette
  301. (cons* #$(virtual-machine
  302. (operating-system nfs-root-server-os)
  303. (volatile? #f))
  304. '("-device" "e1000,netdev=n1,mac=52:54:00:12:34:56"
  305. "-netdev" "socket,id=n1,listen=:1234"))
  306. #:socket-directory "/tmp/server"))
  307. ;;; Wait for the NFS services to be up and running.
  308. (test-assert "nfs services are running"
  309. (wait-for-file "/var/run/rpc.statd.pid" server-marionette))
  310. (test-assert "NFS port is ready"
  311. (wait-for-tcp-port 2049 server-marionette))
  312. ;;; Start up NFS client host.
  313. (mkdir "/tmp/client")
  314. (define client-marionette
  315. (make-marionette
  316. (cons* #$(virtual-machine
  317. (operating-system nfs-root-client-os)
  318. (volatile? #f))
  319. '("-device" "e1000,netdev=n2,mac=52:54:00:12:34:57"
  320. "-netdev" "socket,id=n2,connect=127.0.0.1:1234"))
  321. #:socket-directory "/tmp/client"))
  322. (test-assert "NFS port is ready"
  323. (wait-for-tcp-port 2049 client-marionette))
  324. (marionette-eval
  325. '(begin
  326. (use-modules (rnrs io ports))
  327. (current-output-port
  328. (open-file "/dev/console" "w0"))
  329. (and
  330. (system* (string-append #$nfs-utils "/sbin/mount.nfs")
  331. "10.0.2.15:/export" "/export" "-v")
  332. (let ((content (call-with-input-file "/proc/mounts"
  333. get-string-all)))
  334. (call-with-output-file "/export/mounts"
  335. (lambda (port)
  336. (display content port))))))
  337. client-marionette)
  338. ;;; Check whether NFS client host communicated with NFS server host.
  339. (test-assert "nfs client deposited file"
  340. (wait-for-file "/export/mounts" server-marionette))
  341. (marionette-eval
  342. '(begin
  343. (current-output-port
  344. (open-file "/dev/console" "w0"))
  345. (call-with-input-file "/export/mounts" display))
  346. server-marionette)
  347. (test-end))))
  348. (gexp->derivation "nfs-full-test" test))
  349. (define %test-nfs-full
  350. (system-test
  351. (name "nfs-full")
  352. (description "Test that an NFS server can be started and the exported
  353. directory can be used by another machine.")
  354. (value (run-nfs-full-test))))