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