projects/guix·blob wip-postfix · refs

635 lines · 23.6 KiB

1;;; GNU Guix --- Functional package management for GNU
2;;; Copyright © 2020 Maxim Cournoyer <[email protected]>
3;;; Copyright © 2020 Brice Waegeneire <[email protected]>
4;;; Copyright © 2020, 2023 Efraim Flashner <[email protected]>
5;;; Copyright © 2021 raid5atemyhomework <[email protected]>
6;;; Copyright © 2021 B. Wilson <[email protected]>
7;;; Copyright © 2022 Josselin Poiret <[email protected]>
8;;; Copyright © 2023 Bruno Victal <[email protected]>
9;;; Copyright © 2023 Felix Lechner <[email protected]>
10;;;
11;;; This file is part of GNU Guix.
12;;;
13;;; GNU Guix is free software; you can redistribute it and/or modify it
14;;; under the terms of the GNU General Public License as published by
15;;; the Free Software Foundation; either version 3 of the License, or (at
16;;; your option) any later version.
17;;;
18;;; GNU Guix is distributed in the hope that it will be useful, but
19;;; WITHOUT ANY WARRANTY; without even the implied warranty of
20;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21;;; GNU General Public License for more details.
22;;;
23;;; You should have received a copy of the GNU General Public License
24;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
25
26(define-module (gnu services linux)
27 #:use-module (guix diagnostics)
28 #:use-module (guix gexp)
29 #:use-module (guix records)
30 #:use-module (guix modules)
31 #:use-module (guix i18n)
32 #:use-module (guix ui)
33 #:use-module (gnu services)
34 #:use-module (gnu services admin)
35 #:use-module (gnu services base)
36 #:use-module (gnu services configuration)
37 #:use-module (gnu services mcron)
38 #:use-module (gnu services shepherd)
39 #:use-module (gnu packages linux)
40 #:use-module (srfi srfi-1)
41 #:use-module (srfi srfi-26)
42 #:use-module (srfi srfi-34)
43 #:use-module (srfi srfi-35)
44 #:use-module (ice-9 format)
45 #:use-module (ice-9 match)
46 #:export (earlyoom-configuration
47 earlyoom-configuration?
48 earlyoom-configuration-earlyoom
49 earlyoom-configuration-minimum-available-memory
50 earlyoom-configuration-minimum-free-swap
51 earlyoom-configuration-prefer-regexp
52 earlyoom-configuration-avoid-regexp
53 earlyoom-configuration-memory-report-interval
54 earlyoom-configuration-ignore-positive-oom-score-adj?
55 earlyoom-configuration-show-debug-messages?
56 earlyoom-configuration-send-notification-command
57 earlyoom-service-type
58
59 fstrim-configuration
60 fstrim-configuration?
61 fstrim-configuration-package
62 fstrim-configuration-schedule
63 fstrim-configuration-listed-in
64 fstrim-configuration-verbose?
65 fstrim-configuration-quiet-unsupported?
66 fstrim-configuration-extra-arguments
67 fstrim-service-type
68
69 kernel-module-loader-service-type
70
71 cachefilesd-configuration
72 cachefilesd-configuration?
73 cachefilesd-configuration-cachefilesd
74 cachefilesd-configuration-debug-output?
75 cachefilesd-configuration-use-syslog?
76 cachefilesd-configuration-scan?
77 cachefilesd-configuration-cache-directory
78 cachefilesd-configuration-cache-name
79 cachefilesd-configuration-security-context
80 cachefilesd-configuration-pause-culling-for-block-percentage
81 cachefilesd-configuration-pause-culling-for-file-percentage
82 cachefilesd-configuration-resume-culling-for-block-percentage
83 cachefilesd-configuration-resume-culling-for-file-percentage
84 cachefilesd-configuration-pause-caching-for-block-percentage
85 cachefilesd-configuration-pause-caching-for-file-percentage
86 cachefilesd-configuration-log2-table-size
87 cachefilesd-configuration-cull?
88 cachefilesd-configuration-trace-function-entry-in-kernel-module
89 cachefilesd-configuration-trace-function-exit-in-kernel-module
90 cachefilesd-configuration-trace-internal-checkpoints-in-kernel-module
91 cachefilesd-service-type
92
93 rasdaemon-configuration
94 rasdaemon-configuration?
95 rasdaemon-configuration-record?
96 rasdaemon-service-type
97
98 zram-device-configuration
99 zram-device-configuration?
100 zram-device-configuration-size
101 zram-device-configuration-compression-algorithm
102 zram-device-configuration-memory-limit
103 zram-device-configuration-priority
104 zram-device-service-type))
105
106
107;;;
108;;; Early OOM daemon.
109;;;
110
111(define-record-type* <earlyoom-configuration>
112 earlyoom-configuration make-earlyoom-configuration
113 earlyoom-configuration?
114 (earlyoom earlyoom-configuration-earlyoom
115 (default earlyoom))
116 (minimum-available-memory earlyoom-configuration-minimum-available-memory
117 (default 10)) ; in percent
118 (minimum-free-swap earlyoom-configuration-minimum-free-swap
119 (default 10)) ; in percent
120 (prefer-regexp earlyoom-configuration-prefer-regexp ; <string>
121 (default #f))
122 (avoid-regexp earlyoom-configuration-avoid-regexp ; <string>
123 (default #f))
124 (memory-report-interval earlyoom-configuration-memory-report-interval
125 (default 0)) ; in seconds; 0 means disabled
126 (ignore-positive-oom-score-adj?
127 earlyoom-configuration-ignore-positive-oom-score-adj? (default #f))
128 (run-with-higher-priority? earlyoom-configuration-run-with-higher-priority?
129 (default #f))
130 (show-debug-messages? earlyoom-configuration-show-debug-messages?
131 (default #f))
132 (send-notification-command
133 earlyoom-configuration-send-notification-command ; <string>
134 (default #f)))
135
136(define (earlyoom-configuration->command-line-args config)
137 "Translate a <earlyoom-configuration> object to its command line arguments
138representation."
139 (match config
140 (($ <earlyoom-configuration> earlyoom minimum-available-memory
141 minimum-free-swap prefer-regexp avoid-regexp
142 memory-report-interval
143 ignore-positive-oom-score-adj?
144 run-with-higher-priority? show-debug-messages?
145 send-notification-command)
146 `(,(file-append earlyoom "/bin/earlyoom")
147 ,@(if minimum-available-memory
148 (list "-m" (format #f "~s" minimum-available-memory))
149 '())
150 ,@(if minimum-free-swap
151 (list "-s" (format #f "~s" minimum-free-swap))
152 '())
153 ,@(if prefer-regexp
154 (list "--prefer" prefer-regexp)
155 '())
156 ,@(if avoid-regexp
157 (list "--avoid" avoid-regexp)
158 '())
159 "-r" ,(format #f "~s" memory-report-interval)
160 ,@(if ignore-positive-oom-score-adj?
161 (list "-i")
162 '())
163 ,@(if run-with-higher-priority?
164 (list "-p")
165 '())
166 ,@(if show-debug-messages?
167 (list "-d")
168 '())
169 ,@(if send-notification-command
170 (list "-N" send-notification-command)
171 '())))))
172
173(define (earlyoom-shepherd-service config)
174 (shepherd-service
175 (documentation "Run the Early OOM daemon.")
176 (provision '(earlyoom))
177 (requirement '(user-processes))
178 (start #~(make-forkexec-constructor
179 '#$(earlyoom-configuration->command-line-args config)
180 #:log-file "/var/log/earlyoom.log"))
181 (stop #~(make-kill-destructor))))
182
183(define %earlyoom-log-rotation
184 (list (log-rotation
185 (files '("/var/log/earlyoom.log")))))
186
187(define earlyoom-service-type
188 (service-type
189 (name 'earlyoom)
190 (default-value (earlyoom-configuration))
191 (extensions
192 (list (service-extension shepherd-root-service-type
193 (compose list earlyoom-shepherd-service))
194 (service-extension rottlog-service-type
195 (const %earlyoom-log-rotation))))
196 (description "Run @command{earlyoom}, a daemon that quickly responds to
197@acronym{OOM, out-of-memory} conditions by terminating relevant processes.")))
198
199
200;;;
201;;; fstrim
202;;;
203
204(define (mcron-time? x)
205 (or (procedure? x) (string? x) (list? x)))
206
207(define-maybe list-of-strings (prefix fstrim-))
208
209(define (fstrim-serialize-boolean field-name value)
210 (list (format #f "~:[~;--~a~]" value
211 ;; Drop trailing '?' character.
212 (string-drop-right (symbol->string field-name) 1))))
213
214(define (fstrim-serialize-list-of-strings field-name value)
215 (list (string-append "--" (symbol->string field-name))
216 #~(string-join '#$value ":")))
217
218(define-configuration fstrim-configuration
219 (package
220 (file-like util-linux)
221 "The package providing the @command{fstrim} command."
222 empty-serializer)
223 (schedule
224 (mcron-time "0 0 * * 0")
225 "Schedule for launching @command{fstrim}. This can be a procedure, a list
226or a string. For additional information, see @ref{Guile Syntax,,
227Job specification, mcron, the mcron manual}. By default this is set to run
228weekly on Sunday at 00:00."
229 empty-serializer)
230 ;; The following are fstrim-related options.
231 (listed-in
232 (maybe-list-of-strings '("/etc/fstab" "/proc/self/mountinfo"))
233 ;; Note: documentation sourced from the fstrim manpage.
234 "List of files in fstab or kernel mountinfo format. All missing or
235empty files are silently ignored. The evaluation of the list @emph{stops}
236after the first non-empty file. File systems with @code{X-fstrim.notrim} mount
237option in fstab are skipped.")
238 (verbose?
239 (boolean #t)
240 "Verbose execution.")
241 (quiet-unsupported?
242 (boolean #t)
243 "Suppress error messages if trim operation (ioctl) is unsupported.")
244 (extra-arguments
245 maybe-list-of-strings
246 "Extra options to append to @command{fstrim} (run @samp{man fstrim} for
247more information)."
248 (serializer
249 (lambda (_ value)
250 (if (maybe-value-set? value)
251 value '()))))
252 (prefix fstrim-))
253
254(define (serialize-fstrim-configuration config)
255 (concatenate
256 (filter list?
257 (map (lambda (field)
258 ((configuration-field-serializer field)
259 (configuration-field-name field)
260 ((configuration-field-getter field) config)))
261 fstrim-configuration-fields))))
262
263(define (fstrim-mcron-job config)
264 (match-record config <fstrim-configuration> (package schedule)
265 #~(job
266 ;; Note: The “if” below is to ensure that
267 ;; lists are ungexp'd correctly since @var{schedule}
268 ;; can be either a procedure, a string or a list.
269 #$(if (list? schedule)
270 #~'(#$@schedule)
271 schedule)
272 (lambda ()
273 (system* #$(file-append package "/sbin/fstrim")
274 #$@(serialize-fstrim-configuration config)))
275 "fstrim")))
276
277(define fstrim-service-type
278 (service-type
279 (name 'fstrim)
280 (extensions
281 (list (service-extension mcron-service-type
282 (compose list fstrim-mcron-job))))
283 (description "Discard unused blocks from file systems.")
284 (default-value (fstrim-configuration))))
285
286
287;;;
288;;; Kernel module loader.
289;;;
290
291(define kernel-module-loader-shepherd-service
292 (match-lambda
293 ((and (? list? kernel-modules) ((? string?) ...))
294 (shepherd-service
295 (documentation "Load kernel modules.")
296 (provision '(kernel-module-loader))
297 (requirement '())
298 (one-shot? #t)
299 (modules `((srfi srfi-1)
300 (srfi srfi-34)
301 (srfi srfi-35)
302 (rnrs io ports)
303 ,@%default-modules))
304 (start
305 #~(lambda _
306 (cond
307 ((null? '#$kernel-modules) #t)
308 ((file-exists? "/proc/sys/kernel/modprobe")
309 (let ((modprobe (call-with-input-file
310 "/proc/sys/kernel/modprobe" get-line)))
311 (guard (c ((message-condition? c)
312 (format (current-error-port) "~a~%"
313 (condition-message c))
314 #f))
315 (every (lambda (module)
316 (invoke/quiet modprobe "--" module))
317 '#$kernel-modules))))
318 (else
319 (format (current-error-port) "error: ~a~%"
320 "Kernel is missing loadable module support.")
321 #f))))))))
322
323(define kernel-module-loader-service-type
324 (service-type
325 (name 'kernel-module-loader)
326 (description "Load kernel modules.")
327 (extensions
328 (list (service-extension shepherd-root-service-type
329 (compose list kernel-module-loader-shepherd-service))))
330 (compose concatenate)
331 (extend append)
332 (default-value '())))
333
334
335;;;
336;;; Cachefilesd, an FS-Cache daemon
337;;;
338
339(define (serialize-string variable-symbol value)
340 #~(format #f "~a ~a~%" #$(symbol->string variable-symbol) #$value))
341
342(define-maybe string)
343
344(define (non-negative-integer? val)
345 (and (exact-integer? val) (not (negative? val))))
346
347(define (serialize-non-negative-integer variable-symbol value)
348 #~(format #f "~a ~d~%" #$(symbol->string variable-symbol) #$value))
349
350(define-maybe non-negative-integer)
351
352(define (make-option-serializer option-symbol)
353 (lambda (variable-symbol text)
354 (if (maybe-value-set? text)
355 #~(format #f "~a ~a~%" #$(symbol->string option-symbol) #$text)
356 "")))
357
358(define (make-percentage-threshold-serializer threshold-symbol)
359 (lambda (variable-symbol percentage)
360 (if (maybe-value-set? percentage)
361 #~(format #f "~a ~a%~%" #$(symbol->string threshold-symbol) #$percentage)
362 "")))
363
364(define-configuration cachefilesd-configuration
365 (cachefilesd
366 (file-like cachefilesd)
367 "The cachefilesd package to use."
368 (serializer empty-serializer))
369
370 ;; command-line options
371 (debug-output?
372 (boolean #f)
373 "Print debugging output to stderr."
374 (serializer empty-serializer))
375
376 (use-syslog?
377 (boolean #t)
378 "Log to syslog facility instead of stdout."
379 (serializer empty-serializer))
380
381 ;; culling is part of the configuration file
382 ;; despite the name of the command-line option
383 (scan?
384 (boolean #t)
385 "Scan for cachable objects."
386 (serializer empty-serializer))
387
388 ;; sole required field in the configuration file
389 (cache-directory
390 maybe-string
391 "Location of the cache directory."
392 (serializer (make-option-serializer 'dir)))
393
394 (cache-name
395 (maybe-string "CacheFiles")
396 "Name of cache (keep unique)."
397 (serializer (make-option-serializer 'tag)))
398
399 (security-context
400 maybe-string
401 "SELinux security context."
402 (serializer (make-option-serializer 'secctx)))
403
404 ;; percentage thresholds in the configuration file
405 (pause-culling-for-block-percentage
406 (maybe-non-negative-integer 7)
407 "Pause culling when available blocks exceed this percentage."
408 (serializer (make-percentage-threshold-serializer 'brun)))
409
410 (pause-culling-for-file-percentage
411 (maybe-non-negative-integer 7)
412 "Pause culling when available files exceed this percentage."
413 (serializer (make-percentage-threshold-serializer 'frun)))
414
415 (resume-culling-for-block-percentage
416 (maybe-non-negative-integer 5)
417 "Start culling when available blocks drop below this percentage."
418 (serializer (make-percentage-threshold-serializer 'bcull)))
419
420 (resume-culling-for-file-percentage
421 (maybe-non-negative-integer 5)
422 "Start culling when available files drop below this percentage."
423 (serializer (make-percentage-threshold-serializer 'fcull)))
424
425 (pause-caching-for-block-percentage
426 (maybe-non-negative-integer 1)
427 "Pause further allocations when available blocks drop below this percentage."
428 (serializer (make-percentage-threshold-serializer 'bstop)))
429
430 (pause-caching-for-file-percentage
431 (maybe-non-negative-integer 1)
432 "Pause further allocations when available files drop below this percentage."
433 (serializer (make-percentage-threshold-serializer 'fstop)))
434
435 ;; run time optimizations in the configuration file
436 (log2-table-size
437 (maybe-non-negative-integer 12)
438 "Size of tables holding cullable objects in logarithm of base 2."
439 (serializer (make-option-serializer 'culltable)))
440
441 (cull?
442 (boolean #t)
443 "Create free space by culling (consumes system load)."
444 (serializer
445 (lambda (variable-symbol value)
446 (if value "" "nocull\n"))))
447
448 ;; kernel module debugging in the configuration file
449 (trace-function-entry-in-kernel-module?
450 (boolean #f)
451 "Trace function entry in the kernel module (for debugging)."
452 (serializer empty-serializer))
453
454 (trace-function-exit-in-kernel-module?
455 (boolean #f)
456 "Trace function exit in the kernel module (for debugging)."
457 (serializer empty-serializer))
458
459 (trace-internal-checkpoints-in-kernel-module?
460 (boolean #f)
461 "Trace internal checkpoints in the kernel module (for debugging)."
462 (serializer empty-serializer)))
463
464(define (serialize-cachefilesd-configuration configuration)
465 (mixed-text-file
466 "cachefilesd.conf"
467 (serialize-configuration configuration cachefilesd-configuration-fields)))
468
469(define (cachefilesd-shepherd-service config)
470 "Return a list of <shepherd-service> for cachefilesd for CONFIG."
471 (match-record
472 config <cachefilesd-configuration> (cachefilesd
473 debug-output?
474 use-syslog?
475 scan?
476 cache-directory)
477 (let ((configuration-file (serialize-cachefilesd-configuration config)))
478 (shepherd-service
479 (documentation "Run the cachefilesd daemon for FS-Cache.")
480 (provision '(cachefilesd))
481 (requirement (append '(file-systems)
482 (if use-syslog? '(syslogd) '())))
483 (start #~(begin
484 (and=> #$(maybe-value cache-directory) mkdir-p)
485 (make-forkexec-constructor
486 `(#$(file-append cachefilesd "/sbin/cachefilesd")
487 ;; do not detach
488 "-n"
489 #$@(if debug-output? '("-d") '())
490 #$@(if use-syslog? '() '("-s"))
491 #$@(if scan? '() '("-N"))
492 "-f" #$configuration-file))))
493 (stop #~(make-kill-destructor))))))
494
495(define cachefilesd-service-type
496 (service-type
497 (name 'cachefilesd)
498 (description
499 "Run the file system cache daemon @command{cachefilesd}, which relies on
500the Linux @code{cachefiles} module.")
501 (extensions
502 (list (service-extension kernel-module-loader-service-type
503 (const '("cachefiles")))
504 (service-extension shepherd-root-service-type
505 (compose list cachefilesd-shepherd-service))))
506 (default-value (cachefilesd-configuration))))
507
508
509;;;
510;;; Reliability, Availability, and Serviceability (RAS) daemon
511;;;
512
513(define-record-type* <rasdaemon-configuration>
514 rasdaemon-configuration make-rasdaemon-configuration
515 rasdaemon-configuration?
516 (record? rasdaemon-configuration-record? (default #f)))
517
518(define (rasdaemon-configuration->command-line-args config)
519 "Translate <rasdaemon-configuration> to its command line arguments
520 representation"
521 (let ((record? (rasdaemon-configuration-record? config)))
522 `(,(file-append rasdaemon "/sbin/rasdaemon")
523 "--foreground" ,@(if record? '("--record") '()))))
524
525(define (rasdaemon-activation config)
526 (let ((record? (rasdaemon-configuration-record? config))
527 (rasdaemon-dir "/var/lib/rasdaemon"))
528 (with-imported-modules '((guix build utils))
529 #~(if #$record? (mkdir-p #$rasdaemon-dir)))))
530
531(define (rasdaemon-shepherd-service config)
532 (shepherd-service
533 (documentation "Run rasdaemon")
534 (provision '(rasdaemon))
535 (requirement '(syslogd))
536 (start #~(make-forkexec-constructor
537 '#$(rasdaemon-configuration->command-line-args config)))
538 (stop #~(make-kill-destructor))))
539
540(define rasdaemon-service-type
541 (service-type
542 (name 'rasdaemon)
543 (default-value (rasdaemon-configuration))
544 (extensions
545 (list (service-extension shepherd-root-service-type
546 (compose list rasdaemon-shepherd-service))
547 (service-extension activation-service-type rasdaemon-activation)))
548 (compose concatenate)
549 (description "Run @command{rasdaemon}, the RAS monitor")))
550
551
552;;;
553;;; Zram device
554;;;
555
556(define-record-type* <zram-device-configuration>
557 zram-device-configuration make-zram-device-configuration
558 zram-device-configuration?
559 (size zram-device-configuration-size
560 (default "1G")) ; string or integer
561 (compression-algorithm zram-device-configuration-compression-algorithm
562 (default 'lzo)) ; symbol
563 (memory-limit zram-device-configuration-memory-limit
564 (default 0)) ; string or integer
565 (priority zram-device-configuration-priority
566 (default #f) ; integer | #f
567 (delayed) ; to avoid printing the deprecation
568 ; warning multiple times
569 (sanitize warn-zram-priority-change)))
570
571(define-with-syntax-properties
572 (warn-zram-priority-change (priority properties))
573 (if (eqv? priority -1)
574 (begin
575 (warning (source-properties->location properties)
576 (G_ "using -1 for zram priority is deprecated~%"))
577 (display-hint (G_ "Use #f or leave as default instead (@pxref{Linux \
578Services})."))
579 #f)
580 priority))
581
582(define (zram-device-configuration->udev-string config)
583 "Translate a <zram-device-configuration> into a string which can be
584placed in a udev rules file."
585 (match config
586 (($ <zram-device-configuration> size compression-algorithm memory-limit priority)
587 (string-append
588 "KERNEL==\"zram0\", "
589 "ATTR{comp_algorithm}=\"" (symbol->string compression-algorithm) "\" "
590 (if (not (or (equal? "0" size)
591 (equal? 0 size)))
592 (string-append "ATTR{disksize}=\"" (if (number? size)
593 (number->string size)
594 size)
595 "\" ")
596 "")
597 (if (not (or (equal? "0" memory-limit)
598 (equal? 0 memory-limit)))
599 (string-append "ATTR{mem_limit}=\"" (if (number? memory-limit)
600 (number->string memory-limit)
601 memory-limit)
602 "\" ")
603 "")
604 "RUN+=\"/run/current-system/profile/sbin/mkswap /dev/zram0\" "
605 "RUN+=\"/run/current-system/profile/sbin/swapon "
606 ;; TODO: Revert to simply use 'priority' after removing the deprecation
607 ;; warning and the delayed property of the field.
608 (let ((priority* (force priority)))
609 (if priority*
610 (format #f "--priority ~a " priority*)
611 ""))
612 "/dev/zram0\"\n"))))
613
614(define %zram-device-config
615 `("modprobe.d/zram.conf"
616 ,(plain-file "zram.conf"
617 "options zram num_devices=1")))
618
619(define (zram-device-udev-rule config)
620 (file->udev-rule "99-zram.rules"
621 (plain-file "99-zram.rules"
622 (zram-device-configuration->udev-string config))))
623
624(define zram-device-service-type
625 (service-type
626 (name 'zram)
627 (default-value (zram-device-configuration))
628 (extensions
629 (list (service-extension kernel-module-loader-service-type
630 (const (list "zram")))
631 (service-extension etc-service-type
632 (const (list %zram-device-config)))
633 (service-extension udev-service-type
634 (compose list zram-device-udev-rule))))
635 (description "Creates a zram swap device.")))