guix: system: Add `--label' option.
[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
036f23f0
JL
66 system-image
67 image-with-label))
f19cf27c
MO
68
69\f
70;;;
71;;; Images definitions.
72;;;
73
b7b45372
MO
74;; This is the offset before the first partition. GRUB will install itself in
75;; this post-MBR gap.
76(define root-offset (* 512 2048))
77
78;; Generic root partition label.
79(define root-label "Guix_image")
80
f19cf27c
MO
81(define esp-partition
82 (partition
83 (size (* 40 (expt 2 20)))
b7b45372 84 (offset root-offset)
f19cf27c
MO
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).
88 (file-system "vfat")
89 (flags '(esp))
90 (initializer (gexp initialize-efi-partition))))
91
92(define root-partition
93 (partition
94 (size 'guess)
b7b45372 95 (label root-label)
f19cf27c
MO
96 (file-system "ext4")
97 (flags '(boot))
98 (initializer (gexp initialize-root-partition))))
99
100(define efi-disk-image
101 (image
102 (format 'disk-image)
103 (partitions (list esp-partition root-partition))))
104
105(define iso9660-image
106 (image
107 (format 'iso9660)
108 (partitions
109 (list (partition
110 (size 'guess)
111 (label "GUIX_IMAGE")
f56144e1 112 (flags '(boot)))))))
f19cf27c
MO
113
114\f
115;;
116;; Helpers.
117;;
118
119(define not-config?
120 ;; Select (guix …) and (gnu …) modules, except (guix config).
121 (match-lambda
122 (('guix 'config) #f)
123 (('guix rest ...) #t)
124 (('gnu rest ...) #t)
125 (rest #f)))
126
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)
bd3716f6 132 #$(partition-file-system-options partition)
f19cf27c
MO
133 #$(partition-label partition)
134 #$(and=> (partition-uuid partition)
135 uuid-bytevector)))
136
137(define gcrypt-sqlite3&co
138 ;; Guile-Gcrypt, Guile-SQLite3, and their propagated inputs.
139 (srfi-1:append-map
140 (lambda (package)
141 (cons package
142 (match (package-transitive-propagated-inputs package)
143 (((labels packages) ...)
144 packages))))
dac7dd1b 145 (list guile-gcrypt guile-sqlite3)))
f19cf27c
MO
146
147(define-syntax-rule (with-imported-modules* gexp* ...)
148 (with-extensions gcrypt-sqlite3&co
149 (with-imported-modules `(,@(source-module-closure
150 '((gnu build vm)
151 (gnu build image)
b37c5441 152 (gnu build hurd-boot)
c77b9285 153 (gnu build linux-boot)
f19cf27c
MO
154 (guix store database))
155 #:select? not-config?)
156 ((guix config) => ,(make-config.scm)))
157 #~(begin
158 (use-modules (gnu build vm)
159 (gnu build image)
b37c5441 160 (gnu build hurd-boot)
c77b9285 161 (gnu build linux-boot)
f19cf27c
MO
162 (guix store database)
163 (guix build utils))
164 gexp* ...))))
165
7feefb3b
MO
166(define (root-partition? partition)
167 "Return true if PARTITION is the root partition, false otherwise."
168 (member 'boot (partition-flags partition)))
169
170(define (find-root-partition image)
171 "Return the root partition of the given IMAGE."
172 (srfi-1:find root-partition? (image-partitions image)))
173
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))))
177
f19cf27c
MO
178\f
179;;
180;; Disk image.
181;;
182
183(define* (system-disk-image image
184 #:key
185 (name "disk-image")
186 bootcfg
187 bootloader
188 register-closures?
189 (inputs '()))
190 "Return as a file-like object, the disk-image described by IMAGE. Said
191image can be copied on a USB stick as is. BOOTLOADER is the bootloader that
192will be installed and configured according to BOOTCFG parameter.
193
194Raw images of the IMAGE partitions are first created. Then, genimage is used
195to assemble the partition images into a disk-image without resorting to a
196virtual machine.
197
198INPUTS is a list of inputs (as for packages). When REGISTER-CLOSURES? is
199true, register INPUTS in the store database of the image so that Guix can be
200used in the image."
201
202 (define genimage-name "image")
203
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.
210 (case format
211 ((disk-image) "hdimage")
212 (else
213 (raise (condition
214 (&message
215 (message
216 (format #f (G_ "Unsupported image type ~a~%.") format))))))))
217
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)))
222 (cond
223 ((member 'esp flags) "0xEF")
224 (else "0x83"))))
225
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")))
235 (graph (match inputs
236 (((names . _) ...)
237 names)))
7f75a7ec
MO
238 (type (partition-file-system partition))
239 (image-builder
f19cf27c 240 (with-imported-modules*
7f75a7ec 241 (let ((initializer #$(partition-initializer partition))
fd45ecb5 242 (inputs '#+(list e2fsprogs fakeroot dosfstools mtools))
7f75a7ec 243 (image-root "tmp-root"))
f19cf27c
MO
244 (sql-schema #$schema)
245
7f75a7ec
MO
246 (set-path-environment-variable "PATH" '("bin" "sbin") inputs)
247
f19cf27c
MO
248 ;; Allow non-ASCII file names--e.g., 'nss-certs'--to be
249 ;; decoded.
250 (setenv "GUIX_LOCPATH"
251 #+(file-append glibc-utf8-locales "/lib/locale"))
252 (setlocale LC_ALL "en_US.utf8")
253
7f75a7ec 254 (initializer image-root
f19cf27c
MO
255 #:references-graphs '#$graph
256 #:deduplicate? #f
257 #:system-directory #$os
05f37c16 258 #:grub-efi #+grub-efi
f19cf27c 259 #:bootloader-package
9c1adb24
MO
260 #+(bootloader-package bootloader)
261 #:bootloader-installer
262 #+(bootloader-installer bootloader)
f19cf27c
MO
263 #:bootcfg #$bootcfg
264 #:bootcfg-location
7f75a7ec 265 #$(bootloader-configuration-file bootloader))
f19cf27c
MO
266 (make-partition-image #$(partition->gexp partition)
267 #$output
7f75a7ec
MO
268 image-root)))))
269 (computed-file "partition.img" image-builder
6a958174
MC
270 #:options `(#:local-build? #t ;typically large file
271 #:references-graphs ,inputs))))
f19cf27c
MO
272
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))
1b4fa785
MO
277 (image (partition-image partition))
278 (offset (partition-offset partition)))
f19cf27c 279 #~(format #f "~/partition ~a {
7d4ecda6
MO
280~/~/partition-type = ~a
281~/~/image = \"~a\"
282~/~/offset = \"~a\"
283~/}"
1b4fa785
MO
284 #$label
285 #$dos-type
286 #$image
1dd7b87f 287 #$offset)))
f19cf27c
MO
288
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))
293 (builder
294 #~(begin
295 (let ((format (@ (ice-9 format) format)))
296 (call-with-output-file #$output
297 (lambda (port)
298 (format port
299 "\
300image ~a {
301~/~a {}
302~{~a~^~%~}
303}~%" #$genimage-name #$image-type (list #$@partitions-config))))))))
304 (computed-file "genimage.cfg" builder)))
305
f27bec10
MO
306 (let* ((image-name (image-name image))
307 (name (if image-name
308 (symbol->string image-name)
309 name))
5980ec8a 310 (substitutable? (image-substitutable? image))
f19cf27c
MO
311 (builder
312 (with-imported-modules*
7feefb3b
MO
313 (let ((inputs '#+(list genimage coreutils findutils))
314 (bootloader-installer
315 #+(bootloader-disk-image-installer bootloader)))
f19cf27c 316 (set-path-environment-variable "PATH" '("bin" "sbin") inputs)
7feefb3b
MO
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))))))
f19cf27c
MO
324 (image-dir (computed-file "image-dir" builder)))
325 (computed-file name
326 #~(symlink
327 (string-append #$image-dir "/" #$genimage-name)
328 #$output)
6a958174
MC
329 #:options `(#:local-build? #t ;typically large file
330 #:substitutable? ,substitutable?))))
f19cf27c
MO
331
332\f
333;;
334;; ISO9660 image.
335;;
336
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)))))
342
343(define* (system-iso9660-image image
344 #:key
345 (name "iso9660-image")
346 bootcfg
347 bootloader
348 register-closures?
349 (inputs '())
350 (grub-mkrescue-environment '()))
351 "Return as a file-like object a bootable, stand-alone iso9660 image.
352
353INPUTS is a list of inputs (as for packages). When REGISTER-CLOSURES? is
354true, register INPUTS in the store database of the image so that Guix can be
355used in the image. "
356 (define root-label
357 (match (image-partitions image)
358 ((partition)
359 (partition-label partition))))
360
361 (define root-uuid
362 (match (image-partitions image)
363 ((partition)
364 (uuid-bytevector (partition-uuid partition)))))
365
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")))
372 (graph (match inputs
373 (((names . _) ...)
374 names)))
f19cf27c
MO
375 (builder
376 (with-imported-modules*
377 (let* ((inputs '#$(list parted e2fsprogs dosfstools xorriso
1cb9effc
MO
378 sed grep coreutils findutils gawk))
379 (image-root "tmp-root"))
380 (sql-schema #$schema)
381
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"))
385
386 (setlocale LC_ALL "en_US.utf8")
387
f19cf27c 388 (set-path-environment-variable "PATH" '("bin" "sbin") inputs)
1cb9effc
MO
389
390 (initialize-root-partition image-root
391 #:references-graphs '#$graph
392 #:deduplicate? #f
393 #:system-directory #$os)
f19cf27c
MO
394 (make-iso9660-image #$xorriso
395 '#$grub-mkrescue-environment
396 #$bootloader
397 #$bootcfg
398 #$os
1cb9effc 399 image-root
f19cf27c
MO
400 #$output
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
6a958174
MC
407 #:options `(#:local-build? #t ;typically large file
408 #:references-graphs ,inputs
f19cf27c
MO
409 #:substitutable? ,substitutable?))))
410
036f23f0
JL
411(define (image-with-label base-image label)
412 "The volume ID of an ISO is the label of the first partition. This procedure
413returns an image record where the first partition's label is set to <label>."
414 (image
415 (inherit base-image)
416 (partitions
417 (match (image-partitions base-image)
418 ((boot others ...)
419 (cons
420 (partition
421 (inherit boot)
422 (label label))
423 others))))))
424
f19cf27c
MO
425\f
426;;
427;; Image creation.
428;;
429
f19cf27c
MO
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)
434 "iso9660"
435 (partition-file-system (find-root-partition image)))))
436
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)))
442 (cond
443 ((and (eq? root-size 'guess) image-size)
444 image-size)
445 (else root-size))))
446
447(define* (image-with-os base-image os)
448 "Return an image based on BASE-IMAGE but with the operating-system field set
449to OS. Also set the UUID and the size of the root partition."
450 (define root-file-system
451 (srfi-1:find
452 (lambda (fs)
453 (string=? (file-system-mount-point fs) "/"))
454 (operating-system-file-systems os)))
455
74938105
MO
456 (image
457 (inherit base-image)
458 (operating-system os)
459 (partitions
460 (map (lambda (p)
461 (if (root-partition? p)
462 (partition
463 (inherit p)
464 (uuid (file-system-device root-file-system))
465 (size (root-size base-image)))
466 p))
467 (image-partitions base-image)))))
f19cf27c
MO
468
469(define (operating-system-for-image image)
470 "Return an operating-system based on the one specified in IMAGE, but
471suitable for image creation. Assign an UUID to the root file-system, so that
472it can be used for bootloading."
473 (define volatile-root? (image-volatile-root? image))
474
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)
480 'iso9660
481 'dce)))
482 (operating-system-uuid os type)))
483
484 (let* ((root-file-system-type (image->root-file-system image))
485 (base-os (image-operating-system image))
486 (file-systems-to-keep
487 (srfi-1:remove
488 (lambda (fs)
489 (string=? (file-system-mount-point fs) "/"))
490 (operating-system-file-systems base-os)))
491 (format (image-format image))
492 (os
493 (operating-system
494 (inherit base-os)
495 (initrd (lambda (file-systems . rest)
496 (apply (operating-system-initrd base-os)
497 file-systems
498 #:volatile-root? volatile-root?
499 rest)))
500 (bootloader (if (eq? format 'iso9660)
501 (bootloader-configuration
502 (inherit
503 (operating-system-bootloader base-os))
504 (bootloader grub-mkrescue-bootloader))
505 (operating-system-bootloader base-os)))
506 (file-systems (cons (file-system
507 (mount-point "/")
508 (device "/dev/placeholder")
509 (type root-file-system-type))
510 file-systems-to-keep))))
511 (uuid (root-uuid os)))
512 (operating-system
513 (inherit os)
514 (file-systems (cons (file-system
515 (mount-point "/")
516 (device uuid)
517 (type root-file-system-type))
518 file-systems-to-keep)))))
519
e3f0155c 520(define* (system-image image)
f19cf27c
MO
521 "Return the derivation of IMAGE. It can be a raw disk-image or an ISO9660
522image, depending on IMAGE format."
523 (define substitutable? (image-substitutable? image))
c9f6e2e5
MO
524 (define target (image-target image))
525
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))))
f292d471
MO
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)
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))
b904b59c
MO
569 (module-ref (resolve-interface '(gnu system images hurd))
570 'hurd-disk-image))
7ca533c7
JN
571 (else
572 efi-disk-image)))))
f19cf27c
MO
573
574;;; image.scm ends here