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