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 gexp)
22 #:use-module (guix modules)
23 #:use-module (guix monads)
24 #:use-module (guix records)
25 #:use-module (guix store)
26 #:use-module (guix ui)
27 #:use-module (guix utils)
28 #:use-module ((guix self) #:select (make-config.scm))
29 #:use-module (gnu bootloader)
30 #:use-module (gnu bootloader grub)
31 #:use-module (gnu image)
32 #:use-module (gnu services)
33 #:use-module (gnu services base)
34 #:use-module (gnu system)
35 #:use-module (gnu system file-systems)
36 #:use-module (gnu system uuid)
37 #:use-module (gnu system vm)
38 #:use-module (guix packages)
39 #:use-module (gnu packages base)
40 #:use-module (gnu packages bootloaders)
41 #:use-module (gnu packages cdrom)
42 #:use-module (gnu packages disk)
43 #:use-module (gnu packages gawk)
44 #:use-module (gnu packages genimage)
45 #:use-module (gnu packages guile)
46 #:autoload (gnu packages gnupg) (guile-gcrypt)
47 #:use-module (gnu packages hurd)
48 #:use-module (gnu packages linux)
49 #:use-module (gnu packages mtools)
50 #:use-module ((srfi srfi-1) #:prefix srfi-1:)
51 #:use-module (srfi srfi-11)
52 #:use-module (srfi srfi-26)
53 #:use-module (srfi srfi-35)
54 #:use-module (rnrs bytevectors)
55 #:use-module (ice-9 match)
71 ;;; Images definitions.
74 ;; This is the offset before the first partition. GRUB will install itself in
76 (define root-offset (* 512 2048))
78 ;; Generic root partition label.
79 (define root-label "Guix_image")
83 (size (* 40 (expt 2 20)))
85 (label "GNU-ESP") ;cosmetic only
86 ;; Use "vfat" here since this property is used when mounting. The actual
87 ;; FAT-ness is based on file system size (16 in this case).
90 (initializer (gexp initialize-efi-partition))))
92 (define root-partition
98 (initializer (gexp initialize-root-partition))))
100 (define efi-disk-image
103 (partitions (list esp-partition root-partition))))
105 (define iso9660-image
120 ;; Select (guix …) and (gnu …) modules, except (guix config).
123 (('guix rest ...) #t)
127 (define (partition->gexp partition)
128 "Turn PARTITION, a <partition> object, into a list-valued gexp suitable for
129 'make-partition-image'."
130 #~'(#$@(list (partition-size partition))
131 #$(partition-file-system partition)
132 #$(partition-file-system-options partition)
133 #$(partition-label partition)
134 #$(and=> (partition-uuid partition)
137 (define gcrypt-sqlite3&co
138 ;; Guile-Gcrypt, Guile-SQLite3, and their propagated inputs.
142 (match (package-transitive-propagated-inputs package)
143 (((labels packages) ...)
145 (list guile-gcrypt guile-sqlite3)))
147 (define-syntax-rule (with-imported-modules* gexp* ...)
148 (with-extensions gcrypt-sqlite3&co
149 (with-imported-modules `(,@(source-module-closure
152 (gnu build hurd-boot)
153 (gnu build linux-boot)
154 (guix store database))
155 #:select? not-config?)
156 ((guix config) => ,(make-config.scm)))
158 (use-modules (gnu build vm)
160 (gnu build hurd-boot)
161 (gnu build linux-boot)
162 (guix store database)
166 (define (root-partition? partition)
167 "Return true if PARTITION is the root partition, false otherwise."
168 (member 'boot (partition-flags partition)))
170 (define (find-root-partition image)
171 "Return the root partition of the given IMAGE."
172 (srfi-1:find root-partition? (image-partitions image)))
174 (define (root-partition-index image)
175 "Return the index of the root partition of the given IMAGE."
176 (1+ (srfi-1:list-index root-partition? (image-partitions image))))
183 (define* (system-disk-image image
190 "Return as a file-like object, the disk-image described by IMAGE. Said
191 image can be copied on a USB stick as is. BOOTLOADER is the bootloader that
192 will be installed and configured according to BOOTCFG parameter.
194 Raw images of the IMAGE partitions are first created. Then, genimage is used
195 to assemble the partition images into a disk-image without resorting to a
198 INPUTS is a list of inputs (as for packages). When REGISTER-CLOSURES? is
199 true, register INPUTS in the store database of the image so that Guix can be
202 (define genimage-name "image")
204 (define (image->genimage-cfg image)
205 ;; Return as a file-like object, the genimage configuration file
206 ;; describing the given IMAGE.
207 (define (format->image-type format)
208 ;; Return the genimage format corresponding to FORMAT. For now, only
209 ;; the hdimage format (raw disk-image) is supported.
211 ((disk-image) "hdimage")
216 (format #f (G_ "Unsupported image type ~a~%.") format))))))))
218 (define (partition->dos-type partition)
219 ;; Return the MBR partition type corresponding to the given PARTITION.
220 ;; See: https://en.wikipedia.org/wiki/Partition_type.
221 (let ((flags (partition-flags partition)))
223 ((member 'esp flags) "0xEF")
226 (define (partition-image partition)
227 ;; Return as a file-like object, an image of the given PARTITION. A
228 ;; directory, filled by calling the PARTITION initializer procedure, is
229 ;; first created within the store. Then, an image of this directory is
230 ;; created using tools such as 'mke2fs' or 'mkdosfs', depending on the
231 ;; partition file-system type.
232 (let* ((os (image-operating-system image))
233 (schema (local-file (search-path %load-path
234 "guix/store/schema.sql")))
238 (type (partition-file-system partition))
240 (with-imported-modules*
241 (let ((initializer #$(partition-initializer partition))
242 (inputs '#+(list e2fsprogs fakeroot dosfstools mtools))
243 (image-root "tmp-root"))
244 (sql-schema #$schema)
246 (set-path-environment-variable "PATH" '("bin" "sbin") inputs)
248 ;; Allow non-ASCII file names--e.g., 'nss-certs'--to be
250 (setenv "GUIX_LOCPATH"
251 #+(file-append glibc-utf8-locales "/lib/locale"))
252 (setlocale LC_ALL "en_US.utf8")
254 (initializer image-root
255 #:references-graphs '#$graph
257 #:system-directory #$os
258 #:grub-efi #+grub-efi
260 #+(bootloader-package bootloader)
261 #:bootloader-installer
262 #+(bootloader-installer bootloader)
265 #$(bootloader-configuration-file bootloader))
266 (make-partition-image #$(partition->gexp partition)
269 (computed-file "partition.img" image-builder
270 #:options `(#:local-build? #t ;typically large file
271 #:references-graphs ,inputs))))
273 (define (partition->config partition)
274 ;; Return the genimage partition configuration for PARTITION.
275 (let ((label (partition-label partition))
276 (dos-type (partition->dos-type partition))
277 (image (partition-image partition))
278 (offset (partition-offset partition)))
279 #~(format #f "~/partition ~a {
280 ~/~/partition-type = ~a
289 (let* ((format (image-format image))
290 (image-type (format->image-type format))
291 (partitions (image-partitions image))
292 (partitions-config (map partition->config partitions))
295 (let ((format (@ (ice-9 format) format)))
296 (call-with-output-file #$output
303 }~%" #$genimage-name #$image-type (list #$@partitions-config))))))))
304 (computed-file "genimage.cfg" builder)))
306 (let* ((image-name (image-name image))
308 (symbol->string image-name)
310 (substitutable? (image-substitutable? image))
312 (with-imported-modules*
313 (let ((inputs '#+(list genimage coreutils findutils))
314 (bootloader-installer
315 #+(bootloader-disk-image-installer bootloader)))
316 (set-path-environment-variable "PATH" '("bin" "sbin") inputs)
317 (genimage #$(image->genimage-cfg image) #$output)
318 ;; Install the bootloader directly on the disk-image.
319 (when bootloader-installer
320 (bootloader-installer
321 #+(bootloader-package bootloader)
322 #$(root-partition-index image)
323 (string-append #$output "/" #$genimage-name))))))
324 (image-dir (computed-file "image-dir" builder)))
327 (string-append #$image-dir "/" #$genimage-name)
329 #:options `(#:local-build? #t ;typically large file
330 #:substitutable? ,substitutable?))))
337 (define (has-guix-service-type? os)
338 "Return true if OS contains a service of the type GUIX-SERVICE-TYPE."
339 (not (not (srfi-1:find (lambda (service)
340 (eq? (service-kind service) guix-service-type))
341 (operating-system-services os)))))
343 (define* (system-iso9660-image image
345 (name "iso9660-image")
350 (grub-mkrescue-environment '()))
351 "Return as a file-like object a bootable, stand-alone iso9660 image.
353 INPUTS is a list of inputs (as for packages). When REGISTER-CLOSURES? is
354 true, register INPUTS in the store database of the image so that Guix can be
357 (match (image-partitions image)
359 (partition-label partition))))
362 (match (image-partitions image)
364 (uuid-bytevector (partition-uuid partition)))))
366 (let* ((os (image-operating-system image))
367 (bootloader (bootloader-package bootloader))
368 (compression? (image-compression? image))
369 (substitutable? (image-substitutable? image))
370 (schema (local-file (search-path %load-path
371 "guix/store/schema.sql")))
376 (with-imported-modules*
377 (let* ((inputs '#$(list parted e2fsprogs dosfstools xorriso
378 sed grep coreutils findutils gawk))
379 (image-root "tmp-root"))
380 (sql-schema #$schema)
382 ;; Allow non-ASCII file names--e.g., 'nss-certs'--to be decoded.
383 (setenv "GUIX_LOCPATH"
384 #+(file-append glibc-utf8-locales "/lib/locale"))
386 (setlocale LC_ALL "en_US.utf8")
388 (set-path-environment-variable "PATH" '("bin" "sbin") inputs)
390 (initialize-root-partition image-root
391 #:references-graphs '#$graph
393 #:system-directory #$os)
394 (make-iso9660-image #$xorriso
395 '#$grub-mkrescue-environment
401 #:references-graphs '#$graph
402 #:register-closures? #$register-closures?
403 #:compression? #$compression?
404 #:volume-id #$root-label
405 #:volume-uuid #$root-uuid)))))
406 (computed-file name builder
407 #:options `(#:local-build? #t ;typically large file
408 #:references-graphs ,inputs
409 #:substitutable? ,substitutable?))))
411 (define (image-with-label base-image label)
412 "The volume ID of an ISO is the label of the first partition. This procedure
413 returns an image record where the first partition's label is set to <label>."
417 (match (image-partitions base-image)
430 (define (image->root-file-system image)
431 "Return the IMAGE root partition file-system type."
432 (let ((format (image-format image)))
433 (if (eq? format 'iso9660)
435 (partition-file-system (find-root-partition image)))))
437 (define (root-size image)
438 "Return the root partition size of IMAGE."
439 (let* ((image-size (image-size image))
440 (root-partition (find-root-partition image))
441 (root-size (partition-size root-partition)))
443 ((and (eq? root-size 'guess) image-size)
447 (define* (image-with-os base-image os)
448 "Return an image based on BASE-IMAGE but with the operating-system field set
449 to OS. Also set the UUID and the size of the root partition."
450 (define root-file-system
453 (string=? (file-system-mount-point fs) "/"))
454 (operating-system-file-systems os)))
458 (operating-system os)
461 (if (root-partition? p)
464 (uuid (file-system-device root-file-system))
465 (size (root-size base-image)))
467 (image-partitions base-image)))))
469 (define (operating-system-for-image image)
470 "Return an operating-system based on the one specified in IMAGE, but
471 suitable for image creation. Assign an UUID to the root file-system, so that
472 it can be used for bootloading."
473 (define volatile-root? (image-volatile-root? image))
475 (define (root-uuid os)
476 ;; UUID of the root file system, computed in a deterministic fashion.
477 ;; This is what we use to locate the root file system so it has to be
478 ;; different from the user's own file system UUIDs.
479 (let ((type (if (eq? (image-format image) 'iso9660)
482 (operating-system-uuid os type)))
484 (let* ((root-file-system-type (image->root-file-system image))
485 (base-os (image-operating-system image))
486 (file-systems-to-keep
489 (string=? (file-system-mount-point fs) "/"))
490 (operating-system-file-systems base-os)))
491 (format (image-format image))
495 (initrd (lambda (file-systems . rest)
496 (apply (operating-system-initrd base-os)
498 #:volatile-root? volatile-root?
500 (bootloader (if (eq? format 'iso9660)
501 (bootloader-configuration
503 (operating-system-bootloader base-os))
504 (bootloader grub-mkrescue-bootloader))
505 (operating-system-bootloader base-os)))
506 (file-systems (cons (file-system
508 (device "/dev/placeholder")
509 (type root-file-system-type))
510 file-systems-to-keep))))
511 (uuid (root-uuid os)))
514 (file-systems (cons (file-system
517 (type root-file-system-type))
518 file-systems-to-keep)))))
520 (define* (system-image image)
521 "Return the derivation of IMAGE. It can be a raw disk-image or an ISO9660
522 image, depending on IMAGE format."
523 (define substitutable? (image-substitutable? image))
524 (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 (register-closures? (has-guix-service-type? os))
530 (bootcfg (operating-system-bootcfg os))
531 (bootloader (bootloader-configuration-bootloader
532 (operating-system-bootloader os))))
533 (case (image-format image)
535 (system-disk-image image*
537 #:bootloader bootloader
538 #:register-closures? register-closures?
539 #:inputs `(("system" ,os)
540 ("bootcfg" ,bootcfg))))
542 (system-iso9660-image
545 #:bootloader bootloader
546 #:register-closures? register-closures?
547 #:inputs `(("system" ,os)
548 ("bootcfg" ,bootcfg))
549 ;; Make sure to use a mode that does no imply
550 ;; HFS+ tree creation that may fail with:
552 ;; "libisofs: FAILURE : Too much files to mangle,
553 ;; cannot guarantee unique file names"
555 ;; This happens if some limits are exceeded, see:
556 ;; https://lists.gnu.org/archive/html/grub-devel/2020-06/msg00048.html
557 #:grub-mkrescue-environment
558 '(("MKRESCUE_SED_MODE" . "mbr_only"))))))))
560 (define (find-image file-system-type target)
561 "Find and return an image built that could match the given FILE-SYSTEM-TYPE,
562 built for TARGET. This is useful to adapt to interfaces written before the
563 addition of the <image> record."
564 (match file-system-type
565 ("iso9660" iso9660-image)
568 (hurd-triplet? target))
569 (module-ref (resolve-interface '(gnu system images hurd))
574 ;;; image.scm ends here