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