hurd-boot: Further cleanup of "rc".
[jackhill/guix/guix.git] / gnu / system / vm.scm
CommitLineData
04086015 1;;; GNU Guix --- Functional package management for GNU
09844816 2;;; Copyright © 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020 Ludovic Courtès <ludo@gnu.org>
944d2b17 3;;; Copyright © 2016 Christopher Allan Webber <cwebber@dustycloud.org>
2ca712bd 4;;; Copyright © 2016, 2017 Leo Famulari <leo@famulari.name>
07f812c4 5;;; Copyright © 2017 Mathieu Othacehe <m.othacehe@gmail.com>
ecf5d537 6;;; Copyright © 2017 Marius Bakke <mbakke@fastmail.com>
8c9bf294 7;;; Copyright © 2018 Chris Marusich <cmmarusich@gmail.com>
04086015
LC
8;;;
9;;; This file is part of GNU Guix.
10;;;
11;;; GNU Guix is free software; you can redistribute it and/or modify it
12;;; under the terms of the GNU General Public License as published by
13;;; the Free Software Foundation; either version 3 of the License, or (at
14;;; your option) any later version.
15;;;
16;;; GNU Guix is distributed in the hope that it will be useful, but
17;;; WITHOUT ANY WARRANTY; without even the implied warranty of
18;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19;;; GNU General Public License for more details.
20;;;
21;;; You should have received a copy of the GNU General Public License
22;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
23
24(define-module (gnu system vm)
93d44bd8 25 #:use-module (guix config)
04086015 26 #:use-module (guix store)
02100028 27 #:use-module (guix gexp)
04086015
LC
28 #:use-module (guix derivations)
29 #:use-module (guix packages)
d9f0a237 30 #:use-module (guix monads)
fcf63cf8 31 #:use-module (guix records)
239c6e27 32 #:use-module (guix modules)
00e39b2e 33 #:use-module (guix utils)
ca719424 34 #:use-module (gcrypt hash)
dffc5ab5 35 #:use-module (guix base32)
c45477d2 36 #:use-module ((guix self) #:select (make-config.scm))
fcf63cf8 37
548f7a8f 38 #:use-module ((gnu build vm)
66670cf3 39 #:select (qemu-command))
bdb36958 40 #:use-module (gnu packages base)
862e38d5 41 #:use-module (gnu packages bootloaders)
be1033a3 42 #:use-module (gnu packages cdrom)
a335f6fc 43 #:use-module (gnu packages compression)
1b89a66e 44 #:use-module (gnu packages guile)
ca719424 45 #:autoload (gnu packages gnupg) (guile-gcrypt)
bdb36958 46 #:use-module (gnu packages gawk)
1b89a66e 47 #:use-module (gnu packages bash)
4f62d8d6 48 #:use-module (gnu packages less)
59132b80 49 #:use-module (gnu packages virtualization)
cc4a2aeb 50 #:use-module (gnu packages disk)
5b16ff09 51 #:use-module (gnu packages zile)
04086015 52 #:use-module (gnu packages linux)
9de46ffb 53 #:use-module (gnu packages admin)
0ded70f3 54
9121ce55 55 #:use-module (gnu bootloader)
9b396c0c 56 #:use-module (gnu bootloader grub)
0ded70f3 57 #:use-module (gnu system shadow)
6e828634 58 #:use-module (gnu system pam)
69cae3d3 59 #:use-module (gnu system linux-container)
735c6dd7 60 #:use-module (gnu system linux-initrd)
b09a8da4 61 #:use-module (gnu bootloader)
c5df1839 62 #:use-module (gnu system file-systems)
033adfe7 63 #:use-module (gnu system)
db4fdc04 64 #:use-module (gnu services)
d03de6be 65 #:use-module (gnu services base)
9b336338 66 #:use-module (gnu system uuid)
0ded70f3 67
ca85d7bc 68 #:use-module (srfi srfi-1)
04086015 69 #:use-module (srfi srfi-26)
5f7fe1c5 70 #:use-module (rnrs bytevectors)
04086015 71 #:use-module (ice-9 match)
0ded70f3 72
04086015 73 #:export (expression->derivation-in-linux-vm
aedb72fb 74 qemu-image
e9f693d0 75 virtualized-operating-system
fd3bfc44 76 system-qemu-image
fcf63cf8 77
fd3bfc44 78 system-qemu-image/shared-store
1e77fedb 79 system-qemu-image/shared-store-script
f19cf27c 80 system-disk-image-in-vm
a335f6fc 81 system-docker-image
ed419fa0
LC
82
83 virtual-machine
84 virtual-machine?))
04086015
LC
85
86\f
87;;; Commentary:
88;;;
89;;; Tools to evaluate build expressions within virtual machines.
90;;;
91;;; Code:
92
83bcd0b8 93(define %linux-vm-file-systems
8c9bf294
CM
94 ;; File systems mounted for 'derivation-in-linux-vm'. These are shared with
95 ;; the host over 9p.
66ec3895
LC
96 ;;
97 ;; The 9p documentation says that cache=loose is "intended for exclusive,
98 ;; read-only mounts", without additional details. It's much faster than the
99 ;; default cache=none, especially when copying and registering store items.
100 ;; Thus, use cache=loose, except for /xchg where we want to ensure
101 ;; consistency.
83bcd0b8
LC
102 (list (file-system
103 (mount-point (%store-prefix))
104 (device "store")
105 (type "9p")
106 (needed-for-boot? #t)
fce22547
LC
107 (flags '(read-only))
108 (options "trans=virtio,cache=loose")
3c05b4bc 109 (check? #f))
83bcd0b8
LC
110 (file-system
111 (mount-point "/xchg")
112 (device "xchg")
113 (type "9p")
114 (needed-for-boot? #t)
66ec3895 115 (options "trans=virtio")
8c9bf294
CM
116 (check? #f))
117 (file-system
118 (mount-point "/tmp")
119 (device "tmp")
120 (type "9p")
121 (needed-for-boot? #t)
fce22547 122 (options "trans=virtio,cache=loose")
3c05b4bc 123 (check? #f))))
83bcd0b8 124
c45477d2
LC
125(define not-config?
126 ;; Select (guix …) and (gnu …) modules, except (guix config).
127 (match-lambda
128 (('guix 'config) #f)
129 (('guix rest ...) #t)
130 (('gnu rest ...) #t)
131 (rest #f)))
132
ca719424
LC
133(define gcrypt-sqlite3&co
134 ;; Guile-Gcrypt, Guile-SQLite3, and their propagated inputs.
135 (append-map (lambda (package)
136 (cons package
910d0121
LC
137 (match (package-transitive-propagated-inputs package)
138 (((labels packages) ...)
139 packages))))
ca719424 140 (list guile-gcrypt guile-sqlite3)))
c45477d2 141
d9f0a237 142(define* (expression->derivation-in-linux-vm name exp
04086015 143 #:key
b3477234 144 (system (%current-system))
04086015 145 (linux linux-libre)
735c6dd7 146 initrd
06da1a6b 147 (qemu qemu-minimal)
04086015 148 (env-vars '())
04086015
LC
149 (guile-for-build
150 (%guile-for-build))
50e53c1c
LC
151 (file-systems
152 %linux-vm-file-systems)
04086015 153
8d033e3e 154 (single-file-output? #f)
04086015 155 (make-disk-image? #f)
ca85d7bc 156 (references-graphs #f)
defa1b9b 157 (memory-size 256)
c4a74364 158 (disk-image-format "qcow2")
a328f66a
LC
159 (disk-image-size 'guess)
160
161 (substitutable? #t))
735c6dd7 162 "Evaluate EXP in a QEMU virtual machine running LINUX with INITRD (a
8d033e3e 163derivation). The virtual machine runs with MEMORY-SIZE MiB of memory. In the
50e53c1c
LC
164virtual machine, EXP has access to FILE-SYSTEMS, which, by default, includes a
1659p share of the store, the '/xchg' where EXP should put its output file(s),
166and a 9p share of /tmp.
8d033e3e
LC
167
168If SINGLE-FILE-OUTPUT? is true, copy a single file from '/xchg' to OUTPUT.
169Otherwise, copy the contents of /xchg to a new directory OUTPUT.
04086015 170
c4a74364
LC
171When MAKE-DISK-IMAGE? is true, then create a QEMU disk image of type
172DISK-IMAGE-FORMAT (e.g., 'qcow2' or 'raw'), of DISK-IMAGE-SIZE bytes and
a8ac4f08
LC
173return it. When DISK-IMAGE-SIZE is 'guess, estimate the image size based
174based on the size of the closure of REFERENCES-GRAPHS.
ca85d7bc
LC
175
176When REFERENCES-GRAPHS is true, it must be a list of file name/store path
177pairs, as for `derivation'. The files containing the reference graphs are
a328f66a
LC
178made available under the /xchg CIFS share.
179
180SUBSTITUTABLE? determines whether the returned derivation should be marked as
181substitutable."
be43c08b
LC
182 (define user-builder
183 (program-file "builder-in-linux-vm" exp))
184
185 (define loader
186 ;; Invoke USER-BUILDER instead using 'primitive-load'. The reason for
187 ;; this is to allow USER-BUILDER to dlopen stuff by using a full-featured
188 ;; Guile, which it couldn't do using the statically-linked guile used in
189 ;; the initrd. See example at
190 ;; <https://lists.gnu.org/archive/html/guix-devel/2017-10/msg00233.html>.
191 (program-file "linux-vm-loader"
be6520e6
LC
192 ;; Communicate USER-BUILDER's exit status via /xchg so that
193 ;; the host can distinguish between success, failure, and
194 ;; kernel panic.
195 #~(let ((status (system* #$user-builder)))
196 (call-with-output-file "/xchg/.exit-status"
197 (lambda (port)
198 (write status port)))
199 (sync)
200 (reboot))))
be43c08b 201
502f609d
LC
202 (define-syntax-rule (check predicate)
203 (let-system (system target)
204 (predicate (or target system))))
205
e34ae75d
LC
206 (let ((initrd (or initrd
207 (base-initrd file-systems
208 #:on-error 'backtrace
209 #:linux linux
210 #:linux-modules %base-initrd-modules
211 #:qemu-networking? #t))))
1aa0033b
LC
212
213 (define builder
214 ;; Code that launches the VM that evaluates EXP.
ca719424 215 (with-extensions gcrypt-sqlite3&co
c45477d2
LC
216 (with-imported-modules `(,@(source-module-closure
217 '((guix build utils)
218 (gnu build vm))
219 #:select? not-config?)
ca719424
LC
220
221 ;; For consumption by (gnu store database).
222 ((guix config) => ,(make-config.scm)))
c45477d2
LC
223 #~(begin
224 (use-modules (guix build utils)
225 (gnu build vm))
226
d4ddf22d
MO
227 (let* ((native-inputs
228 '#+(list qemu (canonical-package coreutils)))
b3477234
LC
229 (linux (string-append
230 #+linux "/"
231 #+(system-linux-image-file-name system)))
232 (initrd #+initrd)
233 (loader #+loader)
c45477d2
LC
234 (graphs '#$(match references-graphs
235 (((graph-files . _) ...) graph-files)
236 (_ #f)))
502f609d
LC
237 (target #$(let-system (system target)
238 (or target system)))
c45477d2
LC
239 (size #$(if (eq? 'guess disk-image-size)
240 #~(+ (* 70 (expt 2 20)) ;ESP
241 (estimated-partition-size graphs))
242 disk-image-size)))
243
d4ddf22d 244 (set-path-environment-variable "PATH" '("bin") native-inputs)
c45477d2
LC
245
246 (load-in-linux-vm loader
247 #:output #$output
248 #:linux linux #:initrd initrd
d4ddf22d 249 #:qemu (qemu-command target)
c45477d2
LC
250 #:memory-size #$memory-size
251 #:make-disk-image? #$make-disk-image?
252 #:single-file-output? #$single-file-output?
c45477d2
LC
253 #:disk-image-format #$disk-image-format
254 #:disk-image-size size
255 #:references-graphs graphs))))))
1aa0033b
LC
256
257 (gexp->derivation name builder
258 ;; TODO: Require the "kvm" feature.
259 #:system system
b3477234 260 #:target #f ;EXP is always executed natively
1aa0033b 261 #:env-vars env-vars
1aa0033b 262 #:guile-for-build guile-for-build
a328f66a
LC
263 #:references-graphs references-graphs
264 #:substitutable? substitutable?)))
d9f0a237 265
d03de6be
MC
266(define (has-guix-service-type? os)
267 "Return true if OS contains a service of the type GUIX-SERVICE-TYPE."
268 (not (not (find (lambda (service)
269 (eq? (service-kind service) guix-service-type))
270 (operating-system-services os)))))
271
d9f0a237 272(define* (qemu-image #:key
04086015
LC
273 (name "qemu-image")
274 (system (%current-system))
d4ddf22d 275 (target (%current-target-system))
06da1a6b 276 (qemu qemu-minimal)
a8ac4f08 277 (disk-image-size 'guess)
c4a74364 278 (disk-image-format "qcow2")
03ddfaf5 279 (file-system-type "ext4")
4d1ff68d 280 (file-system-options '())
cd45d656 281 (device-nodes 'linux)
82782d8c 282 (extra-directives '())
ef9fc40d 283 file-system-label
fd3b4b98 284 file-system-uuid
8bff7dc2 285 os
9121ce55
MO
286 bootcfg-drv
287 bootloader
d03de6be 288 (register-closures? (has-guix-service-type? os))
150e20dd 289 (inputs '())
a328f66a
LC
290 copy-inputs?
291 (substitutable? #t))
c4a74364 292 "Return a bootable, stand-alone QEMU image of type DISK-IMAGE-FORMAT (e.g.,
ef9fc40d
LC
293'qcow2' or 'raw'), with a root partition of type FILE-SYSTEM-TYPE.
294Optionally, FILE-SYSTEM-LABEL can be specified as the volume name for the root
fd3b4b98 295partition; likewise FILE-SYSTEM-UUID, if true, specifies the UUID of the root
4d1ff68d
LC
296partition (a UUID object). FILE-SYSTEM-OPTIONS is an optional list of
297command-line options passed to 'mkfs.ext4' (or similar).
fd3b4b98
LC
298
299The returned image is a full disk image that runs OS-DERIVATION,
f2c403ea
LC
300with a GRUB installation that uses GRUB-CONFIGURATION as its configuration
301file (GRUB-CONFIGURATION must be the name of a file in the VM.)
93d44bd8 302
150e20dd
LC
303INPUTS is a list of inputs (as for packages). When COPY-INPUTS? is true, copy
304all of INPUTS into the image being built. When REGISTER-CLOSURES? is true,
305register INPUTS in the store database of the image so that Guix can be used in
d03de6be
MC
306the image. By default, REGISTER-CLOSURES? is set to true only if a service of
307type GUIX-SERVICE-TYPE is present in the services definition of the operating
82782d8c
LC
308system.
309
cd45d656
LC
310When DEVICE-NODES is 'linux, create Linux-device block and character devices
311under /dev. When it is 'hurd, do Hurdish things.
312
82782d8c
LC
313EXTRA-DIRECTIVES is an optional list of directives to populate the root file
314system that is passed to 'populate-root-file-system'."
c45477d2
LC
315 (define schema
316 (and register-closures?
317 (local-file (search-path %load-path
318 "guix/store/schema.sql"))))
319
52c1b90a
LC
320 (define preserve-target
321 (if target
322 (lambda (obj)
323 (with-parameters ((%current-target-system target))
324 obj))
325 identity))
326
327 (define inputs*
328 (map (match-lambda
329 ((name thing)
330 `(,name ,(preserve-target thing)))
331 ((name thing output)
332 `(,name ,(preserve-target thing) ,output)))
333 inputs))
334
b53833b2
LC
335 (expression->derivation-in-linux-vm
336 name
ca719424 337 (with-extensions gcrypt-sqlite3&co
c45477d2
LC
338 (with-imported-modules `(,@(source-module-closure '((gnu build vm)
339 (gnu build bootloader)
340 (guix store database)
341 (guix build utils))
342 #:select? not-config?)
ca719424 343 ((guix config) => ,(make-config.scm)))
c45477d2
LC
344 #~(begin
345 (use-modules (gnu build bootloader)
346 (gnu build vm)
b37c5441
JN
347 ((gnu build hurd-boot)
348 #:select (make-hurd-device-nodes))
cd45d656 349 ((gnu build linux-boot)
b37c5441 350 #:select (make-essential-device-nodes))
c45477d2
LC
351 (guix store database)
352 (guix build utils)
353 (srfi srfi-26)
354 (ice-9 binary-ports))
355
356 (sql-schema #$schema)
357
9c941364
LC
358 ;; Allow non-ASCII file names--e.g., 'nss-certs'--to be decoded.
359 (setenv "GUIX_LOCPATH"
360 #+(file-append glibc-utf8-locales "/lib/locale"))
361 (setlocale LC_ALL "en_US.utf8")
362
c45477d2 363 (let ((inputs
96cb3f8a 364 '#+(append (list parted e2fsprogs dosfstools)
c45477d2
LC
365 (map canonical-package
366 (list sed grep coreutils findutils gawk))))
367
368 ;; This variable is unused but allows us to add INPUTS-TO-COPY
369 ;; as inputs.
370 (to-register
371 '#$(map (match-lambda
372 ((name thing) thing)
373 ((name thing output) `(,thing ,output)))
52c1b90a 374 inputs*)))
c45477d2
LC
375
376 (set-path-environment-variable "PATH" '("bin" "sbin") inputs)
377
378 (let* ((graphs '#$(match inputs
379 (((names . _) ...)
380 names)))
381 (initialize (root-partition-initializer
82782d8c 382 #:extra-directives '#$extra-directives
c45477d2
LC
383 #:closures graphs
384 #:copy-closures? #$copy-inputs?
385 #:register-closures? #$register-closures?
52c1b90a 386 #:system-directory #$(preserve-target os)
b5460d95 387
cd45d656
LC
388 #:make-device-nodes
389 #$(match device-nodes
390 ('linux #~make-essential-device-nodes)
391 ('hurd #~make-hurd-device-nodes))
392
b5460d95
LC
393 ;; Disable deduplication to speed things up,
394 ;; and because it doesn't help much for a
395 ;; single system generation.
396 #:deduplicate? #f))
c45477d2
LC
397 (root-size #$(if (eq? 'guess disk-image-size)
398 #~(max
399 ;; Minimum 20 MiB root size
400 (* 20 (expt 2 20))
401 (estimated-partition-size
402 (map (cut string-append "/xchg/" <>)
403 graphs)))
404 (- disk-image-size
405 (* 50 (expt 2 20)))))
406 (partitions
407 (append
408 (list (partition
409 (size root-size)
410 (label #$file-system-label)
411 (uuid #$(and=> file-system-uuid
412 uuid-bytevector))
413 (file-system #$file-system-type)
4d1ff68d 414 (file-system-options '#$file-system-options)
c45477d2
LC
415 (flags '(boot))
416 (initializer initialize)))
417 ;; Append a small EFI System Partition for use with UEFI
418 ;; bootloaders if we are not targeting ARM because UEFI
419 ;; support in U-Boot is experimental.
420 ;;
1ee72bb5 421 ;; FIXME: ‘target-arm?’ may be not operate on the right
c45477d2
LC
422 ;; system/target values. Rewrite using ‘let-system’ when
423 ;; available.
1ee72bb5 424 (if #$(target-arm?)
c45477d2
LC
425 '()
426 (list (partition
427 ;; The standalone grub image is about 10MiB, but
428 ;; leave some room for custom or multiple images.
429 (size (* 40 (expt 2 20)))
430 (label "GNU-ESP") ;cosmetic only
431 ;; Use "vfat" here since this property is used
432 ;; when mounting. The actual FAT-ness is based
433 ;; on file system size (16 in this case).
434 (file-system "vfat")
1ee72bb5
MO
435 (flags '(esp)))))))
436 (grub-efi #$(and (not (target-arm?)) grub-efi)))
c45477d2
LC
437 (initialize-hard-disk "/dev/vda"
438 #:partitions partitions
1ee72bb5 439 #:grub-efi grub-efi
c45477d2 440 #:bootloader-package
96cb3f8a 441 #+(bootloader-package bootloader)
52c1b90a 442 #:bootcfg #$(preserve-target bootcfg-drv)
c45477d2
LC
443 #:bootcfg-location
444 #$(bootloader-configuration-file bootloader)
445 #:bootloader-installer
96cb3f8a 446 #+(bootloader-installer bootloader)))))))
b53833b2
LC
447 #:system system
448 #:make-disk-image? #t
449 #:disk-image-size disk-image-size
450 #:disk-image-format disk-image-format
52c1b90a 451 #:references-graphs inputs*
a328f66a 452 #:substitutable? substitutable?))
04086015 453
a335f6fc
CM
454(define* (system-docker-image os
455 #:key
247649d4 456 (name "guix-docker-image")
d6c43d7b
LC
457 (register-closures? (has-guix-service-type? os))
458 shared-network?)
a335f6fc 459 "Build a docker image. OS is the desired <operating-system>. NAME is the
d6c43d7b
LC
460base name to use for the output file. When SHARED-NETWORK? is true, assume
461that the container will share network with the host and thus doesn't need a
462DHCP client, nscd, and so on.
463
464When REGISTER-CLOSURES? is true, register the closure of OS with Guix in the
465resulting Docker image. By default, REGISTER-CLOSURES? is set to true only if
466a service of type GUIX-SERVICE-TYPE is present in the services definition of
467the operating system."
c45477d2
LC
468 (define schema
469 (and register-closures?
470 (local-file (search-path %load-path
471 "guix/store/schema.sql"))))
9f160a0d 472
247649d4
LC
473 (define boot-program
474 ;; Program that runs the boot script of OS, which in turn starts shepherd.
475 (program-file "boot-program"
476 #~(let ((system (cadr (command-line))))
477 (setenv "GUIX_NEW_SYSTEM" system)
478 (execl #$(file-append guile-2.2 "/bin/guile")
479 "guile" "--no-auto-compile"
480 (string-append system "/boot")))))
481
482
483 (let ((os (operating-system-with-gc-roots
d6c43d7b
LC
484 (containerized-operating-system os '()
485 #:shared-network?
486 shared-network?)
247649d4 487 (list boot-program)))
69cae3d3
LC
488 (name (string-append name ".tar.gz"))
489 (graph "system-graph"))
a335f6fc 490 (define build
81c3dc32 491 (with-extensions (cons guile-json-3 ;for (guix docker)
ca719424 492 gcrypt-sqlite3&co) ;for (guix store database)
9f160a0d
LC
493 (with-imported-modules `(,@(source-module-closure
494 '((guix docker)
c45477d2 495 (guix store database)
9f160a0d 496 (guix build utils)
c45477d2 497 (guix build store-copy)
9f160a0d
LC
498 (gnu build vm))
499 #:select? not-config?)
ca719424 500 ((guix config) => ,(make-config.scm)))
9f160a0d
LC
501 #~(begin
502 (use-modules (guix docker)
503 (guix build utils)
504 (gnu build vm)
505 (srfi srfi-19)
c45477d2
LC
506 (guix build store-copy)
507 (guix store database))
508
509 ;; Set the SQL schema location.
510 (sql-schema #$schema)
9f160a0d 511
ed504caf
LC
512 ;; Allow non-ASCII file names--e.g., 'nss-certs'--to be decoded.
513 (setenv "GUIX_LOCPATH"
514 #+(file-append glibc-utf8-locales "/lib/locale"))
515 (setlocale LC_ALL "en_US.utf8")
516
c45477d2 517 (let* (;; This initializer requires elevated privileges that are
9f160a0d
LC
518 ;; not normally available in the build environment (e.g.,
519 ;; it needs to create device nodes). In order to obtain
520 ;; such privileges, we run it as root in a VM.
521 (initialize (root-partition-initializer
522 #:closures '(#$graph)
523 #:register-closures? #$register-closures?
69cae3d3 524 #:system-directory #$os
9f160a0d
LC
525 ;; De-duplication would fail due to
526 ;; cross-device link errors, so don't do it.
527 #:deduplicate? #f))
528 ;; Even as root in a VM, the initializer would fail due to
529 ;; lack of privileges if we use a root-directory that is on
530 ;; a file system that is shared with the host (e.g., /tmp).
531 (root-directory "/guixsd-system-root"))
c45477d2 532 (set-path-environment-variable "PATH" '("bin" "sbin") '(#+tar))
9f160a0d
LC
533 (mkdir root-directory)
534 (initialize root-directory)
535 (build-docker-image
536 (string-append "/xchg/" #$name) ;; The output file.
537 (cons* root-directory
6892f0a2
LC
538 (map store-info-item
539 (call-with-input-file
540 (string-append "/xchg/" #$graph)
541 read-reference-graph)))
69cae3d3 542 #$os
247649d4 543 #:entry-point '(#$boot-program #$os)
9f160a0d
LC
544 #:compressor '(#+(file-append gzip "/bin/gzip") "-9n")
545 #:creation-time (make-time time-utc 0 1)
66ec3895 546 #:transformations `((,root-directory -> ""))))))))
247649d4 547
a335f6fc 548 (expression->derivation-in-linux-vm
be43c08b 549 name build
a335f6fc
CM
550 #:make-disk-image? #f
551 #:single-file-output? #t
69cae3d3 552 #:references-graphs `((,graph ,os)))))
a335f6fc 553
04086015
LC
554\f
555;;;
1e77fedb 556;;; VM and disk images.
04086015
LC
557;;;
558
f19cf27c
MO
559(define* (system-disk-image-in-vm os
560 #:key
561 (name "disk-image")
562 (file-system-type "ext4")
563 (disk-image-size (* 900 (expt 2 20)))
564 (volatile? #t)
565 (substitutable? #t))
1e77fedb
LC
566 "Return the derivation of a disk image of DISK-IMAGE-SIZE bytes of the
567system described by OS. Said image can be copied on a USB stick as is. When
568VOLATILE? is true, the root file system is made volatile; this is useful
a328f66a
LC
569to USB sticks meant to be read-only.
570
571SUBSTITUTABLE? determines whether the returned derivation should be marked as
572substitutable."
10ace2c4 573 (define root-label
77f52962 574 "Guix_image")
10ace2c4 575
5be7aecd 576 (define (root-uuid os)
5f7fe1c5
LC
577 ;; UUID of the root file system, computed in a deterministic fashion.
578 ;; This is what we use to locate the root file system so it has to be
579 ;; different from the user's own file system UUIDs.
77f52962 580 (operating-system-uuid os 'dce))
5f7fe1c5 581
1e77fedb
LC
582 (define file-systems-to-keep
583 (remove (lambda (fs)
584 (string=? (file-system-mount-point fs) "/"))
585 (operating-system-file-systems os)))
586
8bff7dc2
LC
587 (let* ((os (operating-system (inherit os)
588 ;; Since this is meant to be used on real hardware, don't
589 ;; install QEMU networking or anything like that. Assume USB
590 ;; mass storage devices (usb-storage.ko) are available.
591 (initrd (lambda (file-systems . rest)
592 (apply (operating-system-initrd os)
593 file-systems
67ed5442 594 #:volatile-root? volatile?
8bff7dc2
LC
595 rest)))
596
77f52962 597 (bootloader (operating-system-bootloader os))
8bff7dc2 598
5be7aecd
LC
599 ;; Force our own root file system. (We need a "/" file system
600 ;; to call 'root-uuid'.)
8bff7dc2
LC
601 (file-systems (cons (file-system
602 (mount-point "/")
5be7aecd
LC
603 (device "/dev/placeholder")
604 (type file-system-type))
605 file-systems-to-keep))))
606 (uuid (root-uuid os))
607 (os (operating-system
608 (inherit os)
609 (file-systems (cons (file-system
610 (mount-point "/")
611 (device uuid)
8bff7dc2
LC
612 (type file-system-type))
613 file-systems-to-keep))))
614 (bootcfg (operating-system-bootcfg os)))
77f52962
MO
615 (qemu-image #:name name
616 #:os os
617 #:bootcfg-drv bootcfg
618 #:bootloader (bootloader-configuration-bootloader
619 (operating-system-bootloader os))
620 #:disk-image-size disk-image-size
621 #:disk-image-format "raw"
622 #:file-system-type file-system-type
623 #:file-system-label root-label
624 #:file-system-uuid uuid
625 #:copy-inputs? #t
626 #:inputs `(("system" ,os)
627 ("bootcfg" ,bootcfg))
628 #:substitutable? substitutable?)))
1e77fedb 629
0b14d1d7 630(define* (system-qemu-image os
66f23d66
LC
631 #:key
632 (file-system-type "ext4")
633 (disk-image-size (* 900 (expt 2 20))))
634 "Return the derivation of a freestanding QEMU image of DISK-IMAGE-SIZE bytes
635of the GNU system as described by OS."
1eeccc2f
LC
636 (define file-systems-to-keep
637 ;; Keep only file systems other than root and not normally bound to real
638 ;; devices.
639 (remove (lambda (fs)
640 (let ((target (file-system-mount-point fs))
641 (source (file-system-device fs)))
642 (or (string=? target "/")
643 (string-prefix? "/dev/" source))))
644 (operating-system-file-systems os)))
645
61b94b8c
LC
646 (define root-uuid
647 ;; UUID of the root file system.
648 (operating-system-uuid os
649 (if (string=? file-system-type "iso9660")
650 'iso9660
651 'dce)))
652
653
8bff7dc2
LC
654 (let* ((os (operating-system (inherit os)
655 ;; Assume we have an initrd with the whole QEMU shebang.
656
657 ;; Force our own root file system. Refer to it by UUID so that
658 ;; it works regardless of how the image is used ("qemu -hda",
659 ;; Xen, etc.).
660 (file-systems (cons (file-system
661 (mount-point "/")
662 (device root-uuid)
663 (type file-system-type))
664 file-systems-to-keep))))
665 (bootcfg (operating-system-bootcfg os)))
666 (qemu-image #:os os
667 #:bootcfg-drv bootcfg
668 #:bootloader (bootloader-configuration-bootloader
669 (operating-system-bootloader os))
670 #:disk-image-size disk-image-size
671 #:file-system-type file-system-type
672 #:file-system-uuid root-uuid
673 #:inputs `(("system" ,os)
674 ("bootcfg" ,bootcfg))
675 #:copy-inputs? #t)))
04086015 676
fcf63cf8
LC
677\f
678;;;
679;;; VMs that share file systems with the host.
680;;;
681
96ffa27b
LC
682(define (file-system->mount-tag fs)
683 "Return a 9p mount tag for host file system FS."
dffc5ab5
LC
684 ;; QEMU mount tags must be ASCII, at most 31-byte long, cannot contain
685 ;; slashes, and cannot start with '_'. Compute an identifier that
686 ;; corresponds to the rules.
96ffa27b 687 (string-append "TAG"
dffc5ab5
LC
688 (string-drop (bytevector->base32-string
689 (sha1 (string->utf8 fs)))
690 4)))
96ffa27b 691
fcf63cf8
LC
692(define (mapping->file-system mapping)
693 "Return a 9p file system that realizes MAPPING."
694 (match mapping
695 (($ <file-system-mapping> source target writable?)
696 (file-system
697 (mount-point target)
698 (device (file-system->mount-tag source))
699 (type "9p")
700 (flags (if writable? '() '(read-only)))
e0d96774 701 (options "trans=virtio,cache=loose")
fcf63cf8
LC
702 (check? #f)
703 (create-mount-point? #t)))))
704
909de139 705(define* (virtualized-operating-system os mappings #:optional (full-boot? #f))
83bcd0b8 706 "Return an operating system based on OS suitable for use in a virtualized
fcf63cf8
LC
707environment with the store shared with the host. MAPPINGS is a list of
708<file-system-mapping> to realize in the virtualized OS."
709 (define user-file-systems
710 ;; Remove file systems that conflict with those added below, or that are
711 ;; normally bound to real devices.
712 (remove (lambda (fs)
713 (let ((target (file-system-mount-point fs))
714 (source (file-system-device fs)))
715 (or (string=? target (%store-prefix))
716 (string=? target "/")
a5acc17a 717 (and (string? source)
f00515b4
LC
718 (string-prefix? "/dev/" source))
719
720 ;; Labels and UUIDs are necessarily invalid in the VM.
721 (and (file-system-mount? fs)
a5acc17a 722 (or (file-system-label? source)
f00515b4 723 (uuid? source))))))
fcf63cf8
LC
724 (operating-system-file-systems os)))
725
909de139
DC
726 (define virtual-file-systems
727 (cons (file-system
728 (mount-point "/")
729 (device "/dev/vda1")
730 (type "ext4"))
731
732 (append (map mapping->file-system mappings)
733 user-file-systems)))
734
83bcd0b8 735 (operating-system (inherit os)
9b396c0c
LC
736
737 ;; XXX: Until we run QEMU with UEFI support (with the OVMF firmware),
738 ;; force the traditional i386/BIOS method.
739 ;; See <https://bugs.gnu.org/28768>.
740 (bootloader (bootloader-configuration
132823c2 741 (inherit (operating-system-bootloader os))
9b396c0c
LC
742 (bootloader grub-bootloader)
743 (target "/dev/vda")))
744
52ac153e 745 (initrd (lambda (file-systems . rest)
b8e77811
MO
746 (apply (operating-system-initrd os)
747 file-systems
52ac153e 748 #:volatile-root? #t
52ac153e 749 rest)))
65fb4515
LC
750
751 ;; Disable swap.
752 (swap-devices '())
753
909de139
DC
754 ;; XXX: When FULL-BOOT? is true, do not add a 9p mount for /gnu/store
755 ;; since that would lead the bootloader config to look for the kernel and
756 ;; initrd in it.
757 (file-systems (if full-boot?
758 virtual-file-systems
759 (cons
760 (file-system
761 (inherit (mapping->file-system %store-mapping))
762 (needed-for-boot? #t))
763 virtual-file-systems)))))
83bcd0b8 764
fd3bfc44 765(define* (system-qemu-image/shared-store
0b14d1d7 766 os
6aa260af 767 #:key
60f759f0
LC
768 (system (%current-system))
769 (target (%current-target-system))
6aa260af 770 full-boot?
4c0416ae 771 (disk-image-size (* (if full-boot? 500 30) (expt 2 20))))
fd3bfc44 772 "Return a derivation that builds a QEMU image of OS that shares its store
6aa260af
LC
773with the host.
774
775When FULL-BOOT? is true, return an image that does a complete boot sequence,
776bootloaded included; thus, make a disk image that contains everything the
777bootloader refers to: OS kernel, initrd, bootloader data, etc."
56f9d442
LC
778 (define root-uuid
779 ;; Use a fixed UUID to improve determinism.
780 (operating-system-uuid os 'dce))
781
8bff7dc2
LC
782 (define bootcfg
783 (operating-system-bootcfg os))
784
785 ;; XXX: When FULL-BOOT? is true, we end up creating an image that contains
786 ;; BOOTCFG and all its dependencies, including the output of OS.
787 ;; This is more than needed (we only need the kernel, initrd, GRUB for its
788 ;; font, and the background image), but it's hard to filter that.
789 (qemu-image #:os os
60f759f0
LC
790 #:system system
791 #:target target
8bff7dc2
LC
792 #:bootcfg-drv bootcfg
793 #:bootloader (bootloader-configuration-bootloader
794 (operating-system-bootloader os))
795 #:disk-image-size disk-image-size
796 #:file-system-uuid root-uuid
797 #:inputs (if full-boot?
798 `(("bootcfg" ,bootcfg))
799 '())
800
801 ;; XXX: Passing #t here is too slow, so let it off by default.
802 #:register-closures? #f
803 #:copy-inputs? full-boot?))
fd3bfc44 804
96ffa27b
LC
805(define* (common-qemu-options image shared-fs)
806 "Return the a string-value gexp with the common QEMU options to boot IMAGE,
807with '-virtfs' options for the host file systems listed in SHARED-FS."
26a076ed 808
96ffa27b 809 (define (virtfs-option fs)
26a076ed
DC
810 #~(format #f "-virtfs local,path=~s,security_model=none,mount_tag=~s"
811 #$fs #$(file-system->mount-tag fs)))
96ffa27b 812
26a076ed 813 #~(;; Only enable kvm if we see /dev/kvm exists.
944d2b17
CAW
814 ;; This allows users without hardware virtualization to still use these
815 ;; commands.
26a076ed
DC
816 #$@(if (file-exists? "/dev/kvm")
817 '("-enable-kvm")
818 '())
819
820 "-no-reboot"
53793927 821 "-nic" "user,model=virtio-net-pci"
2ca712bd
LF
822 "-object" "rng-random,filename=/dev/urandom,id=guixsd-vm-rng"
823 "-device" "virtio-rng-pci,rng=guixsd-vm-rng"
26a076ed
DC
824
825 #$@(map virtfs-option shared-fs)
826 "-vga std"
827 (format #f "-drive file=~a,if=virtio,cache=writeback,werror=report,readonly"
ebfb71d4 828 #$image)))
3c1f0e3b 829
ab11f0be
LC
830(define* (system-qemu-image/shared-store-script os
831 #:key
60f759f0
LC
832 (system (%current-system))
833 (target (%current-target-system))
ab11f0be
LC
834 (qemu qemu)
835 (graphic? #t)
ebfb71d4 836 (memory-size 256)
fcf63cf8 837 (mappings '())
6aa260af
LC
838 full-boot?
839 (disk-image-size
9a1bfe76 840 (* (if full-boot? 500 70)
ed419fa0
LC
841 (expt 2 20)))
842 (options '()))
fd3bfc44 843 "Return a derivation that builds a script to run a virtual machine image of
ebfb71d4
JN
844OS that shares its store with the host. The virtual machine runs with
845MEMORY-SIZE MiB of memory.
6aa260af 846
fcf63cf8
LC
847MAPPINGS is a list of <file-system-mapping> specifying mapping of host file
848systems into the guest.
849
6aa260af
LC
850When FULL-BOOT? is true, the returned script runs everything starting from the
851bootloader; otherwise it directly starts the operating system kernel. The
852DISK-IMAGE-SIZE parameter specifies the size in bytes of the root disk image;
853it is mostly useful when FULL-BOOT? is true."
909de139 854 (mlet* %store-monad ((os -> (virtualized-operating-system os mappings full-boot?))
6aa260af
LC
855 (image (system-qemu-image/shared-store
856 os
60f759f0
LC
857 #:system system
858 #:target target
6aa260af
LC
859 #:full-boot? full-boot?
860 #:disk-image-size disk-image-size)))
26a076ed 861 (define kernel-arguments
83071b05 862 #~(list #$@(if graphic? #~() #~("console=ttyS0"))
a7ef45d9 863 #+@(operating-system-kernel-arguments os "/dev/vda1")))
26a076ed
DC
864
865 (define qemu-exec
e768e4e9
LC
866 #~(list #+(file-append qemu "/bin/"
867 (qemu-command (or target system)))
26a076ed
DC
868 #$@(if full-boot?
869 #~()
870 #~("-kernel" #$(operating-system-kernel-file os)
a7ef45d9 871 "-initrd" #$(file-append os "/initrd")
26a076ed
DC
872 (format #f "-append ~s"
873 (string-join #$kernel-arguments " "))))
874 #$@(common-qemu-options image
875 (map file-system-mapping-source
ebfb71d4 876 (cons %store-mapping mappings)))
ed419fa0
LC
877 "-m " (number->string #$memory-size)
878 #$@options))
26a076ed 879
fd3bfc44 880 (define builder
02100028
LC
881 #~(call-with-output-file #$output
882 (lambda (port)
26a076ed 883 (format port "#!~a~% exec ~a \"$@\"~%"
e768e4e9 884 #+(file-append bash "/bin/sh")
26a076ed 885 (string-join #$qemu-exec " "))
02100028
LC
886 (chmod port #o555))))
887
888 (gexp->derivation "run-vm.sh" builder)))
fd3bfc44 889
ed419fa0
LC
890\f
891;;;
892;;; High-level abstraction.
893;;;
894
895(define-record-type* <virtual-machine> %virtual-machine
896 make-virtual-machine
897 virtual-machine?
898 (operating-system virtual-machine-operating-system) ;<operating-system>
899 (qemu virtual-machine-qemu ;<package>
900 (default qemu))
901 (graphic? virtual-machine-graphic? ;Boolean
902 (default #f))
903 (memory-size virtual-machine-memory-size ;integer (MiB)
904 (default 256))
eb152070
CB
905 (disk-image-size virtual-machine-disk-image-size ;integer (bytes)
906 (default 'guess))
ed419fa0
LC
907 (port-forwardings virtual-machine-port-forwardings ;list of integer pairs
908 (default '())))
909
910(define-syntax virtual-machine
911 (syntax-rules ()
912 "Declare a virtual machine running the specified OS, with the given
913options."
914 ((_ os) ;shortcut
915 (%virtual-machine (operating-system os)))
916 ((_ fields ...)
917 (%virtual-machine fields ...))))
918
919(define (port-forwardings->qemu-options forwardings)
920 "Return the QEMU option for the given port FORWARDINGS as a string, where
921FORWARDINGS is a list of host-port/guest-port pairs."
922 (string-join
923 (map (match-lambda
924 ((host-port . guest-port)
925 (string-append "hostfwd=tcp::"
926 (number->string host-port)
927 "-:" (number->string guest-port))))
928 forwardings)
929 ","))
930
931(define-gexp-compiler (virtual-machine-compiler (vm <virtual-machine>)
932 system target)
ed419fa0 933 (match vm
eb152070 934 (($ <virtual-machine> os qemu graphic? memory-size disk-image-size ())
ed419fa0 935 (system-qemu-image/shared-store-script os
60f759f0
LC
936 #:system system
937 #:target target
ed419fa0
LC
938 #:qemu qemu
939 #:graphic? graphic?
eb152070
CB
940 #:memory-size memory-size
941 #:disk-image-size
942 disk-image-size))
943 (($ <virtual-machine> os qemu graphic? memory-size disk-image-size
944 forwardings)
ed419fa0 945 (let ((options
8e53fe2b
MB
946 `("-nic" ,(string-append
947 "user,model=virtio-net-pci,"
ed419fa0
LC
948 (port-forwardings->qemu-options forwardings)))))
949 (system-qemu-image/shared-store-script os
60f759f0
LC
950 #:system system
951 #:target target
ed419fa0
LC
952 #:qemu qemu
953 #:graphic? graphic?
954 #:memory-size memory-size
eb152070
CB
955 #:disk-image-size
956 disk-image-size
ed419fa0
LC
957 #:options options)))))
958
04086015 959;;; vm.scm ends here