mapped-devices: Use 'cryptsetup-static' in 'luks-device-mapping'.
[jackhill/guix/guix.git] / gnu / system / mapped-devices.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2014, 2015, 2016 Ludovic Courtès <ludo@gnu.org>
3 ;;; Copyright © 2016 Andreas Enge <andreas@enge.fr>
4 ;;;
5 ;;; This file is part of GNU Guix.
6 ;;;
7 ;;; GNU Guix is free software; you can redistribute it and/or modify it
8 ;;; under the terms of the GNU General Public License as published by
9 ;;; the Free Software Foundation; either version 3 of the License, or (at
10 ;;; your option) any later version.
11 ;;;
12 ;;; GNU Guix is distributed in the hope that it will be useful, but
13 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;;; GNU General Public License for more details.
16 ;;;
17 ;;; You should have received a copy of the GNU General Public License
18 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
19
20 (define-module (gnu system mapped-devices)
21 #:use-module (guix gexp)
22 #:use-module (guix records)
23 #:use-module (guix modules)
24 #:use-module (gnu services)
25 #:use-module (gnu services shepherd)
26 #:autoload (gnu packages cryptsetup) (cryptsetup-static)
27 #:autoload (gnu packages linux) (mdadm-static)
28 #:use-module (srfi srfi-1)
29 #:use-module (ice-9 match)
30 #:export (mapped-device
31 mapped-device?
32 mapped-device-source
33 mapped-device-target
34 mapped-device-type
35
36 mapped-device-kind
37 mapped-device-kind?
38 mapped-device-kind-open
39 mapped-device-kind-close
40
41 device-mapping-service-type
42 device-mapping-service
43
44 luks-device-mapping
45 raid-device-mapping))
46
47 ;;; Commentary:
48 ;;;
49 ;;; This module supports "device mapping", a concept implemented by Linux's
50 ;;; device-mapper.
51 ;;;
52 ;;; Code:
53
54 (define-record-type* <mapped-device> mapped-device
55 make-mapped-device
56 mapped-device?
57 (source mapped-device-source) ;string
58 (target mapped-device-target) ;string
59 (type mapped-device-type)) ;<mapped-device-kind>
60
61 (define-record-type* <mapped-device-type> mapped-device-kind
62 make-mapped-device-kind
63 mapped-device-kind?
64 (open mapped-device-kind-open) ;source target -> gexp
65 (close mapped-device-kind-close ;source target -> gexp
66 (default (const #~(const #f)))))
67
68 \f
69 ;;;
70 ;;; Device mapping as a Shepherd service.
71 ;;;
72
73 (define device-mapping-service-type
74 (shepherd-service-type
75 'device-mapping
76 (match-lambda
77 (($ <mapped-device> source target
78 ($ <mapped-device-type> open close))
79 (shepherd-service
80 (provision (list (symbol-append 'device-mapping- (string->symbol target))))
81 (requirement '(udev))
82 (documentation "Map a device node using Linux's device mapper.")
83 (start #~(lambda () #$(open source target)))
84 (stop #~(lambda _ (not #$(close source target))))
85 (respawn? #f))))))
86
87 (define (device-mapping-service mapped-device)
88 "Return a service that sets up @var{mapped-device}."
89 (service device-mapping-service-type mapped-device))
90
91 \f
92 ;;;
93 ;;; Common device mappings.
94 ;;;
95
96 (define (open-luks-device source target)
97 "Return a gexp that maps SOURCE to TARGET as a LUKS device, using
98 'cryptsetup'."
99 (with-imported-modules (source-module-closure
100 '((gnu build file-systems)))
101 #~(let ((source #$source))
102 ;; XXX: 'use-modules' should be at the top level.
103 (use-modules (rnrs bytevectors) ;bytevector?
104 ((gnu build file-systems)
105 #:select (find-partition-by-luks-uuid)))
106
107 ;; Use 'cryptsetup-static', not 'cryptsetup', to avoid pulling the
108 ;; whole world inside the initrd (for when we're in an initrd).
109 (zero? (system* #$(file-append cryptsetup-static "/sbin/cryptsetup")
110 "open" "--type" "luks"
111
112 ;; Note: We cannot use the "UUID=source" syntax here
113 ;; because 'cryptsetup' implements it by searching the
114 ;; udev-populated /dev/disk/by-id directory but udev may
115 ;; be unavailable at the time we run this.
116 (if (bytevector? source)
117 (or (find-partition-by-luks-uuid source)
118 (error "LUKS partition not found" source))
119 source)
120
121 #$target)))))
122
123 (define (close-luks-device source target)
124 "Return a gexp that closes TARGET, a LUKS device."
125 #~(zero? (system* #$(file-append cryptsetup-static "/sbin/cryptsetup")
126 "close" #$target)))
127
128 (define luks-device-mapping
129 ;; The type of LUKS mapped devices.
130 (mapped-device-kind
131 (open open-luks-device)
132 (close close-luks-device)))
133
134 (define (open-raid-device sources target)
135 "Return a gexp that assembles SOURCES (a list of devices) to the RAID device
136 TARGET (e.g., \"/dev/md0\"), using 'mdadm'."
137 #~(let ((sources '#$sources)
138
139 ;; XXX: We're not at the top level here. We could use a
140 ;; non-top-level 'use-modules' form but that doesn't work when the
141 ;; code is eval'd, like the Shepherd does.
142 (every (@ (srfi srfi-1) every))
143 (format (@ (ice-9 format) format)))
144 (let loop ((attempts 0))
145 (unless (every file-exists? sources)
146 (when (> attempts 20)
147 (error "RAID devices did not show up; bailing out"
148 sources))
149
150 (format #t "waiting for RAID source devices~{ ~a~}...~%"
151 sources)
152 (sleep 1)
153 (loop (+ 1 attempts))))
154
155 ;; Use 'mdadm-static' rather than 'mdadm' to avoid pulling its whole
156 ;; closure (80 MiB) in the initrd when a RAID device is needed for boot.
157 (zero? (apply system* #$(file-append mdadm-static "/sbin/mdadm")
158 "--assemble" #$target sources))))
159
160 (define (close-raid-device sources target)
161 "Return a gexp that stops the RAID device TARGET."
162 #~(zero? (system* #$(file-append mdadm-static "/sbin/mdadm")
163 "--stop" #$target)))
164
165 (define raid-device-mapping
166 ;; The type of RAID mapped devices.
167 (mapped-device-kind
168 (open open-raid-device)
169 (close close-raid-device)))
170
171 ;;; mapped-devices.scm ends here