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