1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2013, 2014, 2015, 2016 Ludovic Courtès <ludo@gnu.org>
3 ;;; Copyright © 2016 Christopher Allan Webber <cwebber@dustycloud.org>
4 ;;; Copyright © 2016 Leo Famulari <leo@famulari.name>
6 ;;; This file is part of GNU Guix.
8 ;;; GNU Guix is free software; you can redistribute it and/or modify it
9 ;;; under the terms of the GNU General Public License as published by
10 ;;; the Free Software Foundation; either version 3 of the License, or (at
11 ;;; your option) any later version.
13 ;;; GNU Guix is distributed in the hope that it will be useful, but
14 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;;; GNU General Public License for more details.
18 ;;; You should have received a copy of the GNU General Public License
19 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
21 (define-module (gnu system vm)
22 #:use-module (guix config)
23 #:use-module (guix store)
24 #:use-module (guix gexp)
25 #:use-module (guix derivations)
26 #:use-module (guix packages)
27 #:use-module (guix monads)
28 #:use-module (guix records)
29 #:use-module (guix modules)
31 #:use-module ((gnu build vm)
32 #:select (qemu-command))
33 #:use-module (gnu packages base)
34 #:use-module (gnu packages guile)
35 #:use-module (gnu packages gawk)
36 #:use-module (gnu packages bash)
37 #:use-module (gnu packages less)
38 #:use-module (gnu packages qemu)
39 #:use-module (gnu packages disk)
40 #:use-module (gnu packages zile)
41 #:use-module (gnu packages grub)
42 #:use-module (gnu packages linux)
43 #:use-module (gnu packages package-management)
44 #:use-module ((gnu packages make-bootstrap)
45 #:select (%guile-static-stripped))
46 #:use-module (gnu packages admin)
48 #:use-module (gnu system shadow)
49 #:use-module (gnu system pam)
50 #:use-module (gnu system linux-initrd)
51 #:use-module (gnu system grub)
52 #:use-module (gnu system file-systems)
53 #:use-module (gnu system)
54 #:use-module (gnu services)
56 #:use-module (srfi srfi-1)
57 #:use-module (srfi srfi-26)
58 #:use-module (ice-9 match)
60 #:export (expression->derivation-in-linux-vm
62 virtualized-operating-system
65 system-qemu-image/shared-store
66 system-qemu-image/shared-store-script
72 ;;; Tools to evaluate build expressions within virtual machines.
76 (define %linux-vm-file-systems
77 ;; File systems mounted for 'derivation-in-linux-vm'. The store and /xchg
78 ;; directory are shared with the host over 9p.
80 (mount-point (%store-prefix))
84 (options "trans=virtio")
91 (options "trans=virtio")
94 (define* (expression->derivation-in-linux-vm name exp
96 (system (%current-system))
104 (make-disk-image? #f)
105 (references-graphs #f)
107 (disk-image-format "qcow2")
109 (* 100 (expt 2 20))))
110 "Evaluate EXP in a QEMU virtual machine running LINUX with INITRD (a
111 derivation). In the virtual machine, EXP has access to all its inputs from the
112 store; it should put its output files in the `/xchg' directory, which is
113 copied to the derivation's output when the VM terminates. The virtual machine
114 runs with MEMORY-SIZE MiB of memory.
116 When MAKE-DISK-IMAGE? is true, then create a QEMU disk image of type
117 DISK-IMAGE-FORMAT (e.g., 'qcow2' or 'raw'), of DISK-IMAGE-SIZE bytes and
120 When REFERENCES-GRAPHS is true, it must be a list of file name/store path
121 pairs, as for `derivation'. The files containing the reference graphs are
122 made available under the /xchg CIFS share."
124 ((user-builder (gexp->file "builder-in-linux-vm" exp))
125 (loader (gexp->file "linux-vm-loader"
126 #~(primitive-load #$user-builder)))
127 (coreutils -> (canonical-package coreutils))
128 (initrd (if initrd ; use the default initrd?
130 (base-initrd %linux-vm-file-systems
133 #:qemu-networking? #t))))
136 ;; Code that launches the VM that evaluates EXP.
137 (with-imported-modules (source-module-closure '((guix build utils)
140 (use-modules (guix build utils)
143 (let ((inputs '#$(list qemu coreutils))
144 (linux (string-append #$linux "/bzImage"))
145 (initrd (string-append #$initrd "/initrd"))
147 (graphs '#$(match references-graphs
148 (((graph-files . _) ...) graph-files)
151 (set-path-environment-variable "PATH" '("bin") inputs)
153 (load-in-linux-vm loader
155 #:linux linux #:initrd initrd
156 #:memory-size #$memory-size
157 #:make-disk-image? #$make-disk-image?
158 #:disk-image-format #$disk-image-format
159 #:disk-image-size #$disk-image-size
160 #:references-graphs graphs)))))
162 (gexp->derivation name builder
163 ;; TODO: Require the "kvm" feature.
166 #:guile-for-build guile-for-build
167 #:references-graphs references-graphs)))
169 (define* (qemu-image #:key
171 (system (%current-system))
173 (disk-image-size (* 100 (expt 2 20)))
174 (disk-image-format "qcow2")
175 (file-system-type "ext4")
179 (register-closures? #t)
182 "Return a bootable, stand-alone QEMU image of type DISK-IMAGE-FORMAT (e.g.,
183 'qcow2' or 'raw'), with a root partition of type FILE-SYSTEM-TYPE.
184 Optionally, FILE-SYSTEM-LABEL can be specified as the volume name for the root
185 partition. The returned image is a full disk image that runs OS-DERIVATION,
186 with a GRUB installation that uses GRUB-CONFIGURATION as its configuration
187 file (GRUB-CONFIGURATION must be the name of a file in the VM.)
189 INPUTS is a list of inputs (as for packages). When COPY-INPUTS? is true, copy
190 all of INPUTS into the image being built. When REGISTER-CLOSURES? is true,
191 register INPUTS in the store database of the image so that Guix can be used in
193 (expression->derivation-in-linux-vm
195 (with-imported-modules (source-module-closure '((gnu build vm)
198 (use-modules (gnu build vm)
202 '#$(append (list qemu parted grub e2fsprogs)
203 (map canonical-package
204 (list sed grep coreutils findutils gawk))
205 (if register-closures? (list guix) '())))
207 ;; This variable is unused but allows us to add INPUTS-TO-COPY
210 '#$(map (match-lambda
212 ((name thing output) `(,thing ,output)))
215 (set-path-environment-variable "PATH" '("bin" "sbin") inputs)
217 (let* ((graphs '#$(match inputs
220 (initialize (root-partition-initializer
222 #:copy-closures? #$copy-inputs?
223 #:register-closures? #$register-closures?
224 #:system-directory #$os-derivation))
225 (partitions (list (partition
226 (size #$(- disk-image-size
228 (label #$file-system-label)
229 (file-system #$file-system-type)
231 (initializer initialize)))))
232 (initialize-hard-disk "/dev/vda"
233 #:partitions partitions
234 #:grub.cfg #$grub-configuration)
237 #:make-disk-image? #t
238 #:disk-image-size disk-image-size
239 #:disk-image-format disk-image-format
240 #:references-graphs inputs))
244 ;;; VM and disk images.
247 (define* (system-disk-image os
250 (file-system-type "ext4")
251 (disk-image-size (* 900 (expt 2 20)))
253 "Return the derivation of a disk image of DISK-IMAGE-SIZE bytes of the
254 system described by OS. Said image can be copied on a USB stick as is. When
255 VOLATILE? is true, the root file system is made volatile; this is useful
256 to USB sticks meant to be read-only."
258 ;; Volume name of the root file system. Since we don't know which device
259 ;; will hold it, we use the volume name to find it (using the UUID would
260 ;; be even better, but somewhat less convenient.)
263 (define file-systems-to-keep
265 (string=? (file-system-mount-point fs) "/"))
266 (operating-system-file-systems os)))
268 (let ((os (operating-system (inherit os)
269 ;; Since this is meant to be used on real hardware, don't
270 ;; install QEMU networking or anything like that. Assume USB
271 ;; mass storage devices (usb-storage.ko) are available.
272 (initrd (lambda (file-systems . rest)
273 (apply base-initrd file-systems
277 ;; Force our own root file system.
278 (file-systems (cons (file-system
282 (type file-system-type))
283 file-systems-to-keep)))))
285 (mlet* %store-monad ((os-drv (operating-system-derivation os))
286 (grub.cfg (operating-system-grub.cfg os)))
287 (qemu-image #:name name
288 #:os-derivation os-drv
289 #:grub-configuration grub.cfg
290 #:disk-image-size disk-image-size
291 #:disk-image-format "raw"
292 #:file-system-type file-system-type
293 #:file-system-label root-label
295 #:register-closures? #t
296 #:inputs `(("system" ,os-drv)
297 ("grub.cfg" ,grub.cfg))))))
299 (define* (system-qemu-image os
301 (file-system-type "ext4")
302 (disk-image-size (* 900 (expt 2 20))))
303 "Return the derivation of a freestanding QEMU image of DISK-IMAGE-SIZE bytes
304 of the GNU system as described by OS."
305 (define file-systems-to-keep
306 ;; Keep only file systems other than root and not normally bound to real
309 (let ((target (file-system-mount-point fs))
310 (source (file-system-device fs)))
311 (or (string=? target "/")
312 (string-prefix? "/dev/" source))))
313 (operating-system-file-systems os)))
315 (let ((os (operating-system (inherit os)
316 ;; Use an initrd with the whole QEMU shebang.
317 (initrd (lambda (file-systems . rest)
318 (apply base-initrd file-systems
320 #:qemu-networking? #t
323 ;; Force our own root file system.
324 (file-systems (cons (file-system
327 (type file-system-type))
328 file-systems-to-keep)))))
330 ((os-drv (operating-system-derivation os))
331 (grub.cfg (operating-system-grub.cfg os)))
332 (qemu-image #:os-derivation os-drv
333 #:grub-configuration grub.cfg
334 #:disk-image-size disk-image-size
335 #:file-system-type file-system-type
336 #:inputs `(("system" ,os-drv)
337 ("grub.cfg" ,grub.cfg))
338 #:copy-inputs? #t))))
342 ;;; VMs that share file systems with the host.
345 (define (file-system->mount-tag fs)
346 "Return a 9p mount tag for host file system FS."
347 ;; QEMU mount tags cannot contain slashes and cannot start with '_'.
348 ;; Compute an identifier that corresponds to the rules.
350 (string-map (match-lambda
355 (define (mapping->file-system mapping)
356 "Return a 9p file system that realizes MAPPING."
358 (($ <file-system-mapping> source target writable?)
361 (device (file-system->mount-tag source))
363 (flags (if writable? '() '(read-only)))
364 (options (string-append "trans=virtio"))
366 (create-mount-point? #t)))))
368 (define (virtualized-operating-system os mappings)
369 "Return an operating system based on OS suitable for use in a virtualized
370 environment with the store shared with the host. MAPPINGS is a list of
371 <file-system-mapping> to realize in the virtualized OS."
372 (define user-file-systems
373 ;; Remove file systems that conflict with those added below, or that are
374 ;; normally bound to real devices.
376 (let ((target (file-system-mount-point fs))
377 (source (file-system-device fs)))
378 (or (string=? target (%store-prefix))
379 (string=? target "/")
380 (and (eq? 'device (file-system-title fs))
381 (string-prefix? "/dev/" source)))))
382 (operating-system-file-systems os)))
384 (operating-system (inherit os)
385 (initrd (lambda (file-systems . rest)
386 (apply base-initrd file-systems
389 #:qemu-networking? #t
395 (file-systems (cons* (file-system
400 (file-system (inherit
401 (mapping->file-system %store-mapping))
402 (needed-for-boot? #t))
404 (append (map mapping->file-system mappings)
405 user-file-systems)))))
407 (define* (system-qemu-image/shared-store
411 (disk-image-size (* (if full-boot? 500 30) (expt 2 20))))
412 "Return a derivation that builds a QEMU image of OS that shares its store
415 When FULL-BOOT? is true, return an image that does a complete boot sequence,
416 bootloaded included; thus, make a disk image that contains everything the
417 bootloader refers to: OS kernel, initrd, bootloader data, etc."
418 (mlet* %store-monad ((os-drv (operating-system-derivation os))
419 (grub.cfg (operating-system-grub.cfg os)))
420 ;; XXX: When FULL-BOOT? is true, we end up creating an image that contains
421 ;; GRUB.CFG and all its dependencies, including the output of OS-DRV.
422 ;; This is more than needed (we only need the kernel, initrd, GRUB for its
423 ;; font, and the background image), but it's hard to filter that.
424 (qemu-image #:os-derivation os-drv
425 #:grub-configuration grub.cfg
426 #:disk-image-size disk-image-size
427 #:inputs (if full-boot?
428 `(("grub.cfg" ,grub.cfg))
431 ;; XXX: Passing #t here is too slow, so let it off by default.
432 #:register-closures? #f
433 #:copy-inputs? full-boot?)))
435 (define* (common-qemu-options image shared-fs)
436 "Return the a string-value gexp with the common QEMU options to boot IMAGE,
437 with '-virtfs' options for the host file systems listed in SHARED-FS."
438 (define (virtfs-option fs)
439 #~(string-append "-virtfs local,path=\"" #$fs
440 "\",security_model=none,mount_tag=\""
441 #$(file-system->mount-tag fs)
445 ;; Only enable kvm if we see /dev/kvm exists.
446 ;; This allows users without hardware virtualization to still use these
448 #$(if (file-exists? "/dev/kvm")
451 " -no-reboot -net nic,model=virtio \
452 " #$@(map virtfs-option shared-fs) " \
455 -drive file=" #$image
456 ",if=virtio,cache=writeback,werror=report,readonly \
459 (define* (system-qemu-image/shared-store-script os
466 (* (if full-boot? 500 30)
468 "Return a derivation that builds a script to run a virtual machine image of
469 OS that shares its store with the host.
471 MAPPINGS is a list of <file-system-mapping> specifying mapping of host file
472 systems into the guest.
474 When FULL-BOOT? is true, the returned script runs everything starting from the
475 bootloader; otherwise it directly starts the operating system kernel. The
476 DISK-IMAGE-SIZE parameter specifies the size in bytes of the root disk image;
477 it is mostly useful when FULL-BOOT? is true."
478 (mlet* %store-monad ((os -> (virtualized-operating-system os mappings))
479 (os-drv (operating-system-derivation os))
480 (image (system-qemu-image/shared-store
482 #:full-boot? full-boot?
483 #:disk-image-size disk-image-size)))
485 #~(call-with-output-file #$output
488 (string-append "#!" #$bash "/bin/sh
489 exec " #$qemu "/bin/" #$(qemu-command (%current-system))
493 #~(" -kernel " #$(operating-system-kernel os) "/bzImage \
494 -initrd " #$os-drv "/initrd \
495 -append \"" #$(if graphic? "" "console=ttyS0 ")
496 "--system=" #$os-drv " --load=" #$os-drv "/boot --root=/dev/vda1 "
497 (string-join (list #+@(operating-system-kernel-arguments os))) "\" "))
498 #$(common-qemu-options image
499 (map file-system-mapping-source
500 (cons %store-mapping mappings)))
503 (chmod port #o555))))
505 (gexp->derivation "run-vm.sh" builder)))