all messages for Guix-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* [bug#42478] [PATCH] services: Add zram-device-service.
@ 2020-07-22 18:10 Efraim Flashner
  2020-07-25 17:00 ` Marius Bakke
  0 siblings, 1 reply; 5+ messages in thread
From: Efraim Flashner @ 2020-07-22 18:10 UTC (permalink / raw)
  To: 42478; +Cc: Efraim Flashner

* gnu/services/linux.scm (<zram-device-configuration>): New record.
(zram-device-service-type): New variable.
* doc/guix.texi (Linux Services): Document it.
* tests/services/linux.scm (zram-device-configuration->udev-string): New
test.
---
 doc/guix.texi            | 45 +++++++++++++++++++++++++
 gnu/services/linux.scm   | 73 +++++++++++++++++++++++++++++++++++++++-
 tests/services/linux.scm | 29 ++++++++++++++++
 3 files changed, 146 insertions(+), 1 deletion(-)

diff --git a/doc/guix.texi b/doc/guix.texi
index 8696a9b554..f656c31fab 100644
--- a/doc/guix.texi
+++ b/doc/guix.texi
@@ -27127,6 +27127,51 @@ parameters, can be done as follow:
 @end lisp
 @end deffn
 
+@cindex zram
+@cindex compressed swap
+@cindex Compressed RAM-based block devices
+@subsubheading Zram Device Service
+
+The Zram device service provides a compressed swap device in system
+memory.  The Linux Kernel documentation has more information about
+@uref{https://www.kernel.org/doc/html/latest/admin-guide/blockdev/zram.html,zram}
+devices.
+
+@deffn {Scheme Variable} zram-device-service-type
+This service creates the zram block device, formats it as swap and
+enables it as a swap device.  The service's value is a
+@code{zram-device-configuration} record.
+
+@deftp {Data Type} zram-device-configuration
+This is the data type representing the configuration for the zram-device
+service.
+
+@table @asis
+@item @code{disksize} (default @var{"0"})
+This is the amount of space you wish to provide for the zram device.  It
+accepts a string and can be a number of bytes or use a suffix, eg.:
+@var{2G}.
+@item @code{comp_algorithm} (default @var{"lzo"})
+This is the compression algorithm you wish to use.  It is difficult to
+list all the possible compression options, but common ones supported by
+Guix's Linux Libre Kernel include @var{lzo}, @var{lz4} and @var{zstd}.
+@item @code{mem_limit} (default @var{"0"})
+This is the maximum amount of memory which the zram device can use.
+Setting it to '0' disables the limit.  While it is generally expected
+that compression will be 2:1, it is possible that uncompressable data
+can be written to swap and this is a method to limit how much memory can
+be used.  It accepts a string and can be a number of bytes or use a
+suffix, eg.: @var{2G}.
+@item @code{priority} (default @var{"-1"})
+This is the priority of the swap device created from the zram device.
+@code{swapon} accepts values between -1 and 32767, with higher values
+indicating higher priority.  Higher priority swap will generally be used
+first.
+@end table
+
+@end deftp
+@end deffn
+
 @node Hurd Services
 @subsection Hurd Services
 
diff --git a/gnu/services/linux.scm b/gnu/services/linux.scm
index 12934c2084..7073a06cd7 100644
--- a/gnu/services/linux.scm
+++ b/gnu/services/linux.scm
@@ -1,6 +1,7 @@
 ;;; GNU Guix --- Functional package management for GNU
 ;;; Copyright © 2020 Maxim Cournoyer <maxim.cournoyer@gmail.com>
 ;;; Copyright © 2020 Brice Waegeneire <brice@waegenei.re>
+;;; Copyright © 2020 Efraim Flashner <efraim@flashner.co.il>
 ;;;
 ;;; This file is part of GNU Guix.
 ;;;
@@ -22,6 +23,7 @@
   #:use-module (guix records)
   #:use-module (guix modules)
   #:use-module (gnu services)
+  #:use-module (gnu services base)
   #:use-module (gnu services shepherd)
   #:use-module (gnu packages linux)
   #:use-module (srfi srfi-1)
@@ -42,7 +44,15 @@
             earlyoom-configuration-send-notification-command
             earlyoom-service-type
 
-            kernel-module-loader-service-type))
+            kernel-module-loader-service-type
+
+            zram-device-configuration
+            zram-device-configuration?
+            zram-device-configuration-disksize
+            zram-device-configuration-comp_algorithm
+            zram-device-configuration-mem_limit
+            zram-device-configuration-priority
+            zram-device-service-type))
 
 \f
 ;;;
@@ -177,3 +187,64 @@ representation."
    (compose concatenate)
    (extend append)
    (default-value '())))
+
+\f
+;;;
+;;; Kernel module loader.
+;;;
+
+(define-record-type* <zram-device-configuration>
+  zram-device-configuration make-zram-device-configuration
+  zram-device-configuration?
+  (disksize         zram-device-configration-disksize
+                    (default "0"))      ; string
+  (comp_algorithm   zram-device-configuration-comp_algorithm
+                    (default "lzo"))    ; string
+  (mem_limit        zram-device-configuration-mem_limit
+                    (default "0"))      ; string
+  (priority         zram-device-configuration-priority
+                    (default "-1")))    ; string
+
+(define (zram-device-configuration->udev-string config)
+  "Translate a <zram-device-configuration> into a string which can be
+placed in a udev rules file."
+  (match config
+    (($ <zram-device-configuration> disksize comp_algorithm mem_limit priority)
+     (string-append
+       "KERNEL==\"zram0\", "
+       "ATTR{comp_algorithm}=\"" comp_algorithm "\" "
+       (if (not (equal? "0" disksize))
+         (string-append "ATTR{disksize}=\"" disksize "\" ")
+         "")
+       (if (not (equal? "0" mem_limit))
+         (string-append "ATTR{mem_limit}=\"" mem_limit "\" ")
+         "")
+       "RUN+=\"/run/current-system/profile/sbin/mkswap /dev/zram0\" "
+       "RUN+=\"/run/current-system/profile/sbin/swapon "
+       (if (not (equal? "-1" priority))
+         (string-append "--priority " priority " ")
+         "")
+       "/dev/zram0\"\n"))))
+
+(define %zram-device-config
+  `("modprobe.d/zram.conf"
+    ,(plain-file "zram.conf"
+                 "options zram num_devices=1")))
+
+(define (zram-device-udev-rule config)
+  (file->udev-rule "99-zram.rules"
+                   (plain-file "99-zram.rules"
+                               (zram-device-configuration->udev-string config))))
+
+(define zram-device-service-type
+  (service-type
+    (name 'zram)
+    (default-value (zram-device-configuration))
+    (extensions
+      (list (service-extension kernel-module-loader-service-type
+                               (const (list "zram")))
+            (service-extension etc-service-type
+                               (const (list %zram-device-config)))
+            (service-extension udev-service-type
+                               (compose list zram-device-udev-rule))))
+    (description "Creates a zram swap device.")))
diff --git a/tests/services/linux.scm b/tests/services/linux.scm
index 8ad119c49f..828aa86bd6 100644
--- a/tests/services/linux.scm
+++ b/tests/services/linux.scm
@@ -1,5 +1,6 @@
 ;;; GNU Guix --- Functional package management for GNU
 ;;; Copyright © 2020 Maxim Cournoyer <maxim.cournoyer@gmail.com>
+;;; Copyright © 2020 Efraim Flashner <efraim@flashner.co.il>
 ;;;
 ;;; This file is part of GNU Guix.
 ;;;
@@ -54,4 +55,32 @@
         "-N" "python \"/some/path/notify-all-users.py\"")
   (earlyoom-configuration->command-line-args %earlyoom-configuration-sample))
 
