From d3e439e355284f136ff13eb3e6fffb4661552f3b Mon Sep 17 00:00:00 2001 From: Maxim Cournoyer Date: Mon, 6 Jan 2020 01:36:00 -0500 Subject: gnu: Add earlyoom-service-type. * gnu/services/linux.scm: New file. * tests/services/linux.scm: Add test. * Makefile.am (SCM_TESTS): Register test. * doc/guix.texi (Linux Services): Add a new section and document the new service and its configuration. --- gnu/services/linux.scm | 125 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 125 insertions(+) create mode 100644 gnu/services/linux.scm (limited to 'gnu/services') diff --git a/gnu/services/linux.scm b/gnu/services/linux.scm new file mode 100644 index 0000000000..caa0326c31 --- /dev/null +++ b/gnu/services/linux.scm @@ -0,0 +1,125 @@ +;;; GNU Guix --- Functional package management for GNU +;;; Copyright © 2020 Maxim Cournoyer +;;; +;;; This file is part of GNU Guix. +;;; +;;; GNU Guix is free software; you can redistribute it and/or modify it +;;; under the terms of the GNU General Public License as published by +;;; the Free Software Foundation; either version 3 of the License, or (at +;;; your option) any later version. +;;; +;;; GNU Guix is distributed in the hope that it will be useful, but +;;; WITHOUT ANY WARRANTY; without even the implied warranty of +;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +;;; GNU General Public License for more details. +;;; +;;; You should have received a copy of the GNU General Public License +;;; along with GNU Guix. If not, see . + +(define-module (gnu services linux) + #:use-module (guix gexp) + #:use-module (guix records) + #:use-module (guix modules) + #:use-module (gnu services) + #:use-module (gnu services shepherd) + #:use-module (gnu packages linux) + #:use-module (srfi srfi-1) + #:use-module (srfi srfi-26) + #:use-module (ice-9 match) + #:export (earlyoom-configuration + earlyoom-configuration? + earlyoom-configuration-earlyoom + earlyoom-configuration-minimum-available-memory + earlyoom-configuration-minimum-free-swap + earlyoom-configuration-prefer-regexp + earlyoom-configuration-avoid-regexp + earlyoom-configuration-memory-report-interval + earlyoom-configuration-ignore-positive-oom-score-adj? + earlyoom-configuration-show-debug-messages? + earlyoom-configuration-send-notification-command + earlyoom-service-type)) + + +;;; +;;; Early OOM daemon. +;;; + +(define-record-type* + earlyoom-configuration make-earlyoom-configuration + earlyoom-configuration? + (earlyoom earlyoom-configuration-earlyoom + (default earlyoom)) + (minimum-available-memory earlyoom-configuration-minimum-available-memory + (default 10)) ; in percent + (minimum-free-swap earlyoom-configuration-minimum-free-swap + (default 10)) ; in percent + (prefer-regexp earlyoom-configuration-prefer-regexp ; + (default #f)) + (avoid-regexp earlyoom-configuration-avoid-regexp ; + (default #f)) + (memory-report-interval earlyoom-configuration-memory-report-interval + (default 0)) ; in seconds; 0 means disabled + (ignore-positive-oom-score-adj? + earlyoom-configuration-ignore-positive-oom-score-adj? (default #f)) + (run-with-higher-priority? earlyoom-configuration-run-with-higher-priority? + (default #f)) + (show-debug-messages? earlyoom-configuration-show-debug-messages? + (default #f)) + (send-notification-command + earlyoom-configuration-send-notification-command ; + (default #f))) + +(define (earlyoom-configuration->command-line-args config) + "Translate a object to its command line arguments +representation." + (match config + (($ earlyoom minimum-available-memory + minimum-free-swap prefer-regexp avoid-regexp + memory-report-interval + ignore-positive-oom-score-adj? + run-with-higher-priority? show-debug-messages? + send-notification-command) + `(,(file-append earlyoom "/bin/earlyoom") + ,@(if minimum-available-memory + (list "-m" (format #f "~s" minimum-available-memory)) + '()) + ,@(if minimum-free-swap + (list "-s" (format #f "~s" minimum-free-swap)) + '()) + ,@(if prefer-regexp + (list "--prefer" prefer-regexp) + '()) + ,@(if avoid-regexp + (list "--avoid" avoid-regexp) + '()) + "-r" ,(format #f "~s" memory-report-interval) + ,@(if ignore-positive-oom-score-adj? + (list "-i") + '()) + ,@(if run-with-higher-priority? + (list "-p") + '()) + ,@(if show-debug-messages? + (list "-d") + '()) + ,@(if send-notification-command + (list "-N" send-notification-command) + '()))))) + +(define (earlyoom-shepherd-service config) + (shepherd-service + (documentation "Run the Early OOM daemon.") + (provision '(earlyoom)) + (start #~(make-forkexec-constructor + '#$(earlyoom-configuration->command-line-args config) + #:log-file "/var/log/earlyoom.log")) + (stop #~(make-kill-destructor)))) + +(define earlyoom-service-type + (service-type + (name 'earlyoom) + (default-value (earlyoom-configuration)) + (extensions + (list (service-extension shepherd-root-service-type + (compose list earlyoom-shepherd-service)))) + (description "Run @command{earlyoom}, the Early OOM daemon."))) -- cgit v1.2.3