file-systems.scm 26 KB

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