file-systems.scm 46 KB

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