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>
5 ;;; This file is part of GNU Guix.
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.
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.
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/>.
20 (define-module (gnu system mapped-devices)
21 #:use-module (guix gexp)
22 #:use-module (guix records)
23 #:use-module (guix modules)
24 #:use-module (gnu services)
25 #:use-module (gnu services shepherd)
26 #:autoload (gnu packages cryptsetup) (cryptsetup)
27 #:autoload (gnu packages linux) (mdadm)
28 #:use-module (srfi srfi-1)
29 #:use-module (ice-9 match)
30 #:export (mapped-device
38 mapped-device-kind-open
39 mapped-device-kind-close
41 device-mapping-service-type
42 device-mapping-service
49 ;;; This module supports "device mapping", a concept implemented by Linux's
54 (define-record-type* <mapped-device> mapped-device
57 (source mapped-device-source) ;string
58 (target mapped-device-target) ;string
59 (type mapped-device-type)) ;<mapped-device-kind>
61 (define-record-type* <mapped-device-type> mapped-device-kind
62 make-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)))))
70 ;;; Device mapping as a Shepherd service.
73 (define device-mapping-service-type
74 (shepherd-service-type
77 (($ <mapped-device> source target
78 ($ <mapped-device-type> open close))
80 (provision (list (symbol-append 'device-mapping- (string->symbol target))))
82 (documentation "Map a device node using Linux's device mapper.")
83 (start #~(lambda () #$(open source target)))
84 (stop #~(lambda _ (not #$(close source target))))
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))
93 ;;; Common device mappings.
96 (define (open-luks-device source target)
97 "Return a gexp that maps SOURCE to TARGET as a LUKS device, using
99 (with-imported-modules (source-module-closure
100 '((gnu build file-systems)))
101 #~(let ((source #$source))
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)))
107 (zero? (system* (string-append #$cryptsetup "/sbin/cryptsetup")
108 "open" "--type" "luks"
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))
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")
126 (define luks-device-mapping
127 ;; The type of LUKS mapped devices.
129 (open open-luks-device)
130 (close close-luks-device)))
132 (define (open-raid-device sources target)
133 "Return a gexp that assembles SOURCES (a list of devices) to the RAID device
134 TARGET (e.g., \"/dev/md0\"), using 'mdadm'."
135 #~(let ((sources '#$sources)
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"
148 (format #t "waiting for RAID source devices~{ ~a~}...~%"
151 (loop (+ 1 attempts))))
153 (zero? (system* (string-append #$mdadm "/sbin/mdadm")
154 "--assemble" #$target sources))))
156 (define (close-raid-device sources target)
157 "Return a gexp that stops the RAID device TARGET."
158 #~(zero? (system* (string-append #$mdadm "/sbin/mdadm")
161 (define raid-device-mapping
162 ;; The type of RAID mapped devices.
164 (open open-raid-device)
165 (close close-raid-device)))
167 ;;; mapped-devices.scm ends here