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