gnu: Add GoAccess.
[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>
060d62a7
LC
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)
374f14c2 21 #:use-module (guix gexp)
060d62a7 22 #:use-module (guix records)
239c6e27 23 #:use-module (guix modules)
1ea507bc
LC
24 #:use-module (gnu services)
25 #:use-module (gnu services shepherd)
374f14c2 26 #:autoload (gnu packages cryptsetup) (cryptsetup)
97c8aef1 27 #:autoload (gnu packages linux) (mdadm)
ffba7d49 28 #:use-module (srfi srfi-1)
1ea507bc 29 #:use-module (ice-9 match)
060d62a7
LC
30 #:export (mapped-device
31 mapped-device?
32 mapped-device-source
33 mapped-device-target
34 mapped-device-type
35
36 mapped-device-kind
37 mapped-device-kind?
38 mapped-device-kind-open
374f14c2
LC
39 mapped-device-kind-close
40
1ea507bc
LC
41 device-mapping-service-type
42 device-mapping-service
43
97c8aef1
AE
44 luks-device-mapping
45 raid-device-mapping))
060d62a7
LC
46
47;;; Commentary:
48;;;
49;;; This module supports "device mapping", a concept implemented by Linux's
50;;; device-mapper.
51;;;
52;;; Code:
53
54(define-record-type* <mapped-device> mapped-device
55 make-mapped-device
56 mapped-device?
57 (source mapped-device-source) ;string
58 (target mapped-device-target) ;string
59 (type mapped-device-type)) ;<mapped-device-kind>
60
61(define-record-type* <mapped-device-type> mapped-device-kind
62 make-mapped-device-kind
63 mapped-device-kind?
64 (open mapped-device-kind-open) ;source target -> gexp
65 (close mapped-device-kind-close ;source target -> gexp
66 (default (const #~(const #f)))))
67
374f14c2 68\f
1ea507bc
LC
69;;;
70;;; Device mapping as a Shepherd service.
71;;;
72
73(define device-mapping-service-type
74 (shepherd-service-type
75 'device-mapping
76 (match-lambda
4da8c19e
LC
77 (($ <mapped-device> source target
78 ($ <mapped-device-type> open close))
1ea507bc
LC
79 (shepherd-service
80 (provision (list (symbol-append 'device-mapping- (string->symbol target))))
81 (requirement '(udev))
82 (documentation "Map a device node using Linux's device mapper.")
4da8c19e
LC
83 (start #~(lambda () #$(open source target)))
84 (stop #~(lambda _ (not #$(close source target))))
dfe06f6e 85 (respawn? #f))))))
1ea507bc 86
4da8c19e
LC
87(define (device-mapping-service mapped-device)
88 "Return a service that sets up @var{mapped-device}."
89 (service device-mapping-service-type mapped-device))
1ea507bc
LC
90
91\f
374f14c2
LC
92;;;
93;;; Common device mappings.
94;;;
95
96(define (open-luks-device source target)
97 "Return a gexp that maps SOURCE to TARGET as a LUKS device, using
98'cryptsetup'."
239c6e27
LC
99 (with-imported-modules (source-module-closure
100 '((gnu build file-systems)))
a91c3fc7 101 #~(let ((source #$source))
dfe06f6e
LC
102 ;; XXX: 'use-modules' should be at the top level.
103 (use-modules (rnrs bytevectors) ;bytevector?
104 ((gnu build file-systems)
105 #:select (find-partition-by-luks-uuid)))
106
a91c3fc7
LC
107 (zero? (system* (string-append #$cryptsetup "/sbin/cryptsetup")
108 "open" "--type" "luks"
109
110 ;; Note: We cannot use the "UUID=source" syntax here
111 ;; because 'cryptsetup' implements it by searching the
112 ;; udev-populated /dev/disk/by-id directory but udev may
113 ;; be unavailable at the time we run this.
114 (if (bytevector? source)
115 (or (find-partition-by-luks-uuid source)
116 (error "LUKS partition not found" source))
117 source)
118
119 #$target)))))
374f14c2
LC
120
121(define (close-luks-device source target)
122 "Return a gexp that closes TARGET, a LUKS device."
123 #~(zero? (system* (string-append #$cryptsetup "/sbin/cryptsetup")
124 "close" #$target)))
125
126(define luks-device-mapping
127 ;; The type of LUKS mapped devices.
128 (mapped-device-kind
129 (open open-luks-device)
130 (close close-luks-device)))
131
7f8ad82b
LC
132(define (open-raid-device sources target)
133 "Return a gexp that assembles SOURCES (a list of devices) to the RAID device
134TARGET (e.g., \"/dev/md0\"), using 'mdadm'."
10618627
LC
135 #~(let ((sources '#$sources)
136
137 ;; XXX: We're not at the top level here. We could use a
138 ;; non-top-level 'use-modules' form but that doesn't work when the
139 ;; code is eval'd, like the Shepherd does.
140 (every (@ (srfi srfi-1) every))
141 (format (@ (ice-9 format) format)))
142 (let loop ((attempts 0))
143 (unless (every file-exists? sources)
144 (when (> attempts 20)
145 (error "RAID devices did not show up; bailing out"
146 sources))
147
148 (format #t "waiting for RAID source devices~{ ~a~}...~%"
149 sources)
150 (sleep 1)
151 (loop (+ 1 attempts))))
152
153 (zero? (system* (string-append #$mdadm "/sbin/mdadm")
154 "--assemble" #$target sources))))
7f8ad82b
LC
155
156(define (close-raid-device sources target)
97c8aef1
AE
157 "Return a gexp that stops the RAID device TARGET."
158 #~(zero? (system* (string-append #$mdadm "/sbin/mdadm")
159 "--stop" #$target)))
160
161(define raid-device-mapping
162 ;; The type of RAID mapped devices.
163 (mapped-device-kind
164 (open open-raid-device)
165 (close close-raid-device)))
166
060d62a7 167;;; mapped-devices.scm ends here