services: Add 'guix-publish-service'.
[jackhill/guix/guix.git] / gnu / system / vm.scm
CommitLineData
04086015 1;;; GNU Guix --- Functional package management for GNU
493c245b 2;;; Copyright © 2013, 2014, 2015 Ludovic Courtès <ludo@gnu.org>
04086015
LC
3;;;
4;;; This file is part of GNU Guix.
5;;;
6;;; GNU Guix is free software; you can redistribute it and/or modify it
7;;; under the terms of the GNU General Public License as published by
8;;; the Free Software Foundation; either version 3 of the License, or (at
9;;; your option) any later version.
10;;;
11;;; GNU Guix is distributed in the hope that it will be useful, but
12;;; WITHOUT ANY WARRANTY; without even the implied warranty of
13;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14;;; GNU General Public License for more details.
15;;;
16;;; You should have received a copy of the GNU General Public License
17;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
18
19(define-module (gnu system vm)
93d44bd8 20 #:use-module (guix config)
04086015 21 #:use-module (guix store)
02100028 22 #:use-module (guix gexp)
04086015
LC
23 #:use-module (guix derivations)
24 #:use-module (guix packages)
d9f0a237 25 #:use-module (guix monads)
fcf63cf8
LC
26 #:use-module (guix records)
27
548f7a8f 28 #:use-module ((gnu build vm)
66670cf3 29 #:select (qemu-command))
bdb36958 30 #:use-module (gnu packages base)
1b89a66e 31 #:use-module (gnu packages guile)
bdb36958 32 #:use-module (gnu packages gawk)
1b89a66e 33 #:use-module (gnu packages bash)
4f62d8d6 34 #:use-module (gnu packages less)
04086015 35 #:use-module (gnu packages qemu)
cc4a2aeb 36 #:use-module (gnu packages disk)
5b16ff09 37 #:use-module (gnu packages zile)
04086015
LC
38 #:use-module (gnu packages grub)
39 #:use-module (gnu packages linux)
30f25b03 40 #:use-module (gnu packages package-management)
04086015
LC
41 #:use-module ((gnu packages make-bootstrap)
42 #:select (%guile-static-stripped))
9de46ffb 43 #:use-module (gnu packages admin)
0ded70f3
LC
44
45 #:use-module (gnu system shadow)
46 #:use-module (gnu system linux)
735c6dd7 47 #:use-module (gnu system linux-initrd)
0ded70f3 48 #:use-module (gnu system grub)
c5df1839 49 #:use-module (gnu system file-systems)
033adfe7 50 #:use-module (gnu system)
db4fdc04 51 #:use-module (gnu services)
0ded70f3 52
ca85d7bc 53 #:use-module (srfi srfi-1)
04086015
LC
54 #:use-module (srfi srfi-26)
55 #:use-module (ice-9 match)
0ded70f3 56
04086015 57 #:export (expression->derivation-in-linux-vm
aedb72fb 58 qemu-image
fd3bfc44 59 system-qemu-image
fcf63cf8 60
fd3bfc44 61 system-qemu-image/shared-store
1e77fedb
LC
62 system-qemu-image/shared-store-script
63 system-disk-image))
04086015
LC
64
65\f
66;;; Commentary:
67;;;
68;;; Tools to evaluate build expressions within virtual machines.
69;;;
70;;; Code:
71
83bcd0b8
LC
72(define %linux-vm-file-systems
73 ;; File systems mounted for 'derivation-in-linux-vm'. The store and /xchg
74 ;; directory are shared with the host over 9p.
75 (list (file-system
76 (mount-point (%store-prefix))
77 (device "store")
78 (type "9p")
79 (needed-for-boot? #t)
3c05b4bc
LC
80 (options "trans=virtio")
81 (check? #f))
83bcd0b8
LC
82 (file-system
83 (mount-point "/xchg")
84 (device "xchg")
85 (type "9p")
86 (needed-for-boot? #t)
3c05b4bc
LC
87 (options "trans=virtio")
88 (check? #f))))
83bcd0b8 89
d9f0a237 90(define* (expression->derivation-in-linux-vm name exp
04086015 91 #:key
2455085a 92 (system (%current-system))
04086015 93 (linux linux-libre)
735c6dd7 94 initrd
f200b03e 95 (qemu qemu-headless)
04086015 96 (env-vars '())
1aa0033b 97 (modules
548f7a8f
LC
98 '((gnu build vm)
99 (gnu build install)
8a9e21d1 100 (gnu build linux-boot)
0e704a2d 101 (gnu build linux-modules)
e2f4b305 102 (gnu build file-systems)
0e704a2d 103 (guix elf)
72b891e5 104 (guix records)
6fd1a796 105 (guix build utils)
1e49bcf9 106 (guix build syscalls)
6fd1a796 107 (guix build store-copy)))
04086015
LC
108 (guile-for-build
109 (%guile-for-build))
110
111 (make-disk-image? #f)
ca85d7bc 112 (references-graphs #f)
defa1b9b 113 (memory-size 256)
c4a74364 114 (disk-image-format "qcow2")
04086015
LC
115 (disk-image-size
116 (* 100 (expt 2 20))))
735c6dd7 117 "Evaluate EXP in a QEMU virtual machine running LINUX with INITRD (a
1aa0033b 118derivation). In the virtual machine, EXP has access to all its inputs from the
735c6dd7 119store; it should put its output files in the `/xchg' directory, which is
defa1b9b
LC
120copied to the derivation's output when the VM terminates. The virtual machine
121runs with MEMORY-SIZE MiB of memory.
04086015 122
c4a74364
LC
123When MAKE-DISK-IMAGE? is true, then create a QEMU disk image of type
124DISK-IMAGE-FORMAT (e.g., 'qcow2' or 'raw'), of DISK-IMAGE-SIZE bytes and
125return it.
ca85d7bc 126
1aa0033b 127MODULES is the set of modules imported in the execution environment of EXP.
ade5ce7a 128
ca85d7bc
LC
129When REFERENCES-GRAPHS is true, it must be a list of file name/store path
130pairs, as for `derivation'. The files containing the reference graphs are
131made available under the /xchg CIFS share."
d9f0a237 132 (mlet* %store-monad
1aa0033b
LC
133 ((module-dir (imported-modules modules))
134 (compiled (compiled-modules modules))
135 (user-builder (gexp->file "builder-in-linux-vm" exp))
02100028
LC
136 (loader (gexp->file "linux-vm-loader"
137 #~(begin
138 (set! %load-path
139 (cons #$module-dir %load-path))
140 (set! %load-compiled-path
141 (cons #$compiled
142 %load-compiled-path))
143 (primitive-load #$user-builder))))
bdb36958 144 (coreutils -> (canonical-package coreutils))
d4254711 145 (initrd (if initrd ; use the default initrd?
735c6dd7 146 (return initrd)
060238ae 147 (base-initrd %linux-vm-file-systems
0d275f4a 148 #:linux linux
24e0160a 149 #:virtio? #t
6c1df081 150 #:qemu-networking? #t))))
1aa0033b
LC
151
152 (define builder
153 ;; Code that launches the VM that evaluates EXP.
154 #~(begin
155 (use-modules (guix build utils)
548f7a8f 156 (gnu build vm))
1aa0033b
LC
157
158 (let ((inputs '#$(list qemu coreutils))
159 (linux (string-append #$linux "/bzImage"))
160 (initrd (string-append #$initrd "/initrd"))
161 (loader #$loader)
162 (graphs '#$(match references-graphs
163 (((graph-files . _) ...) graph-files)
164 (_ #f))))
165
166 (set-path-environment-variable "PATH" '("bin") inputs)
167
168 (load-in-linux-vm loader
169 #:output #$output
170 #:linux linux #:initrd initrd
171 #:memory-size #$memory-size
172 #:make-disk-image? #$make-disk-image?
c4a74364 173 #:disk-image-format #$disk-image-format
1aa0033b
LC
174 #:disk-image-size #$disk-image-size
175 #:references-graphs graphs))))
176
177 (gexp->derivation name builder
178 ;; TODO: Require the "kvm" feature.
179 #:system system
180 #:env-vars env-vars
5ce3defe 181 #:modules modules
1aa0033b
LC
182 #:guile-for-build guile-for-build
183 #:references-graphs references-graphs)))
d9f0a237
LC
184
185(define* (qemu-image #:key
04086015
LC
186 (name "qemu-image")
187 (system (%current-system))
1aa0033b 188 (qemu qemu-headless)
04086015 189 (disk-image-size (* 100 (expt 2 20)))
c4a74364 190 (disk-image-format "qcow2")
03ddfaf5 191 (file-system-type "ext4")
ef9fc40d 192 file-system-label
f2c403ea 193 os-derivation
0e2ddecd 194 grub-configuration
150e20dd 195 (register-closures? #t)
150e20dd
LC
196 (inputs '())
197 copy-inputs?)
c4a74364 198 "Return a bootable, stand-alone QEMU image of type DISK-IMAGE-FORMAT (e.g.,
ef9fc40d
LC
199'qcow2' or 'raw'), with a root partition of type FILE-SYSTEM-TYPE.
200Optionally, FILE-SYSTEM-LABEL can be specified as the volume name for the root
f2c403ea
LC
201partition. The returned image is a full disk image that runs OS-DERIVATION,
202with a GRUB installation that uses GRUB-CONFIGURATION as its configuration
203file (GRUB-CONFIGURATION must be the name of a file in the VM.)
93d44bd8 204
150e20dd
LC
205INPUTS is a list of inputs (as for packages). When COPY-INPUTS? is true, copy
206all of INPUTS into the image being built. When REGISTER-CLOSURES? is true,
207register INPUTS in the store database of the image so that Guix can be used in
b4140694 208the image."
b53833b2
LC
209 (expression->derivation-in-linux-vm
210 name
211 #~(begin
212 (use-modules (gnu build vm)
213 (guix build utils))
1aa0033b 214
b53833b2 215 (let ((inputs
ec2406ef 216 '#$(append (list qemu parted grub e2fsprogs)
b53833b2
LC
217 (map canonical-package
218 (list sed grep coreutils findutils gawk))
219 (if register-closures? (list guix) '())))
1aa0033b 220
b53833b2
LC
221 ;; This variable is unused but allows us to add INPUTS-TO-COPY
222 ;; as inputs.
223 (to-register
224 '#$(map (match-lambda
225 ((name thing) thing)
226 ((name thing output) `(,thing ,output)))
227 inputs)))
1aa0033b 228
b53833b2 229 (set-path-environment-variable "PATH" '("bin" "sbin") inputs)
1aa0033b 230
72b891e5
LC
231 (let* ((graphs '#$(match inputs
232 (((names . _) ...)
233 names)))
234 (initialize (root-partition-initializer
235 #:closures graphs
236 #:copy-closures? #$copy-inputs?
237 #:register-closures? #$register-closures?
238 #:system-directory #$os-derivation))
239 (partitions (list (partition
240 (size #$(- disk-image-size
241 (* 10 (expt 2 20))))
242 (label #$file-system-label)
243 (file-system #$file-system-type)
244 (bootable? #t)
245 (initializer initialize)))))
b53833b2 246 (initialize-hard-disk "/dev/vda"
72b891e5
LC
247 #:partitions partitions
248 #:grub.cfg #$grub-configuration)
b53833b2
LC
249 (reboot))))
250 #:system system
251 #:make-disk-image? #t
252 #:disk-image-size disk-image-size
253 #:disk-image-format disk-image-format
254 #:references-graphs inputs))
04086015
LC
255
256\f
257;;;
1e77fedb 258;;; VM and disk images.
04086015
LC
259;;;
260
1e77fedb
LC
261(define* (system-disk-image os
262 #:key
56ef7fcc 263 (name "disk-image")
1e77fedb
LC
264 (file-system-type "ext4")
265 (disk-image-size (* 900 (expt 2 20)))
266 (volatile? #t))
267 "Return the derivation of a disk image of DISK-IMAGE-SIZE bytes of the
268system described by OS. Said image can be copied on a USB stick as is. When
269VOLATILE? is true, the root file system is made volatile; this is useful
270to USB sticks meant to be read-only."
10ace2c4
LC
271 (define root-label
272 ;; Volume name of the root file system. Since we don't know which device
273 ;; will hold it, we use the volume name to find it (using the UUID would
274 ;; be even better, but somewhat less convenient.)
275 "gnu-disk-image")
276
1e77fedb
LC
277 (define file-systems-to-keep
278 (remove (lambda (fs)
279 (string=? (file-system-mount-point fs) "/"))
280 (operating-system-file-systems os)))
281
282 (let ((os (operating-system (inherit os)
932e1f92 283 ;; Since this is meant to be used on real hardware, don't
493c245b
LC
284 ;; install QEMU networking or anything like that. Assume USB
285 ;; mass storage devices (usb-storage.ko) are available.
52ac153e
LC
286 (initrd (lambda (file-systems . rest)
287 (apply base-initrd file-systems
288 #:volatile-root? #t
52ac153e 289 rest)))
1e77fedb
LC
290
291 ;; Force our own root file system.
292 (file-systems (cons (file-system
293 (mount-point "/")
10ace2c4 294 (device root-label)
d4c87617 295 (title 'label)
1e77fedb
LC
296 (type file-system-type))
297 file-systems-to-keep)))))
298
299 (mlet* %store-monad ((os-drv (operating-system-derivation os))
300 (grub.cfg (operating-system-grub.cfg os)))
56ef7fcc 301 (qemu-image #:name name
f2c403ea 302 #:os-derivation os-drv
56ef7fcc 303 #:grub-configuration grub.cfg
1e77fedb
LC
304 #:disk-image-size disk-image-size
305 #:disk-image-format "raw"
306 #:file-system-type file-system-type
10ace2c4 307 #:file-system-label root-label
1e77fedb
LC
308 #:copy-inputs? #t
309 #:register-closures? #t
310 #:inputs `(("system" ,os-drv)
311 ("grub.cfg" ,grub.cfg))))))
312
0b14d1d7 313(define* (system-qemu-image os
66f23d66
LC
314 #:key
315 (file-system-type "ext4")
316 (disk-image-size (* 900 (expt 2 20))))
317 "Return the derivation of a freestanding QEMU image of DISK-IMAGE-SIZE bytes
318of the GNU system as described by OS."
1eeccc2f
LC
319 (define file-systems-to-keep
320 ;; Keep only file systems other than root and not normally bound to real
321 ;; devices.
322 (remove (lambda (fs)
323 (let ((target (file-system-mount-point fs))
324 (source (file-system-device fs)))
325 (or (string=? target "/")
326 (string-prefix? "/dev/" source))))
327 (operating-system-file-systems os)))
328
66f23d66 329 (let ((os (operating-system (inherit os)
e84d8b30 330 ;; Use an initrd with the whole QEMU shebang.
52ac153e
LC
331 (initrd (lambda (file-systems . rest)
332 (apply base-initrd file-systems
333 #:virtio? #t
334 #:qemu-networking? #t
335 rest)))
e84d8b30 336
1eeccc2f
LC
337 ;; Force our own root file system.
338 (file-systems (cons (file-system
66f23d66
LC
339 (mount-point "/")
340 (device "/dev/sda1")
1eeccc2f
LC
341 (type file-system-type))
342 file-systems-to-keep)))))
66f23d66
LC
343 (mlet* %store-monad
344 ((os-drv (operating-system-derivation os))
b4140694 345 (grub.cfg (operating-system-grub.cfg os)))
f2c403ea
LC
346 (qemu-image #:os-derivation os-drv
347 #:grub-configuration grub.cfg
66f23d66
LC
348 #:disk-image-size disk-image-size
349 #:file-system-type file-system-type
b4140694
LC
350 #:inputs `(("system" ,os-drv)
351 ("grub.cfg" ,grub.cfg))
150e20dd 352 #:copy-inputs? #t))))
04086015 353
fcf63cf8
LC
354\f
355;;;
356;;; VMs that share file systems with the host.
357;;;
358
96ffa27b
LC
359(define (file-system->mount-tag fs)
360 "Return a 9p mount tag for host file system FS."
361 ;; QEMU mount tags cannot contain slashes and cannot start with '_'.
362 ;; Compute an identifier that corresponds to the rules.
363 (string-append "TAG"
364 (string-map (match-lambda
365 (#\/ #\_)
366 (chr chr))
367 fs)))
368
fcf63cf8
LC
369(define (mapping->file-system mapping)
370 "Return a 9p file system that realizes MAPPING."
371 (match mapping
372 (($ <file-system-mapping> source target writable?)
373 (file-system
374 (mount-point target)
375 (device (file-system->mount-tag source))
376 (type "9p")
377 (flags (if writable? '() '(read-only)))
378 (options (string-append "trans=virtio"))
379 (check? #f)
380 (create-mount-point? #t)))))
381
382(define (virtualized-operating-system os mappings)
83bcd0b8 383 "Return an operating system based on OS suitable for use in a virtualized
fcf63cf8
LC
384environment with the store shared with the host. MAPPINGS is a list of
385<file-system-mapping> to realize in the virtualized OS."
386 (define user-file-systems
387 ;; Remove file systems that conflict with those added below, or that are
388 ;; normally bound to real devices.
389 (remove (lambda (fs)
390 (let ((target (file-system-mount-point fs))
391 (source (file-system-device fs)))
392 (or (string=? target (%store-prefix))
393 (string=? target "/")
394 (string-prefix? "/dev/" source))))
395 (operating-system-file-systems os)))
396
83bcd0b8 397 (operating-system (inherit os)
52ac153e
LC
398 (initrd (lambda (file-systems . rest)
399 (apply base-initrd file-systems
400 #:volatile-root? #t
401 #:virtio? #t
402 #:qemu-networking? #t
403 rest)))
65fb4515
LC
404
405 ;; Disable swap.
406 (swap-devices '())
407
1eeccc2f
LC
408 (file-systems (cons* (file-system
409 (mount-point "/")
410 (device "/dev/vda1")
411 (type "ext4"))
96ffa27b
LC
412
413 (file-system (inherit
fcf63cf8 414 (mapping->file-system %store-mapping))
96ffa27b 415 (needed-for-boot? #t))
1eeccc2f 416
fcf63cf8
LC
417 (append (map mapping->file-system mappings)
418 user-file-systems)))))
83bcd0b8 419
fd3bfc44 420(define* (system-qemu-image/shared-store
0b14d1d7 421 os
6aa260af
LC
422 #:key
423 full-boot?
424 (disk-image-size (* (if full-boot? 500 15) (expt 2 20))))
fd3bfc44 425 "Return a derivation that builds a QEMU image of OS that shares its store
6aa260af
LC
426with the host.
427
428When FULL-BOOT? is true, return an image that does a complete boot sequence,
429bootloaded included; thus, make a disk image that contains everything the
430bootloader refers to: OS kernel, initrd, bootloader data, etc."
431 (mlet* %store-monad ((os-drv (operating-system-derivation os))
432 (grub.cfg (operating-system-grub.cfg os)))
433 ;; XXX: When FULL-BOOT? is true, we end up creating an image that contains
434 ;; GRUB.CFG and all its dependencies, including the output of OS-DRV.
435 ;; This is more than needed (we only need the kernel, initrd, GRUB for its
436 ;; font, and the background image), but it's hard to filter that.
f2c403ea
LC
437 (qemu-image #:os-derivation os-drv
438 #:grub-configuration grub.cfg
150e20dd 439 #:disk-image-size disk-image-size
6aa260af
LC
440 #:inputs (if full-boot?
441 `(("grub.cfg" ,grub.cfg))
442 '())
150e20dd
LC
443
444 ;; XXX: Passing #t here is too slow, so let it off by default.
445 #:register-closures? #f
6aa260af 446 #:copy-inputs? full-boot?)))
fd3bfc44 447
96ffa27b
LC
448(define* (common-qemu-options image shared-fs)
449 "Return the a string-value gexp with the common QEMU options to boot IMAGE,
450with '-virtfs' options for the host file systems listed in SHARED-FS."
451 (define (virtfs-option fs)
452 #~(string-append "-virtfs local,path=\"" #$fs
453 "\",security_model=none,mount_tag=\""
454 #$(file-system->mount-tag fs)
455 "\" "))
456
457 #~(string-append
3c1f0e3b 458 " -enable-kvm -no-reboot -net nic,model=virtio \
96ffa27b 459 " #$@(map virtfs-option shared-fs) " \
3c1f0e3b 460 -net user \
5c5e7bda 461 -serial stdio -vga std \
3c1f0e3b
LC
462 -drive file=" #$image
463 ",if=virtio,cache=writeback,werror=report,readonly \
810568b3 464 -m 256"))
3c1f0e3b 465
ab11f0be
LC
466(define* (system-qemu-image/shared-store-script os
467 #:key
468 (qemu qemu)
469 (graphic? #t)
fcf63cf8 470 (mappings '())
6aa260af
LC
471 full-boot?
472 (disk-image-size
473 (* (if full-boot? 500 15)
474 (expt 2 20))))
fd3bfc44 475 "Return a derivation that builds a script to run a virtual machine image of
6aa260af
LC
476OS that shares its store with the host.
477
fcf63cf8
LC
478MAPPINGS is a list of <file-system-mapping> specifying mapping of host file
479systems into the guest.
480
6aa260af
LC
481When FULL-BOOT? is true, the returned script runs everything starting from the
482bootloader; otherwise it directly starts the operating system kernel. The
483DISK-IMAGE-SIZE parameter specifies the size in bytes of the root disk image;
484it is mostly useful when FULL-BOOT? is true."
fcf63cf8 485 (mlet* %store-monad ((os -> (virtualized-operating-system os mappings))
6aa260af
LC
486 (os-drv (operating-system-derivation os))
487 (image (system-qemu-image/shared-store
488 os
489 #:full-boot? full-boot?
490 #:disk-image-size disk-image-size)))
fd3bfc44 491 (define builder
02100028
LC
492 #~(call-with-output-file #$output
493 (lambda (port)
494 (display
495 (string-append "#!" #$bash "/bin/sh
66670cf3 496exec " #$qemu "/bin/" #$(qemu-command (%current-system))
ab11f0be
LC
497
498#$@(if full-boot?
499 #~()
500 #~(" -kernel " #$(operating-system-kernel os) "/bzImage \
501 -initrd " #$os-drv "/initrd \
502 -append \"" #$(if graphic? "" "console=ttyS0 ")
ee2a6304
LC
503 "--system=" #$os-drv " --load=" #$os-drv "/boot --root=/dev/vda1 "
504 (string-join (list #+@(operating-system-kernel-arguments os))) "\" "))
fcf63cf8
LC
505#$(common-qemu-options image
506 (map file-system-mapping-source
507 (cons %store-mapping mappings)))
810568b3 508" \"$@\"\n")
02100028
LC
509 port)
510 (chmod port #o555))))
511
512 (gexp->derivation "run-vm.sh" builder)))
fd3bfc44 513
04086015 514;;; vm.scm ends here