partition.scm 35 KB

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