mapped-devices: Make RAID device opening message clearer.
[jackhill/guix/guix.git] / gnu / system / mapped-devices.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2014, 2015, 2016 Ludovic Courtès <ludo@gnu.org>
3 ;;; Copyright © 2016 Andreas Enge <andreas@enge.fr>
4 ;;;
5 ;;; This file is part of GNU Guix.
6 ;;;
7 ;;; GNU Guix is free software; you can redistribute it and/or modify it
8 ;;; under the terms of the GNU General Public License as published by
9 ;;; the Free Software Foundation; either version 3 of the License, or (at
10 ;;; your option) any later version.
11 ;;;
12 ;;; GNU Guix is distributed in the hope that it will be useful, but
13 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;;; GNU General Public License for more details.
16 ;;;
17 ;;; You should have received a copy of the GNU General Public License
18 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
19
20 (define-module (gnu system mapped-devices)
21 #:use-module (guix gexp)
22 #:use-module (guix records)
23 #:use-module (gnu services)
24 #:use-module (gnu services shepherd)
25 #:autoload (gnu packages cryptsetup) (cryptsetup)
26 #:autoload (gnu packages linux) (mdadm)
27 #:use-module (srfi srfi-1)
28 #:use-module (ice-9 match)
29 #:export (mapped-device
30 mapped-device?
31 mapped-device-source
32 mapped-device-target
33 mapped-device-type
34
35 mapped-device-kind
36 mapped-device-kind?
37 mapped-device-kind-open
38 mapped-device-kind-close
39
40 device-mapping-service-type
41 device-mapping-service
42
43 luks-device-mapping
44 raid-device-mapping))
45
46 ;;; Commentary:
47 ;;;
48 ;;; This module supports "device mapping", a concept implemented by Linux's
49 ;;; device-mapper.
50 ;;;
51 ;;; Code:
52
53 (define-record-type* <mapped-device> mapped-device
54 make-mapped-device
55 mapped-device?
56 (source mapped-device-source) ;string
57 (target mapped-device-target) ;string
58 (type mapped-device-type)) ;<mapped-device-kind>
59
60 (define-record-type* <mapped-device-type> mapped-device-kind
61 make-mapped-device-kind
62 mapped-device-kind?
63 (open mapped-device-kind-open) ;source target -> gexp
64 (close mapped-device-kind-close ;source target -> gexp
65 (default (const #~(const #f)))))
66
67 \f
68 ;;;
69 ;;; Device mapping as a Shepherd service.
70 ;;;
71
72 (define device-mapping-service-type
73 (shepherd-service-type
74 'device-mapping
75 (match-lambda
76 (($ <mapped-device> source target
77 ($ <mapped-device-type> open close))
78 (shepherd-service
79 (provision (list (symbol-append 'device-mapping- (string->symbol target))))
80 (requirement '(udev))
81 (documentation "Map a device node using Linux's device mapper.")
82 (start #~(lambda () #$(open source target)))
83 (stop #~(lambda _ (not #$(close source target))))
84 (respawn? #f))))))
85
86 (define (device-mapping-service mapped-device)
87 "Return a service that sets up @var{mapped-device}."
88 (service device-mapping-service-type mapped-device))
89
90 \f
91 ;;;
92 ;;; Common device mappings.
93 ;;;
94
95 (define (open-luks-device source target)
96 "Return a gexp that maps SOURCE to TARGET as a LUKS device, using
97 'cryptsetup'."
98 (with-imported-modules '((gnu build file-systems)
99 (guix build bournish))
100 #~(let ((source #$source))
101 ;; XXX: 'use-modules' should be at the top level.
102 (use-modules (rnrs bytevectors) ;bytevector?
103 ((gnu build file-systems)
104 #:select (find-partition-by-luks-uuid)))
105
106 (zero? (system* (string-append #$cryptsetup "/sbin/cryptsetup")
107 "open" "--type" "luks"
108
109 ;; Note: We cannot use the "UUID=source" syntax here
110 ;; because 'cryptsetup' implements it by searching the
111 ;; udev-populated /dev/disk/by-id directory but udev may
112 ;; be unavailable at the time we run this.
113 (if (bytevector? source)
114 (or (find-partition-by-luks-uuid source)
115 (error "LUKS partition not found" source))
116 source)
117
118 #$target)))))
119
120 (define (close-luks-device source target)
121 "Return a gexp that closes TARGET, a LUKS device."
122 #~(zero? (system* (string-append #$cryptsetup "/sbin/cryptsetup")
123 "close" #$target)))
124
125 (define luks-device-mapping
126 ;; The type of LUKS mapped devices.
127 (mapped-device-kind
128 (open open-luks-device)
129 (close close-luks-device)))
130
131 (define (open-raid-device sources target)
132 "Return a gexp that assembles SOURCES (a list of devices) to the RAID device
133 TARGET (e.g., \"/dev/md0\"), using 'mdadm'."
134 #~(begin
135 (use-modules (srfi srfi-1) (ice-9 format))
136
137 (let ((sources '#$sources))
138 (let loop ()
139 (unless (every file-exists? sources)
140 (format #t "waiting for RAID source devices~{ ~a~}...~%"
141 sources)
142 (sleep 1)
143 (loop)))
144
145 (zero? (system* (string-append #$mdadm "/sbin/mdadm")
146 "--assemble" #$target sources)))))
147
148 (define (close-raid-device sources target)
149 "Return a gexp that stops the RAID device TARGET."
150 #~(zero? (system* (string-append #$mdadm "/sbin/mdadm")
151 "--stop" #$target)))
152
153 (define raid-device-mapping
154 ;; The type of RAID mapped devices.
155 (mapped-device-kind
156 (open open-raid-device)
157 (close close-raid-device)))
158
159 ;;; mapped-devices.scm ends here