mapped-devices.scm 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2014, 2015, 2016, 2017, 2018 Ludovic Courtès <ludo@gnu.org>
  3. ;;; Copyright © 2016 Andreas Enge <andreas@enge.fr>
  4. ;;; Copyright © 2017, 2018 Mark H Weaver <mhw@netris.org>
  5. ;;;
  6. ;;; This file is part of GNU Guix.
  7. ;;;
  8. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  9. ;;; under the terms of the GNU General Public License as published by
  10. ;;; the Free Software Foundation; either version 3 of the License, or (at
  11. ;;; your option) any later version.
  12. ;;;
  13. ;;; GNU Guix is distributed in the hope that it will be useful, but
  14. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. ;;; GNU General Public License for more details.
  17. ;;;
  18. ;;; You should have received a copy of the GNU General Public License
  19. ;;; 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) #:hide (file-name->module-name))
  24. #:use-module (guix i18n)
  25. #:use-module ((guix utils)
  26. #:select (source-properties->location
  27. &fix-hint
  28. &error-location))
  29. #:use-module (gnu services)
  30. #:use-module (gnu services shepherd)
  31. #:use-module (gnu system uuid)
  32. #:autoload (gnu build file-systems) (find-partition-by-luks-uuid)
  33. #:autoload (gnu build linux-modules)
  34. (device-module-aliases matching-modules known-module-aliases
  35. normalize-module-name file-name->module-name)
  36. #:autoload (gnu packages cryptsetup) (cryptsetup-static)
  37. #:autoload (gnu packages linux) (mdadm-static)
  38. #:use-module (srfi srfi-1)
  39. #:use-module (srfi srfi-26)
  40. #:use-module (srfi srfi-34)
  41. #:use-module (srfi srfi-35)
  42. #:use-module (ice-9 match)
  43. #:export (mapped-device
  44. mapped-device?
  45. mapped-device-source
  46. mapped-device-target
  47. mapped-device-type
  48. mapped-device-location
  49. mapped-device-kind
  50. mapped-device-kind?
  51. mapped-device-kind-open
  52. mapped-device-kind-close
  53. mapped-device-kind-check
  54. device-mapping-service-type
  55. device-mapping-service
  56. check-device-initrd-modules ;XXX: needs a better place
  57. luks-device-mapping
  58. raid-device-mapping))
  59. ;;; Commentary:
  60. ;;;
  61. ;;; This module supports "device mapping", a concept implemented by Linux's
  62. ;;; device-mapper.
  63. ;;;
  64. ;;; Code:
  65. (define-record-type* <mapped-device> mapped-device
  66. make-mapped-device
  67. mapped-device?
  68. (source mapped-device-source) ;string | list of strings
  69. (target mapped-device-target) ;string
  70. (type mapped-device-type) ;<mapped-device-kind>
  71. (location mapped-device-location
  72. (default (current-source-location)) (innate)))
  73. (define-record-type* <mapped-device-type> mapped-device-kind
  74. make-mapped-device-kind
  75. mapped-device-kind?
  76. (open mapped-device-kind-open) ;source target -> gexp
  77. (close mapped-device-kind-close ;source target -> gexp
  78. (default (const #~(const #f))))
  79. (check mapped-device-kind-check ;source -> Boolean
  80. (default (const #t))))
  81. ;;;
  82. ;;; Device mapping as a Shepherd service.
  83. ;;;
  84. (define device-mapping-service-type
  85. (shepherd-service-type
  86. 'device-mapping
  87. (match-lambda
  88. (($ <mapped-device> source target
  89. ($ <mapped-device-type> open close))
  90. (shepherd-service
  91. (provision (list (symbol-append 'device-mapping- (string->symbol target))))
  92. (requirement '(udev))
  93. (documentation "Map a device node using Linux's device mapper.")
  94. (start #~(lambda () #$(open source target)))
  95. (stop #~(lambda _ (not #$(close source target))))
  96. (respawn? #f))))))
  97. (define (device-mapping-service mapped-device)
  98. "Return a service that sets up @var{mapped-device}."
  99. (service device-mapping-service-type mapped-device))
  100. ;;;
  101. ;;; Static checks.
  102. ;;;
  103. (define (check-device-initrd-modules device linux-modules location)
  104. "Raise an error if DEVICE needs modules beyond LINUX-MODULES to operate.
  105. DEVICE must be a \"/dev\" file name."
  106. (define aliases
  107. ;; Attempt to load 'modules.alias' from the current kernel, assuming we're
  108. ;; on GuixSD, and assuming that corresponds to the kernel we'll be
  109. ;; installing. Skip the whole thing if that file cannot be read.
  110. (catch 'system-error
  111. (lambda ()
  112. (known-module-aliases))
  113. (const #f)))
  114. (when aliases
  115. (let* ((modules (delete-duplicates
  116. (append-map (cut matching-modules <> aliases)
  117. (device-module-aliases device))))
  118. ;; Module names (not file names) are supposed to use underscores
  119. ;; instead of hyphens. MODULES is a list of module names, whereas
  120. ;; LINUX-MODULES is file names without '.ko', so normalize them.
  121. (provided (map file-name->module-name linux-modules))
  122. (missing (remove (cut member <> provided) modules)))
  123. (unless (null? missing)
  124. ;; Note: What we suggest here is a list of module names (e.g.,
  125. ;; "usb_storage"), not file names (e.g., "usb-storage.ko"). This is
  126. ;; OK because we have machinery that accepts both the hyphen and the
  127. ;; underscore version.
  128. (raise (condition
  129. (&message
  130. (message (format #f (G_ "you may need these modules \
  131. in the initrd for ~a:~{ ~a~}")
  132. device missing)))
  133. (&fix-hint
  134. (hint (format #f (G_ "Try adding them to the
  135. @code{initrd-modules} field of your @code{operating-system} declaration, along
  136. these lines:
  137. @example
  138. (operating-system
  139. ;; @dots{}
  140. (initrd-modules (append (list~{ ~s~})
  141. %base-initrd-modules)))
  142. @end example
  143. If you think this diagnostic is inaccurate, use the @option{--skip-checks}
  144. option of @command{guix system}.\n")
  145. missing)))
  146. (&error-location
  147. (location (source-properties->location location)))))))))
  148. ;;;
  149. ;;; Common device mappings.
  150. ;;;
  151. (define (open-luks-device source target)
  152. "Return a gexp that maps SOURCE to TARGET as a LUKS device, using
  153. 'cryptsetup'."
  154. (with-imported-modules (source-module-closure
  155. '((gnu build file-systems)))
  156. #~(let ((source #$(if (uuid? source)
  157. (uuid-bytevector source)
  158. source)))
  159. ;; XXX: 'use-modules' should be at the top level.
  160. (use-modules (rnrs bytevectors) ;bytevector?
  161. ((gnu build file-systems)
  162. #:select (find-partition-by-luks-uuid)))
  163. ;; Use 'cryptsetup-static', not 'cryptsetup', to avoid pulling the
  164. ;; whole world inside the initrd (for when we're in an initrd).
  165. (zero? (system* #$(file-append cryptsetup-static "/sbin/cryptsetup")
  166. "open" "--type" "luks"
  167. ;; Note: We cannot use the "UUID=source" syntax here
  168. ;; because 'cryptsetup' implements it by searching the
  169. ;; udev-populated /dev/disk/by-id directory but udev may
  170. ;; be unavailable at the time we run this.
  171. (if (bytevector? source)
  172. (or (let loop ((tries-left 10))
  173. (and (positive? tries-left)
  174. (or (find-partition-by-luks-uuid source)
  175. ;; If the underlying partition is
  176. ;; not found, try again after
  177. ;; waiting a second, up to ten
  178. ;; times. FIXME: This should be
  179. ;; dealt with in a more robust way.
  180. (begin (sleep 1)
  181. (loop (- tries-left 1))))))
  182. (error "LUKS partition not found" source))
  183. source)
  184. #$target)))))
  185. (define (close-luks-device source target)
  186. "Return a gexp that closes TARGET, a LUKS device."
  187. #~(zero? (system* #$(file-append cryptsetup-static "/sbin/cryptsetup")
  188. "close" #$target)))
  189. (define* (check-luks-device md #:key
  190. needed-for-boot?
  191. (initrd-modules '())
  192. #:allow-other-keys
  193. #:rest rest)
  194. "Ensure the source of MD is valid."
  195. (let ((source (mapped-device-source md))
  196. (location (mapped-device-location md)))
  197. (or (not (zero? (getuid)))
  198. (if (uuid? source)
  199. (match (find-partition-by-luks-uuid (uuid-bytevector source))
  200. (#f
  201. (raise (condition
  202. (&message
  203. (message (format #f (G_ "no LUKS partition with UUID '~a'")
  204. (uuid->string source))))
  205. (&error-location
  206. (location (source-properties->location
  207. (mapped-device-location md)))))))
  208. ((? string? device)
  209. (check-device-initrd-modules device initrd-modules location)))
  210. (check-device-initrd-modules source initrd-modules location)))))
  211. (define luks-device-mapping
  212. ;; The type of LUKS mapped devices.
  213. (mapped-device-kind
  214. (open open-luks-device)
  215. (close close-luks-device)
  216. (check check-luks-device)))
  217. (define (open-raid-device sources target)
  218. "Return a gexp that assembles SOURCES (a list of devices) to the RAID device
  219. TARGET (e.g., \"/dev/md0\"), using 'mdadm'."
  220. #~(let ((sources '#$sources)
  221. ;; XXX: We're not at the top level here. We could use a
  222. ;; non-top-level 'use-modules' form but that doesn't work when the
  223. ;; code is eval'd, like the Shepherd does.
  224. (every (@ (srfi srfi-1) every))
  225. (format (@ (ice-9 format) format)))
  226. (let loop ((attempts 0))
  227. (unless (every file-exists? sources)
  228. (when (> attempts 20)
  229. (error "RAID devices did not show up; bailing out"
  230. sources))
  231. (format #t "waiting for RAID source devices~{ ~a~}...~%"
  232. sources)
  233. (sleep 1)
  234. (loop (+ 1 attempts))))
  235. ;; Use 'mdadm-static' rather than 'mdadm' to avoid pulling its whole
  236. ;; closure (80 MiB) in the initrd when a RAID device is needed for boot.
  237. (zero? (apply system* #$(file-append mdadm-static "/sbin/mdadm")
  238. "--assemble" #$target sources))))
  239. (define (close-raid-device sources target)
  240. "Return a gexp that stops the RAID device TARGET."
  241. #~(zero? (system* #$(file-append mdadm-static "/sbin/mdadm")
  242. "--stop" #$target)))
  243. (define raid-device-mapping
  244. ;; The type of RAID mapped devices.
  245. (mapped-device-kind
  246. (open open-raid-device)
  247. (close close-raid-device)))
  248. ;;; mapped-devices.scm ends here