image: Make 'find-image' non-monadic.
[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)
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)
c77b9285 47 #:use-module (gnu packages hurd)
f19cf27c
MO
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)
56 #:export (esp-partition
57 root-partition
58
c77b9285 59 hurd-disk-image
f19cf27c
MO
60 efi-disk-image
61 iso9660-image
62
63 find-image
64 system-image))
65
66\f
67;;;
68;;; Images definitions.
69;;;
70
b7b45372
MO
71;; This is the offset before the first partition. GRUB will install itself in
72;; this post-MBR gap.
73(define root-offset (* 512 2048))
74
75;; Generic root partition label.
76(define root-label "Guix_image")
77
f19cf27c
MO
78(define esp-partition
79 (partition
80 (size (* 40 (expt 2 20)))
b7b45372 81 (offset root-offset)
f19cf27c
MO
82 (label "GNU-ESP") ;cosmetic only
83 ;; Use "vfat" here since this property is used when mounting. The actual
84 ;; FAT-ness is based on file system size (16 in this case).
85 (file-system "vfat")
86 (flags '(esp))
87 (initializer (gexp initialize-efi-partition))))
88
89(define root-partition
90 (partition
91 (size 'guess)
b7b45372 92 (label root-label)
f19cf27c
MO
93 (file-system "ext4")
94 (flags '(boot))
95 (initializer (gexp initialize-root-partition))))
96
c77b9285
MO
97(define hurd-initialize-root-partition
98 #~(lambda* (#:rest args)
99 (apply initialize-root-partition
100 (append args
101 (list #:make-device-nodes
102 make-hurd-device-nodes)))))
103
104(define hurd-disk-image
105 (image
106 (format 'disk-image)
107 (partitions
108 (list (partition
109 (size 'guess)
110 (offset root-offset)
111 (label root-label)
112 (file-system "ext2")
113 (file-system-options '("-o" "hurd" "-O" "ext_attr"))
114 (flags '(boot))
115 (initializer hurd-initialize-root-partition))))))
116
f19cf27c
MO
117(define efi-disk-image
118 (image
119 (format 'disk-image)
120 (partitions (list esp-partition root-partition))))
121
122(define iso9660-image
123 (image
124 (format 'iso9660)
125 (partitions
126 (list (partition
127 (size 'guess)
128 (label "GUIX_IMAGE")
129 (flags '(boot)))))
130 ;; XXX: Temporarily disable compression to speed-up the tests.
131 (compression? #f)))
132
133\f
134;;
135;; Helpers.
136;;
137
138(define not-config?
139 ;; Select (guix …) and (gnu …) modules, except (guix config).
140 (match-lambda
141 (('guix 'config) #f)
142 (('guix rest ...) #t)
143 (('gnu rest ...) #t)
144 (rest #f)))
145
146(define (partition->gexp partition)
147 "Turn PARTITION, a <partition> object, into a list-valued gexp suitable for
148'make-partition-image'."
149 #~'(#$@(list (partition-size partition))
150 #$(partition-file-system partition)
bd3716f6 151 #$(partition-file-system-options partition)
f19cf27c
MO
152 #$(partition-label partition)
153 #$(and=> (partition-uuid partition)
154 uuid-bytevector)))
155
156(define gcrypt-sqlite3&co
157 ;; Guile-Gcrypt, Guile-SQLite3, and their propagated inputs.
158 (srfi-1:append-map
159 (lambda (package)
160 (cons package
161 (match (package-transitive-propagated-inputs package)
162 (((labels packages) ...)
163 packages))))
164 (list guile-gcrypt guile-sqlite3)))
165
166(define-syntax-rule (with-imported-modules* gexp* ...)
167 (with-extensions gcrypt-sqlite3&co
168 (with-imported-modules `(,@(source-module-closure
169 '((gnu build vm)
170 (gnu build image)
b37c5441 171 (gnu build hurd-boot)
c77b9285 172 (gnu build linux-boot)
f19cf27c
MO
173 (guix store database))
174 #:select? not-config?)
175 ((guix config) => ,(make-config.scm)))
176 #~(begin
177 (use-modules (gnu build vm)
178 (gnu build image)
b37c5441 179 (gnu build hurd-boot)
c77b9285 180 (gnu build linux-boot)
f19cf27c
MO
181 (guix store database)
182 (guix build utils))
183 gexp* ...))))
184
7feefb3b
MO
185(define (root-partition? partition)
186 "Return true if PARTITION is the root partition, false otherwise."
187 (member 'boot (partition-flags partition)))
188
189(define (find-root-partition image)
190 "Return the root partition of the given IMAGE."
191 (srfi-1:find root-partition? (image-partitions image)))
192
193(define (root-partition-index image)
194 "Return the index of the root partition of the given IMAGE."
195 (1+ (srfi-1:list-index root-partition? (image-partitions image))))
196
f19cf27c
MO
197\f
198;;
199;; Disk image.
200;;
201
202(define* (system-disk-image image
203 #:key
204 (name "disk-image")
205 bootcfg
206 bootloader
207 register-closures?
208 (inputs '()))
209 "Return as a file-like object, the disk-image described by IMAGE. Said
210image can be copied on a USB stick as is. BOOTLOADER is the bootloader that
211will be installed and configured according to BOOTCFG parameter.
212
213Raw images of the IMAGE partitions are first created. Then, genimage is used
214to assemble the partition images into a disk-image without resorting to a
215virtual machine.
216
217INPUTS is a list of inputs (as for packages). When REGISTER-CLOSURES? is
218true, register INPUTS in the store database of the image so that Guix can be
219used in the image."
220
221 (define genimage-name "image")
222
223 (define (image->genimage-cfg image)
224 ;; Return as a file-like object, the genimage configuration file
225 ;; describing the given IMAGE.
226 (define (format->image-type format)
227 ;; Return the genimage format corresponding to FORMAT. For now, only
228 ;; the hdimage format (raw disk-image) is supported.
229 (case format
230 ((disk-image) "hdimage")
231 (else
232 (raise (condition
233 (&message
234 (message
235 (format #f (G_ "Unsupported image type ~a~%.") format))))))))
236
237 (define (partition->dos-type partition)
238 ;; Return the MBR partition type corresponding to the given PARTITION.
239 ;; See: https://en.wikipedia.org/wiki/Partition_type.
240 (let ((flags (partition-flags partition)))
241 (cond
242 ((member 'esp flags) "0xEF")
243 (else "0x83"))))
244
245 (define (partition-image partition)
246 ;; Return as a file-like object, an image of the given PARTITION. A
247 ;; directory, filled by calling the PARTITION initializer procedure, is
248 ;; first created within the store. Then, an image of this directory is
249 ;; created using tools such as 'mke2fs' or 'mkdosfs', depending on the
250 ;; partition file-system type.
251 (let* ((os (image-operating-system image))
252 (schema (local-file (search-path %load-path
253 "guix/store/schema.sql")))
254 (graph (match inputs
255 (((names . _) ...)
256 names)))
257 (root-builder
258 (with-imported-modules*
259 (let* ((initializer #$(partition-initializer partition)))
260 (sql-schema #$schema)
261
262 ;; Allow non-ASCII file names--e.g., 'nss-certs'--to be
263 ;; decoded.
264 (setenv "GUIX_LOCPATH"
265 #+(file-append glibc-utf8-locales "/lib/locale"))
266 (setlocale LC_ALL "en_US.utf8")
267
268 (initializer #$output
269 #:references-graphs '#$graph
270 #:deduplicate? #f
271 #:system-directory #$os
05f37c16 272 #:grub-efi #+grub-efi
f19cf27c 273 #:bootloader-package
9c1adb24
MO
274 #+(bootloader-package bootloader)
275 #:bootloader-installer
276 #+(bootloader-installer bootloader)
f19cf27c
MO
277 #:bootcfg #$bootcfg
278 #:bootcfg-location
279 #$(bootloader-configuration-file bootloader)))))
280 (image-root
281 (computed-file "partition-image-root" root-builder
282 #:options `(#:references-graphs ,inputs)))
283 (type (partition-file-system partition))
284 (image-builder
285 (with-imported-modules*
1dbd0005 286 (let ((inputs '#+(list e2fsprogs dosfstools mtools)))
f19cf27c
MO
287 (set-path-environment-variable "PATH" '("bin" "sbin") inputs)
288 (make-partition-image #$(partition->gexp partition)
289 #$output
290 #$image-root)))))
291 (computed-file "partition.img" image-builder)))
292
293 (define (partition->config partition)
294 ;; Return the genimage partition configuration for PARTITION.
295 (let ((label (partition-label partition))
296 (dos-type (partition->dos-type partition))
1b4fa785
MO
297 (image (partition-image partition))
298 (offset (partition-offset partition)))
f19cf27c 299 #~(format #f "~/partition ~a {
7d4ecda6
MO
300~/~/partition-type = ~a
301~/~/image = \"~a\"
302~/~/offset = \"~a\"
303~/}"
1b4fa785
MO
304 #$label
305 #$dos-type
306 #$image
1dd7b87f 307 #$offset)))
f19cf27c
MO
308
309 (let* ((format (image-format image))
310 (image-type (format->image-type format))
311 (partitions (image-partitions image))
312 (partitions-config (map partition->config partitions))
313 (builder
314 #~(begin
315 (let ((format (@ (ice-9 format) format)))
316 (call-with-output-file #$output
317 (lambda (port)
318 (format port
319 "\
320image ~a {
321~/~a {}
322~{~a~^~%~}
323}~%" #$genimage-name #$image-type (list #$@partitions-config))))))))
324 (computed-file "genimage.cfg" builder)))
325
326 (let* ((substitutable? (image-substitutable? image))
327 (builder
328 (with-imported-modules*
7feefb3b
MO
329 (let ((inputs '#+(list genimage coreutils findutils))
330 (bootloader-installer
331 #+(bootloader-disk-image-installer bootloader)))
f19cf27c 332 (set-path-environment-variable "PATH" '("bin" "sbin") inputs)
7feefb3b
MO
333 (genimage #$(image->genimage-cfg image) #$output)
334 ;; Install the bootloader directly on the disk-image.
335 (when bootloader-installer
336 (bootloader-installer
337 #+(bootloader-package bootloader)
338 #$(root-partition-index image)
339 (string-append #$output "/" #$genimage-name))))))
f19cf27c
MO
340 (image-dir (computed-file "image-dir" builder)))
341 (computed-file name
342 #~(symlink
343 (string-append #$image-dir "/" #$genimage-name)
344 #$output)
345 #:options `(#:substitutable? ,substitutable?))))
346
347\f
348;;
349;; ISO9660 image.
350;;
351
352(define (has-guix-service-type? os)
353 "Return true if OS contains a service of the type GUIX-SERVICE-TYPE."
354 (not (not (srfi-1:find (lambda (service)
355 (eq? (service-kind service) guix-service-type))
356 (operating-system-services os)))))
357
358(define* (system-iso9660-image image
359 #:key
360 (name "iso9660-image")
361 bootcfg
362 bootloader
363 register-closures?
364 (inputs '())
365 (grub-mkrescue-environment '()))
366 "Return as a file-like object a bootable, stand-alone iso9660 image.
367
368INPUTS is a list of inputs (as for packages). When REGISTER-CLOSURES? is
369true, register INPUTS in the store database of the image so that Guix can be
370used in the image. "
371 (define root-label
372 (match (image-partitions image)
373 ((partition)
374 (partition-label partition))))
375
376 (define root-uuid
377 (match (image-partitions image)
378 ((partition)
379 (uuid-bytevector (partition-uuid partition)))))
380
381 (let* ((os (image-operating-system image))
382 (bootloader (bootloader-package bootloader))
383 (compression? (image-compression? image))
384 (substitutable? (image-substitutable? image))
385 (schema (local-file (search-path %load-path
386 "guix/store/schema.sql")))
387 (graph (match inputs
388 (((names . _) ...)
389 names)))
390 (root-builder
391 (with-imported-modules*
392 (sql-schema #$schema)
393
394 ;; Allow non-ASCII file names--e.g., 'nss-certs'--to be decoded.
395 (setenv "GUIX_LOCPATH"
396 #+(file-append glibc-utf8-locales "/lib/locale"))
397 (setlocale LC_ALL "en_US.utf8")
398
399 (initialize-root-partition #$output
400 #:references-graphs '#$graph
401 #:deduplicate? #f
402 #:system-directory #$os)))
403 (image-root
404 (computed-file "image-root" root-builder
405 #:options `(#:references-graphs ,inputs)))
406 (builder
407 (with-imported-modules*
408 (let* ((inputs '#$(list parted e2fsprogs dosfstools xorriso
409 sed grep coreutils findutils gawk)))
410 (set-path-environment-variable "PATH" '("bin" "sbin") inputs)
411 (make-iso9660-image #$xorriso
412 '#$grub-mkrescue-environment
413 #$bootloader
414 #$bootcfg
415 #$os
416 #$image-root
417 #$output
418 #:references-graphs '#$graph
419 #:register-closures? #$register-closures?
420 #:compression? #$compression?
421 #:volume-id #$root-label
422 #:volume-uuid #$root-uuid)))))
423 (computed-file name builder
424 #:options `(#:references-graphs ,inputs
425 #:substitutable? ,substitutable?))))
426
427\f
428;;
429;; Image creation.
430;;
431
f19cf27c
MO
432(define (image->root-file-system image)
433 "Return the IMAGE root partition file-system type."
434 (let ((format (image-format image)))
435 (if (eq? format 'iso9660)
436 "iso9660"
437 (partition-file-system (find-root-partition image)))))
438
439(define (root-size image)
440 "Return the root partition size of IMAGE."
441 (let* ((image-size (image-size image))
442 (root-partition (find-root-partition image))
443 (root-size (partition-size root-partition)))
444 (cond
445 ((and (eq? root-size 'guess) image-size)
446 image-size)
447 (else root-size))))
448
449(define* (image-with-os base-image os)
450 "Return an image based on BASE-IMAGE but with the operating-system field set
451to OS. Also set the UUID and the size of the root partition."
452 (define root-file-system
453 (srfi-1:find
454 (lambda (fs)
455 (string=? (file-system-mount-point fs) "/"))
456 (operating-system-file-systems os)))
457
74938105
MO
458 (image
459 (inherit base-image)
460 (operating-system os)
461 (partitions
462 (map (lambda (p)
463 (if (root-partition? p)
464 (partition
465 (inherit p)
466 (uuid (file-system-device root-file-system))
467 (size (root-size base-image)))
468 p))
469 (image-partitions base-image)))))
f19cf27c
MO
470
471(define (operating-system-for-image image)
472 "Return an operating-system based on the one specified in IMAGE, but
473suitable for image creation. Assign an UUID to the root file-system, so that
474it can be used for bootloading."
475 (define volatile-root? (image-volatile-root? image))
476
477 (define (root-uuid os)
478 ;; UUID of the root file system, computed in a deterministic fashion.
479 ;; This is what we use to locate the root file system so it has to be
480 ;; different from the user's own file system UUIDs.
481 (let ((type (if (eq? (image-format image) 'iso9660)
482 'iso9660
483 'dce)))
484 (operating-system-uuid os type)))
485
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
e3f0155c 522(define* (system-image image)
f19cf27c
MO
523 "Return the derivation of IMAGE. It can be a raw disk-image or an ISO9660
524image, depending on IMAGE format."
525 (define substitutable? (image-substitutable? image))
526
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)
534 ((disk-image)
535 (system-disk-image image*
536 #:bootcfg bootcfg
537 #:bootloader bootloader
538 #:register-closures? register-closures?
539 #:inputs `(("system" ,os)
540 ("bootcfg" ,bootcfg))))
541 ((iso9660)
fd1e8852
MO
542 (system-iso9660-image
543 image*
544 #:bootcfg bootcfg
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:
551 ;;
552 ;; "libisofs: FAILURE : Too much files to mangle,
553 ;; cannot guarantee unique file names"
554 ;;
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")))))))
f19cf27c 559
7ca533c7
JN
560(define (find-image file-system-type target)
561 "Find and return an image built that could match the given FILE-SYSTEM-TYPE,
562built for TARGET. This is useful to adapt to interfaces written before the
563addition of the <image> record."
564 (match file-system-type
565 ("iso9660" iso9660-image)
566 (_ (cond
567 ((and target
568 (hurd-triplet? target))
569 hurd-disk-image)
570 (else
571 efi-disk-image)))))
f19cf27c
MO
572
573;;; image.scm ends here