+\f
+;;;
+;;; Zram swap device.
+;;;
+
+(define zram-device-configuration->udev-string
+  (@@ (gnu services linux) zram-device-configuration->udev-string))
+
+(define %zram-swap-device-test-1
+  (zram-device-configuration
+    (disksize "2G")
+    (comp_algorithm "zstd")
+    (mem_limit "1G")
+    (priority "42")))
+
+(test-equal "zram-device-configuration->udev-string"
+  "KERNEL==\"zram0\", ATTR{comp_algorithm}=\"zstd\" ATTR{disksize}=\"2G\" ATTR{mem_limit}=\"1G\" RUN+=\"/run/current-system/profile/sbin/mkswap /dev/zram0\" RUN+=\"/run/current-system/profile/sbin/swapon --priority 42 /dev/zram0\"\n"
+  (zram-device-configuration->udev-string %zram-swap-device-test-1))
+
+(define %zram-swap-device-test-2
+  (zram-device-configuration
+    (disksize "1G")
+    (comp_algorithm "lz4")))
+
+(test-equal "zram-device-configuration->udev-string"
+  "KERNEL==\"zram0\", ATTR{comp_algorithm}=\"lz4\" ATTR{disksize}=\"1G\" RUN+=\"/run/current-system/profile/sbin/mkswap /dev/zram0\" RUN+=\"/run/current-system/profile/sbin/swapon /dev/zram0\"\n"
+  (zram-device-configuration->udev-string %zram-swap-device-test-2))
+
 (test-end "linux-services")
