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
322 ;; Force our own root file system.
323 (file-systems (cons (file-system
326 (type file-system-type))
327 file-systems-to-keep)))))
329 ((os-drv (operating-system-derivation os))
330 (grub.cfg (operating-system-grub.cfg os)))
331 (qemu-image #:os-derivation os-drv
332 #:grub-configuration grub.cfg
333 #:disk-image-size disk-image-size
334 #:file-system-type file-system-type
335 #:inputs `(("system" ,os-drv)
336 ("grub.cfg" ,grub.cfg))
337 #:copy-inputs? #t))))
341 ;;; VMs that share file systems with the host.
344 (define (file-system->mount-tag fs)
345 "Return a 9p mount tag for host file system FS."
346 ;; QEMU mount tags cannot contain slashes and cannot start with '_'.
347 ;; Compute an identifier that corresponds to the rules.
349 (string-map (match-lambda
354 (define (mapping->file-system mapping)
355 "Return a 9p file system that realizes MAPPING."
357 (($ <file-system-mapping> source target writable?)
360 (device (file-system->mount-tag source))
362 (flags (if writable? '() '(read-only)))
363 (options (string-append "trans=virtio"))
365 (create-mount-point? #t)))))
367 (define (virtualized-operating-system os mappings)
368 "Return an operating system based on OS suitable for use in a virtualized
369 environment with the store shared with the host. MAPPINGS is a list of
370 <file-system-mapping> to realize in the virtualized OS."
371 (define user-file-systems
372 ;; Remove file systems that conflict with those added below, or that are
373 ;; normally bound to real devices.
375 (let ((target (file-system-mount-point fs))
376 (source (file-system-device fs)))
377 (or (string=? target (%store-prefix))
378 (string=? target "/")
379 (and (eq? 'device (file-system-title fs))
380 (string-prefix? "/dev/" source)))))
381 (operating-system-file-systems os)))
383 (operating-system (inherit os)
384 (initrd (lambda (file-systems . rest)
385 (apply base-initrd file-systems
393 (file-systems (cons* (file-system
398 (file-system (inherit
399 (mapping->file-system %store-mapping))
400 (needed-for-boot? #t))
402 (append (map mapping->file-system mappings)
403 user-file-systems)))))
405 (define* (system-qemu-image/shared-store
409 (disk-image-size (* (if full-boot? 500 30) (expt 2 20))))
410 "Return a derivation that builds a QEMU image of OS that shares its store
413 When FULL-BOOT? is true, return an image that does a complete boot sequence,
414 bootloaded included; thus, make a disk image that contains everything the
415 bootloader refers to: OS kernel, initrd, bootloader data, etc."
416 (mlet* %store-monad ((os-drv (operating-system-derivation os))
417 (grub.cfg (operating-system-grub.cfg os)))
418 ;; XXX: When FULL-BOOT? is true, we end up creating an image that contains
419 ;; GRUB.CFG and all its dependencies, including the output of OS-DRV.
420 ;; This is more than needed (we only need the kernel, initrd, GRUB for its
421 ;; font, and the background image), but it's hard to filter that.
422 (qemu-image #:os-derivation os-drv
423 #:grub-configuration grub.cfg
424 #:disk-image-size disk-image-size
425 #:inputs (if full-boot?
426 `(("grub.cfg" ,grub.cfg))
429 ;; XXX: Passing #t here is too slow, so let it off by default.
430 #:register-closures? #f
431 #:copy-inputs? full-boot?)))
433 (define* (common-qemu-options image shared-fs)
434 "Return the a string-value gexp with the common QEMU options to boot IMAGE,
435 with '-virtfs' options for the host file systems listed in SHARED-FS."
436 (define (virtfs-option fs)
437 #~(string-append "-virtfs local,path=\"" #$fs
438 "\",security_model=none,mount_tag=\""
439 #$(file-system->mount-tag fs)
443 ;; Only enable kvm if we see /dev/kvm exists.
444 ;; This allows users without hardware virtualization to still use these
446 #$(if (file-exists? "/dev/kvm")
449 " -no-reboot -net nic,model=virtio \
450 " #$@(map virtfs-option shared-fs) " \
452 -drive file=" #$image
453 ",if=virtio,cache=writeback,werror=report,readonly \
456 (define* (system-qemu-image/shared-store-script os
463 (* (if full-boot? 500 30)
465 "Return a derivation that builds a script to run a virtual machine image of
466 OS that shares its store with the host.
468 MAPPINGS is a list of <file-system-mapping> specifying mapping of host file
469 systems into the guest.
471 When FULL-BOOT? is true, the returned script runs everything starting from the
472 bootloader; otherwise it directly starts the operating system kernel. The
473 DISK-IMAGE-SIZE parameter specifies the size in bytes of the root disk image;
474 it is mostly useful when FULL-BOOT? is true."
475 (mlet* %store-monad ((os -> (virtualized-operating-system os mappings))
476 (os-drv (operating-system-derivation os))
477 (image (system-qemu-image/shared-store
479 #:full-boot? full-boot?
480 #:disk-image-size disk-image-size)))
482 #~(call-with-output-file #$output
485 (string-append "#!" #$bash "/bin/sh
486 exec " #$qemu "/bin/" #$(qemu-command (%current-system))
490 #~(" -kernel " #$(operating-system-kernel os) "/bzImage \
491 -initrd " #$os-drv "/initrd \
492 -append \"" #$(if graphic? "" "console=ttyS0 ")
493 "--system=" #$os-drv " --load=" #$os-drv "/boot --root=/dev/vda1 "
494 (string-join (list #+@(operating-system-kernel-arguments os))) "\" "))
495 #$(common-qemu-options image
496 (map file-system-mapping-source
497 (cons %store-mapping mappings)))
500 (chmod port #o555))))
502 (gexp->derivation "run-vm.sh" builder)))