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