diff options
| author | Ludovic Courtès <ludo@gnu.org> | 2019-07-15 16:14:31 +0200 |
|---|---|---|
| committer | Ludovic Courtès <ludo@gnu.org> | 2019-07-19 11:53:47 +0200 |
| commit | bacfec8611530dc3e849fb804b51f50b299796f0 (patch) | |
| tree | 0d114dfcf5692742ade19a7dfad829b2546835a2 | |
| parent | b41c7beb0b5b7a16656d6acf53f77eaf2a58e125 (diff) | |
linux-container: Add 'eval/container'.
* gnu/system/linux-container.scm (eval/container): New procedure.
* tests/containers.scm ("eval/container, exit status")
("eval/container, writable user mapping"): New tests.
| -rw-r--r-- | gnu/system/linux-container.scm | 49 | ||||
| -rw-r--r-- | tests/containers.scm | 50 |
2 files changed, 98 insertions, 1 deletions
diff --git a/gnu/system/linux-container.scm b/gnu/system/linux-container.scm index 61248c62b96..6273cee3d30 100644 --- a/gnu/system/linux-container.scm +++ b/gnu/system/linux-container.scm | |||
| @@ -35,7 +35,8 @@ | |||
| 35 | #:use-module (gnu system file-systems) | 35 | #:use-module (gnu system file-systems) |
| 36 | #:export (system-container | 36 | #:export (system-container |
| 37 | containerized-operating-system | 37 | containerized-operating-system |
| 38 | container-script)) | 38 | container-script |
| 39 | eval/container)) | ||
| 39 | 40 | ||
| 40 | (define* (container-essential-services os #:key shared-network?) | 41 | (define* (container-essential-services os #:key shared-network?) |
| 41 | "Return a list of essential services corresponding to OS, a | 42 | "Return a list of essential services corresponding to OS, a |
| @@ -205,3 +206,49 @@ that will be shared with the host system." | |||
| 205 | %namespaces))))) | 206 | %namespaces))))) |
| 206 | 207 | ||
| 207 | (gexp->script "run-container" script))) | 208 | (gexp->script "run-container" script))) |
| 209 | |||
| 210 | (define* (eval/container exp | ||
| 211 | #:key | ||
| 212 | (mappings '()) | ||
| 213 | (namespaces %namespaces)) | ||
| 214 | "Evaluate EXP, a gexp, in a new process executing in separate namespaces as | ||
| 215 | listed in NAMESPACES. Add MAPPINGS, a list of <file-system-mapping>, to the | ||
| 216 | set of directories visible in the process's mount namespace. Return the | ||
| 217 | process' exit status as a monadic value. | ||
| 218 | |||
| 219 | This is useful to implement processes that, unlike derivations, are not | ||
| 220 | entirely pure and need to access the outside world or to perform side | ||
| 221 | effects." | ||
| 222 | (mlet %store-monad ((lowered (lower-gexp exp))) | ||
| 223 | (define inputs | ||
| 224 | (cons (lowered-gexp-guile lowered) | ||
| 225 | (lowered-gexp-inputs lowered))) | ||
| 226 | |||
| 227 | (define items | ||
| 228 | (append (append-map derivation-input-output-paths inputs) | ||
| 229 | (lowered-gexp-sources lowered))) | ||
| 230 | |||
| 231 | (mbegin %store-monad | ||
| 232 | (built-derivations inputs) | ||
| 233 | (mlet %store-monad ((closure ((store-lift requisites) items))) | ||
| 234 | (return (call-with-container (map file-system-mapping->bind-mount | ||
| 235 | (append (map (lambda (item) | ||
| 236 | (file-system-mapping | ||
| 237 | (source item) | ||
| 238 | (target source))) | ||
| 239 | closure) | ||
| 240 | mappings)) | ||
| 241 | (lambda () | ||
| 242 | (apply execl | ||
| 243 | (string-append (derivation-input-output-path | ||
| 244 | (lowered-gexp-guile lowered)) | ||
| 245 | "/bin/guile") | ||
| 246 | "guile" | ||
| 247 | (append (map (lambda (directory) `("-L" ,directory)) | ||
| 248 | (lowered-gexp-load-path lowered)) | ||
| 249 | (map (lambda (directory) `("-C" ,directory)) | ||
| 250 | (lowered-gexp-load-compiled-path | ||
| 251 | lowered)) | ||
| 252 | (list "-c" | ||
| 253 | (object->string | ||
| 254 | (lowered-gexp-sexp lowered)))))))))))) | ||
diff --git a/tests/containers.scm b/tests/containers.scm index 37408f380d3..c6c738f2349 100644 --- a/tests/containers.scm +++ b/tests/containers.scm | |||
| @@ -21,7 +21,15 @@ | |||
| 21 | #:use-module (guix utils) | 21 | #:use-module (guix utils) |
| 22 | #:use-module (guix build syscalls) | 22 | #:use-module (guix build syscalls) |
| 23 | #:use-module (gnu build linux-container) | 23 | #:use-module (gnu build linux-container) |
| 24 | #:use-module ((gnu system linux-container) | ||
| 25 | #:select (eval/container)) | ||
| 24 | #:use-module (gnu system file-systems) | 26 | #:use-module (gnu system file-systems) |
| 27 | #:use-module (guix store) | ||
| 28 | #:use-module (guix monads) | ||
| 29 | #:use-module (guix gexp) | ||
| 30 | #:use-module (guix derivations) | ||
| 31 | #:use-module (guix tests) | ||
| 32 | #:use-module (srfi srfi-1) | ||
| 25 | #:use-module (srfi srfi-64) | 33 | #:use-module (srfi srfi-64) |
| 26 | #:use-module (ice-9 match)) | 34 | #:use-module (ice-9 match)) |
| 27 | 35 | ||
| @@ -219,4 +227,46 @@ | |||
| 219 | (lambda () | 227 | (lambda () |
| 220 | (* 6 7)))) | 228 | (* 6 7)))) |
| 221 | 229 | ||
| 230 | (skip-if-unsupported) | ||
| 231 | (test-equal "eval/container, exit status" | ||
| 232 | 42 | ||
| 233 | (let* ((store (open-connection-for-tests)) | ||
| 234 | (status (run-with-store store | ||
| 235 | (eval/container #~(exit 42))))) | ||
| 236 | (close-connection store) | ||
| 237 | (status:exit-val status))) | ||
| 238 | |||
| 239 | (skip-if-unsupported) | ||
| 240 | (test-assert "eval/container, writable user mapping" | ||
| 241 | (call-with-temporary-directory | ||
| 242 | (lambda (directory) | ||
| 243 | (define store | ||
| 244 | (open-connection-for-tests)) | ||
| 245 | (define result | ||
| 246 | (string-append directory "/r")) | ||
| 247 | (define requisites* | ||
| 248 | (store-lift requisites)) | ||
| 249 | |||
| 250 | (call-with-output-file result (const #t)) | ||
| 251 | (run-with-store store | ||
| 252 | (mlet %store-monad ((status (eval/container | ||
| 253 | #~(begin | ||
| 254 | (use-modules (ice-9 ftw)) | ||
| 255 | (call-with-output-file "/result" | ||
| 256 | (lambda (port) | ||
| 257 | (write (scandir #$(%store-prefix)) | ||
| 258 | port)))) | ||
| 259 | #:mappings | ||
| 260 | (list (file-system-mapping | ||
| 261 | (source result) | ||
| 262 | (target "/result") | ||
| 263 | (writable? #t))))) | ||
| 264 | (reqs (requisites* | ||
| 265 | (list (derivation->output-path | ||
| 266 | (%guile-for-build)))))) | ||
| 267 | (close-connection store) | ||
| 268 | (return (and (zero? (pk 'status status)) | ||
| 269 | (lset= string=? (cons* "." ".." (map basename reqs)) | ||
| 270 | (pk (call-with-input-file result read)))))))))) | ||
| 271 | |||
| 222 | (test-end) | 272 | (test-end) |
