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