gnu: qtox: Update to 1.13.0.
[jackhill/guix/guix.git] / gnu / system / vm.scm
CommitLineData
04086015 1;;; GNU Guix --- Functional package management for GNU
a8ac4f08 2;;; Copyright © 2013, 2014, 2015, 2016, 2017 Ludovic Courtès <ludo@gnu.org>
944d2b17
CAW
3;;; Copyright © 2016 Christopher Allan Webber <cwebber@dustycloud.org>
4;;; Copyright © 2016 Leo Famulari <leo@famulari.name>
07f812c4 5;;; Copyright © 2017 Mathieu Othacehe <m.othacehe@gmail.com>
ecf5d537 6;;; Copyright © 2017 Marius Bakke <mbakke@fastmail.com>
04086015
LC
7;;;
8;;; This file is part of GNU Guix.
9;;;
10;;; GNU Guix is free software; you can redistribute it and/or modify it
11;;; under the terms of the GNU General Public License as published by
12;;; the Free Software Foundation; either version 3 of the License, or (at
13;;; your option) any later version.
14;;;
15;;; GNU Guix is distributed in the hope that it will be useful, but
16;;; WITHOUT ANY WARRANTY; without even the implied warranty of
17;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18;;; GNU General Public License for more details.
19;;;
20;;; You should have received a copy of the GNU General Public License
21;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
22
23(define-module (gnu system vm)
93d44bd8 24 #:use-module (guix config)
04086015 25 #:use-module (guix store)
02100028 26 #:use-module (guix gexp)
04086015
LC
27 #:use-module (guix derivations)
28 #:use-module (guix packages)
d9f0a237 29 #:use-module (guix monads)
fcf63cf8 30 #:use-module (guix records)
239c6e27 31 #:use-module (guix modules)
fcf63cf8 32
548f7a8f 33 #:use-module ((gnu build vm)
66670cf3 34 #:select (qemu-command))
bdb36958 35 #:use-module (gnu packages base)
862e38d5 36 #:use-module (gnu packages bootloaders)
be1033a3 37 #:use-module (gnu packages cdrom)
1b89a66e 38 #:use-module (gnu packages guile)
bdb36958 39 #:use-module (gnu packages gawk)
1b89a66e 40 #:use-module (gnu packages bash)
4f62d8d6 41 #:use-module (gnu packages less)
59132b80 42 #:use-module (gnu packages virtualization)
cc4a2aeb 43 #:use-module (gnu packages disk)
5b16ff09 44 #:use-module (gnu packages zile)
04086015 45 #:use-module (gnu packages linux)
30f25b03 46 #:use-module (gnu packages package-management)
04086015
LC
47 #:use-module ((gnu packages make-bootstrap)
48 #:select (%guile-static-stripped))
9de46ffb 49 #:use-module (gnu packages admin)
0ded70f3 50
9121ce55 51 #:use-module (gnu bootloader)
9b396c0c 52 #:use-module (gnu bootloader grub)
0ded70f3 53 #:use-module (gnu system shadow)
6e828634 54 #:use-module (gnu system pam)
735c6dd7 55 #:use-module (gnu system linux-initrd)
b09a8da4 56 #:use-module (gnu bootloader)
c5df1839 57 #:use-module (gnu system file-systems)
033adfe7 58 #:use-module (gnu system)
db4fdc04 59 #:use-module (gnu services)
9b336338 60 #:use-module (gnu system uuid)
0ded70f3 61
ca85d7bc 62 #:use-module (srfi srfi-1)
04086015 63 #:use-module (srfi srfi-26)
5f7fe1c5 64 #:use-module (rnrs bytevectors)
04086015 65 #:use-module (ice-9 match)
0ded70f3 66
04086015 67 #:export (expression->derivation-in-linux-vm
aedb72fb 68 qemu-image
e9f693d0 69 virtualized-operating-system
fd3bfc44 70 system-qemu-image
fcf63cf8 71
fd3bfc44 72 system-qemu-image/shared-store
1e77fedb 73 system-qemu-image/shared-store-script
ed419fa0
LC
74 system-disk-image
75
76 virtual-machine
77 virtual-machine?))
04086015
LC
78
79\f
80;;; Commentary:
81;;;
82;;; Tools to evaluate build expressions within virtual machines.
83;;;
84;;; Code:
85
83bcd0b8
LC
86(define %linux-vm-file-systems
87 ;; File systems mounted for 'derivation-in-linux-vm'. The store and /xchg
88 ;; directory are shared with the host over 9p.
89 (list (file-system
90 (mount-point (%store-prefix))
91 (device "store")
92 (type "9p")
93 (needed-for-boot? #t)
3c05b4bc
LC
94 (options "trans=virtio")
95 (check? #f))
83bcd0b8
LC
96 (file-system
97 (mount-point "/xchg")
98 (device "xchg")
99 (type "9p")
100 (needed-for-boot? #t)
3c05b4bc
LC
101 (options "trans=virtio")
102 (check? #f))))
83bcd0b8 103
d9f0a237 104(define* (expression->derivation-in-linux-vm name exp
04086015 105 #:key
2455085a 106 (system (%current-system))
04086015 107 (linux linux-libre)
735c6dd7 108 initrd
06da1a6b 109 (qemu qemu-minimal)
04086015 110 (env-vars '())
04086015
LC
111 (guile-for-build
112 (%guile-for-build))
113
8d033e3e 114 (single-file-output? #f)
04086015 115 (make-disk-image? #f)
ca85d7bc 116 (references-graphs #f)
defa1b9b 117 (memory-size 256)
c4a74364 118 (disk-image-format "qcow2")
a8ac4f08 119 (disk-image-size 'guess))
735c6dd7 120 "Evaluate EXP in a QEMU virtual machine running LINUX with INITRD (a
8d033e3e
LC
121derivation). The virtual machine runs with MEMORY-SIZE MiB of memory. In the
122virtual machine, EXP has access to all its inputs from the store; it should
123put its output file(s) in the '/xchg' directory.
124
125If SINGLE-FILE-OUTPUT? is true, copy a single file from '/xchg' to OUTPUT.
126Otherwise, copy the contents of /xchg to a new directory OUTPUT.
04086015 127
c4a74364
LC
128When MAKE-DISK-IMAGE? is true, then create a QEMU disk image of type
129DISK-IMAGE-FORMAT (e.g., 'qcow2' or 'raw'), of DISK-IMAGE-SIZE bytes and
a8ac4f08
LC
130return it. When DISK-IMAGE-SIZE is 'guess, estimate the image size based
131based on the size of the closure of REFERENCES-GRAPHS.
ca85d7bc
LC
132
133When REFERENCES-GRAPHS is true, it must be a list of file name/store path
134pairs, as for `derivation'. The files containing the reference graphs are
135made available under the /xchg CIFS share."
d9f0a237 136 (mlet* %store-monad
fd129893 137 ((user-builder (gexp->file "builder-in-linux-vm" exp))
02100028 138 (loader (gexp->file "linux-vm-loader"
fd129893 139 #~(primitive-load #$user-builder)))
bdb36958 140 (coreutils -> (canonical-package coreutils))
d4254711 141 (initrd (if initrd ; use the default initrd?
735c6dd7 142 (return initrd)
060238ae 143 (base-initrd %linux-vm-file-systems
0d275f4a 144 #:linux linux
24e0160a 145 #:virtio? #t
6c1df081 146 #:qemu-networking? #t))))
1aa0033b
LC
147
148 (define builder
149 ;; Code that launches the VM that evaluates EXP.
239c6e27
LC
150 (with-imported-modules (source-module-closure '((guix build utils)
151 (gnu build vm)))
4ee96a79
LC
152 #~(begin
153 (use-modules (guix build utils)
154 (gnu build vm))
155
a8ac4f08
LC
156 (let* ((inputs '#$(list qemu coreutils))
157 (linux (string-append #$linux "/"
158 #$(system-linux-image-file-name)))
159 (initrd (string-append #$initrd "/initrd"))
160 (loader #$loader)
161 (graphs '#$(match references-graphs
162 (((graph-files . _) ...) graph-files)
163 (_ #f)))
164 (size #$(if (eq? 'guess disk-image-size)
165 #~(+ (* 70 (expt 2 20)) ;ESP
166 (estimated-partition-size graphs))
167 disk-image-size)))
4ee96a79
LC
168
169 (set-path-environment-variable "PATH" '("bin") inputs)
170
171 (load-in-linux-vm loader
172 #:output #$output
173 #:linux linux #:initrd initrd
174 #:memory-size #$memory-size
175 #:make-disk-image? #$make-disk-image?
8d033e3e 176 #:single-file-output? #$single-file-output?
4ee96a79 177 #:disk-image-format #$disk-image-format
a8ac4f08 178 #:disk-image-size size
4ee96a79 179 #:references-graphs graphs)))))
1aa0033b
LC
180
181 (gexp->derivation name builder
182 ;; TODO: Require the "kvm" feature.
183 #:system system
184 #:env-vars env-vars
1aa0033b
LC
185 #:guile-for-build guile-for-build
186 #:references-graphs references-graphs)))
d9f0a237 187
be1033a3
DM
188(define* (iso9660-image #:key
189 (name "iso9660-image")
acc0f6bb
DM
190 file-system-label
191 file-system-uuid
be1033a3
DM
192 (system (%current-system))
193 (qemu qemu-minimal)
194 os-drv
195 bootcfg-drv
196 bootloader
e375d3fa 197 register-closures?
be1033a3
DM
198 (inputs '()))
199 "Return a bootable, stand-alone iso9660 image.
200
201INPUTS is a list of inputs (as for packages)."
202 (expression->derivation-in-linux-vm
203 name
204 (with-imported-modules (source-module-closure '((gnu build vm)
205 (guix build utils)))
206 #~(begin
207 (use-modules (gnu build vm)
208 (guix build utils))
209
210 (let ((inputs
211 '#$(append (list qemu parted e2fsprogs dosfstools xorriso)
212 (map canonical-package
e375d3fa
CB
213 (list sed grep coreutils findutils gawk))
214 (if register-closures? (list guix) '())))
be1033a3 215
e375d3fa
CB
216
217 (graphs '#$(match inputs
218 (((names . _) ...)
219 names)))
be1033a3
DM
220 ;; This variable is unused but allows us to add INPUTS-TO-COPY
221 ;; as inputs.
222 (to-register
223 '#$(map (match-lambda
224 ((name thing) thing)
225 ((name thing output) `(,thing ,output)))
226 inputs)))
227
228 (set-path-environment-variable "PATH" '("bin" "sbin") inputs)
229 (make-iso9660-image #$(bootloader-package bootloader)
230 #$bootcfg-drv
231 #$os-drv
acc0f6bb 232 "/xchg/guixsd.iso"
e375d3fa
CB
233 #:register-closures? #$register-closures?
234 #:closures graphs
acc0f6bb 235 #:volume-id #$file-system-label
9b336338
LC
236 #:volume-uuid #$(and=> file-system-uuid
237 uuid-bytevector))
be1033a3
DM
238 (reboot))))
239 #:system system
240 #:make-disk-image? #f
8d033e3e 241 #:single-file-output? #t
be1033a3
DM
242 #:references-graphs inputs))
243
d9f0a237 244(define* (qemu-image #:key
04086015
LC
245 (name "qemu-image")
246 (system (%current-system))
06da1a6b 247 (qemu qemu-minimal)
a8ac4f08 248 (disk-image-size 'guess)
c4a74364 249 (disk-image-format "qcow2")
03ddfaf5 250 (file-system-type "ext4")
ef9fc40d 251 file-system-label
fd3b4b98 252 file-system-uuid
9121ce55
MO
253 os-drv
254 bootcfg-drv
255 bootloader
150e20dd 256 (register-closures? #t)
150e20dd
LC
257 (inputs '())
258 copy-inputs?)
c4a74364 259 "Return a bootable, stand-alone QEMU image of type DISK-IMAGE-FORMAT (e.g.,
ef9fc40d
LC
260'qcow2' or 'raw'), with a root partition of type FILE-SYSTEM-TYPE.
261Optionally, FILE-SYSTEM-LABEL can be specified as the volume name for the root
fd3b4b98
LC
262partition; likewise FILE-SYSTEM-UUID, if true, specifies the UUID of the root
263partition (a UUID object).
264
265The returned image is a full disk image that runs OS-DERIVATION,
f2c403ea
LC
266with a GRUB installation that uses GRUB-CONFIGURATION as its configuration
267file (GRUB-CONFIGURATION must be the name of a file in the VM.)
93d44bd8 268
150e20dd
LC
269INPUTS is a list of inputs (as for packages). When COPY-INPUTS? is true, copy
270all of INPUTS into the image being built. When REGISTER-CLOSURES? is true,
271register INPUTS in the store database of the image so that Guix can be used in
b4140694 272the image."
b53833b2
LC
273 (expression->derivation-in-linux-vm
274 name
239c6e27
LC
275 (with-imported-modules (source-module-closure '((gnu build vm)
276 (guix build utils)))
fd129893
LC
277 #~(begin
278 (use-modules (gnu build vm)
a8ac4f08
LC
279 (guix build utils)
280 (srfi srfi-26))
1aa0033b 281
fd129893 282 (let ((inputs
4d415f0c 283 '#$(append (list qemu parted e2fsprogs dosfstools)
fd129893
LC
284 (map canonical-package
285 (list sed grep coreutils findutils gawk))
286 (if register-closures? (list guix) '())))
1aa0033b 287
fd129893
LC
288 ;; This variable is unused but allows us to add INPUTS-TO-COPY
289 ;; as inputs.
290 (to-register
291 '#$(map (match-lambda
292 ((name thing) thing)
293 ((name thing output) `(,thing ,output)))
294 inputs)))
1aa0033b 295
fd129893 296 (set-path-environment-variable "PATH" '("bin" "sbin") inputs)
1aa0033b 297
fd129893
LC
298 (let* ((graphs '#$(match inputs
299 (((names . _) ...)
300 names)))
301 (initialize (root-partition-initializer
302 #:closures graphs
303 #:copy-closures? #$copy-inputs?
304 #:register-closures? #$register-closures?
9121ce55 305 #:system-directory #$os-drv))
a8ac4f08 306 (root-size #$(if (eq? 'guess disk-image-size)
0c75a4de
CB
307 #~(max
308 ;; Minimum 20 MiB root size
309 (* 20 (expt 2 20))
310 (estimated-partition-size
311 (map (cut string-append "/xchg/" <>)
312 graphs)))
a8ac4f08
LC
313 (- disk-image-size
314 (* 50 (expt 2 20)))))
fd129893 315 (partitions (list (partition
a8ac4f08 316 (size root-size)
fd129893 317 (label #$file-system-label)
fd3b4b98
LC
318 (uuid #$(and=> file-system-uuid
319 uuid-bytevector))
fd129893 320 (file-system #$file-system-type)
01cc84da 321 (flags '(boot))
ecf5d537
MB
322 (initializer initialize))
323 ;; Append a small EFI System Partition for
324 ;; use with UEFI bootloaders.
325 (partition
326 ;; The standalone grub image is about 10MiB, but
327 ;; leave some room for custom or multiple images.
328 (size (* 40 (expt 2 20)))
329 (label "GNU-ESP") ;cosmetic only
330 ;; Use "vfat" here since this property is used
331 ;; when mounting. The actual FAT-ness is based
332 ;; on filesystem size (16 in this case).
333 (file-system "vfat")
334 (flags '(esp))))))
fd129893
LC
335 (initialize-hard-disk "/dev/vda"
336 #:partitions partitions
ecf5d537 337 #:grub-efi #$grub-efi
9121ce55
MO
338 #:bootloader-package
339 #$(bootloader-package bootloader)
340 #:bootcfg #$bootcfg-drv
341 #:bootcfg-location
342 #$(bootloader-configuration-file bootloader)
343 #:bootloader-installer
344 #$(bootloader-installer bootloader))
fd129893 345 (reboot)))))
b53833b2
LC
346 #:system system
347 #:make-disk-image? #t
348 #:disk-image-size disk-image-size
349 #:disk-image-format disk-image-format
350 #:references-graphs inputs))
04086015
LC
351
352\f
353;;;
1e77fedb 354;;; VM and disk images.
04086015
LC
355;;;
356
5f7fe1c5
LC
357(define* (operating-system-uuid os #:optional (type 'dce))
358 "Compute UUID object with a deterministic \"UUID\" for OS, of the given
359TYPE (one of 'iso9660 or 'dce). Return a UUID object."
360 (if (eq? type 'iso9660)
361 (let ((pad (compose (cut string-pad <> 2 #\0)
362 number->string))
363 (h (hash (operating-system-services os) 3600)))
364 (bytevector->uuid
365 (string->iso9660-uuid
366 (string-append "1970-01-01-"
367 (pad (hash (operating-system-host-name os) 24)) "-"
368 (pad (quotient h 60)) "-"
369 (pad (modulo h 60)) "-"
370 (pad (hash (operating-system-file-systems os) 100))))
371 'iso9660))
372 (bytevector->uuid
373 (uint-list->bytevector
374 (list (hash file-system-type
b1a30793 375 (- (expt 2 32) 1))
5f7fe1c5 376 (hash (operating-system-host-name os)
b1a30793 377 (- (expt 2 32) 1))
5f7fe1c5 378 (hash (operating-system-services os)
b1a30793 379 (- (expt 2 32) 1))
5f7fe1c5 380 (hash (operating-system-file-systems os)
b1a30793 381 (- (expt 2 32) 1)))
5f7fe1c5
LC
382 (endianness little)
383 4)
384 type)))
385
1e77fedb
LC
386(define* (system-disk-image os
387 #:key
56ef7fcc 388 (name "disk-image")
1e77fedb
LC
389 (file-system-type "ext4")
390 (disk-image-size (* 900 (expt 2 20)))
391 (volatile? #t))
392 "Return the derivation of a disk image of DISK-IMAGE-SIZE bytes of the
393system described by OS. Said image can be copied on a USB stick as is. When
394VOLATILE? is true, the root file system is made volatile; this is useful
395to USB sticks meant to be read-only."
651de2bd
DM
396 (define normalize-label
397 ;; ISO labels are all-caps (case-insensitive), but since
398 ;; 'find-partition-by-label' is case-sensitive, make it all-caps here.
399 (if (string=? "iso9660" file-system-type)
400 string-upcase
401 identity))
5f7fe1c5 402
10ace2c4 403 (define root-label
5f7fe1c5 404 ;; Volume name of the root file system.
0862b954 405 (normalize-label "GuixSD_image"))
10ace2c4 406
5f7fe1c5
LC
407 (define root-uuid
408 ;; UUID of the root file system, computed in a deterministic fashion.
409 ;; This is what we use to locate the root file system so it has to be
410 ;; different from the user's own file system UUIDs.
411 (operating-system-uuid os
412 (if (string=? file-system-type "iso9660")
413 'iso9660
414 'dce)))
415
1e77fedb
LC
416 (define file-systems-to-keep
417 (remove (lambda (fs)
418 (string=? (file-system-mount-point fs) "/"))
419 (operating-system-file-systems os)))
420
421 (let ((os (operating-system (inherit os)
932e1f92 422 ;; Since this is meant to be used on real hardware, don't
493c245b
LC
423 ;; install QEMU networking or anything like that. Assume USB
424 ;; mass storage devices (usb-storage.ko) are available.
52ac153e
LC
425 (initrd (lambda (file-systems . rest)
426 (apply base-initrd file-systems
427 #:volatile-root? #t
52ac153e 428 rest)))
1e77fedb 429
cf189709
DM
430 (bootloader (if (string=? "iso9660" file-system-type)
431 (bootloader-configuration
432 (inherit (operating-system-bootloader os))
433 (bootloader grub-mkrescue-bootloader))
434 (operating-system-bootloader os)))
435
1e77fedb
LC
436 ;; Force our own root file system.
437 (file-systems (cons (file-system
438 (mount-point "/")
5f7fe1c5
LC
439 (device root-uuid)
440 (title 'uuid)
1e77fedb
LC
441 (type file-system-type))
442 file-systems-to-keep)))))
443
444 (mlet* %store-monad ((os-drv (operating-system-derivation os))
c76b3046 445 (bootcfg (operating-system-bootcfg os)))
be1033a3
DM
446 (if (string=? "iso9660" file-system-type)
447 (iso9660-image #:name name
acc0f6bb 448 #:file-system-label root-label
5f7fe1c5 449 #:file-system-uuid root-uuid
be1033a3 450 #:os-drv os-drv
b069111f 451 #:register-closures? #t
be1033a3
DM
452 #:bootcfg-drv bootcfg
453 #:bootloader (bootloader-configuration-bootloader
454 (operating-system-bootloader os))
455 #:inputs `(("system" ,os-drv)
456 ("bootcfg" ,bootcfg)))
457 (qemu-image #:name name
458 #:os-drv os-drv
459 #:bootcfg-drv bootcfg
460 #:bootloader (bootloader-configuration-bootloader
461 (operating-system-bootloader os))
462 #:disk-image-size disk-image-size
463 #:disk-image-format "raw"
4138e782 464 #:file-system-type file-system-type
be1033a3 465 #:file-system-label root-label
5f7fe1c5 466 #:file-system-uuid root-uuid
be1033a3
DM
467 #:copy-inputs? #t
468 #:register-closures? #t
469 #:inputs `(("system" ,os-drv)
470 ("bootcfg" ,bootcfg)))))))
1e77fedb 471
0b14d1d7 472(define* (system-qemu-image os
66f23d66
LC
473 #:key
474 (file-system-type "ext4")
475 (disk-image-size (* 900 (expt 2 20))))
476 "Return the derivation of a freestanding QEMU image of DISK-IMAGE-SIZE bytes
477of the GNU system as described by OS."
1eeccc2f
LC
478 (define file-systems-to-keep
479 ;; Keep only file systems other than root and not normally bound to real
480 ;; devices.
481 (remove (lambda (fs)
482 (let ((target (file-system-mount-point fs))
483 (source (file-system-device fs)))
484 (or (string=? target "/")
485 (string-prefix? "/dev/" source))))
486 (operating-system-file-systems os)))
487
66f23d66 488 (let ((os (operating-system (inherit os)
e84d8b30 489 ;; Use an initrd with the whole QEMU shebang.
52ac153e
LC
490 (initrd (lambda (file-systems . rest)
491 (apply base-initrd file-systems
492 #:virtio? #t
52ac153e 493 rest)))
e84d8b30 494
1eeccc2f
LC
495 ;; Force our own root file system.
496 (file-systems (cons (file-system
66f23d66
LC
497 (mount-point "/")
498 (device "/dev/sda1")
1eeccc2f
LC
499 (type file-system-type))
500 file-systems-to-keep)))))
66f23d66
LC
501 (mlet* %store-monad
502 ((os-drv (operating-system-derivation os))
c76b3046 503 (bootcfg (operating-system-bootcfg os)))
9121ce55
MO
504 (qemu-image #:os-drv os-drv
505 #:bootcfg-drv bootcfg
506 #:bootloader (bootloader-configuration-bootloader
507 (operating-system-bootloader os))
66f23d66
LC
508 #:disk-image-size disk-image-size
509 #:file-system-type file-system-type
b4140694 510 #:inputs `(("system" ,os-drv)
07f812c4 511 ("bootcfg" ,bootcfg))
150e20dd 512 #:copy-inputs? #t))))
04086015 513
fcf63cf8
LC
514\f
515;;;
516;;; VMs that share file systems with the host.
517;;;
518
96ffa27b
LC
519(define (file-system->mount-tag fs)
520 "Return a 9p mount tag for host file system FS."
521 ;; QEMU mount tags cannot contain slashes and cannot start with '_'.
522 ;; Compute an identifier that corresponds to the rules.
523 (string-append "TAG"
524 (string-map (match-lambda
525 (#\/ #\_)
526 (chr chr))
527 fs)))
528
fcf63cf8
LC
529(define (mapping->file-system mapping)
530 "Return a 9p file system that realizes MAPPING."
531 (match mapping
532 (($ <file-system-mapping> source target writable?)
533 (file-system
534 (mount-point target)
535 (device (file-system->mount-tag source))
536 (type "9p")
537 (flags (if writable? '() '(read-only)))
538 (options (string-append "trans=virtio"))
539 (check? #f)
540 (create-mount-point? #t)))))
541
909de139 542(define* (virtualized-operating-system os mappings #:optional (full-boot? #f))
83bcd0b8 543 "Return an operating system based on OS suitable for use in a virtualized
fcf63cf8
LC
544environment with the store shared with the host. MAPPINGS is a list of
545<file-system-mapping> to realize in the virtualized OS."
546 (define user-file-systems
547 ;; Remove file systems that conflict with those added below, or that are
548 ;; normally bound to real devices.
549 (remove (lambda (fs)
550 (let ((target (file-system-mount-point fs))
551 (source (file-system-device fs)))
552 (or (string=? target (%store-prefix))
553 (string=? target "/")
29824d80
LC
554 (and (eq? 'device (file-system-title fs))
555 (string-prefix? "/dev/" source)))))
fcf63cf8
LC
556 (operating-system-file-systems os)))
557
909de139
DC
558 (define virtual-file-systems
559 (cons (file-system
560 (mount-point "/")
561 (device "/dev/vda1")
562 (type "ext4"))
563
564 (append (map mapping->file-system mappings)
565 user-file-systems)))
566
83bcd0b8 567 (operating-system (inherit os)
9b396c0c
LC
568
569 ;; XXX: Until we run QEMU with UEFI support (with the OVMF firmware),
570 ;; force the traditional i386/BIOS method.
571 ;; See <https://bugs.gnu.org/28768>.
572 (bootloader (bootloader-configuration
573 (bootloader grub-bootloader)
574 (target "/dev/vda")))
575
52ac153e
LC
576 (initrd (lambda (file-systems . rest)
577 (apply base-initrd file-systems
578 #:volatile-root? #t
579 #:virtio? #t
52ac153e 580 rest)))
65fb4515
LC
581
582 ;; Disable swap.
583 (swap-devices '())
584
909de139
DC
585 ;; XXX: When FULL-BOOT? is true, do not add a 9p mount for /gnu/store
586 ;; since that would lead the bootloader config to look for the kernel and
587 ;; initrd in it.
588 (file-systems (if full-boot?
589 virtual-file-systems
590 (cons
591 (file-system
592 (inherit (mapping->file-system %store-mapping))
593 (needed-for-boot? #t))
594 virtual-file-systems)))))
83bcd0b8 595
fd3bfc44 596(define* (system-qemu-image/shared-store
0b14d1d7 597 os
6aa260af
LC
598 #:key
599 full-boot?
4c0416ae 600 (disk-image-size (* (if full-boot? 500 30) (expt 2 20))))
fd3bfc44 601 "Return a derivation that builds a QEMU image of OS that shares its store
6aa260af
LC
602with the host.
603
604When FULL-BOOT? is true, return an image that does a complete boot sequence,
605bootloaded included; thus, make a disk image that contains everything the
606bootloader refers to: OS kernel, initrd, bootloader data, etc."
607 (mlet* %store-monad ((os-drv (operating-system-derivation os))
c76b3046 608 (bootcfg (operating-system-bootcfg os)))
6aa260af 609 ;; XXX: When FULL-BOOT? is true, we end up creating an image that contains
07f812c4 610 ;; BOOTCFG and all its dependencies, including the output of OS-DRV.
6aa260af
LC
611 ;; This is more than needed (we only need the kernel, initrd, GRUB for its
612 ;; font, and the background image), but it's hard to filter that.
9121ce55
MO
613 (qemu-image #:os-drv os-drv
614 #:bootcfg-drv bootcfg
615 #:bootloader (bootloader-configuration-bootloader
616 (operating-system-bootloader os))
150e20dd 617 #:disk-image-size disk-image-size
6aa260af 618 #:inputs (if full-boot?
07f812c4 619 `(("bootcfg" ,bootcfg))
6aa260af 620 '())
150e20dd
LC
621
622 ;; XXX: Passing #t here is too slow, so let it off by default.
623 #:register-closures? #f
6aa260af 624 #:copy-inputs? full-boot?)))
fd3bfc44 625
96ffa27b
LC
626(define* (common-qemu-options image shared-fs)
627 "Return the a string-value gexp with the common QEMU options to boot IMAGE,
628with '-virtfs' options for the host file systems listed in SHARED-FS."
26a076ed 629
96ffa27b 630 (define (virtfs-option fs)
26a076ed
DC
631 #~(format #f "-virtfs local,path=~s,security_model=none,mount_tag=~s"
632 #$fs #$(file-system->mount-tag fs)))
96ffa27b 633
26a076ed 634 #~(;; Only enable kvm if we see /dev/kvm exists.
944d2b17
CAW
635 ;; This allows users without hardware virtualization to still use these
636 ;; commands.
26a076ed
DC
637 #$@(if (file-exists? "/dev/kvm")
638 '("-enable-kvm")
639 '())
640
641 "-no-reboot"
642 "-net nic,model=virtio"
643
644 #$@(map virtfs-option shared-fs)
645 "-vga std"
646 (format #f "-drive file=~a,if=virtio,cache=writeback,werror=report,readonly"
ebfb71d4 647 #$image)))
3c1f0e3b 648
ab11f0be
LC
649(define* (system-qemu-image/shared-store-script os
650 #:key
651 (qemu qemu)
652 (graphic? #t)
ebfb71d4 653 (memory-size 256)
fcf63cf8 654 (mappings '())
6aa260af
LC
655 full-boot?
656 (disk-image-size
9a1bfe76 657 (* (if full-boot? 500 70)
ed419fa0
LC
658 (expt 2 20)))
659 (options '()))
fd3bfc44 660 "Return a derivation that builds a script to run a virtual machine image of
ebfb71d4
JN
661OS that shares its store with the host. The virtual machine runs with
662MEMORY-SIZE MiB of memory.
6aa260af 663
fcf63cf8
LC
664MAPPINGS is a list of <file-system-mapping> specifying mapping of host file
665systems into the guest.
666
6aa260af
LC
667When FULL-BOOT? is true, the returned script runs everything starting from the
668bootloader; otherwise it directly starts the operating system kernel. The
669DISK-IMAGE-SIZE parameter specifies the size in bytes of the root disk image;
670it is mostly useful when FULL-BOOT? is true."
909de139 671 (mlet* %store-monad ((os -> (virtualized-operating-system os mappings full-boot?))
6aa260af
LC
672 (os-drv (operating-system-derivation os))
673 (image (system-qemu-image/shared-store
674 os
675 #:full-boot? full-boot?
676 #:disk-image-size disk-image-size)))
26a076ed 677 (define kernel-arguments
83071b05
DM
678 #~(list #$@(if graphic? #~() #~("console=ttyS0"))
679 #+@(operating-system-kernel-arguments os os-drv "/dev/vda1")))
26a076ed
DC
680
681 (define qemu-exec
682 #~(list (string-append #$qemu "/bin/" #$(qemu-command (%current-system)))
683 #$@(if full-boot?
684 #~()
685 #~("-kernel" #$(operating-system-kernel-file os)
686 "-initrd" #$(file-append os-drv "/initrd")
687 (format #f "-append ~s"
688 (string-join #$kernel-arguments " "))))
689 #$@(common-qemu-options image
690 (map file-system-mapping-source
ebfb71d4 691 (cons %store-mapping mappings)))
ed419fa0
LC
692 "-m " (number->string #$memory-size)
693 #$@options))
26a076ed 694
fd3bfc44 695 (define builder
02100028
LC
696 #~(call-with-output-file #$output
697 (lambda (port)
26a076ed
DC
698 (format port "#!~a~% exec ~a \"$@\"~%"
699 #$(file-append bash "/bin/sh")
700 (string-join #$qemu-exec " "))
02100028
LC
701 (chmod port #o555))))
702
703 (gexp->derivation "run-vm.sh" builder)))
fd3bfc44 704
ed419fa0
LC
705\f
706;;;
707;;; High-level abstraction.
708;;;
709
710(define-record-type* <virtual-machine> %virtual-machine
711 make-virtual-machine
712 virtual-machine?
713 (operating-system virtual-machine-operating-system) ;<operating-system>
714 (qemu virtual-machine-qemu ;<package>
715 (default qemu))
716 (graphic? virtual-machine-graphic? ;Boolean
717 (default #f))
718 (memory-size virtual-machine-memory-size ;integer (MiB)
719 (default 256))
eb152070
CB
720 (disk-image-size virtual-machine-disk-image-size ;integer (bytes)
721 (default 'guess))
ed419fa0
LC
722 (port-forwardings virtual-machine-port-forwardings ;list of integer pairs
723 (default '())))
724
725(define-syntax virtual-machine
726 (syntax-rules ()
727 "Declare a virtual machine running the specified OS, with the given
728options."
729 ((_ os) ;shortcut
730 (%virtual-machine (operating-system os)))
731 ((_ fields ...)
732 (%virtual-machine fields ...))))
733
734(define (port-forwardings->qemu-options forwardings)
735 "Return the QEMU option for the given port FORWARDINGS as a string, where
736FORWARDINGS is a list of host-port/guest-port pairs."
737 (string-join
738 (map (match-lambda
739 ((host-port . guest-port)
740 (string-append "hostfwd=tcp::"
741 (number->string host-port)
742 "-:" (number->string guest-port))))
743 forwardings)
744 ","))
745
746(define-gexp-compiler (virtual-machine-compiler (vm <virtual-machine>)
747 system target)
748 ;; XXX: SYSTEM and TARGET are ignored.
749 (match vm
eb152070 750 (($ <virtual-machine> os qemu graphic? memory-size disk-image-size ())
ed419fa0
LC
751 (system-qemu-image/shared-store-script os
752 #:qemu qemu
753 #:graphic? graphic?
eb152070
CB
754 #:memory-size memory-size
755 #:disk-image-size
756 disk-image-size))
757 (($ <virtual-machine> os qemu graphic? memory-size disk-image-size
758 forwardings)
ed419fa0
LC
759 (let ((options
760 `("-net" ,(string-append
761 "user,"
762 (port-forwardings->qemu-options forwardings)))))
763 (system-qemu-image/shared-store-script os
764 #:qemu qemu
765 #:graphic? graphic?
766 #:memory-size memory-size
eb152070
CB
767 #:disk-image-size
768 disk-image-size
ed419fa0
LC
769 #:options options)))))
770
04086015 771;;; vm.scm ends here