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>
6 ;;; This file is part of GNU Guix.
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.
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.
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/>.
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
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
47 mapped-device-location
51 mapped-device-kind-open
52 mapped-device-kind-close
53 mapped-device-kind-check
55 device-mapping-service-type
56 device-mapping-service
63 ;;; This module supports "device mapping", a concept implemented by Linux's
68 (define-record-type* <mapped-device> 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)))
77 (define-record-type* <mapped-device-type> mapped-device-kind
78 make-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))))
88 ;;; Device mapping as a Shepherd service.
91 (define device-mapping-service-type
92 (shepherd-service-type
95 (($ <mapped-device> source target
96 ($ <mapped-device-type> open close))
98 (provision (list (symbol-append 'device-mapping- (string->symbol target))))
100 (documentation "Map a device node using Linux's device mapper.")
101 (start #~(lambda () #$(open source target)))
102 (stop #~(lambda _ (not #$(close source target))))
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))
111 ;;; Common device mappings.
114 (define (open-luks-device source target)
115 "Return a gexp that maps SOURCE to TARGET as a LUKS device, using
117 (with-imported-modules (source-module-closure
118 '((gnu build file-systems)))
119 #~(let ((source #$(if (uuid? source)
120 (uuid-bytevector 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)))
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"
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.
146 (loop (- tries-left 1))))))
147 (error "LUKS partition not found" source))
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")
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)
166 (message (format #f (G_ "you may need these modules \
167 in the initrd for ~a:~{ ~a~}")
170 (location (source-properties->location location))))))))
172 (define* (check-luks-device md #:key
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)))
182 (match (find-partition-by-luks-uuid (uuid-bytevector source))
186 (message (format #f (G_ "no LUKS partition with UUID '~a'")
187 (uuid->string source))))
189 (location (source-properties->location
190 (mapped-device-location md)))))))
192 (check-device-initrd-modules device initrd-modules location)))
193 (check-device-initrd-modules source initrd-modules location)))))
195 (define luks-device-mapping
196 ;; The type of LUKS mapped devices.
198 (open open-luks-device)
199 (close close-luks-device)
200 (check check-luks-device)))
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)
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"
218 (format #t "waiting for RAID source devices~{ ~a~}...~%"
221 (loop (+ 1 attempts))))
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))))
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")
233 (define raid-device-mapping
234 ;; The type of RAID mapped devices.
236 (open open-raid-device)
237 (close close-raid-device)))
239 ;;; mapped-devices.scm ends here