all messages for Guix-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* [bug#58086] [PATCH] gnu: Add fstrim-service-type.
@ 2022-09-26  7:28 iyzsong--- via Guix-patches via
  2022-10-06 21:00 ` Ludovic Courtès
  2023-03-22 12:03 ` Bruno Victal
  0 siblings, 2 replies; 4+ messages in thread
From: iyzsong--- via Guix-patches via @ 2022-09-26  7:28 UTC (permalink / raw)
  To: 58086; +Cc: 宋文武

From: 宋文武 <iyzsong@member.fsf.org>

A timestamp file "/var/lib/mcron/fstrim.stamp" is used to ensure we will
catch up on missed job runs when the system was powered down.

* gnu/services/mcron.scm (%mcron-activation): New extension to create
'/var/lib/mcron'.
* gnu/services/admin.scm (fstrim-configuration): New record type.
(fstrim-mcron-jobs): New procedure.
(fstrim-service-type): New service type.
---
 gnu/services/admin.scm | 56 +++++++++++++++++++++++++++++++++++++++++-
 gnu/services/mcron.scm |  8 ++++++
 2 files changed, 63 insertions(+), 1 deletion(-)

diff --git a/gnu/services/admin.scm b/gnu/services/admin.scm
index 252bedb0bd..2b22fc5b33 100644
--- a/gnu/services/admin.scm
+++ b/gnu/services/admin.scm
@@ -21,6 +21,7 @@
 (define-module (gnu services admin)
   #:use-module (gnu packages admin)
   #:use-module (gnu packages certs)
+  #:use-module (gnu packages linux)
   #:use-module (gnu packages package-management)
   #:use-module (gnu services)
   #:use-module (gnu services mcron)
@@ -30,6 +31,7 @@ (define-module (gnu services admin)
   #:use-module (guix packages)
   #:use-module (guix records)
   #:use-module (srfi srfi-1)
+  #:use-module (ice-9 match)
   #:use-module (ice-9 vlist)
   #:export (%default-rotations
             %rotated-files
@@ -63,7 +65,11 @@ (define-module (gnu services admin)
             unattended-upgrade-configuration-services-to-restart
             unattended-upgrade-configuration-system-expiration
             unattended-upgrade-configuration-maximum-duration
-            unattended-upgrade-configuration-log-file))
+            unattended-upgrade-configuration-log-file
+
+            fstrim-service-type
+            fstrim-configuration
+            fstrim-configuration?))
 
 ;;; Commentary:
 ;;;
@@ -376,4 +382,52 @@ (define unattended-upgrade-service-type
     "Periodically upgrade the system from the current configuration.")
    (default-value (unattended-upgrade-configuration))))
 
+\f
+;;;
+;;; fstrim.
+;;;
+
+(define-record-type* <fstrim-configuration>
+  fstrim-configuration make-fstrim-configuration fstrim-configuration?
+  (command  fstrim-configuration-command
+            (default
+              (list (file-append util-linux "/sbin/fstrim")
+                    "--verbose" "--quiet-unsupported"
+                    "--listed-in" "/etc/fstab")))
+  (interval fstrim-configuration-interval (default (* 60 60 24 7)))) ; weekly
+
+;;; By storing the time of job's last run in a file, we can catch up on missed
+;;; runs when the system was powered down.
+(define fstrim-mcron-stamp "/var/lib/mcron/fstrim.stamp")
+
+(define fstrim-mcron-jobs
+  (match-lambda
+    (($ <fstrim-configuration> command interval)
+     (list
+      #~(job
+         (let ((last-time
+                (catch #t
+                  (lambda ()
+                    (with-input-from-file #$fstrim-mcron-stamp read))
+                  ;; We schedule a first run immediately.
+                  (const 0))))
+           (lambda (current-time)
+             (let ((next-time (max current-time (+ #$interval last-time))))
+               (set! last-time next-time)
+               next-time)))
+         (lambda ()
+           (apply system* '#$command)
+           (with-output-to-file #$fstrim-mcron-stamp
+             (lambda () (write (current-time))))))))))
+
+(define fstrim-service-type
+  (service-type
+   (name 'fstrim)
+   (extensions
+    (list (service-extension mcron-service-type
+                             fstrim-mcron-jobs)))
+   (description
+    "Periodically discard unused blocks on filesystems.")
+   (default-value (fstrim-configuration))))
+
 ;;; admin.scm ends here
diff --git a/gnu/services/mcron.scm b/gnu/services/mcron.scm
index 23760ebda4..833d979ab4 100644
--- a/gnu/services/mcron.scm
+++ b/gnu/services/mcron.scm
@@ -154,6 +154,12 @@ (define mcron-shepherd-services
               (actions
                (list (shepherd-schedule-action mcron files)))))))))
 
+(define %mcron-activation
+  (with-imported-modules '((guix build utils))
+    #~(begin
+        (use-modules (guix build utils))
+        (mkdir-p "/var/lib/mcron"))))
+
 (define mcron-service-type
   (service-type (name 'mcron)
                 (description
@@ -161,6 +167,8 @@ (define mcron-service-type
                 (extensions
                  (list (service-extension shepherd-root-service-type
                                           mcron-shepherd-services)
+                       (service-extension activation-service-type
+                                          (const %mcron-activation))
                        (service-extension profile-service-type
                                           (compose list
                                                    mcron-configuration-mcron))))
-- 
2.37.3





^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [bug#58086] [PATCH] gnu: Add fstrim-service-type.
  2022-09-26  7:28 [bug#58086] [PATCH] gnu: Add fstrim-service-type iyzsong--- via Guix-patches via
@ 2022-10-06 21:00 ` Ludovic Courtès
  2023-03-22 12:03 ` Bruno Victal
  1 sibling, 0 replies; 4+ messages in thread
From: Ludovic Courtès @ 2022-10-06 21:00 UTC (permalink / raw)
  To: iyzsong; +Cc: 宋文武, 58086

Hi,

iyzsong@envs.net skribis:

> From: 宋文武 <iyzsong@member.fsf.org>
>
> A timestamp file "/var/lib/mcron/fstrim.stamp" is used to ensure we will
> catch up on missed job runs when the system was powered down.
>
> * gnu/services/mcron.scm (%mcron-activation): New extension to create
> '/var/lib/mcron'.
> * gnu/services/admin.scm (fstrim-configuration): New record type.
> (fstrim-mcron-jobs): New procedure.
> (fstrim-service-type): New service type.

Please add documentation in doc/guix.texi.  :-)


[...]

> +(define fstrim-mcron-jobs
> +  (match-lambda
> +    (($ <fstrim-configuration> command interval)
> +     (list
> +      #~(job
> +         (let ((last-time
> +                (catch #t
> +                  (lambda ()
> +                    (with-input-from-file #$fstrim-mcron-stamp read))
> +                  ;; We schedule a first run immediately.
> +                  (const 0))))
> +           (lambda (current-time)
> +             (let ((next-time (max current-time (+ #$interval last-time))))
> +               (set! last-time next-time)
> +               next-time)))
> +         (lambda ()
> +           (apply system* '#$command)
> +           (with-output-to-file #$fstrim-mcron-stamp
> +             (lambda () (write (current-time))))))))))

That seems a little bit complicated, no?  That’s because you want to
make sure it runs immediately at boot if it never ran before, right?  Is
that important?

> +(define fstrim-service-type
> +  (service-type
> +   (name 'fstrim)
> +   (extensions
> +    (list (service-extension mcron-service-type
> +                             fstrim-mcron-jobs)))
> +   (description
> +    "Periodically discard unused blocks on filesystems.")

“file systems”, two words.  Perhaps add a few more words mentioning the
fstrim package?

> +++ b/gnu/services/mcron.scm
> @@ -154,6 +154,12 @@ (define mcron-shepherd-services
>                (actions
>                 (list (shepherd-schedule-action mcron files)))))))))
>  
> +(define %mcron-activation
> +  (with-imported-modules '((guix build utils))
> +    #~(begin
> +        (use-modules (guix build utils))
> +        (mkdir-p "/var/lib/mcron"))))

I’m not sure the fstrim timestamp should leave in a directory that looks
as if it was “owned” by mcron.

Could you send an updated patch?

Thanks,
Ludo’.




^ permalink raw reply	[flat|nested] 4+ messages in thread

* [bug#58086] [PATCH] gnu: Add fstrim-service-type.
  2022-09-26  7:28 [bug#58086] [PATCH] gnu: Add fstrim-service-type iyzsong--- via Guix-patches via
  2022-10-06 21:00 ` Ludovic Courtès
@ 2023-03-22 12:03 ` Bruno Victal
  2023-03-24 10:28   ` 宋文武 via Guix-patches via
  1 sibling, 1 reply; 4+ messages in thread
From: Bruno Victal @ 2023-03-22 12:03 UTC (permalink / raw)
  To: iyzsong; +Cc: Ludovic Courtès, 58086, Maxim Cournoyer

Hi 宋文武,

I didn't notice this patch and ended up reimplementing another fstrim-service-type at [1].
Where we differ in the implementations:
* I didn't attempt to implement anacron capabilities, since I made the scheduling configurable.
* Uses define-configuration which can embed documentation and generate it for the manual.
* Slightly more “guix-y” style of configuration.


[1]: <https://issues.guix.gnu.org/61964>


Apologies for the duplicated effort!


Cheers,
Bruno




^ permalink raw reply	[flat|nested] 4+ messages in thread

* [bug#58086] [PATCH] gnu: Add fstrim-service-type.
  2023-03-22 12:03 ` Bruno Victal
@ 2023-03-24 10:28   ` 宋文武 via Guix-patches via
  0 siblings, 0 replies; 4+ messages in thread
From: 宋文武 via Guix-patches via @ 2023-03-24 10:28 UTC (permalink / raw)
  To: Bruno Victal; +Cc: Ludovic Courtès, 58086, Maxim Cournoyer

Bruno Victal <mirai@makinata.eu> writes:

> I didn't notice this patch and ended up reimplementing another fstrim-service-type at [1].
> Where we differ in the implementations:
> * I didn't attempt to implement anacron capabilities, since I made the scheduling configurable.
> * Uses define-configuration which can embed documentation and generate it for the manual.
> * Slightly more “guix-y” style of configuration.

Yes, that's indeed better, thank you!

I had forgot my patch...




^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2023-03-24 13:17 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-09-26  7:28 [bug#58086] [PATCH] gnu: Add fstrim-service-type iyzsong--- via Guix-patches via
2022-10-06 21:00 ` Ludovic Courtès
2023-03-22 12:03 ` Bruno Victal
2023-03-24 10:28   ` 宋文武 via Guix-patches via

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/guix.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.