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