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 ;;; Copyright © 2020 Efraim Flashner <efraim@flashner.co.il>
5 ;;; Copyright © 2021 raid5atemyhomework <raid5atemyhomework@protonmail.com>
6 ;;; Copyright © 2021 B. Wilson <elaexuotee@wilsonb.com>
7 ;;; Copyright © 2022 Josselin Poiret <dev@jpoiret.xyz>
9 ;;; This file is part of GNU Guix.
11 ;;; GNU Guix is free software; you can redistribute it and/or modify it
12 ;;; under the terms of the GNU General Public License as published by
13 ;;; the Free Software Foundation; either version 3 of the License, or (at
14 ;;; your option) any later version.
16 ;;; GNU Guix is distributed in the hope that it will be useful, but
17 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;;; GNU General Public License for more details.
21 ;;; You should have received a copy of the GNU General Public License
22 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
24 (define-module (gnu services linux)
25 #:use-module (guix diagnostics)
26 #:use-module (guix gexp)
27 #:use-module (guix records)
28 #:use-module (guix modules)
29 #:use-module (guix i18n)
30 #:use-module (guix ui)
31 #:use-module (gnu services)
32 #:use-module (gnu services base)
33 #:use-module (gnu services shepherd)
34 #:use-module (gnu packages linux)
35 #:use-module (srfi srfi-1)
36 #:use-module (srfi srfi-26)
37 #:use-module (srfi srfi-34)
38 #:use-module (srfi srfi-35)
39 #:use-module (ice-9 match)
40 #:export (earlyoom-configuration
41 earlyoom-configuration?
42 earlyoom-configuration-earlyoom
43 earlyoom-configuration-minimum-available-memory
44 earlyoom-configuration-minimum-free-swap
45 earlyoom-configuration-prefer-regexp
46 earlyoom-configuration-avoid-regexp
47 earlyoom-configuration-memory-report-interval
48 earlyoom-configuration-ignore-positive-oom-score-adj?
49 earlyoom-configuration-show-debug-messages?
50 earlyoom-configuration-send-notification-command
53 kernel-module-loader-service-type
55 rasdaemon-configuration
56 rasdaemon-configuration?
57 rasdaemon-configuration-record?
58 rasdaemon-service-type
60 zram-device-configuration
61 zram-device-configuration?
62 zram-device-configuration-size
63 zram-device-configuration-compression-algorithm
64 zram-device-configuration-memory-limit
65 zram-device-configuration-priority
66 zram-device-service-type))
73 (define-record-type* <earlyoom-configuration>
74 earlyoom-configuration make-earlyoom-configuration
75 earlyoom-configuration?
76 (earlyoom earlyoom-configuration-earlyoom
78 (minimum-available-memory earlyoom-configuration-minimum-available-memory
79 (default 10)) ; in percent
80 (minimum-free-swap earlyoom-configuration-minimum-free-swap
81 (default 10)) ; in percent
82 (prefer-regexp earlyoom-configuration-prefer-regexp ; <string>
84 (avoid-regexp earlyoom-configuration-avoid-regexp ; <string>
86 (memory-report-interval earlyoom-configuration-memory-report-interval
87 (default 0)) ; in seconds; 0 means disabled
88 (ignore-positive-oom-score-adj?
89 earlyoom-configuration-ignore-positive-oom-score-adj? (default #f))
90 (run-with-higher-priority? earlyoom-configuration-run-with-higher-priority?
92 (show-debug-messages? earlyoom-configuration-show-debug-messages?
94 (send-notification-command
95 earlyoom-configuration-send-notification-command ; <string>
98 (define (earlyoom-configuration->command-line-args config)
99 "Translate a <earlyoom-configuration> object to its command line arguments
102 (($ <earlyoom-configuration> earlyoom minimum-available-memory
103 minimum-free-swap prefer-regexp avoid-regexp
104 memory-report-interval
105 ignore-positive-oom-score-adj?
106 run-with-higher-priority? show-debug-messages?
107 send-notification-command)
108 `(,(file-append earlyoom "/bin/earlyoom")
109 ,@(if minimum-available-memory
110 (list "-m" (format #f "~s" minimum-available-memory))
112 ,@(if minimum-free-swap
113 (list "-s" (format #f "~s" minimum-free-swap))
116 (list "--prefer" prefer-regexp)
119 (list "--avoid" avoid-regexp)
121 "-r" ,(format #f "~s" memory-report-interval)
122 ,@(if ignore-positive-oom-score-adj?
125 ,@(if run-with-higher-priority?
128 ,@(if show-debug-messages?
131 ,@(if send-notification-command
132 (list "-N" send-notification-command)
135 (define (earlyoom-shepherd-service config)
137 (documentation "Run the Early OOM daemon.")
138 (provision '(earlyoom))
139 (start #~(make-forkexec-constructor
140 '#$(earlyoom-configuration->command-line-args config)
141 #:log-file "/var/log/earlyoom.log"))
142 (stop #~(make-kill-destructor))))
144 (define earlyoom-service-type
147 (default-value (earlyoom-configuration))
149 (list (service-extension shepherd-root-service-type
150 (compose list earlyoom-shepherd-service))))
151 (description "Run @command{earlyoom}, the Early OOM daemon.")))
155 ;;; Kernel module loader.
158 (define kernel-module-loader-shepherd-service
160 ((and (? list? kernel-modules) ((? string?) ...))
162 (documentation "Load kernel modules.")
163 (provision '(kernel-module-loader))
166 (modules `((srfi srfi-1)
174 ((null? '#$kernel-modules) #t)
175 ((file-exists? "/proc/sys/kernel/modprobe")
176 (let ((modprobe (call-with-input-file
177 "/proc/sys/kernel/modprobe" get-line)))
178 (guard (c ((message-condition? c)
179 (format (current-error-port) "~a~%"
180 (condition-message c))
182 (every (lambda (module)
183 (invoke/quiet modprobe "--" module))
184 '#$kernel-modules))))
186 (format (current-error-port) "error: ~a~%"
187 "Kernel is missing loadable module support.")
190 (define kernel-module-loader-service-type
192 (name 'kernel-module-loader)
193 (description "Load kernel modules.")
195 (list (service-extension shepherd-root-service-type
196 (compose list kernel-module-loader-shepherd-service))))
197 (compose concatenate)
199 (default-value '())))
203 ;;; Reliability, Availability, and Serviceability (RAS) daemon
206 (define-record-type* <rasdaemon-configuration>
207 rasdaemon-configuration make-rasdaemon-configuration
208 rasdaemon-configuration?
209 (record? rasdaemon-configuration-record? (default #f)))
211 (define (rasdaemon-configuration->command-line-args config)
212 "Translate <rasdaemon-configuration> to its command line arguments
214 (let ((record? (rasdaemon-configuration-record? config)))
215 `(,(file-append rasdaemon "/sbin/rasdaemon")
216 "--foreground" ,@(if record? '("--record") '()))))
218 (define (rasdaemon-activation config)
219 (let ((record? (rasdaemon-configuration-record? config))
220 (rasdaemon-dir "/var/lib/rasdaemon"))
221 (with-imported-modules '((guix build utils))
222 #~(if #$record? (mkdir-p #$rasdaemon-dir)))))
224 (define (rasdaemon-shepherd-service config)
226 (documentation "Run rasdaemon")
227 (provision '(rasdaemon))
228 (requirement '(syslogd))
229 (start #~(make-forkexec-constructor
230 '#$(rasdaemon-configuration->command-line-args config)))
231 (stop #~(make-kill-destructor))))
233 (define rasdaemon-service-type
236 (default-value (rasdaemon-configuration))
238 (list (service-extension shepherd-root-service-type
239 (compose list rasdaemon-shepherd-service))
240 (service-extension activation-service-type rasdaemon-activation)))
241 (compose concatenate)
242 (description "Run @command{rasdaemon}, the RAS monitor")))
246 ;;; Kernel module loader.
249 (define-record-type* <zram-device-configuration>
250 zram-device-configuration make-zram-device-configuration
251 zram-device-configuration?
252 (size zram-device-configuration-size
253 (default "1G")) ; string or integer
254 (compression-algorithm zram-device-configuration-compression-algorithm
255 (default 'lzo)) ; symbol
256 (memory-limit zram-device-configuration-memory-limit
257 (default 0)) ; string or integer
258 (priority zram-device-configuration-priority
259 (default #f) ; integer | #f
260 (delayed) ; to avoid printing the deprecation
261 ; warning multiple times
262 (sanitize warn-zram-priority-change)))
264 (define-with-syntax-properties
265 (warn-zram-priority-change (priority properties))
266 (if (eqv? priority -1)
268 (warning (source-properties->location properties)
269 (G_ "using -1 for zram priority is deprecated~%"))
270 (display-hint (G_ "Use #f or leave as default instead (@pxref{Linux \
275 (define (zram-device-configuration->udev-string config)
276 "Translate a <zram-device-configuration> into a string which can be
277 placed in a udev rules file."
279 (($ <zram-device-configuration> size compression-algorithm memory-limit priority)
281 "KERNEL==\"zram0\", "
282 "ATTR{comp_algorithm}=\"" (symbol->string compression-algorithm) "\" "
283 (if (not (or (equal? "0" size)
285 (string-append "ATTR{disksize}=\"" (if (number? size)
286 (number->string size)
290 (if (not (or (equal? "0" memory-limit)
291 (equal? 0 memory-limit)))
292 (string-append "ATTR{mem_limit}=\"" (if (number? memory-limit)
293 (number->string memory-limit)
297 "RUN+=\"/run/current-system/profile/sbin/mkswap /dev/zram0\" "
298 "RUN+=\"/run/current-system/profile/sbin/swapon "
299 ;; TODO: Revert to simply use 'priority' after removing the deprecation
300 ;; warning and the delayed property of the field.
301 (let ((priority* (force priority)))
303 (format #f "--priority ~a " priority*)
307 (define %zram-device-config
308 `("modprobe.d/zram.conf"
309 ,(plain-file "zram.conf"
310 "options zram num_devices=1")))
312 (define (zram-device-udev-rule config)
313 (file->udev-rule "99-zram.rules"
314 (plain-file "99-zram.rules"
315 (zram-device-configuration->udev-string config))))
317 (define zram-device-service-type
320 (default-value (zram-device-configuration))
322 (list (service-extension kernel-module-loader-service-type
323 (const (list "zram")))
324 (service-extension etc-service-type
325 (const (list %zram-device-config)))
326 (service-extension udev-service-type
327 (compose list zram-device-udev-rule))))
328 (description "Creates a zram swap device.")))