file-systems.scm 35 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, 2020 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. ;; We completely ignore the back-up superblock & any checksum errors.
  205. ;; Superblock field names, with offset & length respectively, in bytes:
  206. ;; 0 16 bch_csum
  207. ;; 16 8 version
  208. ;; 24 16 magic
  209. ;; 40 16 uuid ← ‘internal UUID’, you probably don't want this
  210. ;; 56 16 user_uuid ← ‘external UUID’, the one by which to mount
  211. ;; 72 32 label
  212. ;; … there are more & the superblock is extensible, but we don't care yet.
  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. (status:exit-val
  227. (apply system* "bcachefs" "fsck" "-p" "-v"
  228. ;; Make each multi-device member a separate argument.
  229. (string-split device #\:)))))
  230. (match (logand (lognot ignored-bits) status)
  231. (0 'pass)
  232. (1 'errors-corrected)
  233. (_ 'fatal-error))))
  234. ;;;
  235. ;;; Btrfs file systems.
  236. ;;;
  237. ;; <https://btrfs.wiki.kernel.org/index.php/On-disk_Format#Superblock>.
  238. (define-syntax %btrfs-endianness
  239. ;; Endianness of btrfs file systems.
  240. (identifier-syntax (endianness little)))
  241. (define (btrfs-superblock? sblock)
  242. "Return #t when SBLOCK is a btrfs superblock."
  243. (bytevector=? (sub-bytevector sblock 64 8)
  244. (string->utf8 "_BHRfS_M")))
  245. (define (read-btrfs-superblock device)
  246. "Return the raw contents of DEVICE's btrfs superblock as a bytevector, or #f
  247. if DEVICE does not contain a btrfs file system."
  248. (read-superblock device 65536 4096 btrfs-superblock?))
  249. (define (btrfs-superblock-uuid sblock)
  250. "Return the UUID of a btrfs superblock SBLOCK as a 16-byte bytevector."
  251. (sub-bytevector sblock 32 16))
  252. (define (btrfs-superblock-volume-name sblock)
  253. "Return the volume name of SBLOCK as a string of at most 256 characters, or
  254. #f if SBLOCK has no volume name."
  255. (null-terminated-latin1->string (sub-bytevector sblock 299 256)))
  256. (define (check-btrfs-file-system device)
  257. "Return the health of a btrfs file system on DEVICE."
  258. (match (status:exit-val
  259. (system* "btrfs" "device" "scan"))
  260. (0 'pass)
  261. (_ 'fatal-error)))
  262. ;;;
  263. ;;; FAT32 file systems.
  264. ;;;
  265. ;; <http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-107.pdf>.
  266. (define (fat32-superblock? sblock)
  267. "Return #t when SBLOCK is a fat32 superblock."
  268. (bytevector=? (sub-bytevector sblock 82 8)
  269. (string->utf8 "FAT32 ")))
  270. (define (read-fat32-superblock device)
  271. "Return the raw contents of DEVICE's fat32 superblock as a bytevector, or
  272. #f if DEVICE does not contain a fat32 file system."
  273. (read-superblock device 0 90 fat32-superblock?))
  274. (define (fat32-superblock-uuid sblock)
  275. "Return the Volume ID of a fat superblock SBLOCK as a 4-byte bytevector."
  276. (sub-bytevector sblock 67 4))
  277. (define (fat32-superblock-volume-name sblock)
  278. "Return the volume name of SBLOCK as a string of at most 11 characters, or
  279. #f if SBLOCK has no volume name. The volume name is a latin1 string.
  280. Trailing spaces are trimmed."
  281. (string-trim-right (latin1->string (sub-bytevector sblock 71 11) (lambda (c) #f)) #\space))
  282. (define (check-fat-file-system device)
  283. "Return the health of a fat file system on DEVICE."
  284. (match (status:exit-val
  285. (system* "fsck.vfat" "-v" "-a" device))
  286. (0 'pass)
  287. (1 'errors-corrected)
  288. (_ 'fatal-error)))
  289. ;;;
  290. ;;; FAT16 file systems.
  291. ;;;
  292. (define (fat16-superblock? sblock)
  293. "Return #t when SBLOCK is a fat16 boot record."
  294. (bytevector=? (sub-bytevector sblock 54 8)
  295. (string->utf8 "FAT16 ")))
  296. (define (read-fat16-superblock device)
  297. "Return the raw contents of DEVICE's fat16 superblock as a bytevector, or
  298. #f if DEVICE does not contain a fat16 file system."
  299. (read-superblock device 0 62 fat16-superblock?))
  300. (define (fat16-superblock-uuid sblock)
  301. "Return the Volume ID of a fat superblock SBLOCK as a 4-byte bytevector."
  302. (sub-bytevector sblock 39 4))
  303. (define (fat16-superblock-volume-name sblock)
  304. "Return the volume name of SBLOCK as a string of at most 11 characters, or
  305. #f if SBLOCK has no volume name. The volume name is a latin1 string.
  306. Trailing spaces are trimmed."
  307. (string-trim-right (latin1->string (sub-bytevector sblock 43 11)
  308. (lambda (c) #f))
  309. #\space))
  310. ;;;
  311. ;;; ISO9660 file systems.
  312. ;;;
  313. ;; <http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-119.pdf>.
  314. (define (iso9660-superblock? sblock)
  315. "Return #t when SBLOCK is an iso9660 volume descriptor."
  316. (bytevector=? (sub-bytevector sblock 1 6)
  317. ;; Note: "\x01" is the volume descriptor format version
  318. (string->utf8 "CD001\x01")))
  319. (define (read-iso9660-primary-volume-descriptor device offset)
  320. "Find and read the first primary volume descriptor, starting at OFFSET.
  321. Return #f if not found."
  322. (let* ((sblock (read-superblock device offset 2048 iso9660-superblock?))
  323. (type-code (if sblock
  324. (bytevector-u8-ref sblock 0)
  325. (error (format #f
  326. "Could not read ISO9660 primary
  327. volume descriptor from ~s"
  328. device)))))
  329. (match type-code
  330. (255 #f) ; Volume Descriptor Set Terminator.
  331. (1 sblock) ; Primary Volume Descriptor
  332. (_ (read-iso9660-primary-volume-descriptor device (+ offset 2048))))))
  333. (define (read-iso9660-superblock device)
  334. "Return the raw contents of DEVICE's iso9660 primary volume descriptor
  335. as a bytevector, or #f if DEVICE does not contain an iso9660 file system."
  336. ;; Start reading at sector 16.
  337. ;; Since we are not sure that the device contains an ISO9660 file system,
  338. ;; we have to find that out first.
  339. (if (read-superblock device (* 2048 16) 2048 iso9660-superblock?)
  340. (read-iso9660-primary-volume-descriptor device (* 2048 16))
  341. #f)) ; Device does not contain an iso9660 file system.
  342. (define (iso9660-superblock-uuid sblock)
  343. "Return the modification time of an iso9660 primary volume descriptor
  344. SBLOCK as a bytevector. If that's not set, returns the creation time."
  345. ;; Drops GMT offset for compatibility with Grub, blkid and /dev/disk/by-uuid.
  346. ;; Compare Grub: "2014-12-02-19-30-23-00".
  347. ;; Compare blkid result: "2014-12-02-19-30-23-00".
  348. ;; Compare /dev/disk/by-uuid entry: "2014-12-02-19-30-23-00".
  349. (let* ((creation-time (sub-bytevector sblock 813 17))
  350. (modification-time (sub-bytevector sblock 830 17))
  351. (unset-time (make-bytevector 17 0))
  352. (time (if (bytevector=? unset-time modification-time)
  353. creation-time
  354. modification-time)))
  355. (sub-bytevector time 0 16))) ; strips GMT offset.
  356. (define (iso9660-superblock-volume-name sblock)
  357. "Return the volume name of SBLOCK as a string. The volume name is an ASCII
  358. string. Trailing spaces are trimmed."
  359. ;; Note: Valid characters are of the set "[0-9][A-Z]_" (ECMA-119 Appendix A)
  360. (string-trim-right (latin1->string (sub-bytevector sblock 40 32)
  361. (lambda (c) #f)) #\space))
  362. ;;;
  363. ;;; JFS file systems.
  364. ;;;
  365. ;; Taken from <linux-libre>/fs/jfs/jfs_superblock.h.
  366. (define-syntax %jfs-endianness
  367. ;; Endianness of JFS file systems.
  368. (identifier-syntax (endianness little)))
  369. (define (jfs-superblock? sblock)
  370. "Return #t when SBLOCK is a JFS superblock."
  371. (bytevector=? (sub-bytevector sblock 0 4)
  372. (string->utf8 "JFS1")))
  373. (define (read-jfs-superblock device)
  374. "Return the raw contents of DEVICE's JFS superblock as a bytevector, or #f
  375. if DEVICE does not contain a JFS file system."
  376. (read-superblock device 32768 184 jfs-superblock?))
  377. (define (jfs-superblock-uuid sblock)
  378. "Return the UUID of JFS superblock SBLOCK as a 16-byte bytevector."
  379. (sub-bytevector sblock 136 16))
  380. (define (jfs-superblock-volume-name sblock)
  381. "Return the volume name of SBLOCK as a string of at most 16 characters, or
  382. #f if SBLOCK has no volume name."
  383. (null-terminated-latin1->string (sub-bytevector sblock 152 16)))
  384. (define (check-jfs-file-system device)
  385. "Return the health of a JFS file system on DEVICE."
  386. (match (status:exit-val
  387. (system* "jfs_fsck" "-p" "-v" device))
  388. (0 'pass)
  389. (1 'errors-corrected)
  390. (2 'reboot-required)
  391. (_ 'fatal-error)))
  392. ;;;
  393. ;;; F2FS (Flash-Friendly File System)
  394. ;;;
  395. ;;; https://git.kernel.org/pub/scm/linux/kernel/git/jaegeuk/f2fs.git/tree/include/linux/f2fs_fs.h
  396. ;;; (but using xxd proved to be simpler)
  397. (define-syntax %f2fs-endianness
  398. ;; Endianness of F2FS file systems
  399. (identifier-syntax (endianness little)))
  400. ;; F2FS actually stores two adjacent copies of the superblock.
  401. ;; should we read both?
  402. (define (f2fs-superblock? sblock)
  403. "Return #t when SBLOCK is an F2FS superblock."
  404. (let ((magic (bytevector-u32-ref sblock 0 %f2fs-endianness)))
  405. (= magic #xF2F52010)))
  406. (define (read-f2fs-superblock device)
  407. "Return the raw contents of DEVICE's F2FS superblock as a bytevector, or #f
  408. if DEVICE does not contain an F2FS file system."
  409. (read-superblock device
  410. ;; offset of magic in first copy
  411. #x400
  412. ;; difference between magic of second
  413. ;; and first copies
  414. (- #x1400 #x400)
  415. f2fs-superblock?))
  416. (define (f2fs-superblock-uuid sblock)
  417. "Return the UUID of F2FS superblock SBLOCK as a 16-byte bytevector."
  418. (sub-bytevector sblock
  419. (- (+ #x460 12)
  420. ;; subtract superblock offset
  421. #x400)
  422. 16))
  423. (define (f2fs-superblock-volume-name sblock)
  424. "Return the volume name of SBLOCK as a string of at most 512 characters, or
  425. #f if SBLOCK has no volume name."
  426. (null-terminated-utf16->string
  427. (sub-bytevector sblock (- (+ #x470 12) #x400) 512)
  428. %f2fs-endianness))
  429. (define (check-f2fs-file-system device)
  430. "Return the health of a F2FS file system on DEVICE."
  431. (match (status:exit-val
  432. (system* "fsck.f2fs" "-p" device))
  433. ;; 0 and -1 are the only two possibilities
  434. ;; (according to the manpage)
  435. (0 'pass)
  436. (_ 'fatal-error)))
  437. ;;;
  438. ;;; LUKS encrypted devices.
  439. ;;;
  440. ;; The LUKS header format is described in "LUKS On-Disk Format Specification":
  441. ;; <https://gitlab.com/cryptsetup/cryptsetup/wikis/Specification>. We follow
  442. ;; version 1.2.1 of this document.
  443. ;; The LUKS2 header format is described in "LUKS2 On-Disk Format Specification":
  444. ;; <https://gitlab.com/cryptsetup/LUKS2-docs/blob/master/luks2_doc_wip.pdf>.
  445. ;; It is a WIP document.
  446. (define-syntax %luks-endianness
  447. ;; Endianness of LUKS headers.
  448. (identifier-syntax (endianness big)))
  449. (define (luks-superblock? sblock)
  450. "Return #t when SBLOCK is a luks superblock."
  451. (define %luks-magic
  452. ;; The 'LUKS_MAGIC' constant.
  453. (u8-list->bytevector (append (map char->integer (string->list "LUKS"))
  454. (list #xba #xbe))))
  455. (let ((magic (sub-bytevector sblock 0 6))
  456. (version (bytevector-u16-ref sblock 6 %luks-endianness)))
  457. (and (bytevector=? magic %luks-magic)
  458. (or (= version 1) (= version 2)))))
  459. (define (read-luks-header file)
  460. "Read a LUKS header from FILE. Return the raw header on success, and #f if
  461. not valid header was found."
  462. ;; Size in bytes of the LUKS binary header, which includes key slots in
  463. ;; LUKS1. In LUKS2 the binary header is partially backward compatible, so
  464. ;; that UUID can be extracted as for LUKS1. Keyslots and other metadata are
  465. ;; not part of this header in LUKS2, but are included in the JSON metadata
  466. ;; area that follows.
  467. (read-superblock file 0 592 luks-superblock?))
  468. (define (luks-header-uuid header)
  469. "Return the LUKS UUID from HEADER, as a 16-byte bytevector."
  470. ;; 40 bytes are reserved for the UUID, but in practice, it contains the 36
  471. ;; bytes of its ASCII representation.
  472. (let ((uuid (sub-bytevector header 168 36)))
  473. (string->uuid (utf8->string uuid))))
  474. ;;;
  475. ;;; NTFS file systems.
  476. ;;;
  477. ;; Taken from <linux-libre>/fs/ntfs/layout.h
  478. (define-syntax %ntfs-endianness
  479. ;; Endianness of NTFS file systems.
  480. (identifier-syntax (endianness little)))
  481. (define (ntfs-superblock? sblock)
  482. "Return #t when SBLOCK is a NTFS superblock."
  483. (bytevector=? (sub-bytevector sblock 3 8)
  484. (string->utf8 "NTFS ")))
  485. (define (read-ntfs-superblock device)
  486. "Return the raw contents of DEVICE's NTFS superblock as a bytevector, or #f
  487. if DEVICE does not contain a NTFS file system."
  488. (read-superblock device 0 511 ntfs-superblock?))
  489. (define (ntfs-superblock-uuid sblock)
  490. "Return the UUID of NTFS superblock SBLOCK as a 8-byte bytevector."
  491. (sub-bytevector sblock 72 8))
  492. ;; TODO: Add ntfs-superblock-volume-name. The partition label is not stored
  493. ;; in the BOOT SECTOR like the UUID, but in the MASTER FILE TABLE, which seems
  494. ;; way harder to access.
  495. (define (check-ntfs-file-system device)
  496. "Return the health of a NTFS file system on DEVICE."
  497. (match (status:exit-val
  498. (system* "ntfsfix" device))
  499. (0 'pass)
  500. (_ 'fatal-error)))
  501. ;;;
  502. ;;; Partition lookup.
  503. ;;;
  504. (define (disk-partitions)
  505. "Return the list of device names corresponding to valid disk partitions."
  506. (define (partition? name major minor)
  507. ;; grub-mkrescue does some funny things for EFI support which
  508. ;; makes it a lot more difficult than one would expect to support
  509. ;; booting an ISO-9660 image from an USB flash drive.
  510. ;; For example there's a buggy (too small) hidden partition in it
  511. ;; which Linux mounts and then proceeds to fail while trying to
  512. ;; fall off the edge.
  513. ;; In any case, partition tables are supposed to be optional so
  514. ;; here we allow checking entire disks for file systems, too.
  515. (> major 2)) ;ignore RAM disks and floppy disks
  516. (call-with-input-file "/proc/partitions"
  517. (lambda (port)
  518. ;; Skip the two header lines.
  519. (read-line port)
  520. (read-line port)
  521. ;; Read each subsequent line, and extract the last space-separated
  522. ;; field.
  523. (let loop ((parts '()))
  524. (let ((line (read-line port)))
  525. (if (eof-object? line)
  526. (reverse parts)
  527. (match (string-tokenize line)
  528. (((= string->number major) (= string->number minor)
  529. blocks name)
  530. (if (partition? name major minor)
  531. (loop (cons name parts))
  532. (loop parts))))))))))
  533. (define (ENOENT-safe proc)
  534. "Wrap the one-argument PROC such that ENOENT errors are caught and lead to a
  535. warning and #f as the result."
  536. (lambda (device)
  537. (catch 'system-error
  538. (lambda ()
  539. (proc device))
  540. (lambda args
  541. ;; When running on the hand-made /dev,
  542. ;; 'disk-partitions' could return partitions for which
  543. ;; we have no /dev node. Handle that gracefully.
  544. (let ((errno (system-error-errno args)))
  545. (cond ((= ENOENT errno)
  546. (format (current-error-port)
  547. "warning: device '~a' not found~%" device)
  548. #f)
  549. ((= ENOMEDIUM errno) ;for removable media
  550. #f)
  551. ((= EIO errno) ;unreadable hardware like audio CDs
  552. (format (current-error-port)
  553. "warning: failed to read from device '~a'~%" device)
  554. #f)
  555. (else
  556. (apply throw args))))))))
  557. (define (partition-field-reader read field)
  558. "Return a procedure that takes a device and returns the value of a FIELD in
  559. the partition superblock or #f."
  560. (let ((read (ENOENT-safe read)))
  561. (lambda (device)
  562. (let ((sblock (read device)))
  563. (and sblock
  564. (field sblock))))))
  565. (define (read-partition-field device partition-field-readers)
  566. "Returns the value of a FIELD in the partition superblock of DEVICE or #f. It
  567. takes a list of PARTITION-FIELD-READERS and returns the result of the first
  568. partition field reader that returned a value."
  569. (match (filter-map (cut apply <> (list device)) partition-field-readers)
  570. ((field . _) field)
  571. (_ #f)))
  572. (define %partition-label-readers
  573. (list (partition-field-reader read-iso9660-superblock
  574. iso9660-superblock-volume-name)
  575. (partition-field-reader read-ext2-superblock
  576. ext2-superblock-volume-name)
  577. (partition-field-reader read-linux-swap-superblock
  578. linux-swap-superblock-volume-name)
  579. (partition-field-reader read-bcachefs-superblock
  580. bcachefs-superblock-volume-name)
  581. (partition-field-reader read-btrfs-superblock
  582. btrfs-superblock-volume-name)
  583. (partition-field-reader read-fat32-superblock
  584. fat32-superblock-volume-name)
  585. (partition-field-reader read-fat16-superblock
  586. fat16-superblock-volume-name)
  587. (partition-field-reader read-jfs-superblock
  588. jfs-superblock-volume-name)
  589. (partition-field-reader read-f2fs-superblock
  590. f2fs-superblock-volume-name)))
  591. (define %partition-uuid-readers
  592. (list (partition-field-reader read-iso9660-superblock
  593. iso9660-superblock-uuid)
  594. (partition-field-reader read-ext2-superblock
  595. ext2-superblock-uuid)
  596. (partition-field-reader read-linux-swap-superblock
  597. linux-swap-superblock-uuid)
  598. (partition-field-reader read-bcachefs-superblock
  599. bcachefs-superblock-external-uuid)
  600. (partition-field-reader read-btrfs-superblock
  601. btrfs-superblock-uuid)
  602. (partition-field-reader read-fat32-superblock
  603. fat32-superblock-uuid)
  604. (partition-field-reader read-fat16-superblock
  605. fat16-superblock-uuid)
  606. (partition-field-reader read-jfs-superblock
  607. jfs-superblock-uuid)
  608. (partition-field-reader read-f2fs-superblock
  609. f2fs-superblock-uuid)
  610. (partition-field-reader read-ntfs-superblock
  611. ntfs-superblock-uuid)))
  612. (define read-partition-label
  613. (cut read-partition-field <> %partition-label-readers))
  614. (define read-partition-uuid
  615. (cut read-partition-field <> %partition-uuid-readers))
  616. (define luks-partition-field-reader
  617. (partition-field-reader read-luks-header luks-header-uuid))
  618. (define read-luks-partition-uuid
  619. (cut read-partition-field <> (list luks-partition-field-reader)))
  620. (define (partition-predicate reader =)
  621. "Return a predicate that returns true if the FIELD of partition header that
  622. was READ is = to the given value."
  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