1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2018, 2019 Mathieu Othacehe <m.othacehe@gmail.com>
3 ;;; Copyright © 2019 Ludovic Courtès <ludo@gnu.org>
5 ;;; This file is part of GNU Guix.
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.
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.
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/>.
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)
26 #:select (read-partition-uuid
27 read-luks-partition-uuid))
28 #:use-module ((gnu build linux-modules)
29 #:select (missing-modules))
30 #:use-module ((gnu system linux-initrd)
31 #:select (%base-initrd-modules))
32 #:use-module (guix build syscalls)
33 #:use-module (guix build utils)
34 #:use-module (guix records)
35 #:use-module (guix utils)
36 #:use-module (guix i18n)
38 #:use-module (ice-9 match)
39 #:use-module (ice-9 regex)
40 #:use-module (rnrs io ports)
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>
51 user-partition-file-name
52 user-partition-disk-file-name
53 user-partition-crypt-label
54 user-partition-crypt-password
55 user-partition-fs-type
56 user-partition-bootable?
58 user-partition-bios-grub?
62 user-partition-mount-point
63 user-partition-need-formatting?
64 user-partition-parted-object
67 small-freespace-partition?
70 default-esp-mount-point
72 with-delay-device-in-use?
77 partition-filesystem-user-type
79 partition->user-partition
80 create-special-user-partitions
81 find-user-partition-by-parted-object
84 partition-end-formatted
85 partition-print-number
87 partitions-descriptions
88 user-partition-description
92 &extended-creation-error
93 extended-creation-error?
94 &logical-creation-error
95 logical-creation-error?
107 check-user-partitions
108 set-user-partitions-file-name
109 format-user-partitions
110 mount-user-partitions
111 umount-user-partitions
112 with-mounted-partitions
113 user-partitions->file-systems
114 user-partitions->configuration
121 ;;; Partition record.
124 (define-record-type* <user-partition>
125 user-partition make-user-partition
127 (name user-partition-name ;string
129 (type user-partition-type
130 (default 'normal)) ; 'normal | 'logical | 'extended
131 (file-name user-partition-file-name
133 (disk-file-name user-partition-disk-file-name
135 (crypt-label user-partition-crypt-label
137 (crypt-password user-partition-crypt-password
139 (fs-type user-partition-fs-type
141 (bootable? user-partition-bootable?
143 (esp? user-partition-esp?
145 (bios-grub? user-partition-bios-grub?
147 (size user-partition-size
149 (start user-partition-start ;start as string (e.g. '11MB')
151 (end user-partition-end ;same as start
153 (mount-point user-partition-mount-point ;string
155 (need-formatting? user-partition-need-formatting? ; boolean
157 (parted-object user-partition-parted-object ; <partition> from parted
165 (define (find-esp-partition partitions)
166 "Find and return the ESP partition among PARTITIONS."
167 (find esp-partition? partitions))
169 (define* (small-freespace-partition? device
171 #:key (max-size MEBIBYTE-SIZE))
172 "Return #t is PARTITION is a free-space partition with less a size strictly
173 inferior to MAX-SIZE, #f otherwise."
174 (let ((size (partition-length partition))
175 (max-sector-size (/ max-size
176 (device-sector-size device))))
177 (< size max-sector-size)))
179 (define (partition-user-type partition)
180 "Return the type of PARTITION, to be stored in the TYPE field of
181 <user-partition> record. It can be 'normal, 'extended or 'logical."
182 (cond ((normal-partition? partition)
184 ((extended-partition? partition)
186 ((logical-partition? partition)
190 (define (esp-partition? partition)
191 "Return #t if partition has the ESP flag, return #f otherwise."
192 (let* ((disk (partition-disk partition))
193 (disk-type (disk-disk-type disk))
194 (has-extended? (disk-type-check-feature
196 DISK-TYPE-FEATURE-EXTENDED)))
197 (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."
224 ((swap) "linux-swap")))
226 (define (user-fs-type->mount-type fs-type)
227 "Return the mount type of FS-TYPE."
234 (define (partition-filesystem-user-type partition)
235 "Return the filesystem type of PARTITION, to be stored in the FS-TYPE field
236 of <user-partition> record."
237 (let ((fs-type (partition-fs-type partition)))
239 (let ((name (filesystem-type-name fs-type)))
241 ((string=? name "ext4") 'ext4)
242 ((string=? name "btrfs") 'btrfs)
243 ((string=? name "fat16") 'fat16)
244 ((string=? name "fat32") 'fat32)
245 ((or (string=? name "swsusp")
246 (string=? name "linux-swap(v0)")
247 (string=? name "linux-swap(v1)"))
250 (error (format #f "Unhandled ~a fs-type~%" name))))))))
252 (define (partition-get-flags partition)
253 "Return the list of flags supported by the given PARTITION."
254 (filter-map (lambda (flag)
255 (and (partition-get-flag partition flag)
257 (partition-flags partition)))
259 (define (partition->user-partition partition)
260 "Convert PARTITION into a <user-partition> record and return it."
261 (let* ((disk (partition-disk partition))
262 (device (disk-device disk))
263 (disk-type (disk-disk-type disk))
264 (has-name? (disk-type-check-feature
266 DISK-TYPE-FEATURE-PARTITION-NAME))
268 (data-partition? partition)
269 (partition-get-name partition))))
272 (not (string=? name "")))
274 (type (or (partition-user-type partition)
276 (file-name (partition-get-path partition))
277 (disk-file-name (device-path device))
278 (fs-type (or (partition-filesystem-user-type partition)
280 (mount-point (and (esp-partition? partition)
281 (default-esp-mount-point)))
282 (bootable? (boot-partition? partition))
283 (esp? (esp-partition? partition))
284 (parted-object partition))))
286 (define (create-special-user-partitions partitions)
287 "Return a list with a <user-partition> record describing the ESP partition
288 found in PARTITIONS, if any."
289 (filter-map (lambda (partition)
290 (and (esp-partition? partition)
291 (partition->user-partition partition)))
294 (define (find-user-partition-by-parted-object user-partitions
296 "Find and return the <user-partition> record in USER-PARTITIONS list which
297 PARTED-OBJECT field equals PARTITION, return #f if not found."
298 (find (lambda (user-partition)
299 (equal? (user-partition-parted-object user-partition)
308 (define (with-delay-device-in-use? file-name)
309 "Call DEVICE-IN-USE? with a few retries, as the first re-read will often
310 fail. See rereadpt function in wipefs.c of util-linux for an explanation."
311 ;; Kernel always return EINVAL for BLKRRPART on loopdevices.
312 (and (not (string-match "/dev/loop*" file-name))
315 (let ((in-use? (device-in-use? file-name)))
316 (if (and in-use? (> try 0))
320 (define* (force-device-sync device)
321 "Force a flushing of the given DEVICE."
324 (device-close device))
326 (define (non-install-devices)
327 "Return all the available devices, except the busy one, allegedly the
328 install device. DEVICE-IS-BUSY? is a parted call, checking if the device is
329 mounted. The install image uses an overlayfs so the install device does not
330 appear as mounted and won't be considered as busy. So use also DEVICE-IN-USE?
331 from (guix build syscalls) module, who will try to re-read the device's
332 partition table to determine whether or not it is already used (like sfdisk
334 (remove (lambda (device)
335 (let ((file-name (device-path device)))
336 (or (device-is-busy? device)
337 (with-delay-device-in-use? file-name))))
342 ;; Disk and partition printing.
345 (define* (device-description device #:optional disk)
346 "Return a string describing the given DEVICE."
347 (let* ((type (device-type device))
348 (file-name (device-path device))
349 (model (device-model device))
350 (type-str (device-type->string type))
352 (disk-disk-type disk)
353 (disk-probe device)))
354 (length (device-length device))
355 (sector-size (device-sector-size device))
356 (end (unit-format-custom-byte device
357 (* length sector-size)
360 `(,@(if (string=? model "")
362 `(,model ,(string-append "(" type-str ")")))
366 `(,(disk-type-name disk-type))
370 (define (partition-end-formatted device partition)
371 "Return as a string the end of PARTITION with the relevant unit."
375 (* (+ (partition-end partition) 1)
376 (device-sector-size device))
379 (define (partition-print-number partition)
380 "Convert the given partition NUMBER to string."
381 (let ((number (partition-number partition)))
382 (number->string number)))
384 (define (partition-description partition user-partition)
385 "Return a string describing the given PARTITION, located on the DISK of
388 (define (partition-print-type partition)
389 "Return the type of PARTITION as a string."
390 (if (freespace-partition? partition)
392 (let ((type (partition-type partition)))
395 (symbol->string type-symbol))))))
397 (define (partition-print-flags partition)
398 "Return the flags of PARTITION as a string of comma separated flags."
402 (and (partition-get-flag partition flag)
403 (partition-flag-get-name flag)))
404 (partition-flags partition))
407 (define (maybe-string-pad string length)
408 "Returned a string formatted by padding STRING of LENGTH characters to the
409 right. If STRING is #f use an empty string."
410 (if (and string (not (string=? string "")))
411 (string-pad-right string length)
414 (let* ((disk (partition-disk partition))
415 (device (disk-device disk))
416 (disk-type (disk-disk-type disk))
417 (has-name? (disk-type-check-feature
419 DISK-TYPE-FEATURE-PARTITION-NAME))
420 (has-extended? (disk-type-check-feature
422 DISK-TYPE-FEATURE-EXTENDED))
423 (part-type (partition-print-type partition))
424 (number (and (not (freespace-partition? partition))
425 (partition-print-number partition)))
427 (if (freespace-partition? partition)
429 (partition-get-name partition))))
430 (start (unit-format device
431 (partition-start partition)))
432 (end (partition-end-formatted device partition))
433 (size (unit-format device (partition-length partition)))
434 (fs-type (partition-fs-type partition))
435 (fs-type-name (and fs-type
436 (filesystem-type-name fs-type)))
437 (crypt-label (and user-partition
438 (user-partition-crypt-label user-partition)))
439 (flags (and (not (freespace-partition? partition))
440 (partition-print-flags partition)))
441 (mount-point (and user-partition
442 (user-partition-mount-point user-partition))))
448 ,(or fs-type-name "")
452 ,(maybe-string-pad name 30))))
454 (define (partitions-descriptions partitions user-partitions)
455 "Return a list of strings describing all the partitions found on
456 DEVICE. METADATA partitions are not described. The strings are padded to the
457 right so that they can be displayed as a table."
459 (define (max-length-column lists column-index)
460 "Return the maximum length of the string at position COLUMN-INDEX in the
461 list of string lists LISTS."
465 (list-ref list column-index)))
468 (define (pad-descriptions descriptions)
469 "Return a padded version of the list of string lists DESCRIPTIONS. The
470 strings are padded to the length of the longer string in a same column, as
471 determined by MAX-LENGTH-COLUMN procedure."
472 (let* ((description-length (length (car descriptions)))
473 (paddings (map (lambda (index)
474 (max-length-column descriptions index))
475 (iota description-length))))
476 (map (lambda (description)
477 (map string-pad-right description paddings))
483 (let ((user-partition
484 (find-user-partition-by-parted-object user-partitions
486 (partition-description partition user-partition)))
488 (padded-descriptions (if (null? partitions)
490 (pad-descriptions descriptions))))
491 (map (cut string-join <> " ") padded-descriptions)))
493 (define (user-partition-description user-partition)
494 "Return a string describing the given USER-PARTITION record."
495 (let* ((partition (user-partition-parted-object user-partition))
496 (disk (partition-disk partition))
497 (disk-type (disk-disk-type disk))
498 (device (disk-device disk))
499 (has-name? (disk-type-check-feature
501 DISK-TYPE-FEATURE-PARTITION-NAME))
502 (has-extended? (disk-type-check-feature
504 DISK-TYPE-FEATURE-EXTENDED))
505 (name (user-partition-name user-partition))
506 (type (user-partition-type user-partition))
507 (type-name (symbol->string type))
508 (fs-type (user-partition-fs-type user-partition))
509 (fs-type-name (user-fs-type-name fs-type))
510 (bootable? (user-partition-bootable? user-partition))
511 (esp? (user-partition-esp? user-partition))
512 (need-formatting? (user-partition-need-formatting? user-partition))
513 (crypt-label (user-partition-crypt-label user-partition))
514 (size (user-partition-size user-partition))
515 (mount-point (user-partition-mount-point user-partition)))
517 `((name . ,(string-append "Name: " (or name "None"))))
519 ,@(if (and has-extended?
520 (freespace-partition? partition)
521 (not (eq? type 'logical)))
522 `((type . ,(string-append "Type: " type-name)))
524 ,@(if (eq? type 'extended)
526 `((fs-type . ,(string-append "Filesystem type: " fs-type-name))))
527 ,@(if (or (eq? type 'extended)
531 `((bootable . ,(string-append "Bootable flag: "
532 (if bootable? "On" "Off")))))
533 ,@(if (and (not has-extended?)
534 (not (eq? fs-type 'swap)))
535 `((esp? . ,(string-append "ESP flag: "
536 (if esp? "On" "Off"))))
538 ,@(if (freespace-partition? partition)
539 (let ((size-formatted
540 (or size (unit-format device
541 (partition-length partition)))))
542 `((size . ,(string-append "Size : " size-formatted))))
544 ,@(if (or (eq? type 'extended)
551 (format #f "Yes (label ~a)" crypt-label)
553 ,@(if (or (freespace-partition? partition)
557 . ,(string-append "Format the partition? : "
558 (if need-formatting? "Yes" "No")))))
559 ,@(if (or (eq? type 'extended)
563 . ,(string-append "Mount point : "
565 (and esp? (default-esp-mount-point))
570 ;; Partition table creation.
573 (define (mklabel device type-name)
574 "Create a partition table on DEVICE. TYPE-NAME is the type of the partition
575 table, \"msdos\" or \"gpt\"."
576 (let ((type (disk-type-get type-name)))
577 (disk-new-fresh device type)))
581 ;; Partition creation.
584 ;; The maximum count of primary partitions is exceeded.
585 (define-condition-type &max-primary-exceeded &condition
586 max-primary-exceeded?)
588 ;; It is not possible to create an extended partition.
589 (define-condition-type &extended-creation-error &condition
590 extended-creation-error?)
592 ;; It is not possible to create a logical partition.
593 (define-condition-type &logical-creation-error &condition
594 logical-creation-error?)
596 (define (can-create-primary? disk)
597 "Return #t if it is possible to create a primary partition on DISK, return
599 (let ((max-primary (disk-get-max-primary-partition-count disk)))
600 (find (lambda (number)
601 (not (disk-get-partition disk number)))
602 (iota max-primary 1))))
604 (define (can-create-extended? disk)
605 "Return #t if it is possible to create an extended partition on DISK, return
607 (let* ((disk-type (disk-disk-type disk))
608 (has-extended? (disk-type-check-feature
610 DISK-TYPE-FEATURE-EXTENDED)))
611 (and (can-create-primary? disk)
613 (not (disk-extended-partition disk)))))
615 (define (can-create-logical? disk)
616 "Return #t is it is possible to create a logical partition on DISK, return
618 (let* ((disk-type (disk-disk-type disk))
619 (has-extended? (disk-type-check-feature
621 DISK-TYPE-FEATURE-EXTENDED)))
623 (disk-extended-partition disk))))
625 (define (can-create-partition? user-part)
626 "Return #t if it is possible to create the given USER-PART record, return #f
628 (let* ((type (user-partition-type user-part))
629 (partition (user-partition-parted-object user-part))
630 (disk (partition-disk partition)))
633 (or (can-create-primary? disk)
635 (condition (&max-primary-exceeded)))))
637 (or (can-create-extended? disk)
639 (condition (&extended-creation-error)))))
641 (or (can-create-logical? disk)
643 (condition (&logical-creation-error))))))))
645 (define* (mkpart disk user-partition
646 #:key (previous-partition #f))
647 "Create the given USER-PARTITION on DISK. The PREVIOUS-PARTITION argument as
648 to be set to the partition preceding USER-PARTITION if any."
650 (define (parse-start-end start end)
651 "Parse start and end strings as positions on DEVICE expressed with a unit,
652 like '100GB' or '12.2KiB'. Return a list of 4 elements, the start sector, its
653 range (1 unit large area centered on start sector), the end sector and its
655 (let ((device (disk-device disk)))
658 (unit-parse start device))
659 (lambda (start-sector start-range)
662 (unit-parse end device))
663 (lambda (end-sector end-range)
664 (list start-sector start-range
665 end-sector end-range)))))))
667 (define* (extend-ranges! start-range end-range
669 "Try to extend START-RANGE by 1 MEBIBYTE to the right and END-RANGE by 1
670 MEBIBYTE to the left. This way, if the disk is aligned on 2048 sectors of
671 512KB (like frequently), we will have a chance for the
672 'optimal-align-constraint' to succeed. Do not extend ranges if that would
673 cause them to cross."
674 (let* ((device (disk-device disk))
675 (start-range-end (geometry-end start-range))
676 (end-range-start (geometry-start end-range))
677 (mebibyte-sector-size (/ MEBIBYTE-SIZE
678 (device-sector-size device)))
680 (+ start-range-end mebibyte-sector-size offset))
682 (- end-range-start mebibyte-sector-size offset)))
683 (when (< new-start-range-end new-end-range-start)
684 (geometry-set-end start-range new-start-range-end)
685 (geometry-set-start end-range new-end-range-start))))
687 (match (parse-start-end (user-partition-start user-partition)
688 (user-partition-end user-partition))
689 ((start-sector start-range end-sector end-range)
690 (let* ((prev-end (if previous-partition
691 (partition-end previous-partition)
693 (start-distance (- start-sector prev-end))
694 (type (user-partition-type user-partition))
695 ;; There should be at least 2 unallocated sectors in front of each
696 ;; logical partition, otherwise parted will fail badly:
697 ;; https://gparted.org/h2-fix-msdos-pt.php#apply-action-fail.
698 (start-offset (if previous-partition
701 (start-sector* (if (and (eq? type 'logical)
702 (< start-distance 3))
703 (+ start-sector start-offset)
705 ;; This is a hack. Parted almost always fails to create optimally
706 ;; aligned partitions (unless specifying percentages) because the
707 ;; default range of 1MB centered on the start sector is not enough when
708 ;; the optimal alignment is 2048 sectors of 512KB.
709 (extend-ranges! start-range end-range #:offset start-offset)
711 (let* ((device (disk-device disk))
712 (disk-type (disk-disk-type disk))
713 (length (device-length device))
714 (name (user-partition-name user-partition))
718 (user-partition-fs-type user-partition))))
719 (flags `(,@(if (user-partition-bootable? user-partition)
720 `(,PARTITION-FLAG-BOOT)
722 ,@(if (user-partition-esp? user-partition)
723 `(,PARTITION-FLAG-ESP)
725 ,@(if (user-partition-bios-grub? user-partition)
726 `(,PARTITION-FLAG-BIOS-GRUB)
728 (has-name? (disk-type-check-feature
730 DISK-TYPE-FEATURE-PARTITION-NAME))
731 (partition-type (partition-type->int type))
732 (partition (partition-new disk
733 #:type partition-type
734 #:filesystem-type filesystem-type
735 #:start start-sector*
737 (user-constraint (constraint-new
740 #:start-range start-range
741 #:end-range end-range
745 (device-get-optimal-aligned-constraint device))
746 (final-constraint (constraint-intersect user-constraint
748 (no-constraint (constraint-any device))
749 ;; Try to create a partition with an optimal alignment
750 ;; constraint. If it fails, fallback to creating a partition with
751 ;; no specific constraint.
753 (or (disk-add-partition disk partition final-constraint)
754 (disk-add-partition disk partition no-constraint))))
755 ;; Set the partition name if supported.
756 (when (and partition-ok? has-name? name)
757 (partition-set-name partition name))
759 ;; Set flags is required.
760 (for-each (lambda (flag)
761 (and (partition-is-flag-available? partition flag)
762 (partition-set-flag partition flag 1)))
766 (partition-set-system partition filesystem-type)
771 ;; Partition destruction.
774 (define (rmpart disk number)
775 "Remove the partition with the given NUMBER on DISK."
776 (let ((partition (disk-get-partition disk number)))
777 (disk-remove-partition* disk partition)))
781 ;; Auto partitionning.
784 (define* (create-adjacent-partitions! disk partitions
785 #:key (last-partition-end 0))
786 "Create the given PARTITIONS on DISK. LAST-PARTITION-END is the sector from
787 which we want to start creating partitions. The START and END of each created
788 partition are computed from its SIZE value and the position of the last
790 (let ((device (disk-device disk)))
791 (let loop ((partitions partitions)
792 (remaining-space (- (device-length device)
794 (start last-partition-end))
798 (let* ((size (user-partition-size partition))
799 (percentage-size (and (string? size)
800 (read-percentage size)))
801 (sector-size (device-sector-size device))
802 (partition-size (if percentage-size
804 (* (/ percentage-size 100)
807 (end-partition (min (- (device-length device) 1)
808 (nearest-exact-integer
809 (+ start partition-size 1))))
810 (name (user-partition-name partition))
811 (type (user-partition-type partition))
812 (fs-type (user-partition-fs-type partition))
813 (start-formatted (unit-format-custom device
816 (end-formatted (unit-format-custom device
819 (new-user-partition (user-partition
821 (start start-formatted)
822 (end end-formatted)))
824 (mkpart disk new-user-partition)))
826 (cons (user-partition
827 (inherit new-user-partition)
828 (file-name (partition-get-path new-partition))
829 (disk-file-name (device-path device))
830 (parted-object new-partition))
832 (if (eq? type 'extended)
835 (partition-length new-partition)))
836 (if (eq? type 'extended)
838 (+ (partition-end new-partition) 1))))
840 (format #f "Unable to create partition ~a~%" name)))))))))
842 (define (force-user-partitions-formatting user-partitions)
843 "Set the NEED-FORMATING? fields to #t on all <user-partition> records of
844 USER-PARTITIONS list and return the updated list."
848 (need-formatting? #t)))
851 (define* (auto-partition! disk
853 (scheme 'entire-root))
854 "Automatically create partitions on DISK. All the previous
855 partitions (except the ESP on a GPT disk, if present) are wiped. SCHEME is the
856 desired partitioning scheme. It can be 'entire-root or
857 'entire-root-home. 'entire-root will create a swap partition and a root
858 partition occupying all the remaining space. 'entire-root-home will create a
859 swap partition, a root partition and a home partition.
861 Return the complete list of partitions on DISK, including the ESP when it
863 (let* ((device (disk-device disk))
864 (disk-type (disk-disk-type disk))
865 (has-extended? (disk-type-check-feature
867 DISK-TYPE-FEATURE-EXTENDED))
868 (partitions (filter data-partition? (disk-partitions disk)))
869 (esp-partition (find-esp-partition partitions))
871 ;; https://wiki.archlinux.org/index.php/EFI_system_partition, the ESP
872 ;; size should be at least 550MiB.
873 (new-esp-size (nearest-exact-integer
874 (/ (* 550 MEBIBYTE-SIZE)
875 (device-sector-size device))))
876 (end-esp-partition (and esp-partition
877 (partition-end esp-partition)))
878 (non-boot-partitions (remove esp-partition? partitions))
879 (bios-grub-size (/ (* 3 MEBIBYTE-SIZE)
880 (device-sector-size device)))
881 (five-percent-disk (nearest-exact-integer
882 (* 0.05 (device-length device))))
883 (default-swap-size (nearest-exact-integer
884 (/ (* 4 GIGABYTE-SIZE)
885 (device-sector-size device))))
886 ;; Use a 4GB size for the swap if it represents less than 5% of the
887 ;; disk space. Otherwise, set the swap size to 5% of the disk space.
888 (swap-size (min default-swap-size five-percent-disk)))
891 ;; msdos - remove everything.
892 (disk-remove-all-partitions disk)
893 ;; gpt - remove everything but esp if it exists.
896 (and (data-partition? partition)
897 (disk-remove-partition* disk partition)))
898 non-boot-partitions))
900 (let* ((start-partition
901 (and (not has-extended?)
903 (if (efi-installation?)
908 (mount-point (default-esp-mount-point)))
913 (size bios-grub-size)))))
916 ((or (eq? scheme 'entire-root)
917 (eq? scheme 'entire-encrypted-root))
918 (let ((encrypted? (eq? scheme 'entire-encrypted-root)))
919 `(,@(if start-partition
929 (bootable? has-extended?)
930 (crypt-label (and encrypted? "cryptroot"))
932 (mount-point "/")))))
933 ((or (eq? scheme 'entire-root-home)
934 (eq? scheme 'entire-encrypted-root-home))
935 (let ((encrypted? (eq? scheme 'entire-encrypted-root-home)))
936 `(,@(if start-partition
941 (bootable? has-extended?)
942 (crypt-label (and encrypted? "cryptroot"))
953 (type (if has-extended?
959 (type (if has-extended?
963 (crypt-label (and encrypted? "crypthome"))
965 (mount-point "/home")))))))
966 (new-partitions* (force-user-partitions-formatting
968 (append (if esp-partition
969 (list (partition->user-partition esp-partition))
971 (create-adjacent-partitions! disk
974 (or end-esp-partition 0))))))
978 ;; Convert user-partitions.
981 ;; No root mount point found.
982 (define-condition-type &no-root-mount-point &condition
983 no-root-mount-point?)
985 (define (check-user-partitions user-partitions)
986 "Return #t if the USER-PARTITIONS lists contains one <user-partition> record
987 with a mount-point set to '/', raise &no-root-mount-point condition
990 (map user-partition-mount-point user-partitions)))
991 (or (member "/" mount-points)
993 (condition (&no-root-mount-point))))))
995 (define (set-user-partitions-file-name user-partitions)
996 "Set the partition file-name of <user-partition> records in USER-PARTITIONS
997 list and return the updated list."
999 (let* ((partition (user-partition-parted-object p))
1000 (file-name (partition-get-path partition)))
1003 (file-name file-name))))
1006 (define-syntax-rule (with-null-output-ports exp ...)
1007 "Evaluate EXP with both the output port and the error port pointing to the
1009 (with-output-to-port (%make-void-port "w")
1011 (with-error-to-port (%make-void-port "w")
1012 (lambda () exp ...)))))
1014 (define (create-btrfs-file-system partition)
1015 "Create an btrfs file-system for PARTITION file-name."
1016 (with-null-output-ports
1017 (invoke "mkfs.btrfs" "-f" partition)))
1019 (define (create-ext4-file-system partition)
1020 "Create an ext4 file-system for PARTITION file-name."
1021 (with-null-output-ports
1022 (invoke "mkfs.ext4" "-F" partition)))
1024 (define (create-fat16-file-system partition)
1025 "Create a fat16 file-system for PARTITION file-name."
1026 (with-null-output-ports
1027 (invoke "mkfs.fat" "-F16" partition)))
1029 (define (create-fat32-file-system partition)
1030 "Create a fat32 file-system for PARTITION file-name."
1031 (with-null-output-ports
1032 (invoke "mkfs.fat" "-F32" partition)))
1034 (define (create-swap-partition partition)
1035 "Set up swap area on PARTITION file-name."
1036 (with-null-output-ports
1037 (invoke "mkswap" "-f" partition)))
1039 (define (call-with-luks-key-file password proc)
1040 "Write PASSWORD in a temporary file and pass it to PROC as argument."
1041 (call-with-temporary-output-file
1043 (put-string port password)
1047 (define (user-partition-upper-file-name user-partition)
1048 "Return the file-name of the virtual block device corresponding to
1049 USER-PARTITION if it is encrypted, or the plain file-name otherwise."
1050 (let ((crypt-label (user-partition-crypt-label user-partition))
1051 (file-name (user-partition-file-name user-partition)))
1053 (string-append "/dev/mapper/" crypt-label)
1056 (define (luks-format-and-open user-partition)
1057 "Format and open the encrypted partition pointed by USER-PARTITION."
1058 (let* ((file-name (user-partition-file-name user-partition))
1059 (label (user-partition-crypt-label user-partition))
1060 (password (user-partition-crypt-password user-partition)))
1061 (call-with-luks-key-file
1064 (system* "cryptsetup" "-q" "luksFormat" file-name key-file)
1065 (system* "cryptsetup" "open" "--type" "luks"
1066 "--key-file" key-file file-name label)))))
1068 (define (luks-close user-partition)
1069 "Close the encrypted partition pointed by USER-PARTITION."
1070 (let ((label (user-partition-crypt-label user-partition)))
1071 (system* "cryptsetup" "close" label)))
1073 (define (format-user-partitions user-partitions)
1074 "Format the <user-partition> records in USER-PARTITIONS list with
1075 NEED-FORMATING? field set to #t."
1077 (lambda (user-partition)
1078 (let* ((need-formatting?
1079 (user-partition-need-formatting? user-partition))
1080 (type (user-partition-type user-partition))
1081 (crypt-label (user-partition-crypt-label user-partition))
1082 (file-name (user-partition-upper-file-name user-partition))
1083 (fs-type (user-partition-fs-type user-partition)))
1085 (luks-format-and-open user-partition))
1089 (and need-formatting?
1090 (not (eq? type 'extended))
1091 (create-btrfs-file-system file-name)))
1093 (and need-formatting?
1094 (not (eq? type 'extended))
1095 (create-ext4-file-system file-name)))
1097 (and need-formatting?
1098 (not (eq? type 'extended))
1099 (create-fat16-file-system file-name)))
1101 (and need-formatting?
1102 (not (eq? type 'extended))
1103 (create-fat32-file-system file-name)))
1105 (create-swap-partition file-name))
1107 ;; TODO: Add support for other file-system types.
1111 (define (sort-partitions user-partitions)
1112 "Sort USER-PARTITIONS by mount-points, so that the more nested mount-point
1113 comes last. This is useful to mount/umount partitions in a coherent order."
1114 (sort user-partitions
1116 (let ((mount-point-a (user-partition-mount-point a))
1117 (mount-point-b (user-partition-mount-point b)))
1118 (string-prefix? mount-point-a mount-point-b)))))
1120 (define (mount-user-partitions user-partitions)
1121 "Mount the <user-partition> records in USER-PARTITIONS list on their
1122 respective mount-points."
1123 (let* ((mount-partitions (filter user-partition-mount-point user-partitions))
1124 (sorted-partitions (sort-partitions mount-partitions)))
1125 (for-each (lambda (user-partition)
1127 (user-partition-mount-point user-partition))
1129 (string-append (%installer-target-dir)
1132 (user-partition-fs-type user-partition))
1134 (user-partition-crypt-label user-partition))
1136 (user-fs-type->mount-type fs-type))
1138 (user-partition-upper-file-name user-partition)))
1140 (mount file-name target mount-type)))
1141 sorted-partitions)))
1143 (define (umount-user-partitions user-partitions)
1144 "Unmount all the <user-partition> records in USER-PARTITIONS list."
1145 (let* ((mount-partitions (filter user-partition-mount-point user-partitions))
1146 (sorted-partitions (sort-partitions mount-partitions)))
1147 (for-each (lambda (user-partition)
1149 (user-partition-mount-point user-partition))
1151 (user-partition-crypt-label user-partition))
1153 (string-append (%installer-target-dir)
1157 (luks-close user-partition))))
1158 (reverse sorted-partitions))))
1160 (define (find-swap-user-partitions user-partitions)
1161 "Return the subset of <user-partition> records in USER-PARTITIONS list with
1162 the FS-TYPE field set to 'swap, return the empty list if none found."
1163 (filter (lambda (user-partition)
1164 (let ((fs-type (user-partition-fs-type user-partition)))
1165 (eq? fs-type 'swap)))
1168 (define (start-swapping user-partitions)
1169 "Start swaping on <user-partition> records with FS-TYPE equal to 'swap."
1170 (let* ((swap-user-partitions (find-swap-user-partitions user-partitions))
1171 (swap-devices (map user-partition-file-name swap-user-partitions)))
1172 (for-each swapon swap-devices)))
1174 (define (stop-swapping user-partitions)
1175 "Stop swaping on <user-partition> records with FS-TYPE equal to 'swap."
1176 (let* ((swap-user-partitions (find-swap-user-partitions user-partitions))
1177 (swap-devices (map user-partition-file-name swap-user-partitions)))
1178 (for-each swapoff swap-devices)))
1180 (define-syntax-rule (with-mounted-partitions user-partitions exp ...)
1181 "Mount USER-PARTITIONS and start swapping within the dynamic extent of EXP."
1184 (mount-user-partitions user-partitions)
1185 (start-swapping user-partitions))
1189 (umount-user-partitions user-partitions)
1190 (stop-swapping user-partitions)
1193 (define (user-partition->file-system user-partition)
1194 "Convert the given USER-PARTITION record in a FILE-SYSTEM record from
1195 (gnu system file-systems) module and return it."
1196 (let* ((mount-point (user-partition-mount-point user-partition))
1197 (fs-type (user-partition-fs-type user-partition))
1198 (crypt-label (user-partition-crypt-label user-partition))
1199 (mount-type (user-fs-type->mount-type fs-type))
1200 (file-name (user-partition-file-name user-partition))
1201 (upper-file-name (user-partition-upper-file-name user-partition))
1202 ;; Only compute uuid if partition is not encrypted.
1203 (uuid (or crypt-label
1204 (uuid->string (read-partition-uuid file-name) fs-type))))
1206 (mount-point ,mount-point)
1207 (device ,@(if crypt-label
1209 `((uuid ,uuid (quote ,fs-type)))))
1212 '((dependencies mapped-devices))
1215 (define (user-partitions->file-systems user-partitions)
1216 "Convert the given USER-PARTITIONS list of <user-partition> records into a
1217 list of <file-system> records."
1219 (lambda (user-partition)
1221 (user-partition-mount-point user-partition)))
1223 (user-partition->file-system user-partition))))
1226 (define (user-partition->mapped-device user-partition)
1227 "Convert the given USER-PARTITION record into a MAPPED-DEVICE record
1228 from (gnu system mapped-devices) and return it."
1229 (let ((label (user-partition-crypt-label user-partition))
1230 (file-name (user-partition-file-name user-partition)))
1232 (source (uuid ,(uuid->string
1233 (read-luks-partition-uuid file-name)
1236 (type luks-device-mapping))))
1238 (define (root-user-partition? partition)
1239 "Return true if PARTITION is the root partition."
1240 (let ((mount-point (user-partition-mount-point partition)))
1242 (string=? mount-point "/"))))
1244 (define (bootloader-configuration user-partitions)
1245 "Return the bootloader configuration field for USER-PARTITIONS."
1246 (let* ((root-partition (find root-user-partition?
1248 (root-partition-disk (user-partition-disk-file-name root-partition)))
1249 `((bootloader-configuration
1250 ,@(if (efi-installation?)
1251 `((bootloader grub-efi-bootloader)
1252 (target ,(default-esp-mount-point)))
1253 `((bootloader grub-bootloader)
1254 (target ,root-partition-disk)))
1256 ;; XXX: Assume we defined the 'keyboard-layout' field of
1257 ;; <operating-system> right above.
1258 (keyboard-layout keyboard-layout)))))
1260 (define (user-partition-missing-modules user-partitions)
1261 "Return the list of kernel modules missing from the default set of kernel
1262 modules to access USER-PARTITIONS."
1263 (let ((devices (filter user-partition-crypt-label user-partitions))
1264 (root (find root-user-partition? user-partitions)))
1266 (append-map (lambda (device)
1267 (catch 'system-error
1269 (missing-modules device %base-initrd-modules))
1272 (map user-partition-file-name
1273 (cons root devices)))))))
1275 (define (initrd-configuration user-partitions)
1276 "Return an 'initrd-modules' field with everything needed for
1277 USER-PARTITIONS, or return nothing."
1278 (match (user-partition-missing-modules user-partitions)
1282 `((initrd-modules (append ',modules
1283 %base-initrd-modules))))))
1285 (define (user-partitions->configuration user-partitions)
1286 "Return the configuration field for USER-PARTITIONS."
1287 (let* ((swap-user-partitions (find-swap-user-partitions user-partitions))
1288 (swap-devices (map user-partition-file-name swap-user-partitions))
1289 (encrypted-partitions
1290 (filter user-partition-crypt-label user-partitions)))
1291 `((bootloader ,@(bootloader-configuration user-partitions))
1292 ,@(initrd-configuration user-partitions)
1293 ,@(if (null? swap-devices)
1295 `((swap-devices (list ,@swap-devices))))
1296 ,@(if (null? encrypted-partitions)
1299 (list ,@(map user-partition->mapped-device
1300 encrypted-partitions)))))
1301 (file-systems (cons*
1302 ,@(user-partitions->file-systems user-partitions)
1303 %base-file-systems)))))
1310 (define (init-parted)
1311 "Initialize libparted support."
1312 (probe-all-devices!)
1313 (exception-set-handler (lambda (exception)
1314 EXCEPTION-OPTION-UNHANDLED)))
1316 (define (free-parted devices)
1317 "Deallocate memory used for DEVICES in parted, force sync them and wait for
1318 the devices not to be used before returning."
1319 ;; XXX: Formatting and further operations on disk partition table may fail
1320 ;; because the partition table changes are not synced, or because the device
1321 ;; is still in use, even if parted should have finished editing
1322 ;; partitions. This is not well understood, but syncing devices and waiting
1323 ;; them to stop returning EBUSY to BLKRRPART ioctl seems to be enough. The
1324 ;; same kind of issue is described here:
1325 ;; https://mail.gnome.org/archives/commits-list/2013-March/msg18423.html.
1326 (let ((device-file-names (map device-path devices)))
1327 (for-each force-device-sync devices)
1328 (for-each (lambda (file-name)
1329 (let ((in-use? (with-delay-device-in-use? file-name)))
1332 (format #f (G_ "Device ~a is still in use.")
1334 device-file-names)))