system: Rework swap space support, add dependencies.

* 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.

Signed-off-by: Ludovic Courtès <ludo@gnu.org>
This commit is contained in:
Josselin Poiret 2021-11-15 20:26:27 +00:00 committed by Ludovic Courtès
parent c984076a7d
commit 133a61ae26
No known key found for this signature in database
GPG key ID: 090B11993D9AEBB5
3 changed files with 84 additions and 34 deletions

View file

@ -63,6 +63,8 @@ (define-module (gnu services base)
#:use-module (guix records) #:use-module (guix records)
#:use-module (guix modules) #:use-module (guix modules)
#:use-module ((guix self) #:select (make-config.scm)) #: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-1)
#:use-module (srfi srfi-26) #:use-module (srfi srfi-26)
#:use-module (ice-9 match) #:use-module (ice-9 match)
@ -2146,62 +2148,94 @@ (define* (udev-rules-service name rules #:key (groups '()))
udev-service-type udev-extension)))))) udev-service-type udev-extension))))))
(service type #f))) (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 (define swap-service-type
(shepherd-service-type (shepherd-service-type
'swap 'swap
(lambda (device) (lambda (swap)
(define requirement (define requirements
(if (and (string? device) (cond ((swap-space? swap)
(string-prefix? "/dev/mapper/" device)) (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- (list (symbol-append 'device-mapping-
(string->symbol (basename device)))) (string->symbol (basename swap)))))
'())) (else
'())))
(define (device-lookup device) (define device-lookup
;; The generic 'find-partition' procedures could return a partition ;; The generic 'find-partition' procedures could return a partition
;; that's not swap space, but that's unlikely. ;; that's not swap space, but that's unlikely.
(cond ((uuid? device) (cond ((swap-space? swap)
#~(find-partition-by-uuid #$(uuid-bytevector device))) (let ((target (swap-space-target swap)))
((file-system-label? device) (cond ((uuid? target)
#~(find-partition-by-uuid #$(uuid-bytevector target)))
((file-system-label? target)
#~(find-partition-by-label #~(find-partition-by-label
#$(file-system-label->string device))) #$(file-system-label->string target)))
(else (else
device))) target))))
; TODO Remove after deprecation
(define service-name ((uuid? swap)
(symbol-append 'swap- #~(find-partition-by-uuid #$(uuid-bytevector swap)))
(string->symbol ((file-system-label? swap)
(cond ((uuid? device) #~(find-partition-by-label
(string-take (uuid->string device) 6)) #$(file-system-label->string swap)))
((file-system-label? device)
(file-system-label->string device))
(else (else
device))))) swap)))
(with-imported-modules (source-module-closure '((gnu build file-systems))) (with-imported-modules (source-module-closure '((gnu build file-systems)))
(shepherd-service (shepherd-service
(provision (list service-name)) (provision (list (swap->shepherd-service-name swap)))
(requirement `(udev ,@requirement)) (requirement `(udev ,@requirements))
(documentation "Enable the given swap device.") (documentation "Enable the given swap space.")
(modules `((gnu build file-systems) (modules `((gnu build file-systems)
,@%default-modules)) ,@%default-modules))
(start #~(lambda () (start #~(lambda ()
(let ((device #$(device-lookup device))) (let ((device #$device-lookup))
(and device (and device
(begin (begin
(restart-on-EINTR (swapon device)) (restart-on-EINTR (swapon device))
#t))))) #t)))))
(stop #~(lambda _ (stop #~(lambda _
(let ((device #$(device-lookup device))) (let ((device #$device-lookup))
(when device (when device
(restart-on-EINTR (swapoff device))) (restart-on-EINTR (swapoff device)))
#f))) #f)))
(respawn? #f)))) (respawn? #f))))
(description "Turn on the virtual memory swap area."))) (description "Turn on the virtual memory swap area.")))
(define (swap-service device) (define (swap-service swap)
"Return a service that uses @var{device} as a swap device." "Return a service that uses @var{swap} as a swap space."
(service swap-service-type device)) (service swap-service-type swap))
(define %default-gpm-options (define %default-gpm-options
;; Default options for GPM. ;; Default options for GPM.

View file

@ -233,8 +233,8 @@ (define-record-type* <operating-system> operating-system
(mapped-devices operating-system-mapped-devices ; list of <mapped-device> (mapped-devices operating-system-mapped-devices ; list of <mapped-device>
(default '())) (default '()))
(file-systems operating-system-file-systems) ; list of fs (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 | <swap-space>
(default '())) (default '())
(users operating-system-users ; list of user accounts (users operating-system-users ; list of user accounts
(default %base-user-accounts)) (default %base-user-accounts))

View file

@ -97,7 +97,12 @@ (define-module (gnu system file-systems)
%store-mapping %store-mapping
%network-configuration-files %network-configuration-files
%network-file-mappings)) %network-file-mappings
swap-space
swap-space?
swap-space-target
swap-space-dependencies))
;;; Commentary: ;;; Commentary:
;;; ;;;
@ -712,4 +717,15 @@ (define (prepend-slash/maybe s)
(G_ "Use the @code{subvol} Btrfs file system option.")))))))) (G_ "Use the @code{subvol} Btrfs file system option."))))))))
;;;
;;; Swap space
;;;
(define-record-type* <swap-space> swap-space make-swap-space
swap-space?
this-swap-space
(target swap-space-target)
(dependencies swap-space-dependencies
(default '())))
;;; file-systems.scm ends here ;;; file-systems.scm ends here