containers.scm 11 KB

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