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 format)
40 #:use-module (ice-9 match)
41 #:use-module (ice-9 regex)
42 #:use-module (rnrs io ports)
43 #:use-module (srfi srfi-1)
44 #:use-module (srfi srfi-19)
45 #:use-module (srfi srfi-26)
46 #:use-module (srfi srfi-34)
47 #:use-module (srfi srfi-35)
48 #:export (<user-partition>
54 user-partition-file-name
55 user-partition-disk-file-name
56 user-partition-crypt-label
57 user-partition-crypt-password
58 user-partition-fs-type
59 user-partition-bootable?
61 user-partition-bios-grub?
65 user-partition-mount-point
66 user-partition-need-formatting?
67 user-partition-parted-object
70 small-freespace-partition?
74 default-esp-mount-point
76 with-delay-device-in-use?
81 partition-filesystem-user-type
83 partition->user-partition
84 create-special-user-partitions
85 find-user-partition-by-parted-object
88 partition-end-formatted
89 partition-print-number
91 partitions-descriptions
92 user-partition-description
96 &extended-creation-error
97 extended-creation-error?
98 &logical-creation-error
99 logical-creation-error?
101 can-create-partition?
111 check-user-partitions
112 set-user-partitions-file-name
113 format-user-partitions
114 mount-user-partitions
115 umount-user-partitions
116 with-mounted-partitions
117 user-partitions->file-systems
118 user-partitions->configuration
125 ;;; Partition record.
128 (define-record-type* <user-partition>
129 user-partition make-user-partition
131 (name user-partition-name ;string
133 (type user-partition-type
134 (default 'normal)) ; 'normal | 'logical | 'extended
135 (file-name user-partition-file-name
137 (disk-file-name user-partition-disk-file-name
139 (crypt-label user-partition-crypt-label
141 (crypt-password user-partition-crypt-password
143 (fs-type user-partition-fs-type
145 (bootable? user-partition-bootable?
147 (esp? user-partition-esp?
149 (bios-grub? user-partition-bios-grub?
151 (size user-partition-size
153 (start user-partition-start ;start as string (e.g. '11MB')
155 (end user-partition-end ;same as start
157 (mount-point user-partition-mount-point ;string
159 (need-formatting? user-partition-need-formatting? ; boolean
161 (parted-object user-partition-parted-object ; <partition> from parted
169 (define (find-esp-partition partitions)
170 "Find and return the ESP partition among PARTITIONS."
171 (find esp-partition? partitions))
173 (define* (small-freespace-partition? device
175 #:key (max-size MEBIBYTE-SIZE))
176 "Return #t is PARTITION is a free-space partition with less a size strictly
177 inferior to MAX-SIZE, #f otherwise."
178 (let ((size (partition-length partition))
179 (max-sector-size (/ max-size
180 (device-sector-size device))))
181 (< size max-sector-size)))
183 (define (partition-user-type partition)
184 "Return the type of PARTITION, to be stored in the TYPE field of
185 <user-partition> record. It can be 'normal, 'extended or 'logical."
186 (cond ((normal-partition? partition)
188 ((extended-partition? partition)
190 ((logical-partition? partition)
194 (define (esp-partition? partition)
195 "Return #t if partition has the ESP flag, return #f otherwise."
196 (let* ((disk (partition-disk partition))
197 (disk-type (disk-disk-type disk)))
198 (and (data-partition? partition)
199 (partition-is-flag-available? partition PARTITION-FLAG-ESP)
200 (partition-get-flag partition PARTITION-FLAG-ESP))))
202 (define (boot-partition? partition)
203 "Return #t if partition has the boot flag, return #f otherwise."
204 (and (data-partition? partition)
205 (partition-is-flag-available? partition PARTITION-FLAG-BOOT)
206 (partition-get-flag partition PARTITION-FLAG-BOOT)))
209 ;; The default mount point for ESP partitions.
210 (define default-esp-mount-point
211 (make-parameter "/boot/efi"))
213 (define (efi-installation?)
214 "Return #t if an EFI installation should be performed, #f otherwise."
215 (file-exists? "/sys/firmware/efi"))
217 (define (user-fs-type-name fs-type)
218 "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."
238 (define (partition-filesystem-user-type partition)
239 "Return the filesystem type of PARTITION, to be stored in the FS-TYPE field
240 of <user-partition> record."
241 (let ((fs-type (partition-fs-type partition)))
243 (let ((name (filesystem-type-name fs-type)))
245 ((string=? name "ext4") 'ext4)
246 ((string=? name "btrfs") 'btrfs)
247 ((string=? name "fat16") 'fat16)
248 ((string=? name "fat32") 'fat32)
249 ((string=? name "jfs") 'jfs)
250 ((string=? name "ntfs") 'ntfs)
251 ((or (string=? name "swsusp")
252 (string=? name "linux-swap(v0)")
253 (string=? name "linux-swap(v1)"))
256 (error (format #f "Unhandled ~a fs-type~%" name))))))))
258 (define (partition-get-flags partition)
259 "Return the list of flags supported by the given PARTITION."
260 (filter-map (lambda (flag)
261 (and (partition-get-flag partition flag)
263 (partition-flags partition)))
265 (define (partition->user-partition partition)
266 "Convert PARTITION into a <user-partition> record and return it."
267 (let* ((disk (partition-disk partition))
268 (device (disk-device disk))
269 (disk-type (disk-disk-type disk))
270 (has-name? (disk-type-check-feature
272 DISK-TYPE-FEATURE-PARTITION-NAME))
274 (data-partition? partition)
275 (partition-get-name partition))))
278 (not (string=? name "")))
280 (type (or (partition-user-type partition)
282 (file-name (partition-get-path partition))
283 (disk-file-name (device-path device))
284 (fs-type (or (partition-filesystem-user-type partition)
286 (mount-point (and (esp-partition? partition)
287 (default-esp-mount-point)))
288 (bootable? (boot-partition? partition))
289 (esp? (esp-partition? partition))
290 (parted-object partition))))
292 (define (create-special-user-partitions partitions)
293 "Return a list with a <user-partition> record describing the ESP partition
294 found in PARTITIONS, if any."
295 (filter-map (lambda (partition)
296 (and (esp-partition? partition)
297 (partition->user-partition partition)))
300 (define (find-user-partition-by-parted-object user-partitions
302 "Find and return the <user-partition> record in USER-PARTITIONS list which
303 PARTED-OBJECT field equals PARTITION, return #f if not found."
304 (find (lambda (user-partition)
305 (equal? (user-partition-parted-object user-partition)
314 (define (with-delay-device-in-use? file-name)
315 "Call DEVICE-IN-USE? with a few retries, as the first re-read will often
316 fail. See rereadpt function in wipefs.c of util-linux for an explanation."
317 ;; Kernel always return EINVAL for BLKRRPART on loopdevices.
318 (and (not (string-match "/dev/loop*" file-name))
321 (let ((in-use? (device-in-use? file-name)))
322 (if (and in-use? (> try 0))
326 (define* (force-device-sync device)
327 "Force a flushing of the given DEVICE."
330 (device-close device))
332 (define (remove-logical-devices)
333 "Remove all active logical devices."
334 (with-null-output-ports
335 (invoke "dmsetup" "remove_all")))
337 (define (non-install-devices)
338 "Return all the available devices, except the busy one, allegedly the
339 install device. DEVICE-IS-BUSY? is a parted call, checking if the device is
341 ;; FIXME: The install image uses an overlayfs so the install device does not
342 ;; appear as mounted and won't be considered as busy.
343 (remove (lambda (device)
344 (let ((file-name (device-path device)))
345 (device-is-busy? device)))
350 ;; Disk and partition printing.
353 (define* (device-description device #:optional disk)
354 "Return a string describing the given DEVICE."
355 (let* ((type (device-type device))
356 (file-name (device-path device))
357 (model (device-model device))
358 (type-str (device-type->string type))
360 (disk-disk-type disk)
361 (disk-probe device)))
362 (length (device-length device))
363 (sector-size (device-sector-size device))
364 (end (unit-format-custom-byte device
365 (* length sector-size)
368 `(,@(if (string=? model "")
370 `(,model ,(string-append "(" type-str ")")))
374 `(,(disk-type-name disk-type))
378 (define (partition-end-formatted device partition)
379 "Return as a string the end of PARTITION with the relevant unit."
383 (* (+ (partition-end partition) 1)
384 (device-sector-size device))
387 (define (partition-print-number partition)
388 "Convert the given partition NUMBER to string."
389 (let ((number (partition-number partition)))
390 (number->string number)))
392 (define (partition-description partition user-partition)
393 "Return a string describing the given PARTITION, located on the DISK of
396 (define (partition-print-type partition)
397 "Return the type of PARTITION as a string."
398 (if (freespace-partition? partition)
400 (let ((type (partition-type partition)))
403 (symbol->string type-symbol))))))
405 (define (partition-print-flags partition)
406 "Return the flags of PARTITION as a string of comma separated flags."
410 (and (partition-get-flag partition flag)
411 (partition-flag-get-name flag)))
412 (partition-flags partition))
415 (define (maybe-string-pad string length)
416 "Returned a string formatted by padding STRING of LENGTH characters to the
417 right. If STRING is #f use an empty string."
418 (if (and string (not (string=? string "")))
419 (string-pad-right string length)
422 (let* ((disk (partition-disk partition))
423 (device (disk-device disk))
424 (disk-type (disk-disk-type disk))
425 (has-name? (disk-type-check-feature
427 DISK-TYPE-FEATURE-PARTITION-NAME))
428 (has-extended? (disk-type-check-feature
430 DISK-TYPE-FEATURE-EXTENDED))
431 (part-type (partition-print-type partition))
432 (number (and (not (freespace-partition? partition))
433 (partition-print-number partition)))
435 (if (freespace-partition? partition)
437 (partition-get-name partition))))
438 (start (unit-format device
439 (partition-start partition)))
440 (end (partition-end-formatted device partition))
441 (size (unit-format device (partition-length partition)))
442 (fs-type (partition-fs-type partition))
443 (fs-type-name (and fs-type
444 (filesystem-type-name fs-type)))
445 (crypt-label (and user-partition
446 (user-partition-crypt-label user-partition)))
447 (flags (and (not (freespace-partition? partition))
448 (partition-print-flags partition)))
449 (mount-point (and user-partition
450 (user-partition-mount-point user-partition))))
456 ,(or fs-type-name "")
460 ,(maybe-string-pad name 30))))
462 (define (partitions-descriptions partitions user-partitions)
463 "Return a list of strings describing all the partitions found on
464 DEVICE. METADATA partitions are not described. The strings are padded to the
465 right so that they can be displayed as a table."
467 (define (max-length-column lists column-index)
468 "Return the maximum length of the string at position COLUMN-INDEX in the
469 list of string lists LISTS."
473 (list-ref list column-index)))
476 (define (pad-descriptions descriptions)
477 "Return a padded version of the list of string lists DESCRIPTIONS. The
478 strings are padded to the length of the longer string in a same column, as
479 determined by MAX-LENGTH-COLUMN procedure."
480 (let* ((description-length (length (car descriptions)))
481 (paddings (map (lambda (index)
482 (max-length-column descriptions index))
483 (iota description-length))))
484 (map (lambda (description)
485 (map string-pad-right description paddings))
491 (let ((user-partition
492 (find-user-partition-by-parted-object user-partitions
494 (partition-description partition user-partition)))
496 (padded-descriptions (if (null? partitions)
498 (pad-descriptions descriptions))))
499 (map (cut string-join <> " ") padded-descriptions)))
501 (define (user-partition-description user-partition)
502 "Return a string describing the given USER-PARTITION record."
503 (let* ((partition (user-partition-parted-object user-partition))
504 (disk (partition-disk partition))
505 (disk-type (disk-disk-type disk))
506 (device (disk-device disk))
507 (has-name? (disk-type-check-feature
509 DISK-TYPE-FEATURE-PARTITION-NAME))
510 (has-extended? (disk-type-check-feature
512 DISK-TYPE-FEATURE-EXTENDED))
513 (name (user-partition-name user-partition))
514 (type (user-partition-type user-partition))
515 (type-name (symbol->string type))
516 (fs-type (user-partition-fs-type user-partition))
517 (fs-type-name (user-fs-type-name fs-type))
518 (bootable? (user-partition-bootable? user-partition))
519 (esp? (user-partition-esp? user-partition))
520 (need-formatting? (user-partition-need-formatting? user-partition))
521 (crypt-label (user-partition-crypt-label user-partition))
522 (size (user-partition-size user-partition))
523 (mount-point (user-partition-mount-point user-partition)))
525 `((name . ,(format #f (G_ "Name: ~a")
526 (or name (G_ "None")))))
528 ,@(if (and has-extended?
529 (freespace-partition? partition)
530 (not (eq? type 'logical)))
531 `((type . ,(format #f (G_ "Type: ~a") type-name)))
533 ,@(if (eq? type 'extended)
535 `((fs-type . ,(format #f (G_ "File system type: ~a")
537 ,@(if (or (eq? type 'extended)
541 `((bootable . ,(format #f (G_ "Bootable flag: ~:[off~;on~]")
543 ,@(if (and (not has-extended?)
544 (not (eq? fs-type 'swap)))
545 `((esp? . ,(format #f (G_ "ESP flag: ~:[off~;on~]") esp?)))
547 ,@(if (freespace-partition? partition)
548 (let ((size-formatted
549 (or size (unit-format device ;XXX: i18n
550 (partition-length partition)))))
551 `((size . ,(format #f (G_ "Size: ~a") size-formatted))))
553 ,@(if (or (eq? type 'extended)
557 . ,(format #f (G_ "Encryption: ~:[No~a~;Yes (label '~a')~]")
558 crypt-label (or crypt-label "")))))
559 ,@(if (or (freespace-partition? partition)
563 . ,(format #f (G_ "Format the partition? ~:[No~;Yes~]")
565 ,@(if (or (eq? type 'extended)
569 . ,(format #f (G_ "Mount point: ~a")
571 (and esp? (default-esp-mount-point))
576 ;; Partition table creation.
579 (define (mklabel device type-name)
580 "Create a partition table on DEVICE. TYPE-NAME is the type of the partition
581 table, \"msdos\" or \"gpt\"."
582 (let ((type (disk-type-get type-name)))
583 (disk-new-fresh device type)))
587 ;; Partition creation.
590 ;; The maximum count of primary partitions is exceeded.
591 (define-condition-type &max-primary-exceeded &condition
592 max-primary-exceeded?)
594 ;; It is not possible to create an extended partition.
595 (define-condition-type &extended-creation-error &condition
596 extended-creation-error?)
598 ;; It is not possible to create a logical partition.
599 (define-condition-type &logical-creation-error &condition
600 logical-creation-error?)
602 (define (can-create-primary? disk)
603 "Return #t if it is possible to create a primary partition on DISK, return
605 (let ((max-primary (disk-get-max-primary-partition-count disk)))
606 (find (lambda (number)
607 (not (disk-get-partition disk number)))
608 (iota max-primary 1))))
610 (define (can-create-extended? disk)
611 "Return #t if it is possible to create an extended partition on DISK, return
613 (let* ((disk-type (disk-disk-type disk))
614 (has-extended? (disk-type-check-feature
616 DISK-TYPE-FEATURE-EXTENDED)))
617 (and (can-create-primary? disk)
619 (not (disk-extended-partition disk)))))
621 (define (can-create-logical? disk)
622 "Return #t is it is possible to create a logical partition on DISK, return
624 (let* ((disk-type (disk-disk-type disk))
625 (has-extended? (disk-type-check-feature
627 DISK-TYPE-FEATURE-EXTENDED)))
629 (disk-extended-partition disk))))
631 (define (can-create-partition? user-part)
632 "Return #t if it is possible to create the given USER-PART record, return #f
634 (let* ((type (user-partition-type user-part))
635 (partition (user-partition-parted-object user-part))
636 (disk (partition-disk partition)))
639 (or (can-create-primary? disk)
641 (condition (&max-primary-exceeded)))))
643 (or (can-create-extended? disk)
645 (condition (&extended-creation-error)))))
647 (or (can-create-logical? disk)
649 (condition (&logical-creation-error))))))))
651 (define* (mkpart disk user-partition
652 #:key (previous-partition #f))
653 "Create the given USER-PARTITION on DISK. The PREVIOUS-PARTITION argument as
654 to be set to the partition preceding USER-PARTITION if any."
656 (define (parse-start-end start end)
657 "Parse start and end strings as positions on DEVICE expressed with a unit,
658 like '100GB' or '12.2KiB'. Return a list of 4 elements, the start sector, its
659 range (1 unit large area centered on start sector), the end sector and its
661 (let ((device (disk-device disk)))
664 (unit-parse start device))
665 (lambda (start-sector start-range)
668 (unit-parse end device))
669 (lambda (end-sector end-range)
670 (list start-sector start-range
671 end-sector end-range)))))))
673 (define* (extend-ranges! start-range end-range
675 "Try to extend START-RANGE by 1 MEBIBYTE to the right and END-RANGE by 1
676 MEBIBYTE to the left. This way, if the disk is aligned on 2048 sectors of
677 512KB (like frequently), we will have a chance for the
678 'optimal-align-constraint' to succeed. Do not extend ranges if that would
679 cause them to cross."
680 (let* ((device (disk-device disk))
681 (start-range-end (geometry-end start-range))
682 (end-range-start (geometry-start end-range))
683 (mebibyte-sector-size (/ MEBIBYTE-SIZE
684 (device-sector-size device)))
686 (+ start-range-end mebibyte-sector-size offset))
688 (- end-range-start mebibyte-sector-size offset)))
689 (when (< new-start-range-end new-end-range-start)
690 (geometry-set-end start-range new-start-range-end)
691 (geometry-set-start end-range new-end-range-start))))
693 (match (parse-start-end (user-partition-start user-partition)
694 (user-partition-end user-partition))
695 ((start-sector start-range end-sector end-range)
696 (let* ((prev-end (if previous-partition
697 (partition-end previous-partition)
699 (start-distance (- start-sector prev-end))
700 (type (user-partition-type user-partition))
701 ;; There should be at least 2 unallocated sectors in front of each
702 ;; logical partition, otherwise parted will fail badly:
703 ;; https://gparted.org/h2-fix-msdos-pt.php#apply-action-fail.
704 (start-offset (if previous-partition
707 (start-sector* (if (and (eq? type 'logical)
708 (< start-distance 3))
709 (+ start-sector start-offset)
711 ;; This is a hack. Parted almost always fails to create optimally
712 ;; aligned partitions (unless specifying percentages) because the
713 ;; default range of 1MB centered on the start sector is not enough when
714 ;; the optimal alignment is 2048 sectors of 512KB.
715 (extend-ranges! start-range end-range #:offset start-offset)
717 (let* ((device (disk-device disk))
718 (disk-type (disk-disk-type disk))
719 (length (device-length device))
720 (name (user-partition-name user-partition))
724 (user-partition-fs-type user-partition))))
725 (flags `(,@(if (user-partition-bootable? user-partition)
726 `(,PARTITION-FLAG-BOOT)
728 ,@(if (user-partition-esp? user-partition)
729 `(,PARTITION-FLAG-ESP)
731 ,@(if (user-partition-bios-grub? user-partition)
732 `(,PARTITION-FLAG-BIOS-GRUB)
734 (has-name? (disk-type-check-feature
736 DISK-TYPE-FEATURE-PARTITION-NAME))
737 (partition-type (partition-type->int type))
738 (partition (partition-new disk
739 #:type partition-type
740 #:filesystem-type filesystem-type
741 #:start start-sector*
743 (user-constraint (constraint-new
746 #:start-range start-range
747 #:end-range end-range
751 (device-get-optimal-aligned-constraint device))
752 (final-constraint (constraint-intersect user-constraint
754 (no-constraint (constraint-any device))
755 ;; Try to create a partition with an optimal alignment
756 ;; constraint. If it fails, fallback to creating a partition
757 ;; with no specific constraint.
758 (partition-constraint-ok?
759 (disk-add-partition disk partition final-constraint))
760 (partition-no-contraint-ok?
761 (or partition-constraint-ok?
762 (disk-add-partition disk partition no-constraint)))
764 (or partition-constraint-ok? partition-no-contraint-ok?)))
765 (syslog "Creating partition:
767 ~/filesystem-type: ~a
770 ~/start-range: [~a, ~a]
771 ~/end-range: [~a, ~a]
776 (filesystem-type-name filesystem-type)
779 (geometry-start start-range) (geometry-end start-range)
780 (geometry-start end-range) (geometry-end end-range)
781 partition-constraint-ok?
782 partition-no-contraint-ok?)
783 ;; Set the partition name if supported.
784 (when (and partition-ok? has-name? name)
785 (partition-set-name partition name))
787 ;; Set flags is required.
788 (for-each (lambda (flag)
789 (and (partition-is-flag-available? partition flag)
790 (partition-set-flag partition flag 1)))
794 (partition-set-system partition filesystem-type)
799 ;; Partition destruction.
802 (define (rmpart disk number)
803 "Remove the partition with the given NUMBER on DISK."
804 (let ((partition (disk-get-partition disk number)))
805 (disk-remove-partition* disk partition)))
809 ;; Auto partitionning.
812 (define* (create-adjacent-partitions! disk partitions
813 #:key (last-partition-end 0))
814 "Create the given PARTITIONS on DISK. LAST-PARTITION-END is the sector from
815 which we want to start creating partitions. The START and END of each created
816 partition are computed from its SIZE value and the position of the last
818 (let ((device (disk-device disk)))
819 (let loop ((partitions partitions)
820 (remaining-space (- (device-length device)
822 (start last-partition-end))
826 (let* ((size (user-partition-size partition))
827 (percentage-size (and (string? size)
828 (read-percentage size)))
829 (sector-size (device-sector-size device))
830 (partition-size (if percentage-size
832 (* (/ percentage-size 100)
835 (end-partition (min (- (device-length device) 1)
836 (nearest-exact-integer
837 (+ start partition-size 1))))
838 (name (user-partition-name partition))
839 (type (user-partition-type partition))
840 (fs-type (user-partition-fs-type partition))
841 (start-formatted (unit-format-custom device
844 (end-formatted (unit-format-custom device
847 (new-user-partition (user-partition
849 (start start-formatted)
850 (end end-formatted)))
852 (mkpart disk new-user-partition)))
854 (cons (user-partition
855 (inherit new-user-partition)
856 (file-name (partition-get-path new-partition))
857 (disk-file-name (device-path device))
858 (parted-object new-partition))
860 (if (eq? type 'extended)
863 (partition-length new-partition)))
864 (if (eq? type 'extended)
866 (+ (partition-end new-partition) 1))))
868 (format #f "Unable to create partition ~a~%" name)))))))))
870 (define (force-user-partitions-formatting user-partitions)
871 "Set the NEED-FORMATING? fields to #t on all <user-partition> records of
872 USER-PARTITIONS list and return the updated list."
876 (need-formatting? #t)))
879 (define* (auto-partition! disk
881 (scheme 'entire-root))
882 "Automatically create partitions on DISK. All the previous
883 partitions (except the ESP on a GPT disk, if present) are wiped. SCHEME is the
884 desired partitioning scheme. It can be 'entire-root or
885 'entire-root-home. 'entire-root will create a swap partition and a root
886 partition occupying all the remaining space. 'entire-root-home will create a
887 swap partition, a root partition and a home partition.
889 Return the complete list of partitions on DISK, including the ESP when it
891 (let* ((device (disk-device disk))
892 (disk-type (disk-disk-type disk))
893 (has-extended? (disk-type-check-feature
895 DISK-TYPE-FEATURE-EXTENDED))
896 (partitions (filter data-partition? (disk-partitions disk)))
897 (esp-partition (find-esp-partition partitions))
899 ;; https://wiki.archlinux.org/index.php/EFI_system_partition, the ESP
900 ;; size should be at least 550MiB.
901 (new-esp-size (nearest-exact-integer
902 (/ (* 550 MEBIBYTE-SIZE)
903 (device-sector-size device))))
904 (end-esp-partition (and esp-partition
905 (partition-end esp-partition)))
906 (non-boot-partitions (remove esp-partition? partitions))
907 (bios-grub-size (/ (* 3 MEBIBYTE-SIZE)
908 (device-sector-size device)))
909 (five-percent-disk (nearest-exact-integer
910 (* 0.05 (device-length device))))
911 (default-swap-size (nearest-exact-integer
912 (/ (* 4 GIGABYTE-SIZE)
913 (device-sector-size device))))
914 ;; Use a 4GB size for the swap if it represents less than 5% of the
915 ;; disk space. Otherwise, set the swap size to 5% of the disk space.
916 (swap-size (min default-swap-size five-percent-disk)))
918 ;; Remove everything but esp if it exists.
921 (and (data-partition? partition)
922 (disk-remove-partition* disk partition)))
925 (let* ((start-partition
926 (if (efi-installation?)
927 (and (not esp-partition)
932 (mount-point (default-esp-mount-point))))
937 (size bios-grub-size))))
940 ((or (eq? scheme 'entire-root)
941 (eq? scheme 'entire-encrypted-root))
942 (let ((encrypted? (eq? scheme 'entire-encrypted-root)))
943 `(,@(if start-partition
953 (bootable? has-extended?)
954 (crypt-label (and encrypted? "cryptroot"))
956 (mount-point "/")))))
957 ((or (eq? scheme 'entire-root-home)
958 (eq? scheme 'entire-encrypted-root-home))
959 (let ((encrypted? (eq? scheme 'entire-encrypted-root-home)))
960 `(,@(if start-partition
965 (bootable? has-extended?)
966 (crypt-label (and encrypted? "cryptroot"))
977 (type (if has-extended?
983 (type (if has-extended?
987 (crypt-label (and encrypted? "crypthome"))
989 (mount-point "/home")))))))
990 (new-partitions* (force-user-partitions-formatting
992 (append (if esp-partition
993 (list (partition->user-partition esp-partition))
995 (create-adjacent-partitions! disk
998 (or end-esp-partition 0))))))
1002 ;; Convert user-partitions.
1005 ;; No root mount point found.
1006 (define-condition-type &no-root-mount-point &condition
1007 no-root-mount-point?)
1009 (define (check-user-partitions user-partitions)
1010 "Return #t if the USER-PARTITIONS lists contains one <user-partition> record
1011 with a mount-point set to '/', raise &no-root-mount-point condition
1014 (map user-partition-mount-point user-partitions)))
1015 (or (member "/" mount-points)
1017 (condition (&no-root-mount-point))))))
1019 (define (set-user-partitions-file-name user-partitions)
1020 "Set the partition file-name of <user-partition> records in USER-PARTITIONS
1021 list and return the updated list."
1023 (let* ((partition (user-partition-parted-object p))
1024 (file-name (partition-get-path partition)))
1027 (file-name file-name))))
1030 (define-syntax-rule (with-null-output-ports exp ...)
1031 "Evaluate EXP with both the output port and the error port pointing to the
1033 (with-output-to-port (%make-void-port "w")
1035 (with-error-to-port (%make-void-port "w")
1036 (lambda () exp ...)))))
1038 (define (create-btrfs-file-system partition)
1039 "Create a btrfs file-system for PARTITION file-name."
1040 (with-null-output-ports
1041 (invoke "mkfs.btrfs" "-f" partition)))
1043 (define (create-ext4-file-system partition)
1044 "Create an ext4 file-system for PARTITION file-name."
1045 (with-null-output-ports
1046 (invoke "mkfs.ext4" "-F" partition)))
1048 (define (create-fat16-file-system partition)
1049 "Create a fat16 file-system for PARTITION file-name."
1050 (with-null-output-ports
1051 (invoke "mkfs.fat" "-F16" partition)))
1053 (define (create-fat32-file-system partition)
1054 "Create a fat32 file-system for PARTITION file-name."
1055 (with-null-output-ports
1056 (invoke "mkfs.fat" "-F32" partition)))
1058 (define (create-jfs-file-system partition)
1059 "Create a JFS file-system for PARTITION file-name."
1060 (with-null-output-ports
1061 (invoke "jfs_mkfs" "-f" partition)))
1063 (define (create-ntfs-file-system partition)
1064 "Create a JFS file-system for PARTITION file-name."
1065 (with-null-output-ports
1066 (invoke "mkfs.ntfs" "-F" "-f" partition)))
1068 (define (create-swap-partition partition)
1069 "Set up swap area on PARTITION file-name."
1070 (with-null-output-ports
1071 (invoke "mkswap" "-f" partition)))
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
1077 (put-string port password)
1081 (define (user-partition-upper-file-name user-partition)
1082 "Return the file-name of the virtual block device corresponding to
1083 USER-PARTITION if it is encrypted, or the plain file-name otherwise."
1084 (let ((crypt-label (user-partition-crypt-label user-partition))
1085 (file-name (user-partition-file-name user-partition)))
1087 (string-append "/dev/mapper/" crypt-label)
1090 (define (luks-format-and-open user-partition)
1091 "Format and open the encrypted partition pointed by USER-PARTITION."
1092 (let* ((file-name (user-partition-file-name user-partition))
1093 (label (user-partition-crypt-label user-partition))
1094 (password (user-partition-crypt-password user-partition)))
1095 (call-with-luks-key-file
1098 (syslog "formatting and opening LUKS entry ~s at ~s~%"
1100 (system* "cryptsetup" "-q" "luksFormat" file-name key-file)
1101 (system* "cryptsetup" "open" "--type" "luks"
1102 "--key-file" key-file file-name label)))))
1104 (define (luks-close user-partition)
1105 "Close the encrypted partition pointed by USER-PARTITION."
1106 (let ((label (user-partition-crypt-label user-partition)))
1107 (syslog "closing LUKS entry ~s~%" label)
1108 (system* "cryptsetup" "close" label)))
1110 (define (format-user-partitions user-partitions)
1111 "Format the <user-partition> records in USER-PARTITIONS list with
1112 NEED-FORMATING? field set to #t."
1114 (lambda (user-partition)
1115 (let* ((need-formatting?
1116 (user-partition-need-formatting? user-partition))
1117 (type (user-partition-type user-partition))
1118 (crypt-label (user-partition-crypt-label user-partition))
1119 (file-name (user-partition-upper-file-name user-partition))
1120 (fs-type (user-partition-fs-type user-partition)))
1122 (luks-format-and-open user-partition))
1126 (and need-formatting?
1127 (not (eq? type 'extended))
1128 (create-btrfs-file-system file-name)))
1130 (and need-formatting?
1131 (not (eq? type 'extended))
1132 (create-ext4-file-system file-name)))
1134 (and need-formatting?
1135 (not (eq? type 'extended))
1136 (create-fat16-file-system file-name)))
1138 (and need-formatting?
1139 (not (eq? type 'extended))
1140 (create-fat32-file-system file-name)))
1142 (and need-formatting?
1143 (not (eq? type 'extended))
1144 (create-jfs-file-system file-name)))
1146 (and need-formatting?
1147 (not (eq? type 'extended))
1148 (create-ntfs-file-system file-name)))
1150 (create-swap-partition file-name))
1152 ;; TODO: Add support for other file-system types.
1156 (define (sort-partitions user-partitions)
1157 "Sort USER-PARTITIONS by mount-points, so that the more nested mount-point
1158 comes last. This is useful to mount/umount partitions in a coherent order."
1159 (sort user-partitions
1161 (let ((mount-point-a (user-partition-mount-point a))
1162 (mount-point-b (user-partition-mount-point b)))
1163 (string-prefix? mount-point-a mount-point-b)))))
1165 (define (mount-user-partitions user-partitions)
1166 "Mount the <user-partition> records in USER-PARTITIONS list on their
1167 respective mount-points."
1168 (let* ((mount-partitions (filter user-partition-mount-point user-partitions))
1169 (sorted-partitions (sort-partitions mount-partitions)))
1170 (for-each (lambda (user-partition)
1172 (user-partition-mount-point user-partition))
1174 (string-append (%installer-target-dir)
1177 (user-partition-fs-type user-partition))
1179 (user-partition-crypt-label user-partition))
1181 (user-fs-type->mount-type fs-type))
1183 (user-partition-upper-file-name user-partition)))
1185 (syslog "mounting ~s on ~s~%" file-name target)
1186 (mount file-name target mount-type)))
1187 sorted-partitions)))
1189 (define (umount-user-partitions user-partitions)
1190 "Unmount all the <user-partition> records in USER-PARTITIONS list."
1191 (let* ((mount-partitions (filter user-partition-mount-point user-partitions))
1192 (sorted-partitions (sort-partitions mount-partitions)))
1193 (for-each (lambda (user-partition)
1195 (user-partition-mount-point user-partition))
1197 (user-partition-crypt-label user-partition))
1199 (string-append (%installer-target-dir)
1201 (syslog "unmounting ~s~%" target)
1204 (luks-close user-partition))))
1205 (reverse sorted-partitions))))
1207 (define (find-swap-user-partitions user-partitions)
1208 "Return the subset of <user-partition> records in USER-PARTITIONS list with
1209 the FS-TYPE field set to 'swap, return the empty list if none found."
1210 (filter (lambda (user-partition)
1211 (let ((fs-type (user-partition-fs-type user-partition)))
1212 (eq? fs-type 'swap)))
1215 (define (start-swapping user-partitions)
1216 "Start swapping on <user-partition> records with FS-TYPE equal to 'swap."
1217 (let* ((swap-user-partitions (find-swap-user-partitions user-partitions))
1218 (swap-devices (map user-partition-file-name swap-user-partitions)))
1219 (for-each swapon swap-devices)))
1221 (define (stop-swapping user-partitions)
1222 "Stop swapping on <user-partition> records with FS-TYPE equal to 'swap."
1223 (let* ((swap-user-partitions (find-swap-user-partitions user-partitions))
1224 (swap-devices (map user-partition-file-name swap-user-partitions)))
1225 (for-each swapoff swap-devices)))
1227 (define-syntax-rule (with-mounted-partitions user-partitions exp ...)
1228 "Mount USER-PARTITIONS and start swapping within the dynamic extent of EXP."
1231 (mount-user-partitions user-partitions)
1232 (start-swapping user-partitions))
1236 (umount-user-partitions user-partitions)
1237 (stop-swapping user-partitions)
1240 (define (user-partition->file-system user-partition)
1241 "Convert the given USER-PARTITION record in a FILE-SYSTEM record from
1242 (gnu system file-systems) module and return it."
1243 (let* ((mount-point (user-partition-mount-point user-partition))
1244 (fs-type (user-partition-fs-type user-partition))
1245 (crypt-label (user-partition-crypt-label user-partition))
1246 (mount-type (user-fs-type->mount-type fs-type))
1247 (file-name (user-partition-file-name user-partition))
1248 (upper-file-name (user-partition-upper-file-name user-partition))
1249 ;; Only compute uuid if partition is not encrypted.
1250 (uuid (or crypt-label
1251 (uuid->string (read-partition-uuid file-name) fs-type))))
1253 (mount-point ,mount-point)
1254 (device ,@(if crypt-label
1256 `((uuid ,uuid (quote ,fs-type)))))
1259 '((dependencies mapped-devices))
1262 (define (user-partitions->file-systems user-partitions)
1263 "Convert the given USER-PARTITIONS list of <user-partition> records into a
1264 list of <file-system> records."
1266 (lambda (user-partition)
1268 (user-partition-mount-point user-partition)))
1270 (user-partition->file-system user-partition))))
1273 (define (user-partition->mapped-device user-partition)
1274 "Convert the given USER-PARTITION record into a MAPPED-DEVICE record
1275 from (gnu system mapped-devices) and return it."
1276 (let ((label (user-partition-crypt-label user-partition))
1277 (file-name (user-partition-file-name user-partition)))
1279 (source (uuid ,(uuid->string
1280 (read-luks-partition-uuid file-name)
1283 (type luks-device-mapping))))
1285 (define (root-user-partition? partition)
1286 "Return true if PARTITION is the root partition."
1287 (let ((mount-point (user-partition-mount-point partition)))
1289 (string=? mount-point "/"))))
1291 (define (bootloader-configuration user-partitions)
1292 "Return the bootloader configuration field for USER-PARTITIONS."
1293 (let* ((root-partition (find root-user-partition?
1295 (root-partition-disk (user-partition-disk-file-name root-partition)))
1296 `((bootloader-configuration
1297 ,@(if (efi-installation?)
1298 `((bootloader grub-efi-bootloader)
1299 (target ,(default-esp-mount-point)))
1300 `((bootloader grub-bootloader)
1301 (target ,root-partition-disk)))
1303 ;; XXX: Assume we defined the 'keyboard-layout' field of
1304 ;; <operating-system> right above.
1305 (keyboard-layout keyboard-layout)))))
1307 (define (user-partition-missing-modules user-partitions)
1308 "Return the list of kernel modules missing from the default set of kernel
1309 modules to access USER-PARTITIONS."
1310 (let ((devices (filter user-partition-crypt-label user-partitions))
1311 (root (find root-user-partition? user-partitions)))
1313 (append-map (lambda (device)
1314 (catch 'system-error
1316 (missing-modules device %base-initrd-modules))
1319 (map user-partition-file-name
1320 (cons root devices)))))))
1322 (define (initrd-configuration user-partitions)
1323 "Return an 'initrd-modules' field with everything needed for
1324 USER-PARTITIONS, or return nothing."
1325 (match (user-partition-missing-modules user-partitions)
1329 `((initrd-modules (append ',modules
1330 %base-initrd-modules))))))
1332 (define (user-partitions->configuration user-partitions)
1333 "Return the configuration field for USER-PARTITIONS."
1334 (let* ((swap-user-partitions (find-swap-user-partitions user-partitions))
1335 (swap-devices (map user-partition-file-name swap-user-partitions))
1336 (encrypted-partitions
1337 (filter user-partition-crypt-label user-partitions)))
1338 `((bootloader ,@(bootloader-configuration user-partitions))
1339 ,@(initrd-configuration user-partitions)
1340 ,@(if (null? swap-devices)
1342 (let* ((uuids (map (lambda (file)
1343 (uuid->string (read-partition-uuid file)))
1345 `((swap-devices (list ,@(map (lambda (uuid)
1348 ,@(if (null? encrypted-partitions)
1351 (list ,@(map user-partition->mapped-device
1352 encrypted-partitions)))))
1353 (file-systems (cons*
1354 ,@(user-partitions->file-systems user-partitions)
1355 %base-file-systems)))))
1362 (define (init-parted)
1363 "Initialize libparted support."
1364 (probe-all-devices!)
1365 ;; Remove all logical devices, otherwise "device-is-busy?" will report true
1366 ;; on all devices containaing active logical volumes.
1367 (remove-logical-devices)
1368 (exception-set-handler (lambda (exception)
1369 EXCEPTION-OPTION-UNHANDLED)))
1371 (define (free-parted devices)
1372 "Deallocate memory used for DEVICES in parted, force sync them and wait for
1373 the devices not to be used before returning."
1374 ;; XXX: Formatting and further operations on disk partition table may fail
1375 ;; because the partition table changes are not synced, or because the device
1376 ;; is still in use, even if parted should have finished editing
1377 ;; partitions. This is not well understood, but syncing devices and waiting
1378 ;; them to stop returning EBUSY to BLKRRPART ioctl seems to be enough. The
1379 ;; same kind of issue is described here:
1380 ;; https://mail.gnome.org/archives/commits-list/2013-March/msg18423.html.
1381 (let ((device-file-names (map device-path devices)))
1382 (for-each force-device-sync devices)
1383 (for-each (lambda (file-name)
1384 (let/time ((time in-use?
1385 (with-delay-device-in-use? file-name)))
1388 (format #f (G_ "Device ~a is still in use.")
1390 (syslog "Syncing ~a took ~a seconds.~%"
1391 file-name (time-second time)))))
1392 device-file-names)))