file-systems.scm 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223
  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. (else
  768. (apply throw args))))))))
  769. (define (partition-field-reader read field)
  770. "Return a procedure that takes a device and returns the value of a FIELD in
  771. the partition superblock or #f."
  772. (lambda (device)
  773. (let ((sblock (read device)))
  774. (and sblock
  775. (field sblock)))))
  776. (define (read-partition-field device partition-field-readers)
  777. "Returns the value of a FIELD in the partition superblock of DEVICE or #f. It
  778. takes a list of PARTITION-FIELD-READERS and returns the result of the first
  779. partition field reader that returned a value."
  780. (match (filter-map (cut apply <> (list device)) partition-field-readers)
  781. ((field . _) field)
  782. (_ #f)))
  783. (define %partition-label-readers
  784. (list (partition-field-reader read-iso9660-superblock
  785. iso9660-superblock-volume-name)
  786. (partition-field-reader read-ext2-superblock
  787. ext2-superblock-volume-name)
  788. (partition-field-reader read-linux-swap-superblock
  789. linux-swap-superblock-volume-name)
  790. (partition-field-reader read-bcachefs-superblock
  791. bcachefs-superblock-volume-name)
  792. (partition-field-reader read-btrfs-superblock
  793. btrfs-superblock-volume-name)
  794. (partition-field-reader read-fat32-superblock
  795. fat32-superblock-volume-name)
  796. (partition-field-reader read-fat16-superblock
  797. fat16-superblock-volume-name)
  798. (partition-field-reader read-jfs-superblock
  799. jfs-superblock-volume-name)
  800. (partition-field-reader read-f2fs-superblock
  801. f2fs-superblock-volume-name)
  802. (partition-field-reader read-xfs-superblock
  803. xfs-superblock-volume-name)))
  804. (define %partition-uuid-readers
  805. (list (partition-field-reader read-iso9660-superblock
  806. iso9660-superblock-uuid)
  807. (partition-field-reader read-ext2-superblock
  808. ext2-superblock-uuid)
  809. (partition-field-reader read-linux-swap-superblock
  810. linux-swap-superblock-uuid)
  811. (partition-field-reader read-bcachefs-superblock
  812. bcachefs-superblock-external-uuid)
  813. (partition-field-reader read-btrfs-superblock
  814. btrfs-superblock-uuid)
  815. (partition-field-reader read-fat32-superblock
  816. fat32-superblock-uuid)
  817. (partition-field-reader read-fat16-superblock
  818. fat16-superblock-uuid)
  819. (partition-field-reader read-jfs-superblock
  820. jfs-superblock-uuid)
  821. (partition-field-reader read-f2fs-superblock
  822. f2fs-superblock-uuid)
  823. (partition-field-reader read-ntfs-superblock
  824. ntfs-superblock-uuid)
  825. (partition-field-reader read-xfs-superblock
  826. xfs-superblock-uuid)))
  827. (define read-partition-label
  828. (cut read-partition-field <> %partition-label-readers))
  829. (define read-partition-uuid
  830. (cut read-partition-field <> %partition-uuid-readers))
  831. (define luks-partition-field-reader
  832. (partition-field-reader read-luks-header luks-header-uuid))
  833. (define read-luks-partition-uuid
  834. (cut read-partition-field <> (list luks-partition-field-reader)))
  835. (define (partition-predicate reader =)
  836. "Return a predicate that returns true if the FIELD of partition header that
  837. was READ is = to the given value."
  838. ;; When running on the hand-made /dev, 'disk-partitions' could return
  839. ;; partitions for which we have no /dev node. Handle that gracefully.
  840. (let ((reader (ENOENT-safe reader)))
  841. (lambda (expected)
  842. (lambda (device)
  843. (let ((actual (reader device)))
  844. (and actual
  845. (= actual expected)))))))
  846. (define partition-label-predicate
  847. (partition-predicate read-partition-label string=?))
  848. (define partition-uuid-predicate
  849. (partition-predicate read-partition-uuid uuid=?))
  850. (define luks-partition-uuid-predicate
  851. (partition-predicate luks-partition-field-reader uuid=?))
  852. (define (find-partition predicate)
  853. "Return the first partition found that matches PREDICATE, or #f if none
  854. were found."
  855. (lambda (expected)
  856. (find (predicate expected)
  857. (map (cut string-append "/dev/" <>)
  858. (disk-partitions)))))
  859. (define find-partition-by-label
  860. (find-partition partition-label-predicate))
  861. (define find-partition-by-uuid
  862. (find-partition partition-uuid-predicate))
  863. (define find-partition-by-luks-uuid
  864. (find-partition luks-partition-uuid-predicate))
  865. (define (canonicalize-device-spec spec)
  866. "Return the device name corresponding to SPEC, which can be a <uuid>, a
  867. <file-system-label>, or a string (typically a /dev file name or an nfs-root
  868. containing ':/')."
  869. (define max-trials
  870. ;; Number of times we retry partition label resolution, 1 second per
  871. ;; trial. Note: somebody reported a delay of 16 seconds (!) before their
  872. ;; USB key would be detected by the kernel, so we must wait for at least
  873. ;; this long.
  874. 20)
  875. (define (resolve find-partition spec fmt)
  876. (let loop ((count 0))
  877. (let ((device (find-partition spec)))
  878. (or device
  879. ;; Some devices take a bit of time to appear, most notably USB
  880. ;; storage devices. Thus, wait for the device to appear.
  881. (if (> count max-trials)
  882. (error "failed to resolve partition" (fmt spec))
  883. (begin
  884. (format #t "waiting for partition '~a' to appear...~%"
  885. (fmt spec))
  886. (sleep 1)
  887. (loop (+ 1 count))))))))
  888. (match spec
  889. ((? string?)
  890. (if (string-contains spec ":/")
  891. spec ; do not resolve NFS devices
  892. ;; Nothing to do, but wait until SPEC shows up.
  893. (resolve identity spec identity)))
  894. ((? file-system-label?)
  895. ;; Resolve the label.
  896. (resolve find-partition-by-label
  897. (file-system-label->string spec)
  898. identity))
  899. ((? uuid?)
  900. (resolve find-partition-by-uuid
  901. (uuid-bytevector spec)
  902. uuid->string))))
  903. (define (check-file-system device type force? repair)
  904. "Check an unmounted TYPE file system on DEVICE. Do nothing but warn if it is
  905. mounted. If FORCE? is true, check even when considered unnecessary. If REPAIR
  906. is false, try not to write to DEVICE at all. If it's #t, try to fix all errors
  907. found. Otherwise, fix only those considered safe to repair automatically. Not
  908. all TYPEs support all values or combinations of FORCE? and REPAIR. Don't throw
  909. an exception in such cases but perform the nearest sane action."
  910. (define check-procedure
  911. (cond
  912. ((string-prefix? "ext" type) check-ext2-file-system)
  913. ((string-prefix? "bcachefs" type) check-bcachefs-file-system)
  914. ((string-prefix? "btrfs" type) check-btrfs-file-system)
  915. ((string-suffix? "fat" type) check-fat-file-system)
  916. ((string-prefix? "jfs" type) check-jfs-file-system)
  917. ((string-prefix? "f2fs" type) check-f2fs-file-system)
  918. ((string-prefix? "ntfs" type) check-ntfs-file-system)
  919. ((string-prefix? "nfs" type) (const 'pass))
  920. ((string-prefix? "xfs" type) check-xfs-file-system)
  921. (else #f)))
  922. (if check-procedure
  923. (let ((mount (find (lambda (mount)
  924. (string=? device (mount-source mount)))
  925. (mounts))))
  926. (if mount
  927. (format (current-error-port)
  928. "Refusing to check ~a file system already mounted at ~a~%"
  929. device (mount-point mount))
  930. (match (check-procedure device force? repair)
  931. ('pass
  932. #t)
  933. ('errors-corrected
  934. (format (current-error-port)
  935. "File system check corrected errors on ~a; continuing~%"
  936. device))
  937. ('reboot-required
  938. (format (current-error-port)
  939. "File system check corrected errors on ~a; rebooting~%"
  940. device)
  941. (sleep 3)
  942. (reboot))
  943. ('fatal-error
  944. (format (current-error-port) "File system check on ~a failed~%"
  945. device)
  946. ;; Spawn a REPL only if someone might interact with it.
  947. (when (isatty? (current-input-port))
  948. (format (current-error-port) "Spawning Bourne-like REPL.~%")
  949. ;; 'current-output-port' is typically connected to /dev/klog
  950. ;; (in PID 1), but here we want to make sure we talk directly
  951. ;; to the user.
  952. (with-output-to-file "/dev/console"
  953. (lambda ()
  954. (start-repl %bournish-language))))))))
  955. (format (current-error-port)
  956. "No file system check procedure for ~a; skipping~%"
  957. device)))
  958. (define (mount-flags->bit-mask flags)
  959. "Return the number suitable for the 'flags' argument of 'mount' that
  960. corresponds to the symbols listed in FLAGS."
  961. (let loop ((flags flags))
  962. (match flags
  963. (('read-only rest ...)
  964. (logior MS_RDONLY (loop rest)))
  965. (('bind-mount rest ...)
  966. (logior MS_BIND (loop rest)))
  967. (('no-suid rest ...)
  968. (logior MS_NOSUID (loop rest)))
  969. (('no-dev rest ...)
  970. (logior MS_NODEV (loop rest)))
  971. (('no-exec rest ...)
  972. (logior MS_NOEXEC (loop rest)))
  973. (('no-atime rest ...)
  974. (logior MS_NOATIME (loop rest)))
  975. (('strict-atime rest ...)
  976. (logior MS_STRICTATIME (loop rest)))
  977. (('lazy-time rest ...)
  978. (logior MS_LAZYTIME (loop rest)))
  979. (('shared rest ...)
  980. (loop rest))
  981. (()
  982. 0))))
  983. (define* (mount-file-system fs #:key (root "/root")
  984. (check? (file-system-check? fs))
  985. (skip-check-if-clean?
  986. (file-system-skip-check-if-clean? fs))
  987. (repair (file-system-repair fs)))
  988. "Mount the file system described by FS, a <file-system> object, under ROOT."
  989. (define (mount-nfs source mount-point type flags options)
  990. (let* ((idx (string-rindex source #\:))
  991. (host-part (string-take source idx))
  992. ;; Strip [] from around host if present
  993. (host (match (string-split host-part (string->char-set "[]"))
  994. (("" h "") h)
  995. ((h) h)))
  996. (aa (match (getaddrinfo host "nfs") ((x . _) x)))
  997. (sa (addrinfo:addr aa))
  998. (inet-addr (inet-ntop (sockaddr:fam sa)
  999. (sockaddr:addr sa))))
  1000. ;; Mounting an NFS file system requires passing the address
  1001. ;; of the server in the addr= option
  1002. (mount source mount-point type flags
  1003. (string-append "addr="
  1004. inet-addr
  1005. (if options
  1006. (string-append "," options)
  1007. "")))))
  1008. (let* ((type (file-system-type fs))
  1009. (source (canonicalize-device-spec (file-system-device fs)))
  1010. (target (string-append root "/"
  1011. (file-system-mount-point fs)))
  1012. (flags (logior (mount-flags->bit-mask (file-system-flags fs))
  1013. ;; For bind mounts, preserve the original flags such
  1014. ;; as MS_NOSUID, etc. Failing to do that, the
  1015. ;; MS_REMOUNT call below fails with EPERM.
  1016. ;; See <https://bugs.gnu.org/46292>
  1017. (if (memq 'bind-mount (file-system-flags fs))
  1018. (statfs-flags->mount-flags
  1019. (file-system-mount-flags (statfs source)))
  1020. 0)))
  1021. (options (file-system-options fs)))
  1022. (when check?
  1023. (check-file-system source type (not skip-check-if-clean?) repair))
  1024. (catch 'system-error
  1025. (lambda ()
  1026. ;; Create the mount point. Most of the time this is a directory, but
  1027. ;; in the case of a bind mount, a regular file or socket may be
  1028. ;; needed.
  1029. (if (and (= MS_BIND (logand flags MS_BIND))
  1030. (not (file-is-directory? source)))
  1031. (unless (file-exists? target)
  1032. (mkdir-p (dirname target))
  1033. (close-fdes
  1034. (open-fdes target (logior O_WRONLY O_CREAT O_CLOEXEC))))
  1035. (mkdir-p target))
  1036. (cond
  1037. ((string-prefix? "nfs" type)
  1038. (mount-nfs source target type flags options))
  1039. ((memq 'shared (file-system-flags fs))
  1040. (mount source target type flags options)
  1041. (mount "none" target #f MS_SHARED))
  1042. (else
  1043. (mount source target type flags options)))
  1044. ;; For read-only bind mounts, an extra remount is needed, as per
  1045. ;; <http://lwn.net/Articles/281157/>, which still applies to Linux
  1046. ;; 4.0.
  1047. (when (and (= MS_BIND (logand flags MS_BIND))
  1048. (= MS_RDONLY (logand flags MS_RDONLY)))
  1049. (let ((flags (logior MS_REMOUNT flags)))
  1050. (mount source target type flags options))))
  1051. (lambda args
  1052. (or (file-system-mount-may-fail? fs)
  1053. (apply throw args))))))
  1054. ;;; file-systems.scm ends here