guix system: Check for the lack of modules in the initrd.
[jackhill/guix/guix.git] / gnu / system / mapped-devices.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2014, 2015, 2016, 2017, 2018 Ludovic Courtès <ludo@gnu.org>
3 ;;; Copyright © 2016 Andreas Enge <andreas@enge.fr>
4 ;;; Copyright © 2017 Mark H Weaver <mhw@netris.org>
5 ;;;
6 ;;; This file is part of GNU Guix.
7 ;;;
8 ;;; GNU Guix is free software; you can redistribute it and/or modify it
9 ;;; under the terms of the GNU General Public License as published by
10 ;;; the Free Software Foundation; either version 3 of the License, or (at
11 ;;; your option) any later version.
12 ;;;
13 ;;; GNU Guix is distributed in the hope that it will be useful, but
14 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;;; GNU General Public License for more details.
17 ;;;
18 ;;; You should have received a copy of the GNU General Public License
19 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
20
21 (define-module (gnu system mapped-devices)
22 #:use-module (guix gexp)
23 #:use-module (guix records)
24 #:use-module (guix modules)
25 #:use-module (guix i18n)
26 #:use-module ((guix utils)
27 #:select (source-properties->location
28 &error-location))
29 #:use-module (gnu services)
30 #:use-module (gnu services shepherd)
31 #:use-module (gnu system uuid)
32 #:autoload (gnu build file-systems) (find-partition-by-luks-uuid)
33 #:autoload (gnu build linux-modules)
34 (device-module-aliases matching-modules)
35 #:autoload (gnu packages cryptsetup) (cryptsetup-static)
36 #:autoload (gnu packages linux) (mdadm-static)
37 #:use-module (srfi srfi-1)
38 #:use-module (srfi srfi-26)
39 #:use-module (srfi srfi-34)
40 #:use-module (srfi srfi-35)
41 #:use-module (ice-9 match)
42 #:export (mapped-device
43 mapped-device?
44 mapped-device-source
45 mapped-device-target
46 mapped-device-type
47 mapped-device-location
48
49 mapped-device-kind
50 mapped-device-kind?
51 mapped-device-kind-open
52 mapped-device-kind-close
53 mapped-device-kind-check
54
55 device-mapping-service-type
56 device-mapping-service
57
58 luks-device-mapping
59 raid-device-mapping))
60
61 ;;; Commentary:
62 ;;;
63 ;;; This module supports "device mapping", a concept implemented by Linux's
64 ;;; device-mapper.
65 ;;;
66 ;;; Code:
67
68 (define-record-type* <mapped-device> mapped-device
69 make-mapped-device
70 mapped-device?
71 (source mapped-device-source) ;string | list of strings
72 (target mapped-device-target) ;string
73 (type mapped-device-type) ;<mapped-device-kind>
74 (location mapped-device-location
75 (default (current-source-location)) (innate)))
76
77 (define-record-type* <mapped-device-type> mapped-device-kind
78 make-mapped-device-kind
79 mapped-device-kind?
80 (open mapped-device-kind-open) ;source target -> gexp
81 (close mapped-device-kind-close ;source target -> gexp
82 (default (const #~(const #f))))
83 (check mapped-device-kind-check ;source -> Boolean
84 (default (const #t))))
85
86 \f
87 ;;;
88 ;;; Device mapping as a Shepherd service.
89 ;;;
90
91 (define device-mapping-service-type
92 (shepherd-service-type
93 'device-mapping
94 (match-lambda
95 (($ <mapped-device> source target
96 ($ <mapped-device-type> open close))
97 (shepherd-service
98 (provision (list (symbol-append 'device-mapping- (string->symbol target))))
99 (requirement '(udev))
100 (documentation "Map a device node using Linux's device mapper.")
101 (start #~(lambda () #$(open source target)))
102 (stop #~(lambda _ (not #$(close source target))))
103 (respawn? #f))))))
104
105 (define (device-mapping-service mapped-device)
106 "Return a service that sets up @var{mapped-device}."
107 (service device-mapping-service-type mapped-device))
108
109 \f
110 ;;;
111 ;;; Common device mappings.
112 ;;;
113
114 (define (open-luks-device source target)
115 "Return a gexp that maps SOURCE to TARGET as a LUKS device, using
116 'cryptsetup'."
117 (with-imported-modules (source-module-closure
118 '((gnu build file-systems)))
119 #~(let ((source #$(if (uuid? source)
120 (uuid-bytevector source)
121 source)))
122 ;; XXX: 'use-modules' should be at the top level.
123 (use-modules (rnrs bytevectors) ;bytevector?
124 ((gnu build file-systems)
125 #:select (find-partition-by-luks-uuid)))
126
127 ;; Use 'cryptsetup-static', not 'cryptsetup', to avoid pulling the
128 ;; whole world inside the initrd (for when we're in an initrd).
129 (zero? (system* #$(file-append cryptsetup-static "/sbin/cryptsetup")
130 "open" "--type" "luks"
131
132 ;; Note: We cannot use the "UUID=source" syntax here
133 ;; because 'cryptsetup' implements it by searching the
134 ;; udev-populated /dev/disk/by-id directory but udev may
135 ;; be unavailable at the time we run this.
136 (if (bytevector? source)
137 (or (let loop ((tries-left 10))
138 (and (positive? tries-left)
139 (or (find-partition-by-luks-uuid source)
140 ;; If the underlying partition is
141 ;; not found, try again after
142 ;; waiting a second, up to ten
143 ;; times. FIXME: This should be
144 ;; dealt with in a more robust way.
145 (begin (sleep 1)
146 (loop (- tries-left 1))))))
147 (error "LUKS partition not found" source))
148 source)
149
150 #$target)))))
151
152 (define (close-luks-device source target)
153 "Return a gexp that closes TARGET, a LUKS device."
154 #~(zero? (system* #$(file-append cryptsetup-static "/sbin/cryptsetup")
155 "close" #$target)))
156
157 (define (check-device-initrd-modules device linux-modules location)
158 "Raise an error if DEVICE needs modules beyond LINUX-MODULES to operate.
159 DEVICE must be a \"/dev\" file name."
160 (let ((modules (delete-duplicates
161 (append-map matching-modules
162 (device-module-aliases device)))))
163 (unless (every (cute member <> linux-modules) modules)
164 (raise (condition
165 (&message
166 (message (format #f (G_ "you may need these modules \
167 in the initrd for ~a:~{ ~a~}")
168 device modules)))
169 (&error-location
170 (location (source-properties->location location))))))))
171
172 (define* (check-luks-device md #:key
173 needed-for-boot?
174 (initrd-modules '())
175 #:allow-other-keys
176 #:rest rest)
177 "Ensure the source of MD is valid."
178 (let ((source (mapped-device-source md))
179 (location (mapped-device-location md)))
180 (or (not (zero? (getuid)))
181 (if (uuid? source)
182 (match (find-partition-by-luks-uuid (uuid-bytevector source))
183 (#f
184 (raise (condition
185 (&message
186 (message (format #f (G_ "no LUKS partition with UUID '~a'")
187 (uuid->string source))))
188 (&error-location
189 (location (source-properties->location
190 (mapped-device-location md)))))))
191 ((? string? device)
192 (check-device-initrd-modules device initrd-modules location)))
193 (check-device-initrd-modules source initrd-modules location)))))
194
195 (define luks-device-mapping
196 ;; The type of LUKS mapped devices.
197 (mapped-device-kind
198 (open open-luks-device)
199 (close close-luks-device)
200 (check check-luks-device)))
201
202 (define (open-raid-device sources target)
203 "Return a gexp that assembles SOURCES (a list of devices) to the RAID device
204 TARGET (e.g., \"/dev/md0\"), using 'mdadm'."
205 #~(let ((sources '#$sources)
206
207 ;; XXX: We're not at the top level here. We could use a
208 ;; non-top-level 'use-modules' form but that doesn't work when the
209 ;; code is eval'd, like the Shepherd does.
210 (every (@ (srfi srfi-1) every))
211 (format (@ (ice-9 format) format)))
212 (let loop ((attempts 0))
213 (unless (every file-exists? sources)
214 (when (> attempts 20)
215 (error "RAID devices did not show up; bailing out"
216 sources))
217
218 (format #t "waiting for RAID source devices~{ ~a~}...~%"
219 sources)
220 (sleep 1)
221 (loop (+ 1 attempts))))
222
223 ;; Use 'mdadm-static' rather than 'mdadm' to avoid pulling its whole
224 ;; closure (80 MiB) in the initrd when a RAID device is needed for boot.
225 (zero? (apply system* #$(file-append mdadm-static "/sbin/mdadm")
226 "--assemble" #$target sources))))
227
228 (define (close-raid-device sources target)
229 "Return a gexp that stops the RAID device TARGET."
230 #~(zero? (system* #$(file-append mdadm-static "/sbin/mdadm")
231 "--stop" #$target)))
232
233 (define raid-device-mapping
234 ;; The type of RAID mapped devices.
235 (mapped-device-kind
236 (open open-raid-device)
237 (close close-raid-device)))
238
239 ;;; mapped-devices.scm ends here