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-static)
27 #:autoload (gnu packages linux) (mdadm-static)
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 ;; Use 'cryptsetup-static', not 'cryptsetup', to avoid pulling the
108 ;; whole world inside the initrd (for when we're in an initrd).
109 (zero? (system* #$(file-append cryptsetup-static "/sbin/cryptsetup")
110 "open" "--type" "luks"
112 ;; Note: We cannot use the "UUID=source" syntax here
113 ;; because 'cryptsetup' implements it by searching the
114 ;; udev-populated /dev/disk/by-id directory but udev may
115 ;; be unavailable at the time we run this.
116 (if (bytevector? source)
117 (or (find-partition-by-luks-uuid source)
118 (error "LUKS partition not found" source))
123 (define (close-luks-device source target)
124 "Return a gexp that closes TARGET, a LUKS device."
125 #~(zero? (system* #$(file-append cryptsetup-static "/sbin/cryptsetup")
128 (define luks-device-mapping
129 ;; The type of LUKS mapped devices.
131 (open open-luks-device)
132 (close close-luks-device)))
134 (define (open-raid-device sources target)
135 "Return a gexp that assembles SOURCES (a list of devices) to the RAID device
136 TARGET (e.g., \"/dev/md0\"), using 'mdadm'."
137 #~(let ((sources '#$sources)
139 ;; XXX: We're not at the top level here. We could use a
140 ;; non-top-level 'use-modules' form but that doesn't work when the
141 ;; code is eval'd, like the Shepherd does.
142 (every (@ (srfi srfi-1) every))
143 (format (@ (ice-9 format) format)))
144 (let loop ((attempts 0))
145 (unless (every file-exists? sources)
146 (when (> attempts 20)
147 (error "RAID devices did not show up; bailing out"
150 (format #t "waiting for RAID source devices~{ ~a~}...~%"
153 (loop (+ 1 attempts))))
155 ;; Use 'mdadm-static' rather than 'mdadm' to avoid pulling its whole
156 ;; closure (80 MiB) in the initrd when a RAID device is needed for boot.
157 (zero? (apply system* #$(file-append mdadm-static "/sbin/mdadm")
158 "--assemble" #$target sources))))
160 (define (close-raid-device sources target)
161 "Return a gexp that stops the RAID device TARGET."
162 #~(zero? (system* #$(file-append mdadm-static "/sbin/mdadm")
165 (define raid-device-mapping
166 ;; The type of RAID mapped devices.
168 (open open-raid-device)
169 (close close-raid-device)))
171 ;;; mapped-devices.scm ends here