Commit | Line | Data |
---|---|---|
e2f4b305 | 1 | ;;; GNU Guix --- Functional package management for GNU |
a5acc17a | 2 | ;;; Copyright © 2014, 2015, 2016, 2017, 2018 Ludovic Courtès <ludo@gnu.org> |
ab4e939c | 3 | ;;; Copyright © 2016, 2017 David Craven <david@craven.ch> |
a5e13c3b | 4 | ;;; Copyright © 2017 Mathieu Othacehe <m.othacehe@gmail.com> |
e2f4b305 LC |
5 | ;;; |
6 | ;;; This file is part of GNU Guix. | |
7 | ;;; | |
8 | ;;; GNU Guix is free software; you can redistribute it and/or modify it | |
9 | ;;; under the terms of the GNU General Public License as published by | |
10 | ;;; the Free Software Foundation; either version 3 of the License, or (at | |
11 | ;;; your option) any later version. | |
12 | ;;; | |
13 | ;;; GNU Guix is distributed in the hope that it will be useful, but | |
14 | ;;; WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | ;;; GNU General Public License for more details. | |
17 | ;;; | |
18 | ;;; You should have received a copy of the GNU General Public License | |
19 | ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>. | |
20 | ||
21 | (define-module (gnu build file-systems) | |
47cef4ec | 22 | #:use-module (gnu system uuid) |
1c65cca5 | 23 | #:use-module (gnu system file-systems) |
e2f4b305 | 24 | #:use-module (guix build utils) |
6eb43907 | 25 | #:use-module (guix build bournish) |
1c65cca5 LC |
26 | #:use-module ((guix build syscalls) |
27 | #:hide (file-system-type)) | |
e2f4b305 LC |
28 | #:use-module (rnrs io ports) |
29 | #:use-module (rnrs bytevectors) | |
30 | #:use-module (ice-9 match) | |
31 | #:use-module (ice-9 rdelim) | |
32 | #:use-module (system foreign) | |
33 | #:autoload (system repl repl) (start-repl) | |
34 | #:use-module (srfi srfi-1) | |
35 | #:use-module (srfi srfi-26) | |
36 | #:export (disk-partitions | |
37 | partition-label-predicate | |
0ec5ee94 | 38 | partition-uuid-predicate |
a1ccefaa | 39 | partition-luks-uuid-predicate |
e2f4b305 | 40 | find-partition-by-label |
0ec5ee94 | 41 | find-partition-by-uuid |
a1ccefaa | 42 | find-partition-by-luks-uuid |
e2f4b305 LC |
43 | canonicalize-device-spec |
44 | ||
983abd2c MO |
45 | read-partition-label |
46 | read-partition-uuid | |
8ae7044f | 47 | read-luks-partition-uuid |
983abd2c | 48 | |
e2f4b305 LC |
49 | bind-mount |
50 | ||
51 | mount-flags->bit-mask | |
52 | check-file-system | |
a5e13c3b | 53 | mount-file-system)) |
e2f4b305 LC |
54 | |
55 | ;;; Commentary: | |
56 | ;;; | |
57 | ;;; This modules provides tools to deal with disk partitions, and to mount and | |
58 | ;;; check file systems. | |
59 | ;;; | |
60 | ;;; Code: | |
61 | ||
e2f4b305 LC |
62 | (define (bind-mount source target) |
63 | "Bind-mount SOURCE at TARGET." | |
64 | (mount source target "" MS_BIND)) | |
65 | ||
2fe4ceee LC |
66 | (define (seek* fd/port offset whence) |
67 | "Like 'seek' but return -1 instead of throwing to 'system-error' upon | |
68 | EINVAL. This makes it easier to catch cases like OFFSET being too large for | |
69 | FD/PORT." | |
70 | (catch 'system-error | |
71 | (lambda () | |
72 | (seek fd/port offset whence)) | |
73 | (lambda args | |
74 | (if (= EINVAL (system-error-errno args)) | |
75 | -1 | |
76 | (apply throw args))))) | |
77 | ||
974e02da DC |
78 | (define (read-superblock device offset size magic?) |
79 | "Read a superblock of SIZE from OFFSET and DEVICE. Return the raw | |
80 | superblock on success, and #f if no valid superblock was found. MAGIC? | |
81 | takes a bytevector and returns #t when it's a valid superblock." | |
82 | (call-with-input-file device | |
83 | (lambda (port) | |
2fe4ceee LC |
84 | (and (= offset (seek* port offset SEEK_SET)) |
85 | (let ((block (make-bytevector size))) | |
86 | (match (get-bytevector-n! port block 0 (bytevector-length block)) | |
87 | ((? eof-object?) | |
88 | #f) | |
89 | ((? number? len) | |
90 | (and (= len (bytevector-length block)) | |
91 | (and (magic? block) | |
92 | block))))))))) | |
974e02da | 93 | |
b0377e58 DC |
94 | (define null-terminated-latin1->string |
95 | (cut latin1->string <> zero?)) | |
96 | ||
a1ccefaa LC |
97 | \f |
98 | ;;; | |
99 | ;;; Ext2 file systems. | |
100 | ;;; | |
101 | ||
974e02da DC |
102 | ;; <http://www.nongnu.org/ext2-doc/ext2.html#DEF-SUPERBLOCK>. |
103 | ;; TODO: Use "packed structs" from Guile-OpenGL or similar. | |
104 | ||
e2f4b305 LC |
105 | (define-syntax %ext2-endianness |
106 | ;; Endianness of ext2 file systems. | |
107 | (identifier-syntax (endianness little))) | |
108 | ||
974e02da DC |
109 | (define (ext2-superblock? sblock) |
110 | "Return #t when SBLOCK is an ext2 superblock." | |
111 | (let ((magic (bytevector-u16-ref sblock 56 %ext2-endianness))) | |
112 | (= magic #xef53))) | |
e2f4b305 LC |
113 | |
114 | (define (read-ext2-superblock device) | |
115 | "Return the raw contents of DEVICE's ext2 superblock as a bytevector, or #f | |
116 | if DEVICE does not contain an ext2 file system." | |
974e02da | 117 | (read-superblock device 1024 264 ext2-superblock?)) |
e2f4b305 LC |
118 | |
119 | (define (ext2-superblock-uuid sblock) | |
120 | "Return the UUID of ext2 superblock SBLOCK as a 16-byte bytevector." | |
974e02da | 121 | (sub-bytevector sblock 104 16)) |
e2f4b305 LC |
122 | |
123 | (define (ext2-superblock-volume-name sblock) | |
124 | "Return the volume name of SBLOCK as a string of at most 16 characters, or | |
125 | #f if SBLOCK has no volume name." | |
974e02da | 126 | (null-terminated-latin1->string (sub-bytevector sblock 120 16))) |
e2f4b305 | 127 | |
26905ec8 DC |
128 | (define (check-ext2-file-system device) |
129 | "Return the health of an ext2 file system on DEVICE." | |
130 | (match (status:exit-val | |
131 | (system* "e2fsck" "-v" "-p" "-C" "0" device)) | |
132 | (0 'pass) | |
133 | (1 'errors-corrected) | |
134 | (2 'reboot-required) | |
135 | (_ 'fatal-error))) | |
e2f4b305 | 136 | |
a1ccefaa | 137 | \f |
b1a505ba DC |
138 | ;;; |
139 | ;;; Btrfs file systems. | |
140 | ;;; | |
141 | ||
142 | ;; <https://btrfs.wiki.kernel.org/index.php/On-disk_Format#Superblock>. | |
143 | ||
144 | (define-syntax %btrfs-endianness | |
145 | ;; Endianness of btrfs file systems. | |
146 | (identifier-syntax (endianness little))) | |
147 | ||
148 | (define (btrfs-superblock? sblock) | |
149 | "Return #t when SBLOCK is a btrfs superblock." | |
150 | (bytevector=? (sub-bytevector sblock 64 8) | |
151 | (string->utf8 "_BHRfS_M"))) | |
152 | ||
153 | (define (read-btrfs-superblock device) | |
154 | "Return the raw contents of DEVICE's btrfs superblock as a bytevector, or #f | |
155 | if DEVICE does not contain a btrfs file system." | |
156 | (read-superblock device 65536 4096 btrfs-superblock?)) | |
157 | ||
158 | (define (btrfs-superblock-uuid sblock) | |
159 | "Return the UUID of a btrfs superblock SBLOCK as a 16-byte bytevector." | |
160 | (sub-bytevector sblock 32 16)) | |
161 | ||
162 | (define (btrfs-superblock-volume-name sblock) | |
163 | "Return the volume name of SBLOCK as a string of at most 256 characters, or | |
164 | #f if SBLOCK has no volume name." | |
165 | (null-terminated-latin1->string (sub-bytevector sblock 299 256))) | |
166 | ||
167 | (define (check-btrfs-file-system device) | |
168 | "Return the health of a btrfs file system on DEVICE." | |
169 | (match (status:exit-val | |
170 | (system* "btrfs" "device" "scan")) | |
171 | (0 'pass) | |
172 | (_ 'fatal-error))) | |
173 | ||
174 | \f | |
b0377e58 DC |
175 | ;;; |
176 | ;;; FAT32 file systems. | |
177 | ;;; | |
178 | ||
179 | ;; <http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-107.pdf>. | |
180 | ||
b0377e58 DC |
181 | (define (fat32-superblock? sblock) |
182 | "Return #t when SBLOCK is a fat32 superblock." | |
183 | (bytevector=? (sub-bytevector sblock 82 8) | |
184 | (string->utf8 "FAT32 "))) | |
185 | ||
186 | (define (read-fat32-superblock device) | |
187 | "Return the raw contents of DEVICE's fat32 superblock as a bytevector, or | |
188 | #f if DEVICE does not contain a fat32 file system." | |
189 | (read-superblock device 0 90 fat32-superblock?)) | |
190 | ||
191 | (define (fat32-superblock-uuid sblock) | |
192 | "Return the Volume ID of a fat superblock SBLOCK as a 4-byte bytevector." | |
193 | (sub-bytevector sblock 67 4)) | |
194 | ||
b0377e58 DC |
195 | (define (fat32-superblock-volume-name sblock) |
196 | "Return the volume name of SBLOCK as a string of at most 11 characters, or | |
197 | #f if SBLOCK has no volume name. The volume name is a latin1 string. | |
198 | Trailing spaces are trimmed." | |
199 | (string-trim-right (latin1->string (sub-bytevector sblock 71 11) (lambda (c) #f)) #\space)) | |
200 | ||
88235675 | 201 | (define (check-fat-file-system device) |
b0377e58 DC |
202 | "Return the health of a fat file system on DEVICE." |
203 | (match (status:exit-val | |
204 | (system* "fsck.vfat" "-v" "-a" device)) | |
205 | (0 'pass) | |
206 | (1 'errors-corrected) | |
207 | (_ 'fatal-error))) | |
208 | ||
209 | \f | |
88235675 LC |
210 | ;;; |
211 | ;;; FAT16 file systems. | |
212 | ;;; | |
213 | ||
214 | (define (fat16-superblock? sblock) | |
215 | "Return #t when SBLOCK is a fat16 boot record." | |
216 | (bytevector=? (sub-bytevector sblock 54 8) | |
217 | (string->utf8 "FAT16 "))) | |
218 | ||
219 | (define (read-fat16-superblock device) | |
220 | "Return the raw contents of DEVICE's fat16 superblock as a bytevector, or | |
221 | #f if DEVICE does not contain a fat16 file system." | |
222 | (read-superblock device 0 62 fat16-superblock?)) | |
223 | ||
224 | (define (fat16-superblock-uuid sblock) | |
225 | "Return the Volume ID of a fat superblock SBLOCK as a 4-byte bytevector." | |
226 | (sub-bytevector sblock 39 4)) | |
227 | ||
228 | (define (fat16-superblock-volume-name sblock) | |
229 | "Return the volume name of SBLOCK as a string of at most 11 characters, or | |
230 | #f if SBLOCK has no volume name. The volume name is a latin1 string. | |
231 | Trailing spaces are trimmed." | |
232 | (string-trim-right (latin1->string (sub-bytevector sblock 43 11) | |
233 | (lambda (c) #f)) | |
234 | #\space)) | |
235 | ||
236 | \f | |
06110559 DM |
237 | ;;; |
238 | ;;; ISO9660 file systems. | |
239 | ;;; | |
240 | ||
241 | ;; <http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-119.pdf>. | |
242 | ||
243 | (define (iso9660-superblock? sblock) | |
3dba9b37 | 244 | "Return #t when SBLOCK is an iso9660 volume descriptor." |
06110559 DM |
245 | (bytevector=? (sub-bytevector sblock 1 6) |
246 | ;; Note: "\x01" is the volume descriptor format version | |
247 | (string->utf8 "CD001\x01"))) | |
248 | ||
249 | (define (read-iso9660-primary-volume-descriptor device offset) | |
250 | "Find and read the first primary volume descriptor, starting at OFFSET. | |
251 | Return #f if not found." | |
252 | (let* ((sblock (read-superblock device offset 2048 iso9660-superblock?)) | |
203a9455 DM |
253 | (type-code (if sblock |
254 | (bytevector-u8-ref sblock 0) | |
255 | (error (format #f | |
256 | "Could not read ISO9660 primary | |
257 | volume descriptor from ~s" | |
258 | device))))) | |
06110559 DM |
259 | (match type-code |
260 | (255 #f) ; Volume Descriptor Set Terminator. | |
261 | (1 sblock) ; Primary Volume Descriptor | |
262 | (_ (read-iso9660-primary-volume-descriptor device (+ offset 2048)))))) | |
263 | ||
264 | (define (read-iso9660-superblock device) | |
3dba9b37 DM |
265 | "Return the raw contents of DEVICE's iso9660 primary volume descriptor |
266 | as a bytevector, or #f if DEVICE does not contain an iso9660 file system." | |
06110559 | 267 | ;; Start reading at sector 16. |
162a1374 | 268 | ;; Since we are not sure that the device contains an ISO9660 file system, |
fb03f44b DM |
269 | ;; we have to find that out first. |
270 | (if (read-superblock device (* 2048 16) 2048 iso9660-superblock?) | |
271 | (read-iso9660-primary-volume-descriptor device (* 2048 16)) | |
162a1374 | 272 | #f)) ; Device does not contain an iso9660 file system. |
06110559 DM |
273 | |
274 | (define (iso9660-superblock-uuid sblock) | |
3dba9b37 | 275 | "Return the modification time of an iso9660 primary volume descriptor |
c6aee77e | 276 | SBLOCK as a bytevector. If that's not set, returns the creation time." |
06110559 DM |
277 | ;; Drops GMT offset for compatibility with Grub, blkid and /dev/disk/by-uuid. |
278 | ;; Compare Grub: "2014-12-02-19-30-23-00". | |
279 | ;; Compare blkid result: "2014-12-02-19-30-23-00". | |
280 | ;; Compare /dev/disk/by-uuid entry: "2014-12-02-19-30-23-00". | |
c6aee77e DM |
281 | (let* ((creation-time (sub-bytevector sblock 813 17)) |
282 | (modification-time (sub-bytevector sblock 830 17)) | |
283 | (unset-time (make-bytevector 17 0)) | |
284 | (time (if (bytevector=? unset-time modification-time) | |
285 | creation-time | |
286 | modification-time))) | |
287 | (sub-bytevector time 0 16))) ; strips GMT offset. | |
06110559 | 288 | |
06110559 DM |
289 | (define (iso9660-superblock-volume-name sblock) |
290 | "Return the volume name of SBLOCK as a string. The volume name is an ASCII | |
291 | string. Trailing spaces are trimmed." | |
cdc701ea | 292 | ;; Note: Valid characters are of the set "[0-9][A-Z]_" (ECMA-119 Appendix A) |
06110559 DM |
293 | (string-trim-right (latin1->string (sub-bytevector sblock 40 32) |
294 | (lambda (c) #f)) #\space)) | |
295 | ||
296 | \f | |
a1ccefaa LC |
297 | ;;; |
298 | ;;; LUKS encrypted devices. | |
299 | ;;; | |
300 | ||
301 | ;; The LUKS header format is described in "LUKS On-Disk Format Specification": | |
de975de3 | 302 | ;; <https://gitlab.com/cryptsetup/cryptsetup/wikis/Specification>. We follow |
a1ccefaa LC |
303 | ;; version 1.2.1 of this document. |
304 | ||
305 | (define-syntax %luks-endianness | |
306 | ;; Endianness of LUKS headers. | |
307 | (identifier-syntax (endianness big))) | |
308 | ||
974e02da DC |
309 | (define (luks-superblock? sblock) |
310 | "Return #t when SBLOCK is a luks superblock." | |
311 | (define %luks-magic | |
312 | ;; The 'LUKS_MAGIC' constant. | |
313 | (u8-list->bytevector (append (map char->integer (string->list "LUKS")) | |
314 | (list #xba #xbe)))) | |
315 | (let ((magic (sub-bytevector sblock 0 6)) | |
316 | (version (bytevector-u16-ref sblock 6 %luks-endianness))) | |
317 | (and (bytevector=? magic %luks-magic) | |
318 | (= version 1)))) | |
a1ccefaa LC |
319 | |
320 | (define (read-luks-header file) | |
321 | "Read a LUKS header from FILE. Return the raw header on success, and #f if | |
322 | not valid header was found." | |
974e02da DC |
323 | ;; Size in bytes of the LUKS header, including key slots. |
324 | (read-superblock file 0 592 luks-superblock?)) | |
a1ccefaa LC |
325 | |
326 | (define (luks-header-uuid header) | |
327 | "Return the LUKS UUID from HEADER, as a 16-byte bytevector." | |
328 | ;; 40 bytes are reserved for the UUID, but in practice, it contains the 36 | |
329 | ;; bytes of its ASCII representation. | |
330 | (let ((uuid (sub-bytevector header 168 36))) | |
331 | (string->uuid (utf8->string uuid)))) | |
332 | ||
333 | \f | |
334 | ;;; | |
335 | ;;; Partition lookup. | |
336 | ;;; | |
337 | ||
e2f4b305 LC |
338 | (define (disk-partitions) |
339 | "Return the list of device names corresponding to valid disk partitions." | |
49baaff4 | 340 | (define (partition? name major minor) |
9833bcfc DM |
341 | ;; grub-mkrescue does some funny things for EFI support which |
342 | ;; makes it a lot more difficult than one would expect to support | |
343 | ;; booting an ISO-9660 image from an USB flash drive. | |
344 | ;; For example there's a buggy (too small) hidden partition in it | |
345 | ;; which Linux mounts and then proceeds to fail while trying to | |
346 | ;; fall off the edge. | |
347 | ;; In any case, partition tables are supposed to be optional so | |
348 | ;; here we allow checking entire disks for file systems, too. | |
349 | (> major 2)) ;ignore RAM disks and floppy disks | |
e2f4b305 LC |
350 | |
351 | (call-with-input-file "/proc/partitions" | |
352 | (lambda (port) | |
353 | ;; Skip the two header lines. | |
354 | (read-line port) | |
355 | (read-line port) | |
356 | ||
357 | ;; Read each subsequent line, and extract the last space-separated | |
358 | ;; field. | |
359 | (let loop ((parts '())) | |
360 | (let ((line (read-line port))) | |
361 | (if (eof-object? line) | |
362 | (reverse parts) | |
363 | (match (string-tokenize line) | |
364 | (((= string->number major) (= string->number minor) | |
365 | blocks name) | |
49baaff4 | 366 | (if (partition? name major minor) |
e2f4b305 LC |
367 | (loop (cons name parts)) |
368 | (loop parts)))))))))) | |
369 | ||
24473356 LC |
370 | (define (ENOENT-safe proc) |
371 | "Wrap the one-argument PROC such that ENOENT errors are caught and lead to a | |
372 | warning and #f as the result." | |
373 | (lambda (device) | |
374 | (catch 'system-error | |
375 | (lambda () | |
376 | (proc device)) | |
377 | (lambda args | |
378 | ;; When running on the hand-made /dev, | |
379 | ;; 'disk-partitions' could return partitions for which | |
380 | ;; we have no /dev node. Handle that gracefully. | |
49baaff4 LC |
381 | (let ((errno (system-error-errno args))) |
382 | (cond ((= ENOENT errno) | |
383 | (format (current-error-port) | |
384 | "warning: device '~a' not found~%" device) | |
385 | #f) | |
386 | ((= ENOMEDIUM errno) ;for removable media | |
387 | #f) | |
b53510e0 AVY |
388 | ((= EIO errno) ;unreadable hardware like audio CDs |
389 | (format (current-error-port) | |
390 | "warning: failed to read from device '~a'~%" device) | |
391 | #f) | |
49baaff4 LC |
392 | (else |
393 | (apply throw args)))))))) | |
24473356 | 394 | |
ab4e939c DC |
395 | (define (partition-field-reader read field) |
396 | "Return a procedure that takes a device and returns the value of a FIELD in | |
397 | the partition superblock or #f." | |
398 | (let ((read (ENOENT-safe read))) | |
399 | (lambda (device) | |
400 | (let ((sblock (read device))) | |
401 | (and sblock | |
402 | (field sblock)))))) | |
403 | ||
404 | (define (read-partition-field device partition-field-readers) | |
405 | "Returns the value of a FIELD in the partition superblock of DEVICE or #f. It | |
406 | takes a list of PARTITION-FIELD-READERS and returns the result of the first | |
407 | partition field reader that returned a value." | |
408 | (match (filter-map (cut apply <> (list device)) partition-field-readers) | |
409 | ((field . _) field) | |
410 | (_ #f))) | |
411 | ||
412 | (define %partition-label-readers | |
06110559 DM |
413 | (list (partition-field-reader read-iso9660-superblock |
414 | iso9660-superblock-volume-name) | |
415 | (partition-field-reader read-ext2-superblock | |
b1a505ba DC |
416 | ext2-superblock-volume-name) |
417 | (partition-field-reader read-btrfs-superblock | |
b0377e58 DC |
418 | btrfs-superblock-volume-name) |
419 | (partition-field-reader read-fat32-superblock | |
88235675 LC |
420 | fat32-superblock-volume-name) |
421 | (partition-field-reader read-fat16-superblock | |
422 | fat16-superblock-volume-name))) | |
ab4e939c DC |
423 | |
424 | (define %partition-uuid-readers | |
06110559 DM |
425 | (list (partition-field-reader read-iso9660-superblock |
426 | iso9660-superblock-uuid) | |
427 | (partition-field-reader read-ext2-superblock | |
b1a505ba DC |
428 | ext2-superblock-uuid) |
429 | (partition-field-reader read-btrfs-superblock | |
b0377e58 DC |
430 | btrfs-superblock-uuid) |
431 | (partition-field-reader read-fat32-superblock | |
88235675 LC |
432 | fat32-superblock-uuid) |
433 | (partition-field-reader read-fat16-superblock | |
434 | fat16-superblock-uuid))) | |
ab4e939c DC |
435 | |
436 | (define read-partition-label | |
437 | (cut read-partition-field <> %partition-label-readers)) | |
438 | ||
439 | (define read-partition-uuid | |
440 | (cut read-partition-field <> %partition-uuid-readers)) | |
441 | ||
8ae7044f MO |
442 | (define luks-partition-field-reader |
443 | (partition-field-reader read-luks-header luks-header-uuid)) | |
444 | ||
445 | (define read-luks-partition-uuid | |
446 | (cut read-partition-field <> (list luks-partition-field-reader))) | |
447 | ||
ab4e939c | 448 | (define (partition-predicate reader =) |
a1ccefaa LC |
449 | "Return a predicate that returns true if the FIELD of partition header that |
450 | was READ is = to the given value." | |
ab4e939c DC |
451 | (lambda (expected) |
452 | (lambda (device) | |
453 | (let ((actual (reader device))) | |
454 | (and actual | |
455 | (= actual expected)))))) | |
0ec5ee94 LC |
456 | |
457 | (define partition-label-predicate | |
ab4e939c | 458 | (partition-predicate read-partition-label string=?)) |
0ec5ee94 LC |
459 | |
460 | (define partition-uuid-predicate | |
aed1f1b0 | 461 | (partition-predicate read-partition-uuid uuid=?)) |
a1ccefaa | 462 | |
974e02da | 463 | (define luks-partition-uuid-predicate |
8ae7044f | 464 | (partition-predicate luks-partition-field-reader uuid=?)) |
e2f4b305 | 465 | |
ab4e939c DC |
466 | (define (find-partition predicate) |
467 | "Return the first partition found that matches PREDICATE, or #f if none | |
e2f4b305 | 468 | were found." |
ab4e939c DC |
469 | (lambda (expected) |
470 | (find (predicate expected) | |
471 | (map (cut string-append "/dev/" <>) | |
472 | (disk-partitions))))) | |
473 | ||
474 | (define find-partition-by-label | |
475 | (find-partition partition-label-predicate)) | |
476 | ||
477 | (define find-partition-by-uuid | |
478 | (find-partition partition-uuid-predicate)) | |
479 | ||
480 | (define find-partition-by-luks-uuid | |
481 | (find-partition luks-partition-uuid-predicate)) | |
a1ccefaa | 482 | |
f8865db6 | 483 | \f |
a5acc17a LC |
484 | (define (canonicalize-device-spec spec) |
485 | "Return the device name corresponding to SPEC, which can be a <uuid>, a | |
486 | <file-system-label>, or a string (typically a /dev file name)." | |
e2f4b305 LC |
487 | (define max-trials |
488 | ;; Number of times we retry partition label resolution, 1 second per | |
489 | ;; trial. Note: somebody reported a delay of 16 seconds (!) before their | |
490 | ;; USB key would be detected by the kernel, so we must wait for at least | |
491 | ;; this long. | |
492 | 20) | |
493 | ||
0ec5ee94 LC |
494 | (define (resolve find-partition spec fmt) |
495 | (let loop ((count 0)) | |
496 | (let ((device (find-partition spec))) | |
497 | (or device | |
498 | ;; Some devices take a bit of time to appear, most notably USB | |
499 | ;; storage devices. Thus, wait for the device to appear. | |
500 | (if (> count max-trials) | |
501 | (error "failed to resolve partition" (fmt spec)) | |
502 | (begin | |
503 | (format #t "waiting for partition '~a' to appear...~%" | |
504 | (fmt spec)) | |
505 | (sleep 1) | |
506 | (loop (+ 1 count)))))))) | |
507 | ||
a5acc17a LC |
508 | (match spec |
509 | ((? string?) | |
69a05eab LC |
510 | ;; Nothing to do, but wait until SPEC shows up. |
511 | (resolve identity spec identity)) | |
a5acc17a | 512 | ((? file-system-label?) |
e2f4b305 | 513 | ;; Resolve the label. |
a5acc17a LC |
514 | (resolve find-partition-by-label |
515 | (file-system-label->string spec) | |
516 | identity)) | |
517 | ((? uuid?) | |
f453f637 | 518 | (resolve find-partition-by-uuid |
a5acc17a LC |
519 | (uuid-bytevector spec) |
520 | uuid->string)))) | |
e2f4b305 LC |
521 | |
522 | (define (check-file-system device type) | |
523 | "Run a file system check of TYPE on DEVICE." | |
26905ec8 DC |
524 | (define check-procedure |
525 | (cond | |
526 | ((string-prefix? "ext" type) check-ext2-file-system) | |
b1a505ba | 527 | ((string-prefix? "btrfs" type) check-btrfs-file-system) |
88235675 | 528 | ((string-suffix? "fat" type) check-fat-file-system) |
26905ec8 DC |
529 | (else #f))) |
530 | ||
531 | (if check-procedure | |
532 | (match (check-procedure device) | |
533 | ('pass | |
534 | #t) | |
535 | ('errors-corrected | |
536 | (format (current-error-port) | |
537 | "File system check corrected errors on ~a; continuing~%" | |
538 | device)) | |
539 | ('reboot-required | |
540 | (format (current-error-port) | |
541 | "File system check corrected errors on ~a; rebooting~%" | |
542 | device) | |
543 | (sleep 3) | |
544 | (reboot)) | |
545 | ('fatal-error | |
6ea6e147 | 546 | (format (current-error-port) "File system check on ~a failed~%" |
26905ec8 | 547 | device) |
6ea6e147 LC |
548 | |
549 | ;; Spawn a REPL only if someone would be able to interact with it. | |
550 | (when (isatty? (current-input-port)) | |
551 | (format (current-error-port) "Spawning Bourne-like REPL.~%") | |
5de5f818 LC |
552 | |
553 | ;; 'current-output-port' is typically connected to /dev/klog (in | |
554 | ;; PID 1), but here we want to make sure we talk directly to the | |
555 | ;; user. | |
556 | (with-output-to-file "/dev/console" | |
557 | (lambda () | |
558 | (start-repl %bournish-language)))))) | |
26905ec8 DC |
559 | (format (current-error-port) |
560 | "No file system check procedure for ~a; skipping~%" | |
561 | device))) | |
e2f4b305 LC |
562 | |
563 | (define (mount-flags->bit-mask flags) | |
564 | "Return the number suitable for the 'flags' argument of 'mount' that | |
565 | corresponds to the symbols listed in FLAGS." | |
566 | (let loop ((flags flags)) | |
567 | (match flags | |
568 | (('read-only rest ...) | |
569 | (logior MS_RDONLY (loop rest))) | |
570 | (('bind-mount rest ...) | |
571 | (logior MS_BIND (loop rest))) | |
572 | (('no-suid rest ...) | |
573 | (logior MS_NOSUID (loop rest))) | |
574 | (('no-dev rest ...) | |
575 | (logior MS_NODEV (loop rest))) | |
576 | (('no-exec rest ...) | |
577 | (logior MS_NOEXEC (loop rest))) | |
578 | (() | |
579 | 0)))) | |
580 | ||
1c65cca5 LC |
581 | (define* (mount-file-system fs #:key (root "/root")) |
582 | "Mount the file system described by FS, a <file-system> object, under ROOT. | |
e2f4b305 LC |
583 | |
584 | DEVICE, MOUNT-POINT, and TYPE must be strings; OPTIONS can be a string or #f; | |
585 | FLAGS must be a list of symbols. CHECK? is a Boolean indicating whether to | |
586 | run a file system check." | |
0c85db79 JD |
587 | |
588 | (define (mount-nfs source mount-point type flags options) | |
589 | (let* ((idx (string-rindex source #\:)) | |
590 | (host-part (string-take source idx)) | |
591 | ;; Strip [] from around host if present | |
592 | (host (match (string-split host-part (string->char-set "[]")) | |
593 | (("" h "") h) | |
594 | ((h) h))) | |
595 | (aa (match (getaddrinfo host "nfs") ((x . _) x))) | |
596 | (sa (addrinfo:addr aa)) | |
597 | (inet-addr (inet-ntop (sockaddr:fam sa) | |
598 | (sockaddr:addr sa)))) | |
599 | ||
600 | ;; Mounting an NFS file system requires passing the address | |
601 | ;; of the server in the addr= option | |
602 | (mount source mount-point type flags | |
603 | (string-append "addr=" | |
604 | inet-addr | |
605 | (if options | |
606 | (string-append "," options) | |
607 | ""))))) | |
1c65cca5 LC |
608 | (let ((type (file-system-type fs)) |
609 | (options (file-system-options fs)) | |
a5acc17a | 610 | (source (canonicalize-device-spec (file-system-device fs))) |
1c65cca5 LC |
611 | (mount-point (string-append root "/" |
612 | (file-system-mount-point fs))) | |
613 | (flags (mount-flags->bit-mask (file-system-flags fs)))) | |
614 | (when (file-system-check? fs) | |
615 | (check-file-system source type)) | |
616 | ||
617 | ;; Create the mount point. Most of the time this is a directory, but | |
618 | ;; in the case of a bind mount, a regular file or socket may be needed. | |
619 | (if (and (= MS_BIND (logand flags MS_BIND)) | |
620 | (not (file-is-directory? source))) | |
621 | (unless (file-exists? mount-point) | |
622 | (mkdir-p (dirname mount-point)) | |
623 | (call-with-output-file mount-point (const #t))) | |
624 | (mkdir-p mount-point)) | |
625 | ||
626 | (cond | |
627 | ((string-prefix? "nfs" type) | |
628 | (mount-nfs source mount-point type flags options)) | |
629 | (else | |
630 | (mount source mount-point type flags options))) | |
631 | ||
632 | ;; For read-only bind mounts, an extra remount is needed, as per | |
633 | ;; <http://lwn.net/Articles/281157/>, which still applies to Linux 4.0. | |
634 | (when (and (= MS_BIND (logand flags MS_BIND)) | |
635 | (= MS_RDONLY (logand flags MS_RDONLY))) | |
636 | (let ((flags (logior MS_BIND MS_REMOUNT MS_RDONLY))) | |
637 | (mount source mount-point type flags #f))))) | |
e2f4b305 LC |
638 | |
639 | ;;; file-systems.scm ends here |