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