file-systems.scm 48 KB

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