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.
68 ;; This is the offset before the first partition. GRUB will install itself in
70 (define root-offset (* 512 2048))
72 ;; Generic root partition label.
73 (define root-label "Guix_image")
77 (size (* 40 (expt 2 20)))
79 (label "GNU-ESP") ;cosmetic only
80 ;; Use "vfat" here since this property is used when mounting. The actual
81 ;; FAT-ness is based on file system size (16 in this case).
84 (initializer (gexp initialize-efi-partition))))
86 (define root-partition
92 (initializer (gexp initialize-root-partition))))
94 (define efi-disk-image
97 (partitions (list esp-partition root-partition))))
107 ;; XXX: Temporarily disable compression to speed-up the tests.
116 ;; Select (guix …) and (gnu …) modules, except (guix config).
119 (('guix rest ...) #t)
123 (define (partition->gexp partition)
124 "Turn PARTITION, a <partition> object, into a list-valued gexp suitable for
125 'make-partition-image'."
126 #~'(#$@(list (partition-size partition))
127 #$(partition-file-system partition)
128 #$(partition-file-system-options partition)
129 #$(partition-label partition)
130 #$(and=> (partition-uuid partition)
133 (define gcrypt-sqlite3&co
134 ;; Guile-Gcrypt, Guile-SQLite3, and their propagated inputs.
138 (match (package-transitive-propagated-inputs package)
139 (((labels packages) ...)
141 (list guile-gcrypt guile-sqlite3)))
143 (define-syntax-rule (with-imported-modules* gexp* ...)
144 (with-extensions gcrypt-sqlite3&co
145 (with-imported-modules `(,@(source-module-closure
148 (guix store database))
149 #:select? not-config?)
150 ((guix config) => ,(make-config.scm)))
152 (use-modules (gnu build vm)
154 (guix store database)
158 (define (root-partition? partition)
159 "Return true if PARTITION is the root partition, false otherwise."
160 (member 'boot (partition-flags partition)))
162 (define (find-root-partition image)
163 "Return the root partition of the given IMAGE."
164 (srfi-1:find root-partition? (image-partitions image)))
166 (define (root-partition-index image)
167 "Return the index of the root partition of the given IMAGE."
168 (1+ (srfi-1:list-index root-partition? (image-partitions image))))
175 (define* (system-disk-image image
182 "Return as a file-like object, the disk-image described by IMAGE. Said
183 image can be copied on a USB stick as is. BOOTLOADER is the bootloader that
184 will be installed and configured according to BOOTCFG parameter.
186 Raw images of the IMAGE partitions are first created. Then, genimage is used
187 to assemble the partition images into a disk-image without resorting to a
190 INPUTS is a list of inputs (as for packages). When REGISTER-CLOSURES? is
191 true, register INPUTS in the store database of the image so that Guix can be
194 (define genimage-name "image")
196 (define (image->genimage-cfg image)
197 ;; Return as a file-like object, the genimage configuration file
198 ;; describing the given IMAGE.
199 (define (format->image-type format)
200 ;; Return the genimage format corresponding to FORMAT. For now, only
201 ;; the hdimage format (raw disk-image) is supported.
203 ((disk-image) "hdimage")
208 (format #f (G_ "Unsupported image type ~a~%.") format))))))))
210 (define (partition->dos-type partition)
211 ;; Return the MBR partition type corresponding to the given PARTITION.
212 ;; See: https://en.wikipedia.org/wiki/Partition_type.
213 (let ((flags (partition-flags partition)))
215 ((member 'esp flags) "0xEF")
218 (define (partition-image partition)
219 ;; Return as a file-like object, an image of the given PARTITION. A
220 ;; directory, filled by calling the PARTITION initializer procedure, is
221 ;; first created within the store. Then, an image of this directory is
222 ;; created using tools such as 'mke2fs' or 'mkdosfs', depending on the
223 ;; partition file-system type.
224 (let* ((os (image-operating-system image))
225 (schema (local-file (search-path %load-path
226 "guix/store/schema.sql")))
231 (with-imported-modules*
232 (let* ((initializer #$(partition-initializer partition)))
233 (sql-schema #$schema)
235 ;; Allow non-ASCII file names--e.g., 'nss-certs'--to be
237 (setenv "GUIX_LOCPATH"
238 #+(file-append glibc-utf8-locales "/lib/locale"))
239 (setlocale LC_ALL "en_US.utf8")
241 (initializer #$output
242 #:references-graphs '#$graph
244 #:system-directory #$os
245 #:grub-efi #+grub-efi
247 #+(bootloader-package bootloader)
248 #:bootloader-installer
249 #+(bootloader-installer bootloader)
252 #$(bootloader-configuration-file bootloader)))))
254 (computed-file "partition-image-root" root-builder
255 #:options `(#:references-graphs ,inputs)))
256 (type (partition-file-system partition))
258 (with-imported-modules*
259 (let ((inputs '#+(list e2fsprogs dosfstools mtools)))
260 (set-path-environment-variable "PATH" '("bin" "sbin") inputs)
261 (make-partition-image #$(partition->gexp partition)
264 (computed-file "partition.img" image-builder)))
266 (define (partition->config partition)
267 ;; Return the genimage partition configuration for PARTITION.
268 (let ((label (partition-label partition))
269 (dos-type (partition->dos-type partition))
270 (image (partition-image partition))
271 (offset (partition-offset partition)))
272 #~(format #f "~/partition ~a {
273 ~/~/partition-type = ~a
282 (let* ((format (image-format image))
283 (image-type (format->image-type format))
284 (partitions (image-partitions image))
285 (partitions-config (map partition->config partitions))
288 (let ((format (@ (ice-9 format) format)))
289 (call-with-output-file #$output
296 }~%" #$genimage-name #$image-type (list #$@partitions-config))))))))
297 (computed-file "genimage.cfg" builder)))
299 (let* ((substitutable? (image-substitutable? image))
301 (with-imported-modules*
302 (let ((inputs '#+(list genimage coreutils findutils))
303 (bootloader-installer
304 #+(bootloader-disk-image-installer bootloader)))
305 (set-path-environment-variable "PATH" '("bin" "sbin") inputs)
306 (genimage #$(image->genimage-cfg image) #$output)
307 ;; Install the bootloader directly on the disk-image.
308 (when bootloader-installer
309 (bootloader-installer
310 #+(bootloader-package bootloader)
311 #$(root-partition-index image)
312 (string-append #$output "/" #$genimage-name))))))
313 (image-dir (computed-file "image-dir" builder)))
316 (string-append #$image-dir "/" #$genimage-name)
318 #:options `(#:substitutable? ,substitutable?))))
325 (define (has-guix-service-type? os)
326 "Return true if OS contains a service of the type GUIX-SERVICE-TYPE."
327 (not (not (srfi-1:find (lambda (service)
328 (eq? (service-kind service) guix-service-type))
329 (operating-system-services os)))))
331 (define* (system-iso9660-image image
333 (name "iso9660-image")
338 (grub-mkrescue-environment '()))
339 "Return as a file-like object a bootable, stand-alone iso9660 image.
341 INPUTS is a list of inputs (as for packages). When REGISTER-CLOSURES? is
342 true, register INPUTS in the store database of the image so that Guix can be
345 (match (image-partitions image)
347 (partition-label partition))))
350 (match (image-partitions image)
352 (uuid-bytevector (partition-uuid partition)))))
354 (let* ((os (image-operating-system image))
355 (bootloader (bootloader-package bootloader))
356 (compression? (image-compression? image))
357 (substitutable? (image-substitutable? image))
358 (schema (local-file (search-path %load-path
359 "guix/store/schema.sql")))
364 (with-imported-modules*
365 (sql-schema #$schema)
367 ;; Allow non-ASCII file names--e.g., 'nss-certs'--to be decoded.
368 (setenv "GUIX_LOCPATH"
369 #+(file-append glibc-utf8-locales "/lib/locale"))
370 (setlocale LC_ALL "en_US.utf8")
372 (initialize-root-partition #$output
373 #:references-graphs '#$graph
375 #:system-directory #$os)))
377 (computed-file "image-root" root-builder
378 #:options `(#:references-graphs ,inputs)))
380 (with-imported-modules*
381 (let* ((inputs '#$(list parted e2fsprogs dosfstools xorriso
382 sed grep coreutils findutils gawk)))
383 (set-path-environment-variable "PATH" '("bin" "sbin") inputs)
384 (make-iso9660-image #$xorriso
385 '#$grub-mkrescue-environment
391 #:references-graphs '#$graph
392 #:register-closures? #$register-closures?
393 #:compression? #$compression?
394 #:volume-id #$root-label
395 #:volume-uuid #$root-uuid)))))
396 (computed-file name builder
397 #:options `(#:references-graphs ,inputs
398 #:substitutable? ,substitutable?))))
405 (define (image->root-file-system image)
406 "Return the IMAGE root partition file-system type."
407 (let ((format (image-format image)))
408 (if (eq? format 'iso9660)
410 (partition-file-system (find-root-partition image)))))
412 (define (root-size image)
413 "Return the root partition size of IMAGE."
414 (let* ((image-size (image-size image))
415 (root-partition (find-root-partition image))
416 (root-size (partition-size root-partition)))
418 ((and (eq? root-size 'guess) image-size)
422 (define* (image-with-os base-image os)
423 "Return an image based on BASE-IMAGE but with the operating-system field set
424 to OS. Also set the UUID and the size of the root partition."
425 (define root-file-system
428 (string=? (file-system-mount-point fs) "/"))
429 (operating-system-file-systems os)))
433 (operating-system os)
436 (if (root-partition? p)
439 (uuid (file-system-device root-file-system))
440 (size (root-size base-image)))
442 (image-partitions base-image)))))
444 (define (operating-system-for-image image)
445 "Return an operating-system based on the one specified in IMAGE, but
446 suitable for image creation. Assign an UUID to the root file-system, so that
447 it can be used for bootloading."
448 (define volatile-root? (image-volatile-root? image))
450 (define (root-uuid os)
451 ;; UUID of the root file system, computed in a deterministic fashion.
452 ;; This is what we use to locate the root file system so it has to be
453 ;; different from the user's own file system UUIDs.
454 (let ((type (if (eq? (image-format image) 'iso9660)
457 (operating-system-uuid os type)))
459 (let* ((root-file-system-type (image->root-file-system image))
460 (base-os (image-operating-system image))
461 (file-systems-to-keep
464 (string=? (file-system-mount-point fs) "/"))
465 (operating-system-file-systems base-os)))
466 (format (image-format image))
470 (initrd (lambda (file-systems . rest)
471 (apply (operating-system-initrd base-os)
473 #:volatile-root? volatile-root?
475 (bootloader (if (eq? format 'iso9660)
476 (bootloader-configuration
478 (operating-system-bootloader base-os))
479 (bootloader grub-mkrescue-bootloader))
480 (operating-system-bootloader base-os)))
481 (file-systems (cons (file-system
483 (device "/dev/placeholder")
484 (type root-file-system-type))
485 file-systems-to-keep))))
486 (uuid (root-uuid os)))
489 (file-systems (cons (file-system
492 (type root-file-system-type))
493 file-systems-to-keep)))))
495 (define* (system-image image)
496 "Return the derivation of IMAGE. It can be a raw disk-image or an ISO9660
497 image, depending on IMAGE format."
498 (define substitutable? (image-substitutable? image))
500 (let* ((os (operating-system-for-image image))
501 (image* (image-with-os image os))
502 (register-closures? (has-guix-service-type? os))
503 (bootcfg (operating-system-bootcfg os))
504 (bootloader (bootloader-configuration-bootloader
505 (operating-system-bootloader os))))
506 (case (image-format image)
508 (system-disk-image image*
510 #:bootloader bootloader
511 #:register-closures? register-closures?
512 #:inputs `(("system" ,os)
513 ("bootcfg" ,bootcfg))))
515 (system-iso9660-image image*
517 #:bootloader bootloader
518 #:register-closures? register-closures?
519 #:inputs `(("system" ,os)
520 ("bootcfg" ,bootcfg))
521 #:grub-mkrescue-environment
522 '(("MKRESCUE_SED_MODE" . "mbr_hfs")))))))
524 (define (find-image file-system-type)
525 "Find and return an image that could match the given FILE-SYSTEM-TYPE. This
526 is useful to adapt to interfaces written before the addition of the <image>
530 (match file-system-type
531 ("iso9660" iso9660-image)
532 (_ efi-disk-image)))))
534 ;;; image.scm ends here