parted.scm 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2018, 2019 Mathieu Othacehe <m.othacehe@gmail.com>
  3. ;;;
  4. ;;; This file is part of GNU Guix.
  5. ;;;
  6. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  7. ;;; under the terms of the GNU General Public License as published by
  8. ;;; the Free Software Foundation; either version 3 of the License, or (at
  9. ;;; your option) any later version.
  10. ;;;
  11. ;;; GNU Guix is distributed in the hope that it will be useful, but
  12. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;;; GNU General Public License for more details.
  15. ;;;
  16. ;;; You should have received a copy of the GNU General Public License
  17. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  18. (define-module (gnu installer parted)
  19. #:use-module (gnu installer steps)
  20. #:use-module (gnu installer utils)
  21. #:use-module (gnu installer newt page)
  22. #:use-module (gnu system uuid)
  23. #:use-module ((gnu build file-systems)
  24. #:select (read-partition-uuid
  25. read-luks-partition-uuid))
  26. #:use-module (guix build syscalls)
  27. #:use-module (guix build utils)
  28. #:use-module (guix records)
  29. #:use-module (guix utils)
  30. #:use-module (guix i18n)
  31. #:use-module (parted)
  32. #:use-module (ice-9 match)
  33. #:use-module (ice-9 regex)
  34. #:use-module (rnrs io ports)
  35. #:use-module (srfi srfi-1)
  36. #:use-module (srfi srfi-26)
  37. #:use-module (srfi srfi-34)
  38. #:use-module (srfi srfi-35)
  39. #:export (<user-partition>
  40. user-partition
  41. make-user-partition
  42. user-partition?
  43. user-partition-name
  44. user-partition-type
  45. user-partition-file-name
  46. user-partition-disk-file-name
  47. user-partition-crypt-label
  48. user-partition-crypt-password
  49. user-partition-fs-type
  50. user-partition-bootable?
  51. user-partition-esp?
  52. user-partition-bios-grub?
  53. user-partition-size
  54. user-partition-start
  55. user-partition-end
  56. user-partition-mount-point
  57. user-partition-need-formatting?
  58. user-partition-parted-object
  59. find-esp-partition
  60. data-partition?
  61. metadata-partition?
  62. freespace-partition?
  63. small-freespace-partition?
  64. normal-partition?
  65. extended-partition?
  66. logical-partition?
  67. esp-partition?
  68. boot-partition?
  69. default-esp-mount-point
  70. with-delay-device-in-use?
  71. force-device-sync
  72. non-install-devices
  73. partition-user-type
  74. user-fs-type-name
  75. partition-filesystem-user-type
  76. partition-get-flags
  77. partition->user-partition
  78. create-special-user-partitions
  79. find-user-partition-by-parted-object
  80. device-description
  81. partition-end-formatted
  82. partition-print-number
  83. partition-description
  84. partitions-descriptions
  85. user-partition-description
  86. &max-primary-exceeded
  87. max-primary-exceeded?
  88. &extended-creation-error
  89. extended-creation-error?
  90. &logical-creation-error
  91. logical-creation-error?
  92. can-create-partition?
  93. mklabel
  94. mkpart
  95. rmpart
  96. create-adjacent-partitions
  97. auto-partition
  98. &no-root-mount-point
  99. no-root-mount-point?
  100. check-user-partitions
  101. set-user-partitions-file-name
  102. format-user-partitions
  103. mount-user-partitions
  104. umount-user-partitions
  105. with-mounted-partitions
  106. user-partitions->file-systems
  107. user-partitions->configuration
  108. init-parted
  109. free-parted))
  110. ;;;
  111. ;;; Partition record.
  112. ;;;
  113. (define-record-type* <user-partition>
  114. user-partition make-user-partition
  115. user-partition?
  116. (name user-partition-name ;string
  117. (default #f))
  118. (type user-partition-type
  119. (default 'normal)) ; 'normal | 'logical | 'extended
  120. (file-name user-partition-file-name
  121. (default #f))
  122. (disk-file-name user-partition-disk-file-name
  123. (default #f))
  124. (crypt-label user-partition-crypt-label
  125. (default #f))
  126. (crypt-password user-partition-crypt-password
  127. (default #f))
  128. (fs-type user-partition-fs-type
  129. (default 'ext4))
  130. (bootable? user-partition-bootable?
  131. (default #f))
  132. (esp? user-partition-esp?
  133. (default #f))
  134. (bios-grub? user-partition-bios-grub?
  135. (default #f))
  136. (size user-partition-size
  137. (default #f))
  138. (start user-partition-start ;start as string (e.g. '11MB')
  139. (default #f))
  140. (end user-partition-end ;same as start
  141. (default #f))
  142. (mount-point user-partition-mount-point ;string
  143. (default #f))
  144. (need-formatting? user-partition-need-formatting? ; boolean
  145. (default #f))
  146. (parted-object user-partition-parted-object ; <partition> from parted
  147. (default #f)))
  148. ;;
  149. ;; Utilities.
  150. ;;
  151. (define (find-esp-partition partitions)
  152. "Find and return the ESP partition among PARTITIONS."
  153. (find esp-partition? partitions))
  154. (define (data-partition? partition)
  155. "Return #t if PARTITION is a partition dedicated to data (by opposition to
  156. freespace, metadata and protected partition types), return #f otherwise."
  157. (let ((type (partition-type partition)))
  158. (not (any (lambda (flag)
  159. (member flag type))
  160. '(free-space metadata protected)))))
  161. (define (metadata-partition? partition)
  162. "Return #t if PARTITION is a metadata partition, #f otherwise."
  163. (let ((type (partition-type partition)))
  164. (member 'metadata type)))
  165. (define (freespace-partition? partition)
  166. "Return #t if PARTITION is a free-space partition, #f otherwise."
  167. (let ((type (partition-type partition)))
  168. (member 'free-space type)))
  169. (define* (small-freespace-partition? device
  170. partition
  171. #:key (max-size MEBIBYTE-SIZE))
  172. "Return #t is PARTITION is a free-space partition with less a size strictly
  173. inferior to MAX-SIZE, #f otherwise."
  174. (let ((size (partition-length partition))
  175. (max-sector-size (/ max-size
  176. (device-sector-size device))))
  177. (< size max-sector-size)))
  178. (define (normal-partition? partition)
  179. "return #t if partition is a normal partition, #f otherwise."
  180. (let ((type (partition-type partition)))
  181. (member 'normal type)))
  182. (define (extended-partition? partition)
  183. "return #t if partition is an extended partition, #f otherwise."
  184. (let ((type (partition-type partition)))
  185. (member 'extended type)))
  186. (define (logical-partition? partition)
  187. "Return #t if PARTITION is a logical partition, #f otherwise."
  188. (let ((type (partition-type partition)))
  189. (member 'logical type)))
  190. (define (partition-user-type partition)
  191. "Return the type of PARTITION, to be stored in the TYPE field of
  192. <user-partition> record. It can be 'normal, 'extended or 'logical."
  193. (cond ((normal-partition? partition)
  194. 'normal)
  195. ((extended-partition? partition)
  196. 'extended)
  197. ((logical-partition? partition)
  198. 'logical)
  199. (else #f)))
  200. (define (esp-partition? partition)
  201. "Return #t if partition has the ESP flag, return #f otherwise."
  202. (let* ((disk (partition-disk partition))
  203. (disk-type (disk-disk-type disk))
  204. (has-extended? (disk-type-check-feature
  205. disk-type
  206. DISK-TYPE-FEATURE-EXTENDED)))
  207. (and (data-partition? partition)
  208. (not has-extended?)
  209. (partition-is-flag-available? partition PARTITION-FLAG-ESP)
  210. (partition-get-flag partition PARTITION-FLAG-ESP))))
  211. (define (boot-partition? partition)
  212. "Return #t if partition has the boot flag, return #f otherwise."
  213. (and (data-partition? partition)
  214. (partition-is-flag-available? partition PARTITION-FLAG-BOOT)
  215. (partition-get-flag partition PARTITION-FLAG-BOOT)))
  216. ;; The default mount point for ESP partitions.
  217. (define default-esp-mount-point
  218. (make-parameter "/boot/efi"))
  219. (define (efi-installation?)
  220. "Return #t if an EFI installation should be performed, #f otherwise."
  221. (file-exists? "/sys/firmware/efi"))
  222. (define (user-fs-type-name fs-type)
  223. "Return the name of FS-TYPE as specified by libparted."
  224. (case fs-type
  225. ((ext4) "ext4")
  226. ((btrfs) "btrfs")
  227. ((fat32) "fat32")
  228. ((swap) "linux-swap")))
  229. (define (user-fs-type->mount-type fs-type)
  230. "Return the mount type of FS-TYPE."
  231. (case fs-type
  232. ((ext4) "ext4")
  233. ((btrfs) "btrfs")
  234. ((fat32) "vfat")))
  235. (define (partition-filesystem-user-type partition)
  236. "Return the filesystem type of PARTITION, to be stored in the FS-TYPE field
  237. of <user-partition> record."
  238. (let ((fs-type (partition-fs-type partition)))
  239. (and fs-type
  240. (let ((name (filesystem-type-name fs-type)))
  241. (cond
  242. ((string=? name "ext4") 'ext4)
  243. ((string=? name "btrfs") 'btrfs)
  244. ((string=? name "fat32") 'fat32)
  245. ((or (string=? name "swsusp")
  246. (string=? name "linux-swap(v0)")
  247. (string=? name "linux-swap(v1)"))
  248. 'swap)
  249. (else
  250. (error (format #f "Unhandled ~a fs-type~%" name))))))))
  251. (define (partition-get-flags partition)
  252. "Return the list of flags supported by the given PARTITION."
  253. (filter-map (lambda (flag)
  254. (and (partition-get-flag partition flag)
  255. flag))
  256. (partition-flags partition)))
  257. (define (partition->user-partition partition)
  258. "Convert PARTITION into a <user-partition> record and return it."
  259. (let* ((disk (partition-disk partition))
  260. (device (disk-device disk))
  261. (disk-type (disk-disk-type disk))
  262. (has-name? (disk-type-check-feature
  263. disk-type
  264. DISK-TYPE-FEATURE-PARTITION-NAME))
  265. (name (and has-name?
  266. (data-partition? partition)
  267. (partition-get-name partition))))
  268. (user-partition
  269. (name (and (and name
  270. (not (string=? name "")))
  271. name))
  272. (type (or (partition-user-type partition)
  273. 'normal))
  274. (file-name (partition-get-path partition))
  275. (disk-file-name (device-path device))
  276. (fs-type (or (partition-filesystem-user-type partition)
  277. 'ext4))
  278. (mount-point (and (esp-partition? partition)
  279. (default-esp-mount-point)))
  280. (bootable? (boot-partition? partition))
  281. (esp? (esp-partition? partition))
  282. (parted-object partition))))
  283. (define (create-special-user-partitions partitions)
  284. "Return a list with a <user-partition> record describing the ESP partition
  285. found in PARTITIONS, if any."
  286. (filter-map (lambda (partition)
  287. (and (esp-partition? partition)
  288. (partition->user-partition partition)))
  289. partitions))
  290. (define (find-user-partition-by-parted-object user-partitions
  291. partition)
  292. "Find and return the <user-partition> record in USER-PARTITIONS list which
  293. PARTED-OBJECT field equals PARTITION, return #f if not found."
  294. (find (lambda (user-partition)
  295. (equal? (user-partition-parted-object user-partition)
  296. partition))
  297. user-partitions))
  298. ;;
  299. ;; Devices
  300. ;;
  301. (define (with-delay-device-in-use? file-name)
  302. "Call DEVICE-IN-USE? with a few retries, as the first re-read will often
  303. fail. See rereadpt function in wipefs.c of util-linux for an explanation."
  304. ;; Kernel always return EINVAL for BLKRRPART on loopdevices.
  305. (and (not (string-match "/dev/loop*" file-name))
  306. (let loop ((try 4))
  307. (usleep 250000)
  308. (let ((in-use? (device-in-use? file-name)))
  309. (if (and in-use? (> try 0))
  310. (loop (- try 1))
  311. in-use?)))))
  312. (define* (force-device-sync device)
  313. "Force a flushing of the given DEVICE."
  314. (device-open device)
  315. (device-sync device)
  316. (device-close device))
  317. (define (non-install-devices)
  318. "Return all the available devices, except the busy one, allegedly the
  319. install device. DEVICE-IS-BUSY? is a parted call, checking if the device is
  320. mounted. The install image uses an overlayfs so the install device does not
  321. appear as mounted and won't be considered as busy. So use also DEVICE-IN-USE?
  322. from (guix build syscalls) module, who will try to re-read the device's
  323. partition table to determine whether or not it is already used (like sfdisk
  324. from util-linux)."
  325. (remove (lambda (device)
  326. (let ((file-name (device-path device)))
  327. (or (device-is-busy? device)
  328. (with-delay-device-in-use? file-name))))
  329. (devices)))
  330. ;;
  331. ;; Disk and partition printing.
  332. ;;
  333. (define* (device-description device #:optional disk)
  334. "Return a string describing the given DEVICE."
  335. (let* ((type (device-type device))
  336. (file-name (device-path device))
  337. (model (device-model device))
  338. (type-str (device-type->string type))
  339. (disk-type (if disk
  340. (disk-disk-type disk)
  341. (disk-probe device)))
  342. (length (device-length device))
  343. (sector-size (device-sector-size device))
  344. (end (unit-format-custom-byte device
  345. (* length sector-size)
  346. UNIT-GIGABYTE)))
  347. (string-join
  348. `(,@(if (string=? model "")
  349. `(,type-str)
  350. `(,model ,(string-append "(" type-str ")")))
  351. ,file-name
  352. ,end
  353. ,@(if disk-type
  354. `(,(disk-type-name disk-type))
  355. '()))
  356. " ")))
  357. (define (partition-end-formatted device partition)
  358. "Return as a string the end of PARTITION with the relevant unit."
  359. (unit-format-byte
  360. device
  361. (-
  362. (* (+ (partition-end partition) 1)
  363. (device-sector-size device))
  364. 1)))
  365. (define (partition-print-number partition)
  366. "Convert the given partition NUMBER to string."
  367. (let ((number (partition-number partition)))
  368. (number->string number)))
  369. (define (partition-description partition user-partition)
  370. "Return a string describing the given PARTITION, located on the DISK of
  371. DEVICE."
  372. (define (partition-print-type partition)
  373. "Return the type of PARTITION as a string."
  374. (if (freespace-partition? partition)
  375. (G_ "Free space")
  376. (let ((type (partition-type partition)))
  377. (match type
  378. ((type-symbol)
  379. (symbol->string type-symbol))))))
  380. (define (partition-print-flags partition)
  381. "Return the flags of PARTITION as a string of comma separated flags."
  382. (string-join
  383. (filter-map
  384. (lambda (flag)
  385. (and (partition-get-flag partition flag)
  386. (partition-flag-get-name flag)))
  387. (partition-flags partition))
  388. ","))
  389. (define (maybe-string-pad string length)
  390. "Returned a string formatted by padding STRING of LENGTH characters to the
  391. right. If STRING is #f use an empty string."
  392. (if (and string (not (string=? string "")))
  393. (string-pad-right string length)
  394. ""))
  395. (let* ((disk (partition-disk partition))
  396. (device (disk-device disk))
  397. (disk-type (disk-disk-type disk))
  398. (has-name? (disk-type-check-feature
  399. disk-type
  400. DISK-TYPE-FEATURE-PARTITION-NAME))
  401. (has-extended? (disk-type-check-feature
  402. disk-type
  403. DISK-TYPE-FEATURE-EXTENDED))
  404. (part-type (partition-print-type partition))
  405. (number (and (not (freespace-partition? partition))
  406. (partition-print-number partition)))
  407. (name (and has-name?
  408. (if (freespace-partition? partition)
  409. (G_ "Free space")
  410. (partition-get-name partition))))
  411. (start (unit-format device
  412. (partition-start partition)))
  413. (end (partition-end-formatted device partition))
  414. (size (unit-format device (partition-length partition)))
  415. (fs-type (partition-fs-type partition))
  416. (fs-type-name (and fs-type
  417. (filesystem-type-name fs-type)))
  418. (crypt-label (and user-partition
  419. (user-partition-crypt-label user-partition)))
  420. (flags (and (not (freespace-partition? partition))
  421. (partition-print-flags partition)))
  422. (mount-point (and user-partition
  423. (user-partition-mount-point user-partition))))
  424. `(,(or number "")
  425. ,@(if has-extended?
  426. (list part-type)
  427. '())
  428. ,size
  429. ,(or fs-type-name "")
  430. ,(or flags "")
  431. ,(or mount-point "")
  432. ,(or crypt-label "")
  433. ,(maybe-string-pad name 30))))
  434. (define (partitions-descriptions partitions user-partitions)
  435. "Return a list of strings describing all the partitions found on
  436. DEVICE. METADATA partitions are not described. The strings are padded to the
  437. right so that they can be displayed as a table."
  438. (define (max-length-column lists column-index)
  439. "Return the maximum length of the string at position COLUMN-INDEX in the
  440. list of string lists LISTS."
  441. (apply max
  442. (map (lambda (list)
  443. (string-length
  444. (list-ref list column-index)))
  445. lists)))
  446. (define (pad-descriptions descriptions)
  447. "Return a padded version of the list of string lists DESCRIPTIONS. The
  448. strings are padded to the length of the longer string in a same column, as
  449. determined by MAX-LENGTH-COLUMN procedure."
  450. (let* ((description-length (length (car descriptions)))
  451. (paddings (map (lambda (index)
  452. (max-length-column descriptions index))
  453. (iota description-length))))
  454. (map (lambda (description)
  455. (map string-pad-right description paddings))
  456. descriptions)))
  457. (let* ((descriptions
  458. (map
  459. (lambda (partition)
  460. (let ((user-partition
  461. (find-user-partition-by-parted-object user-partitions
  462. partition)))
  463. (partition-description partition user-partition)))
  464. partitions))
  465. (padded-descriptions (if (null? partitions)
  466. '()
  467. (pad-descriptions descriptions))))
  468. (map (cut string-join <> " ") padded-descriptions)))
  469. (define (user-partition-description user-partition)
  470. "Return a string describing the given USER-PARTITION record."
  471. (let* ((partition (user-partition-parted-object user-partition))
  472. (disk (partition-disk partition))
  473. (disk-type (disk-disk-type disk))
  474. (device (disk-device disk))
  475. (has-name? (disk-type-check-feature
  476. disk-type
  477. DISK-TYPE-FEATURE-PARTITION-NAME))
  478. (has-extended? (disk-type-check-feature
  479. disk-type
  480. DISK-TYPE-FEATURE-EXTENDED))
  481. (name (user-partition-name user-partition))
  482. (type (user-partition-type user-partition))
  483. (type-name (symbol->string type))
  484. (fs-type (user-partition-fs-type user-partition))
  485. (fs-type-name (user-fs-type-name fs-type))
  486. (bootable? (user-partition-bootable? user-partition))
  487. (esp? (user-partition-esp? user-partition))
  488. (need-formatting? (user-partition-need-formatting? user-partition))
  489. (crypt-label (user-partition-crypt-label user-partition))
  490. (size (user-partition-size user-partition))
  491. (mount-point (user-partition-mount-point user-partition)))
  492. `(,@(if has-name?
  493. `((name . ,(string-append "Name: " (or name "None"))))
  494. '())
  495. ,@(if (and has-extended?
  496. (freespace-partition? partition)
  497. (not (eq? type 'logical)))
  498. `((type . ,(string-append "Type: " type-name)))
  499. '())
  500. ,@(if (eq? type 'extended)
  501. '()
  502. `((fs-type . ,(string-append "Filesystem type: " fs-type-name))))
  503. ,@(if (or (eq? type 'extended)
  504. (eq? fs-type 'swap)
  505. (not has-extended?))
  506. '()
  507. `((bootable . ,(string-append "Bootable flag: "
  508. (if bootable? "On" "Off")))))
  509. ,@(if (and (not has-extended?)
  510. (not (eq? fs-type 'swap)))
  511. `((esp? . ,(string-append "ESP flag: "
  512. (if esp? "On" "Off"))))
  513. '())
  514. ,@(if (freespace-partition? partition)
  515. (let ((size-formatted
  516. (or size (unit-format device
  517. (partition-length partition)))))
  518. `((size . ,(string-append "Size : " size-formatted))))
  519. '())
  520. ,@(if (or (eq? type 'extended)
  521. (eq? fs-type 'swap))
  522. '()
  523. `((crypt-label
  524. . ,(string-append
  525. "Encryption: "
  526. (if crypt-label
  527. (format #f "Yes (label ~a)" crypt-label)
  528. "No")))))
  529. ,@(if (or (freespace-partition? partition)
  530. (eq? fs-type 'swap))
  531. '()
  532. `((need-formatting?
  533. . ,(string-append "Format the partition? : "
  534. (if need-formatting? "Yes" "No")))))
  535. ,@(if (or (eq? type 'extended)
  536. (eq? fs-type 'swap))
  537. '()
  538. `((mount-point
  539. . ,(string-append "Mount point : "
  540. (or mount-point
  541. (and esp? (default-esp-mount-point))
  542. "None"))))))))
  543. ;;
  544. ;; Partition table creation.
  545. ;;
  546. (define (mklabel device type-name)
  547. "Create a partition table on DEVICE. TYPE-NAME is the type of the partition
  548. table, \"msdos\" or \"gpt\"."
  549. (let ((type (disk-type-get type-name)))
  550. (disk-new-fresh device type)))
  551. ;;
  552. ;; Partition creation.
  553. ;;
  554. ;; The maximum count of primary partitions is exceeded.
  555. (define-condition-type &max-primary-exceeded &condition
  556. max-primary-exceeded?)
  557. ;; It is not possible to create an extended partition.
  558. (define-condition-type &extended-creation-error &condition
  559. extended-creation-error?)
  560. ;; It is not possible to create a logical partition.
  561. (define-condition-type &logical-creation-error &condition
  562. logical-creation-error?)
  563. (define (can-create-primary? disk)
  564. "Return #t if it is possible to create a primary partition on DISK, return
  565. #f otherwise."
  566. (let ((max-primary (disk-get-max-primary-partition-count disk)))
  567. (find (lambda (number)
  568. (not (disk-get-partition disk number)))
  569. (iota max-primary 1))))
  570. (define (can-create-extended? disk)
  571. "Return #t if it is possible to create an extended partition on DISK, return
  572. #f otherwise."
  573. (let* ((disk-type (disk-disk-type disk))
  574. (has-extended? (disk-type-check-feature
  575. disk-type
  576. DISK-TYPE-FEATURE-EXTENDED)))
  577. (and (can-create-primary? disk)
  578. has-extended?
  579. (not (disk-extended-partition disk)))))
  580. (define (can-create-logical? disk)
  581. "Return #t is it is possible to create a logical partition on DISK, return
  582. #f otherwise."
  583. (let* ((disk-type (disk-disk-type disk))
  584. (has-extended? (disk-type-check-feature
  585. disk-type
  586. DISK-TYPE-FEATURE-EXTENDED)))
  587. (and has-extended?
  588. (disk-extended-partition disk))))
  589. (define (can-create-partition? user-part)
  590. "Return #t if it is possible to create the given USER-PART record, return #f
  591. otherwise."
  592. (let* ((type (user-partition-type user-part))
  593. (partition (user-partition-parted-object user-part))
  594. (disk (partition-disk partition)))
  595. (case type
  596. ((normal)
  597. (or (can-create-primary? disk)
  598. (raise
  599. (condition (&max-primary-exceeded)))))
  600. ((extended)
  601. (or (can-create-extended? disk)
  602. (raise
  603. (condition (&extended-creation-error)))))
  604. ((logical)
  605. (or (can-create-logical? disk)
  606. (raise
  607. (condition (&logical-creation-error))))))))
  608. (define* (mkpart disk user-partition
  609. #:key (previous-partition #f))
  610. "Create the given USER-PARTITION on DISK. The PREVIOUS-PARTITION argument as
  611. to be set to the partition preceding USER-PARTITION if any."
  612. (define (parse-start-end start end)
  613. "Parse start and end strings as positions on DEVICE expressed with a unit,
  614. like '100GB' or '12.2KiB'. Return a list of 4 elements, the start sector, its
  615. range (1 unit large area centered on start sector), the end sector and its
  616. range."
  617. (let ((device (disk-device disk)))
  618. (call-with-values
  619. (lambda ()
  620. (unit-parse start device))
  621. (lambda (start-sector start-range)
  622. (call-with-values
  623. (lambda ()
  624. (unit-parse end device))
  625. (lambda (end-sector end-range)
  626. (list start-sector start-range
  627. end-sector end-range)))))))
  628. (define* (extend-ranges! start-range end-range
  629. #:key (offset 0))
  630. "Try to extend START-RANGE by 1 MEBIBYTE to the right and END-RANGE by 1
  631. MEBIBYTE to the left. This way, if the disk is aligned on 2048 sectors of
  632. 512KB (like frequently), we will have a chance for the
  633. 'optimal-align-constraint' to succeed. Do not extend ranges if that would
  634. cause them to cross."
  635. (let* ((device (disk-device disk))
  636. (start-range-end (geometry-end start-range))
  637. (end-range-start (geometry-start end-range))
  638. (mebibyte-sector-size (/ MEBIBYTE-SIZE
  639. (device-sector-size device)))
  640. (new-start-range-end
  641. (+ start-range-end mebibyte-sector-size offset))
  642. (new-end-range-start
  643. (- end-range-start mebibyte-sector-size offset)))
  644. (when (< new-start-range-end new-end-range-start)
  645. (geometry-set-end start-range new-start-range-end)
  646. (geometry-set-start end-range new-end-range-start))))
  647. (match (parse-start-end (user-partition-start user-partition)
  648. (user-partition-end user-partition))
  649. ((start-sector start-range end-sector end-range)
  650. (let* ((prev-end (if previous-partition
  651. (partition-end previous-partition)
  652. 0))
  653. (start-distance (- start-sector prev-end))
  654. (type (user-partition-type user-partition))
  655. ;; There should be at least 2 unallocated sectors in front of each
  656. ;; logical partition, otherwise parted will fail badly:
  657. ;; https://gparted.org/h2-fix-msdos-pt.php#apply-action-fail.
  658. (start-offset (if previous-partition
  659. (- 3 start-distance)
  660. 0))
  661. (start-sector* (if (and (eq? type 'logical)
  662. (< start-distance 3))
  663. (+ start-sector start-offset)
  664. start-sector)))
  665. ;; This is a hack. Parted almost always fails to create optimally
  666. ;; aligned partitions (unless specifying percentages) because the
  667. ;; default range of 1MB centered on the start sector is not enough when
  668. ;; the optimal alignment is 2048 sectors of 512KB.
  669. (extend-ranges! start-range end-range #:offset start-offset)
  670. (let* ((device (disk-device disk))
  671. (disk-type (disk-disk-type disk))
  672. (length (device-length device))
  673. (name (user-partition-name user-partition))
  674. (filesystem-type
  675. (filesystem-type-get
  676. (user-fs-type-name
  677. (user-partition-fs-type user-partition))))
  678. (flags `(,@(if (user-partition-bootable? user-partition)
  679. `(,PARTITION-FLAG-BOOT)
  680. '())
  681. ,@(if (user-partition-esp? user-partition)
  682. `(,PARTITION-FLAG-ESP)
  683. '())
  684. ,@(if (user-partition-bios-grub? user-partition)
  685. `(,PARTITION-FLAG-BIOS-GRUB)
  686. '())))
  687. (has-name? (disk-type-check-feature
  688. disk-type
  689. DISK-TYPE-FEATURE-PARTITION-NAME))
  690. (partition-type (partition-type->int type))
  691. (partition (partition-new disk
  692. #:type partition-type
  693. #:filesystem-type filesystem-type
  694. #:start start-sector*
  695. #:end end-sector))
  696. (user-constraint (constraint-new
  697. #:start-align 'any
  698. #:end-align 'any
  699. #:start-range start-range
  700. #:end-range end-range
  701. #:min-size 1
  702. #:max-size length))
  703. (dev-constraint
  704. (device-get-optimal-aligned-constraint device))
  705. (final-constraint (constraint-intersect user-constraint
  706. dev-constraint))
  707. (no-constraint (constraint-any device))
  708. ;; Try to create a partition with an optimal alignment
  709. ;; constraint. If it fails, fallback to creating a partition with
  710. ;; no specific constraint.
  711. (partition-ok?
  712. (or (disk-add-partition disk partition final-constraint)
  713. (disk-add-partition disk partition no-constraint))))
  714. ;; Set the partition name if supported.
  715. (when (and partition-ok? has-name? name)
  716. (partition-set-name partition name))
  717. ;; Set flags is required.
  718. (for-each (lambda (flag)
  719. (and (partition-is-flag-available? partition flag)
  720. (partition-set-flag partition flag 1)))
  721. flags)
  722. (and partition-ok?
  723. (partition-set-system partition filesystem-type)
  724. partition))))))
  725. ;;
  726. ;; Partition destruction.
  727. ;;
  728. (define (rmpart disk number)
  729. "Remove the partition with the given NUMBER on DISK."
  730. (let ((partition (disk-get-partition disk number)))
  731. (disk-remove-partition disk partition)))
  732. ;;
  733. ;; Auto partitionning.
  734. ;;
  735. (define* (create-adjacent-partitions disk partitions
  736. #:key (last-partition-end 0))
  737. "Create the given PARTITIONS on DISK. LAST-PARTITION-END is the sector from
  738. which we want to start creating partitions. The START and END of each created
  739. partition are computed from its SIZE value and the position of the last
  740. partition."
  741. (let ((device (disk-device disk)))
  742. (let loop ((partitions partitions)
  743. (remaining-space (- (device-length device)
  744. last-partition-end))
  745. (start last-partition-end))
  746. (match partitions
  747. (() '())
  748. ((partition . rest)
  749. (let* ((size (user-partition-size partition))
  750. (percentage-size (and (string? size)
  751. (read-percentage size)))
  752. (sector-size (device-sector-size device))
  753. (partition-size (if percentage-size
  754. (exact->inexact
  755. (* (/ percentage-size 100)
  756. remaining-space))
  757. size))
  758. (end-partition (min (- (device-length device) 1)
  759. (nearest-exact-integer
  760. (+ start partition-size 1))))
  761. (name (user-partition-name partition))
  762. (type (user-partition-type partition))
  763. (fs-type (user-partition-fs-type partition))
  764. (start-formatted (unit-format-custom device
  765. start
  766. UNIT-SECTOR))
  767. (end-formatted (unit-format-custom device
  768. end-partition
  769. UNIT-SECTOR))
  770. (new-user-partition (user-partition
  771. (inherit partition)
  772. (start start-formatted)
  773. (end end-formatted)))
  774. (new-partition
  775. (mkpart disk new-user-partition)))
  776. (if new-partition
  777. (cons (user-partition
  778. (inherit new-user-partition)
  779. (file-name (partition-get-path new-partition))
  780. (disk-file-name (device-path device))
  781. (parted-object new-partition))
  782. (loop rest
  783. (if (eq? type 'extended)
  784. remaining-space
  785. (- remaining-space
  786. (partition-length new-partition)))
  787. (if (eq? type 'extended)
  788. (+ start 1)
  789. (+ (partition-end new-partition) 1))))
  790. (error
  791. (format #f "Unable to create partition ~a~%" name)))))))))
  792. (define (force-user-partitions-formatting user-partitions)
  793. "Set the NEED-FORMATING? fields to #t on all <user-partition> records of
  794. USER-PARTITIONS list and return the updated list."
  795. (map (lambda (p)
  796. (user-partition
  797. (inherit p)
  798. (need-formatting? #t)))
  799. user-partitions))
  800. (define* (auto-partition disk
  801. #:key
  802. (scheme 'entire-root))
  803. "Automatically create partitions on DISK. All the previous
  804. partitions (except the ESP on a GPT disk, if present) are wiped. SCHEME is the
  805. desired partitioning scheme. It can be 'entire-root or
  806. 'entire-root-home. 'entire-root will create a swap partition and a root
  807. partition occupying all the remaining space. 'entire-root-home will create a
  808. swap partition, a root partition and a home partition."
  809. (let* ((device (disk-device disk))
  810. (disk-type (disk-disk-type disk))
  811. (has-extended? (disk-type-check-feature
  812. disk-type
  813. DISK-TYPE-FEATURE-EXTENDED))
  814. (partitions (filter data-partition? (disk-partitions disk)))
  815. (esp-partition (find-esp-partition partitions))
  816. ;; According to
  817. ;; https://wiki.archlinux.org/index.php/EFI_system_partition, the ESP
  818. ;; size should be at least 550MiB.
  819. (new-esp-size (nearest-exact-integer
  820. (/ (* 550 MEBIBYTE-SIZE)
  821. (device-sector-size device))))
  822. (end-esp-partition (and esp-partition
  823. (partition-end esp-partition)))
  824. (non-boot-partitions (remove esp-partition? partitions))
  825. (bios-grub-size (/ (* 3 MEBIBYTE-SIZE)
  826. (device-sector-size device)))
  827. (five-percent-disk (nearest-exact-integer
  828. (* 0.05 (device-length device))))
  829. (default-swap-size (nearest-exact-integer
  830. (/ (* 4 GIGABYTE-SIZE)
  831. (device-sector-size device))))
  832. ;; Use a 4GB size for the swap if it represents less than 5% of the
  833. ;; disk space. Otherwise, set the swap size to 5% of the disk space.
  834. (swap-size (min default-swap-size five-percent-disk)))
  835. (if has-extended?
  836. ;; msdos - remove everything.
  837. (disk-delete-all disk)
  838. ;; gpt - remove everything but esp if it exists.
  839. (for-each
  840. (lambda (partition)
  841. (and (data-partition? partition)
  842. (disk-remove-partition disk partition)))
  843. non-boot-partitions))
  844. (let* ((start-partition
  845. (and (not has-extended?)
  846. (not esp-partition)
  847. (if (efi-installation?)
  848. (user-partition
  849. (fs-type 'fat32)
  850. (esp? #t)
  851. (size new-esp-size)
  852. (mount-point (default-esp-mount-point)))
  853. (user-partition
  854. (fs-type 'ext4)
  855. (bootable? #t)
  856. (bios-grub? #t)
  857. (size bios-grub-size)))))
  858. (new-partitions
  859. (cond
  860. ((or (eq? scheme 'entire-root)
  861. (eq? scheme 'entire-encrypted-root))
  862. (let ((encrypted? (eq? scheme 'entire-encrypted-root)))
  863. `(,@(if start-partition
  864. `(,start-partition)
  865. '())
  866. ,@(if encrypted?
  867. '()
  868. `(,(user-partition
  869. (fs-type 'swap)
  870. (size swap-size))))
  871. ,(user-partition
  872. (fs-type 'ext4)
  873. (bootable? has-extended?)
  874. (crypt-label (and encrypted? "cryptroot"))
  875. (size "100%")
  876. (mount-point "/")))))
  877. ((or (eq? scheme 'entire-root-home)
  878. (eq? scheme 'entire-encrypted-root-home))
  879. (let ((encrypted? (eq? scheme 'entire-encrypted-root-home)))
  880. `(,@(if start-partition
  881. `(,start-partition)
  882. '())
  883. ,(user-partition
  884. (fs-type 'ext4)
  885. (bootable? has-extended?)
  886. (crypt-label (and encrypted? "cryptroot"))
  887. (size "33%")
  888. (mount-point "/"))
  889. ,@(if has-extended?
  890. `(,(user-partition
  891. (type 'extended)
  892. (size "100%")))
  893. '())
  894. ,@(if encrypted?
  895. '()
  896. `(,(user-partition
  897. (type (if has-extended?
  898. 'logical
  899. 'normal))
  900. (fs-type 'swap)
  901. (size swap-size))))
  902. ,(user-partition
  903. (type (if has-extended?
  904. 'logical
  905. 'normal))
  906. (fs-type 'ext4)
  907. (crypt-label (and encrypted? "crypthome"))
  908. (size "100%")
  909. (mount-point "/home")))))))
  910. (new-partitions* (force-user-partitions-formatting
  911. new-partitions)))
  912. (create-adjacent-partitions disk
  913. new-partitions*
  914. #:last-partition-end
  915. (or end-esp-partition 0)))))
  916. ;;
  917. ;; Convert user-partitions.
  918. ;;
  919. ;; No root mount point found.
  920. (define-condition-type &no-root-mount-point &condition
  921. no-root-mount-point?)
  922. (define (check-user-partitions user-partitions)
  923. "Return #t if the USER-PARTITIONS lists contains one <user-partition> record
  924. with a mount-point set to '/', raise &no-root-mount-point condition
  925. otherwise."
  926. (let ((mount-points
  927. (map user-partition-mount-point user-partitions)))
  928. (or (member "/" mount-points)
  929. (raise
  930. (condition (&no-root-mount-point))))))
  931. (define (set-user-partitions-file-name user-partitions)
  932. "Set the partition file-name of <user-partition> records in USER-PARTITIONS
  933. list and return the updated list."
  934. (map (lambda (p)
  935. (let* ((partition (user-partition-parted-object p))
  936. (file-name (partition-get-path partition)))
  937. (user-partition
  938. (inherit p)
  939. (file-name file-name))))
  940. user-partitions))
  941. (define-syntax-rule (with-null-output-ports exp ...)
  942. "Evaluate EXP with both the output port and the error port pointing to the
  943. bit bucket."
  944. (with-output-to-port (%make-void-port "w")
  945. (lambda ()
  946. (with-error-to-port (%make-void-port "w")
  947. (lambda () exp ...)))))
  948. (define (create-ext4-file-system partition)
  949. "Create an ext4 file-system for PARTITION file-name."
  950. (with-null-output-ports
  951. (invoke "mkfs.ext4" "-F" partition)))
  952. (define (create-fat32-file-system partition)
  953. "Create an ext4 file-system for PARTITION file-name."
  954. (with-null-output-ports
  955. (invoke "mkfs.fat" "-F32" partition)))
  956. (define (create-swap-partition partition)
  957. "Set up swap area on PARTITION file-name."
  958. (with-null-output-ports
  959. (invoke "mkswap" "-f" partition)))
  960. (define (call-with-luks-key-file password proc)
  961. "Write PASSWORD in a temporary file and pass it to PROC as argument."
  962. (call-with-temporary-output-file
  963. (lambda (file port)
  964. (put-string port password)
  965. (close port)
  966. (proc file))))
  967. (define (user-partition-upper-file-name user-partition)
  968. "Return the file-name of the virtual block device corresponding to
  969. USER-PARTITION if it is encrypted, or the plain file-name otherwise."
  970. (let ((crypt-label (user-partition-crypt-label user-partition))
  971. (file-name (user-partition-file-name user-partition)))
  972. (if crypt-label
  973. (string-append "/dev/mapper/" crypt-label)
  974. file-name)))
  975. (define (luks-format-and-open user-partition)
  976. "Format and open the encrypted partition pointed by USER-PARTITION."
  977. (let* ((file-name (user-partition-file-name user-partition))
  978. (label (user-partition-crypt-label user-partition))
  979. (password (user-partition-crypt-password user-partition)))
  980. (call-with-luks-key-file
  981. password
  982. (lambda (key-file)
  983. (system* "cryptsetup" "-q" "luksFormat" file-name key-file)
  984. (system* "cryptsetup" "open" "--type" "luks"
  985. "--key-file" key-file file-name label)))))
  986. (define (luks-close user-partition)
  987. "Close the encrypted partition pointed by USER-PARTITION."
  988. (let ((label (user-partition-crypt-label user-partition)))
  989. (system* "cryptsetup" "close" label)))
  990. (define (format-user-partitions user-partitions)
  991. "Format the <user-partition> records in USER-PARTITIONS list with
  992. NEED-FORMATING? field set to #t."
  993. (for-each
  994. (lambda (user-partition)
  995. (let* ((need-formatting?
  996. (user-partition-need-formatting? user-partition))
  997. (type (user-partition-type user-partition))
  998. (crypt-label (user-partition-crypt-label user-partition))
  999. (file-name (user-partition-upper-file-name user-partition))
  1000. (fs-type (user-partition-fs-type user-partition)))
  1001. (when crypt-label
  1002. (luks-format-and-open user-partition))
  1003. (case fs-type
  1004. ((ext4)
  1005. (and need-formatting?
  1006. (not (eq? type 'extended))
  1007. (create-ext4-file-system file-name)))
  1008. ((fat32)
  1009. (and need-formatting?
  1010. (not (eq? type 'extended))
  1011. (create-fat32-file-system file-name)))
  1012. ((swap)
  1013. (create-swap-partition file-name))
  1014. (else
  1015. ;; TODO: Add support for other file-system types.
  1016. #t))))
  1017. user-partitions))
  1018. (define (sort-partitions user-partitions)
  1019. "Sort USER-PARTITIONS by mount-points, so that the more nested mount-point
  1020. comes last. This is useful to mount/umount partitions in a coherent order."
  1021. (sort user-partitions
  1022. (lambda (a b)
  1023. (let ((mount-point-a (user-partition-mount-point a))
  1024. (mount-point-b (user-partition-mount-point b)))
  1025. (string-prefix? mount-point-a mount-point-b)))))
  1026. (define (mount-user-partitions user-partitions)
  1027. "Mount the <user-partition> records in USER-PARTITIONS list on their
  1028. respective mount-points."
  1029. (let* ((mount-partitions (filter user-partition-mount-point user-partitions))
  1030. (sorted-partitions (sort-partitions mount-partitions)))
  1031. (for-each (lambda (user-partition)
  1032. (let* ((mount-point
  1033. (user-partition-mount-point user-partition))
  1034. (target
  1035. (string-append (%installer-target-dir)
  1036. mount-point))
  1037. (fs-type
  1038. (user-partition-fs-type user-partition))
  1039. (crypt-label
  1040. (user-partition-crypt-label user-partition))
  1041. (mount-type
  1042. (user-fs-type->mount-type fs-type))
  1043. (file-name
  1044. (user-partition-upper-file-name user-partition)))
  1045. (mkdir-p target)
  1046. (mount file-name target mount-type)))
  1047. sorted-partitions)))
  1048. (define (umount-user-partitions user-partitions)
  1049. "Unmount all the <user-partition> records in USER-PARTITIONS list."
  1050. (let* ((mount-partitions (filter user-partition-mount-point user-partitions))
  1051. (sorted-partitions (sort-partitions mount-partitions)))
  1052. (for-each (lambda (user-partition)
  1053. (let* ((mount-point
  1054. (user-partition-mount-point user-partition))
  1055. (crypt-label
  1056. (user-partition-crypt-label user-partition))
  1057. (target
  1058. (string-append (%installer-target-dir)
  1059. mount-point)))
  1060. (umount target)
  1061. (when crypt-label
  1062. (luks-close user-partition))))
  1063. (reverse sorted-partitions))))
  1064. (define (find-swap-user-partitions user-partitions)
  1065. "Return the subset of <user-partition> records in USER-PARTITIONS list with
  1066. the FS-TYPE field set to 'swap, return the empty list if none found."
  1067. (filter (lambda (user-partition)
  1068. (let ((fs-type (user-partition-fs-type user-partition)))
  1069. (eq? fs-type 'swap)))
  1070. user-partitions))
  1071. (define (start-swapping user-partitions)
  1072. "Start swaping on <user-partition> records with FS-TYPE equal to 'swap."
  1073. (let* ((swap-user-partitions (find-swap-user-partitions user-partitions))
  1074. (swap-devices (map user-partition-file-name swap-user-partitions)))
  1075. (for-each swapon swap-devices)))
  1076. (define (stop-swapping user-partitions)
  1077. "Stop swaping on <user-partition> records with FS-TYPE equal to 'swap."
  1078. (let* ((swap-user-partitions (find-swap-user-partitions user-partitions))
  1079. (swap-devices (map user-partition-file-name swap-user-partitions)))
  1080. (for-each swapoff swap-devices)))
  1081. (define-syntax-rule (with-mounted-partitions user-partitions exp ...)
  1082. "Mount USER-PARTITIONS and start swapping within the dynamic extent of EXP."
  1083. (dynamic-wind
  1084. (lambda ()
  1085. (mount-user-partitions user-partitions)
  1086. (start-swapping user-partitions))
  1087. (lambda ()
  1088. exp ...)
  1089. (lambda ()
  1090. (umount-user-partitions user-partitions)
  1091. (stop-swapping user-partitions)
  1092. #f)))
  1093. (define (user-partition->file-system user-partition)
  1094. "Convert the given USER-PARTITION record in a FILE-SYSTEM record from
  1095. (gnu system file-systems) module and return it."
  1096. (let* ((mount-point (user-partition-mount-point user-partition))
  1097. (fs-type (user-partition-fs-type user-partition))
  1098. (crypt-label (user-partition-crypt-label user-partition))
  1099. (mount-type (user-fs-type->mount-type fs-type))
  1100. (file-name (user-partition-file-name user-partition))
  1101. (upper-file-name (user-partition-upper-file-name user-partition))
  1102. ;; Only compute uuid if partition is not encrypted.
  1103. (uuid (or crypt-label
  1104. (uuid->string (read-partition-uuid file-name) fs-type))))
  1105. `(file-system
  1106. (mount-point ,mount-point)
  1107. (device ,@(if crypt-label
  1108. `(,upper-file-name)
  1109. `((uuid ,uuid (quote ,fs-type)))))
  1110. (type ,mount-type)
  1111. ,@(if crypt-label
  1112. '((dependencies mapped-devices))
  1113. '()))))
  1114. (define (user-partitions->file-systems user-partitions)
  1115. "Convert the given USER-PARTITIONS list of <user-partition> records into a
  1116. list of <file-system> records."
  1117. (filter-map
  1118. (lambda (user-partition)
  1119. (let ((mount-point
  1120. (user-partition-mount-point user-partition)))
  1121. (and mount-point
  1122. (user-partition->file-system user-partition))))
  1123. user-partitions))
  1124. (define (user-partition->mapped-device user-partition)
  1125. "Convert the given USER-PARTITION record into a MAPPED-DEVICE record
  1126. from (gnu system mapped-devices) and return it."
  1127. (let ((label (user-partition-crypt-label user-partition))
  1128. (file-name (user-partition-file-name user-partition)))
  1129. `(mapped-device
  1130. (source (uuid ,(uuid->string
  1131. (read-luks-partition-uuid file-name)
  1132. 'luks)))
  1133. (target ,label)
  1134. (type luks-device-mapping))))
  1135. (define (bootloader-configuration user-partitions)
  1136. "Return the bootloader configuration field for USER-PARTITIONS."
  1137. (let* ((root-partition
  1138. (find (lambda (user-partition)
  1139. (let ((mount-point
  1140. (user-partition-mount-point user-partition)))
  1141. (and mount-point
  1142. (string=? mount-point "/"))))
  1143. user-partitions))
  1144. (root-partition-disk (user-partition-disk-file-name root-partition)))
  1145. `((bootloader-configuration
  1146. ,@(if (efi-installation?)
  1147. `((bootloader grub-efi-bootloader)
  1148. (target ,(default-esp-mount-point)))
  1149. `((bootloader grub-bootloader)
  1150. (target ,root-partition-disk)))))))
  1151. (define (user-partitions->configuration user-partitions)
  1152. "Return the configuration field for USER-PARTITIONS."
  1153. (let* ((swap-user-partitions (find-swap-user-partitions user-partitions))
  1154. (swap-devices (map user-partition-file-name swap-user-partitions))
  1155. (encrypted-partitions
  1156. (filter user-partition-crypt-label user-partitions)))
  1157. `(,@(if (null? swap-devices)
  1158. '()
  1159. `((swap-devices (list ,@swap-devices))))
  1160. (bootloader ,@(bootloader-configuration user-partitions))
  1161. ,@(if (null? encrypted-partitions)
  1162. '()
  1163. `((mapped-devices
  1164. (list ,@(map user-partition->mapped-device
  1165. encrypted-partitions)))))
  1166. (file-systems (cons*
  1167. ,@(user-partitions->file-systems user-partitions)
  1168. %base-file-systems)))))
  1169. ;;
  1170. ;; Initialization.
  1171. ;;
  1172. (define (init-parted)
  1173. "Initialize libparted support."
  1174. (probe-all-devices)
  1175. (exception-set-handler (lambda (exception)
  1176. EXCEPTION-OPTION-UNHANDLED)))
  1177. (define (free-parted devices)
  1178. "Deallocate memory used for DEVICES in parted, force sync them and wait for
  1179. the devices not to be used before returning."
  1180. ;; XXX: Formatting and further operations on disk partition table may fail
  1181. ;; because the partition table changes are not synced, or because the device
  1182. ;; is still in use, even if parted should have finished editing
  1183. ;; partitions. This is not well understood, but syncing devices and waiting
  1184. ;; them to stop returning EBUSY to BLKRRPART ioctl seems to be enough. The
  1185. ;; same kind of issue is described here:
  1186. ;; https://mail.gnome.org/archives/commits-list/2013-March/msg18423.html.
  1187. (let ((device-file-names (map device-path devices)))
  1188. (for-each force-device-sync devices)
  1189. (free-all-devices)
  1190. (for-each (lambda (file-name)
  1191. (let ((in-use? (with-delay-device-in-use? file-name)))
  1192. (and in-use?
  1193. (error
  1194. (format #f (G_ "Device ~a is still in use.")
  1195. file-name)))))
  1196. device-file-names)))