mapped-devices.scm 13 KB

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