containers.scm 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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 "container-excursion"
  125. (call-with-temporary-directory
  126. (lambda (root)
  127. ;; Two pipes: One for the container to signal that the test can begin,
  128. ;; and one for the parent to signal to the container that the test is
  129. ;; over.
  130. (match (list (pipe) (pipe))
  131. (((start-in . start-out) (end-in . end-out))
  132. (define (container)
  133. (close end-out)
  134. (close start-in)
  135. ;; Signal for the test to start.
  136. (write 'ready start-out)
  137. (close start-out)
  138. ;; Wait for test completion.
  139. (read end-in)
  140. (close end-in))
  141. (define (namespaces pid)
  142. (let ((pid (number->string pid)))
  143. (map (lambda (ns)
  144. (readlink (string-append "/proc/" pid "/ns/" ns)))
  145. '("user" "ipc" "uts" "net" "pid" "mnt"))))
  146. (let* ((pid (run-container root '() %namespaces 1 container))
  147. (container-namespaces (namespaces pid))
  148. (result
  149. (begin
  150. (close start-out)
  151. ;; Wait for container to be ready.
  152. (read start-in)
  153. (close start-in)
  154. (container-excursion pid
  155. (lambda ()
  156. ;; Fork again so that the pid is within the context of
  157. ;; the joined pid namespace instead of the original pid
  158. ;; namespace.
  159. (match (primitive-fork)
  160. (0
  161. ;; Check that all of the namespace identifiers are
  162. ;; the same as the container process.
  163. (assert-exit
  164. (equal? container-namespaces
  165. (namespaces (getpid)))))
  166. (fork-pid
  167. (match (waitpid fork-pid)
  168. ((_ . status)
  169. (primitive-exit
  170. (status:exit-val status)))))))))))
  171. (close end-in)
  172. ;; Stop the container.
  173. (write 'done end-out)
  174. (close end-out)
  175. (waitpid pid)
  176. (zero? result)))))))
  177. (skip-if-unsupported)
  178. (test-equal "container-excursion, same namespaces"
  179. 42
  180. ;; The parent and child are in the same namespaces. 'container-excursion'
  181. ;; should notice that and avoid calling 'setns' since that would fail.
  182. (container-excursion (getpid)
  183. (lambda ()
  184. (primitive-exit 42))))
  185. (skip-if-unsupported)
  186. (test-assert "container-excursion*"
  187. (call-with-temporary-directory
  188. (lambda (root)
  189. (define (namespaces pid)
  190. (let ((pid (number->string pid)))
  191. (map (lambda (ns)
  192. (readlink (string-append "/proc/" pid "/ns/" ns)))
  193. '("user" "ipc" "uts" "net" "pid" "mnt"))))
  194. (let* ((pid (run-container root '()
  195. %namespaces 1
  196. (lambda ()
  197. (sleep 100))))
  198. (expected (namespaces pid))
  199. (result (container-excursion* pid
  200. (lambda ()
  201. (namespaces 1)))))
  202. (kill pid SIGKILL)
  203. (equal? result expected)))))
  204. (skip-if-unsupported)
  205. (test-equal "container-excursion*, same namespaces"
  206. 42
  207. (container-excursion* (getpid)
  208. (lambda ()
  209. (* 6 7))))
  210. (skip-if-unsupported)
  211. (test-equal "eval/container, exit status"
  212. 42
  213. (let* ((store (open-connection-for-tests))
  214. (status (run-with-store store
  215. (eval/container #~(exit 42)))))
  216. (close-connection store)
  217. (status:exit-val status)))
  218. (skip-if-unsupported)
  219. (test-assert "eval/container, writable user mapping"
  220. (call-with-temporary-directory
  221. (lambda (directory)
  222. (define store
  223. (open-connection-for-tests))
  224. (define result
  225. (string-append directory "/r"))
  226. (define requisites*
  227. (store-lift requisites))
  228. (call-with-output-file result (const #t))
  229. (run-with-store store
  230. (mlet %store-monad ((status (eval/container
  231. #~(begin
  232. (use-modules (ice-9 ftw))
  233. (call-with-output-file "/result"
  234. (lambda (port)
  235. (write (scandir #$(%store-prefix))
  236. port))))
  237. #:mappings
  238. (list (file-system-mapping
  239. (source result)
  240. (target "/result")
  241. (writable? #t)))))
  242. (reqs (requisites*
  243. (list (derivation->output-path
  244. (%guile-for-build))))))
  245. (close-connection store)
  246. (return (and (zero? (pk 'status status))
  247. (lset= string=? (cons* "." ".." (map basename reqs))
  248. (pk (call-with-input-file result read))))))))))
  249. (skip-if-unsupported)
  250. (test-assert "eval/container, non-empty load path"
  251. (call-with-temporary-directory
  252. (lambda (directory)
  253. (define store
  254. (open-connection-for-tests))
  255. (define result
  256. (string-append directory "/r"))
  257. (define requisites*
  258. (store-lift requisites))
  259. (mkdir result)
  260. (run-with-store store
  261. (mlet %store-monad ((status (eval/container
  262. (with-imported-modules '((guix build utils))
  263. #~(begin
  264. (use-modules (guix build utils))
  265. (mkdir-p "/result/a/b/c")))
  266. #:mappings
  267. (list (file-system-mapping
  268. (source result)
  269. (target "/result")
  270. (writable? #t))))))
  271. (close-connection store)
  272. (return (and (zero? status)
  273. (file-is-directory?
  274. (string-append result "/a/b/c")))))))))
  275. (test-end)