parted.scm 57 KB

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