gnu: Add chess and xboard.
[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 31 #:use-module (gnu packages qemu)
cc4a2aeb 32 #:use-module (gnu packages disk)
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)
060238ae 156 (base-initrd %linux-vm-file-systems
24e0160a 157 #:virtio? #t
4fc96187 158 #:qemu-networking? #t
83bcd0b8 159 #:guile-modules-in-chroot? #t))))
1aa0033b
LC
160
161 (define builder
162 ;; Code that launches the VM that evaluates EXP.
163 #~(begin
164 (use-modules (guix build utils)
165 (guix build vm))
166
167 (let ((inputs '#$(list qemu coreutils))
168 (linux (string-append #$linux "/bzImage"))
169 (initrd (string-append #$initrd "/initrd"))
170 (loader #$loader)
171 (graphs '#$(match references-graphs
172 (((graph-files . _) ...) graph-files)
173 (_ #f))))
174
175 (set-path-environment-variable "PATH" '("bin") inputs)
176
177 (load-in-linux-vm loader
178 #:output #$output
179 #:linux linux #:initrd initrd
180 #:memory-size #$memory-size
181 #:make-disk-image? #$make-disk-image?
c4a74364 182 #:disk-image-format #$disk-image-format
1aa0033b
LC
183 #:disk-image-size #$disk-image-size
184 #:references-graphs graphs))))
185
186 (gexp->derivation name builder
187 ;; TODO: Require the "kvm" feature.
188 #:system system
189 #:env-vars env-vars
5ce3defe 190 #:modules modules
1aa0033b
LC
191 #:guile-for-build guile-for-build
192 #:references-graphs references-graphs)))
d9f0a237
LC
193
194(define* (qemu-image #:key
04086015
LC
195 (name "qemu-image")
196 (system (%current-system))
1aa0033b 197 (qemu qemu-headless)
04086015 198 (disk-image-size (* 100 (expt 2 20)))
c4a74364 199 (disk-image-format "qcow2")
03ddfaf5 200 (file-system-type "ext4")
ef9fc40d 201 file-system-label
f2c403ea 202 os-derivation
0e2ddecd 203 grub-configuration
150e20dd 204 (register-closures? #t)
150e20dd
LC
205 (inputs '())
206 copy-inputs?)
c4a74364 207 "Return a bootable, stand-alone QEMU image of type DISK-IMAGE-FORMAT (e.g.,
ef9fc40d
LC
208'qcow2' or 'raw'), with a root partition of type FILE-SYSTEM-TYPE.
209Optionally, FILE-SYSTEM-LABEL can be specified as the volume name for the root
f2c403ea
LC
210partition. The returned image is a full disk image that runs OS-DERIVATION,
211with a GRUB installation that uses GRUB-CONFIGURATION as its configuration
212file (GRUB-CONFIGURATION must be the name of a file in the VM.)
93d44bd8 213
150e20dd
LC
214INPUTS is a list of inputs (as for packages). When COPY-INPUTS? is true, copy
215all of INPUTS into the image being built. When REGISTER-CLOSURES? is true,
216register INPUTS in the store database of the image so that Guix can be used in
b4140694 217the image."
d9f0a237 218 (mlet %store-monad
150e20dd 219 ((graph (sequence %store-monad (map input->name+output inputs))))
d9f0a237 220 (expression->derivation-in-linux-vm
1aa0033b
LC
221 name
222 #~(begin
223 (use-modules (guix build vm)
224 (guix build utils))
225
226 (let ((inputs
227 '#$(append (list qemu parted grub e2fsprogs util-linux)
228 (map (compose car (cut assoc-ref %final-inputs <>))
229 '("sed" "grep" "coreutils" "findutils" "gawk"))
150e20dd 230 (if register-closures? (list guix) '())))
1aa0033b
LC
231
232 ;; This variable is unused but allows us to add INPUTS-TO-COPY
233 ;; as inputs.
150e20dd 234 (to-register
1aa0033b
LC
235 '#$(map (match-lambda
236 ((name thing) thing)
237 ((name thing output) `(,thing ,output)))
150e20dd 238 inputs)))
1aa0033b
LC
239
240 (set-path-environment-variable "PATH" '("bin" "sbin") inputs)
241
150e20dd 242 (let ((graphs '#$(match inputs
1aa0033b
LC
243 (((names . _) ...)
244 names))))
f19c6e5f 245 (initialize-hard-disk "/dev/vda"
f2c403ea 246 #:system-directory #$os-derivation
e38e18ff 247 #:grub.cfg #$grub-configuration
150e20dd
LC
248 #:closures graphs
249 #:copy-closures? #$copy-inputs?
250 #:register-closures? #$register-closures?
1aa0033b 251 #:disk-image-size #$disk-image-size
ef9fc40d
LC
252 #:file-system-type #$file-system-type
253 #:file-system-label #$file-system-label)
1aa0033b 254 (reboot))))
d9f0a237 255 #:system system
d9f0a237
LC
256 #:make-disk-image? #t
257 #:disk-image-size disk-image-size
c4a74364 258 #:disk-image-format disk-image-format
ade5ce7a 259 #:references-graphs graph)))
04086015
LC
260
261\f
262;;;
1e77fedb 263;;; VM and disk images.
04086015
LC
264;;;
265
1e77fedb
LC
266(define* (system-disk-image os
267 #:key
56ef7fcc 268 (name "disk-image")
1e77fedb
LC
269 (file-system-type "ext4")
270 (disk-image-size (* 900 (expt 2 20)))
271 (volatile? #t))
272 "Return the derivation of a disk image of DISK-IMAGE-SIZE bytes of the
273system described by OS. Said image can be copied on a USB stick as is. When
274VOLATILE? is true, the root file system is made volatile; this is useful
275to USB sticks meant to be read-only."
10ace2c4
LC
276 (define root-label
277 ;; Volume name of the root file system. Since we don't know which device
278 ;; will hold it, we use the volume name to find it (using the UUID would
279 ;; be even better, but somewhat less convenient.)
280 "gnu-disk-image")
281
1e77fedb
LC
282 (define file-systems-to-keep
283 (remove (lambda (fs)
284 (string=? (file-system-mount-point fs) "/"))
285 (operating-system-file-systems os)))
286
287 (let ((os (operating-system (inherit os)
112440a7
LC
288 ;; Since this is meant to be used on real hardware, don't set up
289 ;; QEMU networking.
060238ae 290 (initrd (cut base-initrd <>
4fc96187 291 #:volatile-root? volatile?))
1e77fedb
LC
292
293 ;; Force our own root file system.
294 (file-systems (cons (file-system
295 (mount-point "/")
10ace2c4 296 (device root-label)
d4c87617 297 (title 'label)
1e77fedb
LC
298 (type file-system-type))
299 file-systems-to-keep)))))
300
301 (mlet* %store-monad ((os-drv (operating-system-derivation os))
302 (grub.cfg (operating-system-grub.cfg os)))
56ef7fcc 303 (qemu-image #:name name
f2c403ea 304 #:os-derivation os-drv
56ef7fcc 305 #:grub-configuration grub.cfg
1e77fedb
LC
306 #:disk-image-size disk-image-size
307 #:disk-image-format "raw"
308 #:file-system-type file-system-type
10ace2c4 309 #:file-system-label root-label
1e77fedb
LC
310 #:copy-inputs? #t
311 #:register-closures? #t
312 #:inputs `(("system" ,os-drv)
313 ("grub.cfg" ,grub.cfg))))))
314
0b14d1d7 315(define* (system-qemu-image os
66f23d66
LC
316 #:key
317 (file-system-type "ext4")
318 (disk-image-size (* 900 (expt 2 20))))
319 "Return the derivation of a freestanding QEMU image of DISK-IMAGE-SIZE bytes
320of the GNU system as described by OS."
1eeccc2f
LC
321 (define file-systems-to-keep
322 ;; Keep only file systems other than root and not normally bound to real
323 ;; devices.
324 (remove (lambda (fs)
325 (let ((target (file-system-mount-point fs))
326 (source (file-system-device fs)))
327 (or (string=? target "/")
328 (string-prefix? "/dev/" source))))
329 (operating-system-file-systems os)))
330
66f23d66 331 (let ((os (operating-system (inherit os)
e84d8b30
LC
332 ;; Use an initrd with the whole QEMU shebang.
333 (initrd (cut base-initrd <>
334 #:virtio? #t
335 #:qemu-networking? #t))
336
1eeccc2f
LC
337 ;; Force our own root file system.
338 (file-systems (cons (file-system
66f23d66
LC
339 (mount-point "/")
340 (device "/dev/sda1")
1eeccc2f
LC
341 (type file-system-type))
342 file-systems-to-keep)))))
66f23d66
LC
343 (mlet* %store-monad
344 ((os-drv (operating-system-derivation os))
b4140694 345 (grub.cfg (operating-system-grub.cfg os)))
f2c403ea
LC
346 (qemu-image #:os-derivation os-drv
347 #:grub-configuration grub.cfg
66f23d66
LC
348 #:disk-image-size disk-image-size
349 #:file-system-type file-system-type
b4140694
LC
350 #:inputs `(("system" ,os-drv)
351 ("grub.cfg" ,grub.cfg))
150e20dd 352 #:copy-inputs? #t))))
04086015 353
83bcd0b8
LC
354(define (virtualized-operating-system os)
355 "Return an operating system based on OS suitable for use in a virtualized
356environment with the store shared with the host."
357 (operating-system (inherit os)
060238ae 358 (initrd (cut base-initrd <>
24e0160a 359 #:volatile-root? #t
4fc96187
LC
360 #:virtio? #t
361 #:qemu-networking? #t))
1eeccc2f
LC
362 (file-systems (cons* (file-system
363 (mount-point "/")
364 (device "/dev/vda1")
365 (type "ext4"))
366 (file-system
367 (mount-point (%store-prefix))
368 (device "store")
369 (type "9p")
370 (needed-for-boot? #t)
371 (options "trans=virtio")
372 (check? #f))
373
374 ;; Remove file systems that conflict with those
375 ;; above, or that are normally bound to real devices.
376 (remove (lambda (fs)
377 (let ((target (file-system-mount-point fs))
378 (source (file-system-device fs)))
379 (or (string=? target (%store-prefix))
380 (string=? target "/")
381 (string-prefix? "/dev/" source))))
382 (operating-system-file-systems os))))))
83bcd0b8 383
fd3bfc44 384(define* (system-qemu-image/shared-store
0b14d1d7 385 os
fd3bfc44
LC
386 #:key (disk-image-size (* 15 (expt 2 20))))
387 "Return a derivation that builds a QEMU image of OS that shares its store
388with the host."
389 (mlet* %store-monad
390 ((os-drv (operating-system-derivation os))
b4140694 391 (grub.cfg (operating-system-grub.cfg os)))
f2c403ea
LC
392 (qemu-image #:os-derivation os-drv
393 #:grub-configuration grub.cfg
150e20dd
LC
394 #:disk-image-size disk-image-size
395 #:inputs `(("system" ,os-drv))
396
397 ;; XXX: Passing #t here is too slow, so let it off by default.
398 #:register-closures? #f
399 #:copy-inputs? #f)))
fd3bfc44
LC
400
401(define* (system-qemu-image/shared-store-script
0b14d1d7 402 os
fd3bfc44 403 #:key
1f3838ac 404 (qemu qemu)
fd3bfc44
LC
405 (graphic? #t))
406 "Return a derivation that builds a script to run a virtual machine image of
407OS that shares its store with the host."
c47f0d8b 408 (mlet* %store-monad
83bcd0b8 409 ((os -> (virtualized-operating-system os))
c47f0d8b 410 (os-drv (operating-system-derivation os))
c47f0d8b 411 (image (system-qemu-image/shared-store os)))
fd3bfc44 412 (define builder
02100028
LC
413 #~(call-with-output-file #$output
414 (lambda (port)
415 (display
416 (string-append "#!" #$bash "/bin/sh
417exec " #$qemu "/bin/qemu-system-x86_64 -enable-kvm -no-reboot -net nic,model=virtio \
418 -virtfs local,path=" #$(%store-prefix) ",security_model=none,mount_tag=store \
1f3838ac 419 -net user \
02100028 420 -kernel " #$(operating-system-kernel os) "/bzImage \
83bcd0b8 421 -initrd " #$os-drv "/initrd \
02100028 422-append \"" #$(if graphic? "" "console=ttyS0 ")
b4140694 423 "--system=" #$os-drv " --load=" #$os-drv "/boot --root=/dev/vda1\" \
5a84a6c3 424 -serial stdio \
02100028 425 -drive file=" #$image
fd3bfc44 426 ",if=virtio,cache=writeback,werror=report,readonly\n")
02100028
LC
427 port)
428 (chmod port #o555))))
429
430 (gexp->derivation "run-vm.sh" builder)))
fd3bfc44 431
04086015 432;;; vm.scm ends here