file-systems.scm 24 KB

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