file-systems.scm 23 KB

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