From unknown Wed Jun 18 00:24:43 2025 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 X-Mailer: MIME-tools 5.509 (Entity 5.509) Content-Type: text/plain; charset=utf-8 From: bug#42478 <42478@debbugs.gnu.org> To: bug#42478 <42478@debbugs.gnu.org> Subject: Status: [PATCH] services: Add zram-device-service. Reply-To: bug#42478 <42478@debbugs.gnu.org> Date: Wed, 18 Jun 2025 07:24:43 +0000 retitle 42478 [PATCH] services: Add zram-device-service. reassign 42478 guix-patches submitter 42478 Efraim Flashner severity 42478 normal tag 42478 patch thanks From debbugs-submit-bounces@debbugs.gnu.org Wed Jul 22 14:11:08 2020 Received: (at submit) by debbugs.gnu.org; 22 Jul 2020 18:11:08 +0000 Received: from localhost ([127.0.0.1]:42986 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1jyJCt-0001FU-KL for submit@debbugs.gnu.org; Wed, 22 Jul 2020 14:11:08 -0400 Received: from lists.gnu.org ([209.51.188.17]:55256) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1jyJCq-0001FK-WC for submit@debbugs.gnu.org; Wed, 22 Jul 2020 14:11:05 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:34310) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jyJCq-0007Pj-Qz for guix-patches@gnu.org; Wed, 22 Jul 2020 14:11:04 -0400 Received: from flashner.co.il ([178.62.234.194]:51314) by eggs.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jyJCm-0003yi-Sk for guix-patches@gnu.org; Wed, 22 Jul 2020 14:11:04 -0400 Received: from localhost (unknown [141.226.9.208]) by flashner.co.il (Postfix) with ESMTPSA id 57D6640041; Wed, 22 Jul 2020 18:10:58 +0000 (UTC) From: Efraim Flashner To: guix-patches@gnu.org Subject: [PATCH] services: Add zram-device-service. Date: Wed, 22 Jul 2020 21:10:25 +0300 Message-Id: <20200722181025.29413-1-efraim@flashner.co.il> X-Mailer: git-send-email 2.27.0 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Received-SPF: pass client-ip=178.62.234.194; envelope-from=efraim@flashner.co.il; helo=flashner.co.il X-detected-operating-system: by eggs.gnu.org: First seen = 2020/07/22 14:09:56 X-ACL-Warn: Detected OS = ??? 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_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-Spam-Score: -1.3 (-) X-Debbugs-Envelope-To: submit Cc: Efraim Flashner X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: -2.3 (--) * gnu/services/linux.scm (): 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 ;;; Copyright © 2020 Brice Waegeneire +;;; Copyright © 2020 Efraim Flashner ;;; ;;; 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)) ;;; @@ -177,3 +187,64 @@ representation." (compose concatenate) (extend append) (default-value '()))) + + +;;; +;;; Kernel module loader. +;;; + +(define-record-type* + 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 into a string which can be +placed in a udev rules file." + (match config + (($ 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 +;;; Copyright © 2020 Efraim Flashner ;;; ;;; 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)) + +;;; +;;; 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 From debbugs-submit-bounces@debbugs.gnu.org Sat Jul 25 13:01:20 2020 Received: (at 42478) by debbugs.gnu.org; 25 Jul 2020 17:01:20 +0000 Received: from localhost ([127.0.0.1]:51035 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1jzNY0-0006AU-1I for submit@debbugs.gnu.org; Sat, 25 Jul 2020 13:01:20 -0400 Received: from eggs.gnu.org ([209.51.188.92]:47258) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1jzNXy-0006AG-TB for 42478@debbugs.gnu.org; Sat, 25 Jul 2020 13:01:19 -0400 Received: from fencepost.gnu.org ([2001:470:142:3::e]:47232) by eggs.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jzNXt-0002ad-AP; Sat, 25 Jul 2020 13:01:13 -0400 Received: from 84-52-226.102.3p.ntebredband.no ([84.52.226.102]:45158 helo=localhost) by fencepost.gnu.org with esmtpsa (TLS1.2:RSA_AES_256_CBC_SHA1:256) (Exim 4.82) (envelope-from ) id 1jzNXs-0003ti-OW; Sat, 25 Jul 2020 13:01:13 -0400 From: Marius Bakke To: Efraim Flashner , 42478@debbugs.gnu.org Subject: Re: [bug#42478] [PATCH] services: Add zram-device-service. In-Reply-To: <20200722181025.29413-1-efraim@flashner.co.il> References: <20200722181025.29413-1-efraim@flashner.co.il> Date: Sat, 25 Jul 2020 19:00:54 +0200 Message-ID: <87eeozsf5l.fsf@gnu.org> MIME-Version: 1.0 Content-Type: multipart/signed; boundary="=-=-="; micalg=pgp-sha512; protocol="application/pgp-signature" X-Spam-Score: -2.3 (--) X-Debbugs-Envelope-To: 42478 Cc: Efraim Flashner X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: -3.3 (---) --=-=-= Content-Type: text/plain Content-Transfer-Encoding: quoted-printable Efraim Flashner writes: > * gnu/services/linux.scm (): 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 >=20=20 > +@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.h= tml,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? =20=20 > + > +;;; > +;;; 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! --=-=-= Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQEzBAEBCgAdFiEEu7At3yzq9qgNHeZDoqBt8qM6VPoFAl8cZUYACgkQoqBt8qM6 VPq1kAf7B3s/qVh2ivlOG5eL9hkJ10lfsWEqoFokkk56o4YhcCNHEVcGhYF3ZQyH hMvdj9aBmrLht1gUrpPBqhkRL1ccp0Nz3Af/gFh7UVra51IWfNp+WWnKHOQvDAB+ ooNlZi0b1QJykEpRpAJ5PEY2/9mLIyl9q8PvKvfOehC67aclhGn0ZnBRlFzCsjUB /EBMYUP7vDcb1gze2hWHSTa3R7CmrXH1Q8pVIdCi3U3HE6TSKJfCnM8qLuVdWL6S C4eE5Uj7dv2YQvjTvCxlciJOz0MGqO5ki87GqkUtUiIGm2k+0yF5fVdj0ZsCVa/q zdsUkUj8KFvfpyX2dTf89BFzBjMFxw== =mv2B -----END PGP SIGNATURE----- --=-=-=-- From debbugs-submit-bounces@debbugs.gnu.org Mon Jul 27 08:02:10 2020 Received: (at 42478) by debbugs.gnu.org; 27 Jul 2020 12:02:10 +0000 Received: from localhost ([127.0.0.1]:54255 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1k01pZ-0008FN-Ki for submit@debbugs.gnu.org; Mon, 27 Jul 2020 08:02:09 -0400 Received: from flashner.co.il ([178.62.234.194]:39360) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1k01pX-0008F3-Rr for 42478@debbugs.gnu.org; Mon, 27 Jul 2020 08:02:08 -0400 Received: from localhost (unknown [141.226.9.208]) by flashner.co.il (Postfix) with ESMTPSA id A8CB3401A4; Mon, 27 Jul 2020 12:02:01 +0000 (UTC) Date: Mon, 27 Jul 2020 15:01:29 +0300 From: Efraim Flashner To: Marius Bakke Subject: Re: [bug#42478] [PATCH] services: Add zram-device-service. Message-ID: <20200727120129.GE9269@E5400> References: <20200722181025.29413-1-efraim@flashner.co.il> <87eeozsf5l.fsf@gnu.org> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="J4XPiPrVK1ev6Sgr" Content-Disposition: inline In-Reply-To: <87eeozsf5l.fsf@gnu.org> X-PGP-Key-ID: 0x41AAE7DCCA3D8351 X-PGP-Key: https://flashner.co.il/~efraim/efraim_flashner.asc X-PGP-Fingerprint: A28B F40C 3E55 1372 662D 14F7 41AA E7DC CA3D 8351 X-Spam-Score: -0.0 (/) X-Debbugs-Envelope-To: 42478 Cc: 42478@debbugs.gnu.org X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: -1.0 (-) --J4XPiPrVK1ev6Sgr Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Sat, Jul 25, 2020 at 07:00:54PM +0200, Marius Bakke wrote: > Efraim Flashner writes: >=20 > > * gnu/services/linux.scm (): 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. >=20 > [...] >=20 > > 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 > > =20 > > +@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= =2Ehtml,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-devi= ce > > +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}. >=20 > 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 >=20 > 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'. >=20 > > +@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}. >=20 > "comp_algorithm" is not very idiomatic :-) >=20 > 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. >=20 > > +@item @code{mem_limit} (default @var{"0"}) >=20 > "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. >=20 > Accepting an integer here too would be nice! :-) Noted :) >=20 > > +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 c= an > > +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"}) >=20 > Just an integer I suppose? It would make more sense than a string. > =20 > > + > > +;;; > > +;;; Zram swap device. > > +;;; > > + > > +(define zram-device-configuration->udev-string > > + (@@ (gnu services linux) zram-device-configuration->udev-string)) >=20 > 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. >=20 > 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. >=20 > Other than the cosmetic/idiomatic issues looks great to me! Thanks --=20 Efraim Flashner =D7=90=D7=A4=D7=A8=D7=99=D7=9D = =D7=A4=D7=9C=D7=A9=D7=A0=D7=A8 GPG key =3D A28B F40C 3E55 1372 662D 14F7 41AA E7DC CA3D 8351 Confidentiality cannot be guaranteed on emails sent or received unencrypted --J4XPiPrVK1ev6Sgr Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAABCgAdFiEEoov0DD5VE3JmLRT3Qarn3Mo9g1EFAl8ewhYACgkQQarn3Mo9 g1GfwQ//USTQmnfU0NBAedRrlQXoC5nzap6iGwTEu2vDofkXVJyfKU8HhBktjVj/ cW2ap9dCzcdvCR+s1qyYrwmYBkSmD+iZg0xY9toQBYgENHZLqcx4DjvPz6aIHv4O wTEoiqTXcOmtqfSvFfvshJb913FU84JJ3w2SlD0lYLNWQ9cB+fkbpGpRw03P9FNz a/J2xFXLF29U+VUkfJtOzsp6jqJArJZ6a1fK7wM7j8Fy19JkG/Z8SPmvzB8y4b+p MqMkOWG4ZGJ4OmJ0mFY1IEkdzZogpks4WlqgQpuzZSWzfmbWprukfkicMzYW2DHy xuwRaiEc6gfwVx5e2BTYerg3tP56doSn93SNmx5b24lR8TYCfrFN4YO/VPmWt+XL htK1eZODuQVICGwN5qw+dmocfo4N+OZ3GxpjNiTfLep8mMyBBxDKgyyfd3pljwSV /d1zk4heDU7aSA/U4K5kmMbhdzF4tsRro/8ajEZe66Y9SNJbf83EfikqdbMMx/qe joZRMc34xVnB9H+YcVnnkRFa8NNFxq4L9iOcdq3esjddzgngm4ognC46I6AU8pjk PFu7H4kN6hQoSKiWB2SjIL6KkScSKVwxaI742wjgXyhcr9QQKnsI7OLRGjfoLi10 V9t1zA1bhky2rRk6xLNu62LWQWiQKWRfCj7uuJ0eTNYgP+8QgVs= =PQLu -----END PGP SIGNATURE----- --J4XPiPrVK1ev6Sgr-- From debbugs-submit-bounces@debbugs.gnu.org Mon Jul 27 08:04:28 2020 Received: (at 42478) by debbugs.gnu.org; 27 Jul 2020 12:04:28 +0000 Received: from localhost ([127.0.0.1]:54259 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1k01ro-0008Ii-6I for submit@debbugs.gnu.org; Mon, 27 Jul 2020 08:04:28 -0400 Received: from flashner.co.il ([178.62.234.194]:39372) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1k01rm-0008IU-2k for 42478@debbugs.gnu.org; Mon, 27 Jul 2020 08:04:26 -0400 Received: from localhost (unknown [141.226.9.208]) by flashner.co.il (Postfix) with ESMTPSA id 3424C40041; Mon, 27 Jul 2020 12:04:20 +0000 (UTC) Date: Mon, 27 Jul 2020 15:03:47 +0300 From: Efraim Flashner To: Marius Bakke Subject: Re: [bug#42478] [PATCH v2] services: Add zram-device-service. Message-ID: <20200727120347.GF9269@E5400> References: <20200722181025.29413-1-efraim@flashner.co.il> <87eeozsf5l.fsf@gnu.org> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="NY6JkbSqL3W9mApi" Content-Disposition: inline In-Reply-To: <87eeozsf5l.fsf@gnu.org> X-PGP-Key-ID: 0x41AAE7DCCA3D8351 X-PGP-Key: https://flashner.co.il/~efraim/efraim_flashner.asc X-PGP-Fingerprint: A28B F40C 3E55 1372 662D 14F7 41AA E7DC CA3D 8351 X-Spam-Score: -0.0 (/) X-Debbugs-Envelope-To: 42478 Cc: 42478@debbugs.gnu.org X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: -1.0 (-) --NY6JkbSqL3W9mApi Content-Type: multipart/mixed; boundary="twz1s1Hj1O0rHoT0" Content-Disposition: inline --twz1s1Hj1O0rHoT0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable version 2 of the patch --=20 Efraim Flashner =D7=90=D7=A4=D7=A8=D7=99=D7=9D = =D7=A4=D7=9C=D7=A9=D7=A0=D7=A8 GPG key =3D A28B F40C 3E55 1372 662D 14F7 41AA E7DC CA3D 8351 Confidentiality cannot be guaranteed on emails sent or received unencrypted --twz1s1Hj1O0rHoT0 Content-Type: text/plain; charset=utf-8 Content-Disposition: attachment; filename="v2-0001-services-Add-zram-device-service.patch" Content-Transfer-Encoding: quoted-printable =46rom a445e01371b90253f202539433c937608d887bec Mon Sep 17 00:00:00 2001 =46rom: Efraim Flashner Date: Wed, 22 Jul 2020 21:07:31 +0300 Subject: [PATCH v2] services: Add zram-device-service. * gnu/services/linux.scm (): 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 =20 +@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.htm= l,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 =20 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 =C2=A9 2020 Maxim Cournoyer ;;; Copyright =C2=A9 2020 Brice Waegeneire +;;; Copyright =C2=A9 2020 Efraim Flashner ;;; ;;; 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 =20 - 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)) =20 =0C ;;; @@ -177,3 +187,72 @@ representation." (compose concatenate) (extend append) (default-value '()))) + +=0C +;;; +;;; Kernel module loader. +;;; + +(define-record-type* + 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 into a string which can be +placed in a udev rules file." + (match config + (($ size compression-algorithm memory-limi= t priority) + (string-append + "KERNEL=3D=3D\"zram0\", " + "ATTR{comp_algorithm}=3D\"" (symbol->string compression-algorithm) = "\" " + (if (not (or (equal? "0" size) + (equal? 0 size))) + (string-append "ATTR{disksize}=3D\"" (if (number? size) + (number->string size) + size) + "\" ") + "") + (if (not (or (equal? "0" memory-limit) + (equal? 0 memory-limit))) + (string-append "ATTR{mem_limit}=3D\"" (if (number? memory-limit) + (number->string memory-limi= t) + memory-limit) + "\" ") + "") + "RUN+=3D\"/run/current-system/profile/sbin/mkswap /dev/zram0\" " + "RUN+=3D\"/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=3D1"))) + +(define (zram-device-udev-rule config) + (file->udev-rule "99-zram.rules" + (plain-file "99-zram.rules" + (zram-device-configuration->udev-string con= fig)))) + +(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 =C2=A9 2020 Maxim Cournoyer +;;; Copyright =C2=A9 2020 Efraim Flashner ;;; ;;; 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-sampl= e)) =20 +=0C +;;; +;;; 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=3D=3D\"zram0\", ATTR{comp_algorithm}=3D\"zstd\" ATTR{disksize}= =3D\"2G\" ATTR{mem_limit}=3D\"1G\" RUN+=3D\"/run/current-system/profile/sbi= n/mkswap /dev/zram0\" RUN+=3D\"/run/current-system/profile/sbin/swapon --pr= iority 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=3D=3D\"zram0\", ATTR{comp_algorithm}=3D\"lz4\" ATTR{disksize}=3D= \"1048576\" RUN+=3D\"/run/current-system/profile/sbin/mkswap /dev/zram0\" R= UN+=3D\"/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=3D=3D\"zram0\", ATTR{comp_algorithm}=3D\"lzo\" ATTR{disksize}=3D= \"1G\" ATTR{mem_limit}=3D\"512000\" RUN+=3D\"/run/current-system/profile/sb= in/mkswap /dev/zram0\" RUN+=3D\"/run/current-system/profile/sbin/swapon /de= v/zram0\"\n" + (zram-device-configuration->udev-string %zram-swap-device-test-3)) + (test-end "linux-services") --=20 2.27.0 --twz1s1Hj1O0rHoT0-- --NY6JkbSqL3W9mApi Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAABCgAdFiEEoov0DD5VE3JmLRT3Qarn3Mo9g1EFAl8ewqMACgkQQarn3Mo9 g1EJdA/+IaBA4iT/F4MDVEGQojv4MrAcKdY/lmSFloCSKp+EG0dAl0TSREwv4zcd kifbufPEh9yo56HEX6HNSxf7GwhZvMlHWQVZ6y3uS3a9XNnRu+x5LUMEQWSpIh/B Z2mM/03uP2iMJc2gSJMbyXN/ZuFuPJ4WQcziWVwFkibD+Q67QG9ovEm8PK3myGdd mlaRCujVRYXyCjQfUsrDKiopSYMdbNAvpkT9TrMO81soiMz9jQAZnf+2iNmxGR0o 1OG5OYRc4G9Tbn6hoU4D/DRNCvhg09NNCQvGMeuzpo0WhTrdLsIncfF2fQGblR1m AOwdEMRriYhESFwxGBkEsfKXwWbFyxGpE3VTB22tv1Z9YNvD8UeKTpYloUkjufd2 d621Q0u0dqUb+c3tMGYPas7/GBBBUjYU3tY8P0hwEqeLLqcX+MFasGM2eCHuZFeP tB8CViw5RSaO0d5+JX7T3KPFxrwM3TmuuZ/laf5+mHk711kQcxdsRpavCps3Lks2 lkHK9lpl7O/jBDRvzyjPrbvr7L0m/Ze1SNlpvFa9WKHAC/Ush90uJ/K9Cs6F2Gmc QJhwUM0+Bb3/qE6ttedZkiA0oHL5GMmlxW0KawGeK7GVbEgFhMnCKIlFp77xHI1j E7MZTm4GsXcAbTfz/DYU5D0Yu0MJmnTcVby7PfkVbHiBxw2HpMc= =ETkH -----END PGP SIGNATURE----- --NY6JkbSqL3W9mApi-- From debbugs-submit-bounces@debbugs.gnu.org Sun Aug 02 08:57:35 2020 Received: (at 42478-done) by debbugs.gnu.org; 2 Aug 2020 12:57:35 +0000 Received: from localhost ([127.0.0.1]:41926 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1k2DYV-00058e-2D for submit@debbugs.gnu.org; Sun, 02 Aug 2020 08:57:35 -0400 Received: from flashner.co.il ([178.62.234.194]:59462) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1k2DYS-00058R-TV for 42478-done@debbugs.gnu.org; Sun, 02 Aug 2020 08:57:34 -0400 Received: from localhost (unknown [31.210.177.175]) by flashner.co.il (Postfix) with ESMTPSA id B97314006B for <42478-done@debbugs.gnu.org>; Sun, 2 Aug 2020 12:57:26 +0000 (UTC) Date: Sun, 2 Aug 2020 15:56:54 +0300 From: Efraim Flashner To: 42478-done@debbugs.gnu.org Subject: Re: [bug#42478] [PATCH v2] services: Add zram-device-service. Message-ID: <20200802125654.GE1134@E5400> References: <20200722181025.29413-1-efraim@flashner.co.il> <87eeozsf5l.fsf@gnu.org> <20200727120347.GF9269@E5400> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="jKBxcB1XkHIR0Eqt" Content-Disposition: inline In-Reply-To: <20200727120347.GF9269@E5400> X-PGP-Key-ID: 0x41AAE7DCCA3D8351 X-PGP-Key: https://flashner.co.il/~efraim/efraim_flashner.asc X-PGP-Fingerprint: A28B F40C 3E55 1372 662D 14F7 41AA E7DC CA3D 8351 X-Spam-Score: -0.0 (/) X-Debbugs-Envelope-To: 42478-done X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: -1.0 (-) --jKBxcB1XkHIR0Eqt Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable Patch pushed! --=20 Efraim Flashner =D7=90=D7=A4=D7=A8=D7=99=D7=9D = =D7=A4=D7=9C=D7=A9=D7=A0=D7=A8 GPG key =3D A28B F40C 3E55 1372 662D 14F7 41AA E7DC CA3D 8351 Confidentiality cannot be guaranteed on emails sent or received unencrypted --jKBxcB1XkHIR0Eqt Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAABCgAdFiEEoov0DD5VE3JmLRT3Qarn3Mo9g1EFAl8muBYACgkQQarn3Mo9 g1Gc/g/+NGCgDa3Z5KAQar3uDw36X5SlLtWdDyvuE0NUcP3/IkoA7dByYAa8gdg6 D7G2IwxU2sahLBIwf6IasBFhPBDNxL5SCmV+Wm0ZKzncIRIfm3CLVZi+fj2D1N/D nwdJfwRDfEyVcQdf8LuITlt9d8jZgVMOOLnv7ERi2mhjNNtWF96Z32L9MDBrZmXT 8j1mC2nksp8dre8P/DPax5aTp5c6lTT7DMD0fFP0m/lb1vA22huczfc5AJXF572N +1m9jCftGiD4isgSghHR5ElMv7W8R8k/lmriFVjeCByc051CHIrmfi9v5URpOVRt 2vzfGzj5oUtNz3rBGaD6YGSa36/snNaciHfP6iemFY1bGfCPGxsDZ6jr4+60x9tb aLx4CHJ2BB+mCVejtrLD27m9TZN2Y6A4ai8Z9mVdQ4IAFM1ezoiOhh03ywwVOnz7 NVQTB3l58EOHIaX1LnbvOlUzHOAj15VM+8cXV8ya+62EaKQykSkT5QDn3lygMece PPJR5tYEAe65Wq0HrVidBHebymxk5R4tOq6BjIrM8ujqsKq97WzSbiFG0scOOX6v h6iMEbccMZEmO/3p4UeSlQ0odbmtL31Z+seox2vg6ARubleccEFXYXVeWyE2Cgiy DWgjouqB39bvUMP+qO8Y976xKQl8fSDbIG7IkZ4eisW1HczJ8Hg= =A99Y -----END PGP SIGNATURE----- --jKBxcB1XkHIR0Eqt-- From unknown Wed Jun 18 00:24:43 2025 Received: (at fakecontrol) by fakecontrolmessage; To: internal_control@debbugs.gnu.org From: Debbugs Internal Request Subject: Internal Control Message-Id: bug archived. Date: Mon, 31 Aug 2020 11:24:07 +0000 User-Agent: Fakemail v42.6.9 # This is a fake control message. # # The action: # bug archived. thanks # This fakemail brought to you by your local debbugs # administrator