file-systems.scm 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020 Ludovic Courtès <ludo@gnu.org>
  3. ;;; Copyright © 2020 Google LLC
  4. ;;; Copyright © 2020 Jakub Kądziołka <kuba@kadziolka.net>
  5. ;;; Copyright © 2020, 2021 Maxim Cournoyer <maxim.cournoyer@gmail.com>
  6. ;;; Copyright © 2021 Tobias Geerinckx-Rice <me@tobias.gr>
  7. ;;;
  8. ;;; This file is part of GNU Guix.
  9. ;;;
  10. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  11. ;;; under the terms of the GNU General Public License as published by
  12. ;;; the Free Software Foundation; either version 3 of the License, or (at
  13. ;;; your option) any later version.
  14. ;;;
  15. ;;; GNU Guix is distributed in the hope that it will be useful, but
  16. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  17. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. ;;; GNU General Public License for more details.
  19. ;;;
  20. ;;; You should have received a copy of the GNU General Public License
  21. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  22. (define-module (gnu system file-systems)
  23. #:use-module (ice-9 match)
  24. #:use-module (rnrs bytevectors)
  25. #:use-module (srfi srfi-1)
  26. #:use-module (srfi srfi-2)
  27. #:use-module (srfi srfi-9)
  28. #:use-module (srfi srfi-26)
  29. #:use-module (srfi srfi-35)
  30. #:use-module (srfi srfi-9 gnu)
  31. #:use-module (guix records)
  32. #:use-module ((guix diagnostics) #:select (&fix-hint))
  33. #:use-module (guix i18n)
  34. #:use-module (gnu system uuid)
  35. #:re-export (uuid ;backward compatibility
  36. string->uuid
  37. uuid->string)
  38. #:export (file-system
  39. file-system?
  40. file-system-device
  41. file-system-device->string
  42. file-system-title ;deprecated
  43. file-system-mount-point
  44. file-system-type
  45. file-system-needed-for-boot?
  46. file-system-flags
  47. file-system-options
  48. file-system-options->alist
  49. alist->file-system-options
  50. file-system-mount?
  51. file-system-mount-may-fail?
  52. file-system-check?
  53. file-system-skip-check-if-clean?
  54. file-system-repair
  55. file-system-create-mount-point?
  56. file-system-dependencies
  57. file-system-location
  58. file-system-type-predicate
  59. btrfs-subvolume?
  60. btrfs-store-subvolume-file-name
  61. file-system-label
  62. file-system-label?
  63. file-system-label->string
  64. file-system->spec
  65. spec->file-system
  66. specification->file-system-mapping
  67. %pseudo-file-system-types
  68. %fuse-control-file-system
  69. %binary-format-file-system
  70. %debug-file-system
  71. %efivars-file-system
  72. %shared-memory-file-system
  73. %pseudo-terminal-file-system
  74. %tty-gid
  75. %immutable-store
  76. %control-groups
  77. %elogind-file-systems
  78. %base-file-systems
  79. %container-file-systems
  80. <file-system-mapping>
  81. file-system-mapping
  82. file-system-mapping?
  83. file-system-mapping-source
  84. file-system-mapping-target
  85. file-system-mapping-writable?
  86. file-system-mapping->bind-mount
  87. %store-mapping
  88. %network-configuration-files
  89. %network-file-mappings))
  90. ;;; Commentary:
  91. ;;;
  92. ;;; Declaring file systems to be mounted.
  93. ;;;
  94. ;;; Note: this file system is used both in the Shepherd and on the "host
  95. ;;; side", so it must not include (gnu packages …) modules.
  96. ;;;
  97. ;;; Code:
  98. ;; File system declaration.
  99. (define-record-type* <file-system> %file-system
  100. make-file-system
  101. file-system?
  102. (device file-system-device) ; string | <uuid> | <file-system-label>
  103. (mount-point file-system-mount-point) ; string
  104. (type file-system-type) ; string
  105. (flags file-system-flags ; list of symbols
  106. (default '()))
  107. (options file-system-options ; string or #f
  108. (default #f))
  109. (mount? file-system-mount? ; Boolean
  110. (default #t))
  111. (mount-may-fail? file-system-mount-may-fail? ; Boolean
  112. (default #f))
  113. (needed-for-boot? %file-system-needed-for-boot? ; Boolean
  114. (default #f))
  115. (check? file-system-check? ; Boolean
  116. (default #t))
  117. (skip-check-if-clean? file-system-skip-check-if-clean? ; Boolean
  118. (default #t))
  119. (repair file-system-repair ; symbol or #f
  120. (default 'preen))
  121. (create-mount-point? file-system-create-mount-point? ; Boolean
  122. (default #f))
  123. (dependencies file-system-dependencies ; list of <file-system>
  124. (default '())) ; or <mapped-device>
  125. (location file-system-location
  126. (default (current-source-location))
  127. (innate)))
  128. ;; A file system label for use in the 'device' field.
  129. (define-record-type <file-system-label>
  130. (file-system-label label)
  131. file-system-label?
  132. (label file-system-label->string))
  133. (set-record-type-printer! <file-system-label>
  134. (lambda (obj port)
  135. (format port "#<file-system-label ~s>"
  136. (file-system-label->string obj))))
  137. (define-syntax report-deprecation
  138. (lambda (s)
  139. "Report the use of the now-deprecated 'title' field."
  140. (syntax-case s ()
  141. ((_ field)
  142. (let* ((source (syntax-source #'field))
  143. (file (and source (assq-ref source 'filename)))
  144. (line (and source
  145. (and=> (assq-ref source 'line) 1+)))
  146. (column (and source (assq-ref source 'column))))
  147. (format (current-error-port)
  148. "~a:~a:~a: warning: 'title' field is deprecated~%"
  149. file line column)
  150. #t)))))
  151. ;; Helper for 'process-file-system-declaration'.
  152. (define-syntax device-expression
  153. (syntax-rules (quote label uuid device)
  154. ((_ (quote label) dev)
  155. (file-system-label dev))
  156. ((_ (quote uuid) dev)
  157. (if (uuid? dev) dev (uuid dev)))
  158. ((_ (quote device) dev)
  159. dev)
  160. ((_ title dev)
  161. (case title
  162. ((label) (file-system-label dev))
  163. ((uuid) (uuid dev))
  164. (else dev)))))
  165. ;; Helper to interpret the now-deprecated 'title' field. Detect forms like
  166. ;; (title 'label), remove them, and adjust the 'device' field accordingly.
  167. ;; TODO: Remove this once 'title' has been deprecated long enough.
  168. (define-syntax process-file-system-declaration
  169. (syntax-rules (device title)
  170. ((_ () (rest ...) #f #f) ;no 'title' and no 'device' field
  171. (%file-system rest ...))
  172. ((_ () (rest ...) dev #f) ;no 'title' field
  173. (%file-system rest ... (device dev)))
  174. ((_ () (rest ...) dev titl) ;got a 'title' field
  175. (%file-system rest ...
  176. (device (device-expression titl dev))))
  177. ((_ ((title titl) rest ...) (previous ...) dev _)
  178. (begin
  179. (report-deprecation (title titl))
  180. (process-file-system-declaration (rest ...)
  181. (previous ...)
  182. dev titl)))
  183. ((_ ((device dev) rest ...) (previous ...) _ titl)
  184. (process-file-system-declaration (rest ...)
  185. (previous ...)
  186. dev titl))
  187. ((_ (field rest ...) (previous ...) dev titl)
  188. (process-file-system-declaration (rest ...)
  189. (previous ... field)
  190. dev titl))))
  191. (define-syntax-rule (file-system fields ...)
  192. (process-file-system-declaration (fields ...) () #f #f))
  193. (define (file-system-title fs) ;deprecated
  194. (match (file-system-device fs)
  195. ((? file-system-label?) 'label)
  196. ((? uuid?) 'uuid)
  197. ((? string?) 'device)))
  198. ;; Note: This module is used both on the build side and on the host side.
  199. ;; Arrange not to pull (guix store) and (guix config) because the latter
  200. ;; differs from user to user.
  201. (define (%store-prefix)
  202. "Return the store prefix."
  203. ;; Note: If we have (guix store database) in the search path and we do *not*
  204. ;; have (guix store) proper, 'resolve-module' returns an empty (guix store)
  205. ;; with one sub-module.
  206. (cond ((and=> (resolve-module '(guix store) #:ensure #f)
  207. (lambda (store)
  208. (module-variable store '%store-prefix)))
  209. =>
  210. (lambda (variable)
  211. ((variable-ref variable))))
  212. ((getenv "NIX_STORE")
  213. => identity)
  214. (else
  215. "/gnu/store")))
  216. (define %not-slash
  217. (char-set-complement (char-set #\/)))
  218. (define (file-prefix? file1 file2)
  219. "Return #t if FILE1 denotes the name of a file that is a parent of FILE2.
  220. FILE1 and FILE2 must both be either absolute or relative file names, else #f
  221. is returned.
  222. For example:
  223. (file-prefix? \"/gnu\" \"/gnu/store\")
  224. => #t
  225. (file-prefix? \"/gn\" \"/gnu/store\")
  226. => #f
  227. "
  228. (define (absolute? file)
  229. (string-prefix? "/" file))
  230. (if (or (every absolute? (list file1 file2))
  231. (every (negate absolute?) (list file1 file2)))
  232. (let loop ((file1 (string-tokenize file1 %not-slash))
  233. (file2 (string-tokenize file2 %not-slash)))
  234. (match file1
  235. (()
  236. #t)
  237. ((head1 tail1 ...)
  238. (match file2
  239. ((head2 tail2 ...)
  240. (and (string=? head1 head2) (loop tail1 tail2)))
  241. (()
  242. #f)))))
  243. ;; FILE1 and FILE2 are a mix of absolute and relative file names.
  244. #f))
  245. (define (file-name-depth file-name)
  246. (length (string-tokenize file-name %not-slash)))
  247. (define* (file-system-device->string device #:key uuid-type)
  248. "Return the string representations of the DEVICE field of a <file-system>
  249. record. When the device is a UUID, its representation is chosen depending on
  250. UUID-TYPE, a symbol such as 'dce or 'iso9660."
  251. (match device
  252. ((? file-system-label?)
  253. (file-system-label->string device))
  254. ((? uuid?)
  255. (if uuid-type
  256. (uuid->string (uuid-bytevector device) uuid-type)
  257. (uuid->string device)))
  258. ((? string?)
  259. device)))
  260. (define (file-system-options->alist string)
  261. "Translate the option string format of a <file-system> record into an
  262. association list of options or option/value pairs."
  263. (if string
  264. (let ((options (string-split string #\,)))
  265. (map (lambda (param)
  266. (let ((=index (string-index param #\=)))
  267. (if =index
  268. (cons (string-take param =index)
  269. (string-drop param (1+ =index)))
  270. param)))
  271. options))
  272. '()))
  273. (define (alist->file-system-options options)
  274. "Return the string representation of OPTIONS, an association list. The
  275. string obtained can be used as the option field of a <file-system> record."
  276. (if (null? options)
  277. #f
  278. (string-join (map (match-lambda
  279. ((key . value)
  280. (string-append key "=" value))
  281. (key
  282. key))
  283. options)
  284. ",")))
  285. (define (file-system-needed-for-boot? fs)
  286. "Return true if FS has the 'needed-for-boot?' flag set, or if it holds the
  287. store--e.g., if FS is the root file system."
  288. (or (%file-system-needed-for-boot? fs)
  289. (and (file-prefix? (file-system-mount-point fs) (%store-prefix))
  290. (not (memq 'bind-mount (file-system-flags fs))))))
  291. (define (file-system->spec fs)
  292. "Return a list corresponding to file-system FS that can be passed to the
  293. initrd code."
  294. (match fs
  295. (($ <file-system> device mount-point type flags options mount?
  296. mount-may-fail? needed-for-boot?
  297. check? skip-check-if-clean? repair)
  298. ;; Note: Add new fields towards the end for compatibility.
  299. (list (cond ((uuid? device)
  300. `(uuid ,(uuid-type device) ,(uuid-bytevector device)))
  301. ((file-system-label? device)
  302. `(file-system-label ,(file-system-label->string device)))
  303. (else device))
  304. mount-point type flags options mount-may-fail?
  305. check? skip-check-if-clean? repair))))
  306. (define (spec->file-system sexp)
  307. "Deserialize SEXP, a list, to the corresponding <file-system> object."
  308. (match sexp
  309. ((device mount-point type flags options mount-may-fail?
  310. check? skip-check-if-clean? repair
  311. _ ...) ;placeholder for new fields
  312. (file-system
  313. (device (match device
  314. (('uuid (? symbol? type) (? bytevector? bv))
  315. (bytevector->uuid bv type))
  316. (('file-system-label (? string? label))
  317. (file-system-label label))
  318. (_
  319. device)))
  320. (mount-point mount-point) (type type)
  321. (flags flags) (options options)
  322. (mount-may-fail? mount-may-fail?)
  323. (check? check?)
  324. (skip-check-if-clean? skip-check-if-clean?)
  325. (repair repair)))))
  326. (define (specification->file-system-mapping spec writable?)
  327. "Read the SPEC and return the corresponding <file-system-mapping>. SPEC is
  328. a string of the form \"SOURCE\" or \"SOURCE=TARGET\". The former specifies
  329. that SOURCE from the host should be mounted at SOURCE in the other system.
  330. The latter format specifies that SOURCE from the host should be mounted at
  331. TARGET in the other system."
  332. (let ((index (string-index spec #\=)))
  333. (if index
  334. (file-system-mapping
  335. (source (substring spec 0 index))
  336. (target (substring spec (+ 1 index)))
  337. (writable? writable?))
  338. (file-system-mapping
  339. (source spec)
  340. (target spec)
  341. (writable? writable?)))))
  342. ;;;
  343. ;;; Common file systems.
  344. ;;;
  345. (define %pseudo-file-system-types
  346. ;; List of know pseudo file system types. This is used when validating file
  347. ;; system definitions.
  348. '("binfmt_misc" "cgroup" "debugfs" "devpts" "devtmpfs" "efivarfs" "fusectl"
  349. "hugetlbfs" "overlay" "proc" "securityfs" "sysfs" "tmpfs"))
  350. (define %fuse-control-file-system
  351. ;; Control file system for Linux' file systems in user-space (FUSE).
  352. (file-system
  353. (device "fusectl")
  354. (mount-point "/sys/fs/fuse/connections")
  355. (type "fusectl")
  356. (check? #f)))
  357. (define %binary-format-file-system
  358. ;; Support for arbitrary executable binary format.
  359. (file-system
  360. (device "binfmt_misc")
  361. (mount-point "/proc/sys/fs/binfmt_misc")
  362. (type "binfmt_misc")
  363. (check? #f)))
  364. (define %debug-file-system
  365. (file-system
  366. (type "debugfs")
  367. (device "none")
  368. (mount-point "/sys/kernel/debug")
  369. (check? #f)
  370. (create-mount-point? #t)))
  371. (define %efivars-file-system
  372. ;; Support for EFI variables file system.
  373. (file-system
  374. (device "efivarfs")
  375. (mount-point "/sys/firmware/efi/efivars")
  376. (type "efivarfs")
  377. (mount-may-fail? #t)
  378. (needed-for-boot? #f)
  379. (check? #f)))
  380. (define %tty-gid
  381. ;; ID of the 'tty' group. Allocate it statically to make it easy to refer
  382. ;; to it from here and from the 'tty' group definitions.
  383. 996)
  384. (define %pseudo-terminal-file-system
  385. ;; The pseudo-terminal file system. It needs to be mounted so that
  386. ;; statfs(2) returns DEVPTS_SUPER_MAGIC like libc's getpt(3) expects (and
  387. ;; thus openpty(3) and its users, such as xterm.)
  388. (file-system
  389. (device "none")
  390. (mount-point "/dev/pts")
  391. (type "devpts")
  392. (check? #f)
  393. (needed-for-boot? #f)
  394. (create-mount-point? #t)
  395. (options (string-append "gid=" (number->string %tty-gid) ",mode=620"))))
  396. (define %shared-memory-file-system
  397. ;; Shared memory.
  398. (file-system
  399. (device "tmpfs")
  400. (mount-point "/dev/shm")
  401. (type "tmpfs")
  402. (check? #f)
  403. (flags '(no-suid no-dev))
  404. (options "size=50%") ;TODO: make size configurable
  405. (create-mount-point? #t)))
  406. (define %immutable-store
  407. ;; Read-only store to avoid users or daemons accidentally modifying it.
  408. ;; 'guix-daemon' has provisions to remount it read-write in its own name
  409. ;; space.
  410. (file-system
  411. (device (%store-prefix))
  412. (mount-point (%store-prefix))
  413. (type "none")
  414. (check? #f)
  415. (flags '(read-only bind-mount no-atime))))
  416. (define %control-groups
  417. (let ((parent (file-system
  418. (device "cgroup")
  419. (mount-point "/sys/fs/cgroup")
  420. (type "tmpfs")
  421. (check? #f))))
  422. (cons parent
  423. (map (lambda (subsystem)
  424. (file-system
  425. (device "cgroup")
  426. (mount-point (string-append "/sys/fs/cgroup/" subsystem))
  427. (type "cgroup")
  428. (check? #f)
  429. (options subsystem)
  430. (create-mount-point? #t)
  431. ;; This must be mounted after, and unmounted before the
  432. ;; parent directory.
  433. (dependencies (list parent))))
  434. '("cpuset" "cpu" "cpuacct" "memory" "devices" "freezer"
  435. "blkio" "perf_event" "pids")))))
  436. (define %elogind-file-systems
  437. ;; We don't use systemd, but these file systems are needed for elogind,
  438. ;; which was extracted from systemd.
  439. (append
  440. (list (file-system
  441. (device "none")
  442. (mount-point "/run/systemd")
  443. (type "tmpfs")
  444. (check? #f)
  445. (flags '(no-suid no-dev no-exec))
  446. (options "mode=0755")
  447. (create-mount-point? #t))
  448. (file-system
  449. (device "none")
  450. (mount-point "/run/user")
  451. (type "tmpfs")
  452. (check? #f)
  453. (flags '(no-suid no-dev no-exec))
  454. (options "mode=0755")
  455. (create-mount-point? #t))
  456. ;; Elogind uses cgroups to organize processes, allowing it to map PIDs
  457. ;; to sessions. Elogind's cgroup hierarchy isn't associated with any
  458. ;; resource controller ("subsystem").
  459. (file-system
  460. (device "cgroup")
  461. (mount-point "/sys/fs/cgroup/elogind")
  462. (type "cgroup")
  463. (check? #f)
  464. (options "none,name=elogind")
  465. (create-mount-point? #t)
  466. (dependencies (list (car %control-groups)))))
  467. %control-groups))
  468. (define %base-file-systems
  469. ;; List of basic file systems to be mounted. Note that /proc and /sys are
  470. ;; currently mounted by the initrd.
  471. (list %pseudo-terminal-file-system
  472. %debug-file-system
  473. %shared-memory-file-system
  474. %efivars-file-system
  475. %immutable-store))
  476. ;; File systems for Linux containers differ from %base-file-systems in that
  477. ;; they impose additional restrictions such as no-exec or need different
  478. ;; options to function properly.
  479. ;;
  480. ;; The file system flags and options conform to the libcontainer
  481. ;; specification:
  482. ;; https://github.com/docker/libcontainer/blob/master/SPEC.md#filesystem
  483. (define %container-file-systems
  484. (list
  485. ;; Pseudo-terminal file system.
  486. (file-system
  487. (device "none")
  488. (mount-point "/dev/pts")
  489. (type "devpts")
  490. (flags '(no-exec no-suid))
  491. (needed-for-boot? #t)
  492. (create-mount-point? #t)
  493. (check? #f)
  494. (options "newinstance,ptmxmode=0666,mode=620"))
  495. ;; Shared memory file system.
  496. (file-system
  497. (device "tmpfs")
  498. (mount-point "/dev/shm")
  499. (type "tmpfs")
  500. (flags '(no-exec no-suid no-dev))
  501. (options "mode=1777,size=65536k")
  502. (needed-for-boot? #t)
  503. (create-mount-point? #t)
  504. (check? #f))
  505. ;; Message queue file system.
  506. (file-system
  507. (device "mqueue")
  508. (mount-point "/dev/mqueue")
  509. (type "mqueue")
  510. (flags '(no-exec no-suid no-dev))
  511. (needed-for-boot? #t)
  512. (create-mount-point? #t)
  513. (check? #f))))
  514. ;;;
  515. ;;; Shared file systems, for VMs/containers.
  516. ;;;
  517. ;; Mapping of host file system SOURCE to mount point TARGET in the guest.
  518. (define-record-type* <file-system-mapping> file-system-mapping
  519. make-file-system-mapping
  520. file-system-mapping?
  521. (source file-system-mapping-source) ;string
  522. (target file-system-mapping-target) ;string
  523. (writable? file-system-mapping-writable? ;Boolean
  524. (default #f)))
  525. (define (file-system-mapping->bind-mount mapping)
  526. "Return a file system that realizes MAPPING, a <file-system-mapping>, using
  527. a bind mount."
  528. (match mapping
  529. (($ <file-system-mapping> source target writable?)
  530. (file-system
  531. (mount-point target)
  532. (device source)
  533. (type "none")
  534. (flags (if writable?
  535. '(bind-mount)
  536. '(bind-mount read-only)))
  537. (check? #f)
  538. (create-mount-point? #t)))))
  539. (define %store-mapping
  540. ;; Mapping of the host's store into the guest.
  541. (file-system-mapping
  542. (source (%store-prefix))
  543. (target (%store-prefix))
  544. (writable? #f)))
  545. (define %network-configuration-files
  546. ;; List of essential network configuration files.
  547. '("/etc/resolv.conf"
  548. "/etc/nsswitch.conf"
  549. "/etc/services"
  550. "/etc/hosts"))
  551. (define %network-file-mappings
  552. ;; List of file mappings for essential network files.
  553. (filter-map (lambda (file)
  554. (file-system-mapping
  555. (source file)
  556. (target file)
  557. ;; XXX: On some GNU/Linux systems, /etc/resolv.conf is a
  558. ;; symlink to a file in a tmpfs which, for an unknown reason,
  559. ;; cannot be bind mounted read-only within the container.
  560. (writable? (string=? file "/etc/resolv.conf"))))
  561. %network-configuration-files))
  562. (define (file-system-type-predicate type)
  563. "Return a predicate that, when passed a file system, returns #t if that file
  564. system has the given TYPE."
  565. (lambda (fs)
  566. (string=? (file-system-type fs) type)))
  567. ;;;
  568. ;;; Btrfs specific helpers.
  569. ;;;
  570. (define (btrfs-subvolume? fs)
  571. "Predicate to check if FS, a file-system object, is a Btrfs subvolume."
  572. (and-let* ((btrfs-file-system? (string= "btrfs" (file-system-type fs)))
  573. (option-keys (map (match-lambda
  574. ((key . value) key)
  575. (key key))
  576. (file-system-options->alist
  577. (file-system-options fs)))))
  578. (find (cut string-prefix? "subvol" <>) option-keys)))
  579. (define (btrfs-store-subvolume-file-name file-systems)
  580. "Return the subvolume file name within the Btrfs top level onto which the
  581. store is located, else #f."
  582. (define (prepend-slash/maybe s)
  583. (if (string=? "/" (string-take s 1))
  584. s
  585. (string-append "/" s)))
  586. (and-let* ((btrfs-subvolume-fs (filter btrfs-subvolume? file-systems))
  587. (btrfs-subvolume-fs*
  588. (sort btrfs-subvolume-fs
  589. (lambda (fs1 fs2)
  590. (> (file-name-depth (file-system-mount-point fs1))
  591. (file-name-depth (file-system-mount-point fs2))))))
  592. (store-subvolume-fs
  593. (find (lambda (fs) (file-prefix? (file-system-mount-point fs)
  594. (%store-prefix)))
  595. btrfs-subvolume-fs*))
  596. (options (file-system-options->alist
  597. (file-system-options store-subvolume-fs))))
  598. ;; XXX: Deriving the subvolume name based from a subvolume ID is not
  599. ;; supported, as we'd need to query the actual file system.
  600. (or (and=> (assoc-ref options "subvol") prepend-slash/maybe)
  601. (raise (condition
  602. (&message
  603. (message "The store is on a Btrfs subvolume, but the \
  604. subvolume name is unknown."))
  605. (&fix-hint
  606. (hint
  607. (G_ "Use the @code{subvol} Btrfs file system option."))))))))
  608. ;;; file-systems.scm ends here