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>
6 ;;; This file is part of GNU Guix.
8 ;;; GNU Guix is free software; you can redistribute it and/or modify it
9 ;;; under the terms of the GNU General Public License as published by
10 ;;; the Free Software Foundation; either version 3 of the License, or (at
11 ;;; your option) any later version.
13 ;;; GNU Guix is distributed in the hope that it will be useful, but
14 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;;; GNU General Public License for more details.
18 ;;; You should have received a copy of the GNU General Public License
19 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
21 (define-module (gnu services linux)
22 #:use-module (guix gexp)
23 #:use-module (guix records)
24 #:use-module (guix modules)
25 #:use-module (gnu services)
26 #:use-module (gnu services base)
27 #:use-module (gnu services shepherd)
28 #:use-module (gnu packages linux)
29 #:use-module (srfi srfi-1)
30 #:use-module (srfi srfi-26)
31 #:use-module (srfi srfi-34)
32 #:use-module (srfi srfi-35)
33 #:use-module (ice-9 match)
34 #:export (earlyoom-configuration
35 earlyoom-configuration?
36 earlyoom-configuration-earlyoom
37 earlyoom-configuration-minimum-available-memory
38 earlyoom-configuration-minimum-free-swap
39 earlyoom-configuration-prefer-regexp
40 earlyoom-configuration-avoid-regexp
41 earlyoom-configuration-memory-report-interval
42 earlyoom-configuration-ignore-positive-oom-score-adj?
43 earlyoom-configuration-show-debug-messages?
44 earlyoom-configuration-send-notification-command
47 kernel-module-loader-service-type
49 zram-device-configuration
50 zram-device-configuration?
51 zram-device-configuration-size
52 zram-device-configuration-compression-algorithm
53 zram-device-configuration-memory-limit
54 zram-device-configuration-priority
55 zram-device-service-type))
62 (define-record-type* <earlyoom-configuration>
63 earlyoom-configuration make-earlyoom-configuration
64 earlyoom-configuration?
65 (earlyoom earlyoom-configuration-earlyoom
67 (minimum-available-memory earlyoom-configuration-minimum-available-memory
68 (default 10)) ; in percent
69 (minimum-free-swap earlyoom-configuration-minimum-free-swap
70 (default 10)) ; in percent
71 (prefer-regexp earlyoom-configuration-prefer-regexp ; <string>
73 (avoid-regexp earlyoom-configuration-avoid-regexp ; <string>
75 (memory-report-interval earlyoom-configuration-memory-report-interval
76 (default 0)) ; in seconds; 0 means disabled
77 (ignore-positive-oom-score-adj?
78 earlyoom-configuration-ignore-positive-oom-score-adj? (default #f))
79 (run-with-higher-priority? earlyoom-configuration-run-with-higher-priority?
81 (show-debug-messages? earlyoom-configuration-show-debug-messages?
83 (send-notification-command
84 earlyoom-configuration-send-notification-command ; <string>
87 (define (earlyoom-configuration->command-line-args config)
88 "Translate a <earlyoom-configuration> object to its command line arguments
91 (($ <earlyoom-configuration> earlyoom minimum-available-memory
92 minimum-free-swap prefer-regexp avoid-regexp
93 memory-report-interval
94 ignore-positive-oom-score-adj?
95 run-with-higher-priority? show-debug-messages?
96 send-notification-command)
97 `(,(file-append earlyoom "/bin/earlyoom")
98 ,@(if minimum-available-memory
99 (list "-m" (format #f "~s" minimum-available-memory))
101 ,@(if minimum-free-swap
102 (list "-s" (format #f "~s" minimum-free-swap))
105 (list "--prefer" prefer-regexp)
108 (list "--avoid" avoid-regexp)
110 "-r" ,(format #f "~s" memory-report-interval)
111 ,@(if ignore-positive-oom-score-adj?
114 ,@(if run-with-higher-priority?
117 ,@(if show-debug-messages?
120 ,@(if send-notification-command
121 (list "-N" send-notification-command)
124 (define (earlyoom-shepherd-service config)
126 (documentation "Run the Early OOM daemon.")
127 (provision '(earlyoom))
128 (start #~(make-forkexec-constructor
129 '#$(earlyoom-configuration->command-line-args config)
130 #:log-file "/var/log/earlyoom.log"))
131 (stop #~(make-kill-destructor))))
133 (define earlyoom-service-type
136 (default-value (earlyoom-configuration))
138 (list (service-extension shepherd-root-service-type
139 (compose list earlyoom-shepherd-service))))
140 (description "Run @command{earlyoom}, the Early OOM daemon.")))
144 ;;; Kernel module loader.
147 (define kernel-module-loader-shepherd-service
149 ((and (? list? kernel-modules) ((? string?) ...))
152 (documentation "Load kernel modules.")
153 (provision '(kernel-module-loader))
154 (requirement '(file-systems))
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 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.")))