From patchwork Wed Oct 27 15:09:10 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josselin Poiret X-Patchwork-Id: 34116 Return-Path: X-Original-To: patchwork@mira.cbaines.net Delivered-To: patchwork@mira.cbaines.net Received: by mira.cbaines.net (Postfix, from userid 113) id 0B1CD27BBE3; Wed, 27 Oct 2021 16:49:21 +0100 (BST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on mira.cbaines.net X-Spam-Level: X-Spam-Status: No, score=-2.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, MAILING_LIST_MULTI,RCVD_IN_MSPIKE_H2,SPF_HELO_PASS,T_DKIM_INVALID, URIBL_BLOCKED autolearn=unavailable autolearn_force=no version=3.4.2 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mira.cbaines.net (Postfix) with ESMTPS id 7D23927BBE1 for ; Wed, 27 Oct 2021 16:49:20 +0100 (BST) Received: from localhost ([::1]:39290 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1mflB1-0003wS-E8 for patchwork@mira.cbaines.net; Wed, 27 Oct 2021 11:49:19 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:57880) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mfkZ0-0004Pl-R9 for guix-patches@gnu.org; Wed, 27 Oct 2021 11:10:02 -0400 Received: from debbugs.gnu.org ([209.51.188.43]:38649) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1mfkZ0-000784-Hd for guix-patches@gnu.org; Wed, 27 Oct 2021 11:10:02 -0400 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1mfkZ0-0000PG-DG for guix-patches@gnu.org; Wed, 27 Oct 2021 11:10:02 -0400 X-Loop: help-debbugs@gnu.org Subject: [bug#51346] [PATCH v2 1/4] gnu: system: Rework swap space support, add dependencies. Resent-From: Josselin Poiret Original-Sender: "Debbugs-submit" Resent-CC: guix-patches@gnu.org Resent-Date: Wed, 27 Oct 2021 15:10:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 51346 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch To: Tobias Geerinckx-Rice Cc: Josselin Poiret , 51346@debbugs.gnu.org Received: via spool by 51346-submit@debbugs.gnu.org id=B51346.16353473831482 (code B ref 51346); Wed, 27 Oct 2021 15:10:02 +0000 Received: (at 51346) by debbugs.gnu.org; 27 Oct 2021 15:09:43 +0000 Received: from localhost ([127.0.0.1]:50182 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1mfkYg-0000Np-P4 for submit@debbugs.gnu.org; Wed, 27 Oct 2021 11:09:43 -0400 Received: from jpoiret.xyz ([206.189.101.64]:34654) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1mfkYf-0000Nh-8R for 51346@debbugs.gnu.org; Wed, 27 Oct 2021 11:09:41 -0400 Received: from authenticated-user (jpoiret.xyz [206.189.101.64]) by jpoiret.xyz (Postfix) with ESMTPA id 7DC55184BC9; Wed, 27 Oct 2021 15:09:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=jpoiret.xyz; s=dkim; t=1635347380; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=d+fmujT6setCL9MnWntBntviV5dlhnM8zxeOYNC/OX8=; b=Jk9S4qjpfFsYNAnpPhZAvCVFmA19E9vzeM/wCHpQ+J4atY9RMkFdcJX8x9286leNSS1m40 67mykt4xCKpmozuMor218Hm6Bar95S/RInNX5DGmq9F0eUARAhBNKTcGX822/berHCmdAg 4KA6kg3EE6g2PvR4PdmCNCfS5kj6TljTo5cPMIXM+w3E4rCAu5oLtIcS7c1VoO6LWG1V+A wgQzFOGNBdnnVS8CiGcTb6cmOY/cCbc4uv5JDE5Gw3KOzOTDhMBRkuJZfA/EgkQegqaHXc ScgJWOTcCUTeQh0UeGXelLiyxjLEwHtRxaxNG0XH18GF8+dWc2E7ecqQBfUamA== Date: Wed, 27 Oct 2021 15:09:10 +0000 Message-Id: <20211027150913.6038-2-dev@jpoiret.xyz> In-Reply-To: <20211027150913.6038-1-dev@jpoiret.xyz> References: <87tuh6ifwe.fsf@nckx> <20211027150913.6038-1-dev@jpoiret.xyz> MIME-Version: 1.0 X-Spamd-Bar: / Authentication-Results: jpoiret.xyz; auth=pass smtp.auth=jpoiret@jpoiret.xyz smtp.mailfrom=dev@jpoiret.xyz X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: guix-patches@gnu.org List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guix-patches-bounces+patchwork=mira.cbaines.net@gnu.org Sender: "Guix-patches" Reply-to: Josselin Poiret X-ACL-Warn: , Josselin Poiret via Guix-patches X-Patchwork-Original-From: Josselin Poiret via Guix-patches via From: Josselin Poiret X-getmail-retrieved-from-mailbox: Patches * gnu/system/file-systems.scm (swap-space): Add it. * gnu/system.scm (operating-system)[swap-devices]: Update comment. * gnu/services/base.scm (swap-space->shepherd-service-name, swap-deprecated->shepherd-service-name, swap->shepherd-service-name): Add them. * gnu/services/base.scm (swap-service-type, swap-service): Use the new records. --- gnu/services/base.scm | 102 +++++++++++++++++++++++++----------- gnu/system.scm | 2 +- gnu/system/file-systems.scm | 18 ++++++- 3 files changed, 88 insertions(+), 34 deletions(-) diff --git a/gnu/services/base.scm b/gnu/services/base.scm index 50865055fe..c816381198 100644 --- a/gnu/services/base.scm +++ b/gnu/services/base.scm @@ -63,6 +63,8 @@ (define-module (gnu services base) #:use-module (guix records) #:use-module (guix modules) #:use-module ((guix self) #:select (make-config.scm)) + #:use-module (guix diagnostics) + #:use-module (guix i18n) #:use-module (srfi srfi-1) #:use-module (srfi srfi-26) #:use-module (ice-9 match) @@ -2146,62 +2148,98 @@ (define* (udev-rules-service name rules #:key (groups '())) udev-service-type udev-extension)))))) (service type #f))) +(define (swap-space->shepherd-service-name space) + (let ((target (swap-space-target space))) + (symbol-append 'swap- + (string->symbol + (cond ((uuid? target) + (uuid->string target)) + ((file-system-label? target) + (file-system-label->string target)) + (else + target)))))) + +; TODO Remove after deprecation +(define (swap-deprecated->shepherd-service-name sdep) + (symbol-append 'swap- + (string->symbol + (cond ((uuid? sdep) + (string-take (uuid->string sdep) 6)) + ((file-system-label? sdep) + (file-system-label->string sdep)) + (else + sdep))))) + +(define swap->shepherd-service-name + (match-lambda ((? swap-space? space) + (swap-space->shepherd-service-name space)) + (sdep + (swap-deprecated->shepherd-service-name sdep)))) + (define swap-service-type (shepherd-service-type 'swap - (lambda (device) - (define requirement - (if (and (string? device) - (string-prefix? "/dev/mapper/" device)) - (list (symbol-append 'device-mapping- - (string->symbol (basename device)))) - '())) - - (define (device-lookup device) + (lambda (swap) + (define requirements + (cond ((swap-space? swap) + (map dependency->shepherd-service-name + (swap-space-dependencies swap))) + ; TODO Remove after deprecation + ((and (string? swap) (string-prefix? "/dev/mapper/" swap)) + (list (symbol-append 'device-mapping- + (string->symbol (basename swap))))) + (else + '()))) + + (define device-lookup ;; The generic 'find-partition' procedures could return a partition ;; that's not swap space, but that's unlikely. - (cond ((uuid? device) - #~(find-partition-by-uuid #$(uuid-bytevector device))) - ((file-system-label? device) + (cond ((swap-space? swap) + (let ((target (swap-space-target swap))) + (cond ((uuid? target) + #~(find-partition-by-uuid #$(uuid-bytevector target))) + ((file-system-label? target) + #~(find-partition-by-label + #$(file-system-label->string target))) + (else + target)))) + ; TODO Remove after deprecation + ((uuid? swap) + #~(find-partition-by-uuid #$(uuid-bytevector swap))) + ((file-system-label? swap) #~(find-partition-by-label - #$(file-system-label->string device))) + #$(file-system-label->string swap))) (else - device))) - - (define service-name - (symbol-append 'swap- - (string->symbol - (cond ((uuid? device) - (string-take (uuid->string device) 6)) - ((file-system-label? device) - (file-system-label->string device)) - (else - device))))) + swap))) (with-imported-modules (source-module-closure '((gnu build file-systems))) (shepherd-service - (provision (list service-name)) - (requirement `(udev ,@requirement)) - (documentation "Enable the given swap device.") + (provision (list (swap->shepherd-service-name swap))) + (requirement `(udev ,@requirements)) + (documentation "Enable the given swap space.") (modules `((gnu build file-systems) ,@%default-modules)) (start #~(lambda () - (let ((device #$(device-lookup device))) + (let ((device #$device-lookup)) (and device (begin (restart-on-EINTR (swapon device)) #t))))) (stop #~(lambda _ - (let ((device #$(device-lookup device))) + (let ((device #$device-lookup)) (when device (restart-on-EINTR (swapoff device))) #f))) (respawn? #f)))) (description "Turn on the virtual memory swap area."))) -(define (swap-service device) - "Return a service that uses @var{device} as a swap device." - (service swap-service-type device)) +(define (swap-service swap) + "Return a service that uses @var{swap} as a swap space." + (unless (swap-space? swap) + (warning (G_ "Specifying swap space without @code{swap-space} +is deprecated, see \"(guix) operating-system Reference\" for +more details.~%"))) + (service swap-service-type swap)) (define %default-gpm-options ;; Default options for GPM. diff --git a/gnu/system.scm b/gnu/system.scm index 58b594694a..2797c07e36 100644 --- a/gnu/system.scm +++ b/gnu/system.scm @@ -234,7 +234,7 @@ (define-record-type* operating-system (mapped-devices operating-system-mapped-devices ; list of (default '())) (file-systems operating-system-file-systems) ; list of fs - (swap-devices operating-system-swap-devices ; list of strings + (swap-devices operating-system-swap-devices ; list of string | (default '())) (users operating-system-users ; list of user accounts diff --git a/gnu/system/file-systems.scm b/gnu/system/file-systems.scm index e69cfd06e6..7aa19069a1 100644 --- a/gnu/system/file-systems.scm +++ b/gnu/system/file-systems.scm @@ -96,7 +96,12 @@ (define-module (gnu system file-systems) %store-mapping %network-configuration-files - %network-file-mappings)) + %network-file-mappings + + swap-space + swap-space? + swap-space-target + swap-space-dependencies)) ;;; Commentary: ;;; @@ -671,4 +676,15 @@ (define (prepend-slash/maybe s) (G_ "Use the @code{subvol} Btrfs file system option.")))))))) +;;; +;;; Swap space +;;; + +(define-record-type* swap-space make-swap-space + swap-space? + this-swap-space + (target swap-space-target) + (dependencies swap-space-dependencies + (default '()))) + ;;; file-systems.scm ends here From patchwork Wed Oct 27 15:09:11 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josselin Poiret X-Patchwork-Id: 34117 Return-Path: X-Original-To: patchwork@mira.cbaines.net Delivered-To: patchwork@mira.cbaines.net Received: by mira.cbaines.net (Postfix, from userid 113) id 071D327BBE3; Wed, 27 Oct 2021 16:49:57 +0100 (BST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on mira.cbaines.net X-Spam-Level: X-Spam-Status: No, score=-2.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, MAILING_LIST_MULTI,RCVD_IN_MSPIKE_H2,SPF_HELO_PASS,T_DKIM_INVALID, URIBL_BLOCKED autolearn=unavailable autolearn_force=no version=3.4.2 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mira.cbaines.net (Postfix) with ESMTPS id 871D527BBE1 for ; Wed, 27 Oct 2021 16:49:56 +0100 (BST) Received: from localhost ([::1]:40132 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1mflBb-0004W4-29 for patchwork@mira.cbaines.net; Wed, 27 Oct 2021 11:49:55 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:57884) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mfkZ1-0004Qy-9S for guix-patches@gnu.org; Wed, 27 Oct 2021 11:10:03 -0400 Received: from debbugs.gnu.org ([209.51.188.43]:38650) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1mfkZ0-00078B-Tq for guix-patches@gnu.org; Wed, 27 Oct 2021 11:10:02 -0400 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1mfkZ0-0000PN-QE for guix-patches@gnu.org; Wed, 27 Oct 2021 11:10:02 -0400 X-Loop: help-debbugs@gnu.org Subject: [bug#51346] [PATCH v2 2/4] gnu: system: Add swap flags. Resent-From: Josselin Poiret Original-Sender: "Debbugs-submit" Resent-CC: guix-patches@gnu.org Resent-Date: Wed, 27 Oct 2021 15:10:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 51346 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch To: Tobias Geerinckx-Rice Cc: Josselin Poiret , 51346@debbugs.gnu.org Received: via spool by 51346-submit@debbugs.gnu.org id=B51346.16353473881515 (code B ref 51346); Wed, 27 Oct 2021 15:10:02 +0000 Received: (at 51346) by debbugs.gnu.org; 27 Oct 2021 15:09:48 +0000 Received: from localhost ([127.0.0.1]:50190 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1mfkYm-0000OM-7S for submit@debbugs.gnu.org; Wed, 27 Oct 2021 11:09:48 -0400 Received: from jpoiret.xyz ([206.189.101.64]:34742) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1mfkYh-0000Nq-E7 for 51346@debbugs.gnu.org; Wed, 27 Oct 2021 11:09:43 -0400 Received: from authenticated-user (jpoiret.xyz [206.189.101.64]) by jpoiret.xyz (Postfix) with ESMTPA id 961D9184BC9; Wed, 27 Oct 2021 15:09:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=jpoiret.xyz; s=dkim; t=1635347382; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=PAKY3EBJZaeHuIoMZ/xxWuXzRdhV65m7BJSYz1AAK+U=; b=m7Gg12GToY/14D5tF3bAR17Oj0gMCiyG4Ljv41HSsaSt+xO/3K6dM4Js0BmUlNBn1iw7Wz KxRA+dIAir8S50FSm9U02QIGvAzo16tlrBdqlrA2bJHkHv0RiwRtU05uU+bA6pjhuWLatM CHR5B5lAaZsoywx6VEk2BuPMOgCpoCYDdogDU+2dOnvs9n3eeTe3mME2PdAMuWH1f4BZv7 tDLR1RWTBMTFFJY9FHRXzklodHCziQDZ6oF+8PAIv/mTyv0Aa4nK1PMsPJ+8IJGJlNGHsQ OnZCpLVG1b/ILBPzkrSDLFb8wee88+0f6dQpN5wtiapMrgn58+aN57Uhn6kCYg== Date: Wed, 27 Oct 2021 15:09:11 +0000 Message-Id: <20211027150913.6038-3-dev@jpoiret.xyz> In-Reply-To: <20211027150913.6038-1-dev@jpoiret.xyz> References: <87tuh6ifwe.fsf@nckx> <20211027150913.6038-1-dev@jpoiret.xyz> MIME-Version: 1.0 X-Spamd-Bar: / Authentication-Results: jpoiret.xyz; auth=pass smtp.auth=jpoiret@jpoiret.xyz smtp.mailfrom=dev@jpoiret.xyz X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: guix-patches@gnu.org List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guix-patches-bounces+patchwork=mira.cbaines.net@gnu.org Sender: "Guix-patches" Reply-to: Josselin Poiret X-ACL-Warn: , Josselin Poiret via Guix-patches X-Patchwork-Original-From: Josselin Poiret via Guix-patches via From: Josselin Poiret X-getmail-retrieved-from-mailbox: Patches * gnu/system/file-systems.scm (swap-space)[priority, discard?]: Add them. * guix/build/syscalls.scm (SWAP_FLAG_PREFER, SWAP_FLAG_PRIO_MASK, SWAP_FLAG_PRIO_SHIFT, SWAP_FLAG_DISCARD): Add them. * gnu/build/file-systems.scm (swap-space->flags-bit-mask): Add it. * gnu/services/base.scm (swap-service-type): Use it. --- gnu/build/file-systems.scm | 35 ++++++++++++++++++++++++++++++++++- gnu/services/base.scm | 7 +++++-- gnu/system/file-systems.scm | 10 ++++++++-- guix/build/syscalls.scm | 12 ++++++++++++ 4 files changed, 59 insertions(+), 5 deletions(-) diff --git a/gnu/build/file-systems.scm b/gnu/build/file-systems.scm index d8a5ddf1e5..39a408e8c1 100644 --- a/gnu/build/file-systems.scm +++ b/gnu/build/file-systems.scm @@ -29,6 +29,8 @@ (define-module (gnu build file-systems) #:use-module (guix build bournish) #:use-module ((guix build syscalls) #:hide (file-system-type)) + #:use-module (guix diagnostics) + #:use-module (guix i18n) #:use-module (rnrs io ports) #:use-module (rnrs bytevectors) #:use-module (ice-9 match) @@ -54,7 +56,9 @@ (define-module (gnu build file-systems) mount-flags->bit-mask check-file-system - mount-file-system)) + mount-file-system + + swap-space->flags-bit-mask)) ;;; Commentary: ;;; @@ -227,6 +231,35 @@ (define (linux-swap-superblock-volume-name sblock) "Return the label of Linux-swap superblock SBLOCK as a string." (null-terminated-latin1->string (sub-bytevector sblock (+ 1024 4 4 4 16) 16))) + +(define (swap-space->flags-bit-mask swap) + "Return the number suitable for the 'flags' argument of 'mount' +that corresponds to the swap-space SWAP." + (define prio-flag + (let ((p (swap-space-priority swap)) + (max (ash SWAP_FLAG_PRIO_MASK (- SWAP_FLAG_PRIO_SHIFT)))) + (if p + (logior SWAP_FLAG_PREFER + (ash (cond + ((< p 0) + (begin (warning + (G_ "Given swap priority ~a is negative, +defaulting to 0.~%") p) + 0)) + ((> p max) + (begin (warning + (G_ "Limiting swap priority ~a to ~a.~%") + p max) + max)) + (else p)) + SWAP_FLAG_PRIO_SHIFT)) + 0))) + (define delayed-flag + (if (swap-space-discard? swap) + SWAP_FLAG_DISCARD + 0)) + (logior prio-flag delayed-flag)) + ;;; diff --git a/gnu/services/base.scm b/gnu/services/base.scm index c816381198..cf43a78fd0 100644 --- a/gnu/services/base.scm +++ b/gnu/services/base.scm @@ -58,7 +58,8 @@ (define-module (gnu services base) #:use-module (gnu packages linux) #:use-module (gnu packages terminals) #:use-module ((gnu build file-systems) - #:select (mount-flags->bit-mask)) + #:select (mount-flags->bit-mask + swap-space->flags-bit-mask)) #:use-module (guix gexp) #:use-module (guix records) #:use-module (guix modules) @@ -2223,7 +2224,9 @@ (define device-lookup (let ((device #$device-lookup)) (and device (begin - (restart-on-EINTR (swapon device)) + (restart-on-EINTR (swapon device + #$(swap-space->flags-bit-mask + swap))) #t))))) (stop #~(lambda _ (let ((device #$device-lookup)) diff --git a/gnu/system/file-systems.scm b/gnu/system/file-systems.scm index 7aa19069a1..fba4ebf65d 100644 --- a/gnu/system/file-systems.scm +++ b/gnu/system/file-systems.scm @@ -101,7 +101,9 @@ (define-module (gnu system file-systems) swap-space swap-space? swap-space-target - swap-space-dependencies)) + swap-space-dependencies + swap-space-priority + swap-space-discard?)) ;;; Commentary: ;;; @@ -685,6 +687,10 @@ (define-record-type* swap-space make-swap-space this-swap-space (target swap-space-target) (dependencies swap-space-dependencies - (default '()))) + (default '())) + (priority swap-space-priority + (default #f)) + (discard? swap-space-discard? + (default #f))) ;;; file-systems.scm ends here diff --git a/guix/build/syscalls.scm b/guix/build/syscalls.scm index 99a3b45004..f2b18abf5a 100644 --- a/guix/build/syscalls.scm +++ b/guix/build/syscalls.scm @@ -71,6 +71,11 @@ (define-module (guix build syscalls) mounts mount-points + SWAP_FLAG_PREFER + SWAP_FLAG_PRIO_MASK + SWAP_FLAG_PRIO_SHIFT + SWAP_FLAG_DISCARD + swapon swapoff @@ -677,6 +682,13 @@ (define (mount-points) "Return the mounts points for currently mounted file systems." (map mount-point (mounts))) +;; Pulled from glibc's sysdeps/unix/sysv/linux/sys/swap.h + +(define SWAP_FLAG_PREFER #x8000) ;; Set if swap priority is specified. +(define SWAP_FLAG_PRIO_MASK #x7fff) +(define SWAP_FLAG_PRIO_SHIFT 0) +(define SWAP_FLAG_DISCARD #x10000) ;; Discard swap cluster after use. + (define swapon (let ((proc (syscall->procedure int "swapon" (list '* int)))) (lambda* (device #:optional (flags 0)) From patchwork Wed Oct 27 15:09:12 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josselin Poiret X-Patchwork-Id: 34119 Return-Path: X-Original-To: patchwork@mira.cbaines.net Delivered-To: patchwork@mira.cbaines.net Received: by mira.cbaines.net (Postfix, from userid 113) id 5FE6827BBE3; Wed, 27 Oct 2021 16:51:28 +0100 (BST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on mira.cbaines.net X-Spam-Level: X-Spam-Status: No, score=-2.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, MAILING_LIST_MULTI,RCVD_IN_MSPIKE_H2,SPF_HELO_PASS,T_DKIM_INVALID, URIBL_BLOCKED autolearn=unavailable autolearn_force=no version=3.4.2 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mira.cbaines.net (Postfix) with ESMTPS id 12C8227BBE1 for ; Wed, 27 Oct 2021 16:51:28 +0100 (BST) Received: from localhost ([::1]:42790 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1mflD3-0006RM-54 for patchwork@mira.cbaines.net; Wed, 27 Oct 2021 11:51:25 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:57886) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mfkZ1-0004St-Og for guix-patches@gnu.org; Wed, 27 Oct 2021 11:10:03 -0400 Received: from debbugs.gnu.org ([209.51.188.43]:38651) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1mfkZ1-00078T-FK for guix-patches@gnu.org; Wed, 27 Oct 2021 11:10:03 -0400 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1mfkZ1-0000PW-Ac for guix-patches@gnu.org; Wed, 27 Oct 2021 11:10:03 -0400 X-Loop: help-debbugs@gnu.org Subject: [bug#51346] [PATCH v2 3/4] gnu: system: Filter out boot dependencies from swap-space. Resent-From: Josselin Poiret Original-Sender: "Debbugs-submit" Resent-CC: guix-patches@gnu.org Resent-Date: Wed, 27 Oct 2021 15:10:03 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 51346 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch To: Tobias Geerinckx-Rice Cc: Josselin Poiret , 51346@debbugs.gnu.org Received: via spool by 51346-submit@debbugs.gnu.org id=B51346.16353473891523 (code B ref 51346); Wed, 27 Oct 2021 15:10:03 +0000 Received: (at 51346) by debbugs.gnu.org; 27 Oct 2021 15:09:49 +0000 Received: from localhost ([127.0.0.1]:50192 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1mfkYm-0000OO-JK for submit@debbugs.gnu.org; Wed, 27 Oct 2021 11:09:48 -0400 Received: from jpoiret.xyz ([206.189.101.64]:34834) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1mfkYj-0000O7-84 for 51346@debbugs.gnu.org; Wed, 27 Oct 2021 11:09:45 -0400 Received: from authenticated-user (jpoiret.xyz [206.189.101.64]) by jpoiret.xyz (Postfix) with ESMTPA id 504D0184BC9; Wed, 27 Oct 2021 15:09:44 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=jpoiret.xyz; s=dkim; t=1635347384; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=83Fns071x+nG1qjkWnmqp22+5nT32NIAaiErrr/0H+4=; b=HAE/fuMccxzYyLVpCc2XsCWcn/i6Zs1KO4LZAFb2Khluey9ReoqLPlnrSCtHhqGs76N6Jm tp1ndDld6klNRJbglrOTPXVJE5njUNjuK5d++H5iO550tuY6xJQasHg+kWHbmgB9UEgmhr apQtZ+l1aZ7ryMzU3uL+7KKNuI7Reo7rJuP44PGsQcpEEU4U5x/7NeliYb0rH4CVgw0PjV XxW32cG/DVQTAh9I/BIKTjc0WqZC0MtTZ4T8ZFU69K3NcwdZpGm5lmY1rAhGCjZeZ3ROxX tc4bunUXsZyq0xPFXkTKphi1vPWucyNCFTGSfeYB4mLgOYimjj/vSdED4UboXw== Date: Wed, 27 Oct 2021 15:09:12 +0000 Message-Id: <20211027150913.6038-4-dev@jpoiret.xyz> In-Reply-To: <20211027150913.6038-1-dev@jpoiret.xyz> References: <87tuh6ifwe.fsf@nckx> <20211027150913.6038-1-dev@jpoiret.xyz> MIME-Version: 1.0 X-Spamd-Bar: / Authentication-Results: jpoiret.xyz; auth=pass smtp.auth=jpoiret@jpoiret.xyz smtp.mailfrom=dev@jpoiret.xyz X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: guix-patches@gnu.org List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guix-patches-bounces+patchwork=mira.cbaines.net@gnu.org Sender: "Guix-patches" Reply-to: Josselin Poiret X-ACL-Warn: , Josselin Poiret via Guix-patches X-Patchwork-Original-From: Josselin Poiret via Guix-patches via From: Josselin Poiret X-getmail-retrieved-from-mailbox: Patches * gnu/systems.scm (swap-services): Filter them. --- gnu/system.scm | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/gnu/system.scm b/gnu/system.scm index 2797c07e36..e3accb3c1e 100644 --- a/gnu/system.scm +++ b/gnu/system.scm @@ -586,7 +586,23 @@ (define (device-mapping-services os) (define (swap-services os) "Return the list of swap services for OS." - (map swap-service (operating-system-swap-devices os))) + (define early-userspace-file-systems + (filter file-system-needed-for-boot? + (operating-system-file-systems os))) + + (define early-userspace-mapped-devices + (operating-system-boot-mapped-devices os)) + + (define (filter-deps swap) + (swap-space + (inherit swap) + (dependencies (remove (lambda (dep) + (or (member dep early-userspace-mapped-devices) + (member dep early-userspace-file-systems))) + (swap-space-dependencies swap))))) + + (map (compose swap-service filter-deps) + (operating-system-swap-devices os))) (define* (system-linux-image-file-name #:optional (target (or (%current-target-system) From patchwork Wed Oct 27 15:09:13 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josselin Poiret X-Patchwork-Id: 34118 Return-Path: X-Original-To: patchwork@mira.cbaines.net Delivered-To: patchwork@mira.cbaines.net Received: by mira.cbaines.net (Postfix, from userid 113) id 810AF27BBE3; Wed, 27 Oct 2021 16:50:13 +0100 (BST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on mira.cbaines.net X-Spam-Level: X-Spam-Status: No, score=-2.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, MAILING_LIST_MULTI,RCVD_IN_MSPIKE_H2,SPF_HELO_PASS,T_DKIM_INVALID, URIBL_BLOCKED autolearn=unavailable autolearn_force=no version=3.4.2 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mira.cbaines.net (Postfix) with ESMTPS id D494427BBE1 for ; Wed, 27 Oct 2021 16:50:12 +0100 (BST) Received: from localhost ([::1]:40940 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1mflBr-00055b-Th for patchwork@mira.cbaines.net; Wed, 27 Oct 2021 11:50:11 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:57888) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mfkZ2-0004Ul-7V for guix-patches@gnu.org; Wed, 27 Oct 2021 11:10:04 -0400 Received: from debbugs.gnu.org ([209.51.188.43]:38652) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1mfkZ1-00078Z-Ud for guix-patches@gnu.org; Wed, 27 Oct 2021 11:10:03 -0400 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1mfkZ1-0000Pe-Q1 for guix-patches@gnu.org; Wed, 27 Oct 2021 11:10:03 -0400 X-Loop: help-debbugs@gnu.org Subject: [bug#51346] [PATCH v2 4/4] doc: Add new Swap Space section. Resent-From: Josselin Poiret Original-Sender: "Debbugs-submit" Resent-CC: guix-patches@gnu.org Resent-Date: Wed, 27 Oct 2021 15:10:03 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 51346 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch To: Tobias Geerinckx-Rice Cc: Josselin Poiret , 51346@debbugs.gnu.org Received: via spool by 51346-submit@debbugs.gnu.org id=B51346.16353473891529 (code B ref 51346); Wed, 27 Oct 2021 15:10:03 +0000 Received: (at 51346) by debbugs.gnu.org; 27 Oct 2021 15:09:49 +0000 Received: from localhost ([127.0.0.1]:50194 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1mfkYm-0000OV-SB for submit@debbugs.gnu.org; Wed, 27 Oct 2021 11:09:49 -0400 Received: from jpoiret.xyz ([206.189.101.64]:34926) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1mfkYk-0000OE-K4 for 51346@debbugs.gnu.org; Wed, 27 Oct 2021 11:09:47 -0400 Received: from authenticated-user (jpoiret.xyz [206.189.101.64]) by jpoiret.xyz (Postfix) with ESMTPA id C0165184BC9; Wed, 27 Oct 2021 15:09:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=jpoiret.xyz; s=dkim; t=1635347386; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=HOBTRR4VEZuK6wFDIvJ78aYavptxQsPFfAQpJXLvRgY=; b=s+LMKR9ZN/4aMLJJ3i1AsdICalJtaO56X4a4rW/B2Mc4pRZ6AWbq0RdTwn4NMY2l5WAcX4 dphT6etsQYswcSNjSX2pH5/zAUYMghMjAICZbfJEeMhgYw1J6eEFLPPgrkSk+lWRHmU37Y ZBtOLu2Cfz5c79SvVNGzZIFvUmt6RybnyPoYvsTV8P5H9VQZQEHAbRbGiUYz41MggkqTsF N1p6LDkXa8m9/btL/q8WU18yCoc8Ur+SbnWFA1qWhGok6an1GI2slGXWQK7U+s0jwjdNRT Ch0WVlMEhkG1BW/p4pSgVD/sSPLp+c4WnwkDj8SrDubKWO9I+E9DBUMV60vVxA== Date: Wed, 27 Oct 2021 15:09:13 +0000 Message-Id: <20211027150913.6038-5-dev@jpoiret.xyz> In-Reply-To: <20211027150913.6038-1-dev@jpoiret.xyz> References: <87tuh6ifwe.fsf@nckx> <20211027150913.6038-1-dev@jpoiret.xyz> MIME-Version: 1.0 X-Spamd-Bar: / Authentication-Results: jpoiret.xyz; auth=pass smtp.auth=jpoiret@jpoiret.xyz smtp.mailfrom=dev@jpoiret.xyz X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: guix-patches@gnu.org List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guix-patches-bounces+patchwork=mira.cbaines.net@gnu.org Sender: "Guix-patches" Reply-to: Josselin Poiret X-ACL-Warn: , Josselin Poiret via Guix-patches X-Patchwork-Original-From: Josselin Poiret via Guix-patches via From: Josselin Poiret X-getmail-retrieved-from-mailbox: Patches * doc/guix.texi (operating-system Reference): Update swap-devices. * doc/guix.texi (Swap Space): Add it. * gnu/system/examples/desktop.tmpl: Add swap-devices example. --- doc/guix.texi | 136 ++++++++++++++++++++++--------- gnu/system/examples/desktop.tmpl | 7 +- 2 files changed, 105 insertions(+), 38 deletions(-) diff --git a/doc/guix.texi b/doc/guix.texi index 67a05a10ff..2cf30536fe 100644 --- a/doc/guix.texi +++ b/doc/guix.texi @@ -319,6 +319,7 @@ System Configuration * operating-system Reference:: Detail of operating-system declarations. * File Systems:: Configuring file system mounts. * Mapped Devices:: Block device extra processing. +* Swap Space:: Backing RAM with disk space. * User Accounts:: Specifying user accounts. * Keyboard Layout:: How the system interprets key strokes. * Locales:: Language and cultural convention settings. @@ -2515,10 +2516,9 @@ system relative to this path. If you have opted for @file{/boot/efi} as an EFI mount point for example, mount it at @file{/mnt/boot/efi} now so it is found by @code{guix system init} afterwards. -Finally, if you plan to use one or more swap partitions (@pxref{Memory -Concepts, swap space,, libc, The GNU C Library Reference Manual}), make -sure to initialize them with @command{mkswap}. Assuming you have one -swap partition on @file{/dev/sda3}, you would run: +Finally, if you plan to use one or more swap partitions (@pxref{Swap +Space}), make sure to initialize them with @command{mkswap}. Assuming +you have one swap partition on @file{/dev/sda3}, you would run: @example mkswap /dev/sda3 @@ -13769,6 +13769,7 @@ instance to support new system services. * operating-system Reference:: Detail of operating-system declarations. * File Systems:: Configuring file system mounts. * Mapped Devices:: Block device extra processing. +* Swap Space:: Backing RAM with disk space. * User Accounts:: Specifying user accounts. * Keyboard Layout:: How the system interprets key strokes. * Locales:: Language and cultural convention settings. @@ -13937,7 +13938,7 @@ configuration, but with a few modifications. @cindex encrypted disk The configuration for a typical ``desktop'' usage, with an encrypted -root partition, the X11 display +root partition, a swap file on the root partition, the X11 display server, GNOME and Xfce (users can choose which of these desktop environments to use at the log-in screen by pressing @kbd{F1}), network management, power management, and more, would look like this: @@ -14135,38 +14136,9 @@ A list of mapped devices. @xref{Mapped Devices}. @item @code{file-systems} A list of file systems. @xref{File Systems}. -@cindex swap devices -@cindex swap space @item @code{swap-devices} (default: @code{'()}) -A list of UUIDs, file system labels, or strings identifying devices or -files to be used for ``swap -space'' (@pxref{Memory Concepts,,, libc, The GNU C Library Reference -Manual}). Here are some examples: - -@table @code -@item (list (uuid "4dab5feb-d176-45de-b287-9b0a6e4c01cb")) -Use the swap partition with the given UUID@. You can learn the UUID of a -Linux swap partition by running @command{swaplabel @var{device}}, where -@var{device} is the @file{/dev} file name of that partition. - -@item (list (file-system-label "swap")) -Use the partition with label @code{swap}. Again, the -@command{swaplabel} command allows you to view and change the label of a -Linux swap partition. - -@item (list "/swapfile") -Use the file @file{/swapfile} as swap space. - -@item (list "/dev/sda3" "/dev/sdb2") -Use the @file{/dev/sda3} and @file{/dev/sdb2} partitions as swap space. -We recommend referring to swap devices by UUIDs or labels as shown above -instead. -@end table - -It is possible to specify a swap file in a file system on a mapped -device (under @file{/dev/mapper}), provided that the necessary device -mapping and file system are also specified. @xref{Mapped Devices} and -@ref{File Systems}. +@cindex swap devices +A list of swap spaces. @xref{Swap Space}. @item @code{users} (default: @code{%base-user-accounts}) @itemx @code{groups} (default: @code{%base-groups}) @@ -14756,7 +14728,8 @@ It is also desirable to encrypt swap space, since swap space may contain sensitive data. One way to accomplish that is to use a swap file in a file system on a device mapped via LUKS encryption. In this way, the swap file is encrypted because the entire device is encrypted. -@xref{Preparing for Installation,,Disk Partitioning}, for an example. +@xref{Swap Space}, or @xref{Preparing for Installation,,Disk +Partitioning}, for an example. A RAID device formed of the partitions @file{/dev/sda1} and @file{/dev/sdb1} may be declared as follows: @@ -14788,6 +14761,95 @@ Devices @file{/dev/mapper/vg0-alpha} and @file{/dev/mapper/vg0-beta} can then be used as the @code{device} of a @code{file-system} declaration (@pxref{File Systems}). +@node Swap Space +@section Swap Space +@cindex swap space + +Swap space, as it is commonly called, is a disk area specifically +designated for paging: the process in charge of memory management +(the Linux kernel or Hurd's default pager) can decide that some memory +pages stored in RAM which belong to a running program but are unused +should be stored on disk instead. It unloads those from the RAM, +freeing up precious fast memory, and writes them to the swap space. If +the program tries to access that very page, the memory management +process loads it back into memory for the program to use. + +A common misconception about swap is that it is only useful when small +amounts of RAM are available to the system. However, it should be noted +that kernels often use all available RAM for disk access caching to make +I/O faster, and thus paging out unused portions of program memory will +expand the RAM available for such caching. + +For a more detailed description of how memory is managed from the +viewpoint of a monolithic kernel, @xref{Memory +Concepts,,, libc, The GNU C Library Reference Manual}. + +The Linux kernel has support for swap partitions and swap files: the +former uses a whole disk partition for paging, whereas the second uses a +file on a file system for that (the file system driver needs to support +it). On a comparable setup, both have the same performance, so one +should consider ease of use when deciding between them. Partitions are +``simpler'' and do not need file system support, but need to be +allocated at disk formatting time (logical volumes notwithstanding), +whereas files can be allocated and deallocated at any time. + +Note that swap space is not zeroed on shutdown, so sensitive data (such +as passwords) may linger on it if it was paged out. As such, you should +consider having your swap reside on an encrypted device (@pxref{Mapped +Devices}). + +@deftp {Data Type} swap-space +Objects of this type represent swap spaces. They contain the following +members: + +@table @asis +@item @code{target} +The device or file to use, either a UUID, a @code{file-system-label} or +a string, as in the definition of a @code{file-system} (@pxref{File +Systems}). + +@item @code{dependencies} (default: @code{'()}) +A list of @code{file-system} or @code{mapped-device} objects, upon which +the availability of the space depends. Note that just like for +@code{file-system} objects, dependencies which are needed for boot and +mounted in early userspace are not managed by the Shepherd, and so +automatically filtered out for you. + +@item @code{priority} (default: @code{#f}) +Only supported by the Linux kernel. Either @code{#f} to disable swap +priority, or an integer between 0 and 32767. The kernel will first use +swap spaces of higher priority when paging, and use same priority spaces +on a round-robin basis. The kernel will use swap spaces without a set +priority after prioritized spaces, and in the order that they appeared in +(not round-robin). + +@item @code{discard?} (default: @code{#f}) +Only supported by the Linux kernel. When true, the kernel will notify +the disk controller of discarded pages, for example with the TRIM +operation on Solid State Drives. + +@end table +@end deftp + +Here are some examples: + +@table @code +@item (swap-space (target (uuid "4dab5feb-d176-45de-b287-9b0a6e4c01cb"))) +Use the swap partition with the given UUID@. You can learn the UUID of a +Linux swap partition by running @command{swaplabel @var{device}}, where +@var{device} is the @file{/dev} file name of that partition. + +@item (swap-space (target (file-system-label "swap")) (dependencies (list lvm-device))) +Use the partition with label @code{swap}, which can be found after the +@var{lvm-device} mapped device has been opened. Again, the +@command{swaplabel} command allows you to view and change the label of a +Linux swap partition. + +@item (swap-space (target "/btrfs/swapfile") (dependencies (list btrfs-fs))) +Use the file @file{/btrfs/swapfile} as swap space, which is present on the +@var{btrfs-fs} filesystem. +@end table + @node User Accounts @section User Accounts diff --git a/gnu/system/examples/desktop.tmpl b/gnu/system/examples/desktop.tmpl index c928008c92..93c35a03bc 100644 --- a/gnu/system/examples/desktop.tmpl +++ b/gnu/system/examples/desktop.tmpl @@ -1,6 +1,6 @@ ;; This is an operating system configuration template ;; for a "desktop" setup with GNOME and Xfce where the -;; root partition is encrypted with LUKS. +;; root partition is encrypted with LUKS, and a swap file. (use-modules (gnu) (gnu system nss)) (use-service-modules desktop xorg) @@ -42,6 +42,11 @@ (type "vfat"))) %base-file-systems)) + ;; Specify a swap file for the system, which resides on the + ;; root file system. + (swap-devices (list (swap-space + (target "/swapfile")))) + ;; Create user `bob' with `alice' as its initial password. (users (cons (user-account (name "bob")