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>
5 ;;; This file is part of GNU Guix.
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.
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.
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/>.
20 (define-module (gnu system image)
21 #:use-module (guix diagnostics)
22 #:use-module (guix discovery)
23 #:use-module (guix gexp)
24 #:use-module (guix modules)
25 #:use-module (guix monads)
26 #:use-module (guix records)
27 #:use-module (guix store)
28 #:use-module (guix ui)
29 #:use-module (guix utils)
30 #:use-module ((guix self) #:select (make-config.scm))
31 #:use-module (gnu bootloader)
32 #:use-module (gnu bootloader grub)
33 #:use-module (gnu image)
34 #:use-module (gnu services)
35 #:use-module (gnu services base)
36 #:use-module (gnu system)
37 #:use-module (gnu system file-systems)
38 #:use-module (gnu system uuid)
39 #:use-module (gnu system vm)
40 #:use-module (guix packages)
41 #:use-module (gnu packages base)
42 #:use-module (gnu packages bootloaders)
43 #:use-module (gnu packages cdrom)
44 #:use-module (gnu packages disk)
45 #:use-module (gnu packages gawk)
46 #:use-module (gnu packages genimage)
47 #:use-module (gnu packages guile)
48 #:autoload (gnu packages gnupg) (guile-gcrypt)
49 #:use-module (gnu packages hurd)
50 #:use-module (gnu packages linux)
51 #:use-module (gnu packages mtools)
52 #:use-module (gnu packages virtualization)
53 #:use-module ((srfi srfi-1) #:prefix srfi-1:)
54 #:use-module (srfi srfi-11)
55 #:use-module (srfi srfi-26)
56 #:use-module (srfi srfi-34)
57 #:use-module (srfi srfi-35)
58 #:use-module (rnrs bytevectors)
59 #:use-module (ice-9 format)
60 #:use-module (ice-9 match)
76 uncompressed-iso-image-type
84 lookup-image-type-by-name))
88 ;;; Images definitions.
91 ;; This is the offset before the first partition. GRUB will install itself in
93 (define root-offset (* 512 2048))
95 ;; Generic root partition label.
96 (define root-label "Guix_image")
100 (size (* 40 (expt 2 20)))
102 (label "GNU-ESP") ;cosmetic only
103 ;; Use "vfat" here since this property is used when mounting. The actual
104 ;; FAT-ness is based on file system size (16 in this case).
107 (initializer (gexp initialize-efi-partition))))
109 (define root-partition
115 (initializer (gexp initialize-root-partition))))
117 (define efi-disk-image
120 (partitions (list esp-partition root-partition))))
122 (define iso9660-image
131 (define arm32-disk-image
134 (target "arm-linux-gnueabihf")
137 (inherit root-partition)
138 (offset root-offset))))
139 ;; FIXME: Deleting and creating "/var/run" and "/tmp" on the overlayfs
141 (volatile-root? #f)))
143 (define arm64-disk-image
145 (inherit arm32-disk-image)
146 (target "aarch64-linux-gnu")))
153 (define-syntax-rule (image-with-os base-image os)
154 "Return an image inheriting from BASE-IMAGE, with the operating-system field
155 set to the given OS."
158 (operating-system os)))
160 (define raw-image-type
163 (constructor (cut image-with-os efi-disk-image <>))))
165 (define qcow2-image-type
168 (constructor (cut image-with-os
170 (inherit efi-disk-image)
172 (format 'compressed-qcow2))
175 (define iso-image-type
178 (constructor (cut image-with-os iso9660-image <>))))
180 (define uncompressed-iso-image-type
182 (name 'uncompressed-iso9660)
183 (constructor (cut image-with-os
185 (inherit iso9660-image)
189 (define arm32-image-type
192 (constructor (cut image-with-os arm32-disk-image <>))))
194 (define arm64-image-type
197 (constructor (cut image-with-os arm64-disk-image <>))))
205 ;; Select (guix …) and (gnu …) modules, except (guix config).
208 (('guix rest ...) #t)
212 (define (partition->gexp partition)
213 "Turn PARTITION, a <partition> object, into a list-valued gexp suitable for
214 'make-partition-image'."
215 #~'(#$@(list (partition-size partition))
216 #$(partition-file-system partition)
217 #$(partition-file-system-options partition)
218 #$(partition-label partition)
219 #$(and=> (partition-uuid partition)
222 (define gcrypt-sqlite3&co
223 ;; Guile-Gcrypt, Guile-SQLite3, and their propagated inputs.
227 (match (package-transitive-propagated-inputs package)
228 (((labels packages) ...)
230 (list guile-gcrypt guile-sqlite3)))
232 (define-syntax-rule (with-imported-modules* gexp* ...)
233 (with-extensions gcrypt-sqlite3&co
234 (with-imported-modules `(,@(source-module-closure
237 (gnu build bootloader)
238 (gnu build hurd-boot)
239 (gnu build linux-boot)
240 (guix store database))
241 #:select? not-config?)
242 ((guix config) => ,(make-config.scm)))
244 (use-modules (gnu build vm)
246 (gnu build bootloader)
247 (gnu build hurd-boot)
248 (gnu build linux-boot)
249 (guix store database)
253 (define (root-partition? partition)
254 "Return true if PARTITION is the root partition, false otherwise."
255 (member 'boot (partition-flags partition)))
257 (define (find-root-partition image)
258 "Return the root partition of the given IMAGE."
259 (srfi-1:find root-partition? (image-partitions image)))
261 (define (root-partition-index image)
262 "Return the index of the root partition of the given IMAGE."
263 (1+ (srfi-1:list-index root-partition? (image-partitions image))))
270 (define* (system-disk-image image
277 "Return as a file-like object, the disk-image described by IMAGE. Said
278 image can be copied on a USB stick as is. BOOTLOADER is the bootloader that
279 will be installed and configured according to BOOTCFG parameter.
281 Raw images of the IMAGE partitions are first created. Then, genimage is used
282 to assemble the partition images into a disk-image without resorting to a
285 INPUTS is a list of inputs (as for packages). When REGISTER-CLOSURES? is
286 true, register INPUTS in the store database of the image so that Guix can be
289 (define genimage-name "image")
291 (define (image->genimage-cfg image)
292 ;; Return as a file-like object, the genimage configuration file
293 ;; describing the given IMAGE.
294 (define (format->image-type format)
295 ;; Return the genimage format corresponding to FORMAT. For now, only
296 ;; the hdimage format (raw disk-image) is supported.
298 ((memq format '(disk-image compressed-qcow2)) "hdimage")
303 (format #f (G_ "Unsupported image type ~a~%.") format))))))))
305 (define (partition->dos-type partition)
306 ;; Return the MBR partition type corresponding to the given PARTITION.
307 ;; See: https://en.wikipedia.org/wiki/Partition_type.
308 (let ((flags (partition-flags partition)))
310 ((member 'esp flags) "0xEF")
313 (define (partition-image partition)
314 ;; Return as a file-like object, an image of the given PARTITION. A
315 ;; directory, filled by calling the PARTITION initializer procedure, is
316 ;; first created within the store. Then, an image of this directory is
317 ;; created using tools such as 'mke2fs' or 'mkdosfs', depending on the
318 ;; partition file-system type.
319 (let* ((os (image-operating-system image))
320 (schema (local-file (search-path %load-path
321 "guix/store/schema.sql")))
325 (type (partition-file-system partition))
327 (with-imported-modules*
328 (let ((initializer #$(partition-initializer partition))
329 (inputs '#+(list e2fsprogs fakeroot dosfstools mtools))
330 (image-root "tmp-root"))
331 (sql-schema #$schema)
333 (set-path-environment-variable "PATH" '("bin" "sbin") inputs)
335 ;; Allow non-ASCII file names--e.g., 'nss-certs'--to be
337 (setenv "GUIX_LOCPATH"
338 #+(file-append glibc-utf8-locales "/lib/locale"))
339 (setlocale LC_ALL "en_US.utf8")
341 (initializer image-root
342 #:references-graphs '#$graph
344 #:system-directory #$os
345 #:grub-efi #+grub-efi
347 #+(bootloader-package bootloader)
348 #:bootloader-installer
349 #+(bootloader-installer bootloader)
352 #$(bootloader-configuration-file bootloader))
353 (make-partition-image #$(partition->gexp partition)
356 (computed-file "partition.img" image-builder
357 #:options `(#:references-graphs ,inputs))))
359 (define (partition->config partition)
360 ;; Return the genimage partition configuration for PARTITION.
361 (let ((label (partition-label partition))
362 (dos-type (partition->dos-type partition))
363 (image (partition-image partition))
364 (offset (partition-offset partition)))
365 #~(format #f "~/partition ~a {
366 ~/~/partition-type = ~a
375 (let* ((format (image-format image))
376 (image-type (format->image-type format))
377 (partitions (image-partitions image))
378 (partitions-config (map partition->config partitions))
381 (let ((format (@ (ice-9 format) format)))
382 (call-with-output-file #$output
389 }~%" #$genimage-name #$image-type (list #$@partitions-config))))))))
390 (computed-file "genimage.cfg" builder)))
392 (let* ((image-name (image-name image))
394 (symbol->string image-name)
396 (format (image-format image))
397 (substitutable? (image-substitutable? image))
399 (with-imported-modules*
400 (let ((inputs '#+(list genimage coreutils findutils qemu-minimal))
401 (bootloader-installer
402 #+(bootloader-disk-image-installer bootloader))
403 (out-image (string-append "images/" #$genimage-name)))
404 (set-path-environment-variable "PATH" '("bin" "sbin") inputs)
405 (genimage #$(image->genimage-cfg image))
406 ;; Install the bootloader directly on the disk-image.
407 (when bootloader-installer
408 (bootloader-installer
409 #+(bootloader-package bootloader)
410 #$(root-partition-index image)
412 (convert-disk-image out-image '#$format #$output)))))
413 (computed-file name builder
414 #:options `(#:substitutable? ,substitutable?))))
421 (define (has-guix-service-type? os)
422 "Return true if OS contains a service of the type GUIX-SERVICE-TYPE."
423 (not (not (srfi-1:find (lambda (service)
424 (eq? (service-kind service) guix-service-type))
425 (operating-system-services os)))))
427 (define* (system-iso9660-image image
434 (grub-mkrescue-environment '()))
435 "Return as a file-like object a bootable, stand-alone iso9660 image.
437 INPUTS is a list of inputs (as for packages). When REGISTER-CLOSURES? is
438 true, register INPUTS in the store database of the image so that Guix can be
441 (match (image-partitions image)
443 (partition-label partition))))
446 (match (image-partitions image)
448 (uuid-bytevector (partition-uuid partition)))))
450 (let* ((os (image-operating-system image))
451 (bootloader (bootloader-package bootloader))
452 (compression? (image-compression? image))
453 (substitutable? (image-substitutable? image))
454 (schema (local-file (search-path %load-path
455 "guix/store/schema.sql")))
460 (with-imported-modules*
461 (let* ((inputs '#$(list parted e2fsprogs dosfstools xorriso
462 sed grep coreutils findutils gawk))
463 (image-root "tmp-root"))
464 (sql-schema #$schema)
466 ;; Allow non-ASCII file names--e.g., 'nss-certs'--to be decoded.
467 (setenv "GUIX_LOCPATH"
468 #+(file-append glibc-utf8-locales "/lib/locale"))
470 (setlocale LC_ALL "en_US.utf8")
472 (set-path-environment-variable "PATH" '("bin" "sbin") inputs)
474 (initialize-root-partition image-root
475 #:references-graphs '#$graph
477 #:system-directory #$os)
478 (make-iso9660-image #$xorriso
479 '#$grub-mkrescue-environment
485 #:references-graphs '#$graph
486 #:register-closures? #$register-closures?
487 #:compression? #$compression?
488 #:volume-id #$root-label
489 #:volume-uuid #$root-uuid)))))
490 (computed-file name builder
491 #:options `(#:references-graphs ,inputs
492 #:substitutable? ,substitutable?))))
494 (define (image-with-label base-image label)
495 "The volume ID of an ISO is the label of the first partition. This procedure
496 returns an image record where the first partition's label is set to <label>."
500 (match (image-partitions base-image)
513 (define (image->root-file-system image)
514 "Return the IMAGE root partition file-system type."
515 (let ((format (image-format image)))
516 (if (eq? format 'iso9660)
518 (partition-file-system (find-root-partition image)))))
520 (define (root-size image)
521 "Return the root partition size of IMAGE."
522 (let* ((image-size (image-size image))
523 (root-partition (find-root-partition image))
524 (root-size (partition-size root-partition)))
526 ((and (eq? root-size 'guess) image-size)
530 (define* (image-with-os* base-image os)
531 "Return an image based on BASE-IMAGE but with the operating-system field set
532 to OS. Also set the UUID and the size of the root partition."
533 (define root-file-system
536 (string=? (file-system-mount-point fs) "/"))
537 (operating-system-file-systems os)))
541 (operating-system os)
544 (if (root-partition? p)
547 (uuid (file-system-device root-file-system))
548 (size (root-size base-image)))
550 (image-partitions base-image)))))
552 (define (operating-system-for-image image)
553 "Return an operating-system based on the one specified in IMAGE, but
554 suitable for image creation. Assign an UUID to the root file-system, so that
555 it can be used for bootloading."
556 (define volatile-root? (image-volatile-root? image))
558 (define (root-uuid os)
559 ;; UUID of the root file system, computed in a deterministic fashion.
560 ;; This is what we use to locate the root file system so it has to be
561 ;; different from the user's own file system UUIDs.
562 (let ((type (if (eq? (image-format image) 'iso9660)
565 (operating-system-uuid os type)))
567 (let* ((root-file-system-type (image->root-file-system image))
568 (base-os (image-operating-system image))
569 (file-systems-to-keep
572 (string=? (file-system-mount-point fs) "/"))
573 (operating-system-file-systems base-os)))
574 (format (image-format image))
578 (initrd (lambda (file-systems . rest)
579 (apply (operating-system-initrd base-os)
581 #:volatile-root? volatile-root?
583 (bootloader (if (eq? format 'iso9660)
584 (bootloader-configuration
586 (operating-system-bootloader base-os))
587 (bootloader grub-mkrescue-bootloader))
588 (operating-system-bootloader base-os)))
589 (file-systems (cons (file-system
591 (device "/dev/placeholder")
592 (type root-file-system-type))
593 file-systems-to-keep))))
594 (uuid (root-uuid os)))
597 (file-systems (cons (file-system
600 (type root-file-system-type))
601 file-systems-to-keep)))))
603 (define* (system-image image)
604 "Return the derivation of IMAGE. It can be a raw disk-image or an ISO9660
605 image, depending on IMAGE format."
606 (define substitutable? (image-substitutable? image))
607 (define target (image-target image))
609 (with-parameters ((%current-target-system target))
610 (let* ((os (operating-system-for-image image))
611 (image* (image-with-os* image os))
612 (image-format (image-format image))
613 (register-closures? (has-guix-service-type? os))
614 (bootcfg (operating-system-bootcfg os))
615 (bootloader (bootloader-configuration-bootloader
616 (operating-system-bootloader os))))
618 ((memq image-format '(disk-image compressed-qcow2))
619 (system-disk-image image*
621 #:bootloader bootloader
622 #:register-closures? register-closures?
623 #:inputs `(("system" ,os)
624 ("bootcfg" ,bootcfg))))
625 ((memq image-format '(iso9660))
626 (system-iso9660-image
629 #:bootloader bootloader
630 #:register-closures? register-closures?
631 #:inputs `(("system" ,os)
632 ("bootcfg" ,bootcfg))
633 ;; Make sure to use a mode that does no imply
634 ;; HFS+ tree creation that may fail with:
636 ;; "libisofs: FAILURE : Too much files to mangle,
637 ;; cannot guarantee unique file names"
639 ;; This happens if some limits are exceeded, see:
640 ;; https://lists.gnu.org/archive/html/grub-devel/2020-06/msg00048.html
641 #:grub-mkrescue-environment
642 '(("MKRESCUE_SED_MODE" . "mbr_only"))))))))
649 (define (image-modules)
650 "Return the list of image modules."
651 (cons (resolve-interface '(gnu system image))
652 (all-modules (map (lambda (entry)
653 `(,entry . "gnu/system/images/"))
655 #:warn warn-about-load-error)))
658 ;; The list of publically-known image types.
659 (delay (fold-module-public-variables (lambda (obj result)
660 (if (image-type? obj)
666 (define (lookup-image-type-by-name name)
667 "Return the image type called NAME."
668 (or (srfi-1:find (lambda (image-type)
669 (eq? name (image-type-name image-type)))
670 (force %image-types))
672 (formatted-message (G_ "~a: no such image type") name))))
674 ;;; image.scm ends here