file-systems.scm 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2014, 2015, 2016, 2017, 2018, 2020, 2021 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. ;;; Copyright © 2019–2021 Tobias Geerinckx-Rice <me@tobias.gr>
  7. ;;; Copyright © 2019 David C. Trudgian <dave@trudgian.net>
  8. ;;; Copyright © 2020 Maxim Cournoyer <maxim.cournoyer@gmail.com>
  9. ;;;
  10. ;;; This file is part of GNU Guix.
  11. ;;;
  12. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  13. ;;; under the terms of the GNU General Public License as published by
  14. ;;; the Free Software Foundation; either version 3 of the License, or (at
  15. ;;; your option) any later version.
  16. ;;;
  17. ;;; GNU Guix is distributed in the hope that it will be useful, but
  18. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  19. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. ;;; GNU General Public License for more details.
  21. ;;;
  22. ;;; You should have received a copy of the GNU General Public License
  23. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  24. (define-module (gnu build file-systems)
  25. #:use-module (gnu system uuid)
  26. #:use-module (gnu system file-systems)
  27. #:use-module (guix build utils)
  28. #:use-module (guix build bournish)
  29. #:use-module ((guix build syscalls)
  30. #:hide (file-system-type))
  31. #:use-module (rnrs io ports)
  32. #:use-module (rnrs bytevectors)
  33. #:use-module (ice-9 match)
  34. #:use-module (ice-9 rdelim)
  35. #:use-module (system foreign)
  36. #:autoload (system repl repl) (start-repl)
  37. #:use-module (srfi srfi-1)
  38. #:use-module (srfi srfi-26)
  39. #:export (disk-partitions
  40. partition-label-predicate
  41. partition-uuid-predicate
  42. partition-luks-uuid-predicate
  43. find-partition-by-label
  44. find-partition-by-uuid
  45. find-partition-by-luks-uuid
  46. canonicalize-device-spec
  47. read-partition-label
  48. read-partition-uuid
  49. read-luks-partition-uuid
  50. bind-mount
  51. mount-flags->bit-mask
  52. check-file-system
  53. mount-file-system))
  54. ;;; Commentary:
  55. ;;;
  56. ;;; This modules provides tools to deal with disk partitions, and to mount and
  57. ;;; check file systems.
  58. ;;;
  59. ;;; Code:
  60. (define (bind-mount source target)
  61. "Bind-mount SOURCE at TARGET."
  62. (mount source target "" MS_BIND))
  63. (define (seek* fd/port offset whence)
  64. "Like 'seek' but return -1 instead of throwing to 'system-error' upon
  65. EINVAL. This makes it easier to catch cases like OFFSET being too large for
  66. FD/PORT."
  67. (catch 'system-error
  68. (lambda ()
  69. (seek fd/port offset whence))
  70. (lambda args
  71. (if (= EINVAL (system-error-errno args))
  72. -1
  73. (apply throw args)))))
  74. (define (read-superblock device offset size magic?)
  75. "Read a superblock of SIZE from OFFSET and DEVICE. Return the raw
  76. superblock on success, and #f if no valid superblock was found. MAGIC?
  77. takes a bytevector and returns #t when it's a valid superblock."
  78. (call-with-input-file device
  79. (lambda (port)
  80. (and (= offset (seek* port offset SEEK_SET))
  81. (let ((block (make-bytevector size)))
  82. (match (get-bytevector-n! port block 0 (bytevector-length block))
  83. ((? eof-object?)
  84. #f)
  85. ((? number? len)
  86. (and (= len (bytevector-length block))
  87. (and (magic? block)
  88. block)))))))))
  89. (define null-terminated-latin1->string
  90. (cut latin1->string <> zero?))
  91. (define (bytevector-utf16-length bv)
  92. "Given a bytevector BV containing a NUL-terminated UTF16-encoded string,
  93. determine where the NUL terminator is and return its index. If there's no
  94. NUL terminator, return the size of the bytevector."
  95. (let ((length (bytevector-length bv)))
  96. (let loop ((index 0))
  97. (if (< index length)
  98. (if (zero? (bytevector-u16-ref bv index 'little))
  99. index
  100. (loop (+ index 2)))
  101. length))))
  102. (define* (bytevector->u16-list bv endianness #:optional (index 0))
  103. (if (< index (bytevector-length bv))
  104. (cons (bytevector-u16-ref bv index endianness)
  105. (bytevector->u16-list bv endianness (+ index 2)))
  106. '()))
  107. ;; The initrd doesn't have iconv data, so do the conversion ourselves.
  108. (define (utf16->string bv endianness)
  109. (list->string
  110. (map integer->char
  111. (reverse
  112. (let loop ((remainder (bytevector->u16-list bv endianness))
  113. (result '()))
  114. (match remainder
  115. (() result)
  116. ((a) (cons a result))
  117. ((a b x ...)
  118. (if (and (>= a #xD800) (< a #xDC00) ; high surrogate
  119. (>= b #xDC00) (< b #xE000)) ; low surrogate
  120. (loop x (cons (+ #x10000
  121. (* #x400 (- a #xD800))
  122. (- b #xDC00))
  123. result))
  124. (loop (cons b x) (cons a result))))))))))
  125. (define (null-terminated-utf16->string bv endianness)
  126. (utf16->string (sub-bytevector bv 0 (bytevector-utf16-length bv))
  127. endianness))
  128. ;;;
  129. ;;; Ext2 file systems.
  130. ;;;
  131. ;; <http://www.nongnu.org/ext2-doc/ext2.html#DEF-SUPERBLOCK>.
  132. ;; TODO: Use "packed structs" from Guile-OpenGL or similar.
  133. (define-syntax %ext2-endianness
  134. ;; Endianness of ext2 file systems.
  135. (identifier-syntax (endianness little)))
  136. (define (ext2-superblock? sblock)
  137. "Return #t when SBLOCK is an ext2 superblock."
  138. (let ((magic (bytevector-u16-ref sblock 56 %ext2-endianness)))
  139. (= magic #xef53)))
  140. (define (read-ext2-superblock device)
  141. "Return the raw contents of DEVICE's ext2 superblock as a bytevector, or #f
  142. if DEVICE does not contain an ext2 file system."
  143. (read-superblock device 1024 264 ext2-superblock?))
  144. (define (ext2-superblock-uuid sblock)
  145. "Return the UUID of ext2 superblock SBLOCK as a 16-byte bytevector."
  146. (sub-bytevector sblock 104 16))
  147. (define (ext2-superblock-volume-name sblock)
  148. "Return the volume name of ext2 superblock SBLOCK as a string of at most 16
  149. characters, or #f if SBLOCK has no volume name."
  150. (null-terminated-latin1->string (sub-bytevector sblock 120 16)))
  151. (define (check-ext2-file-system device force? repair)
  152. "Return the health of an unmounted ext2 file system on DEVICE. If FORCE? is
  153. true, check the file system even if it's marked as clean. If REPAIR is false,
  154. do not write to the file system to fix errors. If it's #t, fix all
  155. errors. Otherwise, fix only those considered safe to repair automatically."
  156. (match (status:exit-val
  157. (apply system* `("e2fsck" "-v" "-C" "0"
  158. ,@(if force? '("-f") '())
  159. ,@(match repair
  160. (#f '("-n"))
  161. (#t '("-y"))
  162. (_ '("-p")))
  163. ,device)))
  164. (0 'pass)
  165. (1 'errors-corrected)
  166. (2 'reboot-required)
  167. (_ 'fatal-error)))
  168. ;;;
  169. ;;; Linux swap.
  170. ;;;
  171. ;; Linux "swap space" is not a file system but it has a UUID and volume name,
  172. ;; like actual file systems, and we want to be able to look up swap partitions
  173. ;; by UUID and by label.
  174. (define %linux-swap-magic
  175. (string->utf8 "SWAPSPACE2"))
  176. ;; Like 'PAGE_SIZE' in Linux, arch/x86/include/asm/page.h.
  177. ;; XXX: This is always 4K on x86_64, i386, and ARMv7. However, on AArch64,
  178. ;; this is determined by 'CONFIG_ARM64_PAGE_SHIFT' in the kernel, which is 12
  179. ;; by default (4K) but can be 14 or 16.
  180. (define %page-size 4096)
  181. (define (linux-swap-superblock? sblock)
  182. "Return #t when SBLOCK is an linux-swap superblock."
  183. (and (= (bytevector-length sblock) %page-size)
  184. (bytevector=? (sub-bytevector sblock (- %page-size 10) 10)
  185. %linux-swap-magic)))
  186. (define (read-linux-swap-superblock device)
  187. "Return the raw contents of DEVICE's linux-swap superblock as a bytevector, or #f
  188. if DEVICE does not contain an linux-swap file system."
  189. (read-superblock device 0 %page-size linux-swap-superblock?))
  190. ;; See 'union swap_header' in 'include/linux/swap.h'.
  191. (define (linux-swap-superblock-uuid sblock)
  192. "Return the UUID of Linux-swap superblock SBLOCK as a 16-byte bytevector."
  193. (sub-bytevector sblock (+ 1024 4 4 4) 16))
  194. (define (linux-swap-superblock-volume-name sblock)
  195. "Return the label of Linux-swap superblock SBLOCK as a string."
  196. (null-terminated-latin1->string
  197. (sub-bytevector sblock (+ 1024 4 4 4 16) 16)))
  198. ;;;
  199. ;;; Bcachefs file systems.
  200. ;;;
  201. ;; <https://evilpiepirate.org/git/bcachefs-tools.git/tree/libbcachefs/bcachefs_format.h>
  202. (define-syntax %bcachefs-endianness
  203. ;; Endianness of bcachefs file systems.
  204. (identifier-syntax (endianness little)))
  205. (define (bcachefs-superblock? sblock)
  206. "Return #t when SBLOCK is an bcachefs superblock."
  207. (bytevector=? (sub-bytevector sblock 24 16)
  208. #vu8(#xc6 #x85 #x73 #xf6 #x4e #x1a #x45 #xca
  209. #x82 #x65 #xf5 #x7f #x48 #xba #x6d #x81)))
  210. (define (read-bcachefs-superblock device)
  211. "Return the raw contents of DEVICE's bcachefs superblock as a bytevector, or #f
  212. if DEVICE does not contain a bcachefs file system."
  213. ;; Field offsets & lengths, in bytes. There are more (and the superblock is
  214. ;; extensible) but we need only some basic information here:
  215. ;; 0 16 bch_csum
  216. ;; 16 8 version
  217. ;; 24 16 magic
  218. ;; 40 16 uuid ← ‘internal’: you probably don't want this one
  219. ;; 56 16 user_uuid ← ‘external’: user-visible one by which to mount
  220. ;; 72 32 label
  221. ;; Assume a sane file system: ignore the back-up superblock & checksums.
  222. (read-superblock device 4096 104 bcachefs-superblock?))
  223. (define (bcachefs-superblock-external-uuid sblock)
  224. "Return the external UUID of bcachefs superblock SBLOCK as a 16-byte
  225. bytevector."
  226. (sub-bytevector sblock 56 16))
  227. (define (bcachefs-superblock-volume-name sblock)
  228. "Return the volume name of bcachefs superblock SBLOCK as a string of at most
  229. 32 characters, or #f if SBLOCK has no volume name."
  230. (null-terminated-latin1->string (sub-bytevector sblock 72 32)))
  231. (define (check-bcachefs-file-system device force? repair)
  232. "Return the health of an unmounted bcachefs file system on DEVICE. If FORCE?
  233. is true, check the file system even if it's marked as clean. If REPAIR is
  234. false, do not write to the file system to fix errors. If it's #t, fix all
  235. errors. Otherwise, fix only those considered safe to repair automatically."
  236. (let ((ignored-bits (logior 2)) ; DEVICE was mounted read-only
  237. (status
  238. ;; A number, or #f on abnormal termination (e.g., assertion failure).
  239. (status:exit-val
  240. (apply system* `("bcachefs" "fsck" "-v"
  241. ,@(if force? '("-f") '())
  242. ,@(match repair
  243. (#f '("-n"))
  244. (#t '("-y"))
  245. (_ '("-p")))
  246. ;; Make each multi-device member a separate argument.
  247. ,@(string-split device #\:))))))
  248. (match (and=> status (cut logand <> (lognot ignored-bits)))
  249. (0 'pass)
  250. (1 'errors-corrected)
  251. (_ 'fatal-error))))
  252. ;;;
  253. ;;; Btrfs file systems.
  254. ;;;
  255. ;; <https://btrfs.wiki.kernel.org/index.php/On-disk_Format#Superblock>.
  256. (define-syntax %btrfs-endianness
  257. ;; Endianness of btrfs file systems.
  258. (identifier-syntax (endianness little)))
  259. (define (btrfs-superblock? sblock)
  260. "Return #t when SBLOCK is a btrfs superblock."
  261. (bytevector=? (sub-bytevector sblock 64 8)
  262. (string->utf8 "_BHRfS_M")))
  263. (define (read-btrfs-superblock device)
  264. "Return the raw contents of DEVICE's btrfs superblock as a bytevector, or #f
  265. if DEVICE does not contain a btrfs file system."
  266. (read-superblock device 65536 4096 btrfs-superblock?))
  267. (define (btrfs-superblock-uuid sblock)
  268. "Return the UUID of a btrfs superblock SBLOCK as a 16-byte bytevector."
  269. (sub-bytevector sblock 32 16))
  270. (define (btrfs-superblock-volume-name sblock)
  271. "Return the volume name of btrfs superblock SBLOCK as a string of at most 256
  272. characters, or #f if SBLOCK has no volume name."
  273. (null-terminated-latin1->string (sub-bytevector sblock 299 256)))
  274. (define (check-btrfs-file-system device force? repair)
  275. "Return the health of an unmounted btrfs file system on DEVICE. If FORCE? is
  276. false, return 'PASS unconditionally as btrfs claims no need for off-line checks.
  277. When FORCE? is true, do perform a real check. This is not recommended! See
  278. @uref{https://bugzilla.redhat.com/show_bug.cgi?id=625967#c8}. If REPAIR is
  279. false, do not write to DEVICE. If it's #t, fix any errors found. Otherwise,
  280. fix only those considered safe to repair automatically."
  281. (if force?
  282. (match (status:exit-val
  283. (apply system* `("btrfs" "check" "--progress"
  284. ;; Btrfs's ‘--force’ is not relevant to us here.
  285. ,@(match repair
  286. ;; Upstream considers ALL repairs dangerous
  287. ;; and will warn the user at run time.
  288. (#t '("--repair"))
  289. (_ '("--readonly" ; a no-op for clarity
  290. ;; A 466G file system with 180G used is
  291. ;; enough to kill btrfs with 6G of RAM.
  292. "--mode" "lowmem")))
  293. ,device)))
  294. (0 'pass)
  295. (_ 'fatal-error))
  296. 'pass))
  297. ;;;
  298. ;;; FAT32 file systems.
  299. ;;;
  300. ;; <http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-107.pdf>.
  301. (define (fat32-superblock? sblock)
  302. "Return #t when SBLOCK is a fat32 superblock."
  303. (bytevector=? (sub-bytevector sblock 82 8)
  304. (string->utf8 "FAT32 ")))
  305. (define (read-fat32-superblock device)
  306. "Return the raw contents of DEVICE's fat32 superblock as a bytevector, or
  307. #f if DEVICE does not contain a fat32 file system."
  308. (read-superblock device 0 90 fat32-superblock?))
  309. (define (fat32-superblock-uuid sblock)
  310. "Return the Volume ID of a fat superblock SBLOCK as a 4-byte bytevector."
  311. (sub-bytevector sblock 67 4))
  312. (define (fat32-superblock-volume-name sblock)
  313. "Return the volume name of fat superblock SBLOCK as a string of at most 11
  314. characters, or #f if SBLOCK has no volume name. The volume name is a latin1
  315. string. Trailing spaces are trimmed."
  316. (string-trim-right (latin1->string (sub-bytevector sblock 71 11) (lambda (c) #f)) #\space))
  317. (define (check-fat-file-system device force? repair)
  318. "Return the health of an unmounted FAT file system on DEVICE. FORCE? is
  319. ignored: a full file system scan is always performed. If REPAIR is false, do
  320. not write to the file system to fix errors. Otherwise, automatically fix them
  321. using the least destructive approach."
  322. (match (status:exit-val
  323. (apply system* `("fsck.vfat" "-v"
  324. ,@(match repair
  325. (#f '("-n"))
  326. (_ '("-a"))) ; no 'safe/#t distinction
  327. ,device)))
  328. (0 'pass)
  329. (1 'errors-corrected)
  330. (_ 'fatal-error)))
  331. ;;;
  332. ;;; FAT16 file systems.
  333. ;;;
  334. (define (fat16-superblock? sblock)
  335. "Return #t when SBLOCK is a fat16 boot record."
  336. (bytevector=? (sub-bytevector sblock 54 8)
  337. (string->utf8 "FAT16 ")))
  338. (define (read-fat16-superblock device)
  339. "Return the raw contents of DEVICE's fat16 superblock as a bytevector, or
  340. #f if DEVICE does not contain a fat16 file system."
  341. (read-superblock device 0 62 fat16-superblock?))
  342. (define (fat16-superblock-uuid sblock)
  343. "Return the Volume ID of a fat superblock SBLOCK as a 4-byte bytevector."
  344. (sub-bytevector sblock 39 4))
  345. (define (fat16-superblock-volume-name sblock)
  346. "Return the volume name of fat superblock SBLOCK as a string of at most 11
  347. characters, or #f if SBLOCK has no volume name. The volume name is a latin1
  348. string. Trailing spaces are trimmed."
  349. (string-trim-right (latin1->string (sub-bytevector sblock 43 11)
  350. (lambda (c) #f))
  351. #\space))
  352. ;;;
  353. ;;; ISO9660 file systems.
  354. ;;;
  355. ;; <http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-119.pdf>.
  356. (define (iso9660-superblock? sblock)
  357. "Return #t when SBLOCK is an iso9660 volume descriptor."
  358. (bytevector=? (sub-bytevector sblock 1 6)
  359. ;; Note: "\x01" is the volume descriptor format version
  360. (string->utf8 "CD001\x01")))
  361. (define (read-iso9660-primary-volume-descriptor device offset)
  362. "Find and read the first primary volume descriptor, starting at OFFSET.
  363. Return #f if not found."
  364. (let* ((sblock (read-superblock device offset 2048 iso9660-superblock?))
  365. (type-code (if sblock
  366. (bytevector-u8-ref sblock 0)
  367. (error (format #f
  368. "Could not read ISO9660 primary
  369. volume descriptor from ~s"
  370. device)))))
  371. (match type-code
  372. (255 #f) ; Volume Descriptor Set Terminator.
  373. (1 sblock) ; Primary Volume Descriptor
  374. (_ (read-iso9660-primary-volume-descriptor device (+ offset 2048))))))
  375. (define (read-iso9660-superblock device)
  376. "Return the raw contents of DEVICE's iso9660 primary volume descriptor
  377. as a bytevector, or #f if DEVICE does not contain an iso9660 file system."
  378. ;; Start reading at sector 16.
  379. ;; Since we are not sure that the device contains an ISO9660 file system,
  380. ;; we have to find that out first.
  381. (if (read-superblock device (* 2048 16) 2048 iso9660-superblock?)
  382. (read-iso9660-primary-volume-descriptor device (* 2048 16))
  383. #f)) ; Device does not contain an iso9660 file system.
  384. (define (iso9660-superblock-uuid sblock)
  385. "Return the modification time of an iso9660 primary volume descriptor
  386. SBLOCK as a bytevector. If that's not set, returns the creation time."
  387. ;; Drops GMT offset for compatibility with Grub, blkid and /dev/disk/by-uuid.
  388. ;; Compare Grub: "2014-12-02-19-30-23-00".
  389. ;; Compare blkid result: "2014-12-02-19-30-23-00".
  390. ;; Compare /dev/disk/by-uuid entry: "2014-12-02-19-30-23-00".
  391. (let* ((creation-time (sub-bytevector sblock 813 17))
  392. (modification-time (sub-bytevector sblock 830 17))
  393. (unset-time (make-bytevector 17 0))
  394. (time (if (bytevector=? unset-time modification-time)
  395. creation-time
  396. modification-time)))
  397. (sub-bytevector time 0 16))) ; strips GMT offset.
  398. (define (iso9660-superblock-volume-name sblock)
  399. "Return the volume name of iso9660 superblock SBLOCK as a string. The volume
  400. name is an ASCII string. Trailing spaces are trimmed."
  401. ;; Note: Valid characters are of the set "[0-9][A-Z]_" (ECMA-119 Appendix A)
  402. (string-trim-right (latin1->string (sub-bytevector sblock 40 32)
  403. (lambda (c) #f)) #\space))
  404. ;;;
  405. ;;; JFS file systems.
  406. ;;;
  407. ;; Taken from <linux-libre>/fs/jfs/jfs_superblock.h.
  408. (define-syntax %jfs-endianness
  409. ;; Endianness of JFS file systems.
  410. (identifier-syntax (endianness little)))
  411. (define (jfs-superblock? sblock)
  412. "Return #t when SBLOCK is a JFS superblock."
  413. (bytevector=? (sub-bytevector sblock 0 4)
  414. (string->utf8 "JFS1")))
  415. (define (read-jfs-superblock device)
  416. "Return the raw contents of DEVICE's JFS superblock as a bytevector, or #f
  417. if DEVICE does not contain a JFS file system."
  418. (read-superblock device 32768 184 jfs-superblock?))
  419. (define (jfs-superblock-uuid sblock)
  420. "Return the UUID of JFS superblock SBLOCK as a 16-byte bytevector."
  421. (sub-bytevector sblock 136 16))
  422. (define (jfs-superblock-volume-name sblock)
  423. "Return the volume name of JFS superblock SBLOCK as a string of at most 16
  424. characters, or #f if SBLOCK has no volume name."
  425. (null-terminated-latin1->string (sub-bytevector sblock 152 16)))
  426. (define (check-jfs-file-system device force? repair)
  427. "Return the health of an unmounted JFS file system on DEVICE. If FORCE? is
  428. true, check the file system even if it's marked as clean. If REPAIR is false,
  429. do not write to the file system to fix errors, and replay the transaction log
  430. only if FORCE? is true. Otherwise, replay the transaction log before checking
  431. and automatically fix found errors."
  432. (match (status:exit-val
  433. (apply system*
  434. `("jfs_fsck" "-v"
  435. ;; The ‘LEVEL’ logic is convoluted. To quote fsck/xchkdsk.c
  436. ;; (‘-p’, ‘-a’, and ‘-r’ are aliases in every way):
  437. ;; “If -f was chosen, have it override [-p] by [forcing] a
  438. ;; check regardless of the outcome after the log is
  439. ;; replayed”.
  440. ;; “If -n is specified by itself, don't replay the journal.
  441. ;; If -n is specified with [-p], replay the journal but
  442. ;; don't make any other changes”.
  443. ,@(if force? '("-f") '())
  444. ,@(match repair
  445. (#f '("-n"))
  446. (_ '("-p"))) ; no 'safe/#t distinction
  447. ,device)))
  448. (0 'pass)
  449. (1 'errors-corrected)
  450. (2 'reboot-required)
  451. (_ 'fatal-error)))
  452. ;;;
  453. ;;; F2FS (Flash-Friendly File System)
  454. ;;;
  455. ;;; https://git.kernel.org/pub/scm/linux/kernel/git/jaegeuk/f2fs.git/tree/include/linux/f2fs_fs.h
  456. ;;; (but using xxd proved to be simpler)
  457. (define-syntax %f2fs-endianness
  458. ;; Endianness of F2FS file systems
  459. (identifier-syntax (endianness little)))
  460. ;; F2FS actually stores two adjacent copies of the superblock.
  461. ;; should we read both?
  462. (define (f2fs-superblock? sblock)
  463. "Return #t when SBLOCK is an F2FS superblock."
  464. (let ((magic (bytevector-u32-ref sblock 0 %f2fs-endianness)))
  465. (= magic #xF2F52010)))
  466. (define (read-f2fs-superblock device)
  467. "Return the raw contents of DEVICE's F2FS superblock as a bytevector, or #f
  468. if DEVICE does not contain an F2FS file system."
  469. (read-superblock device
  470. ;; offset of magic in first copy
  471. #x400
  472. ;; difference between magic of second
  473. ;; and first copies
  474. (- #x1400 #x400)
  475. f2fs-superblock?))
  476. (define (f2fs-superblock-uuid sblock)
  477. "Return the UUID of F2FS superblock SBLOCK as a 16-byte bytevector."
  478. (sub-bytevector sblock
  479. (- (+ #x460 12)
  480. ;; subtract superblock offset
  481. #x400)
  482. 16))
  483. (define (f2fs-superblock-volume-name sblock)
  484. "Return the volume name of F2FS superblock SBLOCK as a string of at most 512
  485. characters, or #f if SBLOCK has no volume name."
  486. (null-terminated-utf16->string
  487. (sub-bytevector sblock (- (+ #x470 12) #x400) 512)
  488. %f2fs-endianness))
  489. (define (check-f2fs-file-system device force? repair)
  490. "Return the health of an unmuounted F2FS file system on DEVICE. If FORCE? is
  491. true, check the file system even if it's marked as clean. If either FORCE? or
  492. REPAIR are true, automatically fix found errors."
  493. ;; There's no ‘-n’ equivalent (‘--dry-run’ does not disable writes).
  494. ;; ’-y’ is an alias of ‘-f’. The man page is bad: read main.c.
  495. (when (and force? (not repair))
  496. (format (current-error-port)
  497. "warning: forced check of F2FS ~a implies repairing any errors~%"
  498. device))
  499. (match (status:exit-val
  500. (apply system* `("fsck.f2fs"
  501. ,@(if force? '("-f") '())
  502. ,@(if repair '("-p") '("--dry-run"))
  503. ,device)))
  504. ;; 0 and -1 are the only two possibilities according to the man page.
  505. (0 'pass)
  506. (_ 'fatal-error)))
  507. ;;;
  508. ;;; LUKS encrypted devices.
  509. ;;;
  510. ;; The LUKS header format is described in "LUKS On-Disk Format Specification":
  511. ;; <https://gitlab.com/cryptsetup/cryptsetup/wikis/Specification>. We follow
  512. ;; version 1.2.1 of this document.
  513. ;; The LUKS2 header format is described in "LUKS2 On-Disk Format Specification":
  514. ;; <https://gitlab.com/cryptsetup/LUKS2-docs/blob/master/luks2_doc_wip.pdf>.
  515. ;; It is a WIP document.
  516. (define-syntax %luks-endianness
  517. ;; Endianness of LUKS headers.
  518. (identifier-syntax (endianness big)))
  519. (define (luks-superblock? sblock)
  520. "Return #t when SBLOCK is a luks superblock."
  521. (define %luks-magic
  522. ;; The 'LUKS_MAGIC' constant.
  523. (u8-list->bytevector (append (map char->integer (string->list "LUKS"))
  524. (list #xba #xbe))))
  525. (let ((magic (sub-bytevector sblock 0 6))
  526. (version (bytevector-u16-ref sblock 6 %luks-endianness)))
  527. (and (bytevector=? magic %luks-magic)
  528. (or (= version 1) (= version 2)))))
  529. (define (read-luks-header file)
  530. "Read a LUKS header from FILE. Return the raw header on success, and #f if
  531. not valid header was found."
  532. ;; Size in bytes of the LUKS binary header, which includes key slots in
  533. ;; LUKS1. In LUKS2 the binary header is partially backward compatible, so
  534. ;; that UUID can be extracted as for LUKS1. Keyslots and other metadata are
  535. ;; not part of this header in LUKS2, but are included in the JSON metadata
  536. ;; area that follows.
  537. (read-superblock file 0 592 luks-superblock?))
  538. (define (luks-header-uuid header)
  539. "Return the LUKS UUID from HEADER, as a 16-byte bytevector."
  540. ;; 40 bytes are reserved for the UUID, but in practice, it contains the 36
  541. ;; bytes of its ASCII representation.
  542. (let ((uuid (sub-bytevector header 168 36)))
  543. (string->uuid (utf8->string uuid))))
  544. ;;;
  545. ;;; NTFS file systems.
  546. ;;;
  547. ;; Taken from <linux-libre>/fs/ntfs/layout.h
  548. (define-syntax %ntfs-endianness
  549. ;; Endianness of NTFS file systems.
  550. (identifier-syntax (endianness little)))
  551. (define (ntfs-superblock? sblock)
  552. "Return #t when SBLOCK is a NTFS superblock."
  553. (bytevector=? (sub-bytevector sblock 3 8)
  554. (string->utf8 "NTFS ")))
  555. (define (read-ntfs-superblock device)
  556. "Return the raw contents of DEVICE's NTFS superblock as a bytevector, or #f
  557. if DEVICE does not contain a NTFS file system."
  558. (read-superblock device 0 511 ntfs-superblock?))
  559. (define (ntfs-superblock-uuid sblock)
  560. "Return the UUID of NTFS superblock SBLOCK as a 8-byte bytevector."
  561. (sub-bytevector sblock 72 8))
  562. ;; TODO: Add ntfs-superblock-volume-name. The partition label is not stored
  563. ;; in the BOOT SECTOR like the UUID, but in the MASTER FILE TABLE, which seems
  564. ;; way harder to access.
  565. (define (check-ntfs-file-system device force? repair)
  566. "Return the health of an unmounted NTFS file system on DEVICE. FORCE? is
  567. ignored: a full check is always performed. Repair is not possible: if REPAIR is
  568. true and the volume has been repaired by an external tool, clear the volume
  569. dirty flag to indicate that it's now safe to mount."
  570. (match (status:exit-val
  571. (apply system* `("ntfsfix"
  572. ,@(if repair '("--clear-dirty") '("--no-action"))
  573. ,device)))
  574. (0 'pass)
  575. (_ 'fatal-error)))
  576. ;;;
  577. ;;; XFS file systems.
  578. ;;;
  579. ;; <https://git.kernel.org/pub/scm/fs/xfs/xfs-documentation.git/tree/design/XFS_Filesystem_Structure/allocation_groups.asciidoc>
  580. (define-syntax %xfs-endianness
  581. ;; Endianness of XFS file systems.
  582. (identifier-syntax (endianness big)))
  583. (define (xfs-superblock? sblock)
  584. "Return #t when SBLOCK is an XFS superblock."
  585. (bytevector=? (sub-bytevector sblock 0 4)
  586. (string->utf8 "XFSB")))
  587. (define (read-xfs-superblock device)
  588. "Return the raw contents of DEVICE's XFS superblock as a bytevector, or #f
  589. if DEVICE does not contain an XFS file system."
  590. (read-superblock device 0 120 xfs-superblock?))
  591. (define (xfs-superblock-uuid sblock)
  592. "Return the UUID of XFS superblock SBLOCK as a 16-byte bytevector."
  593. (sub-bytevector sblock 32 16))
  594. (define (xfs-superblock-volume-name sblock)
  595. "Return the volume name of XFS superblock SBLOCK as a string of at most 12
  596. characters, or #f if SBLOCK has no volume name."
  597. (null-terminated-latin1->string (sub-bytevector sblock 108 12)))
  598. (define (check-xfs-file-system device force? repair)
  599. "Return the health of an unmounted XFS file system on DEVICE. If FORCE? is
  600. false, return 'PASS unconditionally as XFS claims no need for off-line checks.
  601. When FORCE? is true, do perform a thorough check. If REPAIR is false, do not
  602. write to DEVICE. If it's #t, replay the log, check, and fix any errors found.
  603. Otherwise, only replay the log, and check without attempting further repairs."
  604. (define (xfs_repair)
  605. (status:exit-val
  606. (apply system* `("xfs_repair" "-Pv"
  607. ,@(match repair
  608. (#t '("-e"))
  609. (_ '("-n"))) ; will miss some errors
  610. ,device))))
  611. (if force?
  612. ;; xfs_repair fails with exit status 2 if the log is dirty, which is
  613. ;; likely in situations where you're running xfs_repair. Only the kernel
  614. ;; can replay the log by {,un}mounting it cleanly.
  615. (match (let ((status (xfs_repair)))
  616. (if (and repair (eq? 2 status))
  617. (let ((target "/replay-XFS-log"))
  618. ;; The kernel helpfully prints a ‘Mounting…’ notice for us.
  619. (mkdir target)
  620. (mount device target "xfs")
  621. (umount target)
  622. (rmdir target)
  623. (xfs_repair))
  624. status))
  625. (0 'pass)
  626. (4 'errors-corrected)
  627. (_ 'fatal-error))
  628. 'pass))
  629. ;;;
  630. ;;; Partition lookup.
  631. ;;;
  632. (define (disk-partitions)
  633. "Return the list of device names corresponding to valid disk partitions."
  634. (define (partition? name major minor)
  635. ;; grub-mkrescue does some funny things for EFI support which
  636. ;; makes it a lot more difficult than one would expect to support
  637. ;; booting an ISO-9660 image from an USB flash drive.
  638. ;; For example there's a buggy (too small) hidden partition in it
  639. ;; which Linux mounts and then proceeds to fail while trying to
  640. ;; fall off the edge.
  641. ;; In any case, partition tables are supposed to be optional so
  642. ;; here we allow checking entire disks for file systems, too.
  643. (> major 2)) ;ignore RAM disks and floppy disks
  644. (call-with-input-file "/proc/partitions"
  645. (lambda (port)
  646. ;; Skip the two header lines.
  647. (read-line port)
  648. (read-line port)
  649. ;; Read each subsequent line, and extract the last space-separated
  650. ;; field.
  651. (let loop ((parts '()))
  652. (let ((line (read-line port)))
  653. (if (eof-object? line)
  654. (reverse parts)
  655. (match (string-tokenize line)
  656. (((= string->number major) (= string->number minor)
  657. blocks name)
  658. (if (partition? name major minor)
  659. (loop (cons name parts))
  660. (loop parts))))))))))
  661. (define (ENOENT-safe proc)
  662. "Wrap the one-argument PROC such that ENOENT, EIO, and ENOMEDIUM errors are
  663. caught and lead to a warning and #f as the result."
  664. (lambda (device)
  665. (catch 'system-error
  666. (lambda ()
  667. (proc device))
  668. (lambda args
  669. (let ((errno (system-error-errno args)))
  670. (cond ((= ENOENT errno)
  671. (format (current-error-port)
  672. "warning: device '~a' not found~%" device)
  673. #f)
  674. ((= ENOMEDIUM errno) ;for removable media
  675. #f)
  676. ((= EIO errno) ;unreadable hardware like audio CDs
  677. (format (current-error-port)
  678. "warning: failed to read from device '~a'~%" device)
  679. #f)
  680. (else
  681. (apply throw args))))))))
  682. (define (partition-field-reader read field)
  683. "Return a procedure that takes a device and returns the value of a FIELD in
  684. the partition superblock or #f."
  685. (lambda (device)
  686. (let ((sblock (read device)))
  687. (and sblock
  688. (field sblock)))))
  689. (define (read-partition-field device partition-field-readers)
  690. "Returns the value of a FIELD in the partition superblock of DEVICE or #f. It
  691. takes a list of PARTITION-FIELD-READERS and returns the result of the first
  692. partition field reader that returned a value."
  693. (match (filter-map (cut apply <> (list device)) partition-field-readers)
  694. ((field . _) field)
  695. (_ #f)))
  696. (define %partition-label-readers
  697. (list (partition-field-reader read-iso9660-superblock
  698. iso9660-superblock-volume-name)
  699. (partition-field-reader read-ext2-superblock
  700. ext2-superblock-volume-name)
  701. (partition-field-reader read-linux-swap-superblock
  702. linux-swap-superblock-volume-name)
  703. (partition-field-reader read-bcachefs-superblock
  704. bcachefs-superblock-volume-name)
  705. (partition-field-reader read-btrfs-superblock
  706. btrfs-superblock-volume-name)
  707. (partition-field-reader read-fat32-superblock
  708. fat32-superblock-volume-name)
  709. (partition-field-reader read-fat16-superblock
  710. fat16-superblock-volume-name)
  711. (partition-field-reader read-jfs-superblock
  712. jfs-superblock-volume-name)
  713. (partition-field-reader read-f2fs-superblock
  714. f2fs-superblock-volume-name)
  715. (partition-field-reader read-xfs-superblock
  716. xfs-superblock-volume-name)))
  717. (define %partition-uuid-readers
  718. (list (partition-field-reader read-iso9660-superblock
  719. iso9660-superblock-uuid)
  720. (partition-field-reader read-ext2-superblock
  721. ext2-superblock-uuid)
  722. (partition-field-reader read-linux-swap-superblock
  723. linux-swap-superblock-uuid)
  724. (partition-field-reader read-bcachefs-superblock
  725. bcachefs-superblock-external-uuid)
  726. (partition-field-reader read-btrfs-superblock
  727. btrfs-superblock-uuid)
  728. (partition-field-reader read-fat32-superblock
  729. fat32-superblock-uuid)
  730. (partition-field-reader read-fat16-superblock
  731. fat16-superblock-uuid)
  732. (partition-field-reader read-jfs-superblock
  733. jfs-superblock-uuid)
  734. (partition-field-reader read-f2fs-superblock
  735. f2fs-superblock-uuid)
  736. (partition-field-reader read-ntfs-superblock
  737. ntfs-superblock-uuid)
  738. (partition-field-reader read-xfs-superblock
  739. xfs-superblock-uuid)))
  740. (define read-partition-label
  741. (cut read-partition-field <> %partition-label-readers))
  742. (define read-partition-uuid
  743. (cut read-partition-field <> %partition-uuid-readers))
  744. (define luks-partition-field-reader
  745. (partition-field-reader read-luks-header luks-header-uuid))
  746. (define read-luks-partition-uuid
  747. (cut read-partition-field <> (list luks-partition-field-reader)))
  748. (define (partition-predicate reader =)
  749. "Return a predicate that returns true if the FIELD of partition header that
  750. was READ is = to the given value."
  751. ;; When running on the hand-made /dev, 'disk-partitions' could return
  752. ;; partitions for which we have no /dev node. Handle that gracefully.
  753. (let ((reader (ENOENT-safe reader)))
  754. (lambda (expected)
  755. (lambda (device)
  756. (let ((actual (reader device)))
  757. (and actual
  758. (= actual expected)))))))
  759. (define partition-label-predicate
  760. (partition-predicate read-partition-label string=?))
  761. (define partition-uuid-predicate
  762. (partition-predicate read-partition-uuid uuid=?))
  763. (define luks-partition-uuid-predicate
  764. (partition-predicate luks-partition-field-reader uuid=?))
  765. (define (find-partition predicate)
  766. "Return the first partition found that matches PREDICATE, or #f if none
  767. were found."
  768. (lambda (expected)
  769. (find (predicate expected)
  770. (map (cut string-append "/dev/" <>)
  771. (disk-partitions)))))
  772. (define find-partition-by-label
  773. (find-partition partition-label-predicate))
  774. (define find-partition-by-uuid
  775. (find-partition partition-uuid-predicate))
  776. (define find-partition-by-luks-uuid
  777. (find-partition luks-partition-uuid-predicate))
  778. (define (canonicalize-device-spec spec)
  779. "Return the device name corresponding to SPEC, which can be a <uuid>, a
  780. <file-system-label>, or a string (typically a /dev file name or an nfs-root
  781. containing ':/')."
  782. (define max-trials
  783. ;; Number of times we retry partition label resolution, 1 second per
  784. ;; trial. Note: somebody reported a delay of 16 seconds (!) before their
  785. ;; USB key would be detected by the kernel, so we must wait for at least
  786. ;; this long.
  787. 20)
  788. (define (resolve find-partition spec fmt)
  789. (let loop ((count 0))
  790. (let ((device (find-partition spec)))
  791. (or device
  792. ;; Some devices take a bit of time to appear, most notably USB
  793. ;; storage devices. Thus, wait for the device to appear.
  794. (if (> count max-trials)
  795. (error "failed to resolve partition" (fmt spec))
  796. (begin
  797. (format #t "waiting for partition '~a' to appear...~%"
  798. (fmt spec))
  799. (sleep 1)
  800. (loop (+ 1 count))))))))
  801. (match spec
  802. ((? string?)
  803. (if (string-contains spec ":/")
  804. spec ; do not resolve NFS devices
  805. ;; Nothing to do, but wait until SPEC shows up.
  806. (resolve identity spec identity)))
  807. ((? file-system-label?)
  808. ;; Resolve the label.
  809. (resolve find-partition-by-label
  810. (file-system-label->string spec)
  811. identity))
  812. ((? uuid?)
  813. (resolve find-partition-by-uuid
  814. (uuid-bytevector spec)
  815. uuid->string))))
  816. (define (check-file-system device type force? repair)
  817. "Check an unmounted TYPE file system on DEVICE. Do nothing but warn if it is
  818. mounted. If FORCE? is true, check even when considered unnecessary. If REPAIR
  819. is false, try not to write to DEVICE at all. If it's #t, try to fix all errors
  820. found. Otherwise, fix only those considered safe to repair automatically. Not
  821. all TYPEs support all values or combinations of FORCE? and REPAIR. Don't throw
  822. an exception in such cases but perform the nearest sane action."
  823. (define check-procedure
  824. (cond
  825. ((string-prefix? "ext" type) check-ext2-file-system)
  826. ((string-prefix? "bcachefs" type) check-bcachefs-file-system)
  827. ((string-prefix? "btrfs" type) check-btrfs-file-system)
  828. ((string-suffix? "fat" type) check-fat-file-system)
  829. ((string-prefix? "jfs" type) check-jfs-file-system)
  830. ((string-prefix? "f2fs" type) check-f2fs-file-system)
  831. ((string-prefix? "ntfs" type) check-ntfs-file-system)
  832. ((string-prefix? "nfs" type) (const 'pass))
  833. ((string-prefix? "xfs" type) check-xfs-file-system)
  834. (else #f)))
  835. (if check-procedure
  836. (let ((mount (find (lambda (mount)
  837. (string=? device (mount-source mount)))
  838. (mounts))))
  839. (if mount
  840. (format (current-error-port)
  841. "Refusing to check ~a file system already mounted at ~a~%"
  842. device (mount-point mount))
  843. (match (check-procedure device force? repair)
  844. ('pass
  845. #t)
  846. ('errors-corrected
  847. (format (current-error-port)
  848. "File system check corrected errors on ~a; continuing~%"
  849. device))
  850. ('reboot-required
  851. (format (current-error-port)
  852. "File system check corrected errors on ~a; rebooting~%"
  853. device)
  854. (sleep 3)
  855. (reboot))
  856. ('fatal-error
  857. (format (current-error-port) "File system check on ~a failed~%"
  858. device)
  859. ;; Spawn a REPL only if someone might interact with it.
  860. (when (isatty? (current-input-port))
  861. (format (current-error-port) "Spawning Bourne-like REPL.~%")
  862. ;; 'current-output-port' is typically connected to /dev/klog
  863. ;; (in PID 1), but here we want to make sure we talk directly
  864. ;; to the user.
  865. (with-output-to-file "/dev/console"
  866. (lambda ()
  867. (start-repl %bournish-language))))))))
  868. (format (current-error-port)
  869. "No file system check procedure for ~a; skipping~%"
  870. device)))
  871. (define (mount-flags->bit-mask flags)
  872. "Return the number suitable for the 'flags' argument of 'mount' that
  873. corresponds to the symbols listed in FLAGS."
  874. (let loop ((flags flags))
  875. (match flags
  876. (('read-only rest ...)
  877. (logior MS_RDONLY (loop rest)))
  878. (('bind-mount rest ...)
  879. (logior MS_BIND (loop rest)))
  880. (('no-suid rest ...)
  881. (logior MS_NOSUID (loop rest)))
  882. (('no-dev rest ...)
  883. (logior MS_NODEV (loop rest)))
  884. (('no-exec rest ...)
  885. (logior MS_NOEXEC (loop rest)))
  886. (('no-atime rest ...)
  887. (logior MS_NOATIME (loop rest)))
  888. (('strict-atime rest ...)
  889. (logior MS_STRICTATIME (loop rest)))
  890. (('lazy-time rest ...)
  891. (logior MS_LAZYTIME (loop rest)))
  892. (()
  893. 0))))
  894. (define* (mount-file-system fs #:key (root "/root")
  895. (check? (file-system-check? fs))
  896. (skip-check-if-clean?
  897. (file-system-skip-check-if-clean? fs))
  898. (repair (file-system-repair fs)))
  899. "Mount the file system described by FS, a <file-system> object, under ROOT."
  900. (define (mount-nfs source mount-point type flags options)
  901. (let* ((idx (string-rindex source #\:))
  902. (host-part (string-take source idx))
  903. ;; Strip [] from around host if present
  904. (host (match (string-split host-part (string->char-set "[]"))
  905. (("" h "") h)
  906. ((h) h)))
  907. (aa (match (getaddrinfo host "nfs") ((x . _) x)))
  908. (sa (addrinfo:addr aa))
  909. (inet-addr (inet-ntop (sockaddr:fam sa)
  910. (sockaddr:addr sa))))
  911. ;; Mounting an NFS file system requires passing the address
  912. ;; of the server in the addr= option
  913. (mount source mount-point type flags
  914. (string-append "addr="
  915. inet-addr
  916. (if options
  917. (string-append "," options)
  918. "")))))
  919. (let* ((type (file-system-type fs))
  920. (source (canonicalize-device-spec (file-system-device fs)))
  921. (target (string-append root "/"
  922. (file-system-mount-point fs)))
  923. (flags (logior (mount-flags->bit-mask (file-system-flags fs))
  924. ;; For bind mounts, preserve the original flags such
  925. ;; as MS_NOSUID, etc. Failing to do that, the
  926. ;; MS_REMOUNT call below fails with EPERM.
  927. ;; See <https://bugs.gnu.org/46292>
  928. (if (memq 'bind-mount (file-system-flags fs))
  929. (statfs-flags->mount-flags
  930. (file-system-mount-flags (statfs source)))
  931. 0)))
  932. (options (file-system-options fs)))
  933. (when check?
  934. (check-file-system source type (not skip-check-if-clean?) repair))
  935. (catch 'system-error
  936. (lambda ()
  937. ;; Create the mount point. Most of the time this is a directory, but
  938. ;; in the case of a bind mount, a regular file or socket may be
  939. ;; needed.
  940. (if (and (= MS_BIND (logand flags MS_BIND))
  941. (not (file-is-directory? source)))
  942. (unless (file-exists? target)
  943. (mkdir-p (dirname target))
  944. (call-with-output-file target (const #t)))
  945. (mkdir-p target))
  946. (cond
  947. ((string-prefix? "nfs" type)
  948. (mount-nfs source target type flags options))
  949. (else
  950. (mount source target type flags options)))
  951. ;; For read-only bind mounts, an extra remount is needed, as per
  952. ;; <http://lwn.net/Articles/281157/>, which still applies to Linux
  953. ;; 4.0.
  954. (when (and (= MS_BIND (logand flags MS_BIND))
  955. (= MS_RDONLY (logand flags MS_RDONLY)))
  956. (let ((flags (logior MS_REMOUNT flags)))
  957. (mount source target type flags options))))
  958. (lambda args
  959. (or (file-system-mount-may-fail? fs)
  960. (apply throw args))))))
  961. ;;; file-systems.scm ends here