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 (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
37 mapped-device-kind-open
38 mapped-device-kind-close
40 device-mapping-service-type
41 device-mapping-service
48 ;;; This module supports "device mapping", a concept implemented by Linux's
53 (define-record-type* <mapped-device> mapped-device
56 (source mapped-device-source) ;string
57 (target mapped-device-target) ;string
58 (type mapped-device-type)) ;<mapped-device-kind>
60 (define-record-type* <mapped-device-type> mapped-device-kind
61 make-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)))))
69 ;;; Device mapping as a Shepherd service.
72 (define device-mapping-service-type
73 (shepherd-service-type
76 (($ <mapped-device> source target
77 ($ <mapped-device-type> open close))
79 (provision (list (symbol-append 'device-mapping- (string->symbol target))))
81 (documentation "Map a device node using Linux's device mapper.")
82 (start #~(lambda () #$(open source target)))
83 (stop #~(lambda _ (not #$(close source target))))
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))
92 ;;; Common device mappings.
95 (define (open-luks-device source target)
96 "Return a gexp that maps SOURCE to TARGET as a LUKS device, using
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)))
106 (zero? (system* (string-append #$cryptsetup "/sbin/cryptsetup")
107 "open" "--type" "luks"
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))
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")
125 (define luks-device-mapping
126 ;; The type of LUKS mapped devices.
128 (open open-luks-device)
129 (close close-luks-device)))
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'."
135 (use-modules (srfi srfi-1) (ice-9 format))
137 (let ((sources '#$sources))
139 (unless (every file-exists? sources)
140 (format #t "waiting for RAID source devices~{ ~a~}...~%"
145 (zero? (system* (string-append #$mdadm "/sbin/mdadm")
146 "--assemble" #$target sources)))))
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")
153 (define raid-device-mapping
154 ;; The type of RAID mapped devices.
156 (open open-raid-device)
157 (close close-raid-device)))
159 ;;; mapped-devices.scm ends here