1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2014, 2015, 2016 Ludovic Courtès <ludo@gnu.org>
4 ;;; This file is part of GNU Guix.
6 ;;; GNU Guix is free software; you can redistribute it and/or modify it
7 ;;; under the terms of the GNU General Public License as published by
8 ;;; the Free Software Foundation; either version 3 of the License, or (at
9 ;;; your option) any later version.
11 ;;; GNU Guix is distributed in the hope that it will be useful, but
12 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ;;; GNU General Public License for more details.
16 ;;; You should have received a copy of the GNU General Public License
17 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
19 (define-module (gnu system mapped-devices)
20 #:use-module (guix gexp)
21 #:use-module (guix records)
22 #:use-module (gnu services)
23 #:use-module (gnu services shepherd)
24 #:autoload (gnu packages cryptsetup) (cryptsetup)
25 #:use-module (srfi srfi-1)
26 #:use-module (ice-9 match)
27 #:export (mapped-device
35 mapped-device-kind-open
36 mapped-device-kind-close
38 device-mapping-service-type
39 device-mapping-service
45 ;;; This module supports "device mapping", a concept implemented by Linux's
50 (define-record-type* <mapped-device> mapped-device
53 (source mapped-device-source) ;string
54 (target mapped-device-target) ;string
55 (type mapped-device-type)) ;<mapped-device-kind>
57 (define-record-type* <mapped-device-type> mapped-device-kind
58 make-mapped-device-kind
60 (open mapped-device-kind-open) ;source target -> gexp
61 (close mapped-device-kind-close ;source target -> gexp
62 (default (const #~(const #f)))))
66 ;;; Device mapping as a Shepherd service.
69 (define device-mapping-service-type
70 (shepherd-service-type
73 (($ <mapped-device> source target
74 ($ <mapped-device-type> open close))
76 (provision (list (symbol-append 'device-mapping- (string->symbol target))))
78 (documentation "Map a device node using Linux's device mapper.")
79 (start #~(lambda () #$(open source target)))
80 (stop #~(lambda _ (not #$(close source target))))
83 ;; Add the modules needed by LUKS-DEVICE-MAPPING.
84 ;; FIXME: This info should be propagated via gexps.
85 (modules `((rnrs bytevectors) ;bytevector?
86 ((gnu build file-systems)
87 #:select (find-partition-by-luks-uuid))
88 ,@%default-modules)))))))
90 (define (device-mapping-service mapped-device)
91 "Return a service that sets up @var{mapped-device}."
92 (service device-mapping-service-type mapped-device))
96 ;;; Common device mappings.
99 (define (open-luks-device source target)
100 "Return a gexp that maps SOURCE to TARGET as a LUKS device, using
102 (with-imported-modules '((gnu build file-systems)
103 (guix build bournish))
104 #~(let ((source #$source))
105 (zero? (system* (string-append #$cryptsetup "/sbin/cryptsetup")
106 "open" "--type" "luks"
108 ;; Note: We cannot use the "UUID=source" syntax here
109 ;; because 'cryptsetup' implements it by searching the
110 ;; udev-populated /dev/disk/by-id directory but udev may
111 ;; be unavailable at the time we run this.
112 (if (bytevector? source)
113 (or (find-partition-by-luks-uuid source)
114 (error "LUKS partition not found" source))
119 (define (close-luks-device source target)
120 "Return a gexp that closes TARGET, a LUKS device."
121 #~(zero? (system* (string-append #$cryptsetup "/sbin/cryptsetup")
124 (define luks-device-mapping
125 ;; The type of LUKS mapped devices.
127 (open open-luks-device)
128 (close close-luks-device)))
130 ;;; mapped-devices.scm ends here