Commit | Line | Data |
---|---|---|
04086015 | 1 | ;;; GNU Guix --- Functional package management for GNU |
09844816 | 2 | ;;; Copyright © 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020 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> |
8c9bf294 | 7 | ;;; Copyright © 2018 Chris Marusich <cmmarusich@gmail.com> |
04086015 LC |
8 | ;;; |
9 | ;;; This file is part of GNU Guix. | |
10 | ;;; | |
11 | ;;; GNU Guix is free software; you can redistribute it and/or modify it | |
12 | ;;; under the terms of the GNU General Public License as published by | |
13 | ;;; the Free Software Foundation; either version 3 of the License, or (at | |
14 | ;;; your option) any later version. | |
15 | ;;; | |
16 | ;;; GNU Guix is distributed in the hope that it will be useful, but | |
17 | ;;; WITHOUT ANY WARRANTY; without even the implied warranty of | |
18 | ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
19 | ;;; GNU General Public License for more details. | |
20 | ;;; | |
21 | ;;; You should have received a copy of the GNU General Public License | |
22 | ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>. | |
23 | ||
24 | (define-module (gnu system vm) | |
93d44bd8 | 25 | #:use-module (guix config) |
04086015 | 26 | #:use-module (guix store) |
02100028 | 27 | #:use-module (guix gexp) |
04086015 LC |
28 | #:use-module (guix derivations) |
29 | #:use-module (guix packages) | |
d9f0a237 | 30 | #:use-module (guix monads) |
fcf63cf8 | 31 | #:use-module (guix records) |
239c6e27 | 32 | #:use-module (guix modules) |
00e39b2e | 33 | #:use-module (guix utils) |
ca719424 | 34 | #:use-module (gcrypt hash) |
dffc5ab5 | 35 | #:use-module (guix base32) |
c45477d2 | 36 | #:use-module ((guix self) #:select (make-config.scm)) |
fcf63cf8 | 37 | |
548f7a8f | 38 | #:use-module ((gnu build vm) |
66670cf3 | 39 | #:select (qemu-command)) |
bdb36958 | 40 | #:use-module (gnu packages base) |
862e38d5 | 41 | #:use-module (gnu packages bootloaders) |
be1033a3 | 42 | #:use-module (gnu packages cdrom) |
a335f6fc | 43 | #:use-module (gnu packages compression) |
1b89a66e | 44 | #:use-module (gnu packages guile) |
ca719424 | 45 | #:autoload (gnu packages gnupg) (guile-gcrypt) |
bdb36958 | 46 | #:use-module (gnu packages gawk) |
1b89a66e | 47 | #:use-module (gnu packages bash) |
4f62d8d6 | 48 | #:use-module (gnu packages less) |
59132b80 | 49 | #:use-module (gnu packages virtualization) |
cc4a2aeb | 50 | #:use-module (gnu packages disk) |
5b16ff09 | 51 | #:use-module (gnu packages zile) |
04086015 | 52 | #:use-module (gnu packages linux) |
9de46ffb | 53 | #:use-module (gnu packages admin) |
0ded70f3 | 54 | |
9121ce55 | 55 | #:use-module (gnu bootloader) |
9b396c0c | 56 | #:use-module (gnu bootloader grub) |
0ded70f3 | 57 | #:use-module (gnu system shadow) |
6e828634 | 58 | #:use-module (gnu system pam) |
69cae3d3 | 59 | #:use-module (gnu system linux-container) |
735c6dd7 | 60 | #:use-module (gnu system linux-initrd) |
b09a8da4 | 61 | #:use-module (gnu bootloader) |
c5df1839 | 62 | #:use-module (gnu system file-systems) |
033adfe7 | 63 | #:use-module (gnu system) |
db4fdc04 | 64 | #:use-module (gnu services) |
d03de6be | 65 | #:use-module (gnu services base) |
9b336338 | 66 | #:use-module (gnu system uuid) |
0ded70f3 | 67 | |
ca85d7bc | 68 | #:use-module (srfi srfi-1) |
04086015 | 69 | #:use-module (srfi srfi-26) |
5f7fe1c5 | 70 | #:use-module (rnrs bytevectors) |
04086015 | 71 | #:use-module (ice-9 match) |
0ded70f3 | 72 | |
04086015 | 73 | #:export (expression->derivation-in-linux-vm |
aedb72fb | 74 | qemu-image |
e9f693d0 | 75 | virtualized-operating-system |
fd3bfc44 | 76 | system-qemu-image |
fcf63cf8 | 77 | |
fd3bfc44 | 78 | system-qemu-image/shared-store |
1e77fedb | 79 | system-qemu-image/shared-store-script |
ed419fa0 | 80 | system-disk-image |
a335f6fc | 81 | system-docker-image |
ed419fa0 LC |
82 | |
83 | virtual-machine | |
84 | virtual-machine?)) | |
04086015 LC |
85 | |
86 | \f | |
87 | ;;; Commentary: | |
88 | ;;; | |
89 | ;;; Tools to evaluate build expressions within virtual machines. | |
90 | ;;; | |
91 | ;;; Code: | |
92 | ||
83bcd0b8 | 93 | (define %linux-vm-file-systems |
8c9bf294 CM |
94 | ;; File systems mounted for 'derivation-in-linux-vm'. These are shared with |
95 | ;; the host over 9p. | |
66ec3895 LC |
96 | ;; |
97 | ;; The 9p documentation says that cache=loose is "intended for exclusive, | |
98 | ;; read-only mounts", without additional details. It's much faster than the | |
99 | ;; default cache=none, especially when copying and registering store items. | |
100 | ;; Thus, use cache=loose, except for /xchg where we want to ensure | |
101 | ;; consistency. | |
83bcd0b8 LC |
102 | (list (file-system |
103 | (mount-point (%store-prefix)) | |
104 | (device "store") | |
105 | (type "9p") | |
106 | (needed-for-boot? #t) | |
fce22547 LC |
107 | (flags '(read-only)) |
108 | (options "trans=virtio,cache=loose") | |
3c05b4bc | 109 | (check? #f)) |
83bcd0b8 LC |
110 | (file-system |
111 | (mount-point "/xchg") | |
112 | (device "xchg") | |
113 | (type "9p") | |
114 | (needed-for-boot? #t) | |
66ec3895 | 115 | (options "trans=virtio") |
8c9bf294 CM |
116 | (check? #f)) |
117 | (file-system | |
118 | (mount-point "/tmp") | |
119 | (device "tmp") | |
120 | (type "9p") | |
121 | (needed-for-boot? #t) | |
fce22547 | 122 | (options "trans=virtio,cache=loose") |
3c05b4bc | 123 | (check? #f)))) |
83bcd0b8 | 124 | |
c45477d2 LC |
125 | (define not-config? |
126 | ;; Select (guix …) and (gnu …) modules, except (guix config). | |
127 | (match-lambda | |
128 | (('guix 'config) #f) | |
129 | (('guix rest ...) #t) | |
130 | (('gnu rest ...) #t) | |
131 | (rest #f))) | |
132 | ||
ca719424 LC |
133 | (define gcrypt-sqlite3&co |
134 | ;; Guile-Gcrypt, Guile-SQLite3, and their propagated inputs. | |
135 | (append-map (lambda (package) | |
136 | (cons package | |
910d0121 LC |
137 | (match (package-transitive-propagated-inputs package) |
138 | (((labels packages) ...) | |
139 | packages)))) | |
ca719424 | 140 | (list guile-gcrypt guile-sqlite3))) |
c45477d2 | 141 | |
d9f0a237 | 142 | (define* (expression->derivation-in-linux-vm name exp |
04086015 | 143 | #:key |
d4ddf22d | 144 | (system (%current-system)) target |
04086015 | 145 | (linux linux-libre) |
735c6dd7 | 146 | initrd |
06da1a6b | 147 | (qemu qemu-minimal) |
04086015 | 148 | (env-vars '()) |
04086015 LC |
149 | (guile-for-build |
150 | (%guile-for-build)) | |
50e53c1c LC |
151 | (file-systems |
152 | %linux-vm-file-systems) | |
04086015 | 153 | |
8d033e3e | 154 | (single-file-output? #f) |
04086015 | 155 | (make-disk-image? #f) |
ca85d7bc | 156 | (references-graphs #f) |
defa1b9b | 157 | (memory-size 256) |
c4a74364 | 158 | (disk-image-format "qcow2") |
a328f66a LC |
159 | (disk-image-size 'guess) |
160 | ||
161 | (substitutable? #t)) | |
735c6dd7 | 162 | "Evaluate EXP in a QEMU virtual machine running LINUX with INITRD (a |
8d033e3e | 163 | derivation). The virtual machine runs with MEMORY-SIZE MiB of memory. In the |
50e53c1c LC |
164 | virtual machine, EXP has access to FILE-SYSTEMS, which, by default, includes a |
165 | 9p share of the store, the '/xchg' where EXP should put its output file(s), | |
166 | and a 9p share of /tmp. | |
8d033e3e LC |
167 | |
168 | If SINGLE-FILE-OUTPUT? is true, copy a single file from '/xchg' to OUTPUT. | |
169 | Otherwise, copy the contents of /xchg to a new directory OUTPUT. | |
04086015 | 170 | |
c4a74364 LC |
171 | When MAKE-DISK-IMAGE? is true, then create a QEMU disk image of type |
172 | DISK-IMAGE-FORMAT (e.g., 'qcow2' or 'raw'), of DISK-IMAGE-SIZE bytes and | |
a8ac4f08 LC |
173 | return it. When DISK-IMAGE-SIZE is 'guess, estimate the image size based |
174 | based on the size of the closure of REFERENCES-GRAPHS. | |
ca85d7bc LC |
175 | |
176 | When REFERENCES-GRAPHS is true, it must be a list of file name/store path | |
177 | pairs, as for `derivation'. The files containing the reference graphs are | |
a328f66a LC |
178 | made available under the /xchg CIFS share. |
179 | ||
180 | SUBSTITUTABLE? determines whether the returned derivation should be marked as | |
181 | substitutable." | |
be43c08b LC |
182 | (define user-builder |
183 | (program-file "builder-in-linux-vm" exp)) | |
184 | ||
185 | (define loader | |
186 | ;; Invoke USER-BUILDER instead using 'primitive-load'. The reason for | |
187 | ;; this is to allow USER-BUILDER to dlopen stuff by using a full-featured | |
188 | ;; Guile, which it couldn't do using the statically-linked guile used in | |
189 | ;; the initrd. See example at | |
190 | ;; <https://lists.gnu.org/archive/html/guix-devel/2017-10/msg00233.html>. | |
191 | (program-file "linux-vm-loader" | |
be6520e6 LC |
192 | ;; Communicate USER-BUILDER's exit status via /xchg so that |
193 | ;; the host can distinguish between success, failure, and | |
194 | ;; kernel panic. | |
195 | #~(let ((status (system* #$user-builder))) | |
196 | (call-with-output-file "/xchg/.exit-status" | |
197 | (lambda (port) | |
198 | (write status port))) | |
199 | (sync) | |
200 | (reboot)))) | |
be43c08b | 201 | |
e34ae75d LC |
202 | (let ((initrd (or initrd |
203 | (base-initrd file-systems | |
204 | #:on-error 'backtrace | |
205 | #:linux linux | |
206 | #:linux-modules %base-initrd-modules | |
207 | #:qemu-networking? #t)))) | |
1aa0033b LC |
208 | |
209 | (define builder | |
210 | ;; Code that launches the VM that evaluates EXP. | |
ca719424 | 211 | (with-extensions gcrypt-sqlite3&co |
c45477d2 LC |
212 | (with-imported-modules `(,@(source-module-closure |
213 | '((guix build utils) | |
214 | (gnu build vm)) | |
215 | #:select? not-config?) | |
ca719424 LC |
216 | |
217 | ;; For consumption by (gnu store database). | |
218 | ((guix config) => ,(make-config.scm))) | |
c45477d2 LC |
219 | #~(begin |
220 | (use-modules (guix build utils) | |
221 | (gnu build vm)) | |
222 | ||
d4ddf22d MO |
223 | (let* ((native-inputs |
224 | '#+(list qemu (canonical-package coreutils))) | |
c45477d2 LC |
225 | (linux (string-append #$linux "/" |
226 | #$(system-linux-image-file-name))) | |
d422cbb3 | 227 | (initrd #$initrd) |
c45477d2 LC |
228 | (loader #$loader) |
229 | (graphs '#$(match references-graphs | |
230 | (((graph-files . _) ...) graph-files) | |
231 | (_ #f))) | |
d4ddf22d | 232 | (target #$(or (%current-target-system) (%current-system))) |
c45477d2 LC |
233 | (size #$(if (eq? 'guess disk-image-size) |
234 | #~(+ (* 70 (expt 2 20)) ;ESP | |
235 | (estimated-partition-size graphs)) | |
236 | disk-image-size))) | |
237 | ||
d4ddf22d | 238 | (set-path-environment-variable "PATH" '("bin") native-inputs) |
c45477d2 LC |
239 | |
240 | (load-in-linux-vm loader | |
241 | #:output #$output | |
242 | #:linux linux #:initrd initrd | |
d4ddf22d | 243 | #:qemu (qemu-command target) |
c45477d2 LC |
244 | #:memory-size #$memory-size |
245 | #:make-disk-image? #$make-disk-image? | |
246 | #:single-file-output? #$single-file-output? | |
1ee72bb5 MO |
247 | ;; FIXME: ‘target-arm32?’ and |
248 | ;; ‘target-aarch64?’ may not operate on the | |
249 | ;; right system/target values. Rewrite | |
c45477d2 LC |
250 | ;; using ‘let-system’ when available. |
251 | #:target-arm32? #$(target-arm32?) | |
1ee72bb5 | 252 | #:target-aarch64? #$(target-aarch64?) |
c45477d2 LC |
253 | #:disk-image-format #$disk-image-format |
254 | #:disk-image-size size | |
255 | #:references-graphs graphs)))))) | |
1aa0033b LC |
256 | |
257 | (gexp->derivation name builder | |
258 | ;; TODO: Require the "kvm" feature. | |
259 | #:system system | |
d4ddf22d | 260 | #:target target |
1aa0033b | 261 | #:env-vars env-vars |
1aa0033b | 262 | #:guile-for-build guile-for-build |
a328f66a LC |
263 | #:references-graphs references-graphs |
264 | #:substitutable? substitutable?))) | |
d9f0a237 | 265 | |
d03de6be MC |
266 | (define (has-guix-service-type? os) |
267 | "Return true if OS contains a service of the type GUIX-SERVICE-TYPE." | |
268 | (not (not (find (lambda (service) | |
269 | (eq? (service-kind service) guix-service-type)) | |
270 | (operating-system-services os))))) | |
271 | ||
be1033a3 DM |
272 | (define* (iso9660-image #:key |
273 | (name "iso9660-image") | |
acc0f6bb DM |
274 | file-system-label |
275 | file-system-uuid | |
be1033a3 | 276 | (system (%current-system)) |
d4ddf22d | 277 | (target (%current-target-system)) |
be1033a3 | 278 | (qemu qemu-minimal) |
8bff7dc2 | 279 | os |
be1033a3 DM |
280 | bootcfg-drv |
281 | bootloader | |
d03de6be | 282 | (register-closures? (has-guix-service-type? os)) |
1d86b056 | 283 | (inputs '()) |
76026b5f LC |
284 | (grub-mkrescue-environment '()) |
285 | (substitutable? #t)) | |
be1033a3 DM |
286 | "Return a bootable, stand-alone iso9660 image. |
287 | ||
288 | INPUTS is a list of inputs (as for packages)." | |
c45477d2 LC |
289 | (define schema |
290 | (and register-closures? | |
291 | (local-file (search-path %load-path | |
292 | "guix/store/schema.sql")))) | |
293 | ||
be1033a3 DM |
294 | (expression->derivation-in-linux-vm |
295 | name | |
ca719424 | 296 | (with-extensions gcrypt-sqlite3&co |
c45477d2 LC |
297 | (with-imported-modules `(,@(source-module-closure '((gnu build vm) |
298 | (guix store database) | |
299 | (guix build utils)) | |
300 | #:select? not-config?) | |
ca719424 | 301 | ((guix config) => ,(make-config.scm))) |
c45477d2 LC |
302 | #~(begin |
303 | (use-modules (gnu build vm) | |
304 | (guix store database) | |
305 | (guix build utils)) | |
306 | ||
307 | (sql-schema #$schema) | |
308 | ||
9c941364 LC |
309 | ;; Allow non-ASCII file names--e.g., 'nss-certs'--to be decoded. |
310 | (setenv "GUIX_LOCPATH" | |
311 | #+(file-append glibc-utf8-locales "/lib/locale")) | |
312 | (setlocale LC_ALL "en_US.utf8") | |
313 | ||
c45477d2 | 314 | (let ((inputs |
d4ddf22d | 315 | '#$(append (list parted e2fsprogs dosfstools xorriso) |
c45477d2 LC |
316 | (map canonical-package |
317 | (list sed grep coreutils findutils gawk)))) | |
318 | ||
319 | ||
320 | (graphs '#$(match inputs | |
321 | (((names . _) ...) | |
322 | names))) | |
323 | ;; This variable is unused but allows us to add INPUTS-TO-COPY | |
324 | ;; as inputs. | |
325 | (to-register | |
326 | '#$(map (match-lambda | |
327 | ((name thing) thing) | |
328 | ((name thing output) `(,thing ,output))) | |
329 | inputs))) | |
330 | ||
331 | (set-path-environment-variable "PATH" '("bin" "sbin") inputs) | |
1d86b056 DM |
332 | (make-iso9660-image #$xorriso |
333 | '#$grub-mkrescue-environment | |
334 | #$(bootloader-package bootloader) | |
c45477d2 | 335 | #$bootcfg-drv |
8bff7dc2 | 336 | #$os |
c45477d2 LC |
337 | "/xchg/guixsd.iso" |
338 | #:register-closures? #$register-closures? | |
339 | #:closures graphs | |
340 | #:volume-id #$file-system-label | |
341 | #:volume-uuid #$(and=> file-system-uuid | |
342 | uuid-bytevector)))))) | |
be1033a3 | 343 | #:system system |
d4ddf22d | 344 | #:target target |
fb126314 LC |
345 | |
346 | ;; Keep a local file system for /tmp so that we can populate it directly as | |
347 | ;; root and have files owned by root. See <https://bugs.gnu.org/31752>. | |
348 | #:file-systems (remove (lambda (file-system) | |
349 | (string=? (file-system-mount-point file-system) | |
350 | "/tmp")) | |
351 | %linux-vm-file-systems) | |
352 | ||
be1033a3 | 353 | #:make-disk-image? #f |
8d033e3e | 354 | #:single-file-output? #t |
45c0d1d7 | 355 | #:references-graphs inputs |
76026b5f | 356 | #:substitutable? substitutable? |
45c0d1d7 LC |
357 | |
358 | ;; Xorriso seems to be quite memory-hungry, so increase the VM's RAM size. | |
359 | #:memory-size 512)) | |
be1033a3 | 360 | |
d9f0a237 | 361 | (define* (qemu-image #:key |
04086015 LC |
362 | (name "qemu-image") |
363 | (system (%current-system)) | |
d4ddf22d | 364 | (target (%current-target-system)) |
06da1a6b | 365 | (qemu qemu-minimal) |
a8ac4f08 | 366 | (disk-image-size 'guess) |
c4a74364 | 367 | (disk-image-format "qcow2") |
03ddfaf5 | 368 | (file-system-type "ext4") |
ef9fc40d | 369 | file-system-label |
fd3b4b98 | 370 | file-system-uuid |
8bff7dc2 | 371 | os |
9121ce55 MO |
372 | bootcfg-drv |
373 | bootloader | |
d03de6be | 374 | (register-closures? (has-guix-service-type? os)) |
150e20dd | 375 | (inputs '()) |
a328f66a LC |
376 | copy-inputs? |
377 | (substitutable? #t)) | |
c4a74364 | 378 | "Return a bootable, stand-alone QEMU image of type DISK-IMAGE-FORMAT (e.g., |
ef9fc40d LC |
379 | 'qcow2' or 'raw'), with a root partition of type FILE-SYSTEM-TYPE. |
380 | Optionally, FILE-SYSTEM-LABEL can be specified as the volume name for the root | |
fd3b4b98 LC |
381 | partition; likewise FILE-SYSTEM-UUID, if true, specifies the UUID of the root |
382 | partition (a UUID object). | |
383 | ||
384 | The returned image is a full disk image that runs OS-DERIVATION, | |
f2c403ea LC |
385 | with a GRUB installation that uses GRUB-CONFIGURATION as its configuration |
386 | file (GRUB-CONFIGURATION must be the name of a file in the VM.) | |
93d44bd8 | 387 | |
150e20dd LC |
388 | INPUTS is a list of inputs (as for packages). When COPY-INPUTS? is true, copy |
389 | all of INPUTS into the image being built. When REGISTER-CLOSURES? is true, | |
390 | register INPUTS in the store database of the image so that Guix can be used in | |
d03de6be MC |
391 | the image. By default, REGISTER-CLOSURES? is set to true only if a service of |
392 | type GUIX-SERVICE-TYPE is present in the services definition of the operating | |
393 | system." | |
c45477d2 LC |
394 | (define schema |
395 | (and register-closures? | |
396 | (local-file (search-path %load-path | |
397 | "guix/store/schema.sql")))) | |
398 | ||
b53833b2 LC |
399 | (expression->derivation-in-linux-vm |
400 | name | |
ca719424 | 401 | (with-extensions gcrypt-sqlite3&co |
c45477d2 LC |
402 | (with-imported-modules `(,@(source-module-closure '((gnu build vm) |
403 | (gnu build bootloader) | |
404 | (guix store database) | |
405 | (guix build utils)) | |
406 | #:select? not-config?) | |
ca719424 | 407 | ((guix config) => ,(make-config.scm))) |
c45477d2 LC |
408 | #~(begin |
409 | (use-modules (gnu build bootloader) | |
410 | (gnu build vm) | |
411 | (guix store database) | |
412 | (guix build utils) | |
413 | (srfi srfi-26) | |
414 | (ice-9 binary-ports)) | |
415 | ||
416 | (sql-schema #$schema) | |
417 | ||
9c941364 LC |
418 | ;; Allow non-ASCII file names--e.g., 'nss-certs'--to be decoded. |
419 | (setenv "GUIX_LOCPATH" | |
420 | #+(file-append glibc-utf8-locales "/lib/locale")) | |
421 | (setlocale LC_ALL "en_US.utf8") | |
422 | ||
c45477d2 | 423 | (let ((inputs |
d4ddf22d | 424 | '#$(append (list parted e2fsprogs dosfstools) |
c45477d2 LC |
425 | (map canonical-package |
426 | (list sed grep coreutils findutils gawk)))) | |
427 | ||
428 | ;; This variable is unused but allows us to add INPUTS-TO-COPY | |
429 | ;; as inputs. | |
430 | (to-register | |
431 | '#$(map (match-lambda | |
432 | ((name thing) thing) | |
433 | ((name thing output) `(,thing ,output))) | |
434 | inputs))) | |
435 | ||
436 | (set-path-environment-variable "PATH" '("bin" "sbin") inputs) | |
437 | ||
438 | (let* ((graphs '#$(match inputs | |
439 | (((names . _) ...) | |
440 | names))) | |
441 | (initialize (root-partition-initializer | |
442 | #:closures graphs | |
443 | #:copy-closures? #$copy-inputs? | |
444 | #:register-closures? #$register-closures? | |
8bff7dc2 | 445 | #:system-directory #$os |
b5460d95 LC |
446 | |
447 | ;; Disable deduplication to speed things up, | |
448 | ;; and because it doesn't help much for a | |
449 | ;; single system generation. | |
450 | #:deduplicate? #f)) | |
c45477d2 LC |
451 | (root-size #$(if (eq? 'guess disk-image-size) |
452 | #~(max | |
453 | ;; Minimum 20 MiB root size | |
454 | (* 20 (expt 2 20)) | |
455 | (estimated-partition-size | |
456 | (map (cut string-append "/xchg/" <>) | |
457 | graphs))) | |
458 | (- disk-image-size | |
459 | (* 50 (expt 2 20))))) | |
460 | (partitions | |
461 | (append | |
462 | (list (partition | |
463 | (size root-size) | |
464 | (label #$file-system-label) | |
465 | (uuid #$(and=> file-system-uuid | |
466 | uuid-bytevector)) | |
467 | (file-system #$file-system-type) | |
468 | (flags '(boot)) | |
469 | (initializer initialize))) | |
470 | ;; Append a small EFI System Partition for use with UEFI | |
471 | ;; bootloaders if we are not targeting ARM because UEFI | |
472 | ;; support in U-Boot is experimental. | |
473 | ;; | |
1ee72bb5 | 474 | ;; FIXME: ‘target-arm?’ may be not operate on the right |
c45477d2 LC |
475 | ;; system/target values. Rewrite using ‘let-system’ when |
476 | ;; available. | |
1ee72bb5 | 477 | (if #$(target-arm?) |
c45477d2 LC |
478 | '() |
479 | (list (partition | |
480 | ;; The standalone grub image is about 10MiB, but | |
481 | ;; leave some room for custom or multiple images. | |
482 | (size (* 40 (expt 2 20))) | |
483 | (label "GNU-ESP") ;cosmetic only | |
484 | ;; Use "vfat" here since this property is used | |
485 | ;; when mounting. The actual FAT-ness is based | |
486 | ;; on file system size (16 in this case). | |
487 | (file-system "vfat") | |
1ee72bb5 MO |
488 | (flags '(esp))))))) |
489 | (grub-efi #$(and (not (target-arm?)) grub-efi))) | |
c45477d2 LC |
490 | (initialize-hard-disk "/dev/vda" |
491 | #:partitions partitions | |
1ee72bb5 | 492 | #:grub-efi grub-efi |
c45477d2 LC |
493 | #:bootloader-package |
494 | #$(bootloader-package bootloader) | |
495 | #:bootcfg #$bootcfg-drv | |
496 | #:bootcfg-location | |
497 | #$(bootloader-configuration-file bootloader) | |
498 | #:bootloader-installer | |
499 | #$(bootloader-installer bootloader))))))) | |
b53833b2 | 500 | #:system system |
d4ddf22d | 501 | #:target target |
b53833b2 LC |
502 | #:make-disk-image? #t |
503 | #:disk-image-size disk-image-size | |
504 | #:disk-image-format disk-image-format | |
a328f66a LC |
505 | #:references-graphs inputs |
506 | #:substitutable? substitutable?)) | |
04086015 | 507 | |
a335f6fc CM |
508 | (define* (system-docker-image os |
509 | #:key | |
247649d4 | 510 | (name "guix-docker-image") |
d03de6be | 511 | (register-closures? (has-guix-service-type? os))) |
a335f6fc | 512 | "Build a docker image. OS is the desired <operating-system>. NAME is the |
d03de6be MC |
513 | base name to use for the output file. When REGISTER-CLOSURES? is true, |
514 | register the closure of OS with Guix in the resulting Docker image. By | |
515 | default, REGISTER-CLOSURES? is set to true only if a service of type | |
516 | GUIX-SERVICE-TYPE is present in the services definition of the operating | |
517 | system." | |
c45477d2 LC |
518 | (define schema |
519 | (and register-closures? | |
520 | (local-file (search-path %load-path | |
521 | "guix/store/schema.sql")))) | |
9f160a0d | 522 | |
247649d4 LC |
523 | (define boot-program |
524 | ;; Program that runs the boot script of OS, which in turn starts shepherd. | |
525 | (program-file "boot-program" | |
526 | #~(let ((system (cadr (command-line)))) | |
527 | (setenv "GUIX_NEW_SYSTEM" system) | |
528 | (execl #$(file-append guile-2.2 "/bin/guile") | |
529 | "guile" "--no-auto-compile" | |
530 | (string-append system "/boot"))))) | |
531 | ||
532 | ||
533 | (let ((os (operating-system-with-gc-roots | |
534 | (containerized-operating-system os '()) | |
535 | (list boot-program))) | |
69cae3d3 LC |
536 | (name (string-append name ".tar.gz")) |
537 | (graph "system-graph")) | |
a335f6fc | 538 | (define build |
81c3dc32 | 539 | (with-extensions (cons guile-json-3 ;for (guix docker) |
ca719424 | 540 | gcrypt-sqlite3&co) ;for (guix store database) |
9f160a0d LC |
541 | (with-imported-modules `(,@(source-module-closure |
542 | '((guix docker) | |
c45477d2 | 543 | (guix store database) |
9f160a0d | 544 | (guix build utils) |
c45477d2 | 545 | (guix build store-copy) |
9f160a0d LC |
546 | (gnu build vm)) |
547 | #:select? not-config?) | |
ca719424 | 548 | ((guix config) => ,(make-config.scm))) |
9f160a0d LC |
549 | #~(begin |
550 | (use-modules (guix docker) | |
551 | (guix build utils) | |
552 | (gnu build vm) | |
553 | (srfi srfi-19) | |
c45477d2 LC |
554 | (guix build store-copy) |
555 | (guix store database)) | |
556 | ||
557 | ;; Set the SQL schema location. | |
558 | (sql-schema #$schema) | |
9f160a0d | 559 | |
ed504caf LC |
560 | ;; Allow non-ASCII file names--e.g., 'nss-certs'--to be decoded. |
561 | (setenv "GUIX_LOCPATH" | |
562 | #+(file-append glibc-utf8-locales "/lib/locale")) | |
563 | (setlocale LC_ALL "en_US.utf8") | |
564 | ||
c45477d2 | 565 | (let* (;; This initializer requires elevated privileges that are |
9f160a0d LC |
566 | ;; not normally available in the build environment (e.g., |
567 | ;; it needs to create device nodes). In order to obtain | |
568 | ;; such privileges, we run it as root in a VM. | |
569 | (initialize (root-partition-initializer | |
570 | #:closures '(#$graph) | |
571 | #:register-closures? #$register-closures? | |
69cae3d3 | 572 | #:system-directory #$os |
9f160a0d LC |
573 | ;; De-duplication would fail due to |
574 | ;; cross-device link errors, so don't do it. | |
575 | #:deduplicate? #f)) | |
576 | ;; Even as root in a VM, the initializer would fail due to | |
577 | ;; lack of privileges if we use a root-directory that is on | |
578 | ;; a file system that is shared with the host (e.g., /tmp). | |
579 | (root-directory "/guixsd-system-root")) | |
c45477d2 | 580 | (set-path-environment-variable "PATH" '("bin" "sbin") '(#+tar)) |
9f160a0d LC |
581 | (mkdir root-directory) |
582 | (initialize root-directory) | |
583 | (build-docker-image | |
584 | (string-append "/xchg/" #$name) ;; The output file. | |
585 | (cons* root-directory | |
6892f0a2 LC |
586 | (map store-info-item |
587 | (call-with-input-file | |
588 | (string-append "/xchg/" #$graph) | |
589 | read-reference-graph))) | |
69cae3d3 | 590 | #$os |
247649d4 | 591 | #:entry-point '(#$boot-program #$os) |
9f160a0d LC |
592 | #:compressor '(#+(file-append gzip "/bin/gzip") "-9n") |
593 | #:creation-time (make-time time-utc 0 1) | |
66ec3895 | 594 | #:transformations `((,root-directory -> "")))))))) |
247649d4 | 595 | |
a335f6fc | 596 | (expression->derivation-in-linux-vm |
be43c08b | 597 | name build |
a335f6fc CM |
598 | #:make-disk-image? #f |
599 | #:single-file-output? #t | |
69cae3d3 | 600 | #:references-graphs `((,graph ,os))))) |
a335f6fc | 601 | |
04086015 LC |
602 | \f |
603 | ;;; | |
1e77fedb | 604 | ;;; VM and disk images. |
04086015 LC |
605 | ;;; |
606 | ||
5f7fe1c5 | 607 | |
1e77fedb LC |
608 | (define* (system-disk-image os |
609 | #:key | |
56ef7fcc | 610 | (name "disk-image") |
1e77fedb LC |
611 | (file-system-type "ext4") |
612 | (disk-image-size (* 900 (expt 2 20))) | |
a328f66a LC |
613 | (volatile? #t) |
614 | (substitutable? #t)) | |
1e77fedb LC |
615 | "Return the derivation of a disk image of DISK-IMAGE-SIZE bytes of the |
616 | system described by OS. Said image can be copied on a USB stick as is. When | |
617 | VOLATILE? is true, the root file system is made volatile; this is useful | |
a328f66a LC |
618 | to USB sticks meant to be read-only. |
619 | ||
620 | SUBSTITUTABLE? determines whether the returned derivation should be marked as | |
621 | substitutable." | |
651de2bd DM |
622 | (define normalize-label |
623 | ;; ISO labels are all-caps (case-insensitive), but since | |
624 | ;; 'find-partition-by-label' is case-sensitive, make it all-caps here. | |
625 | (if (string=? "iso9660" file-system-type) | |
626 | string-upcase | |
627 | identity)) | |
5f7fe1c5 | 628 | |
10ace2c4 | 629 | (define root-label |
5f7fe1c5 | 630 | ;; Volume name of the root file system. |
59e80445 | 631 | (normalize-label "Guix_image")) |
10ace2c4 | 632 | |
5be7aecd | 633 | (define (root-uuid os) |
5f7fe1c5 LC |
634 | ;; UUID of the root file system, computed in a deterministic fashion. |
635 | ;; This is what we use to locate the root file system so it has to be | |
636 | ;; different from the user's own file system UUIDs. | |
637 | (operating-system-uuid os | |
638 | (if (string=? file-system-type "iso9660") | |
639 | 'iso9660 | |
640 | 'dce))) | |
641 | ||
1e77fedb LC |
642 | (define file-systems-to-keep |
643 | (remove (lambda (fs) | |
644 | (string=? (file-system-mount-point fs) "/")) | |
645 | (operating-system-file-systems os))) | |
646 | ||
8bff7dc2 LC |
647 | (let* ((os (operating-system (inherit os) |
648 | ;; Since this is meant to be used on real hardware, don't | |
649 | ;; install QEMU networking or anything like that. Assume USB | |
650 | ;; mass storage devices (usb-storage.ko) are available. | |
651 | (initrd (lambda (file-systems . rest) | |
652 | (apply (operating-system-initrd os) | |
653 | file-systems | |
67ed5442 | 654 | #:volatile-root? volatile? |
8bff7dc2 LC |
655 | rest))) |
656 | ||
657 | (bootloader (if (string=? "iso9660" file-system-type) | |
658 | (bootloader-configuration | |
659 | (inherit (operating-system-bootloader os)) | |
660 | (bootloader grub-mkrescue-bootloader)) | |
661 | (operating-system-bootloader os))) | |
662 | ||
5be7aecd LC |
663 | ;; Force our own root file system. (We need a "/" file system |
664 | ;; to call 'root-uuid'.) | |
8bff7dc2 LC |
665 | (file-systems (cons (file-system |
666 | (mount-point "/") | |
5be7aecd LC |
667 | (device "/dev/placeholder") |
668 | (type file-system-type)) | |
669 | file-systems-to-keep)))) | |
670 | (uuid (root-uuid os)) | |
671 | (os (operating-system | |
672 | (inherit os) | |
673 | (file-systems (cons (file-system | |
674 | (mount-point "/") | |
675 | (device uuid) | |
8bff7dc2 LC |
676 | (type file-system-type)) |
677 | file-systems-to-keep)))) | |
678 | (bootcfg (operating-system-bootcfg os))) | |
679 | (if (string=? "iso9660" file-system-type) | |
680 | (iso9660-image #:name name | |
681 | #:file-system-label root-label | |
5be7aecd | 682 | #:file-system-uuid uuid |
8bff7dc2 | 683 | #:os os |
8bff7dc2 LC |
684 | #:bootcfg-drv bootcfg |
685 | #:bootloader (bootloader-configuration-bootloader | |
686 | (operating-system-bootloader os)) | |
687 | #:inputs `(("system" ,os) | |
1d86b056 DM |
688 | ("bootcfg" ,bootcfg)) |
689 | #:grub-mkrescue-environment | |
76026b5f LC |
690 | '(("MKRESCUE_SED_MODE" . "mbr_hfs")) |
691 | #:substitutable? substitutable?) | |
8bff7dc2 LC |
692 | (qemu-image #:name name |
693 | #:os os | |
694 | #:bootcfg-drv bootcfg | |
695 | #:bootloader (bootloader-configuration-bootloader | |
696 | (operating-system-bootloader os)) | |
697 | #:disk-image-size disk-image-size | |
698 | #:disk-image-format "raw" | |
699 | #:file-system-type file-system-type | |
700 | #:file-system-label root-label | |
5be7aecd | 701 | #:file-system-uuid uuid |
8bff7dc2 | 702 | #:copy-inputs? #t |
8bff7dc2 | 703 | #:inputs `(("system" ,os) |
a328f66a LC |
704 | ("bootcfg" ,bootcfg)) |
705 | #:substitutable? substitutable?)))) | |
1e77fedb | 706 | |
0b14d1d7 | 707 | (define* (system-qemu-image os |
66f23d66 LC |
708 | #:key |
709 | (file-system-type "ext4") | |
710 | (disk-image-size (* 900 (expt 2 20)))) | |
711 | "Return the derivation of a freestanding QEMU image of DISK-IMAGE-SIZE bytes | |
712 | of the GNU system as described by OS." | |
1eeccc2f LC |
713 | (define file-systems-to-keep |
714 | ;; Keep only file systems other than root and not normally bound to real | |
715 | ;; devices. | |
716 | (remove (lambda (fs) | |
717 | (let ((target (file-system-mount-point fs)) | |
718 | (source (file-system-device fs))) | |
719 | (or (string=? target "/") | |
720 | (string-prefix? "/dev/" source)))) | |
721 | (operating-system-file-systems os))) | |
722 | ||
61b94b8c LC |
723 | (define root-uuid |
724 | ;; UUID of the root file system. | |
725 | (operating-system-uuid os | |
726 | (if (string=? file-system-type "iso9660") | |
727 | 'iso9660 | |
728 | 'dce))) | |
729 | ||
730 | ||
8bff7dc2 LC |
731 | (let* ((os (operating-system (inherit os) |
732 | ;; Assume we have an initrd with the whole QEMU shebang. | |
733 | ||
734 | ;; Force our own root file system. Refer to it by UUID so that | |
735 | ;; it works regardless of how the image is used ("qemu -hda", | |
736 | ;; Xen, etc.). | |
737 | (file-systems (cons (file-system | |
738 | (mount-point "/") | |
739 | (device root-uuid) | |
740 | (type file-system-type)) | |
741 | file-systems-to-keep)))) | |
742 | (bootcfg (operating-system-bootcfg os))) | |
743 | (qemu-image #:os os | |
744 | #:bootcfg-drv bootcfg | |
745 | #:bootloader (bootloader-configuration-bootloader | |
746 | (operating-system-bootloader os)) | |
747 | #:disk-image-size disk-image-size | |
748 | #:file-system-type file-system-type | |
749 | #:file-system-uuid root-uuid | |
750 | #:inputs `(("system" ,os) | |
751 | ("bootcfg" ,bootcfg)) | |
752 | #:copy-inputs? #t))) | |
04086015 | 753 | |
fcf63cf8 LC |
754 | \f |
755 | ;;; | |
756 | ;;; VMs that share file systems with the host. | |
757 | ;;; | |
758 | ||
96ffa27b LC |
759 | (define (file-system->mount-tag fs) |
760 | "Return a 9p mount tag for host file system FS." | |
dffc5ab5 LC |
761 | ;; QEMU mount tags must be ASCII, at most 31-byte long, cannot contain |
762 | ;; slashes, and cannot start with '_'. Compute an identifier that | |
763 | ;; corresponds to the rules. | |
96ffa27b | 764 | (string-append "TAG" |
dffc5ab5 LC |
765 | (string-drop (bytevector->base32-string |
766 | (sha1 (string->utf8 fs))) | |
767 | 4))) | |
96ffa27b | 768 | |
fcf63cf8 LC |
769 | (define (mapping->file-system mapping) |
770 | "Return a 9p file system that realizes MAPPING." | |
771 | (match mapping | |
772 | (($ <file-system-mapping> source target writable?) | |
773 | (file-system | |
774 | (mount-point target) | |
775 | (device (file-system->mount-tag source)) | |
776 | (type "9p") | |
777 | (flags (if writable? '() '(read-only))) | |
e0d96774 | 778 | (options "trans=virtio,cache=loose") |
fcf63cf8 LC |
779 | (check? #f) |
780 | (create-mount-point? #t))))) | |
781 | ||
909de139 | 782 | (define* (virtualized-operating-system os mappings #:optional (full-boot? #f)) |
83bcd0b8 | 783 | "Return an operating system based on OS suitable for use in a virtualized |
fcf63cf8 LC |
784 | environment with the store shared with the host. MAPPINGS is a list of |
785 | <file-system-mapping> to realize in the virtualized OS." | |
786 | (define user-file-systems | |
787 | ;; Remove file systems that conflict with those added below, or that are | |
788 | ;; normally bound to real devices. | |
789 | (remove (lambda (fs) | |
790 | (let ((target (file-system-mount-point fs)) | |
791 | (source (file-system-device fs))) | |
792 | (or (string=? target (%store-prefix)) | |
793 | (string=? target "/") | |
a5acc17a | 794 | (and (string? source) |
f00515b4 LC |
795 | (string-prefix? "/dev/" source)) |
796 | ||
797 | ;; Labels and UUIDs are necessarily invalid in the VM. | |
798 | (and (file-system-mount? fs) | |
a5acc17a | 799 | (or (file-system-label? source) |
f00515b4 | 800 | (uuid? source)))))) |
fcf63cf8 LC |
801 | (operating-system-file-systems os))) |
802 | ||
909de139 DC |
803 | (define virtual-file-systems |
804 | (cons (file-system | |
805 | (mount-point "/") | |
806 | (device "/dev/vda1") | |
807 | (type "ext4")) | |
808 | ||
809 | (append (map mapping->file-system mappings) | |
810 | user-file-systems))) | |
811 | ||
83bcd0b8 | 812 | (operating-system (inherit os) |
9b396c0c LC |
813 | |
814 | ;; XXX: Until we run QEMU with UEFI support (with the OVMF firmware), | |
815 | ;; force the traditional i386/BIOS method. | |
816 | ;; See <https://bugs.gnu.org/28768>. | |
817 | (bootloader (bootloader-configuration | |
132823c2 | 818 | (inherit (operating-system-bootloader os)) |
9b396c0c LC |
819 | (bootloader grub-bootloader) |
820 | (target "/dev/vda"))) | |
821 | ||
52ac153e | 822 | (initrd (lambda (file-systems . rest) |
b8e77811 MO |
823 | (apply (operating-system-initrd os) |
824 | file-systems | |
52ac153e | 825 | #:volatile-root? #t |
52ac153e | 826 | rest))) |
65fb4515 LC |
827 | |
828 | ;; Disable swap. | |
829 | (swap-devices '()) | |
830 | ||
909de139 DC |
831 | ;; XXX: When FULL-BOOT? is true, do not add a 9p mount for /gnu/store |
832 | ;; since that would lead the bootloader config to look for the kernel and | |
833 | ;; initrd in it. | |
834 | (file-systems (if full-boot? | |
835 | virtual-file-systems | |
836 | (cons | |
837 | (file-system | |
838 | (inherit (mapping->file-system %store-mapping)) | |
839 | (needed-for-boot? #t)) | |
840 | virtual-file-systems))))) | |
83bcd0b8 | 841 | |
fd3bfc44 | 842 | (define* (system-qemu-image/shared-store |
0b14d1d7 | 843 | os |
6aa260af LC |
844 | #:key |
845 | full-boot? | |
4c0416ae | 846 | (disk-image-size (* (if full-boot? 500 30) (expt 2 20)))) |
fd3bfc44 | 847 | "Return a derivation that builds a QEMU image of OS that shares its store |
6aa260af LC |
848 | with the host. |
849 | ||
850 | When FULL-BOOT? is true, return an image that does a complete boot sequence, | |
851 | bootloaded included; thus, make a disk image that contains everything the | |
852 | bootloader refers to: OS kernel, initrd, bootloader data, etc." | |
56f9d442 LC |
853 | (define root-uuid |
854 | ;; Use a fixed UUID to improve determinism. | |
855 | (operating-system-uuid os 'dce)) | |
856 | ||
8bff7dc2 LC |
857 | (define bootcfg |
858 | (operating-system-bootcfg os)) | |
859 | ||
860 | ;; XXX: When FULL-BOOT? is true, we end up creating an image that contains | |
861 | ;; BOOTCFG and all its dependencies, including the output of OS. | |
862 | ;; This is more than needed (we only need the kernel, initrd, GRUB for its | |
863 | ;; font, and the background image), but it's hard to filter that. | |
864 | (qemu-image #:os os | |
865 | #:bootcfg-drv bootcfg | |
866 | #:bootloader (bootloader-configuration-bootloader | |
867 | (operating-system-bootloader os)) | |
868 | #:disk-image-size disk-image-size | |
869 | #:file-system-uuid root-uuid | |
870 | #:inputs (if full-boot? | |
871 | `(("bootcfg" ,bootcfg)) | |
872 | '()) | |
873 | ||
874 | ;; XXX: Passing #t here is too slow, so let it off by default. | |
875 | #:register-closures? #f | |
876 | #:copy-inputs? full-boot?)) | |
fd3bfc44 | 877 | |
96ffa27b LC |
878 | (define* (common-qemu-options image shared-fs) |
879 | "Return the a string-value gexp with the common QEMU options to boot IMAGE, | |
880 | with '-virtfs' options for the host file systems listed in SHARED-FS." | |
26a076ed | 881 | |
96ffa27b | 882 | (define (virtfs-option fs) |
26a076ed DC |
883 | #~(format #f "-virtfs local,path=~s,security_model=none,mount_tag=~s" |
884 | #$fs #$(file-system->mount-tag fs))) | |
96ffa27b | 885 | |
26a076ed | 886 | #~(;; Only enable kvm if we see /dev/kvm exists. |
944d2b17 CAW |
887 | ;; This allows users without hardware virtualization to still use these |
888 | ;; commands. | |
26a076ed DC |
889 | #$@(if (file-exists? "/dev/kvm") |
890 | '("-enable-kvm") | |
891 | '()) | |
892 | ||
893 | "-no-reboot" | |
53793927 | 894 | "-nic" "user,model=virtio-net-pci" |
2ca712bd LF |
895 | "-object" "rng-random,filename=/dev/urandom,id=guixsd-vm-rng" |
896 | "-device" "virtio-rng-pci,rng=guixsd-vm-rng" | |
26a076ed DC |
897 | |
898 | #$@(map virtfs-option shared-fs) | |
899 | "-vga std" | |
900 | (format #f "-drive file=~a,if=virtio,cache=writeback,werror=report,readonly" | |
ebfb71d4 | 901 | #$image))) |
3c1f0e3b | 902 | |
ab11f0be LC |
903 | (define* (system-qemu-image/shared-store-script os |
904 | #:key | |
905 | (qemu qemu) | |
906 | (graphic? #t) | |
ebfb71d4 | 907 | (memory-size 256) |
fcf63cf8 | 908 | (mappings '()) |
6aa260af LC |
909 | full-boot? |
910 | (disk-image-size | |
9a1bfe76 | 911 | (* (if full-boot? 500 70) |
ed419fa0 LC |
912 | (expt 2 20))) |
913 | (options '())) | |
fd3bfc44 | 914 | "Return a derivation that builds a script to run a virtual machine image of |
ebfb71d4 JN |
915 | OS that shares its store with the host. The virtual machine runs with |
916 | MEMORY-SIZE MiB of memory. | |
6aa260af | 917 | |
fcf63cf8 LC |
918 | MAPPINGS is a list of <file-system-mapping> specifying mapping of host file |
919 | systems into the guest. | |
920 | ||
6aa260af LC |
921 | When FULL-BOOT? is true, the returned script runs everything starting from the |
922 | bootloader; otherwise it directly starts the operating system kernel. The | |
923 | DISK-IMAGE-SIZE parameter specifies the size in bytes of the root disk image; | |
924 | it is mostly useful when FULL-BOOT? is true." | |
909de139 | 925 | (mlet* %store-monad ((os -> (virtualized-operating-system os mappings full-boot?)) |
6aa260af LC |
926 | (image (system-qemu-image/shared-store |
927 | os | |
928 | #:full-boot? full-boot? | |
929 | #:disk-image-size disk-image-size))) | |
26a076ed | 930 | (define kernel-arguments |
83071b05 | 931 | #~(list #$@(if graphic? #~() #~("console=ttyS0")) |
a7ef45d9 | 932 | #+@(operating-system-kernel-arguments os "/dev/vda1"))) |
26a076ed DC |
933 | |
934 | (define qemu-exec | |
935 | #~(list (string-append #$qemu "/bin/" #$(qemu-command (%current-system))) | |
936 | #$@(if full-boot? | |
937 | #~() | |
938 | #~("-kernel" #$(operating-system-kernel-file os) | |
a7ef45d9 | 939 | "-initrd" #$(file-append os "/initrd") |
26a076ed DC |
940 | (format #f "-append ~s" |
941 | (string-join #$kernel-arguments " ")))) | |
942 | #$@(common-qemu-options image | |
943 | (map file-system-mapping-source | |
ebfb71d4 | 944 | (cons %store-mapping mappings))) |
ed419fa0 LC |
945 | "-m " (number->string #$memory-size) |
946 | #$@options)) | |
26a076ed | 947 | |
fd3bfc44 | 948 | (define builder |
02100028 LC |
949 | #~(call-with-output-file #$output |
950 | (lambda (port) | |
26a076ed DC |
951 | (format port "#!~a~% exec ~a \"$@\"~%" |
952 | #$(file-append bash "/bin/sh") | |
953 | (string-join #$qemu-exec " ")) | |
02100028 LC |
954 | (chmod port #o555)))) |
955 | ||
956 | (gexp->derivation "run-vm.sh" builder))) | |
fd3bfc44 | 957 | |
ed419fa0 LC |
958 | \f |
959 | ;;; | |
960 | ;;; High-level abstraction. | |
961 | ;;; | |
962 | ||
963 | (define-record-type* <virtual-machine> %virtual-machine | |
964 | make-virtual-machine | |
965 | virtual-machine? | |
966 | (operating-system virtual-machine-operating-system) ;<operating-system> | |
967 | (qemu virtual-machine-qemu ;<package> | |
968 | (default qemu)) | |
969 | (graphic? virtual-machine-graphic? ;Boolean | |
970 | (default #f)) | |
971 | (memory-size virtual-machine-memory-size ;integer (MiB) | |
972 | (default 256)) | |
eb152070 CB |
973 | (disk-image-size virtual-machine-disk-image-size ;integer (bytes) |
974 | (default 'guess)) | |
ed419fa0 LC |
975 | (port-forwardings virtual-machine-port-forwardings ;list of integer pairs |
976 | (default '()))) | |
977 | ||
978 | (define-syntax virtual-machine | |
979 | (syntax-rules () | |
980 | "Declare a virtual machine running the specified OS, with the given | |
981 | options." | |
982 | ((_ os) ;shortcut | |
983 | (%virtual-machine (operating-system os))) | |
984 | ((_ fields ...) | |
985 | (%virtual-machine fields ...)))) | |
986 | ||
987 | (define (port-forwardings->qemu-options forwardings) | |
988 | "Return the QEMU option for the given port FORWARDINGS as a string, where | |
989 | FORWARDINGS is a list of host-port/guest-port pairs." | |
990 | (string-join | |
991 | (map (match-lambda | |
992 | ((host-port . guest-port) | |
993 | (string-append "hostfwd=tcp::" | |
994 | (number->string host-port) | |
995 | "-:" (number->string guest-port)))) | |
996 | forwardings) | |
997 | ",")) | |
998 | ||
999 | (define-gexp-compiler (virtual-machine-compiler (vm <virtual-machine>) | |
1000 | system target) | |
1001 | ;; XXX: SYSTEM and TARGET are ignored. | |
1002 | (match vm | |
eb152070 | 1003 | (($ <virtual-machine> os qemu graphic? memory-size disk-image-size ()) |
ed419fa0 LC |
1004 | (system-qemu-image/shared-store-script os |
1005 | #:qemu qemu | |
1006 | #:graphic? graphic? | |
eb152070 CB |
1007 | #:memory-size memory-size |
1008 | #:disk-image-size | |
1009 | disk-image-size)) | |
1010 | (($ <virtual-machine> os qemu graphic? memory-size disk-image-size | |
1011 | forwardings) | |
ed419fa0 | 1012 | (let ((options |
8e53fe2b MB |
1013 | `("-nic" ,(string-append |
1014 | "user,model=virtio-net-pci," | |
ed419fa0 LC |
1015 | (port-forwardings->qemu-options forwardings))))) |
1016 | (system-qemu-image/shared-store-script os | |
1017 | #:qemu qemu | |
1018 | #:graphic? graphic? | |
1019 | #:memory-size memory-size | |
eb152070 CB |
1020 | #:disk-image-size |
1021 | disk-image-size | |
ed419fa0 LC |
1022 | #:options options))))) |
1023 | ||
04086015 | 1024 | ;;; vm.scm ends here |