mapped-devices.scm 13 KB

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