parted.scm 55 KB

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