file-systems: Convey hint via '&fix-hint'.
[jackhill/guix/guix.git] / gnu / system / mapped-devices.scm
CommitLineData
060d62a7 1;;; GNU Guix --- Functional package management for GNU
db170ee9 2;;; Copyright © 2014, 2015, 2016, 2017, 2018, 2019, 2020 Ludovic Courtès <ludo@gnu.org>
97c8aef1 3;;; Copyright © 2016 Andreas Enge <andreas@enge.fr>
3e5783e2 4;;; Copyright © 2017, 2018 Mark H Weaver <mhw@netris.org>
060d62a7
LC
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)
374f14c2 22 #:use-module (guix gexp)
060d62a7 23 #:use-module (guix records)
d2a1cf45 24 #:use-module ((guix modules) #:hide (file-name->module-name))
42ff7d3b 25 #:use-module (guix i18n)
f9a8dd05 26 #:use-module ((guix diagnostics)
42ff7d3b 27 #:select (source-properties->location
8ab10c19 28 &fix-hint
42ff7d3b 29 &error-location))
1ea507bc
LC
30 #:use-module (gnu services)
31 #:use-module (gnu services shepherd)
9b336338 32 #:use-module (gnu system uuid)
42ff7d3b 33 #:autoload (gnu build file-systems) (find-partition-by-luks-uuid)
8ab10c19 34 #:autoload (gnu build linux-modules)
4cd386af 35 (missing-modules)
b7d408ec 36 #:autoload (gnu packages cryptsetup) (cryptsetup-static)
4ed2f515 37 #:autoload (gnu packages linux) (mdadm-static)
ffba7d49 38 #:use-module (srfi srfi-1)
424cea80 39 #:use-module (srfi srfi-26)
42ff7d3b
LC
40 #:use-module (srfi srfi-34)
41 #:use-module (srfi srfi-35)
1ea507bc 42 #:use-module (ice-9 match)
db170ee9 43 #:use-module (ice-9 format)
060d62a7
LC
44 #:export (mapped-device
45 mapped-device?
46 mapped-device-source
47 mapped-device-target
48 mapped-device-type
4ca90ff5 49 mapped-device-location
060d62a7
LC
50
51 mapped-device-kind
52 mapped-device-kind?
53 mapped-device-kind-open
374f14c2 54 mapped-device-kind-close
4ca90ff5 55 mapped-device-kind-check
374f14c2 56
1ea507bc
LC
57 device-mapping-service-type
58 device-mapping-service
59
8ab10c19
LC
60 check-device-initrd-modules ;XXX: needs a better place
61
97c8aef1
AE
62 luks-device-mapping
63 raid-device-mapping))
060d62a7
LC
64
65;;; Commentary:
66;;;
67;;; This module supports "device mapping", a concept implemented by Linux's
68;;; device-mapper.
69;;;
70;;; Code:
71
72(define-record-type* <mapped-device> mapped-device
73 make-mapped-device
74 mapped-device?
d6d1cea6 75 (source mapped-device-source) ;string | list of strings
060d62a7 76 (target mapped-device-target) ;string
4ca90ff5
LC
77 (type mapped-device-type) ;<mapped-device-kind>
78 (location mapped-device-location
79 (default (current-source-location)) (innate)))
060d62a7
LC
80
81(define-record-type* <mapped-device-type> mapped-device-kind
82 make-mapped-device-kind
83 mapped-device-kind?
84 (open mapped-device-kind-open) ;source target -> gexp
85 (close mapped-device-kind-close ;source target -> gexp
4ca90ff5
LC
86 (default (const #~(const #f))))
87 (check mapped-device-kind-check ;source -> Boolean
88 (default (const #t))))
060d62a7 89
374f14c2 90\f
1ea507bc
LC
91;;;
92;;; Device mapping as a Shepherd service.
93;;;
94
95(define device-mapping-service-type
96 (shepherd-service-type
97 'device-mapping
98 (match-lambda
4da8c19e
LC
99 (($ <mapped-device> source target
100 ($ <mapped-device-type> open close))
1ea507bc
LC
101 (shepherd-service
102 (provision (list (symbol-append 'device-mapping- (string->symbol target))))
103 (requirement '(udev))
104 (documentation "Map a device node using Linux's device mapper.")
4da8c19e
LC
105 (start #~(lambda () #$(open source target)))
106 (stop #~(lambda _ (not #$(close source target))))
dfe06f6e 107 (respawn? #f))))))
1ea507bc 108
4da8c19e
LC
109(define (device-mapping-service mapped-device)
110 "Return a service that sets up @var{mapped-device}."
111 (service device-mapping-service-type mapped-device))
1ea507bc
LC
112
113\f
8ab10c19
LC
114;;;
115;;; Static checks.
116;;;
117
118(define (check-device-initrd-modules device linux-modules location)
119 "Raise an error if DEVICE needs modules beyond LINUX-MODULES to operate.
120DEVICE must be a \"/dev\" file name."
4cd386af
LC
121 (define missing
122 ;; Attempt to determine missing modules.
8ab10c19
LC
123 (catch 'system-error
124 (lambda ()
4cd386af
LC
125 (missing-modules device linux-modules))
126
127 ;; If we can't do that (e.g., EPERM), skip the whole thing.
128 (const '())))
129
130 (unless (null? missing)
131 ;; Note: What we suggest here is a list of module names (e.g.,
132 ;; "usb_storage"), not file names (e.g., "usb-storage.ko"). This is
133 ;; OK because we have machinery that accepts both the hyphen and the
134 ;; underscore version.
135 (raise (condition
136 (&message
137 (message (format #f (G_ "you may need these modules \
8ab10c19 138in the initrd for ~a:~{ ~a~}")
4cd386af
LC
139 device missing)))
140 (&fix-hint
141 (hint (format #f (G_ "Try adding them to the
8ab10c19
LC
142@code{initrd-modules} field of your @code{operating-system} declaration, along
143these lines:
144
145@example
146 (operating-system
147 ;; @dots{}
148 (initrd-modules (append (list~{ ~s~})
149 %base-initrd-modules)))
88600acc
LC
150@end example
151
152If you think this diagnostic is inaccurate, use the @option{--skip-checks}
153option of @command{guix system}.\n")
4cd386af
LC
154 missing)))
155 (&error-location
156 (location (source-properties->location location)))))))
8ab10c19
LC
157
158\f
374f14c2
LC
159;;;
160;;; Common device mappings.
161;;;
162
163(define (open-luks-device source target)
164 "Return a gexp that maps SOURCE to TARGET as a LUKS device, using
165'cryptsetup'."
239c6e27
LC
166 (with-imported-modules (source-module-closure
167 '((gnu build file-systems)))
9b336338
LC
168 #~(let ((source #$(if (uuid? source)
169 (uuid-bytevector source)
170 source)))
dfe06f6e
LC
171 ;; XXX: 'use-modules' should be at the top level.
172 (use-modules (rnrs bytevectors) ;bytevector?
173 ((gnu build file-systems)
174 #:select (find-partition-by-luks-uuid)))
175
b7d408ec
LC
176 ;; Use 'cryptsetup-static', not 'cryptsetup', to avoid pulling the
177 ;; whole world inside the initrd (for when we're in an initrd).
178 (zero? (system* #$(file-append cryptsetup-static "/sbin/cryptsetup")
a91c3fc7
LC
179 "open" "--type" "luks"
180
181 ;; Note: We cannot use the "UUID=source" syntax here
182 ;; because 'cryptsetup' implements it by searching the
183 ;; udev-populated /dev/disk/by-id directory but udev may
184 ;; be unavailable at the time we run this.
185 (if (bytevector? source)
f45878a8
MW
186 (or (let loop ((tries-left 10))
187 (and (positive? tries-left)
188 (or (find-partition-by-luks-uuid source)
189 ;; If the underlying partition is
190 ;; not found, try again after
191 ;; waiting a second, up to ten
192 ;; times. FIXME: This should be
193 ;; dealt with in a more robust way.
194 (begin (sleep 1)
195 (loop (- tries-left 1))))))
a91c3fc7
LC
196 (error "LUKS partition not found" source))
197 source)
198
199 #$target)))))
374f14c2
LC
200
201(define (close-luks-device source target)
202 "Return a gexp that closes TARGET, a LUKS device."
b7d408ec 203 #~(zero? (system* #$(file-append cryptsetup-static "/sbin/cryptsetup")
374f14c2
LC
204 "close" #$target)))
205
424cea80
LC
206(define* (check-luks-device md #:key
207 needed-for-boot?
208 (initrd-modules '())
209 #:allow-other-keys
210 #:rest rest)
42ff7d3b 211 "Ensure the source of MD is valid."
424cea80
LC
212 (let ((source (mapped-device-source md))
213 (location (mapped-device-location md)))
214 (or (not (zero? (getuid)))
215 (if (uuid? source)
216 (match (find-partition-by-luks-uuid (uuid-bytevector source))
217 (#f
218 (raise (condition
219 (&message
220 (message (format #f (G_ "no LUKS partition with UUID '~a'")
221 (uuid->string source))))
222 (&error-location
223 (location (source-properties->location
224 (mapped-device-location md)))))))
225 ((? string? device)
226 (check-device-initrd-modules device initrd-modules location)))
227 (check-device-initrd-modules source initrd-modules location)))))
42ff7d3b 228
374f14c2
LC
229(define luks-device-mapping
230 ;; The type of LUKS mapped devices.
231 (mapped-device-kind
232 (open open-luks-device)
42ff7d3b
LC
233 (close close-luks-device)
234 (check check-luks-device)))
374f14c2 235
7f8ad82b
LC
236(define (open-raid-device sources target)
237 "Return a gexp that assembles SOURCES (a list of devices) to the RAID device
238TARGET (e.g., \"/dev/md0\"), using 'mdadm'."
10618627
LC
239 #~(let ((sources '#$sources)
240
241 ;; XXX: We're not at the top level here. We could use a
242 ;; non-top-level 'use-modules' form but that doesn't work when the
243 ;; code is eval'd, like the Shepherd does.
244 (every (@ (srfi srfi-1) every))
245 (format (@ (ice-9 format) format)))
246 (let loop ((attempts 0))
247 (unless (every file-exists? sources)
248 (when (> attempts 20)
249 (error "RAID devices did not show up; bailing out"
250 sources))
251
252 (format #t "waiting for RAID source devices~{ ~a~}...~%"
253 sources)
254 (sleep 1)
255 (loop (+ 1 attempts))))
256
4ed2f515
LC
257 ;; Use 'mdadm-static' rather than 'mdadm' to avoid pulling its whole
258 ;; closure (80 MiB) in the initrd when a RAID device is needed for boot.
259 (zero? (apply system* #$(file-append mdadm-static "/sbin/mdadm")
6e52376d 260 "--assemble" #$target sources))))
7f8ad82b
LC
261
262(define (close-raid-device sources target)
97c8aef1 263 "Return a gexp that stops the RAID device TARGET."
4ed2f515 264 #~(zero? (system* #$(file-append mdadm-static "/sbin/mdadm")
97c8aef1
AE
265 "--stop" #$target)))
266
267(define raid-device-mapping
268 ;; The type of RAID mapped devices.
269 (mapped-device-kind
270 (open open-raid-device)
271 (close close-raid-device)))
272
060d62a7 273;;; mapped-devices.scm ends here