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