Commit | Line | Data |
---|---|---|
d3e439e3 MC |
1 | ;;; GNU Guix --- Functional package management for GNU |
2 | ;;; Copyright © 2020 Maxim Cournoyer <maxim.cournoyer@gmail.com> | |
044d1478 | 3 | ;;; Copyright © 2020 Brice Waegeneire <brice@waegenei.re> |
d3e439e3 MC |
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 | ||
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) | |
044d1478 BW |
29 | #:use-module (srfi srfi-34) |
30 | #:use-module (srfi srfi-35) | |
d3e439e3 MC |
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 | |
044d1478 BW |
43 | earlyoom-service-type |
44 | ||
45 | kernel-module-loader-service-type)) | |
d3e439e3 MC |
46 | |
47 | \f | |
48 | ;;; | |
49 | ;;; Early OOM daemon. | |
50 | ;;; | |
51 | ||
52 | (define-record-type* <earlyoom-configuration> | |
53 | earlyoom-configuration make-earlyoom-configuration | |
54 | earlyoom-configuration? | |
55 | (earlyoom earlyoom-configuration-earlyoom | |
56 | (default 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> | |
62 | (default #f)) | |
63 | (avoid-regexp earlyoom-configuration-avoid-regexp ; <string> | |
64 | (default #f)) | |
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? | |
70 | (default #f)) | |
71 | (show-debug-messages? earlyoom-configuration-show-debug-messages? | |
72 | (default #f)) | |
73 | (send-notification-command | |
74 | earlyoom-configuration-send-notification-command ; <string> | |
75 | (default #f))) | |
76 | ||
77 | (define (earlyoom-configuration->command-line-args config) | |
78 | "Translate a <earlyoom-configuration> object to its command line arguments | |
79 | representation." | |
80 | (match config | |
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)) | |
90 | '()) | |
91 | ,@(if minimum-free-swap | |
92 | (list "-s" (format #f "~s" minimum-free-swap)) | |
93 | '()) | |
94 | ,@(if prefer-regexp | |
95 | (list "--prefer" prefer-regexp) | |
96 | '()) | |
97 | ,@(if avoid-regexp | |
98 | (list "--avoid" avoid-regexp) | |
99 | '()) | |
100 | "-r" ,(format #f "~s" memory-report-interval) | |
101 | ,@(if ignore-positive-oom-score-adj? | |
102 | (list "-i") | |
103 | '()) | |
104 | ,@(if run-with-higher-priority? | |
105 | (list "-p") | |
106 | '()) | |
107 | ,@(if show-debug-messages? | |
108 | (list "-d") | |
109 | '()) | |
110 | ,@(if send-notification-command | |
111 | (list "-N" send-notification-command) | |
112 | '()))))) | |
113 | ||
114 | (define (earlyoom-shepherd-service config) | |
115 | (shepherd-service | |
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)))) | |
122 | ||
123 | (define earlyoom-service-type | |
124 | (service-type | |
125 | (name 'earlyoom) | |
126 | (default-value (earlyoom-configuration)) | |
127 | (extensions | |
128 | (list (service-extension shepherd-root-service-type | |
129 | (compose list earlyoom-shepherd-service)))) | |
130 | (description "Run @command{earlyoom}, the Early OOM daemon."))) | |
044d1478 BW |
131 | |
132 | \f | |
133 | ;;; | |
134 | ;;; Kernel module loader. | |
135 | ;;; | |
136 | ||
137 | (define kernel-module-loader-shepherd-service | |
138 | (match-lambda | |
139 | ((and (? list? kernel-modules) ((? string?) ...)) | |
140 | (list | |
141 | (shepherd-service | |
142 | (documentation "Load kernel modules.") | |
143 | (provision '(kernel-module-loader)) | |
144 | (requirement '(file-systems)) | |
044d1478 BW |
145 | (one-shot? #t) |
146 | (modules `((srfi srfi-1) | |
147 | (srfi srfi-34) | |
148 | (srfi srfi-35) | |
149 | (rnrs io ports) | |
150 | ,@%default-modules)) | |
151 | (start | |
152 | #~(lambda _ | |
153 | (cond | |
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)) | |
161 | #f)) | |
162 | (every (lambda (module) | |
163 | (invoke/quiet modprobe "--" module)) | |
164 | '#$kernel-modules)))) | |
165 | (else | |
166 | (format (current-error-port) "error: ~a~%" | |
167 | "Kernel is missing loadable module support.") | |
168 | #f))))))))) | |
169 | ||
170 | (define kernel-module-loader-service-type | |
171 | (service-type | |
172 | (name 'kernel-module-loader) | |
173 | (description "Load kernel modules.") | |
174 | (extensions | |
175 | (list (service-extension shepherd-root-service-type | |
176 | kernel-module-loader-shepherd-service))) | |
177 | (compose concatenate) | |
178 | (extend append) | |
179 | (default-value '()))) |