partition.scm 35 KB

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