parted.scm 53 KB

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