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