parted.scm 55 KB

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