gnu: monero-core: Fix missing dependency.
[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 425 (initrd (lambda (file-systems . rest)
b8e77811
MO
426 (apply (operating-system-initrd os)
427 file-systems
52ac153e 428 #:volatile-root? #t
52ac153e 429 rest)))
1e77fedb 430
cf189709
DM
431 (bootloader (if (string=? "iso9660" file-system-type)
432 (bootloader-configuration
433 (inherit (operating-system-bootloader os))
434 (bootloader grub-mkrescue-bootloader))
435 (operating-system-bootloader os)))
436
1e77fedb
LC
437 ;; Force our own root file system.
438 (file-systems (cons (file-system
439 (mount-point "/")
5f7fe1c5
LC
440 (device root-uuid)
441 (title 'uuid)
1e77fedb
LC
442 (type file-system-type))
443 file-systems-to-keep)))))
444
445 (mlet* %store-monad ((os-drv (operating-system-derivation os))
c76b3046 446 (bootcfg (operating-system-bootcfg os)))
be1033a3
DM
447 (if (string=? "iso9660" file-system-type)
448 (iso9660-image #:name name
acc0f6bb 449 #:file-system-label root-label
5f7fe1c5 450 #:file-system-uuid root-uuid
be1033a3 451 #:os-drv os-drv
b069111f 452 #:register-closures? #t
be1033a3
DM
453 #:bootcfg-drv bootcfg
454 #:bootloader (bootloader-configuration-bootloader
455 (operating-system-bootloader os))
456 #:inputs `(("system" ,os-drv)
457 ("bootcfg" ,bootcfg)))
458 (qemu-image #:name name
459 #:os-drv os-drv
460 #:bootcfg-drv bootcfg
461 #:bootloader (bootloader-configuration-bootloader
462 (operating-system-bootloader os))
463 #:disk-image-size disk-image-size
464 #:disk-image-format "raw"
4138e782 465 #:file-system-type file-system-type
be1033a3 466 #:file-system-label root-label
5f7fe1c5 467 #:file-system-uuid root-uuid
be1033a3
DM
468 #:copy-inputs? #t
469 #:register-closures? #t
470 #:inputs `(("system" ,os-drv)
471 ("bootcfg" ,bootcfg)))))))
1e77fedb 472
0b14d1d7 473(define* (system-qemu-image os
66f23d66
LC
474 #:key
475 (file-system-type "ext4")
476 (disk-image-size (* 900 (expt 2 20))))
477 "Return the derivation of a freestanding QEMU image of DISK-IMAGE-SIZE bytes
478of the GNU system as described by OS."
1eeccc2f
LC
479 (define file-systems-to-keep
480 ;; Keep only file systems other than root and not normally bound to real
481 ;; devices.
482 (remove (lambda (fs)
483 (let ((target (file-system-mount-point fs))
484 (source (file-system-device fs)))
485 (or (string=? target "/")
486 (string-prefix? "/dev/" source))))
487 (operating-system-file-systems os)))
488
66f23d66 489 (let ((os (operating-system (inherit os)
e84d8b30 490 ;; Use an initrd with the whole QEMU shebang.
52ac153e 491 (initrd (lambda (file-systems . rest)
b8e77811
MO
492 (apply (operating-system-initrd os)
493 file-systems
52ac153e 494 #:virtio? #t
52ac153e 495 rest)))
e84d8b30 496
1eeccc2f
LC
497 ;; Force our own root file system.
498 (file-systems (cons (file-system
66f23d66
LC
499 (mount-point "/")
500 (device "/dev/sda1")
1eeccc2f
LC
501 (type file-system-type))
502 file-systems-to-keep)))))
66f23d66
LC
503 (mlet* %store-monad
504 ((os-drv (operating-system-derivation os))
c76b3046 505 (bootcfg (operating-system-bootcfg os)))
9121ce55
MO
506 (qemu-image #:os-drv os-drv
507 #:bootcfg-drv bootcfg
508 #:bootloader (bootloader-configuration-bootloader
509 (operating-system-bootloader os))
66f23d66
LC
510 #:disk-image-size disk-image-size
511 #:file-system-type file-system-type
b4140694 512 #:inputs `(("system" ,os-drv)
07f812c4 513 ("bootcfg" ,bootcfg))
150e20dd 514 #:copy-inputs? #t))))
04086015 515
fcf63cf8
LC
516\f
517;;;
518;;; VMs that share file systems with the host.
519;;;
520
96ffa27b
LC
521(define (file-system->mount-tag fs)
522 "Return a 9p mount tag for host file system FS."
523 ;; QEMU mount tags cannot contain slashes and cannot start with '_'.
524 ;; Compute an identifier that corresponds to the rules.
525 (string-append "TAG"
526 (string-map (match-lambda
527 (#\/ #\_)
528 (chr chr))
529 fs)))
530
fcf63cf8
LC
531(define (mapping->file-system mapping)
532 "Return a 9p file system that realizes MAPPING."
533 (match mapping
534 (($ <file-system-mapping> source target writable?)
535 (file-system
536 (mount-point target)
537 (device (file-system->mount-tag source))
538 (type "9p")
539 (flags (if writable? '() '(read-only)))
540 (options (string-append "trans=virtio"))
541 (check? #f)
542 (create-mount-point? #t)))))
543
909de139 544(define* (virtualized-operating-system os mappings #:optional (full-boot? #f))
83bcd0b8 545 "Return an operating system based on OS suitable for use in a virtualized
fcf63cf8
LC
546environment with the store shared with the host. MAPPINGS is a list of
547<file-system-mapping> to realize in the virtualized OS."
548 (define user-file-systems
549 ;; Remove file systems that conflict with those added below, or that are
550 ;; normally bound to real devices.
551 (remove (lambda (fs)
552 (let ((target (file-system-mount-point fs))
553 (source (file-system-device fs)))
554 (or (string=? target (%store-prefix))
555 (string=? target "/")
29824d80
LC
556 (and (eq? 'device (file-system-title fs))
557 (string-prefix? "/dev/" source)))))
fcf63cf8
LC
558 (operating-system-file-systems os)))
559
909de139
DC
560 (define virtual-file-systems
561 (cons (file-system
562 (mount-point "/")
563 (device "/dev/vda1")
564 (type "ext4"))
565
566 (append (map mapping->file-system mappings)
567 user-file-systems)))
568
83bcd0b8 569 (operating-system (inherit os)
9b396c0c
LC
570
571 ;; XXX: Until we run QEMU with UEFI support (with the OVMF firmware),
572 ;; force the traditional i386/BIOS method.
573 ;; See <https://bugs.gnu.org/28768>.
574 (bootloader (bootloader-configuration
575 (bootloader grub-bootloader)
576 (target "/dev/vda")))
577
52ac153e 578 (initrd (lambda (file-systems . rest)
b8e77811
MO
579 (apply (operating-system-initrd os)
580 file-systems
52ac153e
LC
581 #:volatile-root? #t
582 #:virtio? #t
52ac153e 583 rest)))
65fb4515
LC
584
585 ;; Disable swap.
586 (swap-devices '())
587
909de139
DC
588 ;; XXX: When FULL-BOOT? is true, do not add a 9p mount for /gnu/store
589 ;; since that would lead the bootloader config to look for the kernel and
590 ;; initrd in it.
591 (file-systems (if full-boot?
592 virtual-file-systems
593 (cons
594 (file-system
595 (inherit (mapping->file-system %store-mapping))
596 (needed-for-boot? #t))
597 virtual-file-systems)))))
83bcd0b8 598
fd3bfc44 599(define* (system-qemu-image/shared-store
0b14d1d7 600 os
6aa260af
LC
601 #:key
602 full-boot?
4c0416ae 603 (disk-image-size (* (if full-boot? 500 30) (expt 2 20))))
fd3bfc44 604 "Return a derivation that builds a QEMU image of OS that shares its store
6aa260af
LC
605with the host.
606
607When FULL-BOOT? is true, return an image that does a complete boot sequence,
608bootloaded included; thus, make a disk image that contains everything the
609bootloader refers to: OS kernel, initrd, bootloader data, etc."
610 (mlet* %store-monad ((os-drv (operating-system-derivation os))
c76b3046 611 (bootcfg (operating-system-bootcfg os)))
6aa260af 612 ;; XXX: When FULL-BOOT? is true, we end up creating an image that contains
07f812c4 613 ;; BOOTCFG and all its dependencies, including the output of OS-DRV.
6aa260af
LC
614 ;; This is more than needed (we only need the kernel, initrd, GRUB for its
615 ;; font, and the background image), but it's hard to filter that.
9121ce55
MO
616 (qemu-image #:os-drv os-drv
617 #:bootcfg-drv bootcfg
618 #:bootloader (bootloader-configuration-bootloader
619 (operating-system-bootloader os))
150e20dd 620 #:disk-image-size disk-image-size
6aa260af 621 #:inputs (if full-boot?
07f812c4 622 `(("bootcfg" ,bootcfg))
6aa260af 623 '())
150e20dd
LC
624
625 ;; XXX: Passing #t here is too slow, so let it off by default.
626 #:register-closures? #f
6aa260af 627 #:copy-inputs? full-boot?)))
fd3bfc44 628
96ffa27b
LC
629(define* (common-qemu-options image shared-fs)
630 "Return the a string-value gexp with the common QEMU options to boot IMAGE,
631with '-virtfs' options for the host file systems listed in SHARED-FS."
26a076ed 632
96ffa27b 633 (define (virtfs-option fs)
26a076ed
DC
634 #~(format #f "-virtfs local,path=~s,security_model=none,mount_tag=~s"
635 #$fs #$(file-system->mount-tag fs)))
96ffa27b 636
26a076ed 637 #~(;; Only enable kvm if we see /dev/kvm exists.
944d2b17
CAW
638 ;; This allows users without hardware virtualization to still use these
639 ;; commands.
26a076ed
DC
640 #$@(if (file-exists? "/dev/kvm")
641 '("-enable-kvm")
642 '())
643
644 "-no-reboot"
645 "-net nic,model=virtio"
646
647 #$@(map virtfs-option shared-fs)
648 "-vga std"
649 (format #f "-drive file=~a,if=virtio,cache=writeback,werror=report,readonly"
ebfb71d4 650 #$image)))
3c1f0e3b 651
ab11f0be
LC
652(define* (system-qemu-image/shared-store-script os
653 #:key
654 (qemu qemu)
655 (graphic? #t)
ebfb71d4 656 (memory-size 256)
fcf63cf8 657 (mappings '())
6aa260af
LC
658 full-boot?
659 (disk-image-size
9a1bfe76 660 (* (if full-boot? 500 70)
ed419fa0
LC
661 (expt 2 20)))
662 (options '()))
fd3bfc44 663 "Return a derivation that builds a script to run a virtual machine image of
ebfb71d4
JN
664OS that shares its store with the host. The virtual machine runs with
665MEMORY-SIZE MiB of memory.
6aa260af 666
fcf63cf8
LC
667MAPPINGS is a list of <file-system-mapping> specifying mapping of host file
668systems into the guest.
669
6aa260af
LC
670When FULL-BOOT? is true, the returned script runs everything starting from the
671bootloader; otherwise it directly starts the operating system kernel. The
672DISK-IMAGE-SIZE parameter specifies the size in bytes of the root disk image;
673it is mostly useful when FULL-BOOT? is true."
909de139 674 (mlet* %store-monad ((os -> (virtualized-operating-system os mappings full-boot?))
6aa260af
LC
675 (os-drv (operating-system-derivation os))
676 (image (system-qemu-image/shared-store
677 os
678 #:full-boot? full-boot?
679 #:disk-image-size disk-image-size)))
26a076ed 680 (define kernel-arguments
83071b05
DM
681 #~(list #$@(if graphic? #~() #~("console=ttyS0"))
682 #+@(operating-system-kernel-arguments os os-drv "/dev/vda1")))
26a076ed
DC
683
684 (define qemu-exec
685 #~(list (string-append #$qemu "/bin/" #$(qemu-command (%current-system)))
686 #$@(if full-boot?
687 #~()
688 #~("-kernel" #$(operating-system-kernel-file os)
689 "-initrd" #$(file-append os-drv "/initrd")
690 (format #f "-append ~s"
691 (string-join #$kernel-arguments " "))))
692 #$@(common-qemu-options image
693 (map file-system-mapping-source
ebfb71d4 694 (cons %store-mapping mappings)))
ed419fa0
LC
695 "-m " (number->string #$memory-size)
696 #$@options))
26a076ed 697
fd3bfc44 698 (define builder
02100028
LC
699 #~(call-with-output-file #$output
700 (lambda (port)
26a076ed
DC
701 (format port "#!~a~% exec ~a \"$@\"~%"
702 #$(file-append bash "/bin/sh")
703 (string-join #$qemu-exec " "))
02100028
LC
704 (chmod port #o555))))
705
706 (gexp->derivation "run-vm.sh" builder)))
fd3bfc44 707
ed419fa0
LC
708\f
709;;;
710;;; High-level abstraction.
711;;;
712
713(define-record-type* <virtual-machine> %virtual-machine
714 make-virtual-machine
715 virtual-machine?
716 (operating-system virtual-machine-operating-system) ;<operating-system>
717 (qemu virtual-machine-qemu ;<package>
718 (default qemu))
719 (graphic? virtual-machine-graphic? ;Boolean
720 (default #f))
721 (memory-size virtual-machine-memory-size ;integer (MiB)
722 (default 256))
eb152070
CB
723 (disk-image-size virtual-machine-disk-image-size ;integer (bytes)
724 (default 'guess))
ed419fa0
LC
725 (port-forwardings virtual-machine-port-forwardings ;list of integer pairs
726 (default '())))
727
728(define-syntax virtual-machine
729 (syntax-rules ()
730 "Declare a virtual machine running the specified OS, with the given
731options."
732 ((_ os) ;shortcut
733 (%virtual-machine (operating-system os)))
734 ((_ fields ...)
735 (%virtual-machine fields ...))))
736
737(define (port-forwardings->qemu-options forwardings)
738 "Return the QEMU option for the given port FORWARDINGS as a string, where
739FORWARDINGS is a list of host-port/guest-port pairs."
740 (string-join
741 (map (match-lambda
742 ((host-port . guest-port)
743 (string-append "hostfwd=tcp::"
744 (number->string host-port)
745 "-:" (number->string guest-port))))
746 forwardings)
747 ","))
748
749(define-gexp-compiler (virtual-machine-compiler (vm <virtual-machine>)
750 system target)
751 ;; XXX: SYSTEM and TARGET are ignored.
752 (match vm
eb152070 753 (($ <virtual-machine> os qemu graphic? memory-size disk-image-size ())
ed419fa0
LC
754 (system-qemu-image/shared-store-script os
755 #:qemu qemu
756 #:graphic? graphic?
eb152070
CB
757 #:memory-size memory-size
758 #:disk-image-size
759 disk-image-size))
760 (($ <virtual-machine> os qemu graphic? memory-size disk-image-size
761 forwardings)
ed419fa0
LC
762 (let ((options
763 `("-net" ,(string-append
764 "user,"
765 (port-forwardings->qemu-options forwardings)))))
766 (system-qemu-image/shared-store-script os
767 #:qemu qemu
768 #:graphic? graphic?
769 #:memory-size memory-size
eb152070
CB
770 #:disk-image-size
771 disk-image-size
ed419fa0
LC
772 #:options options)))))
773
04086015 774;;; vm.scm ends here