gnu: motion: Update to 4.2.2.
[jackhill/guix/guix.git] / gnu / installer / parted.scm
CommitLineData
69a934f2 1;;; GNU Guix --- Functional package management for GNU
f297c213 2;;; Copyright © 2018, 2019 Mathieu Othacehe <m.othacehe@gmail.com>
69a934f2
MO
3;;;
4;;; This file is part of GNU Guix.
5;;;
6;;; GNU Guix is free software; you can redistribute it and/or modify it
7;;; under the terms of the GNU General Public License as published by
8;;; the Free Software Foundation; either version 3 of the License, or (at
9;;; your option) any later version.
10;;;
11;;; GNU Guix is distributed in the hope that it will be useful, but
12;;; WITHOUT ANY WARRANTY; without even the implied warranty of
13;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14;;; GNU General Public License for more details.
15;;;
16;;; You should have received a copy of the GNU General Public License
17;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
18
19(define-module (gnu installer parted)
20 #:use-module (gnu installer steps)
21 #:use-module (gnu installer utils)
22 #:use-module (gnu installer newt page)
23 #:use-module (gnu system uuid)
24 #:use-module ((gnu build file-systems)
bf304dbc 25 #:select (read-partition-uuid
59e8f3c3 26 read-luks-partition-uuid))
69a934f2
MO
27 #:use-module (guix build syscalls)
28 #:use-module (guix build utils)
29 #:use-module (guix records)
bf304dbc 30 #:use-module (guix utils)
69a934f2
MO
31 #:use-module (guix i18n)
32 #:use-module (parted)
33 #:use-module (ice-9 match)
f297c213 34 #:use-module (ice-9 regex)
bf304dbc 35 #:use-module (rnrs io ports)
69a934f2
MO
36 #:use-module (srfi srfi-1)
37 #:use-module (srfi srfi-26)
38 #:use-module (srfi srfi-34)
39 #:use-module (srfi srfi-35)
40 #:export (<user-partition>
41 user-partition
42 make-user-partition
43 user-partition?
44 user-partition-name
45 user-partition-type
44b2d31c
MO
46 user-partition-file-name
47 user-partition-disk-file-name
bf304dbc
MO
48 user-partition-crypt-label
49 user-partition-crypt-password
69a934f2
MO
50 user-partition-fs-type
51 user-partition-bootable?
52 user-partition-esp?
53 user-partition-bios-grub?
54 user-partition-size
55 user-partition-start
56 user-partition-end
57 user-partition-mount-point
85caf5f3 58 user-partition-need-formatting?
69a934f2
MO
59 user-partition-parted-object
60
61 find-esp-partition
62 data-partition?
63 metadata-partition?
64 freespace-partition?
65 small-freespace-partition?
66 normal-partition?
67 extended-partition?
68 logical-partition?
69 esp-partition?
70 boot-partition?
71 default-esp-mount-point
72
73 with-delay-device-in-use?
74 force-device-sync
75 non-install-devices
76 partition-user-type
77 user-fs-type-name
78 partition-filesystem-user-type
79 partition-get-flags
80 partition->user-partition
81 create-special-user-partitions
82 find-user-partition-by-parted-object
83
84 device-description
85 partition-end-formatted
86 partition-print-number
87 partition-description
88 partitions-descriptions
89 user-partition-description
90
91 &max-primary-exceeded
92 max-primary-exceeded?
93 &extended-creation-error
94 extended-creation-error?
95 &logical-creation-error
96 logical-creation-error?
97
98 can-create-partition?
99 mklabel
100 mkpart
101 rmpart
102
103 create-adjacent-partitions
104 auto-partition
105
106 &no-root-mount-point
107 no-root-mount-point?
108
109 check-user-partitions
44b2d31c 110 set-user-partitions-file-name
69a934f2
MO
111 format-user-partitions
112 mount-user-partitions
113 umount-user-partitions
114 with-mounted-partitions
115 user-partitions->file-systems
116 user-partitions->configuration
117
118 init-parted
119 free-parted))
120
121\f
122;;;
123;;; Partition record.
124;;;
125
126(define-record-type* <user-partition>
127 user-partition make-user-partition
128 user-partition?
129 (name user-partition-name ;string
130 (default #f))
131 (type user-partition-type
132 (default 'normal)) ; 'normal | 'logical | 'extended
44b2d31c 133 (file-name user-partition-file-name
69a934f2 134 (default #f))
44b2d31c 135 (disk-file-name user-partition-disk-file-name
69a934f2 136 (default #f))
bf304dbc
MO
137 (crypt-label user-partition-crypt-label
138 (default #f))
139 (crypt-password user-partition-crypt-password
140 (default #f))
69a934f2
MO
141 (fs-type user-partition-fs-type
142 (default 'ext4))
143 (bootable? user-partition-bootable?
144 (default #f))
145 (esp? user-partition-esp?
146 (default #f))
147 (bios-grub? user-partition-bios-grub?
148 (default #f))
149 (size user-partition-size
150 (default #f))
151 (start user-partition-start ;start as string (e.g. '11MB')
152 (default #f))
153 (end user-partition-end ;same as start
154 (default #f))
155 (mount-point user-partition-mount-point ;string
156 (default #f))
85caf5f3 157 (need-formatting? user-partition-need-formatting? ; boolean
69a934f2
MO
158 (default #f))
159 (parted-object user-partition-parted-object ; <partition> from parted
160 (default #f)))
161
162\f
163;;
164;; Utilities.
165;;
166
167(define (find-esp-partition partitions)
168 "Find and return the ESP partition among PARTITIONS."
169 (find esp-partition? partitions))
170
171(define (data-partition? partition)
172 "Return #t if PARTITION is a partition dedicated to data (by opposition to
173freespace, metadata and protected partition types), return #f otherwise."
174 (let ((type (partition-type partition)))
175 (not (any (lambda (flag)
176 (member flag type))
177 '(free-space metadata protected)))))
178
179(define (metadata-partition? partition)
180 "Return #t if PARTITION is a metadata partition, #f otherwise."
181 (let ((type (partition-type partition)))
182 (member 'metadata type)))
183
184(define (freespace-partition? partition)
185 "Return #t if PARTITION is a free-space partition, #f otherwise."
186 (let ((type (partition-type partition)))
187 (member 'free-space type)))
188
189(define* (small-freespace-partition? device
190 partition
191 #:key (max-size MEBIBYTE-SIZE))
192 "Return #t is PARTITION is a free-space partition with less a size strictly
193inferior to MAX-SIZE, #f otherwise."
194 (let ((size (partition-length partition))
195 (max-sector-size (/ max-size
196 (device-sector-size device))))
197 (< size max-sector-size)))
198
199(define (normal-partition? partition)
200 "return #t if partition is a normal partition, #f otherwise."
201 (let ((type (partition-type partition)))
202 (member 'normal type)))
203
204(define (extended-partition? partition)
205 "return #t if partition is an extended partition, #f otherwise."
206 (let ((type (partition-type partition)))
207 (member 'extended type)))
208
209(define (logical-partition? partition)
210 "Return #t if PARTITION is a logical partition, #f otherwise."
211 (let ((type (partition-type partition)))
212 (member 'logical type)))
213
214(define (partition-user-type partition)
215 "Return the type of PARTITION, to be stored in the TYPE field of
216<user-partition> record. It can be 'normal, 'extended or 'logical."
217 (cond ((normal-partition? partition)
218 'normal)
219 ((extended-partition? partition)
220 'extended)
221 ((logical-partition? partition)
222 'logical)
223 (else #f)))
224
225(define (esp-partition? partition)
226 "Return #t if partition has the ESP flag, return #f otherwise."
227 (let* ((disk (partition-disk partition))
228 (disk-type (disk-disk-type disk))
229 (has-extended? (disk-type-check-feature
230 disk-type
231 DISK-TYPE-FEATURE-EXTENDED)))
232 (and (data-partition? partition)
233 (not has-extended?)
234 (partition-is-flag-available? partition PARTITION-FLAG-ESP)
235 (partition-get-flag partition PARTITION-FLAG-ESP))))
236
237(define (boot-partition? partition)
238 "Return #t if partition has the boot flag, return #f otherwise."
239 (and (data-partition? partition)
240 (partition-is-flag-available? partition PARTITION-FLAG-BOOT)
241 (partition-get-flag partition PARTITION-FLAG-BOOT)))
242
243
244;; The default mount point for ESP partitions.
245(define default-esp-mount-point
246 (make-parameter "/boot/efi"))
247
248(define (efi-installation?)
249 "Return #t if an EFI installation should be performed, #f otherwise."
250 (file-exists? "/sys/firmware/efi"))
251
252(define (user-fs-type-name fs-type)
253 "Return the name of FS-TYPE as specified by libparted."
254 (case fs-type
255 ((ext4) "ext4")
256 ((btrfs) "btrfs")
257 ((fat32) "fat32")
258 ((swap) "linux-swap")))
259
260(define (user-fs-type->mount-type fs-type)
261 "Return the mount type of FS-TYPE."
262 (case fs-type
263 ((ext4) "ext4")
264 ((btrfs) "btrfs")
265 ((fat32) "vfat")))
266
267(define (partition-filesystem-user-type partition)
268 "Return the filesystem type of PARTITION, to be stored in the FS-TYPE field
269of <user-partition> record."
270 (let ((fs-type (partition-fs-type partition)))
271 (and fs-type
272 (let ((name (filesystem-type-name fs-type)))
273 (cond
274 ((string=? name "ext4") 'ext4)
275 ((string=? name "btrfs") 'btrfs)
276 ((string=? name "fat32") 'fat32)
277 ((or (string=? name "swsusp")
278 (string=? name "linux-swap(v0)")
279 (string=? name "linux-swap(v1)"))
280 'swap)
281 (else
282 (error (format #f "Unhandled ~a fs-type~%" name))))))))
283
284(define (partition-get-flags partition)
285 "Return the list of flags supported by the given PARTITION."
286 (filter-map (lambda (flag)
287 (and (partition-get-flag partition flag)
288 flag))
289 (partition-flags partition)))
290
291(define (partition->user-partition partition)
292 "Convert PARTITION into a <user-partition> record and return it."
293 (let* ((disk (partition-disk partition))
294 (device (disk-device disk))
295 (disk-type (disk-disk-type disk))
296 (has-name? (disk-type-check-feature
297 disk-type
298 DISK-TYPE-FEATURE-PARTITION-NAME))
299 (name (and has-name?
300 (data-partition? partition)
301 (partition-get-name partition))))
302 (user-partition
303 (name (and (and name
304 (not (string=? name "")))
305 name))
306 (type (or (partition-user-type partition)
307 'normal))
44b2d31c
MO
308 (file-name (partition-get-path partition))
309 (disk-file-name (device-path device))
69a934f2
MO
310 (fs-type (or (partition-filesystem-user-type partition)
311 'ext4))
312 (mount-point (and (esp-partition? partition)
313 (default-esp-mount-point)))
314 (bootable? (boot-partition? partition))
315 (esp? (esp-partition? partition))
316 (parted-object partition))))
317
318(define (create-special-user-partitions partitions)
319 "Return a list with a <user-partition> record describing the ESP partition
320found in PARTITIONS, if any."
321 (filter-map (lambda (partition)
322 (and (esp-partition? partition)
323 (partition->user-partition partition)))
324 partitions))
325
326(define (find-user-partition-by-parted-object user-partitions
327 partition)
328 "Find and return the <user-partition> record in USER-PARTITIONS list which
329PARTED-OBJECT field equals PARTITION, return #f if not found."
330 (find (lambda (user-partition)
331 (equal? (user-partition-parted-object user-partition)
332 partition))
333 user-partitions))
334
335\f
336;;
337;; Devices
338;;
339
44b2d31c 340(define (with-delay-device-in-use? file-name)
69a934f2
MO
341 "Call DEVICE-IN-USE? with a few retries, as the first re-read will often
342fail. See rereadpt function in wipefs.c of util-linux for an explanation."
f297c213
MO
343 ;; Kernel always return EINVAL for BLKRRPART on loopdevices.
344 (and (not (string-match "/dev/loop*" file-name))
345 (let loop ((try 4))
346 (usleep 250000)
347 (let ((in-use? (device-in-use? file-name)))
348 (if (and in-use? (> try 0))
349 (loop (- try 1))
350 in-use?)))))
69a934f2
MO
351
352(define* (force-device-sync device)
353 "Force a flushing of the given DEVICE."
354 (device-open device)
355 (device-sync device)
356 (device-close device))
357
358(define (non-install-devices)
359 "Return all the available devices, except the busy one, allegedly the
360install device. DEVICE-IS-BUSY? is a parted call, checking if the device is
361mounted. The install image uses an overlayfs so the install device does not
362appear as mounted and won't be considered as busy. So use also DEVICE-IN-USE?
363from (guix build syscalls) module, who will try to re-read the device's
364partition table to determine whether or not it is already used (like sfdisk
365from util-linux)."
366 (remove (lambda (device)
44b2d31c 367 (let ((file-name (device-path device)))
69a934f2 368 (or (device-is-busy? device)
44b2d31c 369 (with-delay-device-in-use? file-name))))
69a934f2
MO
370 (devices)))
371
372\f
373;;
374;; Disk and partition printing.
375;;
376
377(define* (device-description device #:optional disk)
378 "Return a string describing the given DEVICE."
379 (let* ((type (device-type device))
44b2d31c 380 (file-name (device-path device))
69a934f2
MO
381 (model (device-model device))
382 (type-str (device-type->string type))
383 (disk-type (if disk
384 (disk-disk-type disk)
385 (disk-probe device)))
386 (length (device-length device))
387 (sector-size (device-sector-size device))
388 (end (unit-format-custom-byte device
389 (* length sector-size)
390 UNIT-GIGABYTE)))
391 (string-join
392 `(,@(if (string=? model "")
393 `(,type-str)
394 `(,model ,(string-append "(" type-str ")")))
44b2d31c 395 ,file-name
69a934f2
MO
396 ,end
397 ,@(if disk-type
398 `(,(disk-type-name disk-type))
399 '()))
400 " ")))
401
402(define (partition-end-formatted device partition)
403 "Return as a string the end of PARTITION with the relevant unit."
404 (unit-format-byte
405 device
406 (-
407 (* (+ (partition-end partition) 1)
408 (device-sector-size device))
409 1)))
410
411(define (partition-print-number partition)
412 "Convert the given partition NUMBER to string."
413 (let ((number (partition-number partition)))
414 (number->string number)))
415
416(define (partition-description partition user-partition)
417 "Return a string describing the given PARTITION, located on the DISK of
418DEVICE."
419
420 (define (partition-print-type partition)
421 "Return the type of PARTITION as a string."
422 (if (freespace-partition? partition)
423 (G_ "Free space")
424 (let ((type (partition-type partition)))
425 (match type
426 ((type-symbol)
427 (symbol->string type-symbol))))))
428
429 (define (partition-print-flags partition)
430 "Return the flags of PARTITION as a string of comma separated flags."
431 (string-join
432 (filter-map
433 (lambda (flag)
434 (and (partition-get-flag partition flag)
435 (partition-flag-get-name flag)))
436 (partition-flags partition))
437 ","))
438
439 (define (maybe-string-pad string length)
440 "Returned a string formatted by padding STRING of LENGTH characters to the
441right. If STRING is #f use an empty string."
bf304dbc
MO
442 (if (and string (not (string=? string "")))
443 (string-pad-right string length)
444 ""))
69a934f2
MO
445
446 (let* ((disk (partition-disk partition))
447 (device (disk-device disk))
448 (disk-type (disk-disk-type disk))
449 (has-name? (disk-type-check-feature
450 disk-type
451 DISK-TYPE-FEATURE-PARTITION-NAME))
452 (has-extended? (disk-type-check-feature
453 disk-type
454 DISK-TYPE-FEATURE-EXTENDED))
455 (part-type (partition-print-type partition))
456 (number (and (not (freespace-partition? partition))
457 (partition-print-number partition)))
458 (name (and has-name?
459 (if (freespace-partition? partition)
460 (G_ "Free space")
461 (partition-get-name partition))))
462 (start (unit-format device
463 (partition-start partition)))
464 (end (partition-end-formatted device partition))
465 (size (unit-format device (partition-length partition)))
466 (fs-type (partition-fs-type partition))
467 (fs-type-name (and fs-type
468 (filesystem-type-name fs-type)))
bf304dbc
MO
469 (crypt-label (and user-partition
470 (user-partition-crypt-label user-partition)))
69a934f2
MO
471 (flags (and (not (freespace-partition? partition))
472 (partition-print-flags partition)))
473 (mount-point (and user-partition
474 (user-partition-mount-point user-partition))))
475 `(,(or number "")
476 ,@(if has-extended?
477 (list part-type)
478 '())
479 ,size
480 ,(or fs-type-name "")
481 ,(or flags "")
482 ,(or mount-point "")
bf304dbc 483 ,(or crypt-label "")
69a934f2
MO
484 ,(maybe-string-pad name 30))))
485
486(define (partitions-descriptions partitions user-partitions)
487 "Return a list of strings describing all the partitions found on
488DEVICE. METADATA partitions are not described. The strings are padded to the
489right so that they can be displayed as a table."
490
491 (define (max-length-column lists column-index)
492 "Return the maximum length of the string at position COLUMN-INDEX in the
493list of string lists LISTS."
494 (apply max
495 (map (lambda (list)
496 (string-length
497 (list-ref list column-index)))
498 lists)))
499
500 (define (pad-descriptions descriptions)
501 "Return a padded version of the list of string lists DESCRIPTIONS. The
502strings are padded to the length of the longer string in a same column, as
503determined by MAX-LENGTH-COLUMN procedure."
504 (let* ((description-length (length (car descriptions)))
505 (paddings (map (lambda (index)
506 (max-length-column descriptions index))
507 (iota description-length))))
508 (map (lambda (description)
509 (map string-pad-right description paddings))
510 descriptions)))
511
512 (let* ((descriptions
513 (map
514 (lambda (partition)
515 (let ((user-partition
516 (find-user-partition-by-parted-object user-partitions
517 partition)))
518 (partition-description partition user-partition)))
519 partitions))
520 (padded-descriptions (if (null? partitions)
521 '()
522 (pad-descriptions descriptions))))
523 (map (cut string-join <> " ") padded-descriptions)))
524
525(define (user-partition-description user-partition)
526 "Return a string describing the given USER-PARTITION record."
527 (let* ((partition (user-partition-parted-object user-partition))
528 (disk (partition-disk partition))
529 (disk-type (disk-disk-type disk))
530 (device (disk-device disk))
531 (has-name? (disk-type-check-feature
532 disk-type
533 DISK-TYPE-FEATURE-PARTITION-NAME))
534 (has-extended? (disk-type-check-feature
535 disk-type
536 DISK-TYPE-FEATURE-EXTENDED))
537 (name (user-partition-name user-partition))
538 (type (user-partition-type user-partition))
539 (type-name (symbol->string type))
540 (fs-type (user-partition-fs-type user-partition))
541 (fs-type-name (user-fs-type-name fs-type))
542 (bootable? (user-partition-bootable? user-partition))
543 (esp? (user-partition-esp? user-partition))
85caf5f3 544 (need-formatting? (user-partition-need-formatting? user-partition))
bf304dbc 545 (crypt-label (user-partition-crypt-label user-partition))
69a934f2
MO
546 (size (user-partition-size user-partition))
547 (mount-point (user-partition-mount-point user-partition)))
548 `(,@(if has-name?
549 `((name . ,(string-append "Name: " (or name "None"))))
550 '())
551 ,@(if (and has-extended?
552 (freespace-partition? partition)
553 (not (eq? type 'logical)))
554 `((type . ,(string-append "Type: " type-name)))
555 '())
556 ,@(if (eq? type 'extended)
557 '()
558 `((fs-type . ,(string-append "Filesystem type: " fs-type-name))))
559 ,@(if (or (eq? type 'extended)
560 (eq? fs-type 'swap)
561 (not has-extended?))
562 '()
563 `((bootable . ,(string-append "Bootable flag: "
564 (if bootable? "On" "Off")))))
565 ,@(if (and (not has-extended?)
566 (not (eq? fs-type 'swap)))
567 `((esp? . ,(string-append "ESP flag: "
568 (if esp? "On" "Off"))))
569 '())
570 ,@(if (freespace-partition? partition)
571 (let ((size-formatted
572 (or size (unit-format device
573 (partition-length partition)))))
574 `((size . ,(string-append "Size : " size-formatted))))
575 '())
bf304dbc
MO
576 ,@(if (or (eq? type 'extended)
577 (eq? fs-type 'swap))
578 '()
579 `((crypt-label
580 . ,(string-append
581 "Encryption: "
582 (if crypt-label
583 (format #f "Yes (label ~a)" crypt-label)
584 "No")))))
69a934f2
MO
585 ,@(if (or (freespace-partition? partition)
586 (eq? fs-type 'swap))
587 '()
85caf5f3 588 `((need-formatting?
69a934f2 589 . ,(string-append "Format the partition? : "
85caf5f3 590 (if need-formatting? "Yes" "No")))))
69a934f2
MO
591 ,@(if (or (eq? type 'extended)
592 (eq? fs-type 'swap))
593 '()
594 `((mount-point
595 . ,(string-append "Mount point : "
596 (or mount-point
597 (and esp? (default-esp-mount-point))
598 "None"))))))))
599
600\f
601;;
602;; Partition table creation.
603;;
604
605(define (mklabel device type-name)
606 "Create a partition table on DEVICE. TYPE-NAME is the type of the partition
607table, \"msdos\" or \"gpt\"."
608 (let ((type (disk-type-get type-name)))
609 (disk-new-fresh device type)))
610
611\f
612;;
613;; Partition creation.
614;;
615
616;; The maximum count of primary partitions is exceeded.
617(define-condition-type &max-primary-exceeded &condition
618 max-primary-exceeded?)
619
620;; It is not possible to create an extended partition.
621(define-condition-type &extended-creation-error &condition
622 extended-creation-error?)
623
624;; It is not possible to create a logical partition.
625(define-condition-type &logical-creation-error &condition
626 logical-creation-error?)
627
628(define (can-create-primary? disk)
629 "Return #t if it is possible to create a primary partition on DISK, return
630#f otherwise."
631 (let ((max-primary (disk-get-max-primary-partition-count disk)))
632 (find (lambda (number)
633 (not (disk-get-partition disk number)))
634 (iota max-primary 1))))
635
636(define (can-create-extended? disk)
637 "Return #t if it is possible to create an extended partition on DISK, return
638#f otherwise."
639 (let* ((disk-type (disk-disk-type disk))
640 (has-extended? (disk-type-check-feature
641 disk-type
642 DISK-TYPE-FEATURE-EXTENDED)))
643 (and (can-create-primary? disk)
644 has-extended?
645 (not (disk-extended-partition disk)))))
646
647(define (can-create-logical? disk)
648 "Return #t is it is possible to create a logical partition on DISK, return
649#f otherwise."
650 (let* ((disk-type (disk-disk-type disk))
651 (has-extended? (disk-type-check-feature
652 disk-type
653 DISK-TYPE-FEATURE-EXTENDED)))
654 (and has-extended?
655 (disk-extended-partition disk))))
656
657(define (can-create-partition? user-part)
658 "Return #t if it is possible to create the given USER-PART record, return #f
659otherwise."
660 (let* ((type (user-partition-type user-part))
661 (partition (user-partition-parted-object user-part))
662 (disk (partition-disk partition)))
663 (case type
664 ((normal)
665 (or (can-create-primary? disk)
666 (raise
667 (condition (&max-primary-exceeded)))))
668 ((extended)
669 (or (can-create-extended? disk)
670 (raise
671 (condition (&extended-creation-error)))))
672 ((logical)
673 (or (can-create-logical? disk)
674 (raise
675 (condition (&logical-creation-error))))))))
676
677(define* (mkpart disk user-partition
678 #:key (previous-partition #f))
679 "Create the given USER-PARTITION on DISK. The PREVIOUS-PARTITION argument as
680to be set to the partition preceeding USER-PARTITION if any."
681
682 (define (parse-start-end start end)
683 "Parse start and end strings as positions on DEVICE expressed with a unit,
684like '100GB' or '12.2KiB'. Return a list of 4 elements, the start sector, its
685range (1 unit large area centered on start sector), the end sector and its
686range."
687 (let ((device (disk-device disk)))
688 (call-with-values
689 (lambda ()
690 (unit-parse start device))
691 (lambda (start-sector start-range)
692 (call-with-values
693 (lambda ()
694 (unit-parse end device))
695 (lambda (end-sector end-range)
696 (list start-sector start-range
697 end-sector end-range)))))))
698
699 (define* (extend-ranges! start-range end-range
700 #:key (offset 0))
701 "Try to extend START-RANGE by 1 MEBIBYTE to the right and END-RANGE by 1
702MEBIBYTE to the left. This way, if the disk is aligned on 2048 sectors of
703512KB (like frequently), we will have a chance for the
704'optimal-align-constraint' to succeed. Do not extend ranges if that would
705cause them to cross."
706 (let* ((device (disk-device disk))
707 (start-range-end (geometry-end start-range))
708 (end-range-start (geometry-start end-range))
709 (mebibyte-sector-size (/ MEBIBYTE-SIZE
710 (device-sector-size device)))
711 (new-start-range-end
712 (+ start-range-end mebibyte-sector-size offset))
713 (new-end-range-start
714 (- end-range-start mebibyte-sector-size offset)))
715 (when (< new-start-range-end new-end-range-start)
716 (geometry-set-end start-range new-start-range-end)
717 (geometry-set-start end-range new-end-range-start))))
718
719 (match (parse-start-end (user-partition-start user-partition)
720 (user-partition-end user-partition))
721 ((start-sector start-range end-sector end-range)
722 (let* ((prev-end (if previous-partition
723 (partition-end previous-partition)
724 0))
725 (start-distance (- start-sector prev-end))
726 (type (user-partition-type user-partition))
727 ;; There should be at least 2 unallocated sectors in front of each
728 ;; logical partition, otherwise parted will fail badly:
729 ;; https://gparted.org/h2-fix-msdos-pt.php#apply-action-fail.
730 (start-offset (if previous-partition
731 (- 3 start-distance)
732 0))
733 (start-sector* (if (and (eq? type 'logical)
734 (< start-distance 3))
735 (+ start-sector start-offset)
736 start-sector)))
737 ;; This is a hackery but parted almost always fails to create optimally
738 ;; aligned partitions (unless specifiying percentages) because, the
739 ;; default range of 1MB centered on the start sector is not enough when
740 ;; the optimal alignment is 2048 sectors of 512KB.
741 (extend-ranges! start-range end-range #:offset start-offset)
742
743 (let* ((device (disk-device disk))
744 (disk-type (disk-disk-type disk))
745 (length (device-length device))
746 (name (user-partition-name user-partition))
747 (filesystem-type
748 (filesystem-type-get
749 (user-fs-type-name
750 (user-partition-fs-type user-partition))))
751 (flags `(,@(if (user-partition-bootable? user-partition)
752 `(,PARTITION-FLAG-BOOT)
753 '())
754 ,@(if (user-partition-esp? user-partition)
755 `(,PARTITION-FLAG-ESP)
756 '())
757 ,@(if (user-partition-bios-grub? user-partition)
758 `(,PARTITION-FLAG-BIOS-GRUB)
759 '())))
760 (has-name? (disk-type-check-feature
761 disk-type
762 DISK-TYPE-FEATURE-PARTITION-NAME))
763 (partition-type (partition-type->int type))
764 (partition (partition-new disk
765 #:type partition-type
766 #:filesystem-type filesystem-type
767 #:start start-sector*
768 #:end end-sector))
769 (user-constraint (constraint-new
770 #:start-align 'any
771 #:end-align 'any
772 #:start-range start-range
773 #:end-range end-range
774 #:min-size 1
775 #:max-size length))
776 (dev-constraint
777 (device-get-optimal-aligned-constraint device))
778 (final-constraint (constraint-intersect user-constraint
779 dev-constraint))
780 (no-constraint (constraint-any device))
781 ;; Try to create a partition with an optimal alignment
782 ;; constraint. If it fails, fallback to creating a partition with
783 ;; no specific constraint.
784 (partition-ok?
785 (or (disk-add-partition disk partition final-constraint)
786 (disk-add-partition disk partition no-constraint))))
787 ;; Set the partition name if supported.
788 (when (and partition-ok? has-name? name)
789 (partition-set-name partition name))
790
791 ;; Set flags is required.
792 (for-each (lambda (flag)
793 (and (partition-is-flag-available? partition flag)
794 (partition-set-flag partition flag 1)))
795 flags)
796
797 (and partition-ok?
798 (partition-set-system partition filesystem-type)
799 partition))))))
800
801\f
802;;
803;; Partition destruction.
804;;
805
806(define (rmpart disk number)
807 "Remove the partition with the given NUMBER on DISK."
808 (let ((partition (disk-get-partition disk number)))
809 (disk-remove-partition disk partition)))
810
811\f
812;;
813;; Auto partitionning.
814;;
815
816(define* (create-adjacent-partitions disk partitions
817 #:key (last-partition-end 0))
818 "Create the given PARTITIONS on DISK. LAST-PARTITION-END is the sector from
819which we want to start creating partitions. The START and END of each created
820partition are computed from its SIZE value and the position of the last
821partition."
822 (let ((device (disk-device disk)))
823 (let loop ((partitions partitions)
824 (remaining-space (- (device-length device)
825 last-partition-end))
826 (start last-partition-end))
827 (match partitions
828 (() '())
829 ((partition . rest)
830 (let* ((size (user-partition-size partition))
831 (percentage-size (and (string? size)
832 (read-percentage size)))
833 (sector-size (device-sector-size device))
834 (partition-size (if percentage-size
835 (exact->inexact
836 (* (/ percentage-size 100)
837 remaining-space))
838 size))
839 (end-partition (min (- (device-length device) 1)
840 (nearest-exact-integer
841 (+ start partition-size 1))))
842 (name (user-partition-name partition))
843 (type (user-partition-type partition))
844 (fs-type (user-partition-fs-type partition))
845 (start-formatted (unit-format-custom device
846 start
847 UNIT-SECTOR))
848 (end-formatted (unit-format-custom device
849 end-partition
850 UNIT-SECTOR))
851 (new-user-partition (user-partition
852 (inherit partition)
853 (start start-formatted)
854 (end end-formatted)))
855 (new-partition
856 (mkpart disk new-user-partition)))
857 (if new-partition
858 (cons (user-partition
859 (inherit new-user-partition)
44b2d31c
MO
860 (file-name (partition-get-path new-partition))
861 (disk-file-name (device-path device))
69a934f2
MO
862 (parted-object new-partition))
863 (loop rest
864 (if (eq? type 'extended)
865 remaining-space
866 (- remaining-space
867 (partition-length new-partition)))
868 (if (eq? type 'extended)
869 (+ start 1)
870 (+ (partition-end new-partition) 1))))
871 (error
872 (format #f "Unable to create partition ~a~%" name)))))))))
873
85caf5f3 874(define (force-user-partitions-formatting user-partitions)
69a934f2
MO
875 "Set the NEED-FORMATING? fields to #t on all <user-partition> records of
876USER-PARTITIONS list and return the updated list."
877 (map (lambda (p)
878 (user-partition
879 (inherit p)
85caf5f3 880 (need-formatting? #t)))
69a934f2
MO
881 user-partitions))
882
883(define* (auto-partition disk
bf304dbc
MO
884 #:key
885 (scheme 'entire-root))
69a934f2
MO
886 "Automatically create partitions on DISK. All the previous
887partitions (except the ESP on a GPT disk, if present) are wiped. SCHEME is the
888desired partitioning scheme. It can be 'entire-root or
889'entire-root-home. 'entire-root will create a swap partition and a root
890partition occupying all the remaining space. 'entire-root-home will create a
891swap partition, a root partition and a home partition."
892 (let* ((device (disk-device disk))
893 (disk-type (disk-disk-type disk))
894 (has-extended? (disk-type-check-feature
895 disk-type
896 DISK-TYPE-FEATURE-EXTENDED))
897 (partitions (filter data-partition? (disk-partitions disk)))
898 (esp-partition (find-esp-partition partitions))
899 ;; According to
900 ;; https://wiki.archlinux.org/index.php/EFI_system_partition, the ESP
901 ;; size should be at least 550MiB.
902 (new-esp-size (nearest-exact-integer
903 (/ (* 550 MEBIBYTE-SIZE)
904 (device-sector-size device))))
905 (end-esp-partition (and esp-partition
906 (partition-end esp-partition)))
907 (non-boot-partitions (remove esp-partition? partitions))
908 (bios-grub-size (/ (* 3 MEBIBYTE-SIZE)
909 (device-sector-size device)))
910 (five-percent-disk (nearest-exact-integer
911 (* 0.05 (device-length device))))
912 (default-swap-size (nearest-exact-integer
913 (/ (* 4 GIGABYTE-SIZE)
914 (device-sector-size device))))
915 ;; Use a 4GB size for the swap if it represents less than 5% of the
916 ;; disk space. Otherwise, set the swap size to 5% of the disk space.
917 (swap-size (min default-swap-size five-percent-disk)))
918
919 (if has-extended?
920 ;; msdos - remove everything.
921 (disk-delete-all disk)
922 ;; gpt - remove everything but esp if it exists.
923 (for-each
924 (lambda (partition)
925 (and (data-partition? partition)
926 (disk-remove-partition disk partition)))
927 non-boot-partitions))
928
929 (let* ((start-partition
930 (and (not has-extended?)
931 (not esp-partition)
932 (if (efi-installation?)
933 (user-partition
934 (fs-type 'fat32)
935 (esp? #t)
936 (size new-esp-size)
937 (mount-point (default-esp-mount-point)))
938 (user-partition
939 (fs-type 'ext4)
940 (bootable? #t)
941 (bios-grub? #t)
942 (size bios-grub-size)))))
943 (new-partitions
bf304dbc
MO
944 (cond
945 ((or (eq? scheme 'entire-root)
5737ba84
MO
946 (eq? scheme 'entire-encrypted-root))
947 (let ((encrypted? (eq? scheme 'entire-encrypted-root)))
bf304dbc
MO
948 `(,@(if start-partition
949 `(,start-partition)
950 '())
5737ba84 951 ,@(if encrypted?
44b2d31c
MO
952 '()
953 `(,(user-partition
954 (fs-type 'swap)
955 (size swap-size))))
bf304dbc
MO
956 ,(user-partition
957 (fs-type 'ext4)
958 (bootable? has-extended?)
5737ba84 959 (crypt-label (and encrypted? "cryptroot"))
bf304dbc
MO
960 (size "100%")
961 (mount-point "/")))))
962 ((or (eq? scheme 'entire-root-home)
5737ba84
MO
963 (eq? scheme 'entire-encrypted-root-home))
964 (let ((encrypted? (eq? scheme 'entire-encrypted-root-home)))
bf304dbc
MO
965 `(,@(if start-partition
966 `(,start-partition)
967 '())
968 ,(user-partition
969 (fs-type 'ext4)
970 (bootable? has-extended?)
5737ba84 971 (crypt-label (and encrypted? "cryptroot"))
bf304dbc
MO
972 (size "33%")
973 (mount-point "/"))
974 ,@(if has-extended?
975 `(,(user-partition
976 (type 'extended)
977 (size "100%")))
978 '())
5737ba84 979 ,@(if encrypted?
bf304dbc
MO
980 '()
981 `(,(user-partition
982 (type (if has-extended?
983 'logical
984 'normal))
985 (fs-type 'swap)
986 (size swap-size))))
987 ,(user-partition
988 (type (if has-extended?
989 'logical
990 'normal))
991 (fs-type 'ext4)
5737ba84 992 (crypt-label (and encrypted? "crypthome"))
bf304dbc
MO
993 (size "100%")
994 (mount-point "/home")))))))
85caf5f3 995 (new-partitions* (force-user-partitions-formatting
69a934f2
MO
996 new-partitions)))
997 (create-adjacent-partitions disk
998 new-partitions*
999 #:last-partition-end
1000 (or end-esp-partition 0)))))
1001
1002\f
1003;;
1004;; Convert user-partitions.
1005;;
1006
1007;; No root mount point found.
1008(define-condition-type &no-root-mount-point &condition
1009 no-root-mount-point?)
1010
1011(define (check-user-partitions user-partitions)
1012 "Return #t if the USER-PARTITIONS lists contains one <user-partition> record
1013with a mount-point set to '/', raise &no-root-mount-point condition
1014otherwise."
1015 (let ((mount-points
1016 (map user-partition-mount-point user-partitions)))
1017 (or (member "/" mount-points)
1018 (raise
1019 (condition (&no-root-mount-point))))))
1020
44b2d31c
MO
1021(define (set-user-partitions-file-name user-partitions)
1022 "Set the partition file-name of <user-partition> records in USER-PARTITIONS
1023list and return the updated list."
69a934f2
MO
1024 (map (lambda (p)
1025 (let* ((partition (user-partition-parted-object p))
44b2d31c 1026 (file-name (partition-get-path partition)))
69a934f2
MO
1027 (user-partition
1028 (inherit p)
44b2d31c 1029 (file-name file-name))))
69a934f2
MO
1030 user-partitions))
1031
1032(define-syntax-rule (with-null-output-ports exp ...)
1033 "Evaluate EXP with both the output port and the error port pointing to the
1034bit bucket."
1035 (with-output-to-port (%make-void-port "w")
1036 (lambda ()
1037 (with-error-to-port (%make-void-port "w")
1038 (lambda () exp ...)))))
1039
1040(define (create-ext4-file-system partition)
44b2d31c 1041 "Create an ext4 file-system for PARTITION file-name."
69a934f2
MO
1042 (with-null-output-ports
1043 (invoke "mkfs.ext4" "-F" partition)))
1044
1045(define (create-fat32-file-system partition)
44b2d31c 1046 "Create an ext4 file-system for PARTITION file-name."
69a934f2
MO
1047 (with-null-output-ports
1048 (invoke "mkfs.fat" "-F32" partition)))
1049
1050(define (create-swap-partition partition)
44b2d31c 1051 "Set up swap area on PARTITION file-name."
69a934f2
MO
1052 (with-null-output-ports
1053 (invoke "mkswap" "-f" partition)))
1054
bf304dbc
MO
1055(define (call-with-luks-key-file password proc)
1056 "Write PASSWORD in a temporary file and pass it to PROC as argument."
1057 (call-with-temporary-output-file
1058 (lambda (file port)
1059 (put-string port password)
1060 (close port)
1061 (proc file))))
1062
44b2d31c
MO
1063(define (user-partition-upper-file-name user-partition)
1064 "Return the file-name of the virtual block device corresponding to
1065USER-PARTITION if it is encrypted, or the plain file-name otherwise."
bf304dbc 1066 (let ((crypt-label (user-partition-crypt-label user-partition))
44b2d31c 1067 (file-name (user-partition-file-name user-partition)))
bf304dbc
MO
1068 (if crypt-label
1069 (string-append "/dev/mapper/" crypt-label)
44b2d31c 1070 file-name)))
bf304dbc
MO
1071
1072(define (luks-format-and-open user-partition)
5737ba84 1073 "Format and open the encrypted partition pointed by USER-PARTITION."
44b2d31c 1074 (let* ((file-name (user-partition-file-name user-partition))
bf304dbc
MO
1075 (label (user-partition-crypt-label user-partition))
1076 (password (user-partition-crypt-password user-partition)))
1077 (call-with-luks-key-file
1078 password
1079 (lambda (key-file)
44b2d31c 1080 (system* "cryptsetup" "-q" "luksFormat" file-name key-file)
bf304dbc 1081 (system* "cryptsetup" "open" "--type" "luks"
44b2d31c 1082 "--key-file" key-file file-name label)))))
bf304dbc
MO
1083
1084(define (luks-close user-partition)
5737ba84 1085 "Close the encrypted partition pointed by USER-PARTITION."
bf304dbc
MO
1086 (let ((label (user-partition-crypt-label user-partition)))
1087 (system* "cryptsetup" "close" label)))
1088
69a934f2
MO
1089(define (format-user-partitions user-partitions)
1090 "Format the <user-partition> records in USER-PARTITIONS list with
1091NEED-FORMATING? field set to #t."
1092 (for-each
1093 (lambda (user-partition)
85caf5f3
LC
1094 (let* ((need-formatting?
1095 (user-partition-need-formatting? user-partition))
69a934f2 1096 (type (user-partition-type user-partition))
bf304dbc 1097 (crypt-label (user-partition-crypt-label user-partition))
44b2d31c 1098 (file-name (user-partition-upper-file-name user-partition))
69a934f2 1099 (fs-type (user-partition-fs-type user-partition)))
bf304dbc
MO
1100 (when crypt-label
1101 (luks-format-and-open user-partition))
1102
69a934f2
MO
1103 (case fs-type
1104 ((ext4)
85caf5f3 1105 (and need-formatting?
69a934f2 1106 (not (eq? type 'extended))
44b2d31c 1107 (create-ext4-file-system file-name)))
69a934f2 1108 ((fat32)
85caf5f3 1109 (and need-formatting?
69a934f2 1110 (not (eq? type 'extended))
44b2d31c 1111 (create-fat32-file-system file-name)))
69a934f2 1112 ((swap)
44b2d31c 1113 (create-swap-partition file-name))
69a934f2
MO
1114 (else
1115 ;; TODO: Add support for other file-system types.
1116 #t))))
1117 user-partitions))
1118
1119(define (sort-partitions user-partitions)
1120 "Sort USER-PARTITIONS by mount-points, so that the more nested mount-point
1121comes last. This is useful to mount/umount partitions in a coherent order."
1122 (sort user-partitions
1123 (lambda (a b)
1124 (let ((mount-point-a (user-partition-mount-point a))
1125 (mount-point-b (user-partition-mount-point b)))
1126 (string-prefix? mount-point-a mount-point-b)))))
1127
1128(define (mount-user-partitions user-partitions)
1129 "Mount the <user-partition> records in USER-PARTITIONS list on their
b624206d 1130respective mount-points."
69a934f2
MO
1131 (let* ((mount-partitions (filter user-partition-mount-point user-partitions))
1132 (sorted-partitions (sort-partitions mount-partitions)))
1133 (for-each (lambda (user-partition)
1134 (let* ((mount-point
1135 (user-partition-mount-point user-partition))
1136 (target
1137 (string-append (%installer-target-dir)
1138 mount-point))
1139 (fs-type
1140 (user-partition-fs-type user-partition))
bf304dbc
MO
1141 (crypt-label
1142 (user-partition-crypt-label user-partition))
69a934f2
MO
1143 (mount-type
1144 (user-fs-type->mount-type fs-type))
44b2d31c
MO
1145 (file-name
1146 (user-partition-upper-file-name user-partition)))
b624206d 1147 (mkdir-p target)
44b2d31c 1148 (mount file-name target mount-type)))
69a934f2
MO
1149 sorted-partitions)))
1150
1151(define (umount-user-partitions user-partitions)
b624206d 1152 "Unmount all the <user-partition> records in USER-PARTITIONS list."
69a934f2
MO
1153 (let* ((mount-partitions (filter user-partition-mount-point user-partitions))
1154 (sorted-partitions (sort-partitions mount-partitions)))
1155 (for-each (lambda (user-partition)
1156 (let* ((mount-point
1157 (user-partition-mount-point user-partition))
bf304dbc
MO
1158 (crypt-label
1159 (user-partition-crypt-label user-partition))
69a934f2
MO
1160 (target
1161 (string-append (%installer-target-dir)
1162 mount-point)))
bf304dbc
MO
1163 (umount target)
1164 (when crypt-label
1165 (luks-close user-partition))))
69a934f2
MO
1166 (reverse sorted-partitions))))
1167
b624206d
MO
1168(define (find-swap-user-partitions user-partitions)
1169 "Return the subset of <user-partition> records in USER-PARTITIONS list with
1170the FS-TYPE field set to 'swap, return the empty list if none found."
1171 (filter (lambda (user-partition)
44b2d31c
MO
1172 (let ((fs-type (user-partition-fs-type user-partition)))
1173 (eq? fs-type 'swap)))
1174 user-partitions))
b624206d
MO
1175
1176(define (start-swapping user-partitions)
1177 "Start swaping on <user-partition> records with FS-TYPE equal to 'swap."
1178 (let* ((swap-user-partitions (find-swap-user-partitions user-partitions))
44b2d31c 1179 (swap-devices (map user-partition-file-name swap-user-partitions)))
b624206d
MO
1180 (for-each swapon swap-devices)))
1181
1182(define (stop-swapping user-partitions)
1183 "Stop swaping on <user-partition> records with FS-TYPE equal to 'swap."
1184 (let* ((swap-user-partitions (find-swap-user-partitions user-partitions))
44b2d31c 1185 (swap-devices (map user-partition-file-name swap-user-partitions)))
b624206d
MO
1186 (for-each swapoff swap-devices)))
1187
69a934f2 1188(define-syntax-rule (with-mounted-partitions user-partitions exp ...)
b624206d 1189 "Mount USER-PARTITIONS and start swapping within the dynamic extent of EXP."
69a934f2
MO
1190 (dynamic-wind
1191 (lambda ()
b624206d
MO
1192 (mount-user-partitions user-partitions)
1193 (start-swapping user-partitions))
69a934f2
MO
1194 (lambda ()
1195 exp ...)
1196 (lambda ()
1197 (umount-user-partitions user-partitions)
b624206d 1198 (stop-swapping user-partitions)
69a934f2
MO
1199 #f)))
1200
1201(define (user-partition->file-system user-partition)
1202 "Convert the given USER-PARTITION record in a FILE-SYSTEM record from
1203(gnu system file-systems) module and return it."
1204 (let* ((mount-point (user-partition-mount-point user-partition))
1205 (fs-type (user-partition-fs-type user-partition))
bf304dbc 1206 (crypt-label (user-partition-crypt-label user-partition))
69a934f2 1207 (mount-type (user-fs-type->mount-type fs-type))
44b2d31c
MO
1208 (file-name (user-partition-file-name user-partition))
1209 (upper-file-name (user-partition-upper-file-name user-partition))
59e8f3c3
MO
1210 ;; Only compute uuid if partition is not encrypted.
1211 (uuid (or crypt-label
44b2d31c 1212 (uuid->string (read-partition-uuid file-name) fs-type))))
69a934f2
MO
1213 `(file-system
1214 (mount-point ,mount-point)
bf304dbc 1215 (device ,@(if crypt-label
44b2d31c 1216 `(,upper-file-name)
bf304dbc
MO
1217 `((uuid ,uuid (quote ,fs-type)))))
1218 (type ,mount-type)
1219 ,@(if crypt-label
1220 '((dependencies mapped-devices))
1221 '()))))
69a934f2
MO
1222
1223(define (user-partitions->file-systems user-partitions)
1224 "Convert the given USER-PARTITIONS list of <user-partition> records into a
1225list of <file-system> records."
1226 (filter-map
1227 (lambda (user-partition)
1228 (let ((mount-point
1229 (user-partition-mount-point user-partition)))
1230 (and mount-point
1231 (user-partition->file-system user-partition))))
1232 user-partitions))
1233
bf304dbc
MO
1234(define (user-partition->mapped-device user-partition)
1235 "Convert the given USER-PARTITION record into a MAPPED-DEVICE record
1236from (gnu system mapped-devices) and return it."
1237 (let ((label (user-partition-crypt-label user-partition))
44b2d31c 1238 (file-name (user-partition-file-name user-partition)))
bf304dbc 1239 `(mapped-device
59e8f3c3 1240 (source (uuid ,(uuid->string
44b2d31c 1241 (read-luks-partition-uuid file-name)
59e8f3c3 1242 'luks)))
bf304dbc
MO
1243 (target ,label)
1244 (type luks-device-mapping))))
1245
69a934f2
MO
1246(define (bootloader-configuration user-partitions)
1247 "Return the bootloader configuration field for USER-PARTITIONS."
1248 (let* ((root-partition
1249 (find (lambda (user-partition)
1250 (let ((mount-point
1251 (user-partition-mount-point user-partition)))
1252 (and mount-point
1253 (string=? mount-point "/"))))
1254 user-partitions))
44b2d31c 1255 (root-partition-disk (user-partition-disk-file-name root-partition)))
69a934f2
MO
1256 `((bootloader-configuration
1257 ,@(if (efi-installation?)
1258 `((bootloader grub-efi-bootloader)
1259 (target ,(default-esp-mount-point)))
1260 `((bootloader grub-bootloader)
1261 (target ,root-partition-disk)))))))
1262
1263(define (user-partitions->configuration user-partitions)
1264 "Return the configuration field for USER-PARTITIONS."
1265 (let* ((swap-user-partitions (find-swap-user-partitions user-partitions))
44b2d31c 1266 (swap-devices (map user-partition-file-name swap-user-partitions))
5737ba84 1267 (encrypted-partitions
bf304dbc 1268 (filter user-partition-crypt-label user-partitions)))
69a934f2
MO
1269 `(,@(if (null? swap-devices)
1270 '()
1271 `((swap-devices (list ,@swap-devices))))
1272 (bootloader ,@(bootloader-configuration user-partitions))
5737ba84 1273 ,@(if (null? encrypted-partitions)
bf304dbc
MO
1274 '()
1275 `((mapped-devices
1276 (list ,@(map user-partition->mapped-device
5737ba84 1277 encrypted-partitions)))))
69a934f2
MO
1278 (file-systems (cons*
1279 ,@(user-partitions->file-systems user-partitions)
1280 %base-file-systems)))))
1281
1282\f
1283;;
1284;; Initialization.
1285;;
1286
1287(define (init-parted)
1288 "Initialize libparted support."
1289 (probe-all-devices)
1290 (exception-set-handler (lambda (exception)
1291 EXCEPTION-OPTION-UNHANDLED)))
1292
1293(define (free-parted devices)
1294 "Deallocate memory used for DEVICES in parted, force sync them and wait for
1295the devices not to be used before returning."
85caf5f3 1296 ;; XXX: Formatting and further operations on disk partition table may fail
69a934f2
MO
1297 ;; because the partition table changes are not synced, or because the device
1298 ;; is still in use, even if parted should have finished editing
1299 ;; partitions. This is not well understood, but syncing devices and waiting
1300 ;; them to stop returning EBUSY to BLKRRPART ioctl seems to be enough. The
1301 ;; same kind of issue is described here:
1302 ;; https://mail.gnome.org/archives/commits-list/2013-March/msg18423.html.
44b2d31c 1303 (let ((device-file-names (map device-path devices)))
69a934f2
MO
1304 (for-each force-device-sync devices)
1305 (free-all-devices)
44b2d31c
MO
1306 (for-each (lambda (file-name)
1307 (let ((in-use? (with-delay-device-in-use? file-name)))
69a934f2
MO
1308 (and in-use?
1309 (error
1310 (format #f (G_ "Device ~a is still in use.")
44b2d31c
MO
1311 file-name)))))
1312 device-file-names)))