From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp1 ([2001:41d0:2:4a6f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms11 with LMTPS id IN9PFQDDHl8rPgAA0tVLHw (envelope-from ) for ; Mon, 27 Jul 2020 12:05:20 +0000 Received: from aspmx1.migadu.com ([2001:41d0:2:4a6f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp1 with LMTPS id yJEcEQDDHl9wIAAAbx9fmQ (envelope-from ) for ; Mon, 27 Jul 2020 12:05:20 +0000 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 20DF89403E7 for ; Mon, 27 Jul 2020 12:05:19 +0000 (UTC) Received: from localhost ([::1]:60954 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1k01sc-0002JY-4C for larch@yhetil.org; Mon, 27 Jul 2020 08:05:18 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:33082) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1k01sM-0002JA-8g for guix-patches@gnu.org; Mon, 27 Jul 2020 08:05:02 -0400 Received: from debbugs.gnu.org ([209.51.188.43]:42713) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1k01sL-000473-WA for guix-patches@gnu.org; Mon, 27 Jul 2020 08:05:02 -0400 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1k01sL-0008JY-Qu for guix-patches@gnu.org; Mon, 27 Jul 2020 08:05:01 -0400 X-Loop: help-debbugs@gnu.org Subject: [bug#42478] [PATCH v2] services: Add zram-device-service. Resent-From: Efraim Flashner Original-Sender: "Debbugs-submit" Resent-CC: guix-patches@gnu.org Resent-Date: Mon, 27 Jul 2020 12:05:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 42478 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch To: Marius Bakke Cc: 42478@debbugs.gnu.org Received: via spool by 42478-submit@debbugs.gnu.org id=B42478.159585146831916 (code B ref 42478); Mon, 27 Jul 2020 12:05:01 +0000 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 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-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-Spam-Score: -1.0 (-) 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" X-Scanner: scn0 Authentication-Results: aspmx1.migadu.com; dkim=none; dmarc=none; spf=pass (aspmx1.migadu.com: domain of guix-patches-bounces@gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=guix-patches-bounces@gnu.org X-Spam-Score: -2.61 X-TUID: 8FBnLJozXOZv --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--