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>
5 ;;; This file is part of GNU Guix.
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.
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.
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/>.
20 (define-module (gnu services linux)
21 #:use-module (guix gexp)
22 #:use-module (guix records)
23 #:use-module (guix modules)
24 #:use-module (gnu services)
25 #:use-module (gnu services shepherd)
26 #:use-module (gnu packages linux)
27 #:use-module (srfi srfi-1)
28 #:use-module (srfi srfi-26)
29 #:use-module (srfi srfi-34)
30 #:use-module (srfi srfi-35)
31 #:use-module (ice-9 match)
32 #:export (earlyoom-configuration
33 earlyoom-configuration?
34 earlyoom-configuration-earlyoom
35 earlyoom-configuration-minimum-available-memory
36 earlyoom-configuration-minimum-free-swap
37 earlyoom-configuration-prefer-regexp
38 earlyoom-configuration-avoid-regexp
39 earlyoom-configuration-memory-report-interval
40 earlyoom-configuration-ignore-positive-oom-score-adj?
41 earlyoom-configuration-show-debug-messages?
42 earlyoom-configuration-send-notification-command
45 kernel-module-loader-service-type))
52 (define-record-type* <earlyoom-configuration>
53 earlyoom-configuration make-earlyoom-configuration
54 earlyoom-configuration?
55 (earlyoom earlyoom-configuration-earlyoom
57 (minimum-available-memory earlyoom-configuration-minimum-available-memory
58 (default 10)) ; in percent
59 (minimum-free-swap earlyoom-configuration-minimum-free-swap
60 (default 10)) ; in percent
61 (prefer-regexp earlyoom-configuration-prefer-regexp ; <string>
63 (avoid-regexp earlyoom-configuration-avoid-regexp ; <string>
65 (memory-report-interval earlyoom-configuration-memory-report-interval
66 (default 0)) ; in seconds; 0 means disabled
67 (ignore-positive-oom-score-adj?
68 earlyoom-configuration-ignore-positive-oom-score-adj? (default #f))
69 (run-with-higher-priority? earlyoom-configuration-run-with-higher-priority?
71 (show-debug-messages? earlyoom-configuration-show-debug-messages?
73 (send-notification-command
74 earlyoom-configuration-send-notification-command ; <string>
77 (define (earlyoom-configuration->command-line-args config)
78 "Translate a <earlyoom-configuration> object to its command line arguments
81 (($ <earlyoom-configuration> earlyoom minimum-available-memory
82 minimum-free-swap prefer-regexp avoid-regexp
83 memory-report-interval
84 ignore-positive-oom-score-adj?
85 run-with-higher-priority? show-debug-messages?
86 send-notification-command)
87 `(,(file-append earlyoom "/bin/earlyoom")
88 ,@(if minimum-available-memory
89 (list "-m" (format #f "~s" minimum-available-memory))
91 ,@(if minimum-free-swap
92 (list "-s" (format #f "~s" minimum-free-swap))
95 (list "--prefer" prefer-regexp)
98 (list "--avoid" avoid-regexp)
100 "-r" ,(format #f "~s" memory-report-interval)
101 ,@(if ignore-positive-oom-score-adj?
104 ,@(if run-with-higher-priority?
107 ,@(if show-debug-messages?
110 ,@(if send-notification-command
111 (list "-N" send-notification-command)
114 (define (earlyoom-shepherd-service config)
116 (documentation "Run the Early OOM daemon.")
117 (provision '(earlyoom))
118 (start #~(make-forkexec-constructor
119 '#$(earlyoom-configuration->command-line-args config)
120 #:log-file "/var/log/earlyoom.log"))
121 (stop #~(make-kill-destructor))))
123 (define earlyoom-service-type
126 (default-value (earlyoom-configuration))
128 (list (service-extension shepherd-root-service-type
129 (compose list earlyoom-shepherd-service))))
130 (description "Run @command{earlyoom}, the Early OOM daemon.")))
134 ;;; Kernel module loader.
137 (define kernel-module-loader-shepherd-service
139 ((and (? list? kernel-modules) ((? string?) ...))
142 (documentation "Load kernel modules.")
143 (provision '(kernel-module-loader))
144 (requirement '(file-systems))
146 (modules `((srfi srfi-1)
154 ((null? '#$kernel-modules) #t)
155 ((file-exists? "/proc/sys/kernel/modprobe")
156 (let ((modprobe (call-with-input-file
157 "/proc/sys/kernel/modprobe" get-line)))
158 (guard (c ((message-condition? c)
159 (format (current-error-port) "~a~%"
160 (condition-message c))
162 (every (lambda (module)
163 (invoke/quiet modprobe "--" module))
164 '#$kernel-modules))))
166 (format (current-error-port) "error: ~a~%"
167 "Kernel is missing loadable module support.")
170 (define kernel-module-loader-service-type
172 (name 'kernel-module-loader)
173 (description "Load kernel modules.")
175 (list (service-extension shepherd-root-service-type
176 kernel-module-loader-shepherd-service)))
177 (compose concatenate)
179 (default-value '())))