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