mapped-devices: 'mapped-device-service' takes a <mapped-device>.
[jackhill/guix/guix.git] / gnu / system / mapped-devices.scm
CommitLineData
060d62a7
LC
1;;; GNU Guix --- Functional package management for GNU
2;;; Copyright © 2014, 2015, 2016 Ludovic Courtès <ludo@gnu.org>
3;;;
4;;; This file is part of GNU Guix.
5;;;
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.
10;;;
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.
15;;;
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/>.
18
19(define-module (gnu system mapped-devices)
374f14c2 20 #:use-module (guix gexp)
060d62a7 21 #:use-module (guix records)
1ea507bc
LC
22 #:use-module (gnu services)
23 #:use-module (gnu services shepherd)
374f14c2 24 #:autoload (gnu packages cryptsetup) (cryptsetup)
1ea507bc 25 #:use-module (ice-9 match)
060d62a7
LC
26 #:export (mapped-device
27 mapped-device?
28 mapped-device-source
29 mapped-device-target
30 mapped-device-type
31
32 mapped-device-kind
33 mapped-device-kind?
34 mapped-device-kind-open
374f14c2
LC
35 mapped-device-kind-close
36
1ea507bc
LC
37 device-mapping-service-type
38 device-mapping-service
39
374f14c2 40 luks-device-mapping))
060d62a7
LC
41
42;;; Commentary:
43;;;
44;;; This module supports "device mapping", a concept implemented by Linux's
45;;; device-mapper.
46;;;
47;;; Code:
48
49(define-record-type* <mapped-device> mapped-device
50 make-mapped-device
51 mapped-device?
52 (source mapped-device-source) ;string
53 (target mapped-device-target) ;string
54 (type mapped-device-type)) ;<mapped-device-kind>
55
56(define-record-type* <mapped-device-type> mapped-device-kind
57 make-mapped-device-kind
58 mapped-device-kind?
59 (open mapped-device-kind-open) ;source target -> gexp
60 (close mapped-device-kind-close ;source target -> gexp
61 (default (const #~(const #f)))))
62
374f14c2 63\f
1ea507bc
LC
64;;;
65;;; Device mapping as a Shepherd service.
66;;;
67
68(define device-mapping-service-type
69 (shepherd-service-type
70 'device-mapping
71 (match-lambda
4da8c19e
LC
72 (($ <mapped-device> source target
73 ($ <mapped-device-type> open close))
1ea507bc
LC
74 (shepherd-service
75 (provision (list (symbol-append 'device-mapping- (string->symbol target))))
76 (requirement '(udev))
77 (documentation "Map a device node using Linux's device mapper.")
4da8c19e
LC
78 (start #~(lambda () #$(open source target)))
79 (stop #~(lambda _ (not #$(close source target))))
1ea507bc
LC
80 (respawn? #f))))))
81
4da8c19e
LC
82(define (device-mapping-service mapped-device)
83 "Return a service that sets up @var{mapped-device}."
84 (service device-mapping-service-type mapped-device))
1ea507bc
LC
85
86\f
374f14c2
LC
87;;;
88;;; Common device mappings.
89;;;
90
91(define (open-luks-device source target)
92 "Return a gexp that maps SOURCE to TARGET as a LUKS device, using
93'cryptsetup'."
94 #~(zero? (system* (string-append #$cryptsetup "/sbin/cryptsetup")
95 "open" "--type" "luks"
96 #$source #$target)))
97
98(define (close-luks-device source target)
99 "Return a gexp that closes TARGET, a LUKS device."
100 #~(zero? (system* (string-append #$cryptsetup "/sbin/cryptsetup")
101 "close" #$target)))
102
103(define luks-device-mapping
104 ;; The type of LUKS mapped devices.
105 (mapped-device-kind
106 (open open-luks-device)
107 (close close-luks-device)))
108
060d62a7 109;;; mapped-devices.scm ends here