linux-container.scm 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2015 David Thompson <davet@gnu.org>
  3. ;;; Copyright © 2016, 2017 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 (gnu system linux-container)
  20. #:use-module (ice-9 match)
  21. #:use-module (srfi srfi-1)
  22. #:use-module (guix config)
  23. #:use-module (guix store)
  24. #:use-module (guix gexp)
  25. #:use-module (guix derivations)
  26. #:use-module (guix monads)
  27. #:use-module (guix modules)
  28. #:use-module (gnu build linux-container)
  29. #:use-module (gnu services)
  30. #:use-module (gnu system)
  31. #:use-module (gnu system file-systems)
  32. #:export (system-container
  33. containerized-operating-system
  34. container-script))
  35. (define (containerized-operating-system os mappings)
  36. "Return an operating system based on OS for use in a Linux container
  37. environment. MAPPINGS is a list of <file-system-mapping> to realize in the
  38. containerized OS."
  39. (define user-file-systems
  40. (remove (lambda (fs)
  41. (let ((target (file-system-mount-point fs))
  42. (source (file-system-device fs)))
  43. (or (string=? target (%store-prefix))
  44. (string=? target "/")
  45. (and (string? source)
  46. (string-prefix? "/dev/" source))
  47. (string-prefix? "/dev" target)
  48. (string-prefix? "/sys" target))))
  49. (operating-system-file-systems os)))
  50. (define (mapping->fs fs)
  51. (file-system (inherit (file-system-mapping->bind-mount fs))
  52. (needed-for-boot? #t)))
  53. (operating-system (inherit os)
  54. (swap-devices '()) ; disable swap
  55. (file-systems (append (map mapping->fs (cons %store-mapping mappings))
  56. %container-file-systems
  57. user-file-systems))))
  58. (define* (container-script os #:key (mappings '()))
  59. "Return a derivation of a script that runs OS as a Linux container.
  60. MAPPINGS is a list of <file-system> objects that specify the files/directories
  61. that will be shared with the host system."
  62. (let* ((os (containerized-operating-system os mappings))
  63. (file-systems (filter file-system-needed-for-boot?
  64. (operating-system-file-systems os)))
  65. (specs (map file-system->spec file-systems)))
  66. (mlet* %store-monad ((os-drv (operating-system-derivation
  67. os
  68. #:container? #t)))
  69. (define script
  70. (with-imported-modules (source-module-closure
  71. '((guix build utils)
  72. (gnu build linux-container)))
  73. #~(begin
  74. (use-modules (gnu build linux-container)
  75. (gnu system file-systems) ;spec->file-system
  76. (guix build utils))
  77. (call-with-container (map spec->file-system '#$specs)
  78. (lambda ()
  79. (setenv "HOME" "/root")
  80. (setenv "TMPDIR" "/tmp")
  81. (setenv "GUIX_NEW_SYSTEM" #$os-drv)
  82. (for-each mkdir-p '("/run" "/bin" "/etc" "/home" "/var"))
  83. (primitive-load (string-append #$os-drv "/boot")))
  84. ;; A range of 65536 uid/gids is used to cover 16 bits worth of
  85. ;; users and groups, which is sufficient for most cases.
  86. ;;
  87. ;; See: http://www.freedesktop.org/software/systemd/man/systemd-nspawn.html#--private-users=
  88. #:host-uids 65536))))
  89. (gexp->script "run-container" script))))