1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2018, 2019 Mathieu Othacehe <m.othacehe@gmail.com>
3 ;;; Copyright © 2019, 2020 Ludovic Courtès <ludo@gnu.org>
4 ;;; Copyright © 2020 Tobias Geerinckx-Rice <me@tobias.gr>
6 ;;; This file is part of GNU Guix.
8 ;;; GNU Guix is free software; you can redistribute it and/or modify it
9 ;;; under the terms of the GNU General Public License as published by
10 ;;; the Free Software Foundation; either version 3 of the License, or (at
11 ;;; your option) any later version.
13 ;;; GNU Guix is distributed in the hope that it will be useful, but
14 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;;; GNU General Public License for more details.
18 ;;; You should have received a copy of the GNU General Public License
19 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
21 (define-module (gnu installer parted)
22 #:use-module (gnu installer steps)
23 #:use-module (gnu installer utils)
24 #:use-module (gnu installer newt page)
25 #:use-module (gnu system uuid)
26 #:use-module ((gnu build file-systems)
27 #:select (read-partition-uuid
28 read-luks-partition-uuid))
29 #:use-module ((gnu build linux-modules)
30 #:select (missing-modules))
31 #:use-module ((gnu system linux-initrd)
32 #:select (%base-initrd-modules))
33 #:use-module (guix build syscalls)
34 #:use-module (guix build utils)
35 #:use-module (guix records)
36 #:use-module (guix utils)
37 #:use-module (guix i18n)
39 #:use-module (ice-9 match)
40 #:use-module (ice-9 regex)
41 #:use-module (rnrs io ports)
42 #:use-module (srfi srfi-1)
43 #:use-module (srfi srfi-26)
44 #:use-module (srfi srfi-34)
45 #:use-module (srfi srfi-35)
46 #:export (<user-partition>
52 user-partition-file-name
53 user-partition-disk-file-name
54 user-partition-crypt-label
55 user-partition-crypt-password
56 user-partition-fs-type
57 user-partition-bootable?
59 user-partition-bios-grub?
63 user-partition-mount-point
64 user-partition-need-formatting?
65 user-partition-parted-object
68 small-freespace-partition?
71 default-esp-mount-point
73 with-delay-device-in-use?
78 partition-filesystem-user-type
80 partition->user-partition
81 create-special-user-partitions
82 find-user-partition-by-parted-object
85 partition-end-formatted
86 partition-print-number
88 partitions-descriptions
89 user-partition-description
93 &extended-creation-error
94 extended-creation-error?
95 &logical-creation-error
96 logical-creation-error?
108 check-user-partitions
109 set-user-partitions-file-name
110 format-user-partitions
111 mount-user-partitions
112 umount-user-partitions
113 with-mounted-partitions
114 user-partitions->file-systems
115 user-partitions->configuration
122 ;;; Partition record.
125 (define-record-type* <user-partition>
126 user-partition make-user-partition
128 (name user-partition-name ;string
130 (type user-partition-type
131 (default 'normal)) ; 'normal | 'logical | 'extended
132 (file-name user-partition-file-name
134 (disk-file-name user-partition-disk-file-name
136 (crypt-label user-partition-crypt-label
138 (crypt-password user-partition-crypt-password
140 (fs-type user-partition-fs-type
142 (bootable? user-partition-bootable?
144 (esp? user-partition-esp?
146 (bios-grub? user-partition-bios-grub?
148 (size user-partition-size
150 (start user-partition-start ;start as string (e.g. '11MB')
152 (end user-partition-end ;same as start
154 (mount-point user-partition-mount-point ;string
156 (need-formatting? user-partition-need-formatting? ; boolean
158 (parted-object user-partition-parted-object ; <partition> from parted
166 (define (find-esp-partition partitions)
167 "Find and return the ESP partition among PARTITIONS."
168 (find esp-partition? partitions))
170 (define* (small-freespace-partition? device
172 #:key (max-size MEBIBYTE-SIZE))
173 "Return #t is PARTITION is a free-space partition with less a size strictly
174 inferior to MAX-SIZE, #f otherwise."
175 (let ((size (partition-length partition))
176 (max-sector-size (/ max-size
177 (device-sector-size device))))
178 (< size max-sector-size)))
180 (define (partition-user-type partition)
181 "Return the type of PARTITION, to be stored in the TYPE field of
182 <user-partition> record. It can be 'normal, 'extended or 'logical."
183 (cond ((normal-partition? partition)
185 ((extended-partition? partition)
187 ((logical-partition? partition)
191 (define (esp-partition? partition)
192 "Return #t if partition has the ESP flag, return #f otherwise."
193 (let* ((disk (partition-disk partition))
194 (disk-type (disk-disk-type disk))
195 (has-extended? (disk-type-check-feature
197 DISK-TYPE-FEATURE-EXTENDED)))
198 (and (data-partition? partition)
200 (partition-is-flag-available? partition PARTITION-FLAG-ESP)
201 (partition-get-flag partition PARTITION-FLAG-ESP))))
203 (define (boot-partition? partition)
204 "Return #t if partition has the boot flag, return #f otherwise."
205 (and (data-partition? partition)
206 (partition-is-flag-available? partition PARTITION-FLAG-BOOT)
207 (partition-get-flag partition PARTITION-FLAG-BOOT)))
210 ;; The default mount point for ESP partitions.
211 (define default-esp-mount-point
212 (make-parameter "/boot/efi"))
214 (define (efi-installation?)
215 "Return #t if an EFI installation should be performed, #f otherwise."
216 (file-exists? "/sys/firmware/efi"))
218 (define (user-fs-type-name fs-type)
219 "Return the name of FS-TYPE as specified by libparted."
226 ((swap) "linux-swap")))
228 (define (user-fs-type->mount-type fs-type)
229 "Return the mount type of FS-TYPE."
237 (define (partition-filesystem-user-type partition)
238 "Return the filesystem type of PARTITION, to be stored in the FS-TYPE field
239 of <user-partition> record."
240 (let ((fs-type (partition-fs-type partition)))
242 (let ((name (filesystem-type-name fs-type)))
244 ((string=? name "ext4") 'ext4)
245 ((string=? name "btrfs") 'btrfs)
246 ((string=? name "fat16") 'fat16)
247 ((string=? name "fat32") 'fat32)
248 ((string=? name "jfs") 'jfs)
249 ((or (string=? name "swsusp")
250 (string=? name "linux-swap(v0)")
251 (string=? name "linux-swap(v1)"))
254 (error (format #f "Unhandled ~a fs-type~%" name))))))))
256 (define (partition-get-flags partition)
257 "Return the list of flags supported by the given PARTITION."
258 (filter-map (lambda (flag)
259 (and (partition-get-flag partition flag)
261 (partition-flags partition)))
263 (define (partition->user-partition partition)
264 "Convert PARTITION into a <user-partition> record and return it."
265 (let* ((disk (partition-disk partition))
266 (device (disk-device disk))
267 (disk-type (disk-disk-type disk))
268 (has-name? (disk-type-check-feature
270 DISK-TYPE-FEATURE-PARTITION-NAME))
272 (data-partition? partition)
273 (partition-get-name partition))))
276 (not (string=? name "")))
278 (type (or (partition-user-type partition)
280 (file-name (partition-get-path partition))
281 (disk-file-name (device-path device))
282 (fs-type (or (partition-filesystem-user-type partition)
284 (mount-point (and (esp-partition? partition)
285 (default-esp-mount-point)))
286 (bootable? (boot-partition? partition))
287 (esp? (esp-partition? partition))
288 (parted-object partition))))
290 (define (create-special-user-partitions partitions)
291 "Return a list with a <user-partition> record describing the ESP partition
292 found in PARTITIONS, if any."
293 (filter-map (lambda (partition)
294 (and (esp-partition? partition)
295 (partition->user-partition partition)))
298 (define (find-user-partition-by-parted-object user-partitions
300 "Find and return the <user-partition> record in USER-PARTITIONS list which
301 PARTED-OBJECT field equals PARTITION, return #f if not found."
302 (find (lambda (user-partition)
303 (equal? (user-partition-parted-object user-partition)
312 (define (with-delay-device-in-use? file-name)
313 "Call DEVICE-IN-USE? with a few retries, as the first re-read will often
314 fail. See rereadpt function in wipefs.c of util-linux for an explanation."
315 ;; Kernel always return EINVAL for BLKRRPART on loopdevices.
316 (and (not (string-match "/dev/loop*" file-name))
319 (let ((in-use? (device-in-use? file-name)))
320 (if (and in-use? (> try 0))
324 (define* (force-device-sync device)
325 "Force a flushing of the given DEVICE."
328 (device-close device))
330 (define (non-install-devices)
331 "Return all the available devices, except the busy one, allegedly the
332 install device. DEVICE-IS-BUSY? is a parted call, checking if the device is
333 mounted. The install image uses an overlayfs so the install device does not
334 appear as mounted and won't be considered as busy. So use also DEVICE-IN-USE?
335 from (guix build syscalls) module, who will try to re-read the device's
336 partition table to determine whether or not it is already used (like sfdisk
338 (remove (lambda (device)
339 (let ((file-name (device-path device)))
340 (or (device-is-busy? device)
341 (with-delay-device-in-use? file-name))))
346 ;; Disk and partition printing.
349 (define* (device-description device #:optional disk)
350 "Return a string describing the given DEVICE."
351 (let* ((type (device-type device))
352 (file-name (device-path device))
353 (model (device-model device))
354 (type-str (device-type->string type))
356 (disk-disk-type disk)
357 (disk-probe device)))
358 (length (device-length device))
359 (sector-size (device-sector-size device))
360 (end (unit-format-custom-byte device
361 (* length sector-size)
364 `(,@(if (string=? model "")
366 `(,model ,(string-append "(" type-str ")")))
370 `(,(disk-type-name disk-type))
374 (define (partition-end-formatted device partition)
375 "Return as a string the end of PARTITION with the relevant unit."
379 (* (+ (partition-end partition) 1)
380 (device-sector-size device))
383 (define (partition-print-number partition)
384 "Convert the given partition NUMBER to string."
385 (let ((number (partition-number partition)))
386 (number->string number)))
388 (define (partition-description partition user-partition)
389 "Return a string describing the given PARTITION, located on the DISK of
392 (define (partition-print-type partition)
393 "Return the type of PARTITION as a string."
394 (if (freespace-partition? partition)
396 (let ((type (partition-type partition)))
399 (symbol->string type-symbol))))))
401 (define (partition-print-flags partition)
402 "Return the flags of PARTITION as a string of comma separated flags."
406 (and (partition-get-flag partition flag)
407 (partition-flag-get-name flag)))
408 (partition-flags partition))
411 (define (maybe-string-pad string length)
412 "Returned a string formatted by padding STRING of LENGTH characters to the
413 right. If STRING is #f use an empty string."
414 (if (and string (not (string=? string "")))
415 (string-pad-right string length)
418 (let* ((disk (partition-disk partition))
419 (device (disk-device disk))
420 (disk-type (disk-disk-type disk))
421 (has-name? (disk-type-check-feature
423 DISK-TYPE-FEATURE-PARTITION-NAME))
424 (has-extended? (disk-type-check-feature
426 DISK-TYPE-FEATURE-EXTENDED))
427 (part-type (partition-print-type partition))
428 (number (and (not (freespace-partition? partition))
429 (partition-print-number partition)))
431 (if (freespace-partition? partition)
433 (partition-get-name partition))))
434 (start (unit-format device
435 (partition-start partition)))
436 (end (partition-end-formatted device partition))
437 (size (unit-format device (partition-length partition)))
438 (fs-type (partition-fs-type partition))
439 (fs-type-name (and fs-type
440 (filesystem-type-name fs-type)))
441 (crypt-label (and user-partition
442 (user-partition-crypt-label user-partition)))
443 (flags (and (not (freespace-partition? partition))
444 (partition-print-flags partition)))
445 (mount-point (and user-partition
446 (user-partition-mount-point user-partition))))
452 ,(or fs-type-name "")
456 ,(maybe-string-pad name 30))))
458 (define (partitions-descriptions partitions user-partitions)
459 "Return a list of strings describing all the partitions found on
460 DEVICE. METADATA partitions are not described. The strings are padded to the
461 right so that they can be displayed as a table."
463 (define (max-length-column lists column-index)
464 "Return the maximum length of the string at position COLUMN-INDEX in the
465 list of string lists LISTS."
469 (list-ref list column-index)))
472 (define (pad-descriptions descriptions)
473 "Return a padded version of the list of string lists DESCRIPTIONS. The
474 strings are padded to the length of the longer string in a same column, as
475 determined by MAX-LENGTH-COLUMN procedure."
476 (let* ((description-length (length (car descriptions)))
477 (paddings (map (lambda (index)
478 (max-length-column descriptions index))
479 (iota description-length))))
480 (map (lambda (description)
481 (map string-pad-right description paddings))
487 (let ((user-partition
488 (find-user-partition-by-parted-object user-partitions
490 (partition-description partition user-partition)))
492 (padded-descriptions (if (null? partitions)
494 (pad-descriptions descriptions))))
495 (map (cut string-join <> " ") padded-descriptions)))
497 (define (user-partition-description user-partition)
498 "Return a string describing the given USER-PARTITION record."
499 (let* ((partition (user-partition-parted-object user-partition))
500 (disk (partition-disk partition))
501 (disk-type (disk-disk-type disk))
502 (device (disk-device disk))
503 (has-name? (disk-type-check-feature
505 DISK-TYPE-FEATURE-PARTITION-NAME))
506 (has-extended? (disk-type-check-feature
508 DISK-TYPE-FEATURE-EXTENDED))
509 (name (user-partition-name user-partition))
510 (type (user-partition-type user-partition))
511 (type-name (symbol->string type))
512 (fs-type (user-partition-fs-type user-partition))
513 (fs-type-name (user-fs-type-name fs-type))
514 (bootable? (user-partition-bootable? user-partition))
515 (esp? (user-partition-esp? user-partition))
516 (need-formatting? (user-partition-need-formatting? user-partition))
517 (crypt-label (user-partition-crypt-label user-partition))
518 (size (user-partition-size user-partition))
519 (mount-point (user-partition-mount-point user-partition)))
521 `((name . ,(string-append "Name: " (or name "None"))))
523 ,@(if (and has-extended?
524 (freespace-partition? partition)
525 (not (eq? type 'logical)))
526 `((type . ,(string-append "Type: " type-name)))
528 ,@(if (eq? type 'extended)
530 `((fs-type . ,(string-append "Filesystem type: " fs-type-name))))
531 ,@(if (or (eq? type 'extended)
535 `((bootable . ,(string-append "Bootable flag: "
536 (if bootable? "On" "Off")))))
537 ,@(if (and (not has-extended?)
538 (not (eq? fs-type 'swap)))
539 `((esp? . ,(string-append "ESP flag: "
540 (if esp? "On" "Off"))))
542 ,@(if (freespace-partition? partition)
543 (let ((size-formatted
544 (or size (unit-format device
545 (partition-length partition)))))
546 `((size . ,(string-append "Size : " size-formatted))))
548 ,@(if (or (eq? type 'extended)
555 (format #f "Yes (label ~a)" crypt-label)
557 ,@(if (or (freespace-partition? partition)
561 . ,(string-append "Format the partition? : "
562 (if need-formatting? "Yes" "No")))))
563 ,@(if (or (eq? type 'extended)
567 . ,(string-append "Mount point : "
569 (and esp? (default-esp-mount-point))
574 ;; Partition table creation.
577 (define (mklabel device type-name)
578 "Create a partition table on DEVICE. TYPE-NAME is the type of the partition
579 table, \"msdos\" or \"gpt\"."
580 (let ((type (disk-type-get type-name)))
581 (disk-new-fresh device type)))
585 ;; Partition creation.
588 ;; The maximum count of primary partitions is exceeded.
589 (define-condition-type &max-primary-exceeded &condition
590 max-primary-exceeded?)
592 ;; It is not possible to create an extended partition.
593 (define-condition-type &extended-creation-error &condition
594 extended-creation-error?)
596 ;; It is not possible to create a logical partition.
597 (define-condition-type &logical-creation-error &condition
598 logical-creation-error?)
600 (define (can-create-primary? disk)
601 "Return #t if it is possible to create a primary partition on DISK, return
603 (let ((max-primary (disk-get-max-primary-partition-count disk)))
604 (find (lambda (number)
605 (not (disk-get-partition disk number)))
606 (iota max-primary 1))))
608 (define (can-create-extended? disk)
609 "Return #t if it is possible to create an extended partition on DISK, return
611 (let* ((disk-type (disk-disk-type disk))
612 (has-extended? (disk-type-check-feature
614 DISK-TYPE-FEATURE-EXTENDED)))
615 (and (can-create-primary? disk)
617 (not (disk-extended-partition disk)))))
619 (define (can-create-logical? disk)
620 "Return #t is it is possible to create a logical partition on DISK, return
622 (let* ((disk-type (disk-disk-type disk))
623 (has-extended? (disk-type-check-feature
625 DISK-TYPE-FEATURE-EXTENDED)))
627 (disk-extended-partition disk))))
629 (define (can-create-partition? user-part)
630 "Return #t if it is possible to create the given USER-PART record, return #f
632 (let* ((type (user-partition-type user-part))
633 (partition (user-partition-parted-object user-part))
634 (disk (partition-disk partition)))
637 (or (can-create-primary? disk)
639 (condition (&max-primary-exceeded)))))
641 (or (can-create-extended? disk)
643 (condition (&extended-creation-error)))))
645 (or (can-create-logical? disk)
647 (condition (&logical-creation-error))))))))
649 (define* (mkpart disk user-partition
650 #:key (previous-partition #f))
651 "Create the given USER-PARTITION on DISK. The PREVIOUS-PARTITION argument as
652 to be set to the partition preceding USER-PARTITION if any."
654 (define (parse-start-end start end)
655 "Parse start and end strings as positions on DEVICE expressed with a unit,
656 like '100GB' or '12.2KiB'. Return a list of 4 elements, the start sector, its
657 range (1 unit large area centered on start sector), the end sector and its
659 (let ((device (disk-device disk)))
662 (unit-parse start device))
663 (lambda (start-sector start-range)
666 (unit-parse end device))
667 (lambda (end-sector end-range)
668 (list start-sector start-range
669 end-sector end-range)))))))
671 (define* (extend-ranges! start-range end-range
673 "Try to extend START-RANGE by 1 MEBIBYTE to the right and END-RANGE by 1
674 MEBIBYTE to the left. This way, if the disk is aligned on 2048 sectors of
675 512KB (like frequently), we will have a chance for the
676 'optimal-align-constraint' to succeed. Do not extend ranges if that would
677 cause them to cross."
678 (let* ((device (disk-device disk))
679 (start-range-end (geometry-end start-range))
680 (end-range-start (geometry-start end-range))
681 (mebibyte-sector-size (/ MEBIBYTE-SIZE
682 (device-sector-size device)))
684 (+ start-range-end mebibyte-sector-size offset))
686 (- end-range-start mebibyte-sector-size offset)))
687 (when (< new-start-range-end new-end-range-start)
688 (geometry-set-end start-range new-start-range-end)
689 (geometry-set-start end-range new-end-range-start))))
691 (match (parse-start-end (user-partition-start user-partition)
692 (user-partition-end user-partition))
693 ((start-sector start-range end-sector end-range)
694 (let* ((prev-end (if previous-partition
695 (partition-end previous-partition)
697 (start-distance (- start-sector prev-end))
698 (type (user-partition-type user-partition))
699 ;; There should be at least 2 unallocated sectors in front of each
700 ;; logical partition, otherwise parted will fail badly:
701 ;; https://gparted.org/h2-fix-msdos-pt.php#apply-action-fail.
702 (start-offset (if previous-partition
705 (start-sector* (if (and (eq? type 'logical)
706 (< start-distance 3))
707 (+ start-sector start-offset)
709 ;; This is a hack. Parted almost always fails to create optimally
710 ;; aligned partitions (unless specifying percentages) because the
711 ;; default range of 1MB centered on the start sector is not enough when
712 ;; the optimal alignment is 2048 sectors of 512KB.
713 (extend-ranges! start-range end-range #:offset start-offset)
715 (let* ((device (disk-device disk))
716 (disk-type (disk-disk-type disk))
717 (length (device-length device))
718 (name (user-partition-name user-partition))
722 (user-partition-fs-type user-partition))))
723 (flags `(,@(if (user-partition-bootable? user-partition)
724 `(,PARTITION-FLAG-BOOT)
726 ,@(if (user-partition-esp? user-partition)
727 `(,PARTITION-FLAG-ESP)
729 ,@(if (user-partition-bios-grub? user-partition)
730 `(,PARTITION-FLAG-BIOS-GRUB)
732 (has-name? (disk-type-check-feature
734 DISK-TYPE-FEATURE-PARTITION-NAME))
735 (partition-type (partition-type->int type))
736 (partition (partition-new disk
737 #:type partition-type
738 #:filesystem-type filesystem-type
739 #:start start-sector*
741 (user-constraint (constraint-new
744 #:start-range start-range
745 #:end-range end-range
749 (device-get-optimal-aligned-constraint device))
750 (final-constraint (constraint-intersect user-constraint
752 (no-constraint (constraint-any device))
753 ;; Try to create a partition with an optimal alignment
754 ;; constraint. If it fails, fallback to creating a partition with
755 ;; no specific constraint.
757 (or (disk-add-partition disk partition final-constraint)
758 (disk-add-partition disk partition no-constraint))))
759 ;; Set the partition name if supported.
760 (when (and partition-ok? has-name? name)
761 (partition-set-name partition name))
763 ;; Set flags is required.
764 (for-each (lambda (flag)
765 (and (partition-is-flag-available? partition flag)
766 (partition-set-flag partition flag 1)))
770 (partition-set-system partition filesystem-type)
775 ;; Partition destruction.
778 (define (rmpart disk number)
779 "Remove the partition with the given NUMBER on DISK."
780 (let ((partition (disk-get-partition disk number)))
781 (disk-remove-partition* disk partition)))
785 ;; Auto partitionning.
788 (define* (create-adjacent-partitions! disk partitions
789 #:key (last-partition-end 0))
790 "Create the given PARTITIONS on DISK. LAST-PARTITION-END is the sector from
791 which we want to start creating partitions. The START and END of each created
792 partition are computed from its SIZE value and the position of the last
794 (let ((device (disk-device disk)))
795 (let loop ((partitions partitions)
796 (remaining-space (- (device-length device)
798 (start last-partition-end))
802 (let* ((size (user-partition-size partition))
803 (percentage-size (and (string? size)
804 (read-percentage size)))
805 (sector-size (device-sector-size device))
806 (partition-size (if percentage-size
808 (* (/ percentage-size 100)
811 (end-partition (min (- (device-length device) 1)
812 (nearest-exact-integer
813 (+ start partition-size 1))))
814 (name (user-partition-name partition))
815 (type (user-partition-type partition))
816 (fs-type (user-partition-fs-type partition))
817 (start-formatted (unit-format-custom device
820 (end-formatted (unit-format-custom device
823 (new-user-partition (user-partition
825 (start start-formatted)
826 (end end-formatted)))
828 (mkpart disk new-user-partition)))
830 (cons (user-partition
831 (inherit new-user-partition)
832 (file-name (partition-get-path new-partition))
833 (disk-file-name (device-path device))
834 (parted-object new-partition))
836 (if (eq? type 'extended)
839 (partition-length new-partition)))
840 (if (eq? type 'extended)
842 (+ (partition-end new-partition) 1))))
844 (format #f "Unable to create partition ~a~%" name)))))))))
846 (define (force-user-partitions-formatting user-partitions)
847 "Set the NEED-FORMATING? fields to #t on all <user-partition> records of
848 USER-PARTITIONS list and return the updated list."
852 (need-formatting? #t)))
855 (define* (auto-partition! disk
857 (scheme 'entire-root))
858 "Automatically create partitions on DISK. All the previous
859 partitions (except the ESP on a GPT disk, if present) are wiped. SCHEME is the
860 desired partitioning scheme. It can be 'entire-root or
861 'entire-root-home. 'entire-root will create a swap partition and a root
862 partition occupying all the remaining space. 'entire-root-home will create a
863 swap partition, a root partition and a home partition.
865 Return the complete list of partitions on DISK, including the ESP when it
867 (let* ((device (disk-device disk))
868 (disk-type (disk-disk-type disk))
869 (has-extended? (disk-type-check-feature
871 DISK-TYPE-FEATURE-EXTENDED))
872 (partitions (filter data-partition? (disk-partitions disk)))
873 (esp-partition (find-esp-partition partitions))
875 ;; https://wiki.archlinux.org/index.php/EFI_system_partition, the ESP
876 ;; size should be at least 550MiB.
877 (new-esp-size (nearest-exact-integer
878 (/ (* 550 MEBIBYTE-SIZE)
879 (device-sector-size device))))
880 (end-esp-partition (and esp-partition
881 (partition-end esp-partition)))
882 (non-boot-partitions (remove esp-partition? partitions))
883 (bios-grub-size (/ (* 3 MEBIBYTE-SIZE)
884 (device-sector-size device)))
885 (five-percent-disk (nearest-exact-integer
886 (* 0.05 (device-length device))))
887 (default-swap-size (nearest-exact-integer
888 (/ (* 4 GIGABYTE-SIZE)
889 (device-sector-size device))))
890 ;; Use a 4GB size for the swap if it represents less than 5% of the
891 ;; disk space. Otherwise, set the swap size to 5% of the disk space.
892 (swap-size (min default-swap-size five-percent-disk)))
895 ;; msdos - remove everything.
896 (disk-remove-all-partitions disk)
897 ;; gpt - remove everything but esp if it exists.
900 (and (data-partition? partition)
901 (disk-remove-partition* disk partition)))
902 non-boot-partitions))
904 (let* ((start-partition
905 (and (not has-extended?)
907 (if (efi-installation?)
912 (mount-point (default-esp-mount-point)))
917 (size bios-grub-size)))))
920 ((or (eq? scheme 'entire-root)
921 (eq? scheme 'entire-encrypted-root))
922 (let ((encrypted? (eq? scheme 'entire-encrypted-root)))
923 `(,@(if start-partition
933 (bootable? has-extended?)
934 (crypt-label (and encrypted? "cryptroot"))
936 (mount-point "/")))))
937 ((or (eq? scheme 'entire-root-home)
938 (eq? scheme 'entire-encrypted-root-home))
939 (let ((encrypted? (eq? scheme 'entire-encrypted-root-home)))
940 `(,@(if start-partition
945 (bootable? has-extended?)
946 (crypt-label (and encrypted? "cryptroot"))
957 (type (if has-extended?
963 (type (if has-extended?
967 (crypt-label (and encrypted? "crypthome"))
969 (mount-point "/home")))))))
970 (new-partitions* (force-user-partitions-formatting
972 (append (if esp-partition
973 (list (partition->user-partition esp-partition))
975 (create-adjacent-partitions! disk
978 (or end-esp-partition 0))))))
982 ;; Convert user-partitions.
985 ;; No root mount point found.
986 (define-condition-type &no-root-mount-point &condition
987 no-root-mount-point?)
989 (define (check-user-partitions user-partitions)
990 "Return #t if the USER-PARTITIONS lists contains one <user-partition> record
991 with a mount-point set to '/', raise &no-root-mount-point condition
994 (map user-partition-mount-point user-partitions)))
995 (or (member "/" mount-points)
997 (condition (&no-root-mount-point))))))
999 (define (set-user-partitions-file-name user-partitions)
1000 "Set the partition file-name of <user-partition> records in USER-PARTITIONS
1001 list and return the updated list."
1003 (let* ((partition (user-partition-parted-object p))
1004 (file-name (partition-get-path partition)))
1007 (file-name file-name))))
1010 (define-syntax-rule (with-null-output-ports exp ...)
1011 "Evaluate EXP with both the output port and the error port pointing to the
1013 (with-output-to-port (%make-void-port "w")
1015 (with-error-to-port (%make-void-port "w")
1016 (lambda () exp ...)))))
1018 (define (create-btrfs-file-system partition)
1019 "Create a btrfs file-system for PARTITION file-name."
1020 (with-null-output-ports
1021 (invoke "mkfs.btrfs" "-f" partition)))
1023 (define (create-ext4-file-system partition)
1024 "Create an ext4 file-system for PARTITION file-name."
1025 (with-null-output-ports
1026 (invoke "mkfs.ext4" "-F" partition)))
1028 (define (create-fat16-file-system partition)
1029 "Create a fat16 file-system for PARTITION file-name."
1030 (with-null-output-ports
1031 (invoke "mkfs.fat" "-F16" partition)))
1033 (define (create-fat32-file-system partition)
1034 "Create a fat32 file-system for PARTITION file-name."
1035 (with-null-output-ports
1036 (invoke "mkfs.fat" "-F32" partition)))
1038 (define (create-jfs-file-system partition)
1039 "Create a JFS file-system for PARTITION file-name."
1040 (with-null-output-ports
1041 (invoke "jfs_mkfs" "-f" partition)))
1043 (define (create-swap-partition partition)
1044 "Set up swap area on PARTITION file-name."
1045 (with-null-output-ports
1046 (invoke "mkswap" "-f" partition)))
1048 (define (call-with-luks-key-file password proc)
1049 "Write PASSWORD in a temporary file and pass it to PROC as argument."
1050 (call-with-temporary-output-file
1052 (put-string port password)
1056 (define (user-partition-upper-file-name user-partition)
1057 "Return the file-name of the virtual block device corresponding to
1058 USER-PARTITION if it is encrypted, or the plain file-name otherwise."
1059 (let ((crypt-label (user-partition-crypt-label user-partition))
1060 (file-name (user-partition-file-name user-partition)))
1062 (string-append "/dev/mapper/" crypt-label)
1065 (define (luks-format-and-open user-partition)
1066 "Format and open the encrypted partition pointed by USER-PARTITION."
1067 (let* ((file-name (user-partition-file-name user-partition))
1068 (label (user-partition-crypt-label user-partition))
1069 (password (user-partition-crypt-password user-partition)))
1070 (call-with-luks-key-file
1073 (syslog "formatting and opening LUKS entry ~s at ~s~%"
1075 (system* "cryptsetup" "-q" "luksFormat" file-name key-file)
1076 (system* "cryptsetup" "open" "--type" "luks"
1077 "--key-file" key-file file-name label)))))
1079 (define (luks-close user-partition)
1080 "Close the encrypted partition pointed by USER-PARTITION."
1081 (let ((label (user-partition-crypt-label user-partition)))
1082 (syslog "closing LUKS entry ~s~%" label)
1083 (system* "cryptsetup" "close" label)))
1085 (define (format-user-partitions user-partitions)
1086 "Format the <user-partition> records in USER-PARTITIONS list with
1087 NEED-FORMATING? field set to #t."
1089 (lambda (user-partition)
1090 (let* ((need-formatting?
1091 (user-partition-need-formatting? user-partition))
1092 (type (user-partition-type user-partition))
1093 (crypt-label (user-partition-crypt-label user-partition))
1094 (file-name (user-partition-upper-file-name user-partition))
1095 (fs-type (user-partition-fs-type user-partition)))
1097 (luks-format-and-open user-partition))
1101 (and need-formatting?
1102 (not (eq? type 'extended))
1103 (create-btrfs-file-system file-name)))
1105 (and need-formatting?
1106 (not (eq? type 'extended))
1107 (create-ext4-file-system file-name)))
1109 (and need-formatting?
1110 (not (eq? type 'extended))
1111 (create-fat16-file-system file-name)))
1113 (and need-formatting?
1114 (not (eq? type 'extended))
1115 (create-fat32-file-system file-name)))
1117 (and need-formatting?
1118 (not (eq? type 'extended))
1119 (create-jfs-file-system file-name)))
1121 (create-swap-partition file-name))
1123 ;; TODO: Add support for other file-system types.
1127 (define (sort-partitions user-partitions)
1128 "Sort USER-PARTITIONS by mount-points, so that the more nested mount-point
1129 comes last. This is useful to mount/umount partitions in a coherent order."
1130 (sort user-partitions
1132 (let ((mount-point-a (user-partition-mount-point a))
1133 (mount-point-b (user-partition-mount-point b)))
1134 (string-prefix? mount-point-a mount-point-b)))))
1136 (define (mount-user-partitions user-partitions)
1137 "Mount the <user-partition> records in USER-PARTITIONS list on their
1138 respective mount-points."
1139 (let* ((mount-partitions (filter user-partition-mount-point user-partitions))
1140 (sorted-partitions (sort-partitions mount-partitions)))
1141 (for-each (lambda (user-partition)
1143 (user-partition-mount-point user-partition))
1145 (string-append (%installer-target-dir)
1148 (user-partition-fs-type user-partition))
1150 (user-partition-crypt-label user-partition))
1152 (user-fs-type->mount-type fs-type))
1154 (user-partition-upper-file-name user-partition)))
1156 (syslog "mounting ~s on ~s~%" file-name target)
1157 (mount file-name target mount-type)))
1158 sorted-partitions)))
1160 (define (umount-user-partitions user-partitions)
1161 "Unmount all the <user-partition> records in USER-PARTITIONS list."
1162 (let* ((mount-partitions (filter user-partition-mount-point user-partitions))
1163 (sorted-partitions (sort-partitions mount-partitions)))
1164 (for-each (lambda (user-partition)
1166 (user-partition-mount-point user-partition))
1168 (user-partition-crypt-label user-partition))
1170 (string-append (%installer-target-dir)
1172 (syslog "unmounting ~s~%" target)
1175 (luks-close user-partition))))
1176 (reverse sorted-partitions))))
1178 (define (find-swap-user-partitions user-partitions)
1179 "Return the subset of <user-partition> records in USER-PARTITIONS list with
1180 the FS-TYPE field set to 'swap, return the empty list if none found."
1181 (filter (lambda (user-partition)
1182 (let ((fs-type (user-partition-fs-type user-partition)))
1183 (eq? fs-type 'swap)))
1186 (define (start-swapping user-partitions)
1187 "Start swaping on <user-partition> records with FS-TYPE equal to 'swap."
1188 (let* ((swap-user-partitions (find-swap-user-partitions user-partitions))
1189 (swap-devices (map user-partition-file-name swap-user-partitions)))
1190 (for-each swapon swap-devices)))
1192 (define (stop-swapping user-partitions)
1193 "Stop swaping on <user-partition> records with FS-TYPE equal to 'swap."
1194 (let* ((swap-user-partitions (find-swap-user-partitions user-partitions))
1195 (swap-devices (map user-partition-file-name swap-user-partitions)))
1196 (for-each swapoff swap-devices)))
1198 (define-syntax-rule (with-mounted-partitions user-partitions exp ...)
1199 "Mount USER-PARTITIONS and start swapping within the dynamic extent of EXP."
1202 (mount-user-partitions user-partitions)
1203 (start-swapping user-partitions))
1207 (umount-user-partitions user-partitions)
1208 (stop-swapping user-partitions)
1211 (define (user-partition->file-system user-partition)
1212 "Convert the given USER-PARTITION record in a FILE-SYSTEM record from
1213 (gnu system file-systems) module and return it."
1214 (let* ((mount-point (user-partition-mount-point user-partition))
1215 (fs-type (user-partition-fs-type user-partition))
1216 (crypt-label (user-partition-crypt-label user-partition))
1217 (mount-type (user-fs-type->mount-type fs-type))
1218 (file-name (user-partition-file-name user-partition))
1219 (upper-file-name (user-partition-upper-file-name user-partition))
1220 ;; Only compute uuid if partition is not encrypted.
1221 (uuid (or crypt-label
1222 (uuid->string (read-partition-uuid file-name) fs-type))))
1224 (mount-point ,mount-point)
1225 (device ,@(if crypt-label
1227 `((uuid ,uuid (quote ,fs-type)))))
1230 '((dependencies mapped-devices))
1233 (define (user-partitions->file-systems user-partitions)
1234 "Convert the given USER-PARTITIONS list of <user-partition> records into a
1235 list of <file-system> records."
1237 (lambda (user-partition)
1239 (user-partition-mount-point user-partition)))
1241 (user-partition->file-system user-partition))))
1244 (define (user-partition->mapped-device user-partition)
1245 "Convert the given USER-PARTITION record into a MAPPED-DEVICE record
1246 from (gnu system mapped-devices) and return it."
1247 (let ((label (user-partition-crypt-label user-partition))
1248 (file-name (user-partition-file-name user-partition)))
1250 (source (uuid ,(uuid->string
1251 (read-luks-partition-uuid file-name)
1254 (type luks-device-mapping))))
1256 (define (root-user-partition? partition)
1257 "Return true if PARTITION is the root partition."
1258 (let ((mount-point (user-partition-mount-point partition)))
1260 (string=? mount-point "/"))))
1262 (define (bootloader-configuration user-partitions)
1263 "Return the bootloader configuration field for USER-PARTITIONS."
1264 (let* ((root-partition (find root-user-partition?
1266 (root-partition-disk (user-partition-disk-file-name root-partition)))
1267 `((bootloader-configuration
1268 ,@(if (efi-installation?)
1269 `((bootloader grub-efi-bootloader)
1270 (target ,(default-esp-mount-point)))
1271 `((bootloader grub-bootloader)
1272 (target ,root-partition-disk)))
1274 ;; XXX: Assume we defined the 'keyboard-layout' field of
1275 ;; <operating-system> right above.
1276 (keyboard-layout keyboard-layout)))))
1278 (define (user-partition-missing-modules user-partitions)
1279 "Return the list of kernel modules missing from the default set of kernel
1280 modules to access USER-PARTITIONS."
1281 (let ((devices (filter user-partition-crypt-label user-partitions))
1282 (root (find root-user-partition? user-partitions)))
1284 (append-map (lambda (device)
1285 (catch 'system-error
1287 (missing-modules device %base-initrd-modules))
1290 (map user-partition-file-name
1291 (cons root devices)))))))
1293 (define (initrd-configuration user-partitions)
1294 "Return an 'initrd-modules' field with everything needed for
1295 USER-PARTITIONS, or return nothing."
1296 (match (user-partition-missing-modules user-partitions)
1300 `((initrd-modules (append ',modules
1301 %base-initrd-modules))))))
1303 (define (user-partitions->configuration user-partitions)
1304 "Return the configuration field for USER-PARTITIONS."
1305 (let* ((swap-user-partitions (find-swap-user-partitions user-partitions))
1306 (swap-devices (map user-partition-file-name swap-user-partitions))
1307 (encrypted-partitions
1308 (filter user-partition-crypt-label user-partitions)))
1309 `((bootloader ,@(bootloader-configuration user-partitions))
1310 ,@(initrd-configuration user-partitions)
1311 ,@(if (null? swap-devices)
1313 `((swap-devices (list ,@swap-devices))))
1314 ,@(if (null? encrypted-partitions)
1317 (list ,@(map user-partition->mapped-device
1318 encrypted-partitions)))))
1319 (file-systems (cons*
1320 ,@(user-partitions->file-systems user-partitions)
1321 %base-file-systems)))))
1328 (define (init-parted)
1329 "Initialize libparted support."
1330 (probe-all-devices!)
1331 (exception-set-handler (lambda (exception)
1332 EXCEPTION-OPTION-UNHANDLED)))
1334 (define (free-parted devices)
1335 "Deallocate memory used for DEVICES in parted, force sync them and wait for
1336 the devices not to be used before returning."
1337 ;; XXX: Formatting and further operations on disk partition table may fail
1338 ;; because the partition table changes are not synced, or because the device
1339 ;; is still in use, even if parted should have finished editing
1340 ;; partitions. This is not well understood, but syncing devices and waiting
1341 ;; them to stop returning EBUSY to BLKRRPART ioctl seems to be enough. The
1342 ;; same kind of issue is described here:
1343 ;; https://mail.gnome.org/archives/commits-list/2013-March/msg18423.html.
1344 (let ((device-file-names (map device-path devices)))
1345 (for-each force-device-sync devices)
1346 (for-each (lambda (file-name)
1347 (let ((in-use? (with-delay-device-in-use? file-name)))
1350 (format #f (G_ "Device ~a is still in use.")
1352 device-file-names)))