| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 | |
| 19 |
|
| 20 | (define-module (gnu system linux-container)
|
| 21 | #:use-module (ice-9 match)
|
| 22 | #:use-module (srfi srfi-1)
|
| 23 | #:use-module (guix config)
|
| 24 | #:use-module (guix store)
|
| 25 | #:use-module (guix gexp)
|
| 26 | #:use-module (guix derivations)
|
| 27 | #:use-module (guix monads)
|
| 28 | #:use-module (guix modules)
|
| 29 | #:use-module (gnu build linux-container)
|
| 30 | #:use-module (gnu services)
|
| 31 | #:use-module (gnu system)
|
| 32 | #:use-module (gnu system file-systems)
|
| 33 | #:export (system-container
|
| 34 | containerized-operating-system
|
| 35 | container-script))
|
| 36 |
|
| 37 | (define (containerized-operating-system os mappings)
|
| 38 | "Return an operating system based on OS for use in a Linux container
|
| 39 | environment. MAPPINGS is a list of <file-system-mapping> to realize in the
|
| 40 | containerized OS."
|
| 41 | (define user-file-systems
|
| 42 | (remove (lambda (fs)
|
| 43 | (let ((target (file-system-mount-point fs))
|
| 44 | (source (file-system-device fs)))
|
| 45 | (or (string=? target (%store-prefix))
|
| 46 | (string=? target "/")
|
| 47 | (and (string? source)
|
| 48 | (string-prefix? "/dev/" source))
|
| 49 | (string-prefix? "/dev" target)
|
| 50 | (string-prefix? "/sys" target))))
|
| 51 | (operating-system-file-systems os)))
|
| 52 |
|
| 53 | (define (mapping->fs fs)
|
| 54 | (file-system (inherit (file-system-mapping->bind-mount fs))
|
| 55 | (needed-for-boot? #t)))
|
| 56 |
|
| 57 | (operating-system (inherit os)
|
| 58 | (swap-devices '()) |
| 59 | (file-systems (append (map mapping->fs (cons %store-mapping mappings))
|
| 60 | %container-file-systems
|
| 61 | user-file-systems))))
|
| 62 |
|
| 63 | (define* (container-script os #:key (mappings '()))
|
| 64 | "Return a derivation of a script that runs OS as a Linux container.
|
| 65 | MAPPINGS is a list of <file-system> objects that specify the files/directories
|
| 66 | that will be shared with the host system."
|
| 67 | (let* ((os (containerized-operating-system os mappings))
|
| 68 | (file-systems (filter file-system-needed-for-boot?
|
| 69 | (operating-system-file-systems os)))
|
| 70 | (specs (map file-system->spec file-systems)))
|
| 71 |
|
| 72 | (mlet* %store-monad ((os-drv (operating-system-derivation
|
| 73 | os
|
| 74 | #:container? #t)))
|
| 75 |
|
| 76 | (define script
|
| 77 | (with-imported-modules (source-module-closure
|
| 78 | '((guix build utils)
|
| 79 | (gnu build linux-container)))
|
| 80 | #~(begin
|
| 81 | (use-modules (gnu build linux-container)
|
| 82 | (gnu system file-systems) |
| 83 | (guix build utils))
|
| 84 |
|
| 85 | (call-with-container (map spec->file-system '#$specs)
|
| 86 | (lambda ()
|
| 87 | (setenv "HOME" "/root")
|
| 88 | (setenv "TMPDIR" "/tmp")
|
| 89 | (setenv "GUIX_NEW_SYSTEM" #$os-drv)
|
| 90 | (for-each mkdir-p '("/run" "/bin" "/etc" "/home" "/var"))
|
| 91 | (primitive-load (string-append #$os-drv "/boot")))
|
| 92 | |
| 93 | |
| 94 | |
| 95 | |
| 96 | #:host-uids 65536))))
|
| 97 |
|
| 98 | (gexp->script "run-container" script))))
|