partition.scm 34 KB

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