linux.scm 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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. ;;; Copyright © 2021 raid5atemyhomework <raid5atemyhomework@protonmail.com>
  6. ;;; Copyright © 2021 B. Wilson <elaexuotee@wilsonb.com>
  7. ;;;
  8. ;;; This file is part of GNU Guix.
  9. ;;;
  10. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  11. ;;; under the terms of the GNU General Public License as published by
  12. ;;; the Free Software Foundation; either version 3 of the License, or (at
  13. ;;; your option) any later version.
  14. ;;;
  15. ;;; GNU Guix is distributed in the hope that it will be useful, but
  16. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  17. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. ;;; GNU General Public License for more details.
  19. ;;;
  20. ;;; You should have received a copy of the GNU General Public License
  21. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  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)
  27. #:use-module (gnu services base)
  28. #:use-module (gnu services shepherd)
  29. #:use-module (gnu packages linux)
  30. #:use-module (srfi srfi-1)
  31. #:use-module (srfi srfi-26)
  32. #:use-module (srfi srfi-34)
  33. #:use-module (srfi srfi-35)
  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
  46. earlyoom-service-type
  47. kernel-module-loader-service-type
  48. rasdaemon-configuration
  49. rasdaemon-configuration?
  50. rasdaemon-configuration-record?
  51. rasdaemon-service-type
  52. zram-device-configuration
  53. zram-device-configuration?
  54. zram-device-configuration-size
  55. zram-device-configuration-compression-algorithm
  56. zram-device-configuration-memory-limit
  57. zram-device-configuration-priority
  58. zram-device-service-type))
  59. ;;;
  60. ;;; Early OOM daemon.
  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. (define (earlyoom-configuration->command-line-args config)
  87. "Translate a <earlyoom-configuration> object to its command line arguments
  88. representation."
  89. (match config
  90. (($ <earlyoom-configuration> earlyoom minimum-available-memory
  91. minimum-free-swap prefer-regexp avoid-regexp
  92. memory-report-interval
  93. ignore-positive-oom-score-adj?
  94. run-with-higher-priority? show-debug-messages?
  95. send-notification-command)
  96. `(,(file-append earlyoom "/bin/earlyoom")
  97. ,@(if minimum-available-memory
  98. (list "-m" (format #f "~s" minimum-available-memory))
  99. '())
  100. ,@(if minimum-free-swap
  101. (list "-s" (format #f "~s" minimum-free-swap))
  102. '())
  103. ,@(if prefer-regexp
  104. (list "--prefer" prefer-regexp)
  105. '())
  106. ,@(if avoid-regexp
  107. (list "--avoid" avoid-regexp)
  108. '())
  109. "-r" ,(format #f "~s" memory-report-interval)
  110. ,@(if ignore-positive-oom-score-adj?
  111. (list "-i")
  112. '())
  113. ,@(if run-with-higher-priority?
  114. (list "-p")
  115. '())
  116. ,@(if show-debug-messages?
  117. (list "-d")
  118. '())
  119. ,@(if send-notification-command
  120. (list "-N" send-notification-command)
  121. '())))))
  122. (define (earlyoom-shepherd-service config)
  123. (shepherd-service
  124. (documentation "Run the Early OOM daemon.")
  125. (provision '(earlyoom))
  126. (start #~(make-forkexec-constructor
  127. '#$(earlyoom-configuration->command-line-args config)
  128. #:log-file "/var/log/earlyoom.log"))
  129. (stop #~(make-kill-destructor))))
  130. (define earlyoom-service-type
  131. (service-type
  132. (name 'earlyoom)
  133. (default-value (earlyoom-configuration))
  134. (extensions
  135. (list (service-extension shepherd-root-service-type
  136. (compose list earlyoom-shepherd-service))))
  137. (description "Run @command{earlyoom}, the Early OOM daemon.")))
  138. ;;;
  139. ;;; Kernel module loader.
  140. ;;;
  141. (define kernel-module-loader-shepherd-service
  142. (match-lambda
  143. ((and (? list? kernel-modules) ((? string?) ...))
  144. (shepherd-service
  145. (documentation "Load kernel modules.")
  146. (provision '(kernel-module-loader))
  147. (requirement '())
  148. (one-shot? #t)
  149. (modules `((srfi srfi-1)
  150. (srfi srfi-34)
  151. (srfi srfi-35)
  152. (rnrs io ports)
  153. ,@%default-modules))
  154. (start
  155. #~(lambda _
  156. (cond
  157. ((null? '#$kernel-modules) #t)
  158. ((file-exists? "/proc/sys/kernel/modprobe")
  159. (let ((modprobe (call-with-input-file
  160. "/proc/sys/kernel/modprobe" get-line)))
  161. (guard (c ((message-condition? c)
  162. (format (current-error-port) "~a~%"
  163. (condition-message c))
  164. #f))
  165. (every (lambda (module)
  166. (invoke/quiet modprobe "--" module))
  167. '#$kernel-modules))))
  168. (else
  169. (format (current-error-port) "error: ~a~%"
  170. "Kernel is missing loadable module support.")
  171. #f))))))))
  172. (define kernel-module-loader-service-type
  173. (service-type
  174. (name 'kernel-module-loader)
  175. (description "Load kernel modules.")
  176. (extensions
  177. (list (service-extension shepherd-root-service-type
  178. (compose list kernel-module-loader-shepherd-service))))
  179. (compose concatenate)
  180. (extend append)
  181. (default-value '())))
  182. ;;;
  183. ;;; Reliability, Availability, and Serviceability (RAS) daemon
  184. ;;;
  185. (define-record-type* <rasdaemon-configuration>
  186. rasdaemon-configuration make-rasdaemon-configuration
  187. rasdaemon-configuration?
  188. (record? rasdaemon-configuration-record? (default #f)))
  189. (define (rasdaemon-configuration->command-line-args config)
  190. "Translate <rasdaemon-configuration> to its command line arguments
  191. representation"
  192. (let ((record? (rasdaemon-configuration-record? config)))
  193. `(,(file-append rasdaemon "/sbin/rasdaemon")
  194. "--foreground" ,@(if record? '("--record") '()))))
  195. (define (rasdaemon-activation config)
  196. (let ((record? (rasdaemon-configuration-record? config))
  197. (rasdaemon-dir "/var/lib/rasdaemon"))
  198. (with-imported-modules '((guix build utils))
  199. #~(if #$record? (mkdir-p #$rasdaemon-dir)))))
  200. (define (rasdaemon-shepherd-service config)
  201. (shepherd-service
  202. (documentation "Run rasdaemon")
  203. (provision '(rasdaemon))
  204. (requirement '(syslogd))
  205. (start #~(make-forkexec-constructor
  206. '#$(rasdaemon-configuration->command-line-args config)))
  207. (stop #~(make-kill-destructor))))
  208. (define rasdaemon-service-type
  209. (service-type
  210. (name 'rasdaemon)
  211. (default-value (rasdaemon-configuration))
  212. (extensions
  213. (list (service-extension shepherd-root-service-type
  214. (compose list rasdaemon-shepherd-service))
  215. (service-extension activation-service-type rasdaemon-activation)))
  216. (compose concatenate)
  217. (description "Run @command{rasdaemon}, the RAS monitor")))
  218. ;;;
  219. ;;; Kernel module loader.
  220. ;;;
  221. (define-record-type* <zram-device-configuration>
  222. zram-device-configuration make-zram-device-configuration
  223. zram-device-configuration?
  224. (size zram-device-configuration-size
  225. (default "1G")) ; string or integer
  226. (compression-algorithm zram-device-configuration-compression-algorithm
  227. (default 'lzo)) ; symbol
  228. (memory-limit zram-device-configuration-memory-limit
  229. (default 0)) ; string or integer
  230. (priority zram-device-configuration-priority
  231. (default -1))) ; integer
  232. (define (zram-device-configuration->udev-string config)
  233. "Translate a <zram-device-configuration> into a string which can be
  234. placed in a udev rules file."
  235. (match config
  236. (($ <zram-device-configuration> size compression-algorithm memory-limit priority)
  237. (string-append
  238. "KERNEL==\"zram0\", "
  239. "ATTR{comp_algorithm}=\"" (symbol->string compression-algorithm) "\" "
  240. (if (not (or (equal? "0" size)
  241. (equal? 0 size)))
  242. (string-append "ATTR{disksize}=\"" (if (number? size)
  243. (number->string size)
  244. size)
  245. "\" ")
  246. "")
  247. (if (not (or (equal? "0" memory-limit)
  248. (equal? 0 memory-limit)))
  249. (string-append "ATTR{mem_limit}=\"" (if (number? memory-limit)
  250. (number->string memory-limit)
  251. memory-limit)
  252. "\" ")
  253. "")
  254. "RUN+=\"/run/current-system/profile/sbin/mkswap /dev/zram0\" "
  255. "RUN+=\"/run/current-system/profile/sbin/swapon "
  256. (if (not (equal? -1 priority))
  257. (string-append "--priority " (number->string priority) " ")
  258. "")
  259. "/dev/zram0\"\n"))))
  260. (define %zram-device-config
  261. `("modprobe.d/zram.conf"
  262. ,(plain-file "zram.conf"
  263. "options zram num_devices=1")))
  264. (define (zram-device-udev-rule config)
  265. (file->udev-rule "99-zram.rules"
  266. (plain-file "99-zram.rules"
  267. (zram-device-configuration->udev-string config))))
  268. (define zram-device-service-type
  269. (service-type
  270. (name 'zram)
  271. (default-value (zram-device-configuration))
  272. (extensions
  273. (list (service-extension kernel-module-loader-service-type
  274. (const (list "zram")))
  275. (service-extension etc-service-type
  276. (const (list %zram-device-config)))
  277. (service-extension udev-service-type
  278. (compose list zram-device-udev-rule))))
  279. (description "Creates a zram swap device.")))