file-systems.scm 45 KB

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