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