diff options
| author | Arun Isaac <arunisaac@systemreboot.net> | 2019-05-10 16:56:16 +0530 |
|---|---|---|
| committer | Arun Isaac <arunisaac@systemreboot.net> | 2019-05-14 02:54:58 +0530 |
| commit | b33454ae0b488e79faafef75a06090be6b2ac6a2 (patch) | |
| tree | 61fc01c617f20a25c605aff4bc93cefa116b6a8f | |
| parent | 1bba3e8ca89b42a2828a8553dd8b3613592960cf (diff) | |
linux-container: Support container network sharing.
* gnu/system/linux-container.scm (container-essential-services): If network is
to be shared with the host, remove network configuration files from etc
service.
(containerized-operating-system): If network is to be shared with the host,
remove nscd service and map host's /var/run/nscd if it exists.
(container-script): If network is to be shared with the host, do not create
network namespace.
* guix/scripts/system.scm (system-derivation-for-action): Add
#:container-shared-network? argument.
(perform-action): Add #:container-shared-network? argument.
(show-help): Add "-N, --network" help information.
(%options): Add network option.
(process-action): Call perform-action with #container-shared-network? argument.
* doc/guix.texi (Invoking guix system): Document the "-N, --network" option.
Co-authored-by: Christopher Baines <mail@cbaines.net>
| -rw-r--r-- | doc/guix.texi | 5 | ||||
| -rw-r--r-- | gnu/system/linux-container.scm | 63 | ||||
| -rw-r--r-- | guix/scripts/system.scm | 20 |
3 files changed, 75 insertions, 13 deletions
diff --git a/doc/guix.texi b/doc/guix.texi index 843e909fb25..27e0f72ccb3 100644 --- a/doc/guix.texi +++ b/doc/guix.texi | |||
| @@ -24587,6 +24587,11 @@ When this option is omitted, @command{guix system} computes an estimate | |||
| 24587 | of the image size as a function of the size of the system declared in | 24587 | of the image size as a function of the size of the system declared in |
| 24588 | @var{file}. | 24588 | @var{file}. |
| 24589 | 24589 | ||
| 24590 | @item --network | ||
| 24591 | @itemx -N | ||
| 24592 | For the @code{container} action, allow containers to access the host network, | ||
| 24593 | that is, do not create a network namespace. | ||
| 24594 | |||
| 24590 | @item --root=@var{file} | 24595 | @item --root=@var{file} |
| 24591 | @itemx -r @var{file} | 24596 | @itemx -r @var{file} |
| 24592 | Make @var{file} a symlink to the result, and register it as a garbage | 24597 | Make @var{file} a symlink to the result, and register it as a garbage |
diff --git a/gnu/system/linux-container.scm b/gnu/system/linux-container.scm index ded5f279fed..ce786e39b22 100644 --- a/gnu/system/linux-container.scm +++ b/gnu/system/linux-container.scm | |||
| @@ -1,6 +1,7 @@ | |||
| 1 | ;;; GNU Guix --- Functional package management for GNU | 1 | ;;; GNU Guix --- Functional package management for GNU |
| 2 | ;;; Copyright © 2015 David Thompson <davet@gnu.org> | 2 | ;;; Copyright © 2015 David Thompson <davet@gnu.org> |
| 3 | ;;; Copyright © 2016, 2017, 2019 Ludovic Courtès <ludo@gnu.org> | 3 | ;;; Copyright © 2016, 2017, 2019 Ludovic Courtès <ludo@gnu.org> |
| 4 | ;;; Copyright © 2019 Arun Isaac <arunisaac@systemreboot.net> | ||
| 4 | ;;; | 5 | ;;; |
| 5 | ;;; This file is part of GNU Guix. | 6 | ;;; This file is part of GNU Guix. |
| 6 | ;;; | 7 | ;;; |
| @@ -35,7 +36,7 @@ | |||
| 35 | containerized-operating-system | 36 | containerized-operating-system |
| 36 | container-script)) | 37 | container-script)) |
| 37 | 38 | ||
| 38 | (define (container-essential-services os) | 39 | (define* (container-essential-services os #:key shared-network?) |
| 39 | "Return a list of essential services corresponding to OS, a | 40 | "Return a list of essential services corresponding to OS, a |
| 40 | non-containerized OS. This procedure essentially strips essential services | 41 | non-containerized OS. This procedure essentially strips essential services |
| 41 | from OS that are needed on the bare metal and not in a container." | 42 | from OS that are needed on the bare metal and not in a container." |
| @@ -51,9 +52,20 @@ from OS that are needed on the bare metal and not in a container." | |||
| 51 | (let ((locale (operating-system-locale-directory os))) | 52 | (let ((locale (operating-system-locale-directory os))) |
| 52 | (with-monad %store-monad | 53 | (with-monad %store-monad |
| 53 | (return `(("locale" ,locale)))))) | 54 | (return `(("locale" ,locale)))))) |
| 54 | base)) | 55 | ;; If network is to be shared with the host, remove network |
| 56 | ;; configuration files from etc-service. | ||
| 57 | (if shared-network? | ||
| 58 | (modify-services base | ||
| 59 | (etc-service-type | ||
| 60 | files => (remove | ||
| 61 | (match-lambda | ||
| 62 | ((filename _) | ||
| 63 | (member filename | ||
| 64 | (map basename %network-configuration-files)))) | ||
| 65 | files))) | ||
| 66 | base))) | ||
| 55 | 67 | ||
| 56 | (define (containerized-operating-system os mappings) | 68 | (define* (containerized-operating-system os mappings #:key shared-network?) |
| 57 | "Return an operating system based on OS for use in a Linux container | 69 | "Return an operating system based on OS for use in a Linux container |
| 58 | environment. MAPPINGS is a list of <file-system-mapping> to realize in the | 70 | environment. MAPPINGS is a list of <file-system-mapping> to realize in the |
| 59 | containerized OS." | 71 | containerized OS." |
| @@ -76,27 +88,53 @@ containerized OS." | |||
| 76 | (define useless-services | 88 | (define useless-services |
| 77 | ;; Services that make no sense in a container. Those that attempt to | 89 | ;; Services that make no sense in a container. Those that attempt to |
| 78 | ;; access /dev/tty[0-9] in particular cannot work in a container. | 90 | ;; access /dev/tty[0-9] in particular cannot work in a container. |
| 79 | (list console-font-service-type | 91 | (append (list console-font-service-type |
| 80 | mingetty-service-type | 92 | mingetty-service-type |
| 81 | agetty-service-type)) | 93 | agetty-service-type) |
| 94 | ;; Remove nscd service if network is shared with the host. | ||
| 95 | (if shared-network? | ||
| 96 | (list nscd-service-type) | ||
| 97 | (list)))) | ||
| 98 | |||
| 99 | (define shared-network-file-mappings | ||
| 100 | ;; Files to map if network is to be shared with the host | ||
| 101 | (append %network-file-mappings | ||
| 102 | (let ((nscd-run-directory "/var/run/nscd")) | ||
| 103 | (if (file-exists? nscd-run-directory) | ||
| 104 | (list (file-system-mapping | ||
| 105 | (source nscd-run-directory) | ||
| 106 | (target nscd-run-directory))) | ||
| 107 | (list))))) | ||
| 108 | |||
| 109 | ;; (write shared-network-file-mappings) | ||
| 110 | ;; (newline) | ||
| 82 | 111 | ||
| 83 | (operating-system | 112 | (operating-system |
| 84 | (inherit os) | 113 | (inherit os) |
| 85 | (swap-devices '()) ; disable swap | 114 | (swap-devices '()) ; disable swap |
| 86 | (essential-services (container-essential-services os)) | 115 | (essential-services (container-essential-services |
| 116 | os #:shared-network? shared-network?)) | ||
| 87 | (services (remove (lambda (service) | 117 | (services (remove (lambda (service) |
| 88 | (memq (service-kind service) | 118 | (memq (service-kind service) |
| 89 | useless-services)) | 119 | useless-services)) |
| 90 | (operating-system-user-services os))) | 120 | (operating-system-user-services os))) |
| 91 | (file-systems (append (map mapping->fs (cons %store-mapping mappings)) | 121 | (file-systems (append (map mapping->fs |
| 122 | (cons %store-mapping | ||
| 123 | (append mappings | ||
| 124 | (if shared-network? | ||
| 125 | shared-network-file-mappings | ||
| 126 | (list))))) | ||
| 92 | %container-file-systems | 127 | %container-file-systems |
| 93 | user-file-systems)))) | 128 | user-file-systems)))) |
| 94 | 129 | ||
| 95 | (define* (container-script os #:key (mappings '())) | 130 | (define* (container-script os #:key (mappings '()) shared-network?) |
| 96 | "Return a derivation of a script that runs OS as a Linux container. | 131 | "Return a derivation of a script that runs OS as a Linux container. |
| 97 | MAPPINGS is a list of <file-system> objects that specify the files/directories | 132 | MAPPINGS is a list of <file-system> objects that specify the files/directories |
| 98 | that will be shared with the host system." | 133 | that will be shared with the host system." |
| 99 | (let* ((os (containerized-operating-system os mappings)) | 134 | (let* ((os (containerized-operating-system |
| 135 | os | ||
| 136 | mappings | ||
| 137 | #:shared-network? shared-network?)) | ||
| 100 | (file-systems (filter file-system-needed-for-boot? | 138 | (file-systems (filter file-system-needed-for-boot? |
| 101 | (operating-system-file-systems os))) | 139 | (operating-system-file-systems os))) |
| 102 | (specs (map file-system->spec file-systems))) | 140 | (specs (map file-system->spec file-systems))) |
| @@ -121,6 +159,9 @@ that will be shared with the host system." | |||
| 121 | ;; users and groups, which is sufficient for most cases. | 159 | ;; users and groups, which is sufficient for most cases. |
| 122 | ;; | 160 | ;; |
| 123 | ;; See: http://www.freedesktop.org/software/systemd/man/systemd-nspawn.html#--private-users= | 161 | ;; See: http://www.freedesktop.org/software/systemd/man/systemd-nspawn.html#--private-users= |
| 124 | #:host-uids 65536)))) | 162 | #:host-uids 65536 |
| 163 | #:namespaces (if #$shared-network? | ||
| 164 | (delq 'net %namespaces) | ||
| 165 | %namespaces))))) | ||
| 125 | 166 | ||
| 126 | (gexp->script "run-container" script))) | 167 | (gexp->script "run-container" script))) |
diff --git a/guix/scripts/system.scm b/guix/scripts/system.scm index 3c3d6cbd5fd..cf4418f9815 100644 --- a/guix/scripts/system.scm +++ b/guix/scripts/system.scm | |||
| @@ -4,6 +4,7 @@ | |||
| 4 | ;;; Copyright © 2016, 2017, 2018 Chris Marusich <cmmarusich@gmail.com> | 4 | ;;; Copyright © 2016, 2017, 2018 Chris Marusich <cmmarusich@gmail.com> |
| 5 | ;;; Copyright © 2017 Mathieu Othacehe <m.othacehe@gmail.com> | 5 | ;;; Copyright © 2017 Mathieu Othacehe <m.othacehe@gmail.com> |
| 6 | ;;; Copyright © 2018 Ricardo Wurmus <rekado@elephly.net> | 6 | ;;; Copyright © 2018 Ricardo Wurmus <rekado@elephly.net> |
| 7 | ;;; Copyright © 2019 Christopher Baines <mail@cbaines.net> | ||
| 7 | ;;; | 8 | ;;; |
| 8 | ;;; This file is part of GNU Guix. | 9 | ;;; This file is part of GNU Guix. |
| 9 | ;;; | 10 | ;;; |
| @@ -756,13 +757,17 @@ checking this by themselves in their 'check' procedure." | |||
| 756 | 757 | ||
| 757 | (define* (system-derivation-for-action os action | 758 | (define* (system-derivation-for-action os action |
| 758 | #:key image-size file-system-type | 759 | #:key image-size file-system-type |
| 759 | full-boot? mappings) | 760 | full-boot? container-shared-network? |
| 761 | mappings) | ||
| 760 | "Return as a monadic value the derivation for OS according to ACTION." | 762 | "Return as a monadic value the derivation for OS according to ACTION." |
| 761 | (case action | 763 | (case action |
| 762 | ((build init reconfigure) | 764 | ((build init reconfigure) |
| 763 | (operating-system-derivation os)) | 765 | (operating-system-derivation os)) |
| 764 | ((container) | 766 | ((container) |
| 765 | (container-script os #:mappings mappings)) | 767 | (container-script |
| 768 | os | ||
| 769 | #:mappings mappings | ||
| 770 | #:shared-network? container-shared-network?)) | ||
| 766 | ((vm-image) | 771 | ((vm-image) |
| 767 | (system-qemu-image os #:disk-image-size image-size)) | 772 | (system-qemu-image os #:disk-image-size image-size)) |
| 768 | ((vm) | 773 | ((vm) |
| @@ -826,6 +831,7 @@ and TARGET arguments." | |||
| 826 | dry-run? derivations-only? | 831 | dry-run? derivations-only? |
| 827 | use-substitutes? bootloader-target target | 832 | use-substitutes? bootloader-target target |
| 828 | image-size file-system-type full-boot? | 833 | image-size file-system-type full-boot? |
| 834 | container-shared-network? | ||
| 829 | (mappings '()) | 835 | (mappings '()) |
| 830 | (gc-root #f)) | 836 | (gc-root #f)) |
| 831 | "Perform ACTION for OS. INSTALL-BOOTLOADER? specifies whether to install | 837 | "Perform ACTION for OS. INSTALL-BOOTLOADER? specifies whether to install |
| @@ -834,6 +840,8 @@ target root directory; IMAGE-SIZE is the size of the image to be built, for | |||
| 834 | the 'vm-image' and 'disk-image' actions. The root file system is created as a | 840 | the 'vm-image' and 'disk-image' actions. The root file system is created as a |
| 835 | FILE-SYSTEM-TYPE file system. FULL-BOOT? is used for the 'vm' action; it | 841 | FILE-SYSTEM-TYPE file system. FULL-BOOT? is used for the 'vm' action; it |
| 836 | determines whether to boot directly to the kernel or to the bootloader. | 842 | determines whether to boot directly to the kernel or to the bootloader. |
| 843 | CONTAINER-SHARED-NETWORK? determines if the container will use a separate | ||
| 844 | network namespace. | ||
| 837 | 845 | ||
| 838 | When DERIVATIONS-ONLY? is true, print the derivation file name(s) without | 846 | When DERIVATIONS-ONLY? is true, print the derivation file name(s) without |
| 839 | building anything. | 847 | building anything. |
| @@ -883,6 +891,7 @@ static checks." | |||
| 883 | #:file-system-type file-system-type | 891 | #:file-system-type file-system-type |
| 884 | #:image-size image-size | 892 | #:image-size image-size |
| 885 | #:full-boot? full-boot? | 893 | #:full-boot? full-boot? |
| 894 | #:container-shared-network? container-shared-network? | ||
| 886 | #:mappings mappings)) | 895 | #:mappings mappings)) |
| 887 | 896 | ||
| 888 | ;; For 'init' and 'reconfigure', always build BOOTCFG, even if | 897 | ;; For 'init' and 'reconfigure', always build BOOTCFG, even if |
| @@ -1020,6 +1029,8 @@ Some ACTIONS support additional ARGS.\n")) | |||
| 1020 | (display (G_ " | 1029 | (display (G_ " |
| 1021 | --share=SPEC for 'vm', share host file system according to SPEC")) | 1030 | --share=SPEC for 'vm', share host file system according to SPEC")) |
| 1022 | (display (G_ " | 1031 | (display (G_ " |
| 1032 | -N, --network for 'container', allow containers to access the network")) | ||
| 1033 | (display (G_ " | ||
| 1023 | -r, --root=FILE for 'vm', 'vm-image', 'disk-image', 'container', | 1034 | -r, --root=FILE for 'vm', 'vm-image', 'disk-image', 'container', |
| 1024 | and 'build', make FILE a symlink to the result, and | 1035 | and 'build', make FILE a symlink to the result, and |
| 1025 | register it as a garbage collector root")) | 1036 | register it as a garbage collector root")) |
| @@ -1066,6 +1077,9 @@ Some ACTIONS support additional ARGS.\n")) | |||
| 1066 | (lambda (opt name arg result) | 1077 | (lambda (opt name arg result) |
| 1067 | (alist-cons 'image-size (size->number arg) | 1078 | (alist-cons 'image-size (size->number arg) |
| 1068 | result))) | 1079 | result))) |
| 1080 | (option '(#\N "network") #f #f | ||
| 1081 | (lambda (opt name arg result) | ||
| 1082 | (alist-cons 'container-shared-network? #t result))) | ||
| 1069 | (option '("no-bootloader" "no-grub") #f #f | 1083 | (option '("no-bootloader" "no-grub") #f #f |
| 1070 | (lambda (opt name arg result) | 1084 | (lambda (opt name arg result) |
| 1071 | (alist-cons 'install-bootloader? #f result))) | 1085 | (alist-cons 'install-bootloader? #f result))) |
| @@ -1182,6 +1196,8 @@ resulting from command-line parsing." | |||
| 1182 | #:file-system-type (assoc-ref opts 'file-system-type) | 1196 | #:file-system-type (assoc-ref opts 'file-system-type) |
| 1183 | #:image-size (assoc-ref opts 'image-size) | 1197 | #:image-size (assoc-ref opts 'image-size) |
| 1184 | #:full-boot? (assoc-ref opts 'full-boot?) | 1198 | #:full-boot? (assoc-ref opts 'full-boot?) |
| 1199 | #:container-shared-network? | ||
| 1200 | (assoc-ref opts 'container-shared-network?) | ||
| 1185 | #:mappings (filter-map (match-lambda | 1201 | #:mappings (filter-map (match-lambda |
| 1186 | (('file-system-mapping . m) | 1202 | (('file-system-mapping . m) |
| 1187 | m) | 1203 | m) |
