parted.scm 57 KB

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