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