image.scm 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2020 Mathieu Othacehe <m.othacehe@gmail.com>
  3. ;;; Copyright © 2020 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
  4. ;;;
  5. ;;; This file is part of GNU Guix.
  6. ;;;
  7. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  8. ;;; under the terms of the GNU General Public License as published by
  9. ;;; the Free Software Foundation; either version 3 of the License, or (at
  10. ;;; your option) any later version.
  11. ;;;
  12. ;;; GNU Guix is distributed in the hope that it will be useful, but
  13. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;;; GNU General Public License for more details.
  16. ;;;
  17. ;;; You should have received a copy of the GNU General Public License
  18. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  19. (define-module (gnu system image)
  20. #:use-module (guix diagnostics)
  21. #:use-module (guix discovery)
  22. #:use-module (guix gexp)
  23. #:use-module (guix modules)
  24. #:use-module (guix monads)
  25. #:use-module (guix records)
  26. #:use-module (guix store)
  27. #:use-module (guix ui)
  28. #:use-module (guix utils)
  29. #:use-module ((guix self) #:select (make-config.scm))
  30. #:use-module (gnu bootloader)
  31. #:use-module (gnu bootloader grub)
  32. #:use-module (gnu image)
  33. #:use-module (gnu services)
  34. #:use-module (gnu services base)
  35. #:use-module (gnu system)
  36. #:use-module (gnu system file-systems)
  37. #:use-module (gnu system uuid)
  38. #:use-module (gnu system vm)
  39. #:use-module (guix packages)
  40. #:use-module (gnu packages base)
  41. #:use-module (gnu packages bootloaders)
  42. #:use-module (gnu packages cdrom)
  43. #:use-module (gnu packages disk)
  44. #:use-module (gnu packages gawk)
  45. #:use-module (gnu packages genimage)
  46. #:use-module (gnu packages guile)
  47. #:autoload (gnu packages gnupg) (guile-gcrypt)
  48. #:use-module (gnu packages hurd)
  49. #:use-module (gnu packages linux)
  50. #:use-module (gnu packages mtools)
  51. #:use-module (gnu packages virtualization)
  52. #:use-module ((srfi srfi-1) #:prefix srfi-1:)
  53. #:use-module (srfi srfi-11)
  54. #:use-module (srfi srfi-26)
  55. #:use-module (srfi srfi-35)
  56. #:use-module (rnrs bytevectors)
  57. #:use-module (ice-9 format)
  58. #:use-module (ice-9 match)
  59. #:export (root-offset
  60. root-label
  61. esp-partition
  62. root-partition
  63. efi-disk-image
  64. iso9660-image
  65. arm64-disk-image
  66. image-with-os
  67. raw-image-type
  68. qcow2-image-type
  69. iso-image-type
  70. uncompressed-iso-image-type
  71. arm64-image-type
  72. image-with-label
  73. system-image
  74. %image-types
  75. lookup-image-type-by-name))
  76. ;;;
  77. ;;; Images definitions.
  78. ;;;
  79. ;; This is the offset before the first partition. GRUB will install itself in
  80. ;; this post-MBR gap.
  81. (define root-offset (* 512 2048))
  82. ;; Generic root partition label.
  83. (define root-label "Guix_image")
  84. (define esp-partition
  85. (partition
  86. (size (* 40 (expt 2 20)))
  87. (offset root-offset)
  88. (label "GNU-ESP") ;cosmetic only
  89. ;; Use "vfat" here since this property is used when mounting. The actual
  90. ;; FAT-ness is based on file system size (16 in this case).
  91. (file-system "vfat")
  92. (flags '(esp))
  93. (initializer (gexp initialize-efi-partition))))
  94. (define root-partition
  95. (partition
  96. (size 'guess)
  97. (label root-label)
  98. (file-system "ext4")
  99. (flags '(boot))
  100. (initializer (gexp initialize-root-partition))))
  101. (define efi-disk-image
  102. (image
  103. (format 'disk-image)
  104. (partitions (list esp-partition root-partition))))
  105. (define iso9660-image
  106. (image
  107. (format 'iso9660)
  108. (partitions
  109. (list (partition
  110. (size 'guess)
  111. (label "GUIX_IMAGE")
  112. (flags '(boot)))))))
  113. (define arm64-disk-image
  114. (image
  115. (format 'disk-image)
  116. (target "aarch64-linux-gnu")
  117. (partitions
  118. (list (partition
  119. (inherit root-partition)
  120. (offset root-offset))))
  121. ;; FIXME: Deleting and creating "/var/run" and "/tmp" on the overlayfs
  122. ;; fails.
  123. (volatile-root? #f)))
  124. ;;;
  125. ;;; Images types.
  126. ;;;
  127. (define-syntax-rule (image-with-os base-image os)
  128. "Return an image inheriting from BASE-IMAGE, with the operating-system field
  129. set to the given OS."
  130. (image
  131. (inherit base-image)
  132. (operating-system os)))
  133. (define raw-image-type
  134. (image-type
  135. (name 'raw)
  136. (constructor (cut image-with-os efi-disk-image <>))))
  137. (define qcow2-image-type
  138. (image-type
  139. (name 'qcow2)
  140. (constructor (cut image-with-os
  141. (image
  142. (inherit efi-disk-image)
  143. (name 'image.qcow2)
  144. (format 'compressed-qcow2))
  145. <>))))
  146. (define iso-image-type
  147. (image-type
  148. (name 'iso9660)
  149. (constructor (cut image-with-os iso9660-image <>))))
  150. (define uncompressed-iso-image-type
  151. (image-type
  152. (name 'uncompressed-iso9660)
  153. (constructor (cut image-with-os
  154. (image
  155. (inherit iso9660-image)
  156. (compression? #f))
  157. <>))))
  158. (define arm64-image-type
  159. (image-type
  160. (name 'arm)
  161. (constructor (cut image-with-os arm64-disk-image <>))))
  162. ;;
  163. ;; Helpers.
  164. ;;
  165. (define not-config?
  166. ;; Select (guix …) and (gnu …) modules, except (guix config).
  167. (match-lambda
  168. (('guix 'config) #f)
  169. (('guix rest ...) #t)
  170. (('gnu rest ...) #t)
  171. (rest #f)))
  172. (define (partition->gexp partition)
  173. "Turn PARTITION, a <partition> object, into a list-valued gexp suitable for
  174. 'make-partition-image'."
  175. #~'(#$@(list (partition-size partition))
  176. #$(partition-file-system partition)
  177. #$(partition-file-system-options partition)
  178. #$(partition-label partition)
  179. #$(and=> (partition-uuid partition)
  180. uuid-bytevector)))
  181. (define gcrypt-sqlite3&co
  182. ;; Guile-Gcrypt, Guile-SQLite3, and their propagated inputs.
  183. (srfi-1:append-map
  184. (lambda (package)
  185. (cons package
  186. (match (package-transitive-propagated-inputs package)
  187. (((labels packages) ...)
  188. packages))))
  189. (list guile-gcrypt guile-sqlite3)))
  190. (define-syntax-rule (with-imported-modules* gexp* ...)
  191. (with-extensions gcrypt-sqlite3&co
  192. (with-imported-modules `(,@(source-module-closure
  193. '((gnu build vm)
  194. (gnu build image)
  195. (gnu build bootloader)
  196. (gnu build hurd-boot)
  197. (gnu build linux-boot)
  198. (guix store database))
  199. #:select? not-config?)
  200. ((guix config) => ,(make-config.scm)))
  201. #~(begin
  202. (use-modules (gnu build vm)
  203. (gnu build image)
  204. (gnu build bootloader)
  205. (gnu build hurd-boot)
  206. (gnu build linux-boot)
  207. (guix store database)
  208. (guix build utils))
  209. gexp* ...))))
  210. (define (root-partition? partition)
  211. "Return true if PARTITION is the root partition, false otherwise."
  212. (member 'boot (partition-flags partition)))
  213. (define (find-root-partition image)
  214. "Return the root partition of the given IMAGE."
  215. (srfi-1:find root-partition? (image-partitions image)))
  216. (define (root-partition-index image)
  217. "Return the index of the root partition of the given IMAGE."
  218. (1+ (srfi-1:list-index root-partition? (image-partitions image))))
  219. ;;
  220. ;; Disk image.
  221. ;;
  222. (define* (system-disk-image image
  223. #:key
  224. (name "disk-image")
  225. bootcfg
  226. bootloader
  227. register-closures?
  228. (inputs '()))
  229. "Return as a file-like object, the disk-image described by IMAGE. Said
  230. image can be copied on a USB stick as is. BOOTLOADER is the bootloader that
  231. will be installed and configured according to BOOTCFG parameter.
  232. Raw images of the IMAGE partitions are first created. Then, genimage is used
  233. to assemble the partition images into a disk-image without resorting to a
  234. virtual machine.
  235. INPUTS is a list of inputs (as for packages). When REGISTER-CLOSURES? is
  236. true, register INPUTS in the store database of the image so that Guix can be
  237. used in the image."
  238. (define genimage-name "image")
  239. (define (image->genimage-cfg image)
  240. ;; Return as a file-like object, the genimage configuration file
  241. ;; describing the given IMAGE.
  242. (define (format->image-type format)
  243. ;; Return the genimage format corresponding to FORMAT. For now, only
  244. ;; the hdimage format (raw disk-image) is supported.
  245. (cond
  246. ((memq format '(disk-image compressed-qcow2)) "hdimage")
  247. (else
  248. (raise (condition
  249. (&message
  250. (message
  251. (format #f (G_ "Unsupported image type ~a~%.") format))))))))
  252. (define (partition->dos-type partition)
  253. ;; Return the MBR partition type corresponding to the given PARTITION.
  254. ;; See: https://en.wikipedia.org/wiki/Partition_type.
  255. (let ((flags (partition-flags partition)))
  256. (cond
  257. ((member 'esp flags) "0xEF")
  258. (else "0x83"))))
  259. (define (partition-image partition)
  260. ;; Return as a file-like object, an image of the given PARTITION. A
  261. ;; directory, filled by calling the PARTITION initializer procedure, is
  262. ;; first created within the store. Then, an image of this directory is
  263. ;; created using tools such as 'mke2fs' or 'mkdosfs', depending on the
  264. ;; partition file-system type.
  265. (let* ((os (image-operating-system image))
  266. (schema (local-file (search-path %load-path
  267. "guix/store/schema.sql")))
  268. (graph (match inputs
  269. (((names . _) ...)
  270. names)))
  271. (type (partition-file-system partition))
  272. (image-builder
  273. (with-imported-modules*
  274. (let ((initializer #$(partition-initializer partition))
  275. (inputs '#+(list e2fsprogs fakeroot dosfstools mtools))
  276. (image-root "tmp-root"))
  277. (sql-schema #$schema)
  278. (set-path-environment-variable "PATH" '("bin" "sbin") inputs)
  279. ;; Allow non-ASCII file names--e.g., 'nss-certs'--to be
  280. ;; decoded.
  281. (setenv "GUIX_LOCPATH"
  282. #+(file-append glibc-utf8-locales "/lib/locale"))
  283. (setlocale LC_ALL "en_US.utf8")
  284. (initializer image-root
  285. #:references-graphs '#$graph
  286. #:deduplicate? #f
  287. #:system-directory #$os
  288. #:grub-efi #+grub-efi
  289. #:bootloader-package
  290. #+(bootloader-package bootloader)
  291. #:bootloader-installer
  292. #+(bootloader-installer bootloader)
  293. #:bootcfg #$bootcfg
  294. #:bootcfg-location
  295. #$(bootloader-configuration-file bootloader))
  296. (make-partition-image #$(partition->gexp partition)
  297. #$output
  298. image-root)))))
  299. (computed-file "partition.img" image-builder
  300. #:options `(#:references-graphs ,inputs))))
  301. (define (partition->config partition)
  302. ;; Return the genimage partition configuration for PARTITION.
  303. (let ((label (partition-label partition))
  304. (dos-type (partition->dos-type partition))
  305. (image (partition-image partition))
  306. (offset (partition-offset partition)))
  307. #~(format #f "~/partition ~a {
  308. ~/~/partition-type = ~a
  309. ~/~/image = \"~a\"
  310. ~/~/offset = \"~a\"
  311. ~/}"
  312. #$label
  313. #$dos-type
  314. #$image
  315. #$offset)))
  316. (let* ((format (image-format image))
  317. (image-type (format->image-type format))
  318. (partitions (image-partitions image))
  319. (partitions-config (map partition->config partitions))
  320. (builder
  321. #~(begin
  322. (let ((format (@ (ice-9 format) format)))
  323. (call-with-output-file #$output
  324. (lambda (port)
  325. (format port
  326. "\
  327. image ~a {
  328. ~/~a {}
  329. ~{~a~^~%~}
  330. }~%" #$genimage-name #$image-type (list #$@partitions-config))))))))
  331. (computed-file "genimage.cfg" builder)))
  332. (let* ((image-name (image-name image))
  333. (name (if image-name
  334. (symbol->string image-name)
  335. name))
  336. (format (image-format image))
  337. (substitutable? (image-substitutable? image))
  338. (builder
  339. (with-imported-modules*
  340. (let ((inputs '#+(list genimage coreutils findutils qemu-minimal))
  341. (bootloader-installer
  342. #+(bootloader-disk-image-installer bootloader))
  343. (out-image (string-append "images/" #$genimage-name)))
  344. (set-path-environment-variable "PATH" '("bin" "sbin") inputs)
  345. (genimage #$(image->genimage-cfg image))
  346. ;; Install the bootloader directly on the disk-image.
  347. (when bootloader-installer
  348. (bootloader-installer
  349. #+(bootloader-package bootloader)
  350. #$(root-partition-index image)
  351. out-image))
  352. (convert-disk-image out-image '#$format #$output)))))
  353. (computed-file name builder
  354. #:options `(#:substitutable? ,substitutable?))))
  355. ;;
  356. ;; ISO9660 image.
  357. ;;
  358. (define (has-guix-service-type? os)
  359. "Return true if OS contains a service of the type GUIX-SERVICE-TYPE."
  360. (not (not (srfi-1:find (lambda (service)
  361. (eq? (service-kind service) guix-service-type))
  362. (operating-system-services os)))))
  363. (define* (system-iso9660-image image
  364. #:key
  365. (name "image.iso")
  366. bootcfg
  367. bootloader
  368. register-closures?
  369. (inputs '())
  370. (grub-mkrescue-environment '()))
  371. "Return as a file-like object a bootable, stand-alone iso9660 image.
  372. INPUTS is a list of inputs (as for packages). When REGISTER-CLOSURES? is
  373. true, register INPUTS in the store database of the image so that Guix can be
  374. used in the image. "
  375. (define root-label
  376. (match (image-partitions image)
  377. ((partition)
  378. (partition-label partition))))
  379. (define root-uuid
  380. (match (image-partitions image)
  381. ((partition)
  382. (uuid-bytevector (partition-uuid partition)))))
  383. (let* ((os (image-operating-system image))
  384. (bootloader (bootloader-package bootloader))
  385. (compression? (image-compression? image))
  386. (substitutable? (image-substitutable? image))
  387. (schema (local-file (search-path %load-path
  388. "guix/store/schema.sql")))
  389. (graph (match inputs
  390. (((names . _) ...)
  391. names)))
  392. (builder
  393. (with-imported-modules*
  394. (let* ((inputs '#$(list parted e2fsprogs dosfstools xorriso
  395. sed grep coreutils findutils gawk))
  396. (image-root "tmp-root"))
  397. (sql-schema #$schema)
  398. ;; Allow non-ASCII file names--e.g., 'nss-certs'--to be decoded.
  399. (setenv "GUIX_LOCPATH"
  400. #+(file-append glibc-utf8-locales "/lib/locale"))
  401. (setlocale LC_ALL "en_US.utf8")
  402. (set-path-environment-variable "PATH" '("bin" "sbin") inputs)
  403. (initialize-root-partition image-root
  404. #:references-graphs '#$graph
  405. #:deduplicate? #f
  406. #:system-directory #$os)
  407. (make-iso9660-image #$xorriso
  408. '#$grub-mkrescue-environment
  409. #$bootloader
  410. #$bootcfg
  411. #$os
  412. image-root
  413. #$output
  414. #:references-graphs '#$graph
  415. #:register-closures? #$register-closures?
  416. #:compression? #$compression?
  417. #:volume-id #$root-label
  418. #:volume-uuid #$root-uuid)))))
  419. (computed-file name builder
  420. #:options `(#:references-graphs ,inputs
  421. #:substitutable? ,substitutable?))))
  422. (define (image-with-label base-image label)
  423. "The volume ID of an ISO is the label of the first partition. This procedure
  424. returns an image record where the first partition's label is set to <label>."
  425. (image
  426. (inherit base-image)
  427. (partitions
  428. (match (image-partitions base-image)
  429. ((boot others ...)
  430. (cons
  431. (partition
  432. (inherit boot)
  433. (label label))
  434. others))))))
  435. ;;
  436. ;; Image creation.
  437. ;;
  438. (define (image->root-file-system image)
  439. "Return the IMAGE root partition file-system type."
  440. (let ((format (image-format image)))
  441. (if (eq? format 'iso9660)
  442. "iso9660"
  443. (partition-file-system (find-root-partition image)))))
  444. (define (root-size image)
  445. "Return the root partition size of IMAGE."
  446. (let* ((image-size (image-size image))
  447. (root-partition (find-root-partition image))
  448. (root-size (partition-size root-partition)))
  449. (cond
  450. ((and (eq? root-size 'guess) image-size)
  451. image-size)
  452. (else root-size))))
  453. (define* (image-with-os* base-image os)
  454. "Return an image based on BASE-IMAGE but with the operating-system field set
  455. to OS. Also set the UUID and the size of the root partition."
  456. (define root-file-system
  457. (srfi-1:find
  458. (lambda (fs)
  459. (string=? (file-system-mount-point fs) "/"))
  460. (operating-system-file-systems os)))
  461. (image
  462. (inherit base-image)
  463. (operating-system os)
  464. (partitions
  465. (map (lambda (p)
  466. (if (root-partition? p)
  467. (partition
  468. (inherit p)
  469. (uuid (file-system-device root-file-system))
  470. (size (root-size base-image)))
  471. p))
  472. (image-partitions base-image)))))
  473. (define (operating-system-for-image image)
  474. "Return an operating-system based on the one specified in IMAGE, but
  475. suitable for image creation. Assign an UUID to the root file-system, so that
  476. it can be used for bootloading."
  477. (define volatile-root? (image-volatile-root? image))
  478. (define (root-uuid os)
  479. ;; UUID of the root file system, computed in a deterministic fashion.
  480. ;; This is what we use to locate the root file system so it has to be
  481. ;; different from the user's own file system UUIDs.
  482. (let ((type (if (eq? (image-format image) 'iso9660)
  483. 'iso9660
  484. 'dce)))
  485. (operating-system-uuid os type)))
  486. (let* ((root-file-system-type (image->root-file-system image))
  487. (base-os (image-operating-system image))
  488. (file-systems-to-keep
  489. (srfi-1:remove
  490. (lambda (fs)
  491. (string=? (file-system-mount-point fs) "/"))
  492. (operating-system-file-systems base-os)))
  493. (format (image-format image))
  494. (os
  495. (operating-system
  496. (inherit base-os)
  497. (initrd (lambda (file-systems . rest)
  498. (apply (operating-system-initrd base-os)
  499. file-systems
  500. #:volatile-root? volatile-root?
  501. rest)))
  502. (bootloader (if (eq? format 'iso9660)
  503. (bootloader-configuration
  504. (inherit
  505. (operating-system-bootloader base-os))
  506. (bootloader grub-mkrescue-bootloader))
  507. (operating-system-bootloader base-os)))
  508. (file-systems (cons (file-system
  509. (mount-point "/")
  510. (device "/dev/placeholder")
  511. (type root-file-system-type))
  512. file-systems-to-keep))))
  513. (uuid (root-uuid os)))
  514. (operating-system
  515. (inherit os)
  516. (file-systems (cons (file-system
  517. (mount-point "/")
  518. (device uuid)
  519. (type root-file-system-type))
  520. file-systems-to-keep)))))
  521. (define* (system-image image)
  522. "Return the derivation of IMAGE. It can be a raw disk-image or an ISO9660
  523. image, depending on IMAGE format."
  524. (define substitutable? (image-substitutable? image))
  525. (define target (image-target image))
  526. (with-parameters ((%current-target-system target))
  527. (let* ((os (operating-system-for-image image))
  528. (image* (image-with-os* image os))
  529. (image-format (image-format image))
  530. (register-closures? (has-guix-service-type? os))
  531. (bootcfg (operating-system-bootcfg os))
  532. (bootloader (bootloader-configuration-bootloader
  533. (operating-system-bootloader os))))
  534. (cond
  535. ((memq image-format '(disk-image compressed-qcow2))
  536. (system-disk-image image*
  537. #:bootcfg bootcfg
  538. #:bootloader bootloader
  539. #:register-closures? register-closures?
  540. #:inputs `(("system" ,os)
  541. ("bootcfg" ,bootcfg))))
  542. ((memq image-format '(iso9660))
  543. (system-iso9660-image
  544. image*
  545. #:bootcfg bootcfg
  546. #:bootloader bootloader
  547. #:register-closures? register-closures?
  548. #:inputs `(("system" ,os)
  549. ("bootcfg" ,bootcfg))
  550. ;; Make sure to use a mode that does no imply
  551. ;; HFS+ tree creation that may fail with:
  552. ;;
  553. ;; "libisofs: FAILURE : Too much files to mangle,
  554. ;; cannot guarantee unique file names"
  555. ;;
  556. ;; This happens if some limits are exceeded, see:
  557. ;; https://lists.gnu.org/archive/html/grub-devel/2020-06/msg00048.html
  558. #:grub-mkrescue-environment
  559. '(("MKRESCUE_SED_MODE" . "mbr_only"))))))))
  560. ;;
  561. ;; Image detection.
  562. ;;
  563. (define (image-modules)
  564. "Return the list of image modules."
  565. (cons (resolve-interface '(gnu system image))
  566. (all-modules (map (lambda (entry)
  567. `(,entry . "gnu/system/images/"))
  568. %load-path)
  569. #:warn warn-about-load-error)))
  570. (define %image-types
  571. ;; The list of publically-known image types.
  572. (delay (fold-module-public-variables (lambda (obj result)
  573. (if (image-type? obj)
  574. (cons obj result)
  575. result))
  576. '()
  577. (image-modules))))
  578. (define (lookup-image-type-by-name name)
  579. "Return the image type called NAME."
  580. (or (srfi-1:find (lambda (image-type)
  581. (eq? name (image-type-name image-type)))
  582. (force %image-types))
  583. (raise
  584. (formatted-message (G_ "~a: no such image type~%") name))))
  585. ;;; image.scm ends here