linux.scm 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. ;;;
  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. (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. kernel-module-loader-service-type
  47. zram-device-configuration
  48. zram-device-configuration?
  49. zram-device-configuration-size
  50. zram-device-configuration-compression-algorithm
  51. zram-device-configuration-memory-limit
  52. zram-device-configuration-priority
  53. zram-device-service-type))
  54. ;;;
  55. ;;; Early OOM daemon.
  56. ;;;
  57. (define-record-type* <earlyoom-configuration>
  58. earlyoom-configuration make-earlyoom-configuration
  59. earlyoom-configuration?
  60. (earlyoom earlyoom-configuration-earlyoom
  61. (default earlyoom))
  62. (minimum-available-memory earlyoom-configuration-minimum-available-memory
  63. (default 10)) ; in percent
  64. (minimum-free-swap earlyoom-configuration-minimum-free-swap
  65. (default 10)) ; in percent
  66. (prefer-regexp earlyoom-configuration-prefer-regexp ; <string>
  67. (default #f))
  68. (avoid-regexp earlyoom-configuration-avoid-regexp ; <string>
  69. (default #f))
  70. (memory-report-interval earlyoom-configuration-memory-report-interval
  71. (default 0)) ; in seconds; 0 means disabled
  72. (ignore-positive-oom-score-adj?
  73. earlyoom-configuration-ignore-positive-oom-score-adj? (default #f))
  74. (run-with-higher-priority? earlyoom-configuration-run-with-higher-priority?
  75. (default #f))
  76. (show-debug-messages? earlyoom-configuration-show-debug-messages?
  77. (default #f))
  78. (send-notification-command
  79. earlyoom-configuration-send-notification-command ; <string>
  80. (default #f)))
  81. (define (earlyoom-configuration->command-line-args config)
  82. "Translate a <earlyoom-configuration> object to its command line arguments
  83. representation."
  84. (match config
  85. (($ <earlyoom-configuration> earlyoom minimum-available-memory
  86. minimum-free-swap prefer-regexp avoid-regexp
  87. memory-report-interval
  88. ignore-positive-oom-score-adj?
  89. run-with-higher-priority? show-debug-messages?
  90. send-notification-command)
  91. `(,(file-append earlyoom "/bin/earlyoom")
  92. ,@(if minimum-available-memory
  93. (list "-m" (format #f "~s" minimum-available-memory))
  94. '())
  95. ,@(if minimum-free-swap
  96. (list "-s" (format #f "~s" minimum-free-swap))
  97. '())
  98. ,@(if prefer-regexp
  99. (list "--prefer" prefer-regexp)
  100. '())
  101. ,@(if avoid-regexp
  102. (list "--avoid" avoid-regexp)
  103. '())
  104. "-r" ,(format #f "~s" memory-report-interval)
  105. ,@(if ignore-positive-oom-score-adj?
  106. (list "-i")
  107. '())
  108. ,@(if run-with-higher-priority?
  109. (list "-p")
  110. '())
  111. ,@(if show-debug-messages?
  112. (list "-d")
  113. '())
  114. ,@(if send-notification-command
  115. (list "-N" send-notification-command)
  116. '())))))
  117. (define (earlyoom-shepherd-service config)
  118. (shepherd-service
  119. (documentation "Run the Early OOM daemon.")
  120. (provision '(earlyoom))
  121. (start #~(make-forkexec-constructor
  122. '#$(earlyoom-configuration->command-line-args config)
  123. #:log-file "/var/log/earlyoom.log"))
  124. (stop #~(make-kill-destructor))))
  125. (define earlyoom-service-type
  126. (service-type
  127. (name 'earlyoom)
  128. (default-value (earlyoom-configuration))
  129. (extensions
  130. (list (service-extension shepherd-root-service-type
  131. (compose list earlyoom-shepherd-service))))
  132. (description "Run @command{earlyoom}, the Early OOM daemon.")))
  133. ;;;
  134. ;;; Kernel module loader.
  135. ;;;
  136. (define kernel-module-loader-shepherd-service
  137. (match-lambda
  138. ((and (? list? kernel-modules) ((? string?) ...))
  139. (shepherd-service
  140. (documentation "Load kernel modules.")
  141. (provision '(kernel-module-loader))
  142. (requirement '())
  143. (one-shot? #t)
  144. (modules `((srfi srfi-1)
  145. (srfi srfi-34)
  146. (srfi srfi-35)
  147. (rnrs io ports)
  148. ,@%default-modules))
  149. (start
  150. #~(lambda _
  151. (cond
  152. ((null? '#$kernel-modules) #t)
  153. ((file-exists? "/proc/sys/kernel/modprobe")
  154. (let ((modprobe (call-with-input-file
  155. "/proc/sys/kernel/modprobe" get-line)))
  156. (guard (c ((message-condition? c)
  157. (format (current-error-port) "~a~%"
  158. (condition-message c))
  159. #f))
  160. (every (lambda (module)
  161. (invoke/quiet modprobe "--" module))
  162. '#$kernel-modules))))
  163. (else
  164. (format (current-error-port) "error: ~a~%"
  165. "Kernel is missing loadable module support.")
  166. #f))))))))
  167. (define kernel-module-loader-service-type
  168. (service-type
  169. (name 'kernel-module-loader)
  170. (description "Load kernel modules.")
  171. (extensions
  172. (list (service-extension shepherd-root-service-type
  173. (compose list kernel-module-loader-shepherd-service))))
  174. (compose concatenate)
  175. (extend append)
  176. (default-value '())))
  177. ;;;
  178. ;;; Kernel module loader.
  179. ;;;
  180. (define-record-type* <zram-device-configuration>
  181. zram-device-configuration make-zram-device-configuration
  182. zram-device-configuration?
  183. (size zram-device-configuration-size
  184. (default "1G")) ; string or integer
  185. (compression-algorithm zram-device-configuration-compression-algorithm
  186. (default 'lzo)) ; symbol
  187. (memory-limit zram-device-configuration-memory-limit
  188. (default 0)) ; string or integer
  189. (priority zram-device-configuration-priority
  190. (default -1))) ; integer
  191. (define (zram-device-configuration->udev-string config)
  192. "Translate a <zram-device-configuration> into a string which can be
  193. placed in a udev rules file."
  194. (match config
  195. (($ <zram-device-configuration> size compression-algorithm memory-limit priority)
  196. (string-append
  197. "KERNEL==\"zram0\", "
  198. "ATTR{comp_algorithm}=\"" (symbol->string compression-algorithm) "\" "
  199. (if (not (or (equal? "0" size)
  200. (equal? 0 size)))
  201. (string-append "ATTR{disksize}=\"" (if (number? size)
  202. (number->string size)
  203. size)
  204. "\" ")
  205. "")
  206. (if (not (or (equal? "0" memory-limit)
  207. (equal? 0 memory-limit)))
  208. (string-append "ATTR{mem_limit}=\"" (if (number? memory-limit)
  209. (number->string memory-limit)
  210. memory-limit)
  211. "\" ")
  212. "")
  213. "RUN+=\"/run/current-system/profile/sbin/mkswap /dev/zram0\" "
  214. "RUN+=\"/run/current-system/profile/sbin/swapon "
  215. (if (not (equal? -1 priority))
  216. (string-append "--priority " (number->string priority) " ")
  217. "")
  218. "/dev/zram0\"\n"))))
  219. (define %zram-device-config
  220. `("modprobe.d/zram.conf"
  221. ,(plain-file "zram.conf"
  222. "options zram num_devices=1")))
  223. (define (zram-device-udev-rule config)
  224. (file->udev-rule "99-zram.rules"
  225. (plain-file "99-zram.rules"
  226. (zram-device-configuration->udev-string config))))
  227. (define zram-device-service-type
  228. (service-type
  229. (name 'zram)
  230. (default-value (zram-device-configuration))
  231. (extensions
  232. (list (service-extension kernel-module-loader-service-type
  233. (const (list "zram")))
  234. (service-extension etc-service-type
  235. (const (list %zram-device-config)))
  236. (service-extension udev-service-type
  237. (compose list zram-device-udev-rule))))
  238. (description "Creates a zram swap device.")))