Merge branch 'master' into core-updates
[jackhill/guix/guix.git] / gnu / system / vm.scm
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>
5 ;;;
6 ;;; This file is part of GNU Guix.
7 ;;;
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.
12 ;;;
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.
17 ;;;
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/>.
20
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)
30
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)
47
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)
55
56 #:use-module (srfi srfi-1)
57 #:use-module (srfi srfi-26)
58 #:use-module (ice-9 match)
59
60 #:export (expression->derivation-in-linux-vm
61 qemu-image
62 virtualized-operating-system
63 system-qemu-image
64
65 system-qemu-image/shared-store
66 system-qemu-image/shared-store-script
67 system-disk-image))
68
69 \f
70 ;;; Commentary:
71 ;;;
72 ;;; Tools to evaluate build expressions within virtual machines.
73 ;;;
74 ;;; Code:
75
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.
79 (list (file-system
80 (mount-point (%store-prefix))
81 (device "store")
82 (type "9p")
83 (needed-for-boot? #t)
84 (options "trans=virtio")
85 (check? #f))
86 (file-system
87 (mount-point "/xchg")
88 (device "xchg")
89 (type "9p")
90 (needed-for-boot? #t)
91 (options "trans=virtio")
92 (check? #f))))
93
94 (define* (expression->derivation-in-linux-vm name exp
95 #:key
96 (system (%current-system))
97 (linux linux-libre)
98 initrd
99 (qemu qemu-minimal)
100 (env-vars '())
101 (guile-for-build
102 (%guile-for-build))
103
104 (make-disk-image? #f)
105 (references-graphs #f)
106 (memory-size 256)
107 (disk-image-format "qcow2")
108 (disk-image-size
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.
115
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
118 return it.
119
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."
123 (mlet* %store-monad
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?
129 (return initrd)
130 (base-initrd %linux-vm-file-systems
131 #:linux linux
132 #:virtio? #t
133 #:qemu-networking? #t))))
134
135 (define builder
136 ;; Code that launches the VM that evaluates EXP.
137 (with-imported-modules (source-module-closure '((guix build utils)
138 (gnu build vm)))
139 #~(begin
140 (use-modules (guix build utils)
141 (gnu build vm))
142
143 (let ((inputs '#$(list qemu coreutils))
144 (linux (string-append #$linux "/bzImage"))
145 (initrd (string-append #$initrd "/initrd"))
146 (loader #$loader)
147 (graphs '#$(match references-graphs
148 (((graph-files . _) ...) graph-files)
149 (_ #f))))
150
151 (set-path-environment-variable "PATH" '("bin") inputs)
152
153 (load-in-linux-vm loader
154 #:output #$output
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)))))
161
162 (gexp->derivation name builder
163 ;; TODO: Require the "kvm" feature.
164 #:system system
165 #:env-vars env-vars
166 #:guile-for-build guile-for-build
167 #:references-graphs references-graphs)))
168
169 (define* (qemu-image #:key
170 (name "qemu-image")
171 (system (%current-system))
172 (qemu qemu-minimal)
173 (disk-image-size (* 100 (expt 2 20)))
174 (disk-image-format "qcow2")
175 (file-system-type "ext4")
176 file-system-label
177 os-derivation
178 grub-configuration
179 (register-closures? #t)
180 (inputs '())
181 copy-inputs?)
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.)
188
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
192 the image."
193 (expression->derivation-in-linux-vm
194 name
195 (with-imported-modules (source-module-closure '((gnu build vm)
196 (guix build utils)))
197 #~(begin
198 (use-modules (gnu build vm)
199 (guix build utils))
200
201 (let ((inputs
202 '#$(append (list qemu parted grub e2fsprogs)
203 (map canonical-package
204 (list sed grep coreutils findutils gawk))
205 (if register-closures? (list guix) '())))
206
207 ;; This variable is unused but allows us to add INPUTS-TO-COPY
208 ;; as inputs.
209 (to-register
210 '#$(map (match-lambda
211 ((name thing) thing)
212 ((name thing output) `(,thing ,output)))
213 inputs)))
214
215 (set-path-environment-variable "PATH" '("bin" "sbin") inputs)
216
217 (let* ((graphs '#$(match inputs
218 (((names . _) ...)
219 names)))
220 (initialize (root-partition-initializer
221 #:closures graphs
222 #:copy-closures? #$copy-inputs?
223 #:register-closures? #$register-closures?
224 #:system-directory #$os-derivation))
225 (partitions (list (partition
226 (size #$(- disk-image-size
227 (* 10 (expt 2 20))))
228 (label #$file-system-label)
229 (file-system #$file-system-type)
230 (bootable? #t)
231 (initializer initialize)))))
232 (initialize-hard-disk "/dev/vda"
233 #:partitions partitions
234 #:grub.cfg #$grub-configuration)
235 (reboot)))))
236 #:system system
237 #:make-disk-image? #t
238 #:disk-image-size disk-image-size
239 #:disk-image-format disk-image-format
240 #:references-graphs inputs))
241
242 \f
243 ;;;
244 ;;; VM and disk images.
245 ;;;
246
247 (define* (system-disk-image os
248 #:key
249 (name "disk-image")
250 (file-system-type "ext4")
251 (disk-image-size (* 900 (expt 2 20)))
252 (volatile? #t))
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."
257 (define root-label
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.)
261 "gnu-disk-image")
262
263 (define file-systems-to-keep
264 (remove (lambda (fs)
265 (string=? (file-system-mount-point fs) "/"))
266 (operating-system-file-systems os)))
267
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
274 #:volatile-root? #t
275 rest)))
276
277 ;; Force our own root file system.
278 (file-systems (cons (file-system
279 (mount-point "/")
280 (device root-label)
281 (title 'label)
282 (type file-system-type))
283 file-systems-to-keep)))))
284
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
294 #:copy-inputs? #t
295 #:register-closures? #t
296 #:inputs `(("system" ,os-drv)
297 ("grub.cfg" ,grub.cfg))))))
298
299 (define* (system-qemu-image os
300 #:key
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
307 ;; devices.
308 (remove (lambda (fs)
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)))
314
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
319 #:virtio? #t
320 rest)))
321
322 ;; Force our own root file system.
323 (file-systems (cons (file-system
324 (mount-point "/")
325 (device "/dev/sda1")
326 (type file-system-type))
327 file-systems-to-keep)))))
328 (mlet* %store-monad
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))))
338
339 \f
340 ;;;
341 ;;; VMs that share file systems with the host.
342 ;;;
343
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.
348 (string-append "TAG"
349 (string-map (match-lambda
350 (#\/ #\_)
351 (chr chr))
352 fs)))
353
354 (define (mapping->file-system mapping)
355 "Return a 9p file system that realizes MAPPING."
356 (match mapping
357 (($ <file-system-mapping> source target writable?)
358 (file-system
359 (mount-point target)
360 (device (file-system->mount-tag source))
361 (type "9p")
362 (flags (if writable? '() '(read-only)))
363 (options (string-append "trans=virtio"))
364 (check? #f)
365 (create-mount-point? #t)))))
366
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.
374 (remove (lambda (fs)
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)))
382
383 (operating-system (inherit os)
384 (initrd (lambda (file-systems . rest)
385 (apply base-initrd file-systems
386 #:volatile-root? #t
387 #:virtio? #t
388 rest)))
389
390 ;; Disable swap.
391 (swap-devices '())
392
393 (file-systems (cons* (file-system
394 (mount-point "/")
395 (device "/dev/vda1")
396 (type "ext4"))
397
398 (file-system (inherit
399 (mapping->file-system %store-mapping))
400 (needed-for-boot? #t))
401
402 (append (map mapping->file-system mappings)
403 user-file-systems)))))
404
405 (define* (system-qemu-image/shared-store
406 os
407 #:key
408 full-boot?
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
411 with the host.
412
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))
427 '())
428
429 ;; XXX: Passing #t here is too slow, so let it off by default.
430 #:register-closures? #f
431 #:copy-inputs? full-boot?)))
432
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)
440 "\" "))
441
442 #~(string-append
443 ;; Only enable kvm if we see /dev/kvm exists.
444 ;; This allows users without hardware virtualization to still use these
445 ;; commands.
446 #$(if (file-exists? "/dev/kvm")
447 " -enable-kvm "
448 "")
449 " -no-reboot -net nic,model=virtio \
450 " #$@(map virtfs-option shared-fs) " \
451 -vga std \
452 -drive file=" #$image
453 ",if=virtio,cache=writeback,werror=report,readonly \
454 -m 256"))
455
456 (define* (system-qemu-image/shared-store-script os
457 #:key
458 (qemu qemu)
459 (graphic? #t)
460 (mappings '())
461 full-boot?
462 (disk-image-size
463 (* (if full-boot? 500 30)
464 (expt 2 20))))
465 "Return a derivation that builds a script to run a virtual machine image of
466 OS that shares its store with the host.
467
468 MAPPINGS is a list of <file-system-mapping> specifying mapping of host file
469 systems into the guest.
470
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
478 os
479 #:full-boot? full-boot?
480 #:disk-image-size disk-image-size)))
481 (define builder
482 #~(call-with-output-file #$output
483 (lambda (port)
484 (display
485 (string-append "#!" #$bash "/bin/sh
486 exec " #$qemu "/bin/" #$(qemu-command (%current-system))
487
488 #$@(if full-boot?
489 #~()
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)))
498 " \"$@\"\n")
499 port)
500 (chmod port #o555))))
501
502 (gexp->derivation "run-vm.sh" builder)))
503
504 ;;; vm.scm ends here