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