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)
70 ;;; Images definitions.
73 ;; This is the offset before the first partition. GRUB will install itself in
75 (define root-offset (* 512 2048))
77 ;; Generic root partition label.
78 (define root-label "Guix_image")
82 (size (* 40 (expt 2 20)))
84 (label "GNU-ESP") ;cosmetic only
85 ;; Use "vfat" here since this property is used when mounting. The actual
86 ;; FAT-ness is based on file system size (16 in this case).
89 (initializer (gexp initialize-efi-partition))))
91 (define root-partition
97 (initializer (gexp initialize-root-partition))))
99 (define hurd-initialize-root-partition
100 #~(lambda* (#:rest args)
101 (apply initialize-root-partition
103 (list #:make-device-nodes
104 make-hurd-device-nodes)))))
106 (define efi-disk-image
109 (partitions (list esp-partition root-partition))))
111 (define iso9660-image
119 ;; XXX: Temporarily disable compression to speed-up the tests.
128 ;; Select (guix …) and (gnu …) modules, except (guix config).
131 (('guix rest ...) #t)
135 (define (partition->gexp partition)
136 "Turn PARTITION, a <partition> object, into a list-valued gexp suitable for
137 'make-partition-image'."
138 #~'(#$@(list (partition-size partition))
139 #$(partition-file-system partition)
140 #$(partition-file-system-options partition)
141 #$(partition-label partition)
142 #$(and=> (partition-uuid partition)
145 (define gcrypt-sqlite3&co
146 ;; Guile-Gcrypt, Guile-SQLite3, and their propagated inputs.
150 (match (package-transitive-propagated-inputs package)
151 (((labels packages) ...)
153 (list guile-gcrypt guile-sqlite3)))
155 (define-syntax-rule (with-imported-modules* gexp* ...)
156 (with-extensions gcrypt-sqlite3&co
157 (with-imported-modules `(,@(source-module-closure
160 (gnu build hurd-boot)
161 (gnu build linux-boot)
162 (guix store database))
163 #:select? not-config?)
164 ((guix config) => ,(make-config.scm)))
166 (use-modules (gnu build vm)
168 (gnu build hurd-boot)
169 (gnu build linux-boot)
170 (guix store database)
174 (define (root-partition? partition)
175 "Return true if PARTITION is the root partition, false otherwise."
176 (member 'boot (partition-flags partition)))
178 (define (find-root-partition image)
179 "Return the root partition of the given IMAGE."
180 (srfi-1:find root-partition? (image-partitions image)))
182 (define (root-partition-index image)
183 "Return the index of the root partition of the given IMAGE."
184 (1+ (srfi-1:list-index root-partition? (image-partitions image))))
191 (define* (system-disk-image image
198 "Return as a file-like object, the disk-image described by IMAGE. Said
199 image can be copied on a USB stick as is. BOOTLOADER is the bootloader that
200 will be installed and configured according to BOOTCFG parameter.
202 Raw images of the IMAGE partitions are first created. Then, genimage is used
203 to assemble the partition images into a disk-image without resorting to a
206 INPUTS is a list of inputs (as for packages). When REGISTER-CLOSURES? is
207 true, register INPUTS in the store database of the image so that Guix can be
210 (define genimage-name "image")
212 (define (image->genimage-cfg image)
213 ;; Return as a file-like object, the genimage configuration file
214 ;; describing the given IMAGE.
215 (define (format->image-type format)
216 ;; Return the genimage format corresponding to FORMAT. For now, only
217 ;; the hdimage format (raw disk-image) is supported.
219 ((disk-image) "hdimage")
224 (format #f (G_ "Unsupported image type ~a~%.") format))))))))
226 (define (partition->dos-type partition)
227 ;; Return the MBR partition type corresponding to the given PARTITION.
228 ;; See: https://en.wikipedia.org/wiki/Partition_type.
229 (let ((flags (partition-flags partition)))
231 ((member 'esp flags) "0xEF")
234 (define (partition-image partition)
235 ;; Return as a file-like object, an image of the given PARTITION. A
236 ;; directory, filled by calling the PARTITION initializer procedure, is
237 ;; first created within the store. Then, an image of this directory is
238 ;; created using tools such as 'mke2fs' or 'mkdosfs', depending on the
239 ;; partition file-system type.
240 (let* ((os (image-operating-system image))
241 (schema (local-file (search-path %load-path
242 "guix/store/schema.sql")))
246 (type (partition-file-system partition))
248 (with-imported-modules*
249 (let ((initializer #$(partition-initializer partition))
250 (inputs '#+(list e2fsprogs fakeroot dosfstools mtools))
251 (image-root "tmp-root"))
252 (sql-schema #$schema)
254 (set-path-environment-variable "PATH" '("bin" "sbin") inputs)
256 ;; Allow non-ASCII file names--e.g., 'nss-certs'--to be
258 (setenv "GUIX_LOCPATH"
259 #+(file-append glibc-utf8-locales "/lib/locale"))
260 (setlocale LC_ALL "en_US.utf8")
262 (initializer image-root
263 #:references-graphs '#$graph
265 #:system-directory #$os
266 #:grub-efi #+grub-efi
268 #+(bootloader-package bootloader)
269 #:bootloader-installer
270 #+(bootloader-installer bootloader)
273 #$(bootloader-configuration-file bootloader))
274 (make-partition-image #$(partition->gexp partition)
277 (computed-file "partition.img" image-builder
278 #:options `(#:references-graphs ,inputs))))
280 (define (partition->config partition)
281 ;; Return the genimage partition configuration for PARTITION.
282 (let ((label (partition-label partition))
283 (dos-type (partition->dos-type partition))
284 (image (partition-image partition))
285 (offset (partition-offset partition)))
286 #~(format #f "~/partition ~a {
287 ~/~/partition-type = ~a
296 (let* ((format (image-format image))
297 (image-type (format->image-type format))
298 (partitions (image-partitions image))
299 (partitions-config (map partition->config partitions))
302 (let ((format (@ (ice-9 format) format)))
303 (call-with-output-file #$output
310 }~%" #$genimage-name #$image-type (list #$@partitions-config))))))))
311 (computed-file "genimage.cfg" builder)))
313 (let* ((substitutable? (image-substitutable? image))
315 (with-imported-modules*
316 (let ((inputs '#+(list genimage coreutils findutils))
317 (bootloader-installer
318 #+(bootloader-disk-image-installer bootloader)))
319 (set-path-environment-variable "PATH" '("bin" "sbin") inputs)
320 (genimage #$(image->genimage-cfg image) #$output)
321 ;; Install the bootloader directly on the disk-image.
322 (when bootloader-installer
323 (bootloader-installer
324 #+(bootloader-package bootloader)
325 #$(root-partition-index image)
326 (string-append #$output "/" #$genimage-name))))))
327 (image-dir (computed-file "image-dir" builder)))
330 (string-append #$image-dir "/" #$genimage-name)
332 #:options `(#:substitutable? ,substitutable?))))
339 (define (has-guix-service-type? os)
340 "Return true if OS contains a service of the type GUIX-SERVICE-TYPE."
341 (not (not (srfi-1:find (lambda (service)
342 (eq? (service-kind service) guix-service-type))
343 (operating-system-services os)))))
345 (define* (system-iso9660-image image
347 (name "iso9660-image")
352 (grub-mkrescue-environment '()))
353 "Return as a file-like object a bootable, stand-alone iso9660 image.
355 INPUTS is a list of inputs (as for packages). When REGISTER-CLOSURES? is
356 true, register INPUTS in the store database of the image so that Guix can be
359 (match (image-partitions image)
361 (partition-label partition))))
364 (match (image-partitions image)
366 (uuid-bytevector (partition-uuid partition)))))
368 (let* ((os (image-operating-system image))
369 (bootloader (bootloader-package bootloader))
370 (compression? (image-compression? image))
371 (substitutable? (image-substitutable? image))
372 (schema (local-file (search-path %load-path
373 "guix/store/schema.sql")))
378 (with-imported-modules*
379 (let* ((inputs '#$(list parted e2fsprogs dosfstools xorriso
380 sed grep coreutils findutils gawk))
381 (image-root "tmp-root"))
382 (sql-schema #$schema)
384 ;; Allow non-ASCII file names--e.g., 'nss-certs'--to be decoded.
385 (setenv "GUIX_LOCPATH"
386 #+(file-append glibc-utf8-locales "/lib/locale"))
388 (setlocale LC_ALL "en_US.utf8")
390 (set-path-environment-variable "PATH" '("bin" "sbin") inputs)
392 (initialize-root-partition image-root
393 #:references-graphs '#$graph
395 #:system-directory #$os)
396 (make-iso9660-image #$xorriso
397 '#$grub-mkrescue-environment
403 #:references-graphs '#$graph
404 #:register-closures? #$register-closures?
405 #:compression? #$compression?
406 #:volume-id #$root-label
407 #:volume-uuid #$root-uuid)))))
408 (computed-file name builder
409 #:options `(#:references-graphs ,inputs
410 #:substitutable? ,substitutable?))))
417 (define (image->root-file-system image)
418 "Return the IMAGE root partition file-system type."
419 (let ((format (image-format image)))
420 (if (eq? format 'iso9660)
422 (partition-file-system (find-root-partition image)))))
424 (define (root-size image)
425 "Return the root partition size of IMAGE."
426 (let* ((image-size (image-size image))
427 (root-partition (find-root-partition image))
428 (root-size (partition-size root-partition)))
430 ((and (eq? root-size 'guess) image-size)
434 (define* (image-with-os base-image os)
435 "Return an image based on BASE-IMAGE but with the operating-system field set
436 to OS. Also set the UUID and the size of the root partition."
437 (define root-file-system
440 (string=? (file-system-mount-point fs) "/"))
441 (operating-system-file-systems os)))
445 (operating-system os)
448 (if (root-partition? p)
451 (uuid (file-system-device root-file-system))
452 (size (root-size base-image)))
454 (image-partitions base-image)))))
456 (define (operating-system-for-image image)
457 "Return an operating-system based on the one specified in IMAGE, but
458 suitable for image creation. Assign an UUID to the root file-system, so that
459 it can be used for bootloading."
460 (define volatile-root? (image-volatile-root? image))
462 (define (root-uuid os)
463 ;; UUID of the root file system, computed in a deterministic fashion.
464 ;; This is what we use to locate the root file system so it has to be
465 ;; different from the user's own file system UUIDs.
466 (let ((type (if (eq? (image-format image) 'iso9660)
469 (operating-system-uuid os type)))
471 (let* ((root-file-system-type (image->root-file-system image))
472 (base-os (image-operating-system image))
473 (file-systems-to-keep
476 (string=? (file-system-mount-point fs) "/"))
477 (operating-system-file-systems base-os)))
478 (format (image-format image))
482 (initrd (lambda (file-systems . rest)
483 (apply (operating-system-initrd base-os)
485 #:volatile-root? volatile-root?
487 (bootloader (if (eq? format 'iso9660)
488 (bootloader-configuration
490 (operating-system-bootloader base-os))
491 (bootloader grub-mkrescue-bootloader))
492 (operating-system-bootloader base-os)))
493 (file-systems (cons (file-system
495 (device "/dev/placeholder")
496 (type root-file-system-type))
497 file-systems-to-keep))))
498 (uuid (root-uuid os)))
501 (file-systems (cons (file-system
504 (type root-file-system-type))
505 file-systems-to-keep)))))
507 (define* (system-image image)
508 "Return the derivation of IMAGE. It can be a raw disk-image or an ISO9660
509 image, depending on IMAGE format."
510 (define substitutable? (image-substitutable? image))
511 (define target (image-target image))
513 (with-parameters ((%current-target-system target))
514 (let* ((os (operating-system-for-image image))
515 (image* (image-with-os image os))
516 (register-closures? (has-guix-service-type? os))
517 (bootcfg (operating-system-bootcfg os))
518 (bootloader (bootloader-configuration-bootloader
519 (operating-system-bootloader os))))
520 (case (image-format image)
522 (system-disk-image image*
524 #:bootloader bootloader
525 #:register-closures? register-closures?
526 #:inputs `(("system" ,os)
527 ("bootcfg" ,bootcfg))))
529 (system-iso9660-image
532 #:bootloader bootloader
533 #:register-closures? register-closures?
534 #:inputs `(("system" ,os)
535 ("bootcfg" ,bootcfg))
536 ;; Make sure to use a mode that does no imply
537 ;; HFS+ tree creation that may fail with:
539 ;; "libisofs: FAILURE : Too much files to mangle,
540 ;; cannot guarantee unique file names"
542 ;; This happens if some limits are exceeded, see:
543 ;; https://lists.gnu.org/archive/html/grub-devel/2020-06/msg00048.html
544 #:grub-mkrescue-environment
545 '(("MKRESCUE_SED_MODE" . "mbr_only"))))))))
547 (define (find-image file-system-type target)
548 "Find and return an image built that could match the given FILE-SYSTEM-TYPE,
549 built for TARGET. This is useful to adapt to interfaces written before the
550 addition of the <image> record."
551 (match file-system-type
552 ("iso9660" iso9660-image)
555 (hurd-triplet? target))
556 (module-ref (resolve-interface '(gnu system images hurd))
561 ;;; image.scm ends here