mapped-devices: Add 'location' and 'check' fields.
[jackhill/guix/guix.git] / gnu / system / mapped-devices.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2014, 2015, 2016, 2017 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 (gnu services)
26 #:use-module (gnu services shepherd)
27 #:use-module (gnu system uuid)
28 #:autoload (gnu packages cryptsetup) (cryptsetup-static)
29 #:autoload (gnu packages linux) (mdadm-static)
30 #:use-module (srfi srfi-1)
31 #:use-module (ice-9 match)
32 #:export (mapped-device
33 mapped-device?
34 mapped-device-source
35 mapped-device-target
36 mapped-device-type
37 mapped-device-location
38
39 mapped-device-kind
40 mapped-device-kind?
41 mapped-device-kind-open
42 mapped-device-kind-close
43 mapped-device-kind-check
44
45 device-mapping-service-type
46 device-mapping-service
47
48 luks-device-mapping
49 raid-device-mapping))
50
51 ;;; Commentary:
52 ;;;
53 ;;; This module supports "device mapping", a concept implemented by Linux's
54 ;;; device-mapper.
55 ;;;
56 ;;; Code:
57
58 (define-record-type* <mapped-device> mapped-device
59 make-mapped-device
60 mapped-device?
61 (source mapped-device-source) ;string | list of strings
62 (target mapped-device-target) ;string
63 (type mapped-device-type) ;<mapped-device-kind>
64 (location mapped-device-location
65 (default (current-source-location)) (innate)))
66
67 (define-record-type* <mapped-device-type> mapped-device-kind
68 make-mapped-device-kind
69 mapped-device-kind?
70 (open mapped-device-kind-open) ;source target -> gexp
71 (close mapped-device-kind-close ;source target -> gexp
72 (default (const #~(const #f))))
73 (check mapped-device-kind-check ;source -> Boolean
74 (default (const #t))))
75
76 \f
77 ;;;
78 ;;; Device mapping as a Shepherd service.
79 ;;;
80
81 (define device-mapping-service-type
82 (shepherd-service-type
83 'device-mapping
84 (match-lambda
85 (($ <mapped-device> source target
86 ($ <mapped-device-type> open close))
87 (shepherd-service
88 (provision (list (symbol-append 'device-mapping- (string->symbol target))))
89 (requirement '(udev))
90 (documentation "Map a device node using Linux's device mapper.")
91 (start #~(lambda () #$(open source target)))
92 (stop #~(lambda _ (not #$(close source target))))
93 (respawn? #f))))))
94
95 (define (device-mapping-service mapped-device)
96 "Return a service that sets up @var{mapped-device}."
97 (service device-mapping-service-type mapped-device))
98
99 \f
100 ;;;
101 ;;; Common device mappings.
102 ;;;
103
104 (define (open-luks-device source target)
105 "Return a gexp that maps SOURCE to TARGET as a LUKS device, using
106 'cryptsetup'."
107 (with-imported-modules (source-module-closure
108 '((gnu build file-systems)))
109 #~(let ((source #$(if (uuid? source)
110 (uuid-bytevector source)
111 source)))
112 ;; XXX: 'use-modules' should be at the top level.
113 (use-modules (rnrs bytevectors) ;bytevector?
114 ((gnu build file-systems)
115 #:select (find-partition-by-luks-uuid)))
116
117 ;; Use 'cryptsetup-static', not 'cryptsetup', to avoid pulling the
118 ;; whole world inside the initrd (for when we're in an initrd).
119 (zero? (system* #$(file-append cryptsetup-static "/sbin/cryptsetup")
120 "open" "--type" "luks"
121
122 ;; Note: We cannot use the "UUID=source" syntax here
123 ;; because 'cryptsetup' implements it by searching the
124 ;; udev-populated /dev/disk/by-id directory but udev may
125 ;; be unavailable at the time we run this.
126 (if (bytevector? source)
127 (or (let loop ((tries-left 10))
128 (and (positive? tries-left)
129 (or (find-partition-by-luks-uuid source)
130 ;; If the underlying partition is
131 ;; not found, try again after
132 ;; waiting a second, up to ten
133 ;; times. FIXME: This should be
134 ;; dealt with in a more robust way.
135 (begin (sleep 1)
136 (loop (- tries-left 1))))))
137 (error "LUKS partition not found" source))
138 source)
139
140 #$target)))))
141
142 (define (close-luks-device source target)
143 "Return a gexp that closes TARGET, a LUKS device."
144 #~(zero? (system* #$(file-append cryptsetup-static "/sbin/cryptsetup")
145 "close" #$target)))
146
147 (define luks-device-mapping
148 ;; The type of LUKS mapped devices.
149 (mapped-device-kind
150 (open open-luks-device)
151 (close close-luks-device)))
152
153 (define (open-raid-device sources target)
154 "Return a gexp that assembles SOURCES (a list of devices) to the RAID device
155 TARGET (e.g., \"/dev/md0\"), using 'mdadm'."
156 #~(let ((sources '#$sources)
157
158 ;; XXX: We're not at the top level here. We could use a
159 ;; non-top-level 'use-modules' form but that doesn't work when the
160 ;; code is eval'd, like the Shepherd does.
161 (every (@ (srfi srfi-1) every))
162 (format (@ (ice-9 format) format)))
163 (let loop ((attempts 0))
164 (unless (every file-exists? sources)
165 (when (> attempts 20)
166 (error "RAID devices did not show up; bailing out"
167 sources))
168
169 (format #t "waiting for RAID source devices~{ ~a~}...~%"
170 sources)
171 (sleep 1)
172 (loop (+ 1 attempts))))
173
174 ;; Use 'mdadm-static' rather than 'mdadm' to avoid pulling its whole
175 ;; closure (80 MiB) in the initrd when a RAID device is needed for boot.
176 (zero? (apply system* #$(file-append mdadm-static "/sbin/mdadm")
177 "--assemble" #$target sources))))
178
179 (define (close-raid-device sources target)
180 "Return a gexp that stops the RAID device TARGET."
181 #~(zero? (system* #$(file-append mdadm-static "/sbin/mdadm")
182 "--stop" #$target)))
183
184 (define raid-device-mapping
185 ;; The type of RAID mapped devices.
186 (mapped-device-kind
187 (open open-raid-device)
188 (close close-raid-device)))
189
190 ;;; mapped-devices.scm ends here