Commit | Line | Data |
---|---|---|
04086015 | 1 | ;;; GNU Guix --- Functional package management for GNU |
735c6dd7 | 2 | ;;; Copyright © 2013, 2014 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) |
548f7a8f | 26 | #:use-module ((gnu build vm) |
66670cf3 | 27 | #:select (qemu-command)) |
bdb36958 | 28 | #:use-module (gnu packages base) |
1b89a66e | 29 | #:use-module (gnu packages guile) |
bdb36958 | 30 | #:use-module (gnu packages gawk) |
1b89a66e | 31 | #:use-module (gnu packages bash) |
4f62d8d6 | 32 | #:use-module (gnu packages less) |
04086015 | 33 | #:use-module (gnu packages qemu) |
cc4a2aeb | 34 | #:use-module (gnu packages disk) |
5b16ff09 | 35 | #:use-module (gnu packages zile) |
04086015 LC |
36 | #:use-module (gnu packages grub) |
37 | #:use-module (gnu packages linux) | |
30f25b03 | 38 | #:use-module (gnu packages package-management) |
04086015 LC |
39 | #:use-module ((gnu packages make-bootstrap) |
40 | #:select (%guile-static-stripped)) | |
9de46ffb | 41 | #:use-module (gnu packages admin) |
0ded70f3 LC |
42 | |
43 | #:use-module (gnu system shadow) | |
44 | #:use-module (gnu system linux) | |
735c6dd7 | 45 | #:use-module (gnu system linux-initrd) |
0ded70f3 | 46 | #:use-module (gnu system grub) |
c5df1839 | 47 | #:use-module (gnu system file-systems) |
033adfe7 | 48 | #:use-module (gnu system) |
db4fdc04 | 49 | #:use-module (gnu services) |
0ded70f3 | 50 | |
ca85d7bc | 51 | #:use-module (srfi srfi-1) |
04086015 LC |
52 | #:use-module (srfi srfi-26) |
53 | #:use-module (ice-9 match) | |
0ded70f3 | 54 | |
04086015 | 55 | #:export (expression->derivation-in-linux-vm |
aedb72fb | 56 | qemu-image |
fd3bfc44 LC |
57 | system-qemu-image |
58 | system-qemu-image/shared-store | |
1e77fedb LC |
59 | system-qemu-image/shared-store-script |
60 | system-disk-image)) | |
04086015 LC |
61 | |
62 | \f | |
63 | ;;; Commentary: | |
64 | ;;; | |
65 | ;;; Tools to evaluate build expressions within virtual machines. | |
66 | ;;; | |
67 | ;;; Code: | |
68 | ||
83bcd0b8 LC |
69 | (define %linux-vm-file-systems |
70 | ;; File systems mounted for 'derivation-in-linux-vm'. The store and /xchg | |
71 | ;; directory are shared with the host over 9p. | |
72 | (list (file-system | |
73 | (mount-point (%store-prefix)) | |
74 | (device "store") | |
75 | (type "9p") | |
76 | (needed-for-boot? #t) | |
3c05b4bc LC |
77 | (options "trans=virtio") |
78 | (check? #f)) | |
83bcd0b8 LC |
79 | (file-system |
80 | (mount-point "/xchg") | |
81 | (device "xchg") | |
82 | (type "9p") | |
83 | (needed-for-boot? #t) | |
3c05b4bc LC |
84 | (options "trans=virtio") |
85 | (check? #f)))) | |
83bcd0b8 | 86 | |
d9f0a237 | 87 | (define* (expression->derivation-in-linux-vm name exp |
04086015 | 88 | #:key |
2455085a | 89 | (system (%current-system)) |
04086015 | 90 | (linux linux-libre) |
735c6dd7 | 91 | initrd |
f200b03e | 92 | (qemu qemu-headless) |
04086015 | 93 | (env-vars '()) |
1aa0033b | 94 | (modules |
548f7a8f LC |
95 | '((gnu build vm) |
96 | (gnu build install) | |
8a9e21d1 | 97 | (gnu build linux-boot) |
e2f4b305 | 98 | (gnu build file-systems) |
6fd1a796 LC |
99 | (guix build utils) |
100 | (guix build store-copy))) | |
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 | 111 | derivation). In the virtual machine, EXP has access to all its inputs from the |
735c6dd7 | 112 | store; it should put its output files in the `/xchg' directory, which is |
defa1b9b LC |
113 | copied to the derivation's output when the VM terminates. The virtual machine |
114 | runs with MEMORY-SIZE MiB of memory. | |
04086015 | 115 | |
c4a74364 LC |
116 | When MAKE-DISK-IMAGE? is true, then create a QEMU disk image of type |
117 | DISK-IMAGE-FORMAT (e.g., 'qcow2' or 'raw'), of DISK-IMAGE-SIZE bytes and | |
118 | return it. | |
ca85d7bc | 119 | |
1aa0033b | 120 | MODULES is the set of modules imported in the execution environment of EXP. |
ade5ce7a | 121 | |
ca85d7bc LC |
122 | When REFERENCES-GRAPHS is true, it must be a list of file name/store path |
123 | pairs, as for `derivation'. The files containing the reference graphs are | |
124 | made available under the /xchg CIFS share." | |
d9f0a237 | 125 | (mlet* %store-monad |
1aa0033b LC |
126 | ((module-dir (imported-modules modules)) |
127 | (compiled (compiled-modules modules)) | |
128 | (user-builder (gexp->file "builder-in-linux-vm" exp)) | |
02100028 LC |
129 | (loader (gexp->file "linux-vm-loader" |
130 | #~(begin | |
131 | (set! %load-path | |
132 | (cons #$module-dir %load-path)) | |
133 | (set! %load-compiled-path | |
134 | (cons #$compiled | |
135 | %load-compiled-path)) | |
136 | (primitive-load #$user-builder)))) | |
bdb36958 | 137 | (coreutils -> (canonical-package coreutils)) |
d4254711 | 138 | (initrd (if initrd ; use the default initrd? |
735c6dd7 | 139 | (return initrd) |
060238ae | 140 | (base-initrd %linux-vm-file-systems |
24e0160a | 141 | #:virtio? #t |
6c1df081 | 142 | #:qemu-networking? #t)))) |
1aa0033b LC |
143 | |
144 | (define builder | |
145 | ;; Code that launches the VM that evaluates EXP. | |
146 | #~(begin | |
147 | (use-modules (guix build utils) | |
548f7a8f | 148 | (gnu build vm)) |
1aa0033b LC |
149 | |
150 | (let ((inputs '#$(list qemu coreutils)) | |
151 | (linux (string-append #$linux "/bzImage")) | |
152 | (initrd (string-append #$initrd "/initrd")) | |
153 | (loader #$loader) | |
154 | (graphs '#$(match references-graphs | |
155 | (((graph-files . _) ...) graph-files) | |
156 | (_ #f)))) | |
157 | ||
158 | (set-path-environment-variable "PATH" '("bin") inputs) | |
159 | ||
160 | (load-in-linux-vm loader | |
161 | #:output #$output | |
162 | #:linux linux #:initrd initrd | |
163 | #:memory-size #$memory-size | |
164 | #:make-disk-image? #$make-disk-image? | |
c4a74364 | 165 | #:disk-image-format #$disk-image-format |
1aa0033b LC |
166 | #:disk-image-size #$disk-image-size |
167 | #:references-graphs graphs)))) | |
168 | ||
169 | (gexp->derivation name builder | |
170 | ;; TODO: Require the "kvm" feature. | |
171 | #:system system | |
172 | #:env-vars env-vars | |
5ce3defe | 173 | #:modules modules |
1aa0033b LC |
174 | #:guile-for-build guile-for-build |
175 | #:references-graphs references-graphs))) | |
d9f0a237 LC |
176 | |
177 | (define* (qemu-image #:key | |
04086015 LC |
178 | (name "qemu-image") |
179 | (system (%current-system)) | |
1aa0033b | 180 | (qemu qemu-headless) |
04086015 | 181 | (disk-image-size (* 100 (expt 2 20))) |
c4a74364 | 182 | (disk-image-format "qcow2") |
03ddfaf5 | 183 | (file-system-type "ext4") |
ef9fc40d | 184 | file-system-label |
f2c403ea | 185 | os-derivation |
0e2ddecd | 186 | grub-configuration |
150e20dd | 187 | (register-closures? #t) |
150e20dd LC |
188 | (inputs '()) |
189 | copy-inputs?) | |
c4a74364 | 190 | "Return a bootable, stand-alone QEMU image of type DISK-IMAGE-FORMAT (e.g., |
ef9fc40d LC |
191 | 'qcow2' or 'raw'), with a root partition of type FILE-SYSTEM-TYPE. |
192 | Optionally, FILE-SYSTEM-LABEL can be specified as the volume name for the root | |
f2c403ea LC |
193 | partition. The returned image is a full disk image that runs OS-DERIVATION, |
194 | with a GRUB installation that uses GRUB-CONFIGURATION as its configuration | |
195 | file (GRUB-CONFIGURATION must be the name of a file in the VM.) | |
93d44bd8 | 196 | |
150e20dd LC |
197 | INPUTS is a list of inputs (as for packages). When COPY-INPUTS? is true, copy |
198 | all of INPUTS into the image being built. When REGISTER-CLOSURES? is true, | |
199 | register INPUTS in the store database of the image so that Guix can be used in | |
b4140694 | 200 | the image." |
b53833b2 LC |
201 | (expression->derivation-in-linux-vm |
202 | name | |
203 | #~(begin | |
204 | (use-modules (gnu build vm) | |
205 | (guix build utils)) | |
1aa0033b | 206 | |
b53833b2 LC |
207 | (let ((inputs |
208 | '#$(append (list qemu parted grub e2fsprogs util-linux) | |
209 | (map canonical-package | |
210 | (list sed grep coreutils findutils gawk)) | |
211 | (if register-closures? (list guix) '()))) | |
1aa0033b | 212 | |
b53833b2 LC |
213 | ;; This variable is unused but allows us to add INPUTS-TO-COPY |
214 | ;; as inputs. | |
215 | (to-register | |
216 | '#$(map (match-lambda | |
217 | ((name thing) thing) | |
218 | ((name thing output) `(,thing ,output))) | |
219 | inputs))) | |
1aa0033b | 220 | |
b53833b2 | 221 | (set-path-environment-variable "PATH" '("bin" "sbin") inputs) |
1aa0033b | 222 | |
b53833b2 LC |
223 | (let ((graphs '#$(match inputs |
224 | (((names . _) ...) | |
225 | names)))) | |
226 | (initialize-hard-disk "/dev/vda" | |
227 | #:system-directory #$os-derivation | |
228 | #:grub.cfg #$grub-configuration | |
229 | #:closures graphs | |
230 | #:copy-closures? #$copy-inputs? | |
231 | #:register-closures? #$register-closures? | |
232 | #:disk-image-size #$disk-image-size | |
233 | #:file-system-type #$file-system-type | |
234 | #:file-system-label #$file-system-label) | |
235 | (reboot)))) | |
236 | #:system system | |
237 | #:make-disk-image? #t | |
238 | #:disk-image-size disk-image-size | |
239 | #:disk-image-format disk-image-format | |
240 | #:references-graphs inputs)) | |
04086015 LC |
241 | |
242 | \f | |
243 | ;;; | |
1e77fedb | 244 | ;;; VM and disk images. |
04086015 LC |
245 | ;;; |
246 | ||
1e77fedb LC |
247 | (define* (system-disk-image os |
248 | #:key | |
56ef7fcc | 249 | (name "disk-image") |
1e77fedb LC |
250 | (file-system-type "ext4") |
251 | (disk-image-size (* 900 (expt 2 20))) | |
252 | (volatile? #t)) | |
253 | "Return the derivation of a disk image of DISK-IMAGE-SIZE bytes of the | |
254 | system described by OS. Said image can be copied on a USB stick as is. When | |
255 | VOLATILE? is true, the root file system is made volatile; this is useful | |
256 | to USB sticks meant to be read-only." | |
10ace2c4 LC |
257 | (define root-label |
258 | ;; Volume name of the root file system. Since we don't know which device | |
259 | ;; will hold it, we use the volume name to find it (using the UUID would | |
260 | ;; be even better, but somewhat less convenient.) | |
261 | "gnu-disk-image") | |
262 | ||
1e77fedb LC |
263 | (define file-systems-to-keep |
264 | (remove (lambda (fs) | |
265 | (string=? (file-system-mount-point fs) "/")) | |
266 | (operating-system-file-systems os))) | |
267 | ||
268 | (let ((os (operating-system (inherit os) | |
932e1f92 LC |
269 | ;; Since this is meant to be used on real hardware, don't |
270 | ;; install QEMU networking or anything like that, but make sure | |
271 | ;; USB mass storage devices are available. | |
52ac153e LC |
272 | (initrd (lambda (file-systems . rest) |
273 | (apply base-initrd file-systems | |
274 | #:volatile-root? #t | |
275 | #:extra-modules '("usb-storage.ko") | |
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 | |
305 | of 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 | |
321 | #:qemu-networking? #t | |
322 | rest))) | |
e84d8b30 | 323 | |
1eeccc2f LC |
324 | ;; Force our own root file system. |
325 | (file-systems (cons (file-system | |
66f23d66 LC |
326 | (mount-point "/") |
327 | (device "/dev/sda1") | |
1eeccc2f LC |
328 | (type file-system-type)) |
329 | file-systems-to-keep))))) | |
66f23d66 LC |
330 | (mlet* %store-monad |
331 | ((os-drv (operating-system-derivation os)) | |
b4140694 | 332 | (grub.cfg (operating-system-grub.cfg os))) |
f2c403ea LC |
333 | (qemu-image #:os-derivation os-drv |
334 | #:grub-configuration grub.cfg | |
66f23d66 LC |
335 | #:disk-image-size disk-image-size |
336 | #:file-system-type file-system-type | |
b4140694 LC |
337 | #:inputs `(("system" ,os-drv) |
338 | ("grub.cfg" ,grub.cfg)) | |
150e20dd | 339 | #:copy-inputs? #t)))) |
04086015 | 340 | |
83bcd0b8 LC |
341 | (define (virtualized-operating-system os) |
342 | "Return an operating system based on OS suitable for use in a virtualized | |
343 | environment with the store shared with the host." | |
344 | (operating-system (inherit os) | |
52ac153e LC |
345 | (initrd (lambda (file-systems . rest) |
346 | (apply base-initrd file-systems | |
347 | #:volatile-root? #t | |
348 | #:virtio? #t | |
349 | #:qemu-networking? #t | |
350 | rest))) | |
65fb4515 LC |
351 | |
352 | ;; Disable swap. | |
353 | (swap-devices '()) | |
354 | ||
1eeccc2f LC |
355 | (file-systems (cons* (file-system |
356 | (mount-point "/") | |
357 | (device "/dev/vda1") | |
358 | (type "ext4")) | |
359 | (file-system | |
360 | (mount-point (%store-prefix)) | |
361 | (device "store") | |
362 | (type "9p") | |
363 | (needed-for-boot? #t) | |
364 | (options "trans=virtio") | |
365 | (check? #f)) | |
366 | ||
367 | ;; Remove file systems that conflict with those | |
368 | ;; above, or that are normally bound to real devices. | |
369 | (remove (lambda (fs) | |
370 | (let ((target (file-system-mount-point fs)) | |
371 | (source (file-system-device fs))) | |
372 | (or (string=? target (%store-prefix)) | |
373 | (string=? target "/") | |
374 | (string-prefix? "/dev/" source)))) | |
375 | (operating-system-file-systems os)))))) | |
83bcd0b8 | 376 | |
fd3bfc44 | 377 | (define* (system-qemu-image/shared-store |
0b14d1d7 | 378 | os |
fd3bfc44 LC |
379 | #:key (disk-image-size (* 15 (expt 2 20)))) |
380 | "Return a derivation that builds a QEMU image of OS that shares its store | |
381 | with the host." | |
382 | (mlet* %store-monad | |
383 | ((os-drv (operating-system-derivation os)) | |
b4140694 | 384 | (grub.cfg (operating-system-grub.cfg os))) |
f2c403ea LC |
385 | (qemu-image #:os-derivation os-drv |
386 | #:grub-configuration grub.cfg | |
150e20dd LC |
387 | #:disk-image-size disk-image-size |
388 | #:inputs `(("system" ,os-drv)) | |
389 | ||
390 | ;; XXX: Passing #t here is too slow, so let it off by default. | |
391 | #:register-closures? #f | |
392 | #:copy-inputs? #f))) | |
fd3bfc44 LC |
393 | |
394 | (define* (system-qemu-image/shared-store-script | |
0b14d1d7 | 395 | os |
fd3bfc44 | 396 | #:key |
1f3838ac | 397 | (qemu qemu) |
fd3bfc44 LC |
398 | (graphic? #t)) |
399 | "Return a derivation that builds a script to run a virtual machine image of | |
400 | OS that shares its store with the host." | |
c47f0d8b | 401 | (mlet* %store-monad |
83bcd0b8 | 402 | ((os -> (virtualized-operating-system os)) |
c47f0d8b | 403 | (os-drv (operating-system-derivation os)) |
c47f0d8b | 404 | (image (system-qemu-image/shared-store os))) |
fd3bfc44 | 405 | (define builder |
02100028 LC |
406 | #~(call-with-output-file #$output |
407 | (lambda (port) | |
408 | (display | |
409 | (string-append "#!" #$bash "/bin/sh | |
66670cf3 LC |
410 | exec " #$qemu "/bin/" #$(qemu-command (%current-system)) |
411 | " -enable-kvm -no-reboot -net nic,model=virtio \ | |
02100028 | 412 | -virtfs local,path=" #$(%store-prefix) ",security_model=none,mount_tag=store \ |
1f3838ac | 413 | -net user \ |
02100028 | 414 | -kernel " #$(operating-system-kernel os) "/bzImage \ |
83bcd0b8 | 415 | -initrd " #$os-drv "/initrd \ |
02100028 | 416 | -append \"" #$(if graphic? "" "console=ttyS0 ") |
b4140694 | 417 | "--system=" #$os-drv " --load=" #$os-drv "/boot --root=/dev/vda1\" \ |
5a84a6c3 | 418 | -serial stdio \ |
02100028 | 419 | -drive file=" #$image |
70608adb LC |
420 | ",if=virtio,cache=writeback,werror=report,readonly \ |
421 | -m 256 | |
422 | \n") | |
02100028 LC |
423 | port) |
424 | (chmod port #o555)))) | |
425 | ||
426 | (gexp->derivation "run-vm.sh" builder))) | |
fd3bfc44 | 427 | |
04086015 | 428 | ;;; vm.scm ends here |