partition.scm 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2018, 2019, 2022 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>
  5. ;;;
  6. ;;; This file is part of GNU Guix.
  7. ;;;
  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.
  12. ;;;
  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.
  17. ;;;
  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/>.
  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 format)
  28. #:use-module (ice-9 match)
  29. #:use-module (srfi srfi-1)
  30. #:use-module (srfi srfi-26)
  31. #:use-module (srfi srfi-34)
  32. #:use-module (srfi srfi-35)
  33. #:use-module (newt)
  34. #:use-module (parted)
  35. #:export (run-partitioning-page))
  36. (define (button-exit-action)
  37. "Abort the installer step."
  38. (abort-to-prompt 'installer-step 'abort))
  39. (define (run-scheme-page)
  40. "Run a page asking the user for a partitioning scheme."
  41. (let* ((items
  42. `((root . ,(G_ "Everything is one partition"))
  43. (root-home . ,(G_ "Separate /home partition"))))
  44. (result (run-listbox-selection-page
  45. #:info-text (G_ "Please select a partitioning scheme.")
  46. #:title (G_ "Partition scheme")
  47. #:listbox-items items
  48. #:listbox-item->text cdr
  49. #:listbox-height 4
  50. #:sort-listbox-items? #f ;keep the 'root' option first
  51. #:button-text (G_ "Exit")
  52. #:button-callback-procedure button-exit-action)))
  53. (car result)))
  54. (define (draw-formatting-page partitions)
  55. "Draw a page asking for confirmation, and then indicating that partitions
  56. are being formatted."
  57. ;; TRANSLATORS: The ~{ and ~} format specifiers are used to iterate the list
  58. ;; of device names of the user partitions that will be formatted.
  59. (run-confirmation-page (format #f (G_ "We are about to write the configured \
  60. partition table to the disk and format the partitions listed below. Their \
  61. data will be lost. Do you wish to continue?~%~%~{ - ~a~%~}")
  62. (map user-partition-file-name
  63. (filter user-partition-need-formatting?
  64. partitions)))
  65. (G_ "Format disk?")
  66. #:exit-button-procedure button-exit-action)
  67. (draw-info-page
  68. (format #f (G_ "Partition formatting is in progress, please wait."))
  69. (G_ "Preparing partitions")))
  70. (define (run-device-page devices)
  71. "Run a page asking the user to select a device among those in the given
  72. DEVICES list."
  73. (define (device-items)
  74. (map (lambda (device)
  75. `(,device . ,(device-description device)))
  76. devices))
  77. (let* ((result (run-listbox-selection-page
  78. #:info-text (G_ "Please select a \
  79. disk. The installation device as well as the small devices are filtered.")
  80. #:title (G_ "Disk")
  81. #:listbox-items (device-items)
  82. #:listbox-item->text cdr
  83. #:listbox-height 10
  84. #:button-text (G_ "Exit")
  85. #:button-callback-procedure button-exit-action))
  86. (device (car result)))
  87. device))
  88. (define (run-label-confirmation-page callback)
  89. (lambda (item)
  90. (match (current-clients)
  91. (()
  92. (and (run-confirmation-page
  93. (format #f (G_ "This will create a new ~a partition table, \
  94. all data on disk will be lost, are you sure you want to proceed?") item)
  95. (G_ "Format disk?")
  96. #:exit-button-procedure callback)
  97. item))
  98. (_ item))))
  99. (define (run-label-page button-text button-callback)
  100. "Run a page asking the user to select a partition table label."
  101. ;; Force the GPT label if UEFI is supported.
  102. (if (efi-installation?)
  103. ((run-label-confirmation-page button-callback) "gpt")
  104. (run-listbox-selection-page
  105. #:info-text (G_ "Select a new partition table type. \
  106. Be careful, all data on the disk will be lost.")
  107. #:title (G_ "Partition table")
  108. #:listbox-items '("msdos" "gpt")
  109. #:listbox-item->text identity
  110. #:listbox-callback-procedure
  111. (run-label-confirmation-page button-callback)
  112. #:button-text button-text
  113. #:button-callback-procedure button-callback)))
  114. (define (run-type-page partition)
  115. "Run a page asking the user to select a partition type."
  116. (let* ((disk (partition-disk partition))
  117. (partitions (disk-partitions disk))
  118. (other-extended-partitions?
  119. (any extended-partition? partitions))
  120. (items
  121. `(normal ,@(if other-extended-partitions?
  122. '()
  123. '(extended)))))
  124. (run-listbox-selection-page
  125. #:info-text (G_ "Please select a partition type.")
  126. #:title (G_ "Partition type")
  127. #:listbox-items items
  128. #:listbox-item->text symbol->string
  129. #:sort-listbox-items? #f
  130. #:button-text (G_ "Exit")
  131. #:button-callback-procedure button-exit-action)))
  132. (define (run-fs-type-page)
  133. "Run a page asking the user to select a file-system type."
  134. (run-listbox-selection-page
  135. #:info-text (G_ "Please select the file-system type for this partition.")
  136. #:title (G_ "File-system type")
  137. #:listbox-items '(btrfs ext4 jfs xfs
  138. swap
  139. ;; These lack basic Unix features. Their only use
  140. ;; on GNU is for interoperation, e.g., with UEFI.
  141. fat32 fat16 ntfs)
  142. #:listbox-item->text user-fs-type-name
  143. #:sort-listbox-items? #f
  144. #:button-text (G_ "Exit")
  145. #:button-callback-procedure button-exit-action))
  146. (define (inform-can-create-partition? user-partition)
  147. "Return #t if it is possible to create USER-PARTITION. This is determined by
  148. calling CAN-CREATE-PARTITION? procedure. If an exception is raised, catch it
  149. an inform the user with an appropriate error-page and return #f."
  150. (guard (c ((max-primary-exceeded? c)
  151. (run-error-page
  152. (G_ "Primary partitions count exceeded.")
  153. (G_ "Creation error"))
  154. #f)
  155. ((extended-creation-error? c)
  156. (run-error-page
  157. (G_ "Extended partition creation error.")
  158. (G_ "Creation error"))
  159. #f)
  160. ((logical-creation-error? c)
  161. (run-error-page
  162. (G_ "Logical partition creation error.")
  163. (G_ "Creation error"))
  164. #f))
  165. (can-create-partition? user-partition)))
  166. (define (prompt-luks-passwords user-partitions)
  167. "Prompt for the luks passwords of the encrypted partitions in
  168. USER-PARTITIONS list. Return this list with password fields filled-in."
  169. (map (lambda (user-part)
  170. (let* ((crypt-label (user-partition-crypt-label user-part))
  171. (file-name (user-partition-file-name user-part))
  172. (password-page
  173. (lambda ()
  174. (run-input-page
  175. (format #f (G_ "Please enter the password for the \
  176. encryption of partition ~a (label: ~a).") file-name crypt-label)
  177. (G_ "Password required")
  178. #:input-visibility-checkbox? #t)))
  179. (password-confirm-page
  180. (lambda ()
  181. (run-input-page
  182. (format #f (G_ "Please confirm the password for the \
  183. encryption of partition ~a (label: ~a).") file-name crypt-label)
  184. (G_ "Password confirmation required")
  185. #:input-visibility-checkbox? #t))))
  186. (if crypt-label
  187. (let loop ()
  188. (let ((password (password-page))
  189. (confirmation (password-confirm-page)))
  190. (if (string=? password confirmation)
  191. (user-partition
  192. (inherit user-part)
  193. (crypt-password (make-secret password)))
  194. (begin
  195. (run-error-page
  196. (G_ "Password mismatch, please try again.")
  197. (G_ "Password error"))
  198. (loop)))))
  199. user-part)))
  200. user-partitions))
  201. (define* (run-partition-page target-user-partition
  202. #:key
  203. (default-item #f))
  204. "Run a page allowing the user to edit the given TARGET-USER-PARTITION
  205. record. If the argument DEFAULT-ITEM is passed, use it to select the current
  206. listbox item. This is used to avoid the focus to switch back to the first
  207. listbox entry while calling this procedure recursively."
  208. (define (numeric-size device size)
  209. "Parse the given SIZE on DEVICE and return it."
  210. (call-with-values
  211. (lambda ()
  212. (unit-parse size device))
  213. (lambda (value range)
  214. value)))
  215. (define (numeric-size-range device size)
  216. "Parse the given SIZE on DEVICE and return the associated RANGE."
  217. (call-with-values
  218. (lambda ()
  219. (unit-parse size device))
  220. (lambda (value range)
  221. range)))
  222. (define* (fill-user-partition-geom user-part
  223. #:key
  224. device (size #f) start end)
  225. "Return the given USER-PART with the START, END and SIZE fields set to the
  226. eponym arguments. Use UNIT-FORMAT-CUSTOM to format START and END arguments as
  227. sectors on DEVICE."
  228. (user-partition
  229. (inherit user-part)
  230. (size size)
  231. (start (unit-format-custom device start UNIT-SECTOR))
  232. (end (unit-format-custom device end UNIT-SECTOR))))
  233. (define (apply-user-partition-changes user-part)
  234. "Set the name, file-system type and boot flag on the partition specified
  235. by USER-PART, if it is applicable for the partition type."
  236. (let* ((partition (user-partition-parted-object user-part))
  237. (disk (partition-disk partition))
  238. (disk-type (disk-disk-type disk))
  239. (device (disk-device disk))
  240. (has-name? (disk-type-check-feature
  241. disk-type
  242. DISK-TYPE-FEATURE-PARTITION-NAME))
  243. (name (user-partition-name user-part))
  244. (fs-type (filesystem-type-get
  245. (user-fs-type-name
  246. (user-partition-fs-type user-part))))
  247. (bootable? (user-partition-bootable? user-part))
  248. (esp? (user-partition-esp? user-part))
  249. (flag-bootable?
  250. (partition-is-flag-available? partition PARTITION-FLAG-BOOT))
  251. (flag-esp?
  252. (partition-is-flag-available? partition PARTITION-FLAG-ESP)))
  253. (when (and has-name? name)
  254. (partition-set-name partition name))
  255. (partition-set-system partition fs-type)
  256. (when flag-bootable?
  257. (partition-set-flag partition
  258. PARTITION-FLAG-BOOT
  259. (if bootable? 1 0)))
  260. (when flag-esp?
  261. (partition-set-flag partition
  262. PARTITION-FLAG-ESP
  263. (if esp? 1 0)))
  264. #t))
  265. (define (listbox-action listbox-item)
  266. (let* ((item (car listbox-item))
  267. (partition (user-partition-parted-object
  268. target-user-partition))
  269. (disk (partition-disk partition))
  270. (device (disk-device disk)))
  271. (list
  272. item
  273. (case item
  274. ((name)
  275. (let* ((old-name (user-partition-name target-user-partition))
  276. (name
  277. (run-input-page (G_ "Please enter the partition gpt name.")
  278. (G_ "Partition name")
  279. #:default-text old-name)))
  280. (user-partition
  281. (inherit target-user-partition)
  282. (name name))))
  283. ((type)
  284. (let ((new-type (run-type-page partition)))
  285. (user-partition
  286. (inherit target-user-partition)
  287. (type new-type))))
  288. ((bootable)
  289. (user-partition
  290. (inherit target-user-partition)
  291. (bootable? (not (user-partition-bootable?
  292. target-user-partition)))))
  293. ((esp?)
  294. (let ((new-esp? (not (user-partition-esp?
  295. target-user-partition))))
  296. (user-partition
  297. (inherit target-user-partition)
  298. (esp? new-esp?)
  299. (mount-point (if new-esp?
  300. (default-esp-mount-point)
  301. "")))))
  302. ((crypt-label)
  303. (let* ((label (user-partition-crypt-label
  304. target-user-partition))
  305. (new-label
  306. (and (not label)
  307. (run-input-page
  308. (G_ "Please enter the encrypted label")
  309. (G_ "Encryption label")))))
  310. (user-partition
  311. (inherit target-user-partition)
  312. (need-formatting? #t)
  313. (crypt-label new-label))))
  314. ((need-formatting?)
  315. (user-partition
  316. (inherit target-user-partition)
  317. (need-formatting?
  318. (not (user-partition-need-formatting?
  319. target-user-partition)))))
  320. ((size)
  321. (let* ((old-size (user-partition-size target-user-partition))
  322. (max-size-value (partition-length partition))
  323. (max-size (unit-format device max-size-value))
  324. (start (partition-start partition))
  325. (size (run-input-page
  326. (format #f (G_ "Please enter the size of the partition.\
  327. The maximum size is ~a.") max-size)
  328. (G_ "Partition size")
  329. #:default-text (or old-size max-size)))
  330. (size-percentage (read-percentage size))
  331. (size-value (if size-percentage
  332. (nearest-exact-integer
  333. (/ (* max-size-value size-percentage)
  334. 100))
  335. (numeric-size device size)))
  336. (end (and size-value
  337. (+ start size-value)))
  338. (size-range (numeric-size-range device size))
  339. (size-range-ok? (and size-range
  340. (< (+ start
  341. (geometry-start size-range))
  342. (partition-end partition)))))
  343. (cond
  344. ((and size-percentage (> size-percentage 100))
  345. (run-error-page
  346. (G_ "The percentage can not be superior to 100.")
  347. (G_ "Size error"))
  348. target-user-partition)
  349. ((not size-value)
  350. (run-error-page
  351. (G_ "The requested size is incorrectly formatted, or too large.")
  352. (G_ "Size error"))
  353. target-user-partition)
  354. ((not (or size-percentage size-range-ok?))
  355. (run-error-page
  356. (G_ "The request size is superior to the maximum size.")
  357. (G_ "Size error"))
  358. target-user-partition)
  359. (else
  360. (fill-user-partition-geom target-user-partition
  361. #:device device
  362. #:size size
  363. #:start start
  364. #:end end)))))
  365. ((fs-type)
  366. (let ((fs-type (run-fs-type-page)))
  367. (user-partition
  368. (inherit target-user-partition)
  369. (fs-type fs-type))))
  370. ((mount-point)
  371. (let* ((old-mount (or (user-partition-mount-point
  372. target-user-partition)
  373. ""))
  374. (mount
  375. (run-input-page
  376. (G_ "Please enter the desired mounting point for this \
  377. partition. Leave this field empty if you don't want to set a mounting point.")
  378. (G_ "Mounting point")
  379. #:default-text old-mount
  380. #:allow-empty-input? #t)))
  381. (user-partition
  382. (inherit target-user-partition)
  383. (mount-point (and (not (string=? mount ""))
  384. mount)))))))))
  385. (define (button-action)
  386. (let* ((partition (user-partition-parted-object
  387. target-user-partition))
  388. (prev-part (partition-prev partition))
  389. (disk (partition-disk partition))
  390. (device (disk-device disk))
  391. (creation? (freespace-partition? partition))
  392. (start (partition-start partition))
  393. (end (partition-end partition))
  394. (new-user-partition
  395. (if (user-partition-start target-user-partition)
  396. target-user-partition
  397. (fill-user-partition-geom target-user-partition
  398. #:device device
  399. #:start start
  400. #:end end))))
  401. ;; It the backend PARTITION has free-space type, it means we are
  402. ;; creating a new partition, otherwise, we are editing an already
  403. ;; existing PARTITION.
  404. (if creation?
  405. (let* ((ok-create-partition?
  406. (inform-can-create-partition? new-user-partition))
  407. (new-partition
  408. (and ok-create-partition?
  409. (mkpart disk
  410. new-user-partition
  411. #:previous-partition prev-part))))
  412. (and new-partition
  413. (user-partition
  414. (inherit new-user-partition)
  415. (need-formatting? #t)
  416. (file-name (partition-get-path new-partition))
  417. (disk-file-name (device-path device))
  418. (parted-object new-partition))))
  419. (and (apply-user-partition-changes new-user-partition)
  420. new-user-partition))))
  421. (let* ((items (user-partition-description target-user-partition))
  422. (partition (user-partition-parted-object
  423. target-user-partition))
  424. (disk (partition-disk partition))
  425. (device (disk-device disk))
  426. (file-name (device-path device))
  427. (number-str (partition-print-number partition))
  428. (type (user-partition-type target-user-partition))
  429. (type-str (symbol->string type))
  430. (start (unit-format device (partition-start partition)))
  431. (creation? (freespace-partition? partition))
  432. (default-item (and default-item
  433. (find (lambda (item)
  434. (eq? (car item) default-item))
  435. items)))
  436. (result
  437. (run-listbox-selection-page
  438. #:info-text
  439. (if creation?
  440. (format #f (G_ "Creating ~a partition starting at ~a of ~a.")
  441. type-str start file-name)
  442. (format #f (G_ "You are currently editing partition ~a.")
  443. number-str))
  444. #:title (if creation?
  445. (G_ "Partition creation")
  446. (G_ "Partition edit"))
  447. #:listbox-items items
  448. #:listbox-item->text cdr
  449. #:sort-listbox-items? #f
  450. #:listbox-default-item default-item
  451. #:button-text (G_ "OK")
  452. #:listbox-callback-procedure listbox-action
  453. #:button-callback-procedure button-action)))
  454. (match result
  455. ((item new-user-partition)
  456. (run-partition-page new-user-partition
  457. #:default-item item))
  458. (else result))))
  459. (define* (run-disk-page disks
  460. #:optional (user-partitions '())
  461. #:key (guided? #f))
  462. "Run a page allowing to edit the partition tables of the given DISKS. If
  463. specified, USER-PARTITIONS is a list of <user-partition> records associated to
  464. the partitions on DISKS."
  465. (define (other-logical-partitions? partitions)
  466. "Return #t if at least one of the partition in PARTITIONS list is a
  467. logical partition, return #f otherwise."
  468. (any logical-partition? partitions))
  469. (define (other-non-logical-partitions? partitions)
  470. "Return #t is at least one of the partitions in PARTITIONS list is not a
  471. logical partition, return #f otherwise."
  472. (let ((non-logical-partitions
  473. (remove logical-partition? partitions)))
  474. (or (any normal-partition? non-logical-partitions)
  475. (any freespace-partition? non-logical-partitions))))
  476. (define (add-tree-symbols partitions descriptions)
  477. "Concatenate tree symbols to the given DESCRIPTIONS list and return
  478. it. The PARTITIONS list is the list of partitions described in
  479. DESCRIPTIONS. The tree symbols are used to indicate the partition's disk and
  480. for logical partitions, the extended partition which includes them."
  481. (match descriptions
  482. (() '())
  483. ((description . rest-descriptions)
  484. (match partitions
  485. ((partition . rest-partitions)
  486. (if (null? rest-descriptions)
  487. (list (if (logical-partition? partition)
  488. (string-append " ┗━ " description)
  489. (string-append "┗━ " description)))
  490. (cons (cond
  491. ((extended-partition? partition)
  492. (if (other-non-logical-partitions? rest-partitions)
  493. (string-append "┣┳ " description)
  494. (string-append "┗┳ " description)))
  495. ((logical-partition? partition)
  496. (if (other-logical-partitions? rest-partitions)
  497. (if (other-non-logical-partitions? rest-partitions)
  498. (string-append "┃┣━ " description)
  499. (string-append " ┣━ " description))
  500. (if (other-non-logical-partitions? rest-partitions)
  501. (string-append "┃┗━ " description)
  502. (string-append " ┗━ " description))))
  503. (else
  504. (string-append "┣━ " description)))
  505. (add-tree-symbols rest-partitions
  506. rest-descriptions))))))))
  507. (define (skip-item? item)
  508. (eq? (car item) 'skip))
  509. (define (disk-items)
  510. "Return the list of strings describing DISKS."
  511. (let loop ((disks disks))
  512. (match disks
  513. (() '())
  514. ((disk . rest)
  515. (let* ((device (disk-device disk))
  516. (partitions (disk-partitions disk))
  517. (partitions*
  518. (filter-map
  519. (lambda (partition)
  520. (and (not (metadata-partition? partition))
  521. (not (small-freespace-partition? device
  522. partition))
  523. partition))
  524. partitions))
  525. (descriptions (add-tree-symbols
  526. partitions*
  527. (partitions-descriptions partitions*
  528. user-partitions)))
  529. (partition-items (map cons partitions* descriptions)))
  530. (append
  531. `((,disk . ,(device-description device disk))
  532. ,@partition-items
  533. ,@(if (null? rest)
  534. '()
  535. '((skip . ""))))
  536. (loop rest)))))))
  537. (define (remove-user-partition-by-partition user-partitions partition)
  538. "Return the USER-PARTITIONS list with the record with the given PARTITION
  539. object removed. If PARTITION is an extended partition, also remove all logical
  540. partitions from USER-PARTITIONS."
  541. (remove (lambda (p)
  542. (let ((cur-partition (user-partition-parted-object p)))
  543. (or (equal? cur-partition partition)
  544. (and (extended-partition? partition)
  545. (logical-partition? cur-partition)))))
  546. user-partitions))
  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."
  550. (remove (lambda (p)
  551. (let* ((partition (user-partition-parted-object p))
  552. (cur-disk (partition-disk partition)))
  553. (equal? cur-disk disk)))
  554. user-partitions))
  555. (define (update-user-partitions user-partitions new-user-partition)
  556. "Update or insert NEW-USER-PARTITION record in USER-PARTITIONS list
  557. depending if one of the <user-partition> record in USER-PARTITIONS has the
  558. same PARTITION object as NEW-USER-PARTITION."
  559. (let* ((partition (user-partition-parted-object new-user-partition))
  560. (user-partitions*
  561. (remove-user-partition-by-partition user-partitions
  562. partition)))
  563. (cons new-user-partition user-partitions*)))
  564. (define (button-ok-action)
  565. "Commit the modifications to all DISKS and return #t."
  566. (for-each (lambda (disk)
  567. (disk-commit disk))
  568. disks)
  569. #t)
  570. (define (listbox-action listbox-item)
  571. "A disk or a partition has been selected. If it's a disk, ask for a label
  572. to create a new partition table. If it is a partition, propose the user to
  573. edit it."
  574. (let ((item (car listbox-item)))
  575. (cond
  576. ((disk? item)
  577. (let ((label (run-label-page (G_ "Back") (const #f))))
  578. (if label
  579. (let* ((device (disk-device item))
  580. (new-disk (mklabel device label))
  581. (commit-new-disk (disk-commit new-disk))
  582. (other-disks (remove (lambda (disk)
  583. (equal? disk item))
  584. disks))
  585. (new-user-partitions
  586. (remove-user-partition-by-disk user-partitions item)))
  587. `((disks . ,(cons new-disk other-disks))
  588. (user-partitions . ,new-user-partitions)))
  589. `((disks . ,disks)
  590. (user-partitions . ,user-partitions)))))
  591. ((partition? item)
  592. (let* ((partition item)
  593. (disk (partition-disk partition))
  594. (device (disk-device disk))
  595. (existing-user-partition
  596. (find-user-partition-by-parted-object user-partitions
  597. partition))
  598. (edit-user-partition
  599. (or existing-user-partition
  600. (partition->user-partition partition))))
  601. `((disks . ,disks)
  602. (user-partitions . ,user-partitions)
  603. (edit-user-partition . ,edit-user-partition)))))))
  604. (define (hotkey-action key listbox-item)
  605. "The DELETE key has been pressed on a disk or a partition item."
  606. (let ((item (car listbox-item))
  607. (default-result
  608. `((disks . ,disks)
  609. (user-partitions . ,user-partitions))))
  610. (cond
  611. ((disk? item)
  612. (let* ((device (disk-device item))
  613. (file-name (device-path device))
  614. (info-text
  615. (format #f (G_ "Are you sure you want to delete everything on disk ~a?")
  616. file-name))
  617. (result (choice-window (G_ "Delete disk")
  618. (G_ "OK")
  619. (G_ "Exit")
  620. info-text)))
  621. (case result
  622. ((1)
  623. (disk-remove-all-partitions item)
  624. `((disks . ,disks)
  625. (user-partitions
  626. . ,(remove-user-partition-by-disk user-partitions item))))
  627. (else
  628. default-result))))
  629. ((partition? item)
  630. (if (freespace-partition? item)
  631. (begin
  632. (run-error-page (G_ "You cannot delete a free space area.")
  633. (G_ "Delete partition"))
  634. default-result)
  635. (let* ((disk (partition-disk item))
  636. (number-str (partition-print-number item))
  637. (info-text
  638. (format #f (G_ "Are you sure you want to delete partition ~a?")
  639. number-str))
  640. (result (choice-window (G_ "Delete partition")
  641. (G_ "OK")
  642. (G_ "Exit")
  643. info-text)))
  644. (case result
  645. ((1)
  646. (let ((new-user-partitions
  647. (remove-user-partition-by-partition user-partitions
  648. item)))
  649. (disk-remove-partition* disk item)
  650. `((disks . ,disks)
  651. (user-partitions . ,new-user-partitions))))
  652. (else
  653. default-result))))))))
  654. (let* ((info-text (G_ "You can change a disk's partition table by \
  655. selecting it and pressing ENTER. You can also edit a partition by selecting it \
  656. and pressing ENTER, or remove it by pressing DELETE. To create a new \
  657. partition, select a free space area and press ENTER.
  658. At least one partition must have its mounting point set to '/'."))
  659. (guided-info-text (format #f (G_ "This is the proposed \
  660. partitioning. It is still possible to edit it or to go back to install menu \
  661. by pressing the Exit button.~%~%")))
  662. (result
  663. (run-listbox-selection-page
  664. #:info-text (if guided?
  665. (string-append guided-info-text info-text)
  666. info-text)
  667. #:title (if guided?
  668. (G_ "Guided partitioning")
  669. (G_ "Manual partitioning"))
  670. #:info-textbox-width 76 ;we need a lot of room for INFO-TEXT
  671. #:listbox-height (max 5 (- (screen-rows) 30))
  672. #:listbox-items (disk-items)
  673. #:listbox-item->text cdr
  674. #:sort-listbox-items? #f
  675. #:skip-item-procedure? skip-item?
  676. #:allow-delete? #t
  677. #:button-text (G_ "OK")
  678. #:button-callback-procedure button-ok-action
  679. ;; Consider client replies equivalent to hitting the "OK" button.
  680. ;; XXX: In practice this means that clients cannot do anything but
  681. ;; approve the predefined list of partitions.
  682. #:client-callback-procedure (lambda (_) (button-ok-action))
  683. #:button2-text (G_ "Exit")
  684. #:button2-callback-procedure button-exit-action
  685. #:listbox-callback-procedure listbox-action
  686. #:hotkey-callback-procedure hotkey-action)))
  687. (if (eq? result #t)
  688. (let ((user-partitions-ok?
  689. (guard
  690. (c ((no-root-mount-point? c)
  691. (run-error-page
  692. (G_ "No root mount point found.")
  693. (G_ "Missing mount point"))
  694. #f)
  695. ((cannot-read-uuid? c)
  696. (run-error-page
  697. (format #f (G_ "Cannot read the ~a partition UUID.\
  698. You may need to format it.")
  699. (cannot-read-uuid-partition c))
  700. (G_ "Wrong partition format"))
  701. #f))
  702. (check-user-partitions user-partitions))))
  703. (if user-partitions-ok?
  704. user-partitions
  705. (run-disk-page disks user-partitions
  706. #:guided? guided?)))
  707. (let* ((result-disks (assoc-ref result 'disks))
  708. (result-user-partitions (assoc-ref result
  709. 'user-partitions))
  710. (edit-user-partition (assoc-ref result
  711. 'edit-user-partition))
  712. (can-create-partition?
  713. (and edit-user-partition
  714. (inform-can-create-partition? edit-user-partition)))
  715. (new-user-partition (and edit-user-partition
  716. can-create-partition?
  717. (run-partition-page
  718. edit-user-partition)))
  719. (new-user-partitions
  720. (if new-user-partition
  721. (update-user-partitions result-user-partitions
  722. new-user-partition)
  723. result-user-partitions)))
  724. (run-disk-page result-disks new-user-partitions
  725. #:guided? guided?)))))
  726. (define (run-partitioning-page)
  727. "Run a page asking the user for a partitioning method."
  728. (define (run-page devices)
  729. (let* ((items
  730. `((entire . ,(G_ "Guided - using the entire disk"))
  731. (entire-encrypted . ,(G_ "Guided - using the entire disk with encryption"))
  732. (manual . ,(G_ "Manual"))))
  733. (result (run-listbox-selection-page
  734. #:info-text (G_ "Please select a partitioning method.")
  735. #:title (G_ "Partitioning method")
  736. #:listbox-height (+ (length items) 2)
  737. #:listbox-items items
  738. #:listbox-item->text cdr
  739. #:sort-listbox-items? #f
  740. #:button-text (G_ "Exit")
  741. #:button-callback-procedure button-exit-action))
  742. (method (car result)))
  743. (cond
  744. ((or (eq? method 'entire)
  745. (eq? method 'entire-encrypted))
  746. (let* ((device (run-device-page devices))
  747. (disk-type (disk-probe device))
  748. (disk (if disk-type
  749. (disk-new device)
  750. (let* ((label (run-label-page
  751. (G_ "Exit")
  752. button-exit-action))
  753. (disk (mklabel device label)))
  754. (disk-commit disk)
  755. disk)))
  756. (scheme (symbol-append method '- (run-scheme-page)))
  757. (user-partitions (auto-partition! disk #:scheme scheme)))
  758. (run-disk-page (list disk) user-partitions
  759. #:guided? #t)))
  760. ((eq? method 'manual)
  761. (let* ((disks (filter-map disk-new devices))
  762. (user-partitions (append-map
  763. create-special-user-partitions
  764. (map disk-partitions disks)))
  765. (result-user-partitions (run-disk-page disks
  766. user-partitions)))
  767. result-user-partitions)))))
  768. (init-parted)
  769. (let* ((eligible-devices (eligible-devices))
  770. (user-partitions (run-page eligible-devices))
  771. (user-partitions-with-pass (prompt-luks-passwords
  772. user-partitions))
  773. (form (draw-formatting-page user-partitions-with-pass)))
  774. ;; Make sure the disks are not in use before proceeding to formatting.
  775. (free-parted eligible-devices)
  776. (format-user-partitions user-partitions-with-pass)
  777. (installer-log-line "formatted ~a user partitions"
  778. (length user-partitions-with-pass))
  779. (installer-log-line "user-partitions: ~a" user-partitions-with-pass)
  780. (destroy-form-and-pop form)
  781. user-partitions-with-pass))