file-systems.scm 36 KB

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