Commit | Line | Data |
---|---|---|
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) |
1ea507bc LC |
23 | #:use-module (gnu services) |
24 | #:use-module (gnu services shepherd) | |
374f14c2 | 25 | #:autoload (gnu packages cryptsetup) (cryptsetup) |
97c8aef1 | 26 | #:autoload (gnu packages linux) (mdadm) |
ffba7d49 | 27 | #:use-module (srfi srfi-1) |
1ea507bc | 28 | #:use-module (ice-9 match) |
060d62a7 LC |
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 | |
374f14c2 LC |
38 | mapped-device-kind-close |
39 | ||
1ea507bc LC |
40 | device-mapping-service-type |
41 | device-mapping-service | |
42 | ||
97c8aef1 AE |
43 | luks-device-mapping |
44 | raid-device-mapping)) | |
060d62a7 LC |
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 | ||
374f14c2 | 67 | \f |
1ea507bc LC |
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 | |
4da8c19e LC |
76 | (($ <mapped-device> source target |
77 | ($ <mapped-device-type> open close)) | |
1ea507bc LC |
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.") | |
4da8c19e LC |
82 | (start #~(lambda () #$(open source target))) |
83 | (stop #~(lambda _ (not #$(close source target)))) | |
dfe06f6e | 84 | (respawn? #f)))))) |
1ea507bc | 85 | |
4da8c19e LC |
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)) | |
1ea507bc LC |
89 | |
90 | \f | |
374f14c2 LC |
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'." | |
a91c3fc7 LC |
98 | (with-imported-modules '((gnu build file-systems) |
99 | (guix build bournish)) | |
100 | #~(let ((source #$source)) | |
dfe06f6e LC |
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 | ||
a91c3fc7 LC |
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))))) | |
374f14c2 LC |
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 | ||
7f8ad82b LC |
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)) | |
873b51b3 | 138 | (let loop ((attempts 0)) |
7f8ad82b | 139 | (unless (every file-exists? sources) |
873b51b3 LC |
140 | (when (> attempts 20) |
141 | (error "RAID devices did not show up; bailing out" | |
142 | sources)) | |
143 | ||
7f8ad82b LC |
144 | (format #t "waiting for RAID source devices~{ ~a~}...~%" |
145 | sources) | |
146 | (sleep 1) | |
873b51b3 | 147 | (loop (+ 1 attempts)))) |
7f8ad82b LC |
148 | |
149 | (zero? (system* (string-append #$mdadm "/sbin/mdadm") | |
150 | "--assemble" #$target sources))))) | |
151 | ||
152 | (define (close-raid-device sources target) | |
97c8aef1 AE |
153 | "Return a gexp that stops the RAID device TARGET." |
154 | #~(zero? (system* (string-append #$mdadm "/sbin/mdadm") | |
155 | "--stop" #$target))) | |
156 | ||
157 | (define raid-device-mapping | |
158 | ;; The type of RAID mapped devices. | |
159 | (mapped-device-kind | |
160 | (open open-raid-device) | |
161 | (close close-raid-device))) | |
162 | ||
060d62a7 | 163 | ;;; mapped-devices.scm ends here |