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>
7 ;;; This file is part of GNU Guix.
9 ;;; GNU Guix is free software; you can redistribute it and/or modify it
10 ;;; under the terms of the GNU General Public License as published by
11 ;;; the Free Software Foundation; either version 3 of the License, or (at
12 ;;; your option) any later version.
14 ;;; GNU Guix is distributed in the hope that it will be useful, but
15 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;;; GNU General Public License for more details.
19 ;;; You should have received a copy of the GNU General Public License
20 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
22 (define-module (gnu services linux)
23 #:use-module (guix gexp)
24 #:use-module (guix records)
25 #:use-module (guix modules)
26 #:use-module (gnu services)
27 #:use-module (gnu services base)
28 #:use-module (gnu services shepherd)
29 #:use-module (gnu packages linux)
30 #:use-module (srfi srfi-1)
31 #:use-module (srfi srfi-26)
32 #:use-module (srfi srfi-34)
33 #:use-module (srfi srfi-35)
34 #:use-module (ice-9 match)
35 #:export (earlyoom-configuration
36 earlyoom-configuration?
37 earlyoom-configuration-earlyoom
38 earlyoom-configuration-minimum-available-memory
39 earlyoom-configuration-minimum-free-swap
40 earlyoom-configuration-prefer-regexp
41 earlyoom-configuration-avoid-regexp
42 earlyoom-configuration-memory-report-interval
43 earlyoom-configuration-ignore-positive-oom-score-adj?
44 earlyoom-configuration-show-debug-messages?
45 earlyoom-configuration-send-notification-command
48 kernel-module-loader-service-type
50 zram-device-configuration
51 zram-device-configuration?
52 zram-device-configuration-size
53 zram-device-configuration-compression-algorithm
54 zram-device-configuration-memory-limit
55 zram-device-configuration-priority
56 zram-device-service-type))
63 (define-record-type* <earlyoom-configuration>
64 earlyoom-configuration make-earlyoom-configuration
65 earlyoom-configuration?
66 (earlyoom earlyoom-configuration-earlyoom
68 (minimum-available-memory earlyoom-configuration-minimum-available-memory
69 (default 10)) ; in percent
70 (minimum-free-swap earlyoom-configuration-minimum-free-swap
71 (default 10)) ; in percent
72 (prefer-regexp earlyoom-configuration-prefer-regexp ; <string>
74 (avoid-regexp earlyoom-configuration-avoid-regexp ; <string>
76 (memory-report-interval earlyoom-configuration-memory-report-interval
77 (default 0)) ; in seconds; 0 means disabled
78 (ignore-positive-oom-score-adj?
79 earlyoom-configuration-ignore-positive-oom-score-adj? (default #f))
80 (run-with-higher-priority? earlyoom-configuration-run-with-higher-priority?
82 (show-debug-messages? earlyoom-configuration-show-debug-messages?
84 (send-notification-command
85 earlyoom-configuration-send-notification-command ; <string>
88 (define (earlyoom-configuration->command-line-args config)
89 "Translate a <earlyoom-configuration> object to its command line arguments
92 (($ <earlyoom-configuration> earlyoom minimum-available-memory
93 minimum-free-swap prefer-regexp avoid-regexp
94 memory-report-interval
95 ignore-positive-oom-score-adj?
96 run-with-higher-priority? show-debug-messages?
97 send-notification-command)
98 `(,(file-append earlyoom "/bin/earlyoom")
99 ,@(if minimum-available-memory
100 (list "-m" (format #f "~s" minimum-available-memory))
102 ,@(if minimum-free-swap
103 (list "-s" (format #f "~s" minimum-free-swap))
106 (list "--prefer" prefer-regexp)
109 (list "--avoid" avoid-regexp)
111 "-r" ,(format #f "~s" memory-report-interval)
112 ,@(if ignore-positive-oom-score-adj?
115 ,@(if run-with-higher-priority?
118 ,@(if show-debug-messages?
121 ,@(if send-notification-command
122 (list "-N" send-notification-command)
125 (define (earlyoom-shepherd-service config)
127 (documentation "Run the Early OOM daemon.")
128 (provision '(earlyoom))
129 (start #~(make-forkexec-constructor
130 '#$(earlyoom-configuration->command-line-args config)
131 #:log-file "/var/log/earlyoom.log"))
132 (stop #~(make-kill-destructor))))
134 (define earlyoom-service-type
137 (default-value (earlyoom-configuration))
139 (list (service-extension shepherd-root-service-type
140 (compose list earlyoom-shepherd-service))))
141 (description "Run @command{earlyoom}, the Early OOM daemon.")))
145 ;;; Kernel module loader.
148 (define kernel-module-loader-shepherd-service
150 ((and (? list? kernel-modules) ((? string?) ...))
152 (documentation "Load kernel modules.")
153 (provision '(kernel-module-loader))
156 (modules `((srfi srfi-1)
164 ((null? '#$kernel-modules) #t)
165 ((file-exists? "/proc/sys/kernel/modprobe")
166 (let ((modprobe (call-with-input-file
167 "/proc/sys/kernel/modprobe" get-line)))
168 (guard (c ((message-condition? c)
169 (format (current-error-port) "~a~%"
170 (condition-message c))
172 (every (lambda (module)
173 (invoke/quiet modprobe "--" module))
174 '#$kernel-modules))))
176 (format (current-error-port) "error: ~a~%"
177 "Kernel is missing loadable module support.")
180 (define kernel-module-loader-service-type
182 (name 'kernel-module-loader)
183 (description "Load kernel modules.")
185 (list (service-extension shepherd-root-service-type
186 (compose list kernel-module-loader-shepherd-service))))
187 (compose concatenate)
189 (default-value '())))
193 ;;; Kernel module loader.
196 (define-record-type* <zram-device-configuration>
197 zram-device-configuration make-zram-device-configuration
198 zram-device-configuration?
199 (size zram-device-configuration-size
200 (default "1G")) ; string or integer
201 (compression-algorithm zram-device-configuration-compression-algorithm
202 (default 'lzo)) ; symbol
203 (memory-limit zram-device-configuration-memory-limit
204 (default 0)) ; string or integer
205 (priority zram-device-configuration-priority
206 (default -1))) ; integer
208 (define (zram-device-configuration->udev-string config)
209 "Translate a <zram-device-configuration> into a string which can be
210 placed in a udev rules file."
212 (($ <zram-device-configuration> size compression-algorithm memory-limit priority)
214 "KERNEL==\"zram0\", "
215 "ATTR{comp_algorithm}=\"" (symbol->string compression-algorithm) "\" "
216 (if (not (or (equal? "0" size)
218 (string-append "ATTR{disksize}=\"" (if (number? size)
219 (number->string size)
223 (if (not (or (equal? "0" memory-limit)
224 (equal? 0 memory-limit)))
225 (string-append "ATTR{mem_limit}=\"" (if (number? memory-limit)
226 (number->string memory-limit)
230 "RUN+=\"/run/current-system/profile/sbin/mkswap /dev/zram0\" "
231 "RUN+=\"/run/current-system/profile/sbin/swapon "
232 (if (not (equal? -1 priority))
233 (string-append "--priority " (number->string priority) " ")
237 (define %zram-device-config
238 `("modprobe.d/zram.conf"
239 ,(plain-file "zram.conf"
240 "options zram num_devices=1")))
242 (define (zram-device-udev-rule config)
243 (file->udev-rule "99-zram.rules"
244 (plain-file "99-zram.rules"
245 (zram-device-configuration->udev-string config))))
247 (define zram-device-service-type
250 (default-value (zram-device-configuration))
252 (list (service-extension kernel-module-loader-service-type
253 (const (list "zram")))
254 (service-extension etc-service-type
255 (const (list %zram-device-config)))
256 (service-extension udev-service-type
257 (compose list zram-device-udev-rule))))
258 (description "Creates a zram swap device.")))