shepherd.scm 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 accounts
  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. (list (file-system-mapping
  60. (source "/etc/passwd") (target source))
  61. (file-system-mapping
  62. (source "/etc/group") (target source))))
  63. (define nscd-socket
  64. (file-system-mapping
  65. (source "/var/run/nscd") (target source)
  66. (writable? #t)))
  67. (append (cons (tmpfs "/tmp") %container-file-systems)
  68. (let ((mappings `(,@(if (memq 'net namespaces)
  69. '()
  70. (cons nscd-socket
  71. %network-file-mappings))
  72. ,@(if (and (memq 'mnt namespaces)
  73. (not (memq 'user namespaces)))
  74. accounts
  75. '())
  76. ,%store-mapping))) ;XXX: coarse-grain
  77. (map file-system-mapping->bind-mount
  78. (filter (lambda (mapping)
  79. (file-exists? (file-system-mapping-source mapping)))
  80. mappings)))))
  81. ;; XXX: Lazy-bind the Shepherd to avoid a compile-time dependency.
  82. (module-autoload! (current-module)
  83. '(shepherd service) '(read-pid-file exec-command))
  84. (define* (read-pid-file/container pid pid-file #:key (max-delay 5))
  85. "Read PID-FILE in the container namespaces of PID, which exists in a
  86. separate mount and PID name space. Return the \"outer\" PID. "
  87. (match (container-excursion* pid
  88. (lambda ()
  89. (read-pid-file pid-file
  90. #:max-delay max-delay)))
  91. (#f
  92. (catch-system-error (kill pid SIGTERM))
  93. #f)
  94. ((? integer? container-pid)
  95. ;; XXX: When COMMAND is started in a separate PID namespace, its
  96. ;; PID is always 1, but that's not what Shepherd needs to know.
  97. pid)))
  98. (define* (make-forkexec-constructor/container command
  99. #:key
  100. (namespaces
  101. (default-namespaces args))
  102. (mappings '())
  103. (user #f)
  104. (group #f)
  105. (log-file #f)
  106. pid-file
  107. (pid-file-timeout 5)
  108. (directory "/")
  109. (environment-variables
  110. (environ))
  111. #:rest args)
  112. "This is a variant of 'make-forkexec-constructor' that starts COMMAND in
  113. NAMESPACES, a list of Linux namespaces such as '(mnt ipc). MAPPINGS is the
  114. list of <file-system-mapping> to make in the case of a separate mount
  115. namespace, in addition to essential bind-mounts such /proc."
  116. (define container-directory
  117. (match command
  118. ((program _ ...)
  119. (string-append "/var/run/containers/" (basename program)))))
  120. (define auto-mappings
  121. `(,@(if log-file
  122. (list (file-system-mapping
  123. (source log-file)
  124. (target source)
  125. (writable? #t)))
  126. '())))
  127. (define mounts
  128. (append (map file-system-mapping->bind-mount
  129. (append auto-mappings mappings))
  130. (default-mounts #:namespaces namespaces)))
  131. (lambda args
  132. (mkdir-p container-directory)
  133. (when log-file
  134. ;; Create LOG-FILE so we can map it in the container.
  135. (unless (file-exists? log-file)
  136. (call-with-output-file log-file (const #t))))
  137. (let ((pid (run-container container-directory
  138. mounts namespaces 1
  139. (lambda ()
  140. (mkdir-p "/var/run")
  141. (clean-up pid-file)
  142. (clean-up log-file)
  143. (exec-command command
  144. #:user user
  145. #:group group
  146. #:log-file log-file
  147. #:directory directory
  148. #:environment-variables
  149. environment-variables)))))
  150. (if pid-file
  151. (if (or (memq 'mnt namespaces) (memq 'pid namespaces))
  152. (read-pid-file/container pid pid-file
  153. #:max-delay pid-file-timeout)
  154. (read-pid-file pid-file #:max-delay pid-file-timeout))
  155. pid))))
  156. ;; Local Variables:
  157. ;; eval: (put 'container-excursion* 'scheme-indent-function 1)
  158. ;; End:
  159. ;;; shepherd.scm ends here