1046a7e0c2df2cd98004ecaa1e8737d182c449c2
[jackhill/guix/guix.git] / gnu / services / linux.scm
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 ;;;
6 ;;; This file is part of GNU Guix.
7 ;;;
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.
12 ;;;
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.
17 ;;;
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/>.
20
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
45 earlyoom-service-type
46
47 kernel-module-loader-service-type
48
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))
56
57 \f
58 ;;;
59 ;;; Early OOM daemon.
60 ;;;
61
62 (define-record-type* <earlyoom-configuration>
63 earlyoom-configuration make-earlyoom-configuration
64 earlyoom-configuration?
65 (earlyoom earlyoom-configuration-earlyoom
66 (default 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>
72 (default #f))
73 (avoid-regexp earlyoom-configuration-avoid-regexp ; <string>
74 (default #f))
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?
80 (default #f))
81 (show-debug-messages? earlyoom-configuration-show-debug-messages?
82 (default #f))
83 (send-notification-command
84 earlyoom-configuration-send-notification-command ; <string>
85 (default #f)))
86
87 (define (earlyoom-configuration->command-line-args config)
88 "Translate a <earlyoom-configuration> object to its command line arguments
89 representation."
90 (match config
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))
100 '())
101 ,@(if minimum-free-swap
102 (list "-s" (format #f "~s" minimum-free-swap))
103 '())
104 ,@(if prefer-regexp
105 (list "--prefer" prefer-regexp)
106 '())
107 ,@(if avoid-regexp
108 (list "--avoid" avoid-regexp)
109 '())
110 "-r" ,(format #f "~s" memory-report-interval)
111 ,@(if ignore-positive-oom-score-adj?
112 (list "-i")
113 '())
114 ,@(if run-with-higher-priority?
115 (list "-p")
116 '())
117 ,@(if show-debug-messages?
118 (list "-d")
119 '())
120 ,@(if send-notification-command
121 (list "-N" send-notification-command)
122 '())))))
123
124 (define (earlyoom-shepherd-service config)
125 (shepherd-service
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))))
132
133 (define earlyoom-service-type
134 (service-type
135 (name 'earlyoom)
136 (default-value (earlyoom-configuration))
137 (extensions
138 (list (service-extension shepherd-root-service-type
139 (compose list earlyoom-shepherd-service))))
140 (description "Run @command{earlyoom}, the Early OOM daemon.")))
141
142 \f
143 ;;;
144 ;;; Kernel module loader.
145 ;;;
146
147 (define kernel-module-loader-shepherd-service
148 (match-lambda
149 ((and (? list? kernel-modules) ((? string?) ...))
150 (shepherd-service
151 (documentation "Load kernel modules.")
152 (provision '(kernel-module-loader))
153 (requirement '(file-systems))
154 (one-shot? #t)
155 (modules `((srfi srfi-1)
156 (srfi srfi-34)
157 (srfi srfi-35)
158 (rnrs io ports)
159 ,@%default-modules))
160 (start
161 #~(lambda _
162 (cond
163 ((null? '#$kernel-modules) #t)
164 ((file-exists? "/proc/sys/kernel/modprobe")
165 (let ((modprobe (call-with-input-file
166 "/proc/sys/kernel/modprobe" get-line)))
167 (guard (c ((message-condition? c)
168 (format (current-error-port) "~a~%"
169 (condition-message c))
170 #f))
171 (every (lambda (module)
172 (invoke/quiet modprobe "--" module))
173 '#$kernel-modules))))
174 (else
175 (format (current-error-port) "error: ~a~%"
176 "Kernel is missing loadable module support.")
177 #f))))))))
178
179 (define kernel-module-loader-service-type
180 (service-type
181 (name 'kernel-module-loader)
182 (description "Load kernel modules.")
183 (extensions
184 (list (service-extension shepherd-root-service-type
185 (compose list kernel-module-loader-shepherd-service))))
186 (compose concatenate)
187 (extend append)
188 (default-value '())))
189
190 \f
191 ;;;
192 ;;; Kernel module loader.
193 ;;;
194
195 (define-record-type* <zram-device-configuration>
196 zram-device-configuration make-zram-device-configuration
197 zram-device-configuration?
198 (size zram-device-configuration-size
199 (default "1G")) ; string or integer
200 (compression-algorithm zram-device-configuration-compression-algorithm
201 (default 'lzo)) ; symbol
202 (memory-limit zram-device-configuration-memory-limit
203 (default 0)) ; string or integer
204 (priority zram-device-configuration-priority
205 (default -1))) ; integer
206
207 (define (zram-device-configuration->udev-string config)
208 "Translate a <zram-device-configuration> into a string which can be
209 placed in a udev rules file."
210 (match config
211 (($ <zram-device-configuration> size compression-algorithm memory-limit priority)
212 (string-append
213 "KERNEL==\"zram0\", "
214 "ATTR{comp_algorithm}=\"" (symbol->string compression-algorithm) "\" "
215 (if (not (or (equal? "0" size)
216 (equal? 0 size)))
217 (string-append "ATTR{disksize}=\"" (if (number? size)
218 (number->string size)
219 size)
220 "\" ")
221 "")
222 (if (not (or (equal? "0" memory-limit)
223 (equal? 0 memory-limit)))
224 (string-append "ATTR{mem_limit}=\"" (if (number? memory-limit)
225 (number->string memory-limit)
226 memory-limit)
227 "\" ")
228 "")
229 "RUN+=\"/run/current-system/profile/sbin/mkswap /dev/zram0\" "
230 "RUN+=\"/run/current-system/profile/sbin/swapon "
231 (if (not (equal? -1 priority))
232 (string-append "--priority " (number->string priority) " ")
233 "")
234 "/dev/zram0\"\n"))))
235
236 (define %zram-device-config
237 `("modprobe.d/zram.conf"
238 ,(plain-file "zram.conf"
239 "options zram num_devices=1")))
240
241 (define (zram-device-udev-rule config)
242 (file->udev-rule "99-zram.rules"
243 (plain-file "99-zram.rules"
244 (zram-device-configuration->udev-string config))))
245
246 (define zram-device-service-type
247 (service-type
248 (name 'zram)
249 (default-value (zram-device-configuration))
250 (extensions
251 (list (service-extension kernel-module-loader-service-type
252 (const (list "zram")))
253 (service-extension etc-service-type
254 (const (list %zram-device-config)))
255 (service-extension udev-service-type
256 (compose list zram-device-udev-rule))))
257 (description "Creates a zram swap device.")))