You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

179 lines
6.7 KiB

  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2020 Maxim Cournoyer <maxim.cournoyer@gmail.com>
  3. ;;; Copyright © 2020 Brice Waegeneire <brice@waegenei.re>
  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 services linux)
  20. #:use-module (guix gexp)
  21. #:use-module (guix records)
  22. #:use-module (guix modules)
  23. #:use-module (gnu services)
  24. #:use-module (gnu services shepherd)
  25. #:use-module (gnu packages linux)
  26. #:use-module (srfi srfi-1)
  27. #:use-module (srfi srfi-26)
  28. #:use-module (srfi srfi-34)
  29. #:use-module (srfi srfi-35)
  30. #:use-module (ice-9 match)
  31. #:export (earlyoom-configuration
  32. earlyoom-configuration?
  33. earlyoom-configuration-earlyoom
  34. earlyoom-configuration-minimum-available-memory
  35. earlyoom-configuration-minimum-free-swap
  36. earlyoom-configuration-prefer-regexp
  37. earlyoom-configuration-avoid-regexp
  38. earlyoom-configuration-memory-report-interval
  39. earlyoom-configuration-ignore-positive-oom-score-adj?
  40. earlyoom-configuration-show-debug-messages?
  41. earlyoom-configuration-send-notification-command
  42. earlyoom-service-type
  43. kernel-module-loader-service-type))
  44. ;;;
  45. ;;; Early OOM daemon.
  46. ;;;
  47. (define-record-type* <earlyoom-configuration>
  48. earlyoom-configuration make-earlyoom-configuration
  49. earlyoom-configuration?
  50. (earlyoom earlyoom-configuration-earlyoom
  51. (default earlyoom))
  52. (minimum-available-memory earlyoom-configuration-minimum-available-memory
  53. (default 10)) ; in percent
  54. (minimum-free-swap earlyoom-configuration-minimum-free-swap
  55. (default 10)) ; in percent
  56. (prefer-regexp earlyoom-configuration-prefer-regexp ; <string>
  57. (default #f))
  58. (avoid-regexp earlyoom-configuration-avoid-regexp ; <string>
  59. (default #f))
  60. (memory-report-interval earlyoom-configuration-memory-report-interval
  61. (default 0)) ; in seconds; 0 means disabled
  62. (ignore-positive-oom-score-adj?
  63. earlyoom-configuration-ignore-positive-oom-score-adj? (default #f))
  64. (run-with-higher-priority? earlyoom-configuration-run-with-higher-priority?
  65. (default #f))
  66. (show-debug-messages? earlyoom-configuration-show-debug-messages?
  67. (default #f))
  68. (send-notification-command
  69. earlyoom-configuration-send-notification-command ; <string>
  70. (default #f)))
  71. (define (earlyoom-configuration->command-line-args config)
  72. "Translate a <earlyoom-configuration> object to its command line arguments
  73. representation."
  74. (match config
  75. (($ <earlyoom-configuration> earlyoom minimum-available-memory
  76. minimum-free-swap prefer-regexp avoid-regexp
  77. memory-report-interval
  78. ignore-positive-oom-score-adj?
  79. run-with-higher-priority? show-debug-messages?
  80. send-notification-command)
  81. `(,(file-append earlyoom "/bin/earlyoom")
  82. ,@(if minimum-available-memory
  83. (list "-m" (format #f "~s" minimum-available-memory))
  84. '())
  85. ,@(if minimum-free-swap
  86. (list "-s" (format #f "~s" minimum-free-swap))
  87. '())
  88. ,@(if prefer-regexp
  89. (list "--prefer" prefer-regexp)
  90. '())
  91. ,@(if avoid-regexp
  92. (list "--avoid" avoid-regexp)
  93. '())
  94. "-r" ,(format #f "~s" memory-report-interval)
  95. ,@(if ignore-positive-oom-score-adj?
  96. (list "-i")
  97. '())
  98. ,@(if run-with-higher-priority?
  99. (list "-p")
  100. '())
  101. ,@(if show-debug-messages?
  102. (list "-d")
  103. '())
  104. ,@(if send-notification-command
  105. (list "-N" send-notification-command)
  106. '())))))
  107. (define (earlyoom-shepherd-service config)
  108. (shepherd-service
  109. (documentation "Run the Early OOM daemon.")
  110. (provision '(earlyoom))
  111. (start #~(make-forkexec-constructor
  112. '#$(earlyoom-configuration->command-line-args config)
  113. #:log-file "/var/log/earlyoom.log"))
  114. (stop #~(make-kill-destructor))))
  115. (define earlyoom-service-type
  116. (service-type
  117. (name 'earlyoom)
  118. (default-value (earlyoom-configuration))
  119. (extensions
  120. (list (service-extension shepherd-root-service-type
  121. (compose list earlyoom-shepherd-service))))
  122. (description "Run @command{earlyoom}, the Early OOM daemon.")))
  123. ;;;
  124. ;;; Kernel module loader.
  125. ;;;
  126. (define kernel-module-loader-shepherd-service
  127. (match-lambda
  128. ((and (? list? kernel-modules) ((? string?) ...))
  129. (list
  130. (shepherd-service
  131. (documentation "Load kernel modules.")
  132. (provision '(kernel-module-loader))
  133. (requirement '(file-systems))
  134. (one-shot? #t)
  135. (modules `((srfi srfi-1)
  136. (srfi srfi-34)
  137. (srfi srfi-35)
  138. (rnrs io ports)
  139. ,@%default-modules))
  140. (start
  141. #~(lambda _
  142. (cond
  143. ((null? '#$kernel-modules) #t)
  144. ((file-exists? "/proc/sys/kernel/modprobe")
  145. (let ((modprobe (call-with-input-file
  146. "/proc/sys/kernel/modprobe" get-line)))
  147. (guard (c ((message-condition? c)
  148. (format (current-error-port) "~a~%"
  149. (condition-message c))
  150. #f))
  151. (every (lambda (module)
  152. (invoke/quiet modprobe "--" module))
  153. '#$kernel-modules))))
  154. (else
  155. (format (current-error-port) "error: ~a~%"
  156. "Kernel is missing loadable module support.")
  157. #f)))))))))
  158. (define kernel-module-loader-service-type
  159. (service-type
  160. (name 'kernel-module-loader)
  161. (description "Load kernel modules.")
  162. (extensions
  163. (list (service-extension shepherd-root-service-type
  164. kernel-module-loader-shepherd-service)))
  165. (compose concatenate)
  166. (extend append)
  167. (default-value '())))