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>
9 ;;; This file is part of GNU Guix.
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.
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.
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/>.
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))
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)
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 system uuid)
69 #:use-module (srfi srfi-1)
70 #:use-module (srfi srfi-26)
71 #:use-module (rnrs bytevectors)
72 #:use-module (ice-9 match)
74 #:export (expression->derivation-in-linux-vm
76 virtualized-operating-system
79 system-qemu-image/shared-store
80 system-qemu-image/shared-store-script
90 ;;; Tools to evaluate build expressions within virtual machines.
94 (define %linux-vm-file-systems
95 ;; File systems mounted for 'derivation-in-linux-vm'. These are shared with
98 (mount-point (%store-prefix))
101 (needed-for-boot? #t)
103 (options "trans=virtio,cache=loose")
106 ;; The 9p documentation says that cache=loose is "intended for
107 ;; exclusive, read-only mounts", without additional details. In
108 ;; practice it seems to work well for these, and it's much faster than
109 ;; the default cache=none, especially when copying and registering
112 (mount-point "/xchg")
115 (needed-for-boot? #t)
116 (options "trans=virtio,cache=loose")
122 (needed-for-boot? #t)
123 (options "trans=virtio,cache=loose")
127 ;; Select (guix …) and (gnu …) modules, except (guix config).
130 (('guix rest ...) #t)
134 (define gcrypt-sqlite3&co
135 ;; Guile-Gcrypt, Guile-SQLite3, and their propagated inputs.
136 (append-map (lambda (package)
138 (match (package-transitive-propagated-inputs package)
139 (((labels packages) ...)
141 (list guile-gcrypt guile-sqlite3)))
143 (define* (expression->derivation-in-linux-vm name exp
145 (system (%current-system))
153 %linux-vm-file-systems)
155 (single-file-output? #f)
156 (make-disk-image? #f)
157 (references-graphs #f)
159 (disk-image-format "qcow2")
160 (disk-image-size 'guess))
161 "Evaluate EXP in a QEMU virtual machine running LINUX with INITRD (a
162 derivation). The virtual machine runs with MEMORY-SIZE MiB of memory. In the
163 virtual machine, EXP has access to FILE-SYSTEMS, which, by default, includes a
164 9p share of the store, the '/xchg' where EXP should put its output file(s),
165 and a 9p share of /tmp.
167 If SINGLE-FILE-OUTPUT? is true, copy a single file from '/xchg' to OUTPUT.
168 Otherwise, copy the contents of /xchg to a new directory OUTPUT.
170 When MAKE-DISK-IMAGE? is true, then create a QEMU disk image of type
171 DISK-IMAGE-FORMAT (e.g., 'qcow2' or 'raw'), of DISK-IMAGE-SIZE bytes and
172 return it. When DISK-IMAGE-SIZE is 'guess, estimate the image size based
173 based on the size of the closure of REFERENCES-GRAPHS.
175 When REFERENCES-GRAPHS is true, it must be a list of file name/store path
176 pairs, as for `derivation'. The files containing the reference graphs are
177 made available under the /xchg CIFS share."
179 (program-file "builder-in-linux-vm" exp))
182 ;; Invoke USER-BUILDER instead using 'primitive-load'. The reason for
183 ;; this is to allow USER-BUILDER to dlopen stuff by using a full-featured
184 ;; Guile, which it couldn't do using the statically-linked guile used in
185 ;; the initrd. See example at
186 ;; <https://lists.gnu.org/archive/html/guix-devel/2017-10/msg00233.html>.
187 (program-file "linux-vm-loader"
188 ;; When USER-BUILDER succeeds, reboot (indicating a
189 ;; success), otherwise die, which causes a kernel panic
190 ;; ("Attempted to kill init!").
191 #~(if (zero? (system* #$user-builder))
195 (let ((initrd (or initrd
196 (base-initrd file-systems
197 #:on-error 'backtrace
199 #:linux-modules %base-initrd-modules
200 #:qemu-networking? #t))))
203 ;; Code that launches the VM that evaluates EXP.
204 (with-extensions gcrypt-sqlite3&co
205 (with-imported-modules `(,@(source-module-closure
208 #:select? not-config?)
210 ;; For consumption by (gnu store database).
211 ((guix config) => ,(make-config.scm)))
213 (use-modules (guix build utils)
216 (let* ((inputs '#$(list qemu (canonical-package coreutils)))
217 (linux (string-append #$linux "/"
218 #$(system-linux-image-file-name)))
221 (graphs '#$(match references-graphs
222 (((graph-files . _) ...) graph-files)
224 (size #$(if (eq? 'guess disk-image-size)
225 #~(+ (* 70 (expt 2 20)) ;ESP
226 (estimated-partition-size graphs))
229 (set-path-environment-variable "PATH" '("bin") inputs)
231 (load-in-linux-vm loader
233 #:linux linux #:initrd initrd
234 #:memory-size #$memory-size
235 #:make-disk-image? #$make-disk-image?
236 #:single-file-output? #$single-file-output?
237 ;; FIXME: ‘target-arm32?’ may not operate on
238 ;; the right system/target values. Rewrite
239 ;; using ‘let-system’ when available.
240 #:target-arm32? #$(target-arm32?)
241 #:disk-image-format #$disk-image-format
242 #:disk-image-size size
243 #:references-graphs graphs))))))
245 (gexp->derivation name builder
246 ;; TODO: Require the "kvm" feature.
249 #:guile-for-build guile-for-build
250 #:references-graphs references-graphs)))
252 (define* (iso9660-image #:key
253 (name "iso9660-image")
256 (system (%current-system))
263 "Return a bootable, stand-alone iso9660 image.
265 INPUTS is a list of inputs (as for packages)."
267 (and register-closures?
268 (local-file (search-path %load-path
269 "guix/store/schema.sql"))))
271 (expression->derivation-in-linux-vm
273 (with-extensions gcrypt-sqlite3&co
274 (with-imported-modules `(,@(source-module-closure '((gnu build vm)
275 (guix store database)
277 #:select? not-config?)
278 ((guix config) => ,(make-config.scm)))
280 (use-modules (gnu build vm)
281 (guix store database)
284 (sql-schema #$schema)
287 '#$(append (list qemu parted e2fsprogs dosfstools xorriso)
288 (map canonical-package
289 (list sed grep coreutils findutils gawk))))
292 (graphs '#$(match inputs
295 ;; This variable is unused but allows us to add INPUTS-TO-COPY
298 '#$(map (match-lambda
300 ((name thing output) `(,thing ,output)))
303 (set-path-environment-variable "PATH" '("bin" "sbin") inputs)
304 (make-iso9660-image #$(bootloader-package bootloader)
308 #:register-closures? #$register-closures?
310 #:volume-id #$file-system-label
311 #:volume-uuid #$(and=> file-system-uuid
312 uuid-bytevector))))))
315 ;; Keep a local file system for /tmp so that we can populate it directly as
316 ;; root and have files owned by root. See <https://bugs.gnu.org/31752>.
317 #:file-systems (remove (lambda (file-system)
318 (string=? (file-system-mount-point file-system)
320 %linux-vm-file-systems)
322 #:make-disk-image? #f
323 #:single-file-output? #t
324 #:references-graphs inputs))
326 (define* (qemu-image #:key
328 (system (%current-system))
330 (disk-image-size 'guess)
331 (disk-image-format "qcow2")
332 (file-system-type "ext4")
338 (register-closures? #t)
341 "Return a bootable, stand-alone QEMU image of type DISK-IMAGE-FORMAT (e.g.,
342 'qcow2' or 'raw'), with a root partition of type FILE-SYSTEM-TYPE.
343 Optionally, FILE-SYSTEM-LABEL can be specified as the volume name for the root
344 partition; likewise FILE-SYSTEM-UUID, if true, specifies the UUID of the root
345 partition (a UUID object).
347 The returned image is a full disk image that runs OS-DERIVATION,
348 with a GRUB installation that uses GRUB-CONFIGURATION as its configuration
349 file (GRUB-CONFIGURATION must be the name of a file in the VM.)
351 INPUTS is a list of inputs (as for packages). When COPY-INPUTS? is true, copy
352 all of INPUTS into the image being built. When REGISTER-CLOSURES? is true,
353 register INPUTS in the store database of the image so that Guix can be used in
356 (and register-closures?
357 (local-file (search-path %load-path
358 "guix/store/schema.sql"))))
360 (expression->derivation-in-linux-vm
362 (with-extensions gcrypt-sqlite3&co
363 (with-imported-modules `(,@(source-module-closure '((gnu build vm)
364 (gnu build bootloader)
365 (guix store database)
367 #:select? not-config?)
368 ((guix config) => ,(make-config.scm)))
370 (use-modules (gnu build bootloader)
372 (guix store database)
375 (ice-9 binary-ports))
377 (sql-schema #$schema)
380 '#$(append (list qemu parted e2fsprogs dosfstools)
381 (map canonical-package
382 (list sed grep coreutils findutils gawk))))
384 ;; This variable is unused but allows us to add INPUTS-TO-COPY
387 '#$(map (match-lambda
389 ((name thing output) `(,thing ,output)))
392 (set-path-environment-variable "PATH" '("bin" "sbin") inputs)
394 (let* ((graphs '#$(match inputs
397 (initialize (root-partition-initializer
399 #:copy-closures? #$copy-inputs?
400 #:register-closures? #$register-closures?
401 #:system-directory #$os
403 ;; Disable deduplication to speed things up,
404 ;; and because it doesn't help much for a
405 ;; single system generation.
407 (root-size #$(if (eq? 'guess disk-image-size)
409 ;; Minimum 20 MiB root size
411 (estimated-partition-size
412 (map (cut string-append "/xchg/" <>)
415 (* 50 (expt 2 20)))))
420 (label #$file-system-label)
421 (uuid #$(and=> file-system-uuid
423 (file-system #$file-system-type)
425 (initializer initialize)))
426 ;; Append a small EFI System Partition for use with UEFI
427 ;; bootloaders if we are not targeting ARM because UEFI
428 ;; support in U-Boot is experimental.
430 ;; FIXME: ‘target-arm32?’ may be not operate on the right
431 ;; system/target values. Rewrite using ‘let-system’ when
433 (if #$(target-arm32?)
436 ;; The standalone grub image is about 10MiB, but
437 ;; leave some room for custom or multiple images.
438 (size (* 40 (expt 2 20)))
439 (label "GNU-ESP") ;cosmetic only
440 ;; Use "vfat" here since this property is used
441 ;; when mounting. The actual FAT-ness is based
442 ;; on file system size (16 in this case).
445 (initialize-hard-disk "/dev/vda"
446 #:partitions partitions
447 #:grub-efi #$grub-efi
449 #$(bootloader-package bootloader)
450 #:bootcfg #$bootcfg-drv
452 #$(bootloader-configuration-file bootloader)
453 #:bootloader-installer
454 #$(bootloader-installer bootloader)))))))
456 #:make-disk-image? #t
457 #:disk-image-size disk-image-size
458 #:disk-image-format disk-image-format
459 #:references-graphs inputs))
461 (define* (system-docker-image os
463 (name "guixsd-docker-image")
465 "Build a docker image. OS is the desired <operating-system>. NAME is the
466 base name to use for the output file. When REGISTER-CLOSURES? is not #f,
467 register the closure of OS with Guix in the resulting Docker image. This only
468 makes sense when you want to build a Guix System Docker image that has Guix
469 installed inside of it. If you don't need Guix (e.g., your Docker
470 image just contains a web server that is started by the Shepherd), then you
471 should set REGISTER-CLOSURES? to #f."
473 (and register-closures?
474 (local-file (search-path %load-path
475 "guix/store/schema.sql"))))
477 (let ((os (containerized-operating-system os '()))
478 (name (string-append name ".tar.gz"))
479 (graph "system-graph"))
481 (with-extensions (cons guile-json ;for (guix docker)
482 gcrypt-sqlite3&co) ;for (guix store database)
483 (with-imported-modules `(,@(source-module-closure
485 (guix store database)
487 (guix build store-copy)
489 #:select? not-config?)
490 ((guix config) => ,(make-config.scm)))
492 (use-modules (guix docker)
496 (guix build store-copy)
497 (guix store database))
499 ;; Set the SQL schema location.
500 (sql-schema #$schema)
502 (let* (;; This initializer requires elevated privileges that are
503 ;; not normally available in the build environment (e.g.,
504 ;; it needs to create device nodes). In order to obtain
505 ;; such privileges, we run it as root in a VM.
506 (initialize (root-partition-initializer
507 #:closures '(#$graph)
508 #:register-closures? #$register-closures?
509 #:system-directory #$os
510 ;; De-duplication would fail due to
511 ;; cross-device link errors, so don't do it.
513 ;; Even as root in a VM, the initializer would fail due to
514 ;; lack of privileges if we use a root-directory that is on
515 ;; a file system that is shared with the host (e.g., /tmp).
516 (root-directory "/guixsd-system-root"))
517 (set-path-environment-variable "PATH" '("bin" "sbin") '(#+tar))
518 (mkdir root-directory)
519 (initialize root-directory)
521 (string-append "/xchg/" #$name) ;; The output file.
522 (cons* root-directory
524 (call-with-input-file
525 (string-append "/xchg/" #$graph)
526 read-reference-graph)))
528 #:compressor '(#+(file-append gzip "/bin/gzip") "-9n")
529 #:creation-time (make-time time-utc 0 1)
530 #:transformations `((,root-directory -> "")))
532 ;; Make sure the tarball is fully written before rebooting.
534 (expression->derivation-in-linux-vm
536 #:make-disk-image? #f
537 #:single-file-output? #t
538 #:references-graphs `((,graph ,os)))))
542 ;;; VM and disk images.
545 (define* (operating-system-uuid os #:optional (type 'dce))
546 "Compute UUID object with a deterministic \"UUID\" for OS, of the given
547 TYPE (one of 'iso9660 or 'dce). Return a UUID object."
548 ;; Note: For this to be deterministic, we must not hash things that contains
549 ;; (directly or indirectly) procedures, for example. That rules out
550 ;; anything that contains gexps, thunk or delayed record fields, etc.
553 (compose service-type-name service-kind))
555 (define (file-system-digest fs)
556 ;; Return a hashable digest that does not contain 'dependencies' since
557 ;; this field can contain procedures.
558 (let ((device (file-system-device fs)))
559 (list (file-system-mount-point fs)
560 (file-system-type fs)
561 (cond ((file-system-label? device)
562 (file-system-label->string device))
564 (uuid->string device))
568 (file-system-options fs))))
570 (if (eq? type 'iso9660)
571 (let ((pad (compose (cut string-pad <> 2 #\0)
573 (h (hash (map service-name (operating-system-services os))
576 (string->iso9660-uuid
577 (string-append "1970-01-01-"
578 (pad (hash (operating-system-host-name os) 24)) "-"
579 (pad (quotient h 60)) "-"
580 (pad (modulo h 60)) "-"
581 (pad (hash (map file-system-digest
582 (operating-system-file-systems os))
586 (uint-list->bytevector
587 (list (hash file-system-type
589 (hash (operating-system-host-name os)
591 (hash (map service-name (operating-system-services os))
593 (hash (map file-system-digest (operating-system-file-systems os))
599 (define* (system-disk-image os
602 (file-system-type "ext4")
603 (disk-image-size (* 900 (expt 2 20)))
605 "Return the derivation of a disk image of DISK-IMAGE-SIZE bytes of the
606 system described by OS. Said image can be copied on a USB stick as is. When
607 VOLATILE? is true, the root file system is made volatile; this is useful
608 to USB sticks meant to be read-only."
609 (define normalize-label
610 ;; ISO labels are all-caps (case-insensitive), but since
611 ;; 'find-partition-by-label' is case-sensitive, make it all-caps here.
612 (if (string=? "iso9660" file-system-type)
617 ;; Volume name of the root file system.
618 (normalize-label "Guix_image"))
621 ;; UUID of the root file system, computed in a deterministic fashion.
622 ;; This is what we use to locate the root file system so it has to be
623 ;; different from the user's own file system UUIDs.
624 (operating-system-uuid os
625 (if (string=? file-system-type "iso9660")
629 (define file-systems-to-keep
631 (string=? (file-system-mount-point fs) "/"))
632 (operating-system-file-systems os)))
634 (let* ((os (operating-system (inherit os)
635 ;; Since this is meant to be used on real hardware, don't
636 ;; install QEMU networking or anything like that. Assume USB
637 ;; mass storage devices (usb-storage.ko) are available.
638 (initrd (lambda (file-systems . rest)
639 (apply (operating-system-initrd os)
644 (bootloader (if (string=? "iso9660" file-system-type)
645 (bootloader-configuration
646 (inherit (operating-system-bootloader os))
647 (bootloader grub-mkrescue-bootloader))
648 (operating-system-bootloader os)))
650 ;; Force our own root file system.
651 (file-systems (cons (file-system
654 (type file-system-type))
655 file-systems-to-keep))))
656 (bootcfg (operating-system-bootcfg os)))
657 (if (string=? "iso9660" file-system-type)
658 (iso9660-image #:name name
659 #:file-system-label root-label
660 #:file-system-uuid root-uuid
662 #:register-closures? #t
663 #:bootcfg-drv bootcfg
664 #:bootloader (bootloader-configuration-bootloader
665 (operating-system-bootloader os))
666 #:inputs `(("system" ,os)
667 ("bootcfg" ,bootcfg)))
668 (qemu-image #:name name
670 #:bootcfg-drv bootcfg
671 #:bootloader (bootloader-configuration-bootloader
672 (operating-system-bootloader os))
673 #:disk-image-size disk-image-size
674 #:disk-image-format "raw"
675 #:file-system-type file-system-type
676 #:file-system-label root-label
677 #:file-system-uuid root-uuid
679 #:register-closures? #t
680 #:inputs `(("system" ,os)
681 ("bootcfg" ,bootcfg))))))
683 (define* (system-qemu-image os
685 (file-system-type "ext4")
686 (disk-image-size (* 900 (expt 2 20))))
687 "Return the derivation of a freestanding QEMU image of DISK-IMAGE-SIZE bytes
688 of the GNU system as described by OS."
689 (define file-systems-to-keep
690 ;; Keep only file systems other than root and not normally bound to real
693 (let ((target (file-system-mount-point fs))
694 (source (file-system-device fs)))
695 (or (string=? target "/")
696 (string-prefix? "/dev/" source))))
697 (operating-system-file-systems os)))
700 ;; UUID of the root file system.
701 (operating-system-uuid os
702 (if (string=? file-system-type "iso9660")
707 (let* ((os (operating-system (inherit os)
708 ;; Assume we have an initrd with the whole QEMU shebang.
710 ;; Force our own root file system. Refer to it by UUID so that
711 ;; it works regardless of how the image is used ("qemu -hda",
713 (file-systems (cons (file-system
716 (type file-system-type))
717 file-systems-to-keep))))
718 (bootcfg (operating-system-bootcfg os)))
720 #:bootcfg-drv bootcfg
721 #:bootloader (bootloader-configuration-bootloader
722 (operating-system-bootloader os))
723 #:disk-image-size disk-image-size
724 #:file-system-type file-system-type
725 #:file-system-uuid root-uuid
726 #:inputs `(("system" ,os)
727 ("bootcfg" ,bootcfg))
732 ;;; VMs that share file systems with the host.
735 (define (file-system->mount-tag fs)
736 "Return a 9p mount tag for host file system FS."
737 ;; QEMU mount tags must be ASCII, at most 31-byte long, cannot contain
738 ;; slashes, and cannot start with '_'. Compute an identifier that
739 ;; corresponds to the rules.
741 (string-drop (bytevector->base32-string
742 (sha1 (string->utf8 fs)))
745 (define (mapping->file-system mapping)
746 "Return a 9p file system that realizes MAPPING."
748 (($ <file-system-mapping> source target writable?)
751 (device (file-system->mount-tag source))
753 (flags (if writable? '() '(read-only)))
754 (options "trans=virtio,cache=loose")
756 (create-mount-point? #t)))))
758 (define* (virtualized-operating-system os mappings #:optional (full-boot? #f))
759 "Return an operating system based on OS suitable for use in a virtualized
760 environment with the store shared with the host. MAPPINGS is a list of
761 <file-system-mapping> to realize in the virtualized OS."
762 (define user-file-systems
763 ;; Remove file systems that conflict with those added below, or that are
764 ;; normally bound to real devices.
766 (let ((target (file-system-mount-point fs))
767 (source (file-system-device fs)))
768 (or (string=? target (%store-prefix))
769 (string=? target "/")
770 (and (string? source)
771 (string-prefix? "/dev/" source))
773 ;; Labels and UUIDs are necessarily invalid in the VM.
774 (and (file-system-mount? fs)
775 (or (file-system-label? source)
777 (operating-system-file-systems os)))
779 (define virtual-file-systems
785 (append (map mapping->file-system mappings)
788 (operating-system (inherit os)
790 ;; XXX: Until we run QEMU with UEFI support (with the OVMF firmware),
791 ;; force the traditional i386/BIOS method.
792 ;; See <https://bugs.gnu.org/28768>.
793 (bootloader (bootloader-configuration
794 (inherit (operating-system-bootloader os))
795 (bootloader grub-bootloader)
796 (target "/dev/vda")))
798 (initrd (lambda (file-systems . rest)
799 (apply (operating-system-initrd os)
807 ;; XXX: When FULL-BOOT? is true, do not add a 9p mount for /gnu/store
808 ;; since that would lead the bootloader config to look for the kernel and
810 (file-systems (if full-boot?
814 (inherit (mapping->file-system %store-mapping))
815 (needed-for-boot? #t))
816 virtual-file-systems)))))
818 (define* (system-qemu-image/shared-store
822 (disk-image-size (* (if full-boot? 500 30) (expt 2 20))))
823 "Return a derivation that builds a QEMU image of OS that shares its store
826 When FULL-BOOT? is true, return an image that does a complete boot sequence,
827 bootloaded included; thus, make a disk image that contains everything the
828 bootloader refers to: OS kernel, initrd, bootloader data, etc."
830 ;; Use a fixed UUID to improve determinism.
831 (operating-system-uuid os 'dce))
834 (operating-system-bootcfg os))
836 ;; XXX: When FULL-BOOT? is true, we end up creating an image that contains
837 ;; BOOTCFG and all its dependencies, including the output of OS.
838 ;; This is more than needed (we only need the kernel, initrd, GRUB for its
839 ;; font, and the background image), but it's hard to filter that.
841 #:bootcfg-drv bootcfg
842 #:bootloader (bootloader-configuration-bootloader
843 (operating-system-bootloader os))
844 #:disk-image-size disk-image-size
845 #:file-system-uuid root-uuid
846 #:inputs (if full-boot?
847 `(("bootcfg" ,bootcfg))
850 ;; XXX: Passing #t here is too slow, so let it off by default.
851 #:register-closures? #f
852 #:copy-inputs? full-boot?))
854 (define* (common-qemu-options image shared-fs)
855 "Return the a string-value gexp with the common QEMU options to boot IMAGE,
856 with '-virtfs' options for the host file systems listed in SHARED-FS."
858 (define (virtfs-option fs)
859 #~(format #f "-virtfs local,path=~s,security_model=none,mount_tag=~s"
860 #$fs #$(file-system->mount-tag fs)))
862 #~(;; Only enable kvm if we see /dev/kvm exists.
863 ;; This allows users without hardware virtualization to still use these
865 #$@(if (file-exists? "/dev/kvm")
870 "-net nic,model=virtio"
871 "-object" "rng-random,filename=/dev/urandom,id=guixsd-vm-rng"
872 "-device" "virtio-rng-pci,rng=guixsd-vm-rng"
874 #$@(map virtfs-option shared-fs)
876 (format #f "-drive file=~a,if=virtio,cache=writeback,werror=report,readonly"
879 (define* (system-qemu-image/shared-store-script os
887 (* (if full-boot? 500 70)
890 "Return a derivation that builds a script to run a virtual machine image of
891 OS that shares its store with the host. The virtual machine runs with
892 MEMORY-SIZE MiB of memory.
894 MAPPINGS is a list of <file-system-mapping> specifying mapping of host file
895 systems into the guest.
897 When FULL-BOOT? is true, the returned script runs everything starting from the
898 bootloader; otherwise it directly starts the operating system kernel. The
899 DISK-IMAGE-SIZE parameter specifies the size in bytes of the root disk image;
900 it is mostly useful when FULL-BOOT? is true."
901 (mlet* %store-monad ((os -> (virtualized-operating-system os mappings full-boot?))
902 (image (system-qemu-image/shared-store
904 #:full-boot? full-boot?
905 #:disk-image-size disk-image-size)))
906 (define kernel-arguments
907 #~(list #$@(if graphic? #~() #~("console=ttyS0"))
908 #+@(operating-system-kernel-arguments os "/dev/vda1")))
911 #~(list (string-append #$qemu "/bin/" #$(qemu-command (%current-system)))
914 #~("-kernel" #$(operating-system-kernel-file os)
915 "-initrd" #$(file-append os "/initrd")
916 (format #f "-append ~s"
917 (string-join #$kernel-arguments " "))))
918 #$@(common-qemu-options image
919 (map file-system-mapping-source
920 (cons %store-mapping mappings)))
921 "-m " (number->string #$memory-size)
925 #~(call-with-output-file #$output
927 (format port "#!~a~% exec ~a \"$@\"~%"
928 #$(file-append bash "/bin/sh")
929 (string-join #$qemu-exec " "))
930 (chmod port #o555))))
932 (gexp->derivation "run-vm.sh" builder)))
936 ;;; High-level abstraction.
939 (define-record-type* <virtual-machine> %virtual-machine
942 (operating-system virtual-machine-operating-system) ;<operating-system>
943 (qemu virtual-machine-qemu ;<package>
945 (graphic? virtual-machine-graphic? ;Boolean
947 (memory-size virtual-machine-memory-size ;integer (MiB)
949 (disk-image-size virtual-machine-disk-image-size ;integer (bytes)
951 (port-forwardings virtual-machine-port-forwardings ;list of integer pairs
954 (define-syntax virtual-machine
956 "Declare a virtual machine running the specified OS, with the given
959 (%virtual-machine (operating-system os)))
961 (%virtual-machine fields ...))))
963 (define (port-forwardings->qemu-options forwardings)
964 "Return the QEMU option for the given port FORWARDINGS as a string, where
965 FORWARDINGS is a list of host-port/guest-port pairs."
968 ((host-port . guest-port)
969 (string-append "hostfwd=tcp::"
970 (number->string host-port)
971 "-:" (number->string guest-port))))
975 (define-gexp-compiler (virtual-machine-compiler (vm <virtual-machine>)
977 ;; XXX: SYSTEM and TARGET are ignored.
979 (($ <virtual-machine> os qemu graphic? memory-size disk-image-size ())
980 (system-qemu-image/shared-store-script os
983 #:memory-size memory-size
986 (($ <virtual-machine> os qemu graphic? memory-size disk-image-size
989 `("-net" ,(string-append
991 (port-forwardings->qemu-options forwardings)))))
992 (system-qemu-image/shared-store-script os
995 #:memory-size memory-size
998 #:options options)))))
1000 ;;; vm.scm ends here