Merge branch 'core-updates'
[jackhill/guix/guix.git] / gnu / system / vm.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2013, 2014 Ludovic Courtès <ludo@gnu.org>
3 ;;;
4 ;;; This file is part of GNU Guix.
5 ;;;
6 ;;; GNU Guix is free software; you can redistribute it and/or modify it
7 ;;; under the terms of the GNU General Public License as published by
8 ;;; the Free Software Foundation; either version 3 of the License, or (at
9 ;;; your option) any later version.
10 ;;;
11 ;;; GNU Guix is distributed in the hope that it will be useful, but
12 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ;;; GNU General Public License for more details.
15 ;;;
16 ;;; You should have received a copy of the GNU General Public License
17 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
18
19 (define-module (gnu system vm)
20 #:use-module (guix config)
21 #:use-module (guix store)
22 #:use-module (guix gexp)
23 #:use-module (guix derivations)
24 #:use-module (guix packages)
25 #:use-module (guix monads)
26 #:use-module ((gnu build vm)
27 #:select (qemu-command))
28 #:use-module (gnu packages base)
29 #:use-module (gnu packages guile)
30 #:use-module (gnu packages gawk)
31 #:use-module (gnu packages bash)
32 #:use-module (gnu packages less)
33 #:use-module (gnu packages qemu)
34 #:use-module (gnu packages disk)
35 #:use-module (gnu packages zile)
36 #:use-module (gnu packages grub)
37 #:use-module (gnu packages linux)
38 #:use-module (gnu packages package-management)
39 #:use-module ((gnu packages make-bootstrap)
40 #:select (%guile-static-stripped))
41 #:use-module (gnu packages admin)
42
43 #:use-module (gnu system shadow)
44 #:use-module (gnu system linux)
45 #:use-module (gnu system linux-initrd)
46 #:use-module (gnu system grub)
47 #:use-module (gnu system file-systems)
48 #:use-module (gnu system)
49 #:use-module (gnu services)
50
51 #:use-module (srfi srfi-1)
52 #:use-module (srfi srfi-26)
53 #:use-module (ice-9 match)
54
55 #:export (expression->derivation-in-linux-vm
56 qemu-image
57 system-qemu-image
58 system-qemu-image/shared-store
59 system-qemu-image/shared-store-script
60 system-disk-image))
61
62 \f
63 ;;; Commentary:
64 ;;;
65 ;;; Tools to evaluate build expressions within virtual machines.
66 ;;;
67 ;;; Code:
68
69 (define* (input->name+output tuple #:key (system (%current-system)))
70 "Return as a monadic value a name/file-name pair corresponding to TUPLE, an
71 input tuple. The output file name is when building for SYSTEM."
72 (with-monad %store-monad
73 (match tuple
74 ((input (? package? package))
75 (mlet %store-monad ((out (package-file package #:system system)))
76 (return `(,input . ,out))))
77 ((input (? package? package) sub-drv)
78 (mlet %store-monad ((out (package-file package
79 #:output sub-drv
80 #:system system)))
81 (return `(,input . ,out))))
82 ((input (? derivation? drv))
83 (return `(,input . ,(derivation->output-path drv))))
84 ((input (? derivation? drv) sub-drv)
85 (return `(,input . ,(derivation->output-path drv sub-drv))))
86 ((input (and (? string?) (? store-path?) file))
87 (return `(,input . ,file))))))
88
89 (define %linux-vm-file-systems
90 ;; File systems mounted for 'derivation-in-linux-vm'. The store and /xchg
91 ;; directory are shared with the host over 9p.
92 (list (file-system
93 (mount-point (%store-prefix))
94 (device "store")
95 (type "9p")
96 (needed-for-boot? #t)
97 (options "trans=virtio")
98 (check? #f))
99 (file-system
100 (mount-point "/xchg")
101 (device "xchg")
102 (type "9p")
103 (needed-for-boot? #t)
104 (options "trans=virtio")
105 (check? #f))))
106
107 (define* (expression->derivation-in-linux-vm name exp
108 #:key
109 (system (%current-system))
110 (linux linux-libre)
111 initrd
112 (qemu qemu-headless)
113 (env-vars '())
114 (modules
115 '((gnu build vm)
116 (gnu build install)
117 (gnu build linux-boot)
118 (gnu build file-systems)
119 (guix build utils)
120 (guix build store-copy)))
121 (guile-for-build
122 (%guile-for-build))
123
124 (make-disk-image? #f)
125 (references-graphs #f)
126 (memory-size 256)
127 (disk-image-format "qcow2")
128 (disk-image-size
129 (* 100 (expt 2 20))))
130 "Evaluate EXP in a QEMU virtual machine running LINUX with INITRD (a
131 derivation). In the virtual machine, EXP has access to all its inputs from the
132 store; it should put its output files in the `/xchg' directory, which is
133 copied to the derivation's output when the VM terminates. The virtual machine
134 runs with MEMORY-SIZE MiB of memory.
135
136 When MAKE-DISK-IMAGE? is true, then create a QEMU disk image of type
137 DISK-IMAGE-FORMAT (e.g., 'qcow2' or 'raw'), of DISK-IMAGE-SIZE bytes and
138 return it.
139
140 MODULES is the set of modules imported in the execution environment of EXP.
141
142 When REFERENCES-GRAPHS is true, it must be a list of file name/store path
143 pairs, as for `derivation'. The files containing the reference graphs are
144 made available under the /xchg CIFS share."
145 (mlet* %store-monad
146 ((module-dir (imported-modules modules))
147 (compiled (compiled-modules modules))
148 (user-builder (gexp->file "builder-in-linux-vm" exp))
149 (loader (gexp->file "linux-vm-loader"
150 #~(begin
151 (set! %load-path
152 (cons #$module-dir %load-path))
153 (set! %load-compiled-path
154 (cons #$compiled
155 %load-compiled-path))
156 (primitive-load #$user-builder))))
157 (coreutils -> (canonical-package coreutils))
158 (initrd (if initrd ; use the default initrd?
159 (return initrd)
160 (base-initrd %linux-vm-file-systems
161 #:virtio? #t
162 #:qemu-networking? #t))))
163
164 (define builder
165 ;; Code that launches the VM that evaluates EXP.
166 #~(begin
167 (use-modules (guix build utils)
168 (gnu build vm))
169
170 (let ((inputs '#$(list qemu coreutils))
171 (linux (string-append #$linux "/bzImage"))
172 (initrd (string-append #$initrd "/initrd"))
173 (loader #$loader)
174 (graphs '#$(match references-graphs
175 (((graph-files . _) ...) graph-files)
176 (_ #f))))
177
178 (set-path-environment-variable "PATH" '("bin") inputs)
179
180 (load-in-linux-vm loader
181 #:output #$output
182 #:linux linux #:initrd initrd
183 #:memory-size #$memory-size
184 #:make-disk-image? #$make-disk-image?
185 #:disk-image-format #$disk-image-format
186 #:disk-image-size #$disk-image-size
187 #:references-graphs graphs))))
188
189 (gexp->derivation name builder
190 ;; TODO: Require the "kvm" feature.
191 #:system system
192 #:env-vars env-vars
193 #:modules modules
194 #:guile-for-build guile-for-build
195 #:references-graphs references-graphs)))
196
197 (define* (qemu-image #:key
198 (name "qemu-image")
199 (system (%current-system))
200 (qemu qemu-headless)
201 (disk-image-size (* 100 (expt 2 20)))
202 (disk-image-format "qcow2")
203 (file-system-type "ext4")
204 file-system-label
205 os-derivation
206 grub-configuration
207 (register-closures? #t)
208 (inputs '())
209 copy-inputs?)
210 "Return a bootable, stand-alone QEMU image of type DISK-IMAGE-FORMAT (e.g.,
211 'qcow2' or 'raw'), with a root partition of type FILE-SYSTEM-TYPE.
212 Optionally, FILE-SYSTEM-LABEL can be specified as the volume name for the root
213 partition. The returned image is a full disk image that runs OS-DERIVATION,
214 with a GRUB installation that uses GRUB-CONFIGURATION as its configuration
215 file (GRUB-CONFIGURATION must be the name of a file in the VM.)
216
217 INPUTS is a list of inputs (as for packages). When COPY-INPUTS? is true, copy
218 all of INPUTS into the image being built. When REGISTER-CLOSURES? is true,
219 register INPUTS in the store database of the image so that Guix can be used in
220 the image."
221 (expression->derivation-in-linux-vm
222 name
223 #~(begin
224 (use-modules (gnu build vm)
225 (guix build utils))
226
227 (let ((inputs
228 '#$(append (list qemu parted grub e2fsprogs util-linux)
229 (map canonical-package
230 (list sed grep coreutils findutils gawk))
231 (if register-closures? (list guix) '())))
232
233 ;; This variable is unused but allows us to add INPUTS-TO-COPY
234 ;; as inputs.
235 (to-register
236 '#$(map (match-lambda
237 ((name thing) thing)
238 ((name thing output) `(,thing ,output)))
239 inputs)))
240
241 (set-path-environment-variable "PATH" '("bin" "sbin") inputs)
242
243 (let ((graphs '#$(match inputs
244 (((names . _) ...)
245 names))))
246 (initialize-hard-disk "/dev/vda"
247 #:system-directory #$os-derivation
248 #:grub.cfg #$grub-configuration
249 #:closures graphs
250 #:copy-closures? #$copy-inputs?
251 #:register-closures? #$register-closures?
252 #:disk-image-size #$disk-image-size
253 #:file-system-type #$file-system-type
254 #:file-system-label #$file-system-label)
255 (reboot))))
256 #:system system
257 #:make-disk-image? #t
258 #:disk-image-size disk-image-size
259 #:disk-image-format disk-image-format
260 #:references-graphs inputs))
261
262 \f
263 ;;;
264 ;;; VM and disk images.
265 ;;;
266
267 (define* (system-disk-image os
268 #:key
269 (name "disk-image")
270 (file-system-type "ext4")
271 (disk-image-size (* 900 (expt 2 20)))
272 (volatile? #t))
273 "Return the derivation of a disk image of DISK-IMAGE-SIZE bytes of the
274 system described by OS. Said image can be copied on a USB stick as is. When
275 VOLATILE? is true, the root file system is made volatile; this is useful
276 to USB sticks meant to be read-only."
277 (define root-label
278 ;; Volume name of the root file system. Since we don't know which device
279 ;; will hold it, we use the volume name to find it (using the UUID would
280 ;; be even better, but somewhat less convenient.)
281 "gnu-disk-image")
282
283 (define file-systems-to-keep
284 (remove (lambda (fs)
285 (string=? (file-system-mount-point fs) "/"))
286 (operating-system-file-systems os)))
287
288 (let ((os (operating-system (inherit os)
289 ;; Since this is meant to be used on real hardware, don't
290 ;; install QEMU networking or anything like that, but make sure
291 ;; USB mass storage devices are available.
292 (initrd (lambda (file-systems . rest)
293 (apply base-initrd file-systems
294 #:volatile-root? #t
295 #:extra-modules '("usb-storage.ko")
296 rest)))
297
298 ;; Force our own root file system.
299 (file-systems (cons (file-system
300 (mount-point "/")
301 (device root-label)
302 (title 'label)
303 (type file-system-type))
304 file-systems-to-keep)))))
305
306 (mlet* %store-monad ((os-drv (operating-system-derivation os))
307 (grub.cfg (operating-system-grub.cfg os)))
308 (qemu-image #:name name
309 #:os-derivation os-drv
310 #:grub-configuration grub.cfg
311 #:disk-image-size disk-image-size
312 #:disk-image-format "raw"
313 #:file-system-type file-system-type
314 #:file-system-label root-label
315 #:copy-inputs? #t
316 #:register-closures? #t
317 #:inputs `(("system" ,os-drv)
318 ("grub.cfg" ,grub.cfg))))))
319
320 (define* (system-qemu-image os
321 #:key
322 (file-system-type "ext4")
323 (disk-image-size (* 900 (expt 2 20))))
324 "Return the derivation of a freestanding QEMU image of DISK-IMAGE-SIZE bytes
325 of the GNU system as described by OS."
326 (define file-systems-to-keep
327 ;; Keep only file systems other than root and not normally bound to real
328 ;; devices.
329 (remove (lambda (fs)
330 (let ((target (file-system-mount-point fs))
331 (source (file-system-device fs)))
332 (or (string=? target "/")
333 (string-prefix? "/dev/" source))))
334 (operating-system-file-systems os)))
335
336 (let ((os (operating-system (inherit os)
337 ;; Use an initrd with the whole QEMU shebang.
338 (initrd (lambda (file-systems . rest)
339 (apply base-initrd file-systems
340 #:virtio? #t
341 #:qemu-networking? #t
342 rest)))
343
344 ;; Force our own root file system.
345 (file-systems (cons (file-system
346 (mount-point "/")
347 (device "/dev/sda1")
348 (type file-system-type))
349 file-systems-to-keep)))))
350 (mlet* %store-monad
351 ((os-drv (operating-system-derivation os))
352 (grub.cfg (operating-system-grub.cfg os)))
353 (qemu-image #:os-derivation os-drv
354 #:grub-configuration grub.cfg
355 #:disk-image-size disk-image-size
356 #:file-system-type file-system-type
357 #:inputs `(("system" ,os-drv)
358 ("grub.cfg" ,grub.cfg))
359 #:copy-inputs? #t))))
360
361 (define (virtualized-operating-system os)
362 "Return an operating system based on OS suitable for use in a virtualized
363 environment with the store shared with the host."
364 (operating-system (inherit os)
365 (initrd (lambda (file-systems . rest)
366 (apply base-initrd file-systems
367 #:volatile-root? #t
368 #:virtio? #t
369 #:qemu-networking? #t
370 rest)))
371 (file-systems (cons* (file-system
372 (mount-point "/")
373 (device "/dev/vda1")
374 (type "ext4"))
375 (file-system
376 (mount-point (%store-prefix))
377 (device "store")
378 (type "9p")
379 (needed-for-boot? #t)
380 (options "trans=virtio")
381 (check? #f))
382
383 ;; Remove file systems that conflict with those
384 ;; above, or that are normally bound to real devices.
385 (remove (lambda (fs)
386 (let ((target (file-system-mount-point fs))
387 (source (file-system-device fs)))
388 (or (string=? target (%store-prefix))
389 (string=? target "/")
390 (string-prefix? "/dev/" source))))
391 (operating-system-file-systems os))))))
392
393 (define* (system-qemu-image/shared-store
394 os
395 #:key (disk-image-size (* 15 (expt 2 20))))
396 "Return a derivation that builds a QEMU image of OS that shares its store
397 with the host."
398 (mlet* %store-monad
399 ((os-drv (operating-system-derivation os))
400 (grub.cfg (operating-system-grub.cfg os)))
401 (qemu-image #:os-derivation os-drv
402 #:grub-configuration grub.cfg
403 #:disk-image-size disk-image-size
404 #:inputs `(("system" ,os-drv))
405
406 ;; XXX: Passing #t here is too slow, so let it off by default.
407 #:register-closures? #f
408 #:copy-inputs? #f)))
409
410 (define* (system-qemu-image/shared-store-script
411 os
412 #:key
413 (qemu qemu)
414 (graphic? #t))
415 "Return a derivation that builds a script to run a virtual machine image of
416 OS that shares its store with the host."
417 (mlet* %store-monad
418 ((os -> (virtualized-operating-system os))
419 (os-drv (operating-system-derivation os))
420 (image (system-qemu-image/shared-store os)))
421 (define builder
422 #~(call-with-output-file #$output
423 (lambda (port)
424 (display
425 (string-append "#!" #$bash "/bin/sh
426 exec " #$qemu "/bin/" #$(qemu-command (%current-system))
427 " -enable-kvm -no-reboot -net nic,model=virtio \
428 -virtfs local,path=" #$(%store-prefix) ",security_model=none,mount_tag=store \
429 -net user \
430 -kernel " #$(operating-system-kernel os) "/bzImage \
431 -initrd " #$os-drv "/initrd \
432 -append \"" #$(if graphic? "" "console=ttyS0 ")
433 "--system=" #$os-drv " --load=" #$os-drv "/boot --root=/dev/vda1\" \
434 -serial stdio \
435 -drive file=" #$image
436 ",if=virtio,cache=writeback,werror=report,readonly \
437 -m 256
438 \n")
439 port)
440 (chmod port #o555))))
441
442 (gexp->derivation "run-vm.sh" builder)))
443
444 ;;; vm.scm ends here