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