-- 
2.27.0





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

* [bug#42478] [PATCH] services: Add zram-device-service.
  2020-07-22 18:10 [bug#42478] [PATCH] services: Add zram-device-service Efraim Flashner
@ 2020-07-25 17:00 ` Marius Bakke
  2020-07-27 12:01   ` Efraim Flashner
  2020-07-27 12:03   ` [bug#42478] [PATCH v2] " Efraim Flashner
  0 siblings, 2 replies; 5+ messages in thread
From: Marius Bakke @ 2020-07-25 17:00 UTC (permalink / raw)
  To: Efraim Flashner, 42478; +Cc: Efraim Flashner

[-- Attachment #1: Type: text/plain, Size: 3361 bytes --]

Efraim Flashner <efraim@flashner.co.il> writes:

> * gnu/services/linux.scm (<zram-device-configuration>): New record.
> (zram-device-service-type): New variable.
> * doc/guix.texi (Linux Services): Document it.
> * tests/services/linux.scm (zram-device-configuration->udev-string): New
> test.

[...]

> diff --git a/doc/guix.texi b/doc/guix.texi
> index 8696a9b554..f656c31fab 100644
> --- a/doc/guix.texi
> +++ b/doc/guix.texi
> @@ -27127,6 +27127,51 @@ parameters, can be done as follow:
>  @end lisp
>  @end deffn
>  
> +@cindex zram
> +@cindex compressed swap
> +@cindex Compressed RAM-based block devices
> +@subsubheading Zram Device Service
> +
> +The Zram device service provides a compressed swap device in system
> +memory.  The Linux Kernel documentation has more information about
> +@uref{https://www.kernel.org/doc/html/latest/admin-guide/blockdev/zram.html,zram}
> +devices.
> +
> +@deffn {Scheme Variable} zram-device-service-type
> +This service creates the zram block device, formats it as swap and
> +enables it as a swap device.  The service's value is a
> +@code{zram-device-configuration} record.
> +
> +@deftp {Data Type} zram-device-configuration
> +This is the data type representing the configuration for the zram-device
> +service.
> +
> +@table @asis
> +@item @code{disksize} (default @var{"0"})
> +This is the amount of space you wish to provide for the zram device.  It
> +accepts a string and can be a number of bytes or use a suffix, eg.:
> +@var{2G}.

Perhaps this could accept both an integer and a string?  What does a
size 0 device do, would it make sense to not have a default here?

Also, would it make sense to name it "size" instad of "disksize"?

> +@item @code{comp_algorithm} (default @var{"lzo"})
> +This is the compression algorithm you wish to use.  It is difficult to
> +list all the possible compression options, but common ones supported by
> +Guix's Linux Libre Kernel include @var{lzo}, @var{lz4} and @var{zstd}.

"comp_algorithm" is not very idiomatic :-)

Either "compression-algorithm" or just "compression" IMO.  I'd also
prefer a symbol instead of a string (or both!), but no strong opinion.

> +@item @code{mem_limit} (default @var{"0"})

"mem_limit" should instead be "memory-limit" or just "limit".

Accepting an integer here too would be nice!  :-)

> +This is the maximum amount of memory which the zram device can use.
> +Setting it to '0' disables the limit.  While it is generally expected
> +that compression will be 2:1, it is possible that uncompressable data
> +can be written to swap and this is a method to limit how much memory can
> +be used.  It accepts a string and can be a number of bytes or use a
> +suffix, eg.: @var{2G}.
> +@item @code{priority} (default @var{"-1"})

Just an integer I suppose?
  
> +\f
> +;;;
> +;;; Zram swap device.
> +;;;
> +
> +(define zram-device-configuration->udev-string
> +  (@@ (gnu services linux) zram-device-configuration->udev-string))

Would it make sense to export this, or is it strictly for internal use?

Great that you were able to add unit tests for the functionality.  I
think in this case we could also have a system test that checks that a
zram device was created?  But it can come later.

Other than the cosmetic/idiomatic issues looks great to me!

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 487 bytes --]

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

* [bug#42478] [PATCH] services: Add zram-device-service.
  2020-07-25 17:00 ` Marius Bakke
@ 2020-07-27 12:01   ` Efraim Flashner
  2020-07-27 12:03   ` [bug#42478] [PATCH v2] " Efraim Flashner
  1 sibling, 0 replies; 5+ messages in thread
From: Efraim Flashner @ 2020-07-27 12:01 UTC (permalink / raw)
  To: Marius Bakke; +Cc: 42478

[-- Attachment #1: Type: text/plain, Size: 4967 bytes --]

On Sat, Jul 25, 2020 at 07:00:54PM +0200, Marius Bakke wrote:
> Efraim Flashner <efraim@flashner.co.il> writes:
> 
> > * gnu/services/linux.scm (<zram-device-configuration>): New record.
> > (zram-device-service-type): New variable.
> > * doc/guix.texi (Linux Services): Document it.
> > * tests/services/linux.scm (zram-device-configuration->udev-string): New
> > test.
> 
> [...]
> 
> > diff --git a/doc/guix.texi b/doc/guix.texi
> > index 8696a9b554..f656c31fab 100644
> > --- a/doc/guix.texi
> > +++ b/doc/guix.texi
> > @@ -27127,6 +27127,51 @@ parameters, can be done as follow:
> >  @end lisp
> >  @end deffn
> >  
> > +@cindex zram
> > +@cindex compressed swap
> > +@cindex Compressed RAM-based block devices
> > +@subsubheading Zram Device Service
> > +
> > +The Zram device service provides a compressed swap device in system
> > +memory.  The Linux Kernel documentation has more information about
> > +@uref{https://www.kernel.org/doc/html/latest/admin-guide/blockdev/zram.html,zram}
> > +devices.
> > +
> > +@deffn {Scheme Variable} zram-device-service-type
> > +This service creates the zram block device, formats it as swap and
> > +enables it as a swap device.  The service's value is a
> > +@code{zram-device-configuration} record.
> > +
> > +@deftp {Data Type} zram-device-configuration
> > +This is the data type representing the configuration for the zram-device
> > +service.
> > +
> > +@table @asis
> > +@item @code{disksize} (default @var{"0"})
> > +This is the amount of space you wish to provide for the zram device.  It
> > +accepts a string and can be a number of bytes or use a suffix, eg.:
> > +@var{2G}.
> 
> Perhaps this could accept both an integer and a string?  What does a
> size 0 device do, would it make sense to not have a default here?

An integer or a string would certainly be more convenient for users, it
just needs a bit more logic to add in number->string as needed.

A size 0 device is pretty useless. It still creates the zram device but
with a size of 0. I put in 0 as the default because that's the default
if you don't choose anything when creating it. I suppose it would be
better to make the default 1G or 1024**3

> 
> Also, would it make sense to name it "size" instad of "disksize"?

That's the internal name but I'm not opposed to changing it to 'size'.

> 
> > +@item @code{comp_algorithm} (default @var{"lzo"})
> > +This is the compression algorithm you wish to use.  It is difficult to
> > +list all the possible compression options, but common ones supported by
> > +Guix's Linux Libre Kernel include @var{lzo}, @var{lz4} and @var{zstd}.
> 
> "comp_algorithm" is not very idiomatic :-)
> 
> Either "compression-algorithm" or just "compression" IMO.  I'd also
> prefer a symbol instead of a string (or both!), but no strong opinion.

compression-algorithm does seem better. I'll change it to a symbol.

> 
> > +@item @code{mem_limit} (default @var{"0"})
> 
> "mem_limit" should instead be "memory-limit" or just "limit".

I like memory-limit, limit would make me wonder what the difference is
between this an size.

> 
> Accepting an integer here too would be nice!  :-)

Noted :)

> 
> > +This is the maximum amount of memory which the zram device can use.
> > +Setting it to '0' disables the limit.  While it is generally expected
> > +that compression will be 2:1, it is possible that uncompressable data
> > +can be written to swap and this is a method to limit how much memory can
> > +be used.  It accepts a string and can be a number of bytes or use a
> > +suffix, eg.: @var{2G}.
> > +@item @code{priority} (default @var{"-1"})
> 
> Just an integer I suppose?

It would make more sense than a string.

>   
> > +
> > +;;;
> > +;;; Zram swap device.
> > +;;;
> > +
> > +(define zram-device-configuration->udev-string
> > +  (@@ (gnu services linux) zram-device-configuration->udev-string))
> 
> Would it make sense to export this, or is it strictly for internal use?

I'm not opposed to exporting it but I'm not really sure what else you'd
do with it. I think it would be nice to not hardcode zram0 so there can
be more than one or to make it so it can be mounted as /tmp but there's
nothing currently exposed as a variable to change how it works.

> 
> Great that you were able to add unit tests for the functionality.  I
> think in this case we could also have a system test that checks that a
> zram device was created?  But it can come later.

I think it'd be good to add a system test to make sure there's actually
42 MB of swap or something. I'll have to think about how to do that.

> 
> Other than the cosmetic/idiomatic issues looks great to me!

Thanks

-- 
Efraim Flashner   <efraim@flashner.co.il>   אפרים פלשנר
GPG key = A28B F40C 3E55 1372 662D  14F7 41AA E7DC CA3D 8351
Confidentiality cannot be guaranteed on emails sent or received unencrypted

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

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

* [bug#42478] [PATCH v2] services: Add zram-device-service.
  2020-07-25 17:00 ` Marius Bakke
  2020-07-27 12:01   ` Efraim Flashner
@ 2020-07-27 12:03   ` Efraim Flashner
  2020-08-02 12:56     ` bug#42478: " Efraim Flashner
  1 sibling, 1 reply; 5+ messages in thread
From: Efraim Flashner @ 2020-07-27 12:03 UTC (permalink / raw)
  To: Marius Bakke; +Cc: 42478


[-- Attachment #1.1: Type: text/plain, Size: 239 bytes --]

version 2 of the patch


-- 
Efraim Flashner   <efraim@flashner.co.il>   אפרים פלשנר
GPG key = A28B F40C 3E55 1372 662D  14F7 41AA E7DC CA3D 8351
Confidentiality cannot be guaranteed on emails sent or received unencrypted

[-- Attachment #1.2: v2-0001-services-Add-zram-device-service.patch --]
[-- Type: text/plain, Size: 9568 bytes --]

From a445e01371b90253f202539433c937608d887bec Mon Sep 17 00:00:00 2001
From: Efraim Flashner <efraim@flashner.co.il>
Date: Wed, 22 Jul 2020 21:07:31 +0300
Subject: [PATCH v2] services: Add zram-device-service.

* gnu/services/linux.scm (<zram-device-configuration>): New record.
(zram-device-service-type): New variable.
* doc/guix.texi (Linux Services): Document it.
* tests/services/linux.scm (zram-swap-device-test): New tests.
---
 doc/guix.texi            | 45 ++++++++++++++++++++++
 gnu/services/linux.scm   | 81 +++++++++++++++++++++++++++++++++++++++-
 tests/services/linux.scm | 37 ++++++++++++++++++
 3 files changed, 162 insertions(+), 1 deletion(-)

diff --git a/doc/guix.texi b/doc/guix.texi
index d4557b360a..37ececd72f 100644
--- a/doc/guix.texi
+++ b/doc/guix.texi
@@ -27259,6 +27259,51 @@ parameters, can be done as follow:
 @end lisp
 @end deffn
 
+@cindex zram
+@cindex compressed swap
+@cindex Compressed RAM-based block devices
+@subsubheading Zram Device Service
+
+The Zram device service provides a compressed swap device in system
+memory.  The Linux Kernel documentation has more information about
+@uref{https://www.kernel.org/doc/html/latest/admin-guide/blockdev/zram.html,zram}
+devices.
+
+@deffn {Scheme Variable} zram-device-service-type
+This service creates the zram block device, formats it as swap and
+enables it as a swap device.  The service's value is a
+@code{zram-device-configuration} record.
+
+@deftp {Data Type} zram-device-configuration
+This is the data type representing the configuration for the zram-device
+service.
+
+@table @asis
+@item @code{size} (default @var{"1G"})
+This is the amount of space you wish to provide for the zram device.  It
+accepts a string and can be a number of bytes or use a suffix, eg.:
+@var{"512M"} or @var{1024000}.
+@item @code{compression-algorithm} (default @var{'lzo})
+This is the compression algorithm you wish to use.  It is difficult to
+list all the possible compression options, but common ones supported by
+Guix's Linux Libre Kernel include @var{'lzo}, @var{'lz4} and @var{'zstd}.
+@item @code{memory-limit} (default @var{0})
+This is the maximum amount of memory which the zram device can use.
+Setting it to '0' disables the limit.  While it is generally expected
+that compression will be 2:1, it is possible that uncompressable data
+can be written to swap and this is a method to limit how much memory can
+be used.  It accepts a string and can be a number of bytes or use a
+suffix, eg.: @var{"2G"}.
+@item @code{priority} (default @var{-1})
+This is the priority of the swap device created from the zram device.
+@code{swapon} accepts values between -1 and 32767, with higher values
+indicating higher priority.  Higher priority swap will generally be used
+first.
+@end table
+
+@end deftp
+@end deffn
+
 @node Hurd Services
 @subsection Hurd Services
 
diff --git a/gnu/services/linux.scm b/gnu/services/linux.scm
index 12934c2084..ec42663a11 100644
--- a/gnu/services/linux.scm
+++ b/gnu/services/linux.scm
@@ -1,6 +1,7 @@
 ;;; GNU Guix --- Functional package management for GNU
 ;;; Copyright © 2020 Maxim Cournoyer <maxim.cournoyer@gmail.com>
 ;;; Copyright © 2020 Brice Waegeneire <brice@waegenei.re>
+;;; Copyright © 2020 Efraim Flashner <efraim@flashner.co.il>
 ;;;
 ;;; This file is part of GNU Guix.
 ;;;
@@ -22,6 +23,7 @@
   #:use-module (guix records)
   #:use-module (guix modules)
   #:use-module (gnu services)
+  #:use-module (gnu services base)
   #:use-module (gnu services shepherd)
   #:use-module (gnu packages linux)
   #:use-module (srfi srfi-1)
@@ -42,7 +44,15 @@
             earlyoom-configuration-send-notification-command
             earlyoom-service-type
 
-            kernel-module-loader-service-type))
+            kernel-module-loader-service-type
+
+            zram-device-configuration
+            zram-device-configuration?
+            zram-device-configuration-size
+            zram-device-configuration-compression-algorithm
+            zram-device-configuration-memory-limit
+            zram-device-configuration-priority
+            zram-device-service-type))
 
 \f
 ;;;
@@ -177,3 +187,72 @@ representation."
    (compose concatenate)
    (extend append)
    (default-value '())))
+
+\f
+;;;
+;;; Kernel module loader.
+;;;
+
+(define-record-type* <zram-device-configuration>
+  zram-device-configuration make-zram-device-configuration
+  zram-device-configuration?
+  (size                     zram-device-configration-size
+                            (default "1G"))     ; string or integer
+  (compression-algorithm    zram-device-configuration-compression-algorithm
+                            (default 'lzo))     ; symbol
+  (memory-limit             zram-device-configuration-memory-limit
+                            (default 0))        ; string or integer
+  (priority                 zram-device-configuration-priority
+                            (default -1)))      ; integer
+
+(define (zram-device-configuration->udev-string config)
+  "Translate a <zram-device-configuration> into a string which can be
+placed in a udev rules file."
+  (match config
+    (($ <zram-device-configuration> size compression-algorithm memory-limit priority)
+     (string-append
+       "KERNEL==\"zram0\", "
+       "ATTR{comp_algorithm}=\"" (symbol->string compression-algorithm) "\" "
+       (if (not (or (equal? "0" size)
+                    (equal? 0 size)))
+         (string-append "ATTR{disksize}=\"" (if (number? size)
+                                              (number->string size)
+                                              size)
+                        "\" ")
+         "")
+       (if (not (or (equal? "0" memory-limit)
+                    (equal? 0 memory-limit)))
+         (string-append "ATTR{mem_limit}=\"" (if (number? memory-limit)
+                                               (number->string memory-limit)
+                                               memory-limit)
+                        "\" ")
+         "")
+       "RUN+=\"/run/current-system/profile/sbin/mkswap /dev/zram0\" "
+       "RUN+=\"/run/current-system/profile/sbin/swapon "
+       (if (not (equal? -1 priority))
+         (string-append "--priority " (number->string priority) " ")
+         "")
+       "/dev/zram0\"\n"))))
+
+(define %zram-device-config
+  `("modprobe.d/zram.conf"
+    ,(plain-file "zram.conf"
+                 "options zram num_devices=1")))
+
+(define (zram-device-udev-rule config)
+  (file->udev-rule "99-zram.rules"
+                   (plain-file "99-zram.rules"
+                               (zram-device-configuration->udev-string config))))
+
+(define zram-device-service-type
+  (service-type
+    (name 'zram)
+    (default-value (zram-device-configuration))
+    (extensions
+      (list (service-extension kernel-module-loader-service-type
+                               (const (list "zram")))
+            (service-extension etc-service-type
+                               (const (list %zram-device-config)))
+            (service-extension udev-service-type
+                               (compose list zram-device-udev-rule))))
+    (description "Creates a zram swap device.")))
diff --git a/tests/services/linux.scm b/tests/services/linux.scm
index 8ad119c49f..e2cd191e48 100644
--- a/tests/services/linux.scm
+++ b/tests/services/linux.scm
@@ -1,5 +1,6 @@
 ;;; GNU Guix --- Functional package management for GNU
 ;;; Copyright © 2020 Maxim Cournoyer <maxim.cournoyer@gmail.com>
+;;; Copyright © 2020 Efraim Flashner <efraim@flashner.co.il>
 ;;;
 ;;; This file is part of GNU Guix.
 ;;;
@@ -54,4 +55,40 @@
         "-N" "python \"/some/path/notify-all-users.py\"")
   (earlyoom-configuration->command-line-args %earlyoom-configuration-sample))
 
+\f
+;;;
+;;; Zram swap device.
+;;;
+
+(define zram-device-configuration->udev-string
+  (@@ (gnu services linux) zram-device-configuration->udev-string))
+
+(define %zram-swap-device-test-1
+  (zram-device-configuration
+    (size "2G")
+    (compression-algorithm 'zstd)
+    (memory-limit "1G")
+    (priority 42)))
+
+(test-equal "zram-swap-device-test-1"
+  "KERNEL==\"zram0\", ATTR{comp_algorithm}=\"zstd\" ATTR{disksize}=\"2G\" ATTR{mem_limit}=\"1G\" RUN+=\"/run/current-system/profile/sbin/mkswap /dev/zram0\" RUN+=\"/run/current-system/profile/sbin/swapon --priority 42 /dev/zram0\"\n"
+  (zram-device-configuration->udev-string %zram-swap-device-test-1))
+
+(define %zram-swap-device-test-2
+  (zram-device-configuration
+    (size 1048576)  ; 1M
+    (compression-algorithm 'lz4)))
+
+(test-equal "zram-swap-device-test-2"
+  "KERNEL==\"zram0\", ATTR{comp_algorithm}=\"lz4\" ATTR{disksize}=\"1048576\" RUN+=\"/run/current-system/profile/sbin/mkswap /dev/zram0\" RUN+=\"/run/current-system/profile/sbin/swapon /dev/zram0\"\n"
+  (zram-device-configuration->udev-string %zram-swap-device-test-2))
+
+(define %zram-swap-device-test-3
+  (zram-device-configuration
+    (memory-limit (* 512 1000))))
+
+(test-equal "zram-swap-device-test-3"
+  "KERNEL==\"zram0\", ATTR{comp_algorithm}=\"lzo\" ATTR{disksize}=\"1G\" ATTR{mem_limit}=\"512000\" RUN+=\"/run/current-system/profile/sbin/mkswap /dev/zram0\" RUN+=\"/run/current-system/profile/sbin/swapon /dev/zram0\"\n"
+  (zram-device-configuration->udev-string %zram-swap-device-test-3))
+
 (test-end "linux-services")
-- 
2.27.0


[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

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

* bug#42478: [PATCH v2] services: Add zram-device-service.
  2020-07-27 12:03   ` [bug#42478] [PATCH v2] " Efraim Flashner
@ 2020-08-02 12:56     ` Efraim Flashner
  0 siblings, 0 replies; 5+ messages in thread
From: Efraim Flashner @ 2020-08-02 12:56 UTC (permalink / raw)
  To: 42478-done

[-- Attachment #1: Type: text/plain, Size: 230 bytes --]

Patch pushed!


-- 
Efraim Flashner   <efraim@flashner.co.il>   אפרים פלשנר
GPG key = A28B F40C 3E55 1372 662D  14F7 41AA E7DC CA3D 8351
Confidentiality cannot be guaranteed on emails sent or received unencrypted

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

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

end of thread, other threads:[~2020-08-02 12:58 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2020-07-22 18:10 [bug#42478] [PATCH] services: Add zram-device-service Efraim Flashner
2020-07-25 17:00 ` Marius Bakke
2020-07-27 12:01   ` Efraim Flashner
2020-07-27 12:03   ` [bug#42478] [PATCH v2] " Efraim Flashner
2020-08-02 12:56     ` bug#42478: " Efraim Flashner

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.