parted.scm 54 KB

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