file-systems.scm 26 KB

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