Merge branch 'master' into core-updates
[jackhill/guix/guix.git] / gnu / system / vm.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2013, 2014, 2015, 2016, 2017, 2018, 2019 Ludovic Courtès <ludo@gnu.org>
3 ;;; Copyright © 2016 Christopher Allan Webber <cwebber@dustycloud.org>
4 ;;; Copyright © 2016, 2017 Leo Famulari <leo@famulari.name>
5 ;;; Copyright © 2017 Mathieu Othacehe <m.othacehe@gmail.com>
6 ;;; Copyright © 2017 Marius Bakke <mbakke@fastmail.com>
7 ;;; Copyright © 2018 Chris Marusich <cmmarusich@gmail.com>
8 ;;;
9 ;;; This file is part of GNU Guix.
10 ;;;
11 ;;; GNU Guix is free software; you can redistribute it and/or modify it
12 ;;; under the terms of the GNU General Public License as published by
13 ;;; the Free Software Foundation; either version 3 of the License, or (at
14 ;;; your option) any later version.
15 ;;;
16 ;;; GNU Guix is distributed in the hope that it will be useful, but
17 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;;; GNU General Public License for more details.
20 ;;;
21 ;;; You should have received a copy of the GNU General Public License
22 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
23
24 (define-module (gnu system vm)
25 #:use-module (guix config)
26 #:use-module (guix store)
27 #:use-module (guix gexp)
28 #:use-module (guix derivations)
29 #:use-module (guix packages)
30 #:use-module (guix monads)
31 #:use-module (guix records)
32 #:use-module (guix modules)
33 #:use-module (guix utils)
34 #:use-module (gcrypt hash)
35 #:use-module (guix base32)
36 #:use-module ((guix self) #:select (make-config.scm))
37
38 #:use-module ((gnu build vm)
39 #:select (qemu-command))
40 #:use-module (gnu packages base)
41 #:use-module (gnu packages bootloaders)
42 #:use-module (gnu packages cdrom)
43 #:use-module (gnu packages compression)
44 #:use-module (gnu packages guile)
45 #:autoload (gnu packages gnupg) (guile-gcrypt)
46 #:use-module (gnu packages gawk)
47 #:use-module (gnu packages bash)
48 #:use-module (gnu packages less)
49 #:use-module (gnu packages virtualization)
50 #:use-module (gnu packages disk)
51 #:use-module (gnu packages zile)
52 #:use-module (gnu packages linux)
53 #:use-module ((gnu packages make-bootstrap)
54 #:select (%guile-static-stripped))
55 #:use-module (gnu packages admin)
56
57 #:use-module (gnu bootloader)
58 #:use-module (gnu bootloader grub)
59 #:use-module (gnu system shadow)
60 #:use-module (gnu system pam)
61 #:use-module (gnu system linux-container)
62 #:use-module (gnu system linux-initrd)
63 #:use-module (gnu bootloader)
64 #:use-module (gnu system file-systems)
65 #:use-module (gnu system)
66 #:use-module (gnu services)
67 #:use-module (gnu services base)
68 #:use-module (gnu system uuid)
69
70 #:use-module (srfi srfi-1)
71 #:use-module (srfi srfi-26)
72 #:use-module (rnrs bytevectors)
73 #:use-module (ice-9 match)
74
75 #:export (expression->derivation-in-linux-vm
76 qemu-image
77 virtualized-operating-system
78 system-qemu-image
79
80 system-qemu-image/shared-store
81 system-qemu-image/shared-store-script
82 system-disk-image
83 system-docker-image
84
85 virtual-machine
86 virtual-machine?))
87
88 \f
89 ;;; Commentary:
90 ;;;
91 ;;; Tools to evaluate build expressions within virtual machines.
92 ;;;
93 ;;; Code:
94
95 (define %linux-vm-file-systems
96 ;; File systems mounted for 'derivation-in-linux-vm'. These are shared with
97 ;; the host over 9p.
98 ;;
99 ;; The 9p documentation says that cache=loose is "intended for exclusive,
100 ;; read-only mounts", without additional details. It's much faster than the
101 ;; default cache=none, especially when copying and registering store items.
102 ;; Thus, use cache=loose, except for /xchg where we want to ensure
103 ;; consistency.
104 (list (file-system
105 (mount-point (%store-prefix))
106 (device "store")
107 (type "9p")
108 (needed-for-boot? #t)
109 (flags '(read-only))
110 (options "trans=virtio,cache=loose")
111 (check? #f))
112 (file-system
113 (mount-point "/xchg")
114 (device "xchg")
115 (type "9p")
116 (needed-for-boot? #t)
117 (options "trans=virtio")
118 (check? #f))
119 (file-system
120 (mount-point "/tmp")
121 (device "tmp")
122 (type "9p")
123 (needed-for-boot? #t)
124 (options "trans=virtio,cache=loose")
125 (check? #f))))
126
127 (define not-config?
128 ;; Select (guix …) and (gnu …) modules, except (guix config).
129 (match-lambda
130 (('guix 'config) #f)
131 (('guix rest ...) #t)
132 (('gnu rest ...) #t)
133 (rest #f)))
134
135 (define gcrypt-sqlite3&co
136 ;; Guile-Gcrypt, Guile-SQLite3, and their propagated inputs.
137 (append-map (lambda (package)
138 (cons package
139 (match (package-transitive-propagated-inputs package)
140 (((labels packages) ...)
141 packages))))
142 (list guile-gcrypt guile-sqlite3)))
143
144 (define* (expression->derivation-in-linux-vm name exp
145 #:key
146 (system (%current-system)) target
147 (linux linux-libre)
148 initrd
149 (qemu qemu-minimal)
150 (env-vars '())
151 (guile-for-build
152 (%guile-for-build))
153 (file-systems
154 %linux-vm-file-systems)
155
156 (single-file-output? #f)
157 (make-disk-image? #f)
158 (references-graphs #f)
159 (memory-size 256)
160 (disk-image-format "qcow2")
161 (disk-image-size 'guess))
162 "Evaluate EXP in a QEMU virtual machine running LINUX with INITRD (a
163 derivation). The virtual machine runs with MEMORY-SIZE MiB of memory. In the
164 virtual machine, EXP has access to FILE-SYSTEMS, which, by default, includes a
165 9p share of the store, the '/xchg' where EXP should put its output file(s),
166 and a 9p share of /tmp.
167
168 If SINGLE-FILE-OUTPUT? is true, copy a single file from '/xchg' to OUTPUT.
169 Otherwise, copy the contents of /xchg to a new directory OUTPUT.
170
171 When MAKE-DISK-IMAGE? is true, then create a QEMU disk image of type
172 DISK-IMAGE-FORMAT (e.g., 'qcow2' or 'raw'), of DISK-IMAGE-SIZE bytes and
173 return it. When DISK-IMAGE-SIZE is 'guess, estimate the image size based
174 based on the size of the closure of REFERENCES-GRAPHS.
175
176 When REFERENCES-GRAPHS is true, it must be a list of file name/store path
177 pairs, as for `derivation'. The files containing the reference graphs are
178 made available under the /xchg CIFS share."
179 (define user-builder
180 (program-file "builder-in-linux-vm" exp))
181
182 (define loader
183 ;; Invoke USER-BUILDER instead using 'primitive-load'. The reason for
184 ;; this is to allow USER-BUILDER to dlopen stuff by using a full-featured
185 ;; Guile, which it couldn't do using the statically-linked guile used in
186 ;; the initrd. See example at
187 ;; <https://lists.gnu.org/archive/html/guix-devel/2017-10/msg00233.html>.
188 (program-file "linux-vm-loader"
189 ;; When USER-BUILDER succeeds, reboot (indicating a
190 ;; success), otherwise die, which causes a kernel panic
191 ;; ("Attempted to kill init!").
192 #~(if (zero? (system* #$user-builder))
193 (reboot)
194 (exit 1))))
195
196 (let ((initrd (or initrd
197 (base-initrd file-systems
198 #:on-error 'backtrace
199 #:linux linux
200 #:linux-modules %base-initrd-modules
201 #:qemu-networking? #t))))
202
203 (define builder
204 ;; Code that launches the VM that evaluates EXP.
205 (with-extensions gcrypt-sqlite3&co
206 (with-imported-modules `(,@(source-module-closure
207 '((guix build utils)
208 (gnu build vm))
209 #:select? not-config?)
210
211 ;; For consumption by (gnu store database).
212 ((guix config) => ,(make-config.scm)))
213 #~(begin
214 (use-modules (guix build utils)
215 (gnu build vm))
216
217 (let* ((native-inputs
218 '#+(list qemu (canonical-package coreutils)))
219 (linux (string-append #$linux "/"
220 #$(system-linux-image-file-name)))
221 (initrd #$initrd)
222 (loader #$loader)
223 (graphs '#$(match references-graphs
224 (((graph-files . _) ...) graph-files)
225 (_ #f)))
226 (target #$(or (%current-target-system) (%current-system)))
227 (size #$(if (eq? 'guess disk-image-size)
228 #~(+ (* 70 (expt 2 20)) ;ESP
229 (estimated-partition-size graphs))
230 disk-image-size)))
231
232 (set-path-environment-variable "PATH" '("bin") native-inputs)
233
234 (load-in-linux-vm loader
235 #:output #$output
236 #:linux linux #:initrd initrd
237 #:qemu (qemu-command target)
238 #:memory-size #$memory-size
239 #:make-disk-image? #$make-disk-image?
240 #:single-file-output? #$single-file-output?
241 ;; FIXME: ‘target-arm32?’ and
242 ;; ‘target-aarch64?’ may not operate on the
243 ;; right system/target values. Rewrite
244 ;; using ‘let-system’ when available.
245 #:target-arm32? #$(target-arm32?)
246 #:target-aarch64? #$(target-aarch64?)
247 #:disk-image-format #$disk-image-format
248 #:disk-image-size size
249 #:references-graphs graphs))))))
250
251 (gexp->derivation name builder
252 ;; TODO: Require the "kvm" feature.
253 #:system system
254 #:target target
255 #:env-vars env-vars
256 #:guile-for-build guile-for-build
257 #:references-graphs references-graphs)))
258
259 (define (has-guix-service-type? os)
260 "Return true if OS contains a service of the type GUIX-SERVICE-TYPE."
261 (not (not (find (lambda (service)
262 (eq? (service-kind service) guix-service-type))
263 (operating-system-services os)))))
264
265 (define* (iso9660-image #:key
266 (name "iso9660-image")
267 file-system-label
268 file-system-uuid
269 (system (%current-system))
270 (target (%current-target-system))
271 (qemu qemu-minimal)
272 os
273 bootcfg-drv
274 bootloader
275 (register-closures? (has-guix-service-type? os))
276 (inputs '())
277 (grub-mkrescue-environment '()))
278 "Return a bootable, stand-alone iso9660 image.
279
280 INPUTS is a list of inputs (as for packages)."
281 (define schema
282 (and register-closures?
283 (local-file (search-path %load-path
284 "guix/store/schema.sql"))))
285
286 (expression->derivation-in-linux-vm
287 name
288 (with-extensions gcrypt-sqlite3&co
289 (with-imported-modules `(,@(source-module-closure '((gnu build vm)
290 (guix store database)
291 (guix build utils))
292 #:select? not-config?)
293 ((guix config) => ,(make-config.scm)))
294 #~(begin
295 (use-modules (gnu build vm)
296 (guix store database)
297 (guix build utils))
298
299 (sql-schema #$schema)
300
301 ;; Allow non-ASCII file names--e.g., 'nss-certs'--to be decoded.
302 (setenv "GUIX_LOCPATH"
303 #+(file-append glibc-utf8-locales "/lib/locale"))
304 (setlocale LC_ALL "en_US.utf8")
305
306 (let ((inputs
307 '#$(append (list parted e2fsprogs dosfstools xorriso)
308 (map canonical-package
309 (list sed grep coreutils findutils gawk))))
310
311
312 (graphs '#$(match inputs
313 (((names . _) ...)
314 names)))
315 ;; This variable is unused but allows us to add INPUTS-TO-COPY
316 ;; as inputs.
317 (to-register
318 '#$(map (match-lambda
319 ((name thing) thing)
320 ((name thing output) `(,thing ,output)))
321 inputs)))
322
323 (set-path-environment-variable "PATH" '("bin" "sbin") inputs)
324 (make-iso9660-image #$xorriso
325 '#$grub-mkrescue-environment
326 #$(bootloader-package bootloader)
327 #$bootcfg-drv
328 #$os
329 "/xchg/guixsd.iso"
330 #:register-closures? #$register-closures?
331 #:closures graphs
332 #:volume-id #$file-system-label
333 #:volume-uuid #$(and=> file-system-uuid
334 uuid-bytevector))))))
335 #:system system
336 #:target target
337
338 ;; Keep a local file system for /tmp so that we can populate it directly as
339 ;; root and have files owned by root. See <https://bugs.gnu.org/31752>.
340 #:file-systems (remove (lambda (file-system)
341 (string=? (file-system-mount-point file-system)
342 "/tmp"))
343 %linux-vm-file-systems)
344
345 #:make-disk-image? #f
346 #:single-file-output? #t
347 #:references-graphs inputs
348
349 ;; Xorriso seems to be quite memory-hungry, so increase the VM's RAM size.
350 #:memory-size 512))
351
352 (define* (qemu-image #:key
353 (name "qemu-image")
354 (system (%current-system))
355 (target (%current-target-system))
356 (qemu qemu-minimal)
357 (disk-image-size 'guess)
358 (disk-image-format "qcow2")
359 (file-system-type "ext4")
360 file-system-label
361 file-system-uuid
362 os
363 bootcfg-drv
364 bootloader
365 (register-closures? (has-guix-service-type? os))
366 (inputs '())
367 copy-inputs?)
368 "Return a bootable, stand-alone QEMU image of type DISK-IMAGE-FORMAT (e.g.,
369 'qcow2' or 'raw'), with a root partition of type FILE-SYSTEM-TYPE.
370 Optionally, FILE-SYSTEM-LABEL can be specified as the volume name for the root
371 partition; likewise FILE-SYSTEM-UUID, if true, specifies the UUID of the root
372 partition (a UUID object).
373
374 The returned image is a full disk image that runs OS-DERIVATION,
375 with a GRUB installation that uses GRUB-CONFIGURATION as its configuration
376 file (GRUB-CONFIGURATION must be the name of a file in the VM.)
377
378 INPUTS is a list of inputs (as for packages). When COPY-INPUTS? is true, copy
379 all of INPUTS into the image being built. When REGISTER-CLOSURES? is true,
380 register INPUTS in the store database of the image so that Guix can be used in
381 the image. By default, REGISTER-CLOSURES? is set to true only if a service of
382 type GUIX-SERVICE-TYPE is present in the services definition of the operating
383 system."
384 (define schema
385 (and register-closures?
386 (local-file (search-path %load-path
387 "guix/store/schema.sql"))))
388
389 (expression->derivation-in-linux-vm
390 name
391 (with-extensions gcrypt-sqlite3&co
392 (with-imported-modules `(,@(source-module-closure '((gnu build vm)
393 (gnu build bootloader)
394 (guix store database)
395 (guix build utils))
396 #:select? not-config?)
397 ((guix config) => ,(make-config.scm)))
398 #~(begin
399 (use-modules (gnu build bootloader)
400 (gnu build vm)
401 (guix store database)
402 (guix build utils)
403 (srfi srfi-26)
404 (ice-9 binary-ports))
405
406 (sql-schema #$schema)
407
408 ;; Allow non-ASCII file names--e.g., 'nss-certs'--to be decoded.
409 (setenv "GUIX_LOCPATH"
410 #+(file-append glibc-utf8-locales "/lib/locale"))
411 (setlocale LC_ALL "en_US.utf8")
412
413 (let ((inputs
414 '#$(append (list parted e2fsprogs dosfstools)
415 (map canonical-package
416 (list sed grep coreutils findutils gawk))))
417
418 ;; This variable is unused but allows us to add INPUTS-TO-COPY
419 ;; as inputs.
420 (to-register
421 '#$(map (match-lambda
422 ((name thing) thing)
423 ((name thing output) `(,thing ,output)))
424 inputs)))
425
426 (set-path-environment-variable "PATH" '("bin" "sbin") inputs)
427
428 (let* ((graphs '#$(match inputs
429 (((names . _) ...)
430 names)))
431 (initialize (root-partition-initializer
432 #:closures graphs
433 #:copy-closures? #$copy-inputs?
434 #:register-closures? #$register-closures?
435 #:system-directory #$os
436
437 ;; Disable deduplication to speed things up,
438 ;; and because it doesn't help much for a
439 ;; single system generation.
440 #:deduplicate? #f))
441 (root-size #$(if (eq? 'guess disk-image-size)
442 #~(max
443 ;; Minimum 20 MiB root size
444 (* 20 (expt 2 20))
445 (estimated-partition-size
446 (map (cut string-append "/xchg/" <>)
447 graphs)))
448 (- disk-image-size
449 (* 50 (expt 2 20)))))
450 (partitions
451 (append
452 (list (partition
453 (size root-size)
454 (label #$file-system-label)
455 (uuid #$(and=> file-system-uuid
456 uuid-bytevector))
457 (file-system #$file-system-type)
458 (flags '(boot))
459 (initializer initialize)))
460 ;; Append a small EFI System Partition for use with UEFI
461 ;; bootloaders if we are not targeting ARM because UEFI
462 ;; support in U-Boot is experimental.
463 ;;
464 ;; FIXME: ‘target-arm?’ may be not operate on the right
465 ;; system/target values. Rewrite using ‘let-system’ when
466 ;; available.
467 (if #$(target-arm?)
468 '()
469 (list (partition
470 ;; The standalone grub image is about 10MiB, but
471 ;; leave some room for custom or multiple images.
472 (size (* 40 (expt 2 20)))
473 (label "GNU-ESP") ;cosmetic only
474 ;; Use "vfat" here since this property is used
475 ;; when mounting. The actual FAT-ness is based
476 ;; on file system size (16 in this case).
477 (file-system "vfat")
478 (flags '(esp)))))))
479 (grub-efi #$(and (not (target-arm?)) grub-efi)))
480 (initialize-hard-disk "/dev/vda"
481 #:partitions partitions
482 #:grub-efi grub-efi
483 #:bootloader-package
484 #$(bootloader-package bootloader)
485 #:bootcfg #$bootcfg-drv
486 #:bootcfg-location
487 #$(bootloader-configuration-file bootloader)
488 #:bootloader-installer
489 #$(bootloader-installer bootloader)))))))
490 #:system system
491 #:target target
492 #:make-disk-image? #t
493 #:disk-image-size disk-image-size
494 #:disk-image-format disk-image-format
495 #:references-graphs inputs))
496
497 (define* (system-docker-image os
498 #:key
499 (name "guix-docker-image")
500 (register-closures? (has-guix-service-type? os)))
501 "Build a docker image. OS is the desired <operating-system>. NAME is the
502 base name to use for the output file. When REGISTER-CLOSURES? is true,
503 register the closure of OS with Guix in the resulting Docker image. By
504 default, REGISTER-CLOSURES? is set to true only if a service of type
505 GUIX-SERVICE-TYPE is present in the services definition of the operating
506 system."
507 (define schema
508 (and register-closures?
509 (local-file (search-path %load-path
510 "guix/store/schema.sql"))))
511
512 (define boot-program
513 ;; Program that runs the boot script of OS, which in turn starts shepherd.
514 (program-file "boot-program"
515 #~(let ((system (cadr (command-line))))
516 (setenv "GUIX_NEW_SYSTEM" system)
517 (execl #$(file-append guile-2.2 "/bin/guile")
518 "guile" "--no-auto-compile"
519 (string-append system "/boot")))))
520
521
522 (let ((os (operating-system-with-gc-roots
523 (containerized-operating-system os '())
524 (list boot-program)))
525 (name (string-append name ".tar.gz"))
526 (graph "system-graph"))
527 (define build
528 (with-extensions (cons guile-json-3 ;for (guix docker)
529 gcrypt-sqlite3&co) ;for (guix store database)
530 (with-imported-modules `(,@(source-module-closure
531 '((guix docker)
532 (guix store database)
533 (guix build utils)
534 (guix build store-copy)
535 (gnu build vm))
536 #:select? not-config?)
537 ((guix config) => ,(make-config.scm)))
538 #~(begin
539 (use-modules (guix docker)
540 (guix build utils)
541 (gnu build vm)
542 (srfi srfi-19)
543 (guix build store-copy)
544 (guix store database))
545
546 ;; Set the SQL schema location.
547 (sql-schema #$schema)
548
549 ;; Allow non-ASCII file names--e.g., 'nss-certs'--to be decoded.
550 (setenv "GUIX_LOCPATH"
551 #+(file-append glibc-utf8-locales "/lib/locale"))
552 (setlocale LC_ALL "en_US.utf8")
553
554 (let* (;; This initializer requires elevated privileges that are
555 ;; not normally available in the build environment (e.g.,
556 ;; it needs to create device nodes). In order to obtain
557 ;; such privileges, we run it as root in a VM.
558 (initialize (root-partition-initializer
559 #:closures '(#$graph)
560 #:register-closures? #$register-closures?
561 #:system-directory #$os
562 ;; De-duplication would fail due to
563 ;; cross-device link errors, so don't do it.
564 #:deduplicate? #f))
565 ;; Even as root in a VM, the initializer would fail due to
566 ;; lack of privileges if we use a root-directory that is on
567 ;; a file system that is shared with the host (e.g., /tmp).
568 (root-directory "/guixsd-system-root"))
569 (set-path-environment-variable "PATH" '("bin" "sbin") '(#+tar))
570 (mkdir root-directory)
571 (initialize root-directory)
572 (build-docker-image
573 (string-append "/xchg/" #$name) ;; The output file.
574 (cons* root-directory
575 (map store-info-item
576 (call-with-input-file
577 (string-append "/xchg/" #$graph)
578 read-reference-graph)))
579 #$os
580 #:entry-point '(#$boot-program #$os)
581 #:compressor '(#+(file-append gzip "/bin/gzip") "-9n")
582 #:creation-time (make-time time-utc 0 1)
583 #:transformations `((,root-directory -> ""))))))))
584
585 (expression->derivation-in-linux-vm
586 name build
587 #:make-disk-image? #f
588 #:single-file-output? #t
589 #:references-graphs `((,graph ,os)))))
590
591 \f
592 ;;;
593 ;;; VM and disk images.
594 ;;;
595
596 (define* (operating-system-uuid os #:optional (type 'dce))
597 "Compute UUID object with a deterministic \"UUID\" for OS, of the given
598 TYPE (one of 'iso9660 or 'dce). Return a UUID object."
599 ;; Note: For this to be deterministic, we must not hash things that contains
600 ;; (directly or indirectly) procedures, for example. That rules out
601 ;; anything that contains gexps, thunk or delayed record fields, etc.
602
603 (define service-name
604 (compose service-type-name service-kind))
605
606 (define (file-system-digest fs)
607 ;; Return a hashable digest that does not contain 'dependencies' since
608 ;; this field can contain procedures.
609 (let ((device (file-system-device fs)))
610 (list (file-system-mount-point fs)
611 (file-system-type fs)
612 (file-system-device->string device)
613 (file-system-options fs))))
614
615 (if (eq? type 'iso9660)
616 (let ((pad (compose (cut string-pad <> 2 #\0)
617 number->string))
618 (h (hash (map service-name (operating-system-services os))
619 3600)))
620 (bytevector->uuid
621 (string->iso9660-uuid
622 (string-append "1970-01-01-"
623 (pad (hash (operating-system-host-name os) 24)) "-"
624 (pad (quotient h 60)) "-"
625 (pad (modulo h 60)) "-"
626 (pad (hash (map file-system-digest
627 (operating-system-file-systems os))
628 100))))
629 'iso9660))
630 (bytevector->uuid
631 (uint-list->bytevector
632 (list (hash file-system-type
633 (- (expt 2 32) 1))
634 (hash (operating-system-host-name os)
635 (- (expt 2 32) 1))
636 (hash (map service-name (operating-system-services os))
637 (- (expt 2 32) 1))
638 (hash (map file-system-digest (operating-system-file-systems os))
639 (- (expt 2 32) 1)))
640 (endianness little)
641 4)
642 type)))
643
644 (define* (system-disk-image os
645 #:key
646 (name "disk-image")
647 (file-system-type "ext4")
648 (disk-image-size (* 900 (expt 2 20)))
649 (volatile? #t))
650 "Return the derivation of a disk image of DISK-IMAGE-SIZE bytes of the
651 system described by OS. Said image can be copied on a USB stick as is. When
652 VOLATILE? is true, the root file system is made volatile; this is useful
653 to USB sticks meant to be read-only."
654 (define normalize-label
655 ;; ISO labels are all-caps (case-insensitive), but since
656 ;; 'find-partition-by-label' is case-sensitive, make it all-caps here.
657 (if (string=? "iso9660" file-system-type)
658 string-upcase
659 identity))
660
661 (define root-label
662 ;; Volume name of the root file system.
663 (normalize-label "Guix_image"))
664
665 (define (root-uuid os)
666 ;; UUID of the root file system, computed in a deterministic fashion.
667 ;; This is what we use to locate the root file system so it has to be
668 ;; different from the user's own file system UUIDs.
669 (operating-system-uuid os
670 (if (string=? file-system-type "iso9660")
671 'iso9660
672 'dce)))
673
674 (define file-systems-to-keep
675 (remove (lambda (fs)
676 (string=? (file-system-mount-point fs) "/"))
677 (operating-system-file-systems os)))
678
679 (let* ((os (operating-system (inherit os)
680 ;; Since this is meant to be used on real hardware, don't
681 ;; install QEMU networking or anything like that. Assume USB
682 ;; mass storage devices (usb-storage.ko) are available.
683 (initrd (lambda (file-systems . rest)
684 (apply (operating-system-initrd os)
685 file-systems
686 #:volatile-root? volatile?
687 rest)))
688
689 (bootloader (if (string=? "iso9660" file-system-type)
690 (bootloader-configuration
691 (inherit (operating-system-bootloader os))
692 (bootloader grub-mkrescue-bootloader))
693 (operating-system-bootloader os)))
694
695 ;; Force our own root file system. (We need a "/" file system
696 ;; to call 'root-uuid'.)
697 (file-systems (cons (file-system
698 (mount-point "/")
699 (device "/dev/placeholder")
700 (type file-system-type))
701 file-systems-to-keep))))
702 (uuid (root-uuid os))
703 (os (operating-system
704 (inherit os)
705 (file-systems (cons (file-system
706 (mount-point "/")
707 (device uuid)
708 (type file-system-type))
709 file-systems-to-keep))))
710 (bootcfg (operating-system-bootcfg os)))
711 (if (string=? "iso9660" file-system-type)
712 (iso9660-image #:name name
713 #:file-system-label root-label
714 #:file-system-uuid uuid
715 #:os os
716 #:bootcfg-drv bootcfg
717 #:bootloader (bootloader-configuration-bootloader
718 (operating-system-bootloader os))
719 #:inputs `(("system" ,os)
720 ("bootcfg" ,bootcfg))
721 #:grub-mkrescue-environment
722 '(("MKRESCUE_SED_MODE" . "mbr_hfs")))
723 (qemu-image #:name name
724 #:os os
725 #:bootcfg-drv bootcfg
726 #:bootloader (bootloader-configuration-bootloader
727 (operating-system-bootloader os))
728 #:disk-image-size disk-image-size
729 #:disk-image-format "raw"
730 #:file-system-type file-system-type
731 #:file-system-label root-label
732 #:file-system-uuid uuid
733 #:copy-inputs? #t
734 #:inputs `(("system" ,os)
735 ("bootcfg" ,bootcfg))))))
736
737 (define* (system-qemu-image os
738 #:key
739 (file-system-type "ext4")
740 (disk-image-size (* 900 (expt 2 20))))
741 "Return the derivation of a freestanding QEMU image of DISK-IMAGE-SIZE bytes
742 of the GNU system as described by OS."
743 (define file-systems-to-keep
744 ;; Keep only file systems other than root and not normally bound to real
745 ;; devices.
746 (remove (lambda (fs)
747 (let ((target (file-system-mount-point fs))
748 (source (file-system-device fs)))
749 (or (string=? target "/")
750 (string-prefix? "/dev/" source))))
751 (operating-system-file-systems os)))
752
753 (define root-uuid
754 ;; UUID of the root file system.
755 (operating-system-uuid os
756 (if (string=? file-system-type "iso9660")
757 'iso9660
758 'dce)))
759
760
761 (let* ((os (operating-system (inherit os)
762 ;; Assume we have an initrd with the whole QEMU shebang.
763
764 ;; Force our own root file system. Refer to it by UUID so that
765 ;; it works regardless of how the image is used ("qemu -hda",
766 ;; Xen, etc.).
767 (file-systems (cons (file-system
768 (mount-point "/")
769 (device root-uuid)
770 (type file-system-type))
771 file-systems-to-keep))))
772 (bootcfg (operating-system-bootcfg os)))
773 (qemu-image #:os os
774 #:bootcfg-drv bootcfg
775 #:bootloader (bootloader-configuration-bootloader
776 (operating-system-bootloader os))
777 #:disk-image-size disk-image-size
778 #:file-system-type file-system-type
779 #:file-system-uuid root-uuid
780 #:inputs `(("system" ,os)
781 ("bootcfg" ,bootcfg))
782 #:copy-inputs? #t)))
783
784 \f
785 ;;;
786 ;;; VMs that share file systems with the host.
787 ;;;
788
789 (define (file-system->mount-tag fs)
790 "Return a 9p mount tag for host file system FS."
791 ;; QEMU mount tags must be ASCII, at most 31-byte long, cannot contain
792 ;; slashes, and cannot start with '_'. Compute an identifier that
793 ;; corresponds to the rules.
794 (string-append "TAG"
795 (string-drop (bytevector->base32-string
796 (sha1 (string->utf8 fs)))
797 4)))
798
799 (define (mapping->file-system mapping)
800 "Return a 9p file system that realizes MAPPING."
801 (match mapping
802 (($ <file-system-mapping> source target writable?)
803 (file-system
804 (mount-point target)
805 (device (file-system->mount-tag source))
806 (type "9p")
807 (flags (if writable? '() '(read-only)))
808 (options "trans=virtio,cache=loose")
809 (check? #f)
810 (create-mount-point? #t)))))
811
812 (define* (virtualized-operating-system os mappings #:optional (full-boot? #f))
813 "Return an operating system based on OS suitable for use in a virtualized
814 environment with the store shared with the host. MAPPINGS is a list of
815 <file-system-mapping> to realize in the virtualized OS."
816 (define user-file-systems
817 ;; Remove file systems that conflict with those added below, or that are
818 ;; normally bound to real devices.
819 (remove (lambda (fs)
820 (let ((target (file-system-mount-point fs))
821 (source (file-system-device fs)))
822 (or (string=? target (%store-prefix))
823 (string=? target "/")
824 (and (string? source)
825 (string-prefix? "/dev/" source))
826
827 ;; Labels and UUIDs are necessarily invalid in the VM.
828 (and (file-system-mount? fs)
829 (or (file-system-label? source)
830 (uuid? source))))))
831 (operating-system-file-systems os)))
832
833 (define virtual-file-systems
834 (cons (file-system
835 (mount-point "/")
836 (device "/dev/vda1")
837 (type "ext4"))
838
839 (append (map mapping->file-system mappings)
840 user-file-systems)))
841
842 (operating-system (inherit os)
843
844 ;; XXX: Until we run QEMU with UEFI support (with the OVMF firmware),
845 ;; force the traditional i386/BIOS method.
846 ;; See <https://bugs.gnu.org/28768>.
847 (bootloader (bootloader-configuration
848 (inherit (operating-system-bootloader os))
849 (bootloader grub-bootloader)
850 (target "/dev/vda")))
851
852 (initrd (lambda (file-systems . rest)
853 (apply (operating-system-initrd os)
854 file-systems
855 #:volatile-root? #t
856 rest)))
857
858 ;; Disable swap.
859 (swap-devices '())
860
861 ;; XXX: When FULL-BOOT? is true, do not add a 9p mount for /gnu/store
862 ;; since that would lead the bootloader config to look for the kernel and
863 ;; initrd in it.
864 (file-systems (if full-boot?
865 virtual-file-systems
866 (cons
867 (file-system
868 (inherit (mapping->file-system %store-mapping))
869 (needed-for-boot? #t))
870 virtual-file-systems)))))
871
872 (define* (system-qemu-image/shared-store
873 os
874 #:key
875 full-boot?
876 (disk-image-size (* (if full-boot? 500 30) (expt 2 20))))
877 "Return a derivation that builds a QEMU image of OS that shares its store
878 with the host.
879
880 When FULL-BOOT? is true, return an image that does a complete boot sequence,
881 bootloaded included; thus, make a disk image that contains everything the
882 bootloader refers to: OS kernel, initrd, bootloader data, etc."
883 (define root-uuid
884 ;; Use a fixed UUID to improve determinism.
885 (operating-system-uuid os 'dce))
886
887 (define bootcfg
888 (operating-system-bootcfg os))
889
890 ;; XXX: When FULL-BOOT? is true, we end up creating an image that contains
891 ;; BOOTCFG and all its dependencies, including the output of OS.
892 ;; This is more than needed (we only need the kernel, initrd, GRUB for its
893 ;; font, and the background image), but it's hard to filter that.
894 (qemu-image #:os os
895 #:bootcfg-drv bootcfg
896 #:bootloader (bootloader-configuration-bootloader
897 (operating-system-bootloader os))
898 #:disk-image-size disk-image-size
899 #:file-system-uuid root-uuid
900 #:inputs (if full-boot?
901 `(("bootcfg" ,bootcfg))
902 '())
903
904 ;; XXX: Passing #t here is too slow, so let it off by default.
905 #:register-closures? #f
906 #:copy-inputs? full-boot?))
907
908 (define* (common-qemu-options image shared-fs)
909 "Return the a string-value gexp with the common QEMU options to boot IMAGE,
910 with '-virtfs' options for the host file systems listed in SHARED-FS."
911
912 (define (virtfs-option fs)
913 #~(format #f "-virtfs local,path=~s,security_model=none,mount_tag=~s"
914 #$fs #$(file-system->mount-tag fs)))
915
916 #~(;; Only enable kvm if we see /dev/kvm exists.
917 ;; This allows users without hardware virtualization to still use these
918 ;; commands.
919 #$@(if (file-exists? "/dev/kvm")
920 '("-enable-kvm")
921 '())
922
923 "-no-reboot"
924 "-object" "rng-random,filename=/dev/urandom,id=guixsd-vm-rng"
925 "-device" "virtio-rng-pci,rng=guixsd-vm-rng"
926
927 #$@(map virtfs-option shared-fs)
928 "-vga std"
929 (format #f "-drive file=~a,if=virtio,cache=writeback,werror=report,readonly"
930 #$image)))
931
932 (define* (system-qemu-image/shared-store-script os
933 #:key
934 (qemu qemu)
935 (graphic? #t)
936 (memory-size 256)
937 (mappings '())
938 full-boot?
939 (disk-image-size
940 (* (if full-boot? 500 70)
941 (expt 2 20)))
942 (options '()))
943 "Return a derivation that builds a script to run a virtual machine image of
944 OS that shares its store with the host. The virtual machine runs with
945 MEMORY-SIZE MiB of memory.
946
947 MAPPINGS is a list of <file-system-mapping> specifying mapping of host file
948 systems into the guest.
949
950 When FULL-BOOT? is true, the returned script runs everything starting from the
951 bootloader; otherwise it directly starts the operating system kernel. The
952 DISK-IMAGE-SIZE parameter specifies the size in bytes of the root disk image;
953 it is mostly useful when FULL-BOOT? is true."
954 (mlet* %store-monad ((os -> (virtualized-operating-system os mappings full-boot?))
955 (image (system-qemu-image/shared-store
956 os
957 #:full-boot? full-boot?
958 #:disk-image-size disk-image-size)))
959 (define kernel-arguments
960 #~(list #$@(if graphic? #~() #~("console=ttyS0"))
961 #+@(operating-system-kernel-arguments os "/dev/vda1")))
962
963 (define qemu-exec
964 #~(list (string-append #$qemu "/bin/" #$(qemu-command (%current-system)))
965 #$@(if full-boot?
966 #~()
967 #~("-kernel" #$(operating-system-kernel-file os)
968 "-initrd" #$(file-append os "/initrd")
969 (format #f "-append ~s"
970 (string-join #$kernel-arguments " "))))
971 #$@(common-qemu-options image
972 (map file-system-mapping-source
973 (cons %store-mapping mappings)))
974 "-m " (number->string #$memory-size)
975 #$@options))
976
977 (define builder
978 #~(call-with-output-file #$output
979 (lambda (port)
980 (format port "#!~a~% exec ~a \"$@\"~%"
981 #$(file-append bash "/bin/sh")
982 (string-join #$qemu-exec " "))
983 (chmod port #o555))))
984
985 (gexp->derivation "run-vm.sh" builder)))
986
987 \f
988 ;;;
989 ;;; High-level abstraction.
990 ;;;
991
992 (define-record-type* <virtual-machine> %virtual-machine
993 make-virtual-machine
994 virtual-machine?
995 (operating-system virtual-machine-operating-system) ;<operating-system>
996 (qemu virtual-machine-qemu ;<package>
997 (default qemu))
998 (graphic? virtual-machine-graphic? ;Boolean
999 (default #f))
1000 (memory-size virtual-machine-memory-size ;integer (MiB)
1001 (default 256))
1002 (disk-image-size virtual-machine-disk-image-size ;integer (bytes)
1003 (default 'guess))
1004 (port-forwardings virtual-machine-port-forwardings ;list of integer pairs
1005 (default '())))
1006
1007 (define-syntax virtual-machine
1008 (syntax-rules ()
1009 "Declare a virtual machine running the specified OS, with the given
1010 options."
1011 ((_ os) ;shortcut
1012 (%virtual-machine (operating-system os)))
1013 ((_ fields ...)
1014 (%virtual-machine fields ...))))
1015
1016 (define (port-forwardings->qemu-options forwardings)
1017 "Return the QEMU option for the given port FORWARDINGS as a string, where
1018 FORWARDINGS is a list of host-port/guest-port pairs."
1019 (string-join
1020 (map (match-lambda
1021 ((host-port . guest-port)
1022 (string-append "hostfwd=tcp::"
1023 (number->string host-port)
1024 "-:" (number->string guest-port))))
1025 forwardings)
1026 ","))
1027
1028 (define-gexp-compiler (virtual-machine-compiler (vm <virtual-machine>)
1029 system target)
1030 ;; XXX: SYSTEM and TARGET are ignored.
1031 (match vm
1032 (($ <virtual-machine> os qemu graphic? memory-size disk-image-size ())
1033 (system-qemu-image/shared-store-script os
1034 #:qemu qemu
1035 #:graphic? graphic?
1036 #:memory-size memory-size
1037 #:disk-image-size
1038 disk-image-size))
1039 (($ <virtual-machine> os qemu graphic? memory-size disk-image-size
1040 forwardings)
1041 (let ((options
1042 `("-nic" ,(string-append
1043 "user,model=virtio-net-pci,"
1044 (port-forwardings->qemu-options forwardings)))))
1045 (system-qemu-image/shared-store-script os
1046 #:qemu qemu
1047 #:graphic? graphic?
1048 #:memory-size memory-size
1049 #:disk-image-size
1050 disk-image-size
1051 #:options options)))))
1052
1053 ;;; vm.scm ends here