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