1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2018 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 newt partition)
21 #:use-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 installer newt utils)
26 #:use-module (guix i18n)
27 #:use-module (ice-9 match)
28 #:use-module (srfi srfi-1)
29 #:use-module (srfi srfi-26)
30 #:use-module (srfi srfi-34)
31 #:use-module (srfi srfi-35)
34 #:export (run-partioning-page))
36 (define (button-exit-action)
37 "Raise the &installer-step-abort condition."
40 (&installer-step-abort))))
42 (define (run-scheme-page)
43 "Run a page asking the user for a partitioning scheme."
45 `((root . ,(G_ "Everything is one partition"))
46 (root-home . ,(G_ "Separate /home partition"))))
47 (result (run-listbox-selection-page
48 #:info-text (G_ "Please select a partitioning scheme.")
49 #:title (G_ "Partition scheme")
51 #:listbox-item->text cdr
52 #:button-text (G_ "Exit")
53 #:button-callback-procedure button-exit-action)))
56 (define (draw-formatting-page)
57 "Draw a page asking for confirmation, and then indicating that partitions
59 (run-confirmation-page (G_ "We are about to format your hard disk. All \
60 its data will be lost. Do you wish to continue?")
62 #:exit-button-procedure button-exit-action)
64 (format #f (G_ "Partition formatting is in progress, please wait."))
65 (G_ "Preparing partitions")))
67 (define (run-device-page devices)
68 "Run a page asking the user to select a device among those in the given
70 (define (device-items)
72 `(,device . ,(device-description device)))
75 (let* ((result (run-listbox-selection-page
76 #:info-text (G_ "Please select a disk.")
78 #:listbox-items (device-items)
79 #:listbox-item->text cdr
80 #:button-text (G_ "Exit")
81 #:button-callback-procedure button-exit-action))
82 (device (car result)))
85 (define (run-label-page button-text button-callback)
86 "Run a page asking the user to select a partition table label."
87 (run-listbox-selection-page
88 #:info-text (G_ "Select a new partition table type. \
89 Be careful, all data on the disk will be lost.")
90 #:title (G_ "Partition table")
91 #:listbox-items '("msdos" "gpt")
92 #:listbox-item->text identity
93 #:button-text button-text
94 #:button-callback-procedure button-callback))
96 (define (run-type-page partition)
97 "Run a page asking the user to select a partition type."
98 (let* ((disk (partition-disk partition))
99 (partitions (disk-partitions disk))
100 (other-extended-partitions?
101 (any extended-partition? partitions))
103 `(normal ,@(if other-extended-partitions?
106 (run-listbox-selection-page
107 #:info-text (G_ "Please select a partition type.")
108 #:title (G_ "Partition type")
109 #:listbox-items items
110 #:listbox-item->text symbol->string
111 #:sort-listbox-items? #f
112 #:button-text (G_ "Exit")
113 #:button-callback-procedure button-exit-action)))
115 (define (run-fs-type-page)
116 "Run a page asking the user to select a file-system type."
117 (run-listbox-selection-page
118 #:info-text (G_ "Please select the file-system type for this partition.")
119 #:title (G_ "File-system type")
120 #:listbox-items '(ext4 btrfs fat32 swap)
121 #:listbox-item->text user-fs-type-name
122 #:sort-listbox-items? #f
123 #:button-text (G_ "Exit")
124 #:button-callback-procedure button-exit-action))
126 (define (inform-can-create-partition? user-partition)
127 "Return #t if it is possible to create USER-PARTITION. This is determined by
128 calling CAN-CREATE-PARTITION? procedure. If an exception is raised, catch it
129 an inform the user with an appropriate error-page and return #f."
130 (guard (c ((max-primary-exceeded? c)
132 (G_ "Primary partitions count exceeded.")
133 (G_ "Creation error"))
135 ((extended-creation-error? c)
137 (G_ "Extended partition creation error.")
138 (G_ "Creation error"))
140 ((logical-creation-error? c)
142 (G_ "Logical partition creation error.")
143 (G_ "Creation error"))
145 (can-create-partition? user-partition)))
147 (define (prompt-luks-passwords user-partitions)
148 "Prompt for the luks passwords of the encrypted partitions in
149 USER-PARTITIONS list. Return this list with password fields filled-in."
150 (map (lambda (user-part)
151 (let* ((crypt-label (user-partition-crypt-label user-part))
152 (file-name (user-partition-file-name user-part))
155 ;; Note: Don't use FLAG-PASSWORD here because this is the
156 ;; first bit of text that the user types in, so it's
157 ;; probably safer if they can see that the keyboard layout
158 ;; they chose is in effect.
160 (format #f (G_ "Please enter the password for the \
161 encryption of partition ~a (label: ~a).") file-name crypt-label)
162 (G_ "Password required"))))
163 (password-confirm-page
166 (format #f (G_ "Please confirm the password for the \
167 encryption of partition ~a (label: ~a).") file-name crypt-label)
168 (G_ "Password confirmation required")
169 #:input-flags FLAG-PASSWORD))))
172 (let ((password (password-page))
173 (confirmation (password-confirm-page)))
174 (if (string=? password confirmation)
177 (crypt-password password))
180 (G_ "Password mismatch, please try again.")
181 (G_ "Password error"))
186 (define* (run-partition-page target-user-partition
189 "Run a page allowing the user to edit the given TARGET-USER-PARTITION
190 record. If the argument DEFAULT-ITEM is passed, use it to select the current
191 listbox item. This is used to avoid the focus to switch back to the first
192 listbox entry while calling this procedure recursively."
194 (define (numeric-size device size)
195 "Parse the given SIZE on DEVICE and return it."
198 (unit-parse size device))
199 (lambda (value range)
202 (define (numeric-size-range device size)
203 "Parse the given SIZE on DEVICE and return the associated RANGE."
206 (unit-parse size device))
207 (lambda (value range)
210 (define* (fill-user-partition-geom user-part
212 device (size #f) start end)
213 "Return the given USER-PART with the START, END and SIZE fields set to the
214 eponym arguments. Use UNIT-FORMAT-CUSTOM to format START and END arguments as
219 (start (unit-format-custom device start UNIT-SECTOR))
220 (end (unit-format-custom device end UNIT-SECTOR))))
222 (define (apply-user-partition-changes user-part)
223 "Set the name, file-system type and boot flag on the partition specified
224 by USER-PART, if it is applicable for the partition type."
225 (let* ((partition (user-partition-parted-object user-part))
226 (disk (partition-disk partition))
227 (disk-type (disk-disk-type disk))
228 (device (disk-device disk))
229 (has-name? (disk-type-check-feature
231 DISK-TYPE-FEATURE-PARTITION-NAME))
232 (name (user-partition-name user-part))
233 (fs-type (filesystem-type-get
235 (user-partition-fs-type user-part))))
236 (bootable? (user-partition-bootable? user-part))
237 (esp? (user-partition-esp? user-part))
239 (partition-is-flag-available? partition PARTITION-FLAG-BOOT))
241 (partition-is-flag-available? partition PARTITION-FLAG-ESP)))
242 (when (and has-name? name)
243 (partition-set-name partition name))
244 (partition-set-system partition fs-type)
246 (partition-set-flag partition
250 (partition-set-flag partition
255 (define (listbox-action listbox-item)
256 (let* ((item (car listbox-item))
257 (partition (user-partition-parted-object
258 target-user-partition))
259 (disk (partition-disk partition))
260 (device (disk-device disk)))
265 (let* ((old-name (user-partition-name target-user-partition))
267 (run-input-page (G_ "Please enter the partition gpt name.")
268 (G_ "Partition name")
269 #:default-text old-name)))
271 (inherit target-user-partition)
274 (let ((new-type (run-type-page partition)))
276 (inherit target-user-partition)
280 (inherit target-user-partition)
281 (bootable? (not (user-partition-bootable?
282 target-user-partition)))))
284 (let ((new-esp? (not (user-partition-esp?
285 target-user-partition))))
287 (inherit target-user-partition)
289 (mount-point (if new-esp?
290 (default-esp-mount-point)
293 (let* ((label (user-partition-crypt-label
294 target-user-partition))
298 (G_ "Please enter the encrypted label")
299 (G_ "Encryption label")))))
301 (inherit target-user-partition)
302 (need-formatting? #t)
303 (crypt-label new-label))))
306 (inherit target-user-partition)
308 (not (user-partition-need-formatting?
309 target-user-partition)))))
311 (let* ((old-size (user-partition-size target-user-partition))
312 (max-size-value (partition-length partition))
313 (max-size (unit-format device max-size-value))
314 (start (partition-start partition))
315 (size (run-input-page
316 (format #f (G_ "Please enter the size of the partition.\
317 The maximum size is ~a.") max-size)
318 (G_ "Partition size")
319 #:default-text (or old-size max-size)))
320 (size-percentage (read-percentage size))
321 (size-value (if size-percentage
322 (nearest-exact-integer
323 (/ (* max-size-value size-percentage)
325 (numeric-size device size)))
327 (+ start size-value)))
328 (size-range (numeric-size-range device size))
329 (size-range-ok? (and size-range
331 (geometry-start size-range))
332 (partition-end partition)))))
334 ((and size-percentage (> size-percentage 100))
336 (G_ "The percentage can not be superior to 100.")
338 target-user-partition)
341 (G_ "The requested size is incorrectly formatted, or too large.")
343 target-user-partition)
344 ((not (or size-percentage size-range-ok?))
346 (G_ "The request size is superior to the maximum size.")
348 target-user-partition)
350 (fill-user-partition-geom target-user-partition
356 (let ((fs-type (run-fs-type-page)))
358 (inherit target-user-partition)
361 (let* ((old-mount (or (user-partition-mount-point
362 target-user-partition)
366 (G_ "Please enter the desired mounting point for this \
367 partition. Leave this field empty if you don't want to set a mounting point.")
368 (G_ "Mounting point")
369 #:default-text old-mount
370 #:allow-empty-input? #t)))
372 (inherit target-user-partition)
373 (mount-point (and (not (string=? mount ""))
376 (define (button-action)
377 (let* ((partition (user-partition-parted-object
378 target-user-partition))
379 (prev-part (partition-prev partition))
380 (disk (partition-disk partition))
381 (device (disk-device disk))
382 (creation? (freespace-partition? partition))
383 (start (partition-start partition))
384 (end (partition-end partition))
386 (if (user-partition-start target-user-partition)
387 target-user-partition
388 (fill-user-partition-geom target-user-partition
392 ;; It the backend PARTITION has free-space type, it means we are
393 ;; creating a new partition, otherwise, we are editing an already
394 ;; existing PARTITION.
396 (let* ((ok-create-partition?
397 (inform-can-create-partition? new-user-partition))
399 (and ok-create-partition?
402 #:previous-partition prev-part))))
405 (inherit new-user-partition)
406 (need-formatting? #t)
407 (file-name (partition-get-path new-partition))
408 (disk-file-name (device-path device))
409 (parted-object new-partition))))
410 (and (apply-user-partition-changes new-user-partition)
411 new-user-partition))))
413 (let* ((items (user-partition-description target-user-partition))
414 (partition (user-partition-parted-object
415 target-user-partition))
416 (disk (partition-disk partition))
417 (device (disk-device disk))
418 (file-name (device-path device))
419 (number-str (partition-print-number partition))
420 (type (user-partition-type target-user-partition))
421 (type-str (symbol->string type))
422 (start (unit-format device (partition-start partition)))
423 (creation? (freespace-partition? partition))
424 (default-item (and default-item
426 (eq? (car item) default-item))
429 (run-listbox-selection-page
432 (format #f (G_ "Creating ~a partition starting at ~a of ~a.")
433 type-str start file-name)
434 (format #f (G_ "You are currently editing partition ~a.")
436 #:title (if creation?
437 (G_ "Partition creation")
438 (G_ "Partition edit"))
439 #:listbox-items items
440 #:listbox-item->text cdr
441 #:sort-listbox-items? #f
442 #:listbox-default-item default-item
443 #:button-text (G_ "OK")
444 #:listbox-callback-procedure listbox-action
445 #:button-callback-procedure button-action)))
447 ((item new-user-partition)
448 (run-partition-page new-user-partition
449 #:default-item item))
452 (define* (run-disk-page disks
453 #:optional (user-partitions '())
455 "Run a page allowing to edit the partition tables of the given DISKS. If
456 specified, USER-PARTITIONS is a list of <user-partition> records associated to
457 the partitions on DISKS."
459 (define (other-logical-partitions? partitions)
460 "Return #t if at least one of the partition in PARTITIONS list is a
461 logical partition, return #f otherwise."
462 (any logical-partition? partitions))
464 (define (other-non-logical-partitions? partitions)
465 "Return #t is at least one of the partitions in PARTITIONS list is not a
466 logical partition, return #f otherwise."
467 (let ((non-logical-partitions
468 (remove logical-partition? partitions)))
469 (or (any normal-partition? non-logical-partitions)
470 (any freespace-partition? non-logical-partitions))))
472 (define (add-tree-symbols partitions descriptions)
473 "Concatenate tree symbols to the given DESCRIPTIONS list and return
474 it. The PARTITIONS list is the list of partitions described in
475 DESCRIPTIONS. The tree symbols are used to indicate the partition's disk and
476 for logical partitions, the extended partition which includes them."
479 ((description . rest-descriptions)
481 ((partition . rest-partitions)
482 (if (null? rest-descriptions)
483 (list (if (logical-partition? partition)
484 (string-append " ┗━ " description)
485 (string-append "┗━ " description)))
487 ((extended-partition? partition)
488 (if (other-non-logical-partitions? rest-partitions)
489 (string-append "┣┳ " description)
490 (string-append "┗┳ " description)))
491 ((logical-partition? partition)
492 (if (other-logical-partitions? rest-partitions)
493 (if (other-non-logical-partitions? rest-partitions)
494 (string-append "┃┣━ " description)
495 (string-append " ┣━ " description))
496 (if (other-non-logical-partitions? rest-partitions)
497 (string-append "┃┗━ " description)
498 (string-append " ┗━ " description))))
500 (string-append "┣━ " description)))
501 (add-tree-symbols rest-partitions
502 rest-descriptions))))))))
504 (define (skip-item? item)
505 (eq? (car item) 'skip))
508 "Return the list of strings describing DISKS."
509 (let loop ((disks disks))
513 (let* ((device (disk-device disk))
514 (partitions (disk-partitions disk))
518 (and (not (metadata-partition? partition))
519 (not (small-freespace-partition? device
523 (descriptions (add-tree-symbols
525 (partitions-descriptions partitions*
527 (partition-items (map cons partitions* descriptions)))
529 `((,disk . ,(device-description device disk))
536 (define (remove-user-partition-by-partition user-partitions partition)
537 "Return the USER-PARTITIONS list with the record with the given PARTITION
538 object removed. If PARTITION is an extended partition, also remove all logical
539 partitions from USER-PARTITIONS."
541 (let ((cur-partition (user-partition-parted-object p)))
542 (or (equal? cur-partition partition)
543 (and (extended-partition? partition)
544 (logical-partition? cur-partition)))))
547 (define (remove-user-partition-by-disk user-partitions disk)
548 "Return the USER-PARTITIONS list with the <user-partition> records located
549 on given DISK removed."
551 (let* ((partition (user-partition-parted-object p))
552 (cur-disk (partition-disk partition)))
553 (equal? cur-disk disk)))
556 (define (update-user-partitions user-partitions new-user-partition)
557 "Update or insert NEW-USER-PARTITION record in USER-PARTITIONS list
558 depending if one of the <user-partition> record in USER-PARTITIONS has the
559 same PARTITION object as NEW-USER-PARTITION."
560 (let* ((partition (user-partition-parted-object new-user-partition))
562 (remove-user-partition-by-partition user-partitions
564 (cons new-user-partition user-partitions*)))
566 (define (button-ok-action)
567 "Commit the modifications to all DISKS and return #t."
568 (for-each (lambda (disk)
573 (define (listbox-action listbox-item)
574 "A disk or a partition has been selected. If it's a disk, ask for a label
575 to create a new partition table. If it is a partition, propose the user to
577 (let ((item (car listbox-item)))
580 (let ((label (run-label-page (G_ "Back") (const #f))))
582 (let* ((device (disk-device item))
583 (new-disk (mklabel device label))
584 (commit-new-disk (disk-commit new-disk))
585 (other-disks (remove (lambda (disk)
589 (remove-user-partition-by-disk user-partitions item)))
591 `((disks . ,(cons new-disk other-disks))
592 (user-partitions . ,new-user-partitions)))
594 (user-partitions . ,user-partitions)))))
596 (let* ((partition item)
597 (disk (partition-disk partition))
598 (device (disk-device disk))
599 (existing-user-partition
600 (find-user-partition-by-parted-object user-partitions
603 (or existing-user-partition
604 (partition->user-partition partition))))
606 (user-partitions . ,user-partitions)
607 (edit-user-partition . ,edit-user-partition)))))))
609 (define (hotkey-action key listbox-item)
610 "The DELETE key has been pressed on a disk or a partition item."
611 (let ((item (car listbox-item))
614 (user-partitions . ,user-partitions))))
617 (let* ((device (disk-device item))
618 (file-name (device-path device))
620 (format #f (G_ "Are you sure you want to delete everything on disk ~a?")
622 (result (choice-window (G_ "Delete disk")
628 (disk-delete-all item)
631 . ,(remove-user-partition-by-disk user-partitions item))))
635 (if (freespace-partition? item)
636 (run-error-page (G_ "You cannot delete a free space area.")
637 (G_ "Delete partition"))
638 (let* ((disk (partition-disk item))
639 (number-str (partition-print-number item))
641 (format #f (G_ "Are you sure you want to delete partition ~a?")
643 (result (choice-window (G_ "Delete partition")
649 (let ((new-user-partitions
650 (remove-user-partition-by-partition user-partitions
652 (disk-delete-partition disk item)
654 (user-partitions . ,new-user-partitions))))
656 default-result))))))))
658 (let* ((info-text (G_ "You can change a disk's partition table by \
659 selecting it and pressing ENTER. You can also edit a partition by selecting it \
660 and pressing ENTER, or remove it by pressing DELETE. To create a new \
661 partition, select a free space area and press ENTER.
663 At least one partition must have its mounting point set to '/'."))
664 (guided-info-text (format #f (G_ "This is the proposed \
665 partitioning. It is still possible to edit it or to go back to install menu \
666 by pressing the Exit button.~%~%")))
668 (run-listbox-selection-page
669 #:info-text (if guided?
670 (string-append guided-info-text info-text)
674 (G_ "Guided partitioning")
675 (G_ "Manual partitioning"))
676 #:info-textbox-width 70
677 #:listbox-items (disk-items)
678 #:listbox-item->text cdr
679 #:sort-listbox-items? #f
680 #:skip-item-procedure? skip-item?
682 #:button-text (G_ "OK")
683 #:button-callback-procedure button-ok-action
684 #:button2-text (G_ "Exit")
685 #:button2-callback-procedure button-exit-action
686 #:listbox-callback-procedure listbox-action
687 #:hotkey-callback-procedure hotkey-action)))
689 (let ((user-partitions-ok?
691 (c ((no-root-mount-point? c)
693 (G_ "No root mount point found.")
694 (G_ "Missing mount point"))
696 (check-user-partitions user-partitions))))
697 (if user-partitions-ok?
699 (for-each (cut disk-destroy <>) disks)
701 (run-disk-page disks user-partitions
703 (let* ((result-disks (assoc-ref result 'disks))
704 (result-user-partitions (assoc-ref result
706 (edit-user-partition (assoc-ref result
707 'edit-user-partition))
708 (can-create-partition?
709 (and edit-user-partition
710 (inform-can-create-partition? edit-user-partition)))
711 (new-user-partition (and edit-user-partition
712 can-create-partition?
714 edit-user-partition)))
716 (if new-user-partition
717 (update-user-partitions result-user-partitions
719 result-user-partitions)))
720 (run-disk-page result-disks new-user-partitions
721 #:guided? guided?)))))
723 (define (run-partioning-page)
724 "Run a page asking the user for a partitioning method."
725 (define (run-page devices)
727 `((entire . ,(G_ "Guided - using the entire disk"))
728 (entire-encrypted . ,(G_ "Guided - using the entire disk with encryption"))
729 (manual . ,(G_ "Manual"))))
730 (result (run-listbox-selection-page
731 #:info-text (G_ "Please select a partitioning method.")
732 #:title (G_ "Partitioning method")
733 #:listbox-items items
734 #:listbox-item->text cdr
735 #:button-text (G_ "Exit")
736 #:button-callback-procedure button-exit-action))
737 (method (car result)))
739 ((or (eq? method 'entire)
740 (eq? method 'entire-encrypted))
741 (let* ((device (run-device-page devices))
742 (disk-type (disk-probe device))
745 (let* ((label (run-label-page
748 (disk (mklabel device label)))
751 (scheme (symbol-append method '- (run-scheme-page)))
752 (user-partitions (append
753 (auto-partition disk #:scheme scheme)
754 (create-special-user-partitions
755 (disk-partitions disk)))))
756 (run-disk-page (list disk) user-partitions
758 ((eq? method 'manual)
759 (let* ((disks (filter-map disk-new devices))
760 (user-partitions (append-map
761 create-special-user-partitions
762 (map disk-partitions disks)))
763 (result-user-partitions (run-disk-page disks
765 result-user-partitions)))))
768 (let* ((non-install-devices (non-install-devices))
769 (user-partitions (run-page non-install-devices))
770 (user-partitions-with-pass (prompt-luks-passwords
772 (form (draw-formatting-page)))
773 ;; Make sure the disks are not in use before proceeding to formatting.
774 (free-parted non-install-devices)
775 (format-user-partitions user-partitions-with-pass)
776 (destroy-form-and-pop form)