mapped-devices.scm 11 KB

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