From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp12.migadu.com ([2001:41d0:2:bcc0::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms5.migadu.com with LMTPS id OKPCNCGIA2TA5QAAbAwnHQ (envelope-from ) for ; Sat, 04 Mar 2023 19:04:17 +0100 Received: from aspmx1.migadu.com ([2001:41d0:2:bcc0::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp12.migadu.com with LMTPS id GKjJNCGIA2Q3bAAAauVa8A (envelope-from ) for ; Sat, 04 Mar 2023 19:04:17 +0100 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by aspmx1.migadu.com (Postfix) with ESMTPS id 8EA9E3BB47 for ; Sat, 4 Mar 2023 19:04:17 +0100 (CET) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pYWEm-0001YE-VI; Sat, 04 Mar 2023 13:04:04 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pYWEk-0001Xz-Rl for guix-patches@gnu.org; Sat, 04 Mar 2023 13:04:03 -0500 Received: from debbugs.gnu.org ([209.51.188.43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1pYWEk-0000FQ-EB for guix-patches@gnu.org; Sat, 04 Mar 2023 13:04:02 -0500 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1pYWEj-0007sS-TM for guix-patches@gnu.org; Sat, 04 Mar 2023 13:04:01 -0500 X-Loop: help-debbugs@gnu.org Subject: [bug#61964] [PATCH] services: Add fstrim-service-type. Resent-From: Bruno Victal Original-Sender: "Debbugs-submit" Resent-CC: guix-patches@gnu.org Resent-Date: Sat, 04 Mar 2023 18:04:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: report 61964 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch To: 61964@debbugs.gnu.org Cc: Bruno Victal X-Debbugs-Original-To: guix-patches@gnu.org Received: via spool by submit@debbugs.gnu.org id=B.167795304030269 (code B ref -1); Sat, 04 Mar 2023 18:04:01 +0000 Received: (at submit) by debbugs.gnu.org; 4 Mar 2023 18:04:00 +0000 Received: from localhost ([127.0.0.1]:37704 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1pYWEi-0007s8-3E for submit@debbugs.gnu.org; Sat, 04 Mar 2023 13:04:00 -0500 Received: from lists.gnu.org ([209.51.188.17]:48634) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1pYWEg-0007s0-2K for submit@debbugs.gnu.org; Sat, 04 Mar 2023 13:03:58 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pYWEf-0001Xe-QE for guix-patches@gnu.org; Sat, 04 Mar 2023 13:03:57 -0500 Received: from smtpm4.myservices.hosting ([185.26.105.235]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pYWEd-0000E2-Ps for guix-patches@gnu.org; Sat, 04 Mar 2023 13:03:57 -0500 Received: from mail1.netim.hosting (unknown [185.26.106.173]) by smtpm4.myservices.hosting (Postfix) with ESMTP id 1C3CD20AE4 for ; Sat, 4 Mar 2023 19:03:41 +0100 (CET) Received: from localhost (localhost [127.0.0.1]) by mail1.netim.hosting (Postfix) with ESMTP id C954480097; Sat, 4 Mar 2023 19:03:41 +0100 (CET) X-Virus-Scanned: Debian amavisd-new at mail1.netim.hosting Received: from mail1.netim.hosting ([127.0.0.1]) by localhost (mail1-2.netim.hosting [127.0.0.1]) (amavisd-new, port 10026) with ESMTP id M09wF7cnjDEO; Sat, 4 Mar 2023 19:03:37 +0100 (CET) Received: from guix-nuc.home.arpa (bl9-119-177.dsl.telepac.pt [85.242.119.177]) (Authenticated sender: lumen@makinata.eu) by mail1.netim.hosting (Postfix) with ESMTPSA id 5BFED80079; Sat, 4 Mar 2023 19:03:37 +0100 (CET) From: Bruno Victal Date: Sat, 4 Mar 2023 18:03:28 +0000 Message-Id: <9fec722b58c87211f019fa702a5c7047577bec64.1677952942.git.mirai@makinata.eu> X-Mailer: git-send-email 2.39.1 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Received-SPF: pass client-ip=185.26.105.235; envelope-from=mirai@makinata.eu; helo=smtpm4.myservices.hosting X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: guix-patches@gnu.org List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guix-patches-bounces+larch=yhetil.org@gnu.org Sender: guix-patches-bounces+larch=yhetil.org@gnu.org X-Migadu-Country: US X-Migadu-Flow: FLOW_IN ARC-Seal: i=1; s=key1; d=yhetil.org; t=1677953057; a=rsa-sha256; cv=none; b=Wq+q1sGvJdcpA2g2r8ssVetshLsbQgYNYPsd3KRiRWwdHaM1lfDbEDWPKw3uKlP2yRMeor ZwOsyhNFN1wqUjjnCN7ug6bcLbCTvQ6WFp9jh6EKBHbdJmYEU6ObFVrWJ9wPP19R332Hq0 WlElKZBI8B1Me0n5Og9zNsVLjUVKMrjZ3gCySbILGTxTXwfANcZKMhHmAIEUEZkU2MhJ0w f2He7jXe9fSDxceUESeaRAZ7frdgx3/Y41JK1bxMYFSc4mZljOL32u95vmBdE36aA7MXwj zJsHayqwh77ovaWL4/B7v23lgFE5ZnFQKBkPdtNV+i+LecTzUt6FsZkatt7brQ== ARC-Authentication-Results: i=1; aspmx1.migadu.com; dkim=none; spf=pass (aspmx1.migadu.com: domain of "guix-patches-bounces+larch=yhetil.org@gnu.org" designates 209.51.188.17 as permitted sender) smtp.mailfrom="guix-patches-bounces+larch=yhetil.org@gnu.org"; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=yhetil.org; s=key1; t=1677953057; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding:resent-cc: resent-from:resent-sender:resent-message-id:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=/IixI+WJrqJIG5ojjHPjn3XPMPK0Jq/neiYoE2p4AeI=; b=sZpdzDDJoB4fWf+cQoo0pthuU0pJyXL8hGJ1pb3ZOQjKvNKLgmr4d1gyMZCzsRXRvWFE5V LDfKu9gsiJPoW3+/LyKUmhadrD/rgRBaNriPJHC0yb7QCzkeEuoBbYHSGt+z67WmhiQZD3 b4TMPjgYev+yN/vJkXfAmhAMybTP8N0k5HkdfLK109co4/crSjJdpo3uYnVXU6IDibSdoW CaYHgCsuQi/4j0HcUurtqHWGPVQ+UuvmwLky+3VH7KBsMKuMlf3MYT02ceMGdLssG9lZtR jIvGoCeGm9p/9ynmLVktt0q+k1d4bhUlmygFvlzVry6Dfge8rrWu+4NeZnUBNA== X-Migadu-Scanner: scn1.migadu.com X-Migadu-Spam-Score: -2.40 X-Spam-Score: -2.40 X-Migadu-Queue-Id: 8EA9E3BB47 Authentication-Results: aspmx1.migadu.com; dkim=none; spf=pass (aspmx1.migadu.com: domain of "guix-patches-bounces+larch=yhetil.org@gnu.org" designates 209.51.188.17 as permitted sender) smtp.mailfrom="guix-patches-bounces+larch=yhetil.org@gnu.org"; dmarc=none X-TUID: utxgM9qbGBZc * gnu/services/linux.scm (fstrim-service-type): New variable. (fstrim-mcron-job, serialize-fstrim-configuration) (fstrim-serialize-list-of-strings, fstrim-serialize-boolean): New procedure. (mcron-time?): New predicate. (fstrim-configuration): New record. * doc/guix.texi (Linux Services): Document new fstrim-service-type. --- doc/guix.texi | 62 +++++++++++++++++++++++ gnu/services/linux.scm | 109 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 171 insertions(+) diff --git a/doc/guix.texi b/doc/guix.texi index 74658dbc86..d5a83e387f 100644 --- a/doc/guix.texi +++ b/doc/guix.texi @@ -37436,6 +37436,68 @@ Linux Services @end table @end deftp +@cindex fstrim service +@cindex solid state drives, periodic trim +@cindex solid state drives, trim +@subsubheading fstrim Service + +The command @command{fstrim} can be used to discard (or @dfn{trim}) +unused blocks on a mounted filesystem. + +@c This was copied from the fstrim manpage, with some texinfo touch-ups. +@quotation Warning +Running @command{fstrim} frequently, or even using +@command{mount -o discard}, might negatively affect the lifetime of +poor-quality SSD devices. For most desktop and server systems a +sufficient trimming frequency is once a week. Note that not all devices +support a queued trim, so each trim command incurs a performance penalty +on whatever else might be trying to use the disk at the time. +@end quotation + +@defvar fstrim-service-type +Type for a service that periodically runs @command{fstrim}, whose value must +be a @code{} object. The service can be instantiated +in its default configuration with: + +@lisp +(service fstrim-service-type) +@end lisp +@end defvar + +@c %start of fragment +@deftp {Data Type} fstrim-configuration +Available @code{fstrim-configuration} fields are: + +@table @asis +@item @code{package} (default: @code{util-linux}) (type: file-like) +The package providing @command{fstrim}. + +@item @code{schedule} (default: @code{"0 0 * * 0"}) (type: mcron-time) +Schedule for launching @command{fstrim}. This can be a procedure, a +list or a string. For additional information, @pxref{Guile Syntax,, Job +specification, mcron,the mcron manual}. By default this is set to run +weekly on Sunday at 00:00. + +@item @code{listed-in} (default: @code{("/etc/fstab" "/proc/self/mountinfo")}) (type: maybe-list-of-strings) +List of files in fstab or kernel mountinfo format. All missing or empty +files are silently ignored. The evaluation of the list @emph{stops} +after the first non-empty file. Filesystems with @code{X-fstrim.notrim} +mount option in fstab are skipped. + +@item @code{verbose?} (default: @code{#t}) (type: boolean) +Verbose execution. + +@item @code{quiet-unsupported?} (default: @code{#t}) (type: boolean) +Suppress error messages if trim operation (ioctl) is unsupported. + +@item @code{extra-arguments} (type: maybe-list-of-strings) +Extra options to append to @command{fstrim} command.@footnote{Run +@command{man fstrim} for more information.} + +@end table +@end deftp +@c %end of fragment + @cindex modprobe @cindex kernel module loader @subsubheading Kernel Module Loader Service diff --git a/gnu/services/linux.scm b/gnu/services/linux.scm index 60e2093e1d..f5ec5fec48 100644 --- a/gnu/services/linux.scm +++ b/gnu/services/linux.scm @@ -5,6 +5,7 @@ ;;; Copyright © 2021 raid5atemyhomework ;;; Copyright © 2021 B. Wilson ;;; Copyright © 2022 Josselin Poiret +;;; Copyright © 2023 Bruno Victal ;;; ;;; This file is part of GNU Guix. ;;; @@ -30,12 +31,15 @@ (define-module (gnu services linux) #:use-module (guix ui) #:use-module (gnu services) #:use-module (gnu services base) + #:use-module (gnu services configuration) + #:use-module (gnu services mcron) #:use-module (gnu services shepherd) #:use-module (gnu packages linux) #:use-module (srfi srfi-1) #:use-module (srfi srfi-26) #:use-module (srfi srfi-34) #:use-module (srfi srfi-35) + #:use-module (ice-9 format) #:use-module (ice-9 match) #:export (earlyoom-configuration earlyoom-configuration? @@ -50,6 +54,16 @@ (define-module (gnu services linux) earlyoom-configuration-send-notification-command earlyoom-service-type + fstrim-configuration + fstrim-configuration? + fstrim-configuration-package + fstrim-configuration-schedule + fstrim-configuration-listed-in + fstrim-configuration-verbose? + fstrim-configuration-quiet-unsupported? + fstrim-configuration-extra-arguments + fstrim-service-type + kernel-module-loader-service-type rasdaemon-configuration @@ -150,6 +164,101 @@ (define earlyoom-service-type (compose list earlyoom-shepherd-service)))) (description "Run @command{earlyoom}, the Early OOM daemon."))) + +;;; +;;; fstrim +;;; + +(define (mcron-time? x) + (or (procedure? x) (string? x) (list? x))) + +(define-maybe list-of-strings (prefix fstrim-)) + +(define (fstrim-serialize-boolean field-name value) + (list (format #f "~:[~;--~a~]" value + ;; drop trailing '?' character + (string-drop-right (symbol->string field-name) 1)))) + +(define (fstrim-serialize-list-of-strings field-name value) + (list (string-append "--" (symbol->string field-name)) + #~(string-join '#$value ":"))) + +(define-configuration fstrim-configuration + (package + (file-like util-linux) + "The package providing @command{fstrim}." + empty-serializer) + + (schedule + (mcron-time "0 0 * * 0") + "Schedule for launching @command{fstrim}. This can be a procedure, a list +or a string. For additional information, @pxref{Guile Syntax,, +Job specification, mcron, the mcron manual}. By default this is set to run +weekly on Sunday at 00:00." + empty-serializer) + + ;; fstrim options + (listed-in + (maybe-list-of-strings '("/etc/fstab" "/proc/self/mountinfo")) + ;; XXX: documentation sourced from the fstrim manpage. + "List of files in fstab or kernel mountinfo format. All missing or +empty files are silently ignored. The evaluation of the list @emph{stops} +after the first non-empty file. Filesystems with @code{X-fstrim.notrim} mount +option in fstab are skipped.") + + (verbose? + (boolean #t) + "Verbose execution.") + + (quiet-unsupported? + (boolean #t) + "Suppress error messages if trim operation (ioctl) is unsupported.") + + (extra-arguments + maybe-list-of-strings + ;; FIXME@GUILE(TEXINFO): @footnote causes errors when calling + ;; configuration->documentation. + ;; > Throw to key `parser-error' with args `(#f "Unknown command" footnote)' + "Extra options to append to @command{fstrim} command.@footnote{Run +@command{man fstrim} for more information.}" + (lambda (_ value) + (if (maybe-value-set? value) + value '()))) + + (prefix fstrim-)) + +(define (serialize-fstrim-configuration config) + (concatenate + (filter list? + (map (lambda (field) + ((configuration-field-serializer field) + (configuration-field-name field) + ((configuration-field-getter field) config))) + fstrim-configuration-fields)))) + +(define (fstrim-mcron-job config) + (match-record config (package schedule) + #~(job + ;; XXX: The “if” below is to ensure that + ;; lists are ungexp'd correctly since @var{schedule} + ;; can be either a procedure, a string or a list. + #$(if (list? schedule) + `(list ,@schedule) + schedule) + (lambda () + (system* #$(file-append package "/sbin/fstrim") + #$@(serialize-fstrim-configuration config))) + "fstrim"))) + +(define fstrim-service-type + (service-type + (name 'fstrim) + (extensions + (list (service-extension mcron-service-type + (compose list fstrim-mcron-job)))) + (description "Discard unused blocks from filesystems.") + (default-value (fstrim-configuration)))) + ;;; ;;; Kernel module loader. base-commit: d6045055720bc0763f8a079d75b941b4898349a4 -- 2.39.1