partition.scm 34 KB

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