file-systems.scm 22 KB

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