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