From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp0 ([2001:41d0:2:4a6f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms11 with LMTPS id gKgKLLWFGF8KLwAA0tVLHw (envelope-from ) for ; Wed, 22 Jul 2020 18:30:13 +0000 Received: from aspmx1.migadu.com ([2001:41d0:2:4a6f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp0 with LMTPS id 2MDkJ7WFGF9hWwAA1q6Kng (envelope-from ) for ; Wed, 22 Jul 2020 18:30:13 +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 F197B940609 for ; Wed, 22 Jul 2020 18:30:12 +0000 (UTC) Received: from localhost ([::1]:39810 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jyJDs-0007Zb-G8 for larch@yhetil.org; Wed, 22 Jul 2020 14:12:08 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:34510) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jyJDm-0007ZF-F6 for guix-patches@gnu.org; Wed, 22 Jul 2020 14:12:02 -0400 Received: from debbugs.gnu.org ([209.51.188.43]:59673) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1jyJDm-0004Il-6D for guix-patches@gnu.org; Wed, 22 Jul 2020 14:12:02 -0400 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1jyJDm-0001Gj-1l for guix-patches@gnu.org; Wed, 22 Jul 2020 14:12:02 -0400 X-Loop: help-debbugs@gnu.org Subject: [bug#42478] [PATCH] services: Add zram-device-service. Resent-From: Efraim Flashner Original-Sender: "Debbugs-submit" Resent-CC: guix-patches@gnu.org Resent-Date: Wed, 22 Jul 2020 18:12:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: report 42478 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch To: 42478@debbugs.gnu.org Cc: Efraim Flashner X-Debbugs-Original-To: guix-patches@gnu.org Received: via spool by submit@debbugs.gnu.org id=B.15954414684808 (code B ref -1); Wed, 22 Jul 2020 18:12:01 +0000 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 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-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-Spam-Score: -2.3 (--) 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: 4.79 X-TUID: QpnsarsUJTp+ * 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