1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2020 Mathieu Othacehe <m.othacehe@gmail.com>
4 ;;; This file is part of GNU Guix.
6 ;;; GNU Guix is free software; you can redistribute it and/or modify it
7 ;;; under the terms of the GNU General Public License as published by
8 ;;; the Free Software Foundation; either version 3 of the License, or (at
9 ;;; your option) any later version.
11 ;;; GNU Guix is distributed in the hope that it will be useful, but
12 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ;;; GNU General Public License for more details.
16 ;;; You should have received a copy of the GNU General Public License
17 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
19 (define-module (gnu system image)
20 #:use-module (guix gexp)
21 #:use-module (guix modules)
22 #:use-module (guix monads)
23 #:use-module (guix records)
24 #:use-module (guix store)
25 #:use-module (guix ui)
26 #:use-module (guix utils)
27 #:use-module ((guix self) #:select (make-config.scm))
28 #:use-module (gnu bootloader)
29 #:use-module (gnu bootloader grub)
30 #:use-module (gnu image)
31 #:use-module (gnu services)
32 #:use-module (gnu services base)
33 #:use-module (gnu system)
34 #:use-module (gnu system file-systems)
35 #:use-module (gnu system uuid)
36 #:use-module (gnu system vm)
37 #:use-module (guix packages)
38 #:use-module (gnu packages base)
39 #:use-module (gnu packages bootloaders)
40 #:use-module (gnu packages cdrom)
41 #:use-module (gnu packages disk)
42 #:use-module (gnu packages gawk)
43 #:use-module (gnu packages genimage)
44 #:use-module (gnu packages guile)
45 #:autoload (gnu packages gnupg) (guile-gcrypt)
46 #:use-module (gnu packages linux)
47 #:use-module (gnu packages mtools)
48 #:use-module ((srfi srfi-1) #:prefix srfi-1:)
49 #:use-module (srfi srfi-11)
50 #:use-module (srfi srfi-26)
51 #:use-module (srfi srfi-35)
52 #:use-module (rnrs bytevectors)
53 #:use-module (ice-9 match)
54 #:export (esp-partition
65 ;;; Images definitions.
70 (size (* 40 (expt 2 20)))
71 (label "GNU-ESP") ;cosmetic only
72 ;; Use "vfat" here since this property is used when mounting. The actual
73 ;; FAT-ness is based on file system size (16 in this case).
76 (initializer (gexp initialize-efi-partition))))
78 (define root-partition
84 (initializer (gexp initialize-root-partition))))
86 (define efi-disk-image
89 (partitions (list esp-partition root-partition))))
99 ;; XXX: Temporarily disable compression to speed-up the tests.
108 ;; Select (guix …) and (gnu …) modules, except (guix config).
111 (('guix rest ...) #t)
115 (define (partition->gexp partition)
116 "Turn PARTITION, a <partition> object, into a list-valued gexp suitable for
117 'make-partition-image'."
118 #~'(#$@(list (partition-size partition))
119 #$(partition-file-system partition)
120 #$(partition-label partition)
121 #$(and=> (partition-uuid partition)
124 (define gcrypt-sqlite3&co
125 ;; Guile-Gcrypt, Guile-SQLite3, and their propagated inputs.
129 (match (package-transitive-propagated-inputs package)
130 (((labels packages) ...)
132 (list guile-gcrypt guile-sqlite3)))
134 (define-syntax-rule (with-imported-modules* gexp* ...)
135 (with-extensions gcrypt-sqlite3&co
136 (with-imported-modules `(,@(source-module-closure
139 (guix store database))
140 #:select? not-config?)
141 ((guix config) => ,(make-config.scm)))
143 (use-modules (gnu build vm)
145 (guix store database)
154 (define* (system-disk-image image
161 "Return as a file-like object, the disk-image described by IMAGE. Said
162 image can be copied on a USB stick as is. BOOTLOADER is the bootloader that
163 will be installed and configured according to BOOTCFG parameter.
165 Raw images of the IMAGE partitions are first created. Then, genimage is used
166 to assemble the partition images into a disk-image without resorting to a
169 INPUTS is a list of inputs (as for packages). When REGISTER-CLOSURES? is
170 true, register INPUTS in the store database of the image so that Guix can be
173 (define genimage-name "image")
175 (define (image->genimage-cfg image)
176 ;; Return as a file-like object, the genimage configuration file
177 ;; describing the given IMAGE.
178 (define (format->image-type format)
179 ;; Return the genimage format corresponding to FORMAT. For now, only
180 ;; the hdimage format (raw disk-image) is supported.
182 ((disk-image) "hdimage")
187 (format #f (G_ "Unsupported image type ~a~%.") format))))))))
189 (define (partition->dos-type partition)
190 ;; Return the MBR partition type corresponding to the given PARTITION.
191 ;; See: https://en.wikipedia.org/wiki/Partition_type.
192 (let ((flags (partition-flags partition)))
194 ((member 'esp flags) "0xEF")
197 (define (partition-image partition)
198 ;; Return as a file-like object, an image of the given PARTITION. A
199 ;; directory, filled by calling the PARTITION initializer procedure, is
200 ;; first created within the store. Then, an image of this directory is
201 ;; created using tools such as 'mke2fs' or 'mkdosfs', depending on the
202 ;; partition file-system type.
203 (let* ((os (image-operating-system image))
204 (schema (local-file (search-path %load-path
205 "guix/store/schema.sql")))
210 (with-imported-modules*
211 (let* ((initializer #$(partition-initializer partition)))
212 (sql-schema #$schema)
214 ;; Allow non-ASCII file names--e.g., 'nss-certs'--to be
216 (setenv "GUIX_LOCPATH"
217 #+(file-append glibc-utf8-locales "/lib/locale"))
218 (setlocale LC_ALL "en_US.utf8")
220 (initializer #$output
221 #:references-graphs '#$graph
223 #:system-directory #$os
225 #$(bootloader-package bootloader)
228 #$(bootloader-configuration-file bootloader)))))
230 (computed-file "partition-image-root" root-builder
231 #:options `(#:references-graphs ,inputs)))
232 (type (partition-file-system partition))
234 (with-imported-modules*
235 (let ((inputs '#$(list e2fsprogs dosfstools mtools)))
236 (set-path-environment-variable "PATH" '("bin" "sbin") inputs)
237 (make-partition-image #$(partition->gexp partition)
240 (computed-file "partition.img" image-builder)))
242 (define (partition->config partition)
243 ;; Return the genimage partition configuration for PARTITION.
244 (let ((label (partition-label partition))
245 (dos-type (partition->dos-type partition))
246 (image (partition-image partition)))
247 #~(format #f "~/partition ~a {
248 ~/~/partition-type = ~a
250 ~/}" #$label #$dos-type #$image)))
252 (let* ((format (image-format image))
253 (image-type (format->image-type format))
254 (partitions (image-partitions image))
255 (partitions-config (map partition->config partitions))
258 (let ((format (@ (ice-9 format) format)))
259 (call-with-output-file #$output
266 }~%" #$genimage-name #$image-type (list #$@partitions-config))))))))
267 (computed-file "genimage.cfg" builder)))
269 (let* ((substitutable? (image-substitutable? image))
271 (with-imported-modules*
272 (let ((inputs '#$(list genimage coreutils findutils)))
273 (set-path-environment-variable "PATH" '("bin" "sbin") inputs)
274 (genimage #$(image->genimage-cfg image) #$output))))
275 (image-dir (computed-file "image-dir" builder)))
278 (string-append #$image-dir "/" #$genimage-name)
280 #:options `(#:substitutable? ,substitutable?))))
287 (define (has-guix-service-type? os)
288 "Return true if OS contains a service of the type GUIX-SERVICE-TYPE."
289 (not (not (srfi-1:find (lambda (service)
290 (eq? (service-kind service) guix-service-type))
291 (operating-system-services os)))))
293 (define* (system-iso9660-image image
295 (name "iso9660-image")
300 (grub-mkrescue-environment '()))
301 "Return as a file-like object a bootable, stand-alone iso9660 image.
303 INPUTS is a list of inputs (as for packages). When REGISTER-CLOSURES? is
304 true, register INPUTS in the store database of the image so that Guix can be
307 (match (image-partitions image)
309 (partition-label partition))))
312 (match (image-partitions image)
314 (uuid-bytevector (partition-uuid partition)))))
316 (let* ((os (image-operating-system image))
317 (bootloader (bootloader-package bootloader))
318 (compression? (image-compression? image))
319 (substitutable? (image-substitutable? image))
320 (schema (local-file (search-path %load-path
321 "guix/store/schema.sql")))
326 (with-imported-modules*
327 (sql-schema #$schema)
329 ;; Allow non-ASCII file names--e.g., 'nss-certs'--to be decoded.
330 (setenv "GUIX_LOCPATH"
331 #+(file-append glibc-utf8-locales "/lib/locale"))
332 (setlocale LC_ALL "en_US.utf8")
334 (initialize-root-partition #$output
335 #:references-graphs '#$graph
337 #:system-directory #$os)))
339 (computed-file "image-root" root-builder
340 #:options `(#:references-graphs ,inputs)))
342 (with-imported-modules*
343 (let* ((inputs '#$(list parted e2fsprogs dosfstools xorriso
344 sed grep coreutils findutils gawk)))
345 (set-path-environment-variable "PATH" '("bin" "sbin") inputs)
346 (make-iso9660-image #$xorriso
347 '#$grub-mkrescue-environment
353 #:references-graphs '#$graph
354 #:register-closures? #$register-closures?
355 #:compression? #$compression?
356 #:volume-id #$root-label
357 #:volume-uuid #$root-uuid)))))
358 (computed-file name builder
359 #:options `(#:references-graphs ,inputs
360 #:substitutable? ,substitutable?))))
367 (define (root-partition? partition)
368 "Return true if PARTITION is the root partition, false otherwise."
369 (member 'boot (partition-flags partition)))
371 (define (find-root-partition image)
372 "Return the root partition of the given IMAGE."
373 (srfi-1:find root-partition? (image-partitions image)))
375 (define (image->root-file-system image)
376 "Return the IMAGE root partition file-system type."
377 (let ((format (image-format image)))
378 (if (eq? format 'iso9660)
380 (partition-file-system (find-root-partition image)))))
382 (define (root-size image)
383 "Return the root partition size of IMAGE."
384 (let* ((image-size (image-size image))
385 (root-partition (find-root-partition image))
386 (root-size (partition-size root-partition)))
388 ((and (eq? root-size 'guess) image-size)
392 (define* (image-with-os base-image os)
393 "Return an image based on BASE-IMAGE but with the operating-system field set
394 to OS. Also set the UUID and the size of the root partition."
395 (define root-file-system
398 (string=? (file-system-mount-point fs) "/"))
399 (operating-system-file-systems os)))
401 (let*-values (((partitions) (image-partitions base-image))
402 ((root-partition other-partitions)
403 (srfi-1:partition root-partition? partitions)))
406 (operating-system os)
409 (inherit (car root-partition))
410 (uuid (file-system-device root-file-system))
411 (size (root-size base-image)))
412 other-partitions)))))
414 (define (operating-system-for-image image)
415 "Return an operating-system based on the one specified in IMAGE, but
416 suitable for image creation. Assign an UUID to the root file-system, so that
417 it can be used for bootloading."
418 (define volatile-root? (image-volatile-root? image))
420 (define (root-uuid os)
421 ;; UUID of the root file system, computed in a deterministic fashion.
422 ;; This is what we use to locate the root file system so it has to be
423 ;; different from the user's own file system UUIDs.
424 (let ((type (if (eq? (image-format image) 'iso9660)
427 (operating-system-uuid os type)))
429 (let* ((root-file-system-type (image->root-file-system image))
430 (base-os (image-operating-system image))
431 (file-systems-to-keep
434 (string=? (file-system-mount-point fs) "/"))
435 (operating-system-file-systems base-os)))
436 (format (image-format image))
440 (initrd (lambda (file-systems . rest)
441 (apply (operating-system-initrd base-os)
443 #:volatile-root? volatile-root?
445 (bootloader (if (eq? format 'iso9660)
446 (bootloader-configuration
448 (operating-system-bootloader base-os))
449 (bootloader grub-mkrescue-bootloader))
450 (operating-system-bootloader base-os)))
451 (file-systems (cons (file-system
453 (device "/dev/placeholder")
454 (type root-file-system-type))
455 file-systems-to-keep))))
456 (uuid (root-uuid os)))
459 (file-systems (cons (file-system
462 (type root-file-system-type))
463 file-systems-to-keep)))))
465 (define* (make-system-image image)
466 "Return the derivation of IMAGE. It can be a raw disk-image or an ISO9660
467 image, depending on IMAGE format."
468 (define substitutable? (image-substitutable? image))
470 (let* ((os (operating-system-for-image image))
471 (image* (image-with-os image os))
472 (register-closures? (has-guix-service-type? os))
473 (bootcfg (operating-system-bootcfg os))
474 (bootloader (bootloader-configuration-bootloader
475 (operating-system-bootloader os))))
476 (case (image-format image)
478 (system-disk-image image*
480 #:bootloader bootloader
481 #:register-closures? register-closures?
482 #:inputs `(("system" ,os)
483 ("bootcfg" ,bootcfg))))
485 (system-iso9660-image image*
487 #:bootloader bootloader
488 #:register-closures? register-closures?
489 #:inputs `(("system" ,os)
490 ("bootcfg" ,bootcfg))
491 #:grub-mkrescue-environment
492 '(("MKRESCUE_SED_MODE" . "mbr_hfs")))))))
494 (define (find-image file-system-type)
495 "Find and return an image that could match the given FILE-SYSTEM-TYPE. This
496 is useful to adapt to interfaces written before the addition of the <image>
498 ;; XXX: Add support for system and target here, or in the caller.
499 (match file-system-type
500 ("iso9660" iso9660-image)
503 (define (system-image image)
504 "Wrap 'make-system-image' call, so that it is used only if the given IMAGE
505 is supported. Otherwise, fallback to image creation in a VM. This is
506 temporary and should be removed once 'make-system-image' is able to deal with
507 all types of images."
508 (define substitutable? (image-substitutable? image))
509 (define volatile-root? (image-volatile-root? image))
511 (let* ((image-os (image-operating-system image))
512 (image-root-filesystem-type (image->root-file-system image))
513 (bootloader (bootloader-configuration-bootloader
514 (operating-system-bootloader image-os)))
515 (bootloader-name (bootloader-name bootloader))
516 (size (image-size image))
517 (format (image-format image)))
519 (if (and (or (eq? bootloader-name 'grub)
520 (eq? bootloader-name 'extlinux))
521 (eq? format 'disk-image))
522 ;; Fallback to image creation in a VM when it is not yet supported
524 (system-disk-image-in-vm image-os
525 #:disk-image-size size
526 #:file-system-type image-root-filesystem-type
527 #:volatile? volatile-root?
528 #:substitutable? substitutable?)
530 (make-system-image image))))))
532 ;;; image.scm ends here