| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 | |
| 19 |
|
| 20 | (define-module (gnu services linux)
|
| 21 | #:use-module (guix gexp)
|
| 22 | #:use-module (guix records)
|
| 23 | #:use-module (guix modules)
|
| 24 | #:use-module (gnu services)
|
| 25 | #:use-module (gnu services shepherd)
|
| 26 | #:use-module (gnu packages linux)
|
| 27 | #:use-module (srfi srfi-1)
|
| 28 | #:use-module (srfi srfi-26)
|
| 29 | #:use-module (srfi srfi-34)
|
| 30 | #:use-module (srfi srfi-35)
|
| 31 | #:use-module (ice-9 match)
|
| 32 | #:export (earlyoom-configuration
|
| 33 | earlyoom-configuration?
|
| 34 | earlyoom-configuration-earlyoom
|
| 35 | earlyoom-configuration-minimum-available-memory
|
| 36 | earlyoom-configuration-minimum-free-swap
|
| 37 | earlyoom-configuration-prefer-regexp
|
| 38 | earlyoom-configuration-avoid-regexp
|
| 39 | earlyoom-configuration-memory-report-interval
|
| 40 | earlyoom-configuration-ignore-positive-oom-score-adj?
|
| 41 | earlyoom-configuration-show-debug-messages?
|
| 42 | earlyoom-configuration-send-notification-command
|
| 43 | earlyoom-service-type
|
| 44 |
|
| 45 | kernel-module-loader-service-type))
|
| 46 |
|
| 47 |
|
| 48 | |
| 49 | |
| 50 | |
| 51 |
|
| 52 | (define-record-type* <earlyoom-configuration>
|
| 53 | earlyoom-configuration make-earlyoom-configuration
|
| 54 | earlyoom-configuration?
|
| 55 | (earlyoom earlyoom-configuration-earlyoom
|
| 56 | (default earlyoom))
|
| 57 | (minimum-available-memory earlyoom-configuration-minimum-available-memory
|
| 58 | (default 10)) |
| 59 | (minimum-free-swap earlyoom-configuration-minimum-free-swap
|
| 60 | (default 10)) |
| 61 | (prefer-regexp earlyoom-configuration-prefer-regexp |
| 62 | (default #f))
|
| 63 | (avoid-regexp earlyoom-configuration-avoid-regexp |
| 64 | (default #f))
|
| 65 | (memory-report-interval earlyoom-configuration-memory-report-interval
|
| 66 | (default 0)) |
| 67 | (ignore-positive-oom-score-adj?
|
| 68 | earlyoom-configuration-ignore-positive-oom-score-adj? (default #f))
|
| 69 | (run-with-higher-priority? earlyoom-configuration-run-with-higher-priority?
|
| 70 | (default #f))
|
| 71 | (show-debug-messages? earlyoom-configuration-show-debug-messages?
|
| 72 | (default #f))
|
| 73 | (send-notification-command
|
| 74 | earlyoom-configuration-send-notification-command |
| 75 | (default #f)))
|
| 76 |
|
| 77 | (define (earlyoom-configuration->command-line-args config)
|
| 78 | "Translate a <earlyoom-configuration> object to its command line arguments
|
| 79 | representation."
|
| 80 | (match config
|
| 81 | (($ <earlyoom-configuration> earlyoom minimum-available-memory
|
| 82 | minimum-free-swap prefer-regexp avoid-regexp
|
| 83 | memory-report-interval
|
| 84 | ignore-positive-oom-score-adj?
|
| 85 | run-with-higher-priority? show-debug-messages?
|
| 86 | send-notification-command)
|
| 87 | `(,(file-append earlyoom "/bin/earlyoom")
|
| 88 | ,@(if minimum-available-memory
|
| 89 | (list "-m" (format #f "~s" minimum-available-memory))
|
| 90 | '())
|
| 91 | ,@(if minimum-free-swap
|
| 92 | (list "-s" (format #f "~s" minimum-free-swap))
|
| 93 | '())
|
| 94 | ,@(if prefer-regexp
|
| 95 | (list "--prefer" prefer-regexp)
|
| 96 | '())
|
| 97 | ,@(if avoid-regexp
|
| 98 | (list "--avoid" avoid-regexp)
|
| 99 | '())
|
| 100 | "-r" ,(format #f "~s" memory-report-interval)
|
| 101 | ,@(if ignore-positive-oom-score-adj?
|
| 102 | (list "-i")
|
| 103 | '())
|
| 104 | ,@(if run-with-higher-priority?
|
| 105 | (list "-p")
|
| 106 | '())
|
| 107 | ,@(if show-debug-messages?
|
| 108 | (list "-d")
|
| 109 | '())
|
| 110 | ,@(if send-notification-command
|
| 111 | (list "-N" send-notification-command)
|
| 112 | '())))))
|
| 113 |
|
| 114 | (define (earlyoom-shepherd-service config)
|
| 115 | (shepherd-service
|
| 116 | (documentation "Run the Early OOM daemon.")
|
| 117 | (provision '(earlyoom))
|
| 118 | (start #~(make-forkexec-constructor
|
| 119 | '#$(earlyoom-configuration->command-line-args config)
|
| 120 | #:log-file "/var/log/earlyoom.log"))
|
| 121 | (stop #~(make-kill-destructor))))
|
| 122 |
|
| 123 | (define earlyoom-service-type
|
| 124 | (service-type
|
| 125 | (name 'earlyoom)
|
| 126 | (default-value (earlyoom-configuration))
|
| 127 | (extensions
|
| 128 | (list (service-extension shepherd-root-service-type
|
| 129 | (compose list earlyoom-shepherd-service))))
|
| 130 | (description "Run @command{earlyoom}, the Early OOM daemon.")))
|
| 131 |
|
| 132 |
|
| 133 | |
| 134 | |
| 135 | |
| 136 |
|
| 137 | (define kernel-module-loader-shepherd-service
|
| 138 | (match-lambda
|
| 139 | ((and (? list? kernel-modules) ((? string?) ...))
|
| 140 | (list
|
| 141 | (shepherd-service
|
| 142 | (documentation "Load kernel modules.")
|
| 143 | (provision '(kernel-module-loader))
|
| 144 | (requirement '(file-systems))
|
| 145 | (one-shot? #t)
|
| 146 | (modules `((srfi srfi-1)
|
| 147 | (srfi srfi-34)
|
| 148 | (srfi srfi-35)
|
| 149 | (rnrs io ports)
|
| 150 | ,@%default-modules))
|
| 151 | (start
|
| 152 | #~(lambda _
|
| 153 | (cond
|
| 154 | ((null? '#$kernel-modules) #t)
|
| 155 | ((file-exists? "/proc/sys/kernel/modprobe")
|
| 156 | (let ((modprobe (call-with-input-file
|
| 157 | "/proc/sys/kernel/modprobe" get-line)))
|
| 158 | (guard (c ((message-condition? c)
|
| 159 | (format (current-error-port) "~a~%"
|
| 160 | (condition-message c))
|
| 161 | #f))
|
| 162 | (every (lambda (module)
|
| 163 | (invoke/quiet modprobe "--" module))
|
| 164 | '#$kernel-modules))))
|
| 165 | (else
|
| 166 | (format (current-error-port) "error: ~a~%"
|
| 167 | "Kernel is missing loadable module support.")
|
| 168 | #f)))))))))
|
| 169 |
|
| 170 | (define kernel-module-loader-service-type
|
| 171 | (service-type
|
| 172 | (name 'kernel-module-loader)
|
| 173 | (description "Load kernel modules.")
|
| 174 | (extensions
|
| 175 | (list (service-extension shepherd-root-service-type
|
| 176 | kernel-module-loader-shepherd-service)))
|
| 177 | (compose concatenate)
|
| 178 | (extend append)
|
| 179 | (default-value '())))
|