mapped-devices.scm 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2014-2022 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. (guix build utils))) ;; For mkdir-p
  172. (match targets
  173. ((target)
  174. #~(let ((source #$(if (uuid? source)
  175. (uuid-bytevector source)
  176. source)))
  177. ;; XXX: 'use-modules' should be at the top level.
  178. (use-modules (rnrs bytevectors) ;bytevector?
  179. ((gnu build file-systems)
  180. #:select (find-partition-by-luks-uuid
  181. system*/tty))
  182. ((guix build utils) #:select (mkdir-p)))
  183. ;; Create '/run/cryptsetup/' if it does not exist, as device locking
  184. ;; is mandatory for LUKS2.
  185. (mkdir-p "/run/cryptsetup/")
  186. ;; Use 'cryptsetup-static', not 'cryptsetup', to avoid pulling the
  187. ;; whole world inside the initrd (for when we're in an initrd).
  188. ;; 'cryptsetup open' requires standard input to be a tty to allow
  189. ;; for interaction but shepherd sets standard input to /dev/null;
  190. ;; thus, explicitly request a tty.
  191. (zero? (system*/tty
  192. #$(file-append cryptsetup-static "/sbin/cryptsetup")
  193. "open" "--type" "luks"
  194. ;; Note: We cannot use the "UUID=source" syntax here
  195. ;; because 'cryptsetup' implements it by searching the
  196. ;; udev-populated /dev/disk/by-id directory but udev may
  197. ;; be unavailable at the time we run this.
  198. (if (bytevector? source)
  199. (or (let loop ((tries-left 10))
  200. (and (positive? tries-left)
  201. (or (find-partition-by-luks-uuid source)
  202. ;; If the underlying partition is
  203. ;; not found, try again after
  204. ;; waiting a second, up to ten
  205. ;; times. FIXME: This should be
  206. ;; dealt with in a more robust way.
  207. (begin (sleep 1)
  208. (loop (- tries-left 1))))))
  209. (error "LUKS partition not found" source))
  210. source)
  211. #$target)))))))
  212. (define (close-luks-device source targets)
  213. "Return a gexp that closes TARGET, a LUKS device."
  214. (match targets
  215. ((target)
  216. #~(zero? (system* #$(file-append cryptsetup-static "/sbin/cryptsetup")
  217. "close" #$target)))))
  218. (define* (check-luks-device md #:key
  219. needed-for-boot?
  220. (initrd-modules '())
  221. #:allow-other-keys
  222. #:rest rest)
  223. "Ensure the source of MD is valid."
  224. (let ((source (mapped-device-source md))
  225. (location (mapped-device-location md)))
  226. (or (not (zero? (getuid)))
  227. (if (uuid? source)
  228. (match (find-partition-by-luks-uuid (uuid-bytevector source))
  229. (#f
  230. (raise (make-compound-condition
  231. (formatted-message (G_ "no LUKS partition with UUID '~a'")
  232. (uuid->string source))
  233. (condition
  234. (&error-location
  235. (location (source-properties->location
  236. (mapped-device-location md))))))))
  237. ((? string? device)
  238. (check-device-initrd-modules device initrd-modules location)))
  239. (check-device-initrd-modules source initrd-modules location)))))
  240. (define luks-device-mapping
  241. ;; The type of LUKS mapped devices.
  242. (mapped-device-kind
  243. (open open-luks-device)
  244. (close close-luks-device)
  245. (check check-luks-device)))
  246. (define (open-raid-device sources targets)
  247. "Return a gexp that assembles SOURCES (a list of devices) to the RAID device
  248. TARGET (e.g., \"/dev/md0\"), using 'mdadm'."
  249. (match targets
  250. ((target)
  251. #~(let ((sources '#$sources)
  252. ;; XXX: We're not at the top level here. We could use a
  253. ;; non-top-level 'use-modules' form but that doesn't work when the
  254. ;; code is eval'd, like the Shepherd does.
  255. (every (@ (srfi srfi-1) every))
  256. (format (@ (ice-9 format) format)))
  257. (let loop ((attempts 0))
  258. (unless (every file-exists? sources)
  259. (when (> attempts 20)
  260. (error "RAID devices did not show up; bailing out"
  261. sources))
  262. (format #t "waiting for RAID source devices~{ ~a~}...~%"
  263. sources)
  264. (sleep 1)
  265. (loop (+ 1 attempts))))
  266. ;; Use 'mdadm-static' rather than 'mdadm' to avoid pulling its whole
  267. ;; closure (80 MiB) in the initrd when a RAID device is needed for boot.
  268. (zero? (apply system* #$(file-append mdadm-static "/sbin/mdadm")
  269. "--assemble" #$target sources))))))
  270. (define (close-raid-device sources targets)
  271. "Return a gexp that stops the RAID device TARGET."
  272. (match targets
  273. ((target)
  274. #~(zero? (system* #$(file-append mdadm-static "/sbin/mdadm")
  275. "--stop" #$target)))))
  276. (define raid-device-mapping
  277. ;; The type of RAID mapped devices.
  278. (mapped-device-kind
  279. (open open-raid-device)
  280. (close close-raid-device)))
  281. (define (open-lvm-device source targets)
  282. #~(and
  283. (zero? (system* #$(file-append lvm2-static "/sbin/lvm")
  284. "vgchange" "--activate" "ay" #$source))
  285. ; /dev/mapper nodes are usually created by udev, but udev may be unavailable at the time we run this. So we create them here.
  286. (zero? (system* #$(file-append lvm2-static "/sbin/lvm")
  287. "vgscan" "--mknodes"))
  288. (every file-exists? (map (lambda (file) (string-append "/dev/mapper/" file))
  289. '#$targets))))
  290. (define (close-lvm-device source targets)
  291. #~(zero? (system* #$(file-append lvm2-static "/sbin/lvm")
  292. "vgchange" "--activate" "n" #$source)))
  293. (define lvm-device-mapping
  294. (mapped-device-kind
  295. (open open-lvm-device)
  296. (close close-lvm-device)))
  297. ;;; mapped-devices.scm ends here