Commit | Line | Data |
---|---|---|
f19cf27c MO |
1 | ;;; GNU Guix --- Functional package management for GNU |
2 | ;;; Copyright © 2020 Mathieu Othacehe <m.othacehe@gmail.com> | |
3 | ;;; | |
4 | ;;; This file is part of GNU Guix. | |
5 | ;;; | |
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. | |
10 | ;;; | |
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. | |
15 | ;;; | |
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/>. | |
18 | ||
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 | |
55 | root-partition | |
56 | ||
57 | efi-disk-image | |
58 | iso9660-image | |
59 | ||
60 | find-image | |
61 | system-image)) | |
62 | ||
63 | \f | |
64 | ;;; | |
65 | ;;; Images definitions. | |
66 | ;;; | |
67 | ||
68 | (define esp-partition | |
69 | (partition | |
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). | |
74 | (file-system "vfat") | |
75 | (flags '(esp)) | |
76 | (initializer (gexp initialize-efi-partition)))) | |
77 | ||
78 | (define root-partition | |
79 | (partition | |
80 | (size 'guess) | |
81 | (label "Guix_image") | |
82 | (file-system "ext4") | |
83 | (flags '(boot)) | |
84 | (initializer (gexp initialize-root-partition)))) | |
85 | ||
86 | (define efi-disk-image | |
87 | (image | |
88 | (format 'disk-image) | |
89 | (partitions (list esp-partition root-partition)))) | |
90 | ||
91 | (define iso9660-image | |
92 | (image | |
93 | (format 'iso9660) | |
94 | (partitions | |
95 | (list (partition | |
96 | (size 'guess) | |
97 | (label "GUIX_IMAGE") | |
98 | (flags '(boot))))) | |
99 | ;; XXX: Temporarily disable compression to speed-up the tests. | |
100 | (compression? #f))) | |
101 | ||
102 | \f | |
103 | ;; | |
104 | ;; Helpers. | |
105 | ;; | |
106 | ||
107 | (define not-config? | |
108 | ;; Select (guix …) and (gnu …) modules, except (guix config). | |
109 | (match-lambda | |
110 | (('guix 'config) #f) | |
111 | (('guix rest ...) #t) | |
112 | (('gnu rest ...) #t) | |
113 | (rest #f))) | |
114 | ||
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) | |
122 | uuid-bytevector))) | |
123 | ||
124 | (define gcrypt-sqlite3&co | |
125 | ;; Guile-Gcrypt, Guile-SQLite3, and their propagated inputs. | |
126 | (srfi-1:append-map | |
127 | (lambda (package) | |
128 | (cons package | |
129 | (match (package-transitive-propagated-inputs package) | |
130 | (((labels packages) ...) | |
131 | packages)))) | |
132 | (list guile-gcrypt guile-sqlite3))) | |
133 | ||
134 | (define-syntax-rule (with-imported-modules* gexp* ...) | |
135 | (with-extensions gcrypt-sqlite3&co | |
136 | (with-imported-modules `(,@(source-module-closure | |
137 | '((gnu build vm) | |
138 | (gnu build image) | |
139 | (guix store database)) | |
140 | #:select? not-config?) | |
141 | ((guix config) => ,(make-config.scm))) | |
142 | #~(begin | |
143 | (use-modules (gnu build vm) | |
144 | (gnu build image) | |
145 | (guix store database) | |
146 | (guix build utils)) | |
147 | gexp* ...)))) | |
148 | ||
149 | \f | |
150 | ;; | |
151 | ;; Disk image. | |
152 | ;; | |
153 | ||
154 | (define* (system-disk-image image | |
155 | #:key | |
156 | (name "disk-image") | |
157 | bootcfg | |
158 | bootloader | |
159 | register-closures? | |
160 | (inputs '())) | |
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. | |
164 | ||
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 | |
167 | virtual machine. | |
168 | ||
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 | |
171 | used in the image." | |
172 | ||
173 | (define genimage-name "image") | |
174 | ||
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. | |
181 | (case format | |
182 | ((disk-image) "hdimage") | |
183 | (else | |
184 | (raise (condition | |
185 | (&message | |
186 | (message | |
187 | (format #f (G_ "Unsupported image type ~a~%.") format)))))))) | |
188 | ||
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))) | |
193 | (cond | |
194 | ((member 'esp flags) "0xEF") | |
195 | (else "0x83")))) | |
196 | ||
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"))) | |
206 | (graph (match inputs | |
207 | (((names . _) ...) | |
208 | names))) | |
209 | (root-builder | |
210 | (with-imported-modules* | |
211 | (let* ((initializer #$(partition-initializer partition))) | |
212 | (sql-schema #$schema) | |
213 | ||
214 | ;; Allow non-ASCII file names--e.g., 'nss-certs'--to be | |
215 | ;; decoded. | |
216 | (setenv "GUIX_LOCPATH" | |
217 | #+(file-append glibc-utf8-locales "/lib/locale")) | |
218 | (setlocale LC_ALL "en_US.utf8") | |
219 | ||
220 | (initializer #$output | |
221 | #:references-graphs '#$graph | |
222 | #:deduplicate? #f | |
223 | #:system-directory #$os | |
224 | #:bootloader-package | |
225 | #$(bootloader-package bootloader) | |
226 | #:bootcfg #$bootcfg | |
227 | #:bootcfg-location | |
228 | #$(bootloader-configuration-file bootloader))))) | |
229 | (image-root | |
230 | (computed-file "partition-image-root" root-builder | |
231 | #:options `(#:references-graphs ,inputs))) | |
232 | (type (partition-file-system partition)) | |
233 | (image-builder | |
234 | (with-imported-modules* | |
1dbd0005 | 235 | (let ((inputs '#+(list e2fsprogs dosfstools mtools))) |
f19cf27c MO |
236 | (set-path-environment-variable "PATH" '("bin" "sbin") inputs) |
237 | (make-partition-image #$(partition->gexp partition) | |
238 | #$output | |
239 | #$image-root))))) | |
240 | (computed-file "partition.img" image-builder))) | |
241 | ||
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)) | |
1b4fa785 MO |
246 | (image (partition-image partition)) |
247 | (offset (partition-offset partition))) | |
f19cf27c MO |
248 | #~(format #f "~/partition ~a { |
249 | ~/~/partition-type = ~a | |
250 | ~/~/image = \"~a\" | |
1b4fa785 MO |
251 | ~/~/offset = \"~a\" |
252 | ~/}" | |
253 | #$label | |
254 | #$dos-type | |
255 | #$image | |
256 | #$(or offset 0)))) | |
f19cf27c MO |
257 | |
258 | (let* ((format (image-format image)) | |
259 | (image-type (format->image-type format)) | |
260 | (partitions (image-partitions image)) | |
261 | (partitions-config (map partition->config partitions)) | |
262 | (builder | |
263 | #~(begin | |
264 | (let ((format (@ (ice-9 format) format))) | |
265 | (call-with-output-file #$output | |
266 | (lambda (port) | |
267 | (format port | |
268 | "\ | |
269 | image ~a { | |
270 | ~/~a {} | |
271 | ~{~a~^~%~} | |
272 | }~%" #$genimage-name #$image-type (list #$@partitions-config)))))))) | |
273 | (computed-file "genimage.cfg" builder))) | |
274 | ||
275 | (let* ((substitutable? (image-substitutable? image)) | |
276 | (builder | |
277 | (with-imported-modules* | |
1dbd0005 | 278 | (let ((inputs '#+(list genimage coreutils findutils))) |
f19cf27c MO |
279 | (set-path-environment-variable "PATH" '("bin" "sbin") inputs) |
280 | (genimage #$(image->genimage-cfg image) #$output)))) | |
281 | (image-dir (computed-file "image-dir" builder))) | |
282 | (computed-file name | |
283 | #~(symlink | |
284 | (string-append #$image-dir "/" #$genimage-name) | |
285 | #$output) | |
286 | #:options `(#:substitutable? ,substitutable?)))) | |
287 | ||
288 | \f | |
289 | ;; | |
290 | ;; ISO9660 image. | |
291 | ;; | |
292 | ||
293 | (define (has-guix-service-type? os) | |
294 | "Return true if OS contains a service of the type GUIX-SERVICE-TYPE." | |
295 | (not (not (srfi-1:find (lambda (service) | |
296 | (eq? (service-kind service) guix-service-type)) | |
297 | (operating-system-services os))))) | |
298 | ||
299 | (define* (system-iso9660-image image | |
300 | #:key | |
301 | (name "iso9660-image") | |
302 | bootcfg | |
303 | bootloader | |
304 | register-closures? | |
305 | (inputs '()) | |
306 | (grub-mkrescue-environment '())) | |
307 | "Return as a file-like object a bootable, stand-alone iso9660 image. | |
308 | ||
309 | INPUTS is a list of inputs (as for packages). When REGISTER-CLOSURES? is | |
310 | true, register INPUTS in the store database of the image so that Guix can be | |
311 | used in the image. " | |
312 | (define root-label | |
313 | (match (image-partitions image) | |
314 | ((partition) | |
315 | (partition-label partition)))) | |
316 | ||
317 | (define root-uuid | |
318 | (match (image-partitions image) | |
319 | ((partition) | |
320 | (uuid-bytevector (partition-uuid partition))))) | |
321 | ||
322 | (let* ((os (image-operating-system image)) | |
323 | (bootloader (bootloader-package bootloader)) | |
324 | (compression? (image-compression? image)) | |
325 | (substitutable? (image-substitutable? image)) | |
326 | (schema (local-file (search-path %load-path | |
327 | "guix/store/schema.sql"))) | |
328 | (graph (match inputs | |
329 | (((names . _) ...) | |
330 | names))) | |
331 | (root-builder | |
332 | (with-imported-modules* | |
333 | (sql-schema #$schema) | |
334 | ||
335 | ;; Allow non-ASCII file names--e.g., 'nss-certs'--to be decoded. | |
336 | (setenv "GUIX_LOCPATH" | |
337 | #+(file-append glibc-utf8-locales "/lib/locale")) | |
338 | (setlocale LC_ALL "en_US.utf8") | |
339 | ||
340 | (initialize-root-partition #$output | |
341 | #:references-graphs '#$graph | |
342 | #:deduplicate? #f | |
343 | #:system-directory #$os))) | |
344 | (image-root | |
345 | (computed-file "image-root" root-builder | |
346 | #:options `(#:references-graphs ,inputs))) | |
347 | (builder | |
348 | (with-imported-modules* | |
349 | (let* ((inputs '#$(list parted e2fsprogs dosfstools xorriso | |
350 | sed grep coreutils findutils gawk))) | |
351 | (set-path-environment-variable "PATH" '("bin" "sbin") inputs) | |
352 | (make-iso9660-image #$xorriso | |
353 | '#$grub-mkrescue-environment | |
354 | #$bootloader | |
355 | #$bootcfg | |
356 | #$os | |
357 | #$image-root | |
358 | #$output | |
359 | #:references-graphs '#$graph | |
360 | #:register-closures? #$register-closures? | |
361 | #:compression? #$compression? | |
362 | #:volume-id #$root-label | |
363 | #:volume-uuid #$root-uuid))))) | |
364 | (computed-file name builder | |
365 | #:options `(#:references-graphs ,inputs | |
366 | #:substitutable? ,substitutable?)))) | |
367 | ||
368 | \f | |
369 | ;; | |
370 | ;; Image creation. | |
371 | ;; | |
372 | ||
373 | (define (root-partition? partition) | |
374 | "Return true if PARTITION is the root partition, false otherwise." | |
375 | (member 'boot (partition-flags partition))) | |
376 | ||
377 | (define (find-root-partition image) | |
378 | "Return the root partition of the given IMAGE." | |
379 | (srfi-1:find root-partition? (image-partitions image))) | |
380 | ||
381 | (define (image->root-file-system image) | |
382 | "Return the IMAGE root partition file-system type." | |
383 | (let ((format (image-format image))) | |
384 | (if (eq? format 'iso9660) | |
385 | "iso9660" | |
386 | (partition-file-system (find-root-partition image))))) | |
387 | ||
388 | (define (root-size image) | |
389 | "Return the root partition size of IMAGE." | |
390 | (let* ((image-size (image-size image)) | |
391 | (root-partition (find-root-partition image)) | |
392 | (root-size (partition-size root-partition))) | |
393 | (cond | |
394 | ((and (eq? root-size 'guess) image-size) | |
395 | image-size) | |
396 | (else root-size)))) | |
397 | ||
398 | (define* (image-with-os base-image os) | |
399 | "Return an image based on BASE-IMAGE but with the operating-system field set | |
400 | to OS. Also set the UUID and the size of the root partition." | |
401 | (define root-file-system | |
402 | (srfi-1:find | |
403 | (lambda (fs) | |
404 | (string=? (file-system-mount-point fs) "/")) | |
405 | (operating-system-file-systems os))) | |
406 | ||
407 | (let*-values (((partitions) (image-partitions base-image)) | |
408 | ((root-partition other-partitions) | |
409 | (srfi-1:partition root-partition? partitions))) | |
410 | (image | |
411 | (inherit base-image) | |
412 | (operating-system os) | |
413 | (partitions | |
414 | (cons (partition | |
415 | (inherit (car root-partition)) | |
416 | (uuid (file-system-device root-file-system)) | |
417 | (size (root-size base-image))) | |
418 | other-partitions))))) | |
419 | ||
420 | (define (operating-system-for-image image) | |
421 | "Return an operating-system based on the one specified in IMAGE, but | |
422 | suitable for image creation. Assign an UUID to the root file-system, so that | |
423 | it can be used for bootloading." | |
424 | (define volatile-root? (image-volatile-root? image)) | |
425 | ||
426 | (define (root-uuid os) | |
427 | ;; UUID of the root file system, computed in a deterministic fashion. | |
428 | ;; This is what we use to locate the root file system so it has to be | |
429 | ;; different from the user's own file system UUIDs. | |
430 | (let ((type (if (eq? (image-format image) 'iso9660) | |
431 | 'iso9660 | |
432 | 'dce))) | |
433 | (operating-system-uuid os type))) | |
434 | ||
435 | (let* ((root-file-system-type (image->root-file-system image)) | |
436 | (base-os (image-operating-system image)) | |
437 | (file-systems-to-keep | |
438 | (srfi-1:remove | |
439 | (lambda (fs) | |
440 | (string=? (file-system-mount-point fs) "/")) | |
441 | (operating-system-file-systems base-os))) | |
442 | (format (image-format image)) | |
443 | (os | |
444 | (operating-system | |
445 | (inherit base-os) | |
446 | (initrd (lambda (file-systems . rest) | |
447 | (apply (operating-system-initrd base-os) | |
448 | file-systems | |
449 | #:volatile-root? volatile-root? | |
450 | rest))) | |
451 | (bootloader (if (eq? format 'iso9660) | |
452 | (bootloader-configuration | |
453 | (inherit | |
454 | (operating-system-bootloader base-os)) | |
455 | (bootloader grub-mkrescue-bootloader)) | |
456 | (operating-system-bootloader base-os))) | |
457 | (file-systems (cons (file-system | |
458 | (mount-point "/") | |
459 | (device "/dev/placeholder") | |
460 | (type root-file-system-type)) | |
461 | file-systems-to-keep)))) | |
462 | (uuid (root-uuid os))) | |
463 | (operating-system | |
464 | (inherit os) | |
465 | (file-systems (cons (file-system | |
466 | (mount-point "/") | |
467 | (device uuid) | |
468 | (type root-file-system-type)) | |
469 | file-systems-to-keep))))) | |
470 | ||
471 | (define* (make-system-image image) | |
472 | "Return the derivation of IMAGE. It can be a raw disk-image or an ISO9660 | |
473 | image, depending on IMAGE format." | |
474 | (define substitutable? (image-substitutable? image)) | |
475 | ||
476 | (let* ((os (operating-system-for-image image)) | |
477 | (image* (image-with-os image os)) | |
478 | (register-closures? (has-guix-service-type? os)) | |
479 | (bootcfg (operating-system-bootcfg os)) | |
480 | (bootloader (bootloader-configuration-bootloader | |
481 | (operating-system-bootloader os)))) | |
482 | (case (image-format image) | |
483 | ((disk-image) | |
484 | (system-disk-image image* | |
485 | #:bootcfg bootcfg | |
486 | #:bootloader bootloader | |
487 | #:register-closures? register-closures? | |
488 | #:inputs `(("system" ,os) | |
489 | ("bootcfg" ,bootcfg)))) | |
490 | ((iso9660) | |
491 | (system-iso9660-image image* | |
492 | #:bootcfg bootcfg | |
493 | #:bootloader bootloader | |
494 | #:register-closures? register-closures? | |
495 | #:inputs `(("system" ,os) | |
496 | ("bootcfg" ,bootcfg)) | |
497 | #:grub-mkrescue-environment | |
498 | '(("MKRESCUE_SED_MODE" . "mbr_hfs"))))))) | |
499 | ||
500 | (define (find-image file-system-type) | |
501 | "Find and return an image that could match the given FILE-SYSTEM-TYPE. This | |
502 | is useful to adapt to interfaces written before the addition of the <image> | |
503 | record." | |
504 | ;; XXX: Add support for system and target here, or in the caller. | |
505 | (match file-system-type | |
506 | ("iso9660" iso9660-image) | |
507 | (_ efi-disk-image))) | |
508 | ||
509 | (define (system-image image) | |
510 | "Wrap 'make-system-image' call, so that it is used only if the given IMAGE | |
511 | is supported. Otherwise, fallback to image creation in a VM. This is | |
512 | temporary and should be removed once 'make-system-image' is able to deal with | |
513 | all types of images." | |
514 | (define substitutable? (image-substitutable? image)) | |
515 | (define volatile-root? (image-volatile-root? image)) | |
516 | ||
517 | (let* ((image-os (image-operating-system image)) | |
518 | (image-root-filesystem-type (image->root-file-system image)) | |
519 | (bootloader (bootloader-configuration-bootloader | |
520 | (operating-system-bootloader image-os))) | |
521 | (bootloader-name (bootloader-name bootloader)) | |
522 | (size (image-size image)) | |
523 | (format (image-format image))) | |
524 | (mbegin %store-monad | |
525 | (if (and (or (eq? bootloader-name 'grub) | |
526 | (eq? bootloader-name 'extlinux)) | |
527 | (eq? format 'disk-image)) | |
528 | ;; Fallback to image creation in a VM when it is not yet supported | |
529 | ;; by this module. | |
530 | (system-disk-image-in-vm image-os | |
531 | #:disk-image-size size | |
532 | #:file-system-type image-root-filesystem-type | |
533 | #:volatile? volatile-root? | |
534 | #:substitutable? substitutable?) | |
535 | (lower-object | |
536 | (make-system-image image)))))) | |
537 | ||
538 | ;;; image.scm ends here |