file-systems.scm 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2014, 2015, 2016, 2017, 2018 Ludovic Courtès <ludo@gnu.org>
  3. ;;; Copyright © 2016, 2017 David Craven <david@craven.ch>
  4. ;;; Copyright © 2017 Mathieu Othacehe <m.othacehe@gmail.com>
  5. ;;; Copyright © 2019 Guillaume Le Vaillant <glv@posteo.net>
  6. ;;;
  7. ;;; This file is part of GNU Guix.
  8. ;;;
  9. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  10. ;;; under the terms of the GNU General Public License as published by
  11. ;;; the Free Software Foundation; either version 3 of the License, or (at
  12. ;;; your option) any later version.
  13. ;;;
  14. ;;; GNU Guix is distributed in the hope that it will be useful, but
  15. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  16. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. ;;; GNU General Public License for more details.
  18. ;;;
  19. ;;; You should have received a copy of the GNU General Public License
  20. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  21. (define-module (gnu build file-systems)
  22. #:use-module (gnu system uuid)
  23. #:use-module (gnu system file-systems)
  24. #:use-module (guix build utils)
  25. #:use-module (guix build bournish)
  26. #:use-module ((guix build syscalls)
  27. #:hide (file-system-type))
  28. #:use-module (rnrs io ports)
  29. #:use-module (rnrs bytevectors)
  30. #:use-module (ice-9 match)
  31. #:use-module (ice-9 rdelim)
  32. #:use-module (system foreign)
  33. #:autoload (system repl repl) (start-repl)
  34. #:use-module (srfi srfi-1)
  35. #:use-module (srfi srfi-26)
  36. #:export (disk-partitions
  37. partition-label-predicate
  38. partition-uuid-predicate
  39. partition-luks-uuid-predicate
  40. find-partition-by-label
  41. find-partition-by-uuid
  42. find-partition-by-luks-uuid
  43. canonicalize-device-spec
  44. read-partition-label
  45. read-partition-uuid
  46. read-luks-partition-uuid
  47. bind-mount
  48. mount-flags->bit-mask
  49. check-file-system
  50. mount-file-system))
  51. ;;; Commentary:
  52. ;;;
  53. ;;; This modules provides tools to deal with disk partitions, and to mount and
  54. ;;; check file systems.
  55. ;;;
  56. ;;; Code:
  57. (define (bind-mount source target)
  58. "Bind-mount SOURCE at TARGET."
  59. (mount source target "" MS_BIND))
  60. (define (seek* fd/port offset whence)
  61. "Like 'seek' but return -1 instead of throwing to 'system-error' upon
  62. EINVAL. This makes it easier to catch cases like OFFSET being too large for
  63. FD/PORT."
  64. (catch 'system-error
  65. (lambda ()
  66. (seek fd/port offset whence))
  67. (lambda args
  68. (if (= EINVAL (system-error-errno args))
  69. -1
  70. (apply throw args)))))
  71. (define (read-superblock device offset size magic?)
  72. "Read a superblock of SIZE from OFFSET and DEVICE. Return the raw
  73. superblock on success, and #f if no valid superblock was found. MAGIC?
  74. takes a bytevector and returns #t when it's a valid superblock."
  75. (call-with-input-file device
  76. (lambda (port)
  77. (and (= offset (seek* port offset SEEK_SET))
  78. (let ((block (make-bytevector size)))
  79. (match (get-bytevector-n! port block 0 (bytevector-length block))
  80. ((? eof-object?)
  81. #f)
  82. ((? number? len)
  83. (and (= len (bytevector-length block))
  84. (and (magic? block)
  85. block)))))))))
  86. (define null-terminated-latin1->string
  87. (cut latin1->string <> zero?))
  88. ;;;
  89. ;;; Ext2 file systems.
  90. ;;;
  91. ;; <http://www.nongnu.org/ext2-doc/ext2.html#DEF-SUPERBLOCK>.
  92. ;; TODO: Use "packed structs" from Guile-OpenGL or similar.
  93. (define-syntax %ext2-endianness
  94. ;; Endianness of ext2 file systems.
  95. (identifier-syntax (endianness little)))
  96. (define (ext2-superblock? sblock)
  97. "Return #t when SBLOCK is an ext2 superblock."
  98. (let ((magic (bytevector-u16-ref sblock 56 %ext2-endianness)))
  99. (= magic #xef53)))
  100. (define (read-ext2-superblock device)
  101. "Return the raw contents of DEVICE's ext2 superblock as a bytevector, or #f
  102. if DEVICE does not contain an ext2 file system."
  103. (read-superblock device 1024 264 ext2-superblock?))
  104. (define (ext2-superblock-uuid sblock)
  105. "Return the UUID of ext2 superblock SBLOCK as a 16-byte bytevector."
  106. (sub-bytevector sblock 104 16))
  107. (define (ext2-superblock-volume-name sblock)
  108. "Return the volume name of SBLOCK as a string of at most 16 characters, or
  109. #f if SBLOCK has no volume name."
  110. (null-terminated-latin1->string (sub-bytevector sblock 120 16)))
  111. (define (check-ext2-file-system device)
  112. "Return the health of an ext2 file system on DEVICE."
  113. (match (status:exit-val
  114. (system* "e2fsck" "-v" "-p" "-C" "0" device))
  115. (0 'pass)
  116. (1 'errors-corrected)
  117. (2 'reboot-required)
  118. (_ 'fatal-error)))
  119. ;;;
  120. ;;; Btrfs file systems.
  121. ;;;
  122. ;; <https://btrfs.wiki.kernel.org/index.php/On-disk_Format#Superblock>.
  123. (define-syntax %btrfs-endianness
  124. ;; Endianness of btrfs file systems.
  125. (identifier-syntax (endianness little)))
  126. (define (btrfs-superblock? sblock)
  127. "Return #t when SBLOCK is a btrfs superblock."
  128. (bytevector=? (sub-bytevector sblock 64 8)
  129. (string->utf8 "_BHRfS_M")))
  130. (define (read-btrfs-superblock device)
  131. "Return the raw contents of DEVICE's btrfs superblock as a bytevector, or #f
  132. if DEVICE does not contain a btrfs file system."
  133. (read-superblock device 65536 4096 btrfs-superblock?))
  134. (define (btrfs-superblock-uuid sblock)
  135. "Return the UUID of a btrfs superblock SBLOCK as a 16-byte bytevector."
  136. (sub-bytevector sblock 32 16))
  137. (define (btrfs-superblock-volume-name sblock)
  138. "Return the volume name of SBLOCK as a string of at most 256 characters, or
  139. #f if SBLOCK has no volume name."
  140. (null-terminated-latin1->string (sub-bytevector sblock 299 256)))
  141. (define (check-btrfs-file-system device)
  142. "Return the health of a btrfs file system on DEVICE."
  143. (match (status:exit-val
  144. (system* "btrfs" "device" "scan"))
  145. (0 'pass)
  146. (_ 'fatal-error)))
  147. ;;;
  148. ;;; FAT32 file systems.
  149. ;;;
  150. ;; <http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-107.pdf>.
  151. (define (fat32-superblock? sblock)
  152. "Return #t when SBLOCK is a fat32 superblock."
  153. (bytevector=? (sub-bytevector sblock 82 8)
  154. (string->utf8 "FAT32 ")))
  155. (define (read-fat32-superblock device)
  156. "Return the raw contents of DEVICE's fat32 superblock as a bytevector, or
  157. #f if DEVICE does not contain a fat32 file system."
  158. (read-superblock device 0 90 fat32-superblock?))
  159. (define (fat32-superblock-uuid sblock)
  160. "Return the Volume ID of a fat superblock SBLOCK as a 4-byte bytevector."
  161. (sub-bytevector sblock 67 4))
  162. (define (fat32-superblock-volume-name sblock)
  163. "Return the volume name of SBLOCK as a string of at most 11 characters, or
  164. #f if SBLOCK has no volume name. The volume name is a latin1 string.
  165. Trailing spaces are trimmed."
  166. (string-trim-right (latin1->string (sub-bytevector sblock 71 11) (lambda (c) #f)) #\space))
  167. (define (check-fat-file-system device)
  168. "Return the health of a fat file system on DEVICE."
  169. (match (status:exit-val
  170. (system* "fsck.vfat" "-v" "-a" device))
  171. (0 'pass)
  172. (1 'errors-corrected)
  173. (_ 'fatal-error)))
  174. ;;;
  175. ;;; FAT16 file systems.
  176. ;;;
  177. (define (fat16-superblock? sblock)
  178. "Return #t when SBLOCK is a fat16 boot record."
  179. (bytevector=? (sub-bytevector sblock 54 8)
  180. (string->utf8 "FAT16 ")))
  181. (define (read-fat16-superblock device)
  182. "Return the raw contents of DEVICE's fat16 superblock as a bytevector, or
  183. #f if DEVICE does not contain a fat16 file system."
  184. (read-superblock device 0 62 fat16-superblock?))
  185. (define (fat16-superblock-uuid sblock)
  186. "Return the Volume ID of a fat superblock SBLOCK as a 4-byte bytevector."
  187. (sub-bytevector sblock 39 4))
  188. (define (fat16-superblock-volume-name sblock)
  189. "Return the volume name of SBLOCK as a string of at most 11 characters, or
  190. #f if SBLOCK has no volume name. The volume name is a latin1 string.
  191. Trailing spaces are trimmed."
  192. (string-trim-right (latin1->string (sub-bytevector sblock 43 11)
  193. (lambda (c) #f))
  194. #\space))
  195. ;;;
  196. ;;; ISO9660 file systems.
  197. ;;;
  198. ;; <http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-119.pdf>.
  199. (define (iso9660-superblock? sblock)
  200. "Return #t when SBLOCK is an iso9660 volume descriptor."
  201. (bytevector=? (sub-bytevector sblock 1 6)
  202. ;; Note: "\x01" is the volume descriptor format version
  203. (string->utf8 "CD001\x01")))
  204. (define (read-iso9660-primary-volume-descriptor device offset)
  205. "Find and read the first primary volume descriptor, starting at OFFSET.
  206. Return #f if not found."
  207. (let* ((sblock (read-superblock device offset 2048 iso9660-superblock?))
  208. (type-code (if sblock
  209. (bytevector-u8-ref sblock 0)
  210. (error (format #f
  211. "Could not read ISO9660 primary
  212. volume descriptor from ~s"
  213. device)))))
  214. (match type-code
  215. (255 #f) ; Volume Descriptor Set Terminator.
  216. (1 sblock) ; Primary Volume Descriptor
  217. (_ (read-iso9660-primary-volume-descriptor device (+ offset 2048))))))
  218. (define (read-iso9660-superblock device)
  219. "Return the raw contents of DEVICE's iso9660 primary volume descriptor
  220. as a bytevector, or #f if DEVICE does not contain an iso9660 file system."
  221. ;; Start reading at sector 16.
  222. ;; Since we are not sure that the device contains an ISO9660 file system,
  223. ;; we have to find that out first.
  224. (if (read-superblock device (* 2048 16) 2048 iso9660-superblock?)
  225. (read-iso9660-primary-volume-descriptor device (* 2048 16))
  226. #f)) ; Device does not contain an iso9660 file system.
  227. (define (iso9660-superblock-uuid sblock)
  228. "Return the modification time of an iso9660 primary volume descriptor
  229. SBLOCK as a bytevector. If that's not set, returns the creation time."
  230. ;; Drops GMT offset for compatibility with Grub, blkid and /dev/disk/by-uuid.
  231. ;; Compare Grub: "2014-12-02-19-30-23-00".
  232. ;; Compare blkid result: "2014-12-02-19-30-23-00".
  233. ;; Compare /dev/disk/by-uuid entry: "2014-12-02-19-30-23-00".
  234. (let* ((creation-time (sub-bytevector sblock 813 17))
  235. (modification-time (sub-bytevector sblock 830 17))
  236. (unset-time (make-bytevector 17 0))
  237. (time (if (bytevector=? unset-time modification-time)
  238. creation-time
  239. modification-time)))
  240. (sub-bytevector time 0 16))) ; strips GMT offset.
  241. (define (iso9660-superblock-volume-name sblock)
  242. "Return the volume name of SBLOCK as a string. The volume name is an ASCII
  243. string. Trailing spaces are trimmed."
  244. ;; Note: Valid characters are of the set "[0-9][A-Z]_" (ECMA-119 Appendix A)
  245. (string-trim-right (latin1->string (sub-bytevector sblock 40 32)
  246. (lambda (c) #f)) #\space))
  247. ;;;
  248. ;;; LUKS encrypted devices.
  249. ;;;
  250. ;; The LUKS header format is described in "LUKS On-Disk Format Specification":
  251. ;; <https://gitlab.com/cryptsetup/cryptsetup/wikis/Specification>. We follow
  252. ;; version 1.2.1 of this document.
  253. (define-syntax %luks-endianness
  254. ;; Endianness of LUKS headers.
  255. (identifier-syntax (endianness big)))
  256. (define (luks-superblock? sblock)
  257. "Return #t when SBLOCK is a luks superblock."
  258. (define %luks-magic
  259. ;; The 'LUKS_MAGIC' constant.
  260. (u8-list->bytevector (append (map char->integer (string->list "LUKS"))
  261. (list #xba #xbe))))
  262. (let ((magic (sub-bytevector sblock 0 6))
  263. (version (bytevector-u16-ref sblock 6 %luks-endianness)))
  264. (and (bytevector=? magic %luks-magic)
  265. (= version 1))))
  266. (define (read-luks-header file)
  267. "Read a LUKS header from FILE. Return the raw header on success, and #f if
  268. not valid header was found."
  269. ;; Size in bytes of the LUKS header, including key slots.
  270. (read-superblock file 0 592 luks-superblock?))
  271. (define (luks-header-uuid header)
  272. "Return the LUKS UUID from HEADER, as a 16-byte bytevector."
  273. ;; 40 bytes are reserved for the UUID, but in practice, it contains the 36
  274. ;; bytes of its ASCII representation.
  275. (let ((uuid (sub-bytevector header 168 36)))
  276. (string->uuid (utf8->string uuid))))
  277. ;;;
  278. ;;; Partition lookup.
  279. ;;;
  280. (define (disk-partitions)
  281. "Return the list of device names corresponding to valid disk partitions."
  282. (define (partition? name major minor)
  283. ;; grub-mkrescue does some funny things for EFI support which
  284. ;; makes it a lot more difficult than one would expect to support
  285. ;; booting an ISO-9660 image from an USB flash drive.
  286. ;; For example there's a buggy (too small) hidden partition in it
  287. ;; which Linux mounts and then proceeds to fail while trying to
  288. ;; fall off the edge.
  289. ;; In any case, partition tables are supposed to be optional so
  290. ;; here we allow checking entire disks for file systems, too.
  291. (> major 2)) ;ignore RAM disks and floppy disks
  292. (call-with-input-file "/proc/partitions"
  293. (lambda (port)
  294. ;; Skip the two header lines.
  295. (read-line port)
  296. (read-line port)
  297. ;; Read each subsequent line, and extract the last space-separated
  298. ;; field.
  299. (let loop ((parts '()))
  300. (let ((line (read-line port)))
  301. (if (eof-object? line)
  302. (reverse parts)
  303. (match (string-tokenize line)
  304. (((= string->number major) (= string->number minor)
  305. blocks name)
  306. (if (partition? name major minor)
  307. (loop (cons name parts))
  308. (loop parts))))))))))
  309. (define (ENOENT-safe proc)
  310. "Wrap the one-argument PROC such that ENOENT errors are caught and lead to a
  311. warning and #f as the result."
  312. (lambda (device)
  313. (catch 'system-error
  314. (lambda ()
  315. (proc device))
  316. (lambda args
  317. ;; When running on the hand-made /dev,
  318. ;; 'disk-partitions' could return partitions for which
  319. ;; we have no /dev node. Handle that gracefully.
  320. (let ((errno (system-error-errno args)))
  321. (cond ((= ENOENT errno)
  322. (format (current-error-port)
  323. "warning: device '~a' not found~%" device)
  324. #f)
  325. ((= ENOMEDIUM errno) ;for removable media
  326. #f)
  327. ((= EIO errno) ;unreadable hardware like audio CDs
  328. (format (current-error-port)
  329. "warning: failed to read from device '~a'~%" device)
  330. #f)
  331. (else
  332. (apply throw args))))))))
  333. (define (partition-field-reader read field)
  334. "Return a procedure that takes a device and returns the value of a FIELD in
  335. the partition superblock or #f."
  336. (let ((read (ENOENT-safe read)))
  337. (lambda (device)
  338. (let ((sblock (read device)))
  339. (and sblock
  340. (field sblock))))))
  341. (define (read-partition-field device partition-field-readers)
  342. "Returns the value of a FIELD in the partition superblock of DEVICE or #f. It
  343. takes a list of PARTITION-FIELD-READERS and returns the result of the first
  344. partition field reader that returned a value."
  345. (match (filter-map (cut apply <> (list device)) partition-field-readers)
  346. ((field . _) field)
  347. (_ #f)))
  348. (define %partition-label-readers
  349. (list (partition-field-reader read-iso9660-superblock
  350. iso9660-superblock-volume-name)
  351. (partition-field-reader read-ext2-superblock
  352. ext2-superblock-volume-name)
  353. (partition-field-reader read-btrfs-superblock
  354. btrfs-superblock-volume-name)
  355. (partition-field-reader read-fat32-superblock
  356. fat32-superblock-volume-name)
  357. (partition-field-reader read-fat16-superblock
  358. fat16-superblock-volume-name)))
  359. (define %partition-uuid-readers
  360. (list (partition-field-reader read-iso9660-superblock
  361. iso9660-superblock-uuid)
  362. (partition-field-reader read-ext2-superblock
  363. ext2-superblock-uuid)
  364. (partition-field-reader read-btrfs-superblock
  365. btrfs-superblock-uuid)
  366. (partition-field-reader read-fat32-superblock
  367. fat32-superblock-uuid)
  368. (partition-field-reader read-fat16-superblock
  369. fat16-superblock-uuid)))
  370. (define read-partition-label
  371. (cut read-partition-field <> %partition-label-readers))
  372. (define read-partition-uuid
  373. (cut read-partition-field <> %partition-uuid-readers))
  374. (define luks-partition-field-reader
  375. (partition-field-reader read-luks-header luks-header-uuid))
  376. (define read-luks-partition-uuid
  377. (cut read-partition-field <> (list luks-partition-field-reader)))
  378. (define (partition-predicate reader =)
  379. "Return a predicate that returns true if the FIELD of partition header that
  380. was READ is = to the given value."
  381. (lambda (expected)
  382. (lambda (device)
  383. (let ((actual (reader device)))
  384. (and actual
  385. (= actual expected))))))
  386. (define partition-label-predicate
  387. (partition-predicate read-partition-label string=?))
  388. (define partition-uuid-predicate
  389. (partition-predicate read-partition-uuid uuid=?))
  390. (define luks-partition-uuid-predicate
  391. (partition-predicate luks-partition-field-reader uuid=?))
  392. (define (find-partition predicate)
  393. "Return the first partition found that matches PREDICATE, or #f if none
  394. were found."
  395. (lambda (expected)
  396. (find (predicate expected)
  397. (map (cut string-append "/dev/" <>)
  398. (disk-partitions)))))
  399. (define find-partition-by-label
  400. (find-partition partition-label-predicate))
  401. (define find-partition-by-uuid
  402. (find-partition partition-uuid-predicate))
  403. (define find-partition-by-luks-uuid
  404. (find-partition luks-partition-uuid-predicate))
  405. (define (canonicalize-device-spec spec)
  406. "Return the device name corresponding to SPEC, which can be a <uuid>, a
  407. <file-system-label>, or a string (typically a /dev file name)."
  408. (define max-trials
  409. ;; Number of times we retry partition label resolution, 1 second per
  410. ;; trial. Note: somebody reported a delay of 16 seconds (!) before their
  411. ;; USB key would be detected by the kernel, so we must wait for at least
  412. ;; this long.
  413. 20)
  414. (define (resolve find-partition spec fmt)
  415. (let loop ((count 0))
  416. (let ((device (find-partition spec)))
  417. (or device
  418. ;; Some devices take a bit of time to appear, most notably USB
  419. ;; storage devices. Thus, wait for the device to appear.
  420. (if (> count max-trials)
  421. (error "failed to resolve partition" (fmt spec))
  422. (begin
  423. (format #t "waiting for partition '~a' to appear...~%"
  424. (fmt spec))
  425. (sleep 1)
  426. (loop (+ 1 count))))))))
  427. (match spec
  428. ((? string?)
  429. ;; Nothing to do, but wait until SPEC shows up.
  430. (resolve identity spec identity))
  431. ((? file-system-label?)
  432. ;; Resolve the label.
  433. (resolve find-partition-by-label
  434. (file-system-label->string spec)
  435. identity))
  436. ((? uuid?)
  437. (resolve find-partition-by-uuid
  438. (uuid-bytevector spec)
  439. uuid->string))))
  440. (define (check-file-system device type)
  441. "Run a file system check of TYPE on DEVICE."
  442. (define check-procedure
  443. (cond
  444. ((string-prefix? "ext" type) check-ext2-file-system)
  445. ((string-prefix? "btrfs" type) check-btrfs-file-system)
  446. ((string-suffix? "fat" type) check-fat-file-system)
  447. (else #f)))
  448. (if check-procedure
  449. (match (check-procedure device)
  450. ('pass
  451. #t)
  452. ('errors-corrected
  453. (format (current-error-port)
  454. "File system check corrected errors on ~a; continuing~%"
  455. device))
  456. ('reboot-required
  457. (format (current-error-port)
  458. "File system check corrected errors on ~a; rebooting~%"
  459. device)
  460. (sleep 3)
  461. (reboot))
  462. ('fatal-error
  463. (format (current-error-port) "File system check on ~a failed~%"
  464. device)
  465. ;; Spawn a REPL only if someone would be able to interact with it.
  466. (when (isatty? (current-input-port))
  467. (format (current-error-port) "Spawning Bourne-like REPL.~%")
  468. ;; 'current-output-port' is typically connected to /dev/klog (in
  469. ;; PID 1), but here we want to make sure we talk directly to the
  470. ;; user.
  471. (with-output-to-file "/dev/console"
  472. (lambda ()
  473. (start-repl %bournish-language))))))
  474. (format (current-error-port)
  475. "No file system check procedure for ~a; skipping~%"
  476. device)))
  477. (define (mount-flags->bit-mask flags)
  478. "Return the number suitable for the 'flags' argument of 'mount' that
  479. corresponds to the symbols listed in FLAGS."
  480. (let loop ((flags flags))
  481. (match flags
  482. (('read-only rest ...)
  483. (logior MS_RDONLY (loop rest)))
  484. (('bind-mount rest ...)
  485. (logior MS_BIND (loop rest)))
  486. (('no-suid rest ...)
  487. (logior MS_NOSUID (loop rest)))
  488. (('no-dev rest ...)
  489. (logior MS_NODEV (loop rest)))
  490. (('no-exec rest ...)
  491. (logior MS_NOEXEC (loop rest)))
  492. (('no-atime rest ...)
  493. (logior MS_NOATIME (loop rest)))
  494. (('strict-atime rest ...)
  495. (logior MS_STRICTATIME (loop rest)))
  496. (('lazy-time rest ...)
  497. (logior MS_LAZYTIME (loop rest)))
  498. (()
  499. 0))))
  500. (define* (mount-file-system fs #:key (root "/root"))
  501. "Mount the file system described by FS, a <file-system> object, under ROOT."
  502. (define (mount-nfs source mount-point type flags options)
  503. (let* ((idx (string-rindex source #\:))
  504. (host-part (string-take source idx))
  505. ;; Strip [] from around host if present
  506. (host (match (string-split host-part (string->char-set "[]"))
  507. (("" h "") h)
  508. ((h) h)))
  509. (aa (match (getaddrinfo host "nfs") ((x . _) x)))
  510. (sa (addrinfo:addr aa))
  511. (inet-addr (inet-ntop (sockaddr:fam sa)
  512. (sockaddr:addr sa))))
  513. ;; Mounting an NFS file system requires passing the address
  514. ;; of the server in the addr= option
  515. (mount source mount-point type flags
  516. (string-append "addr="
  517. inet-addr
  518. (if options
  519. (string-append "," options)
  520. "")))))
  521. (let ((type (file-system-type fs))
  522. (options (file-system-options fs))
  523. (source (canonicalize-device-spec (file-system-device fs)))
  524. (mount-point (string-append root "/"
  525. (file-system-mount-point fs)))
  526. (flags (mount-flags->bit-mask (file-system-flags fs))))
  527. (when (file-system-check? fs)
  528. (check-file-system source type))
  529. ;; Create the mount point. Most of the time this is a directory, but
  530. ;; in the case of a bind mount, a regular file or socket may be needed.
  531. (if (and (= MS_BIND (logand flags MS_BIND))
  532. (not (file-is-directory? source)))
  533. (unless (file-exists? mount-point)
  534. (mkdir-p (dirname mount-point))
  535. (call-with-output-file mount-point (const #t)))
  536. (mkdir-p mount-point))
  537. (cond
  538. ((string-prefix? "nfs" type)
  539. (mount-nfs source mount-point type flags options))
  540. (else
  541. (mount source mount-point type flags options)))
  542. ;; For read-only bind mounts, an extra remount is needed, as per
  543. ;; <http://lwn.net/Articles/281157/>, which still applies to Linux 4.0.
  544. (when (and (= MS_BIND (logand flags MS_BIND))
  545. (= MS_RDONLY (logand flags MS_RDONLY)))
  546. (let ((flags (logior MS_BIND MS_REMOUNT MS_RDONLY)))
  547. (mount source mount-point type flags #f)))))
  548. ;;; file-systems.scm ends here