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> |
587e0d91 | 4 | ;;; Copyright © 2020 Efraim Flashner <efraim@flashner.co.il> |
fe7529d7 | 5 | ;;; Copyright © 2021 raid5atemyhomework <raid5atemyhomework@protonmail.com> |
d3e439e3 MC |
6 | ;;; |
7 | ;;; This file is part of GNU Guix. | |
8 | ;;; | |
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. | |
13 | ;;; | |
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. | |
18 | ;;; | |
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/>. | |
21 | ||
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) | |
587e0d91 | 27 | #:use-module (gnu services base) |
d3e439e3 MC |
28 | #:use-module (gnu services shepherd) |
29 | #:use-module (gnu packages linux) | |
30 | #:use-module (srfi srfi-1) | |
31 | #:use-module (srfi srfi-26) | |
044d1478 BW |
32 | #:use-module (srfi srfi-34) |
33 | #:use-module (srfi srfi-35) | |
d3e439e3 MC |
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 | |
044d1478 BW |
46 | earlyoom-service-type |
47 | ||
587e0d91 EF |
48 | kernel-module-loader-service-type |
49 | ||
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)) | |
d3e439e3 MC |
57 | |
58 | \f | |
59 | ;;; | |
60 | ;;; Early OOM daemon. | |
61 | ;;; | |
62 | ||
63 | (define-record-type* <earlyoom-configuration> | |
64 | earlyoom-configuration make-earlyoom-configuration | |
65 | earlyoom-configuration? | |
66 | (earlyoom earlyoom-configuration-earlyoom | |
67 | (default 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> | |
73 | (default #f)) | |
74 | (avoid-regexp earlyoom-configuration-avoid-regexp ; <string> | |
75 | (default #f)) | |
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? | |
81 | (default #f)) | |
82 | (show-debug-messages? earlyoom-configuration-show-debug-messages? | |
83 | (default #f)) | |
84 | (send-notification-command | |
85 | earlyoom-configuration-send-notification-command ; <string> | |
86 | (default #f))) | |
87 | ||
88 | (define (earlyoom-configuration->command-line-args config) | |
89 | "Translate a <earlyoom-configuration> object to its command line arguments | |
90 | representation." | |
91 | (match config | |
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)) | |
101 | '()) | |
102 | ,@(if minimum-free-swap | |
103 | (list "-s" (format #f "~s" minimum-free-swap)) | |
104 | '()) | |
105 | ,@(if prefer-regexp | |
106 | (list "--prefer" prefer-regexp) | |
107 | '()) | |
108 | ,@(if avoid-regexp | |
109 | (list "--avoid" avoid-regexp) | |
110 | '()) | |
111 | "-r" ,(format #f "~s" memory-report-interval) | |
112 | ,@(if ignore-positive-oom-score-adj? | |
113 | (list "-i") | |
114 | '()) | |
115 | ,@(if run-with-higher-priority? | |
116 | (list "-p") | |
117 | '()) | |
118 | ,@(if show-debug-messages? | |
119 | (list "-d") | |
120 | '()) | |
121 | ,@(if send-notification-command | |
122 | (list "-N" send-notification-command) | |
123 | '()))))) | |
124 | ||
125 | (define (earlyoom-shepherd-service config) | |
126 | (shepherd-service | |
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)))) | |
133 | ||
134 | (define earlyoom-service-type | |
135 | (service-type | |
136 | (name 'earlyoom) | |
137 | (default-value (earlyoom-configuration)) | |
138 | (extensions | |
139 | (list (service-extension shepherd-root-service-type | |
140 | (compose list earlyoom-shepherd-service)))) | |
141 | (description "Run @command{earlyoom}, the Early OOM daemon."))) | |
044d1478 BW |
142 | |
143 | \f | |
144 | ;;; | |
145 | ;;; Kernel module loader. | |
146 | ;;; | |
147 | ||
148 | (define kernel-module-loader-shepherd-service | |
149 | (match-lambda | |
150 | ((and (? list? kernel-modules) ((? string?) ...)) | |
3d3185b5 BW |
151 | (shepherd-service |
152 | (documentation "Load kernel modules.") | |
153 | (provision '(kernel-module-loader)) | |
fe7529d7 | 154 | (requirement '()) |
3d3185b5 BW |
155 | (one-shot? #t) |
156 | (modules `((srfi srfi-1) | |
157 | (srfi srfi-34) | |
158 | (srfi srfi-35) | |
159 | (rnrs io ports) | |
160 | ,@%default-modules)) | |
161 | (start | |
162 | #~(lambda _ | |
163 | (cond | |
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)) | |
171 | #f)) | |
172 | (every (lambda (module) | |
173 | (invoke/quiet modprobe "--" module)) | |
174 | '#$kernel-modules)))) | |
175 | (else | |
176 | (format (current-error-port) "error: ~a~%" | |
177 | "Kernel is missing loadable module support.") | |
178 | #f)))))))) | |
044d1478 BW |
179 | |
180 | (define kernel-module-loader-service-type | |
181 | (service-type | |
182 | (name 'kernel-module-loader) | |
183 | (description "Load kernel modules.") | |
184 | (extensions | |
185 | (list (service-extension shepherd-root-service-type | |
3d3185b5 | 186 | (compose list kernel-module-loader-shepherd-service)))) |
044d1478 BW |
187 | (compose concatenate) |
188 | (extend append) | |
189 | (default-value '()))) | |
587e0d91 EF |
190 | |
191 | \f | |
192 | ;;; | |
193 | ;;; Kernel module loader. | |
194 | ;;; | |
195 | ||
196 | (define-record-type* <zram-device-configuration> | |
197 | zram-device-configuration make-zram-device-configuration | |
198 | zram-device-configuration? | |
71992a53 | 199 | (size zram-device-configuration-size |
587e0d91 EF |
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 | |
207 | ||
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." | |
211 | (match config | |
212 | (($ <zram-device-configuration> size compression-algorithm memory-limit priority) | |
213 | (string-append | |
214 | "KERNEL==\"zram0\", " | |
215 | "ATTR{comp_algorithm}=\"" (symbol->string compression-algorithm) "\" " | |
216 | (if (not (or (equal? "0" size) | |
217 | (equal? 0 size))) | |
218 | (string-append "ATTR{disksize}=\"" (if (number? size) | |
219 | (number->string size) | |
220 | size) | |
221 | "\" ") | |
222 | "") | |
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) | |
227 | memory-limit) | |
228 | "\" ") | |
229 | "") | |
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) " ") | |
234 | "") | |
235 | "/dev/zram0\"\n")))) | |
236 | ||
237 | (define %zram-device-config | |
238 | `("modprobe.d/zram.conf" | |
239 | ,(plain-file "zram.conf" | |
240 | "options zram num_devices=1"))) | |
241 | ||
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)))) | |
246 | ||
247 | (define zram-device-service-type | |
248 | (service-type | |
249 | (name 'zram) | |
250 | (default-value (zram-device-configuration)) | |
251 | (extensions | |
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."))) |