shepherd.scm 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2017 Ludovic Courtès <ludo@gnu.org>
  3. ;;;
  4. ;;; This file is part of GNU Guix.
  5. ;;;
  6. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  7. ;;; under the terms of the GNU General Public License as published by
  8. ;;; the Free Software Foundation; either version 3 of the License, or (at
  9. ;;; your option) any later version.
  10. ;;;
  11. ;;; GNU Guix is distributed in the hope that it will be useful, but
  12. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;;; GNU General Public License for more details.
  15. ;;;
  16. ;;; You should have received a copy of the GNU General Public License
  17. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  18. (define-module (gnu build shepherd)
  19. #:use-module (gnu system file-systems)
  20. #:use-module (gnu build linux-container)
  21. #:use-module (guix build utils)
  22. #:use-module (srfi srfi-1)
  23. #:use-module (ice-9 match)
  24. #:export (make-forkexec-constructor/container))
  25. ;;; Commentary:
  26. ;;;
  27. ;;; This module provides extensions to the GNU Shepherd. In particular, it
  28. ;;; provides a helper to start services in a container.
  29. ;;;
  30. ;;; Code:
  31. (define (clean-up file)
  32. (when file
  33. (catch 'system-error
  34. (lambda ()
  35. (delete-file file))
  36. (lambda args
  37. (unless (= ENOENT (system-error-errno args))
  38. (apply throw args))))))
  39. (define-syntax-rule (catch-system-error exp)
  40. (catch 'system-error
  41. (lambda ()
  42. exp)
  43. (const #f)))
  44. (define (default-namespaces args)
  45. ;; Most daemons are here to talk to the network, and most of them expect to
  46. ;; run under a non-zero UID.
  47. (fold delq %namespaces '(net user)))
  48. (define* (default-mounts #:key (namespaces (default-namespaces '())))
  49. (define (tmpfs directory)
  50. (file-system
  51. (device "none")
  52. (title 'device)
  53. (mount-point directory)
  54. (type "tmpfs")
  55. (check? #f)))
  56. (define passwd
  57. ;; This is for processes in the default user namespace but living in a
  58. ;; different mount namespace, so that they can lookup users.
  59. (file-system-mapping
  60. (source "/etc/passwd") (target source)))
  61. (define nscd-socket
  62. (file-system-mapping
  63. (source "/var/run/nscd") (target source)
  64. (writable? #t)))
  65. (append (cons (tmpfs "/tmp") %container-file-systems)
  66. (let ((mappings `(,@(if (memq 'net namespaces)
  67. '()
  68. (cons nscd-socket
  69. %network-file-mappings))
  70. ,@(if (and (memq 'mnt namespaces)
  71. (not (memq 'user namespaces)))
  72. (list passwd)
  73. '())
  74. ,%store-mapping))) ;XXX: coarse-grain
  75. (map file-system-mapping->bind-mount
  76. (filter (lambda (mapping)
  77. (file-exists? (file-system-mapping-source mapping)))
  78. mappings)))))
  79. ;; XXX: Lazy-bind the Shepherd to avoid a compile-time dependency.
  80. (module-autoload! (current-module)
  81. '(shepherd service) '(read-pid-file exec-command))
  82. (define* (read-pid-file/container pid pid-file #:key (max-delay 5))
  83. "Read PID-FILE in the container namespaces of PID, which exists in a
  84. separate mount and PID name space. Return the \"outer\" PID. "
  85. (match (container-excursion* pid
  86. (lambda ()
  87. (read-pid-file pid-file
  88. #:max-delay max-delay)))
  89. (#f
  90. (catch-system-error (kill pid SIGTERM))
  91. #f)
  92. ((? integer? container-pid)
  93. ;; XXX: When COMMAND is started in a separate PID namespace, its
  94. ;; PID is always 1, but that's not what Shepherd needs to know.
  95. pid)))
  96. (define* (make-forkexec-constructor/container command
  97. #:key
  98. (namespaces
  99. (default-namespaces args))
  100. (mappings '())
  101. (user #f)
  102. (group #f)
  103. (log-file #f)
  104. pid-file
  105. (pid-file-timeout 5)
  106. (directory "/")
  107. (environment-variables
  108. (environ))
  109. #:rest args)
  110. "This is a variant of 'make-forkexec-constructor' that starts COMMAND in
  111. NAMESPACES, a list of Linux namespaces such as '(mnt ipc). MAPPINGS is the
  112. list of <file-system-mapping> to make in the case of a separate mount
  113. namespace, in addition to essential bind-mounts such /proc."
  114. (define container-directory
  115. (match command
  116. ((program _ ...)
  117. (string-append "/var/run/containers/" (basename program)))))
  118. (define auto-mappings
  119. `(,@(if log-file
  120. (list (file-system-mapping
  121. (source log-file)
  122. (target source)
  123. (writable? #t)))
  124. '())))
  125. (define mounts
  126. (append (map file-system-mapping->bind-mount
  127. (append auto-mappings mappings))
  128. (default-mounts #:namespaces namespaces)))
  129. (lambda args
  130. (mkdir-p container-directory)
  131. (when log-file
  132. ;; Create LOG-FILE so we can map it in the container.
  133. (unless (file-exists? log-file)
  134. (call-with-output-file log-file (const #t))))
  135. (let ((pid (run-container container-directory
  136. mounts namespaces 1
  137. (lambda ()
  138. (mkdir-p "/var/run")
  139. (clean-up pid-file)
  140. (clean-up log-file)
  141. (exec-command command
  142. #:user user
  143. #:group group
  144. #:log-file log-file
  145. #:directory directory
  146. #:environment-variables
  147. environment-variables)))))
  148. (if pid-file
  149. (if (or (memq 'mnt namespaces) (memq 'pid namespaces))
  150. (read-pid-file/container pid pid-file
  151. #:max-delay pid-file-timeout)
  152. (read-pid-file pid-file #:max-delay pid-file-timeout))
  153. pid))))
  154. ;; Local Variables:
  155. ;; eval: (put 'container-excursion* 'scheme-indent-function 1)
  156. ;; End:
  157. ;;; shepherd.scm ends here