containers.scm 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2015 David Thompson <davet@gnu.org>
  3. ;;; Copyright © 2016, 2017, 2019 Ludovic Courtès <ludo@gnu.org>
  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. (define-module (test-containers)
  20. #:use-module (guix utils)
  21. #:use-module (guix build syscalls)
  22. #:use-module (gnu build linux-container)
  23. #:use-module ((gnu system linux-container)
  24. #:select (eval/container))
  25. #:use-module (gnu system file-systems)
  26. #:use-module (guix store)
  27. #:use-module (guix monads)
  28. #:use-module (guix gexp)
  29. #:use-module (guix derivations)
  30. #:use-module (guix tests)
  31. #:use-module (srfi srfi-1)
  32. #:use-module (srfi srfi-64)
  33. #:use-module (ice-9 match))
  34. (define (assert-exit x)
  35. (primitive-exit (if x 0 1)))
  36. (test-begin "containers")
  37. ;; Skip these tests unless user namespaces are available and the setgroups
  38. ;; file (introduced in Linux 3.19 to address a security issue) exists.
  39. (define (skip-if-unsupported)
  40. (unless (and (user-namespace-supported?)
  41. (unprivileged-user-namespace-supported?)
  42. (setgroups-supported?))
  43. (test-skip 1)))
  44. (skip-if-unsupported)
  45. (test-assert "call-with-container, exit with 0 when there is no error"
  46. (zero?
  47. (call-with-container '() (const #t) #:namespaces '(user))))
  48. (skip-if-unsupported)
  49. (test-assert "call-with-container, user namespace"
  50. (zero?
  51. (call-with-container '()
  52. (lambda ()
  53. ;; The user is root within the new user namespace.
  54. (assert-exit (and (zero? (getuid)) (zero? (getgid)))))
  55. #:namespaces '(user))))
  56. (skip-if-unsupported)
  57. (test-assert "call-with-container, user namespace, guest UID/GID"
  58. (zero?
  59. (call-with-container '()
  60. (lambda ()
  61. (assert-exit (and (= 42 (getuid)) (= 77 (getgid)))))
  62. #:guest-uid 42
  63. #:guest-gid 77
  64. #:namespaces '(user))))
  65. (skip-if-unsupported)
  66. (test-assert "call-with-container, uts namespace"
  67. (zero?
  68. (call-with-container '()
  69. (lambda ()
  70. ;; The user is root within the container and should be able to change
  71. ;; the hostname of that container.
  72. (sethostname "test-container")
  73. (primitive-exit 0))
  74. #:namespaces '(user uts))))
  75. (skip-if-unsupported)
  76. (test-assert "call-with-container, pid namespace"
  77. (zero?
  78. (call-with-container '()
  79. (lambda ()
  80. (match (primitive-fork)
  81. (0
  82. ;; The first forked process in the new pid namespace is pid 2.
  83. (assert-exit (= 2 (getpid))))
  84. (pid
  85. (primitive-exit
  86. (match (waitpid pid)
  87. ((_ . status)
  88. (status:exit-val status)))))))
  89. #:namespaces '(user pid))))
  90. (skip-if-unsupported)
  91. (test-assert "call-with-container, mnt namespace"
  92. (zero?
  93. (call-with-container (list (file-system
  94. (device "none")
  95. (mount-point "/testing")
  96. (type "tmpfs")
  97. (check? #f)))
  98. (lambda ()
  99. (assert-exit (file-exists? "/testing")))
  100. #:namespaces '(user mnt))))
  101. (skip-if-unsupported)
  102. (test-equal "call-with-container, mnt namespace, wrong bind mount"
  103. `(system-error ,ENOENT)
  104. ;; An exception should be raised; see <http://bugs.gnu.org/23306>.
  105. (catch 'system-error
  106. (lambda ()
  107. (call-with-container (list (file-system
  108. (device "/does-not-exist")
  109. (mount-point "/foo")
  110. (type "none")
  111. (flags '(bind-mount))
  112. (check? #f)))
  113. (const #t)
  114. #:namespaces '(user mnt)))
  115. (lambda args
  116. (list 'system-error (system-error-errno args)))))
  117. (skip-if-unsupported)
  118. (test-assert "call-with-container, all namespaces"
  119. (zero?
  120. (call-with-container '()
  121. (lambda ()
  122. (primitive-exit 0)))))
  123. (skip-if-unsupported)
  124. (test-assert "call-with-container, mnt namespace, root permissions"
  125. (zero?
  126. (call-with-container '()
  127. (lambda ()
  128. (assert-exit (= #o755 (stat:perms (lstat "/")))))
  129. #:namespaces '(user mnt))))
  130. (skip-if-unsupported)
  131. (test-assert "container-excursion"
  132. (call-with-temporary-directory
  133. (lambda (root)
  134. ;; Two pipes: One for the container to signal that the test can begin,
  135. ;; and one for the parent to signal to the container that the test is
  136. ;; over.
  137. (match (list (pipe) (pipe))
  138. (((start-in . start-out) (end-in . end-out))
  139. (define (container)
  140. (close end-out)
  141. (close start-in)
  142. ;; Signal for the test to start.
  143. (write 'ready start-out)
  144. (close start-out)
  145. ;; Wait for test completion.
  146. (read end-in)
  147. (close end-in))
  148. (define (namespaces pid)
  149. (let ((pid (number->string pid)))
  150. (map (lambda (ns)
  151. (readlink (string-append "/proc/" pid "/ns/" ns)))
  152. '("user" "ipc" "uts" "net" "pid" "mnt"))))
  153. (let* ((pid (run-container root '() %namespaces 1 container))
  154. (container-namespaces (namespaces pid))
  155. (result
  156. (begin
  157. (close start-out)
  158. ;; Wait for container to be ready.
  159. (read start-in)
  160. (close start-in)
  161. (container-excursion pid
  162. (lambda ()
  163. ;; Fork again so that the pid is within the context of
  164. ;; the joined pid namespace instead of the original pid
  165. ;; namespace.
  166. (match (primitive-fork)
  167. (0
  168. ;; Check that all of the namespace identifiers are
  169. ;; the same as the container process.
  170. (assert-exit
  171. (equal? container-namespaces
  172. (namespaces (getpid)))))
  173. (fork-pid
  174. (match (waitpid fork-pid)
  175. ((_ . status)
  176. (primitive-exit
  177. (status:exit-val status)))))))))))
  178. (close end-in)
  179. ;; Stop the container.
  180. (write 'done end-out)
  181. (close end-out)
  182. (waitpid pid)
  183. (zero? result)))))))
  184. (skip-if-unsupported)
  185. (test-equal "container-excursion, same namespaces"
  186. 42
  187. ;; The parent and child are in the same namespaces. 'container-excursion'
  188. ;; should notice that and avoid calling 'setns' since that would fail.
  189. (container-excursion (getpid)
  190. (lambda ()
  191. (primitive-exit 42))))
  192. (skip-if-unsupported)
  193. (test-assert "container-excursion*"
  194. (call-with-temporary-directory
  195. (lambda (root)
  196. (define (namespaces pid)
  197. (let ((pid (number->string pid)))
  198. (map (lambda (ns)
  199. (readlink (string-append "/proc/" pid "/ns/" ns)))
  200. '("user" "ipc" "uts" "net" "pid" "mnt"))))
  201. (let* ((pid (run-container root '()
  202. %namespaces 1
  203. (lambda ()
  204. (sleep 100))))
  205. (expected (namespaces pid))
  206. (result (container-excursion* pid
  207. (lambda ()
  208. (namespaces 1)))))
  209. (kill pid SIGKILL)
  210. (equal? result expected)))))
  211. (skip-if-unsupported)
  212. (test-equal "container-excursion*, same namespaces"
  213. 42
  214. (container-excursion* (getpid)
  215. (lambda ()
  216. (* 6 7))))
  217. (skip-if-unsupported)
  218. (test-equal "eval/container, exit status"
  219. 42
  220. (let* ((store (open-connection-for-tests))
  221. (status (run-with-store store
  222. (eval/container #~(exit 42)))))
  223. (close-connection store)
  224. (status:exit-val status)))
  225. (skip-if-unsupported)
  226. (test-assert "eval/container, writable user mapping"
  227. (call-with-temporary-directory
  228. (lambda (directory)
  229. (define store
  230. (open-connection-for-tests))
  231. (define result
  232. (string-append directory "/r"))
  233. (define requisites*
  234. (store-lift requisites))
  235. (call-with-output-file result (const #t))
  236. (run-with-store store
  237. (mlet %store-monad ((status (eval/container
  238. #~(begin
  239. (use-modules (ice-9 ftw))
  240. (call-with-output-file "/result"
  241. (lambda (port)
  242. (write (scandir #$(%store-prefix))
  243. port))))
  244. #:mappings
  245. (list (file-system-mapping
  246. (source result)
  247. (target "/result")
  248. (writable? #t)))))
  249. (reqs (requisites*
  250. (list (derivation->output-path
  251. (%guile-for-build))))))
  252. (close-connection store)
  253. (return (and (zero? (pk 'status status))
  254. (lset= string=? (cons* "." ".." (map basename reqs))
  255. (pk (call-with-input-file result read))))))))))
  256. (skip-if-unsupported)
  257. (test-assert "eval/container, non-empty load path"
  258. (call-with-temporary-directory
  259. (lambda (directory)
  260. (define store
  261. (open-connection-for-tests))
  262. (define result
  263. (string-append directory "/r"))
  264. (define requisites*
  265. (store-lift requisites))
  266. (mkdir result)
  267. (run-with-store store
  268. (mlet %store-monad ((status (eval/container
  269. (with-imported-modules '((guix build utils))
  270. #~(begin
  271. (use-modules (guix build utils))
  272. (mkdir-p "/result/a/b/c")))
  273. #:mappings
  274. (list (file-system-mapping
  275. (source result)
  276. (target "/result")
  277. (writable? #t))))))
  278. (close-connection store)
  279. (return (and (zero? status)
  280. (file-is-directory?
  281. (string-append result "/a/b/c")))))))))
  282. (test-end)