linux.scm 9.9 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. ;;;
  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. (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 base)
  26. #:use-module (gnu services shepherd)
  27. #:use-module (gnu packages linux)
  28. #:use-module (srfi srfi-1)
  29. #:use-module (srfi srfi-26)
  30. #:use-module (srfi srfi-34)
  31. #:use-module (srfi srfi-35)
  32. #:use-module (ice-9 match)
  33. #:export (earlyoom-configuration
  34. earlyoom-configuration?
  35. earlyoom-configuration-earlyoom
  36. earlyoom-configuration-minimum-available-memory
  37. earlyoom-configuration-minimum-free-swap
  38. earlyoom-configuration-prefer-regexp
  39. earlyoom-configuration-avoid-regexp
  40. earlyoom-configuration-memory-report-interval
  41. earlyoom-configuration-ignore-positive-oom-score-adj?
  42. earlyoom-configuration-show-debug-messages?
  43. earlyoom-configuration-send-notification-command
  44. earlyoom-service-type
  45. kernel-module-loader-service-type
  46. zram-device-configuration
  47. zram-device-configuration?
  48. zram-device-configuration-size
  49. zram-device-configuration-compression-algorithm
  50. zram-device-configuration-memory-limit
  51. zram-device-configuration-priority
  52. zram-device-service-type))
  53. ;;;
  54. ;;; Early OOM daemon.
  55. ;;;
  56. (define-record-type* <earlyoom-configuration>
  57. earlyoom-configuration make-earlyoom-configuration
  58. earlyoom-configuration?
  59. (earlyoom earlyoom-configuration-earlyoom
  60. (default earlyoom))
  61. (minimum-available-memory earlyoom-configuration-minimum-available-memory
  62. (default 10)) ; in percent
  63. (minimum-free-swap earlyoom-configuration-minimum-free-swap
  64. (default 10)) ; in percent
  65. (prefer-regexp earlyoom-configuration-prefer-regexp ; <string>
  66. (default #f))
  67. (avoid-regexp earlyoom-configuration-avoid-regexp ; <string>
  68. (default #f))
  69. (memory-report-interval earlyoom-configuration-memory-report-interval
  70. (default 0)) ; in seconds; 0 means disabled
  71. (ignore-positive-oom-score-adj?
  72. earlyoom-configuration-ignore-positive-oom-score-adj? (default #f))
  73. (run-with-higher-priority? earlyoom-configuration-run-with-higher-priority?
  74. (default #f))
  75. (show-debug-messages? earlyoom-configuration-show-debug-messages?
  76. (default #f))
  77. (send-notification-command
  78. earlyoom-configuration-send-notification-command ; <string>
  79. (default #f)))
  80. (define (earlyoom-configuration->command-line-args config)
  81. "Translate a <earlyoom-configuration> object to its command line arguments
  82. representation."
  83. (match config
  84. (($ <earlyoom-configuration> earlyoom minimum-available-memory
  85. minimum-free-swap prefer-regexp avoid-regexp
  86. memory-report-interval
  87. ignore-positive-oom-score-adj?
  88. run-with-higher-priority? show-debug-messages?
  89. send-notification-command)
  90. `(,(file-append earlyoom "/bin/earlyoom")
  91. ,@(if minimum-available-memory
  92. (list "-m" (format #f "~s" minimum-available-memory))
  93. '())
  94. ,@(if minimum-free-swap
  95. (list "-s" (format #f "~s" minimum-free-swap))
  96. '())
  97. ,@(if prefer-regexp
  98. (list "--prefer" prefer-regexp)
  99. '())
  100. ,@(if avoid-regexp
  101. (list "--avoid" avoid-regexp)
  102. '())
  103. "-r" ,(format #f "~s" memory-report-interval)
  104. ,@(if ignore-positive-oom-score-adj?
  105. (list "-i")
  106. '())
  107. ,@(if run-with-higher-priority?
  108. (list "-p")
  109. '())
  110. ,@(if show-debug-messages?
  111. (list "-d")
  112. '())
  113. ,@(if send-notification-command
  114. (list "-N" send-notification-command)
  115. '())))))
  116. (define (earlyoom-shepherd-service config)
  117. (shepherd-service
  118. (documentation "Run the Early OOM daemon.")
  119. (provision '(earlyoom))
  120. (start #~(make-forkexec-constructor
  121. '#$(earlyoom-configuration->command-line-args config)
  122. #:log-file "/var/log/earlyoom.log"))
  123. (stop #~(make-kill-destructor))))
  124. (define earlyoom-service-type
  125. (service-type
  126. (name 'earlyoom)
  127. (default-value (earlyoom-configuration))
  128. (extensions
  129. (list (service-extension shepherd-root-service-type
  130. (compose list earlyoom-shepherd-service))))
  131. (description "Run @command{earlyoom}, the Early OOM daemon.")))
  132. ;;;
  133. ;;; Kernel module loader.
  134. ;;;
  135. (define kernel-module-loader-shepherd-service
  136. (match-lambda
  137. ((and (? list? kernel-modules) ((? string?) ...))
  138. (list
  139. (shepherd-service
  140. (documentation "Load kernel modules.")
  141. (provision '(kernel-module-loader))
  142. (requirement '(file-systems))
  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. 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.")))