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