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 |
f19cf27c | 80 | system-disk-image-in-vm |
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 | |
502f609d LC |
202 | (define-syntax-rule (check predicate) |
203 | (let-system (system target) | |
204 | (predicate (or target system)))) | |
205 | ||
e34ae75d LC |
206 | (let ((initrd (or initrd |
207 | (base-initrd file-systems | |
208 | #:on-error 'backtrace | |
209 | #:linux linux | |
210 | #:linux-modules %base-initrd-modules | |
211 | #:qemu-networking? #t)))) | |
1aa0033b LC |
212 | |
213 | (define builder | |
214 | ;; Code that launches the VM that evaluates EXP. | |
ca719424 | 215 | (with-extensions gcrypt-sqlite3&co |
c45477d2 LC |
216 | (with-imported-modules `(,@(source-module-closure |
217 | '((guix build utils) | |
218 | (gnu build vm)) | |
219 | #:select? not-config?) | |
ca719424 LC |
220 | |
221 | ;; For consumption by (gnu store database). | |
222 | ((guix config) => ,(make-config.scm))) | |
c45477d2 LC |
223 | #~(begin |
224 | (use-modules (guix build utils) | |
225 | (gnu build vm)) | |
226 | ||
d4ddf22d MO |
227 | (let* ((native-inputs |
228 | '#+(list qemu (canonical-package coreutils))) | |
c45477d2 LC |
229 | (linux (string-append #$linux "/" |
230 | #$(system-linux-image-file-name))) | |
d422cbb3 | 231 | (initrd #$initrd) |
c45477d2 LC |
232 | (loader #$loader) |
233 | (graphs '#$(match references-graphs | |
234 | (((graph-files . _) ...) graph-files) | |
235 | (_ #f))) | |
502f609d LC |
236 | (target #$(let-system (system target) |
237 | (or target system))) | |
c45477d2 LC |
238 | (size #$(if (eq? 'guess disk-image-size) |
239 | #~(+ (* 70 (expt 2 20)) ;ESP | |
240 | (estimated-partition-size graphs)) | |
241 | disk-image-size))) | |
242 | ||
d4ddf22d | 243 | (set-path-environment-variable "PATH" '("bin") native-inputs) |
c45477d2 LC |
244 | |
245 | (load-in-linux-vm loader | |
246 | #:output #$output | |
247 | #:linux linux #:initrd initrd | |
d4ddf22d | 248 | #:qemu (qemu-command target) |
c45477d2 LC |
249 | #:memory-size #$memory-size |
250 | #:make-disk-image? #$make-disk-image? | |
251 | #:single-file-output? #$single-file-output? | |
502f609d LC |
252 | #:target-arm32? #$(check target-arm32?) |
253 | #:target-aarch64? #$(check target-aarch64?) | |
c45477d2 LC |
254 | #:disk-image-format #$disk-image-format |
255 | #:disk-image-size size | |
256 | #:references-graphs graphs)))))) | |
1aa0033b LC |
257 | |
258 | (gexp->derivation name builder | |
259 | ;; TODO: Require the "kvm" feature. | |
260 | #:system system | |
d4ddf22d | 261 | #:target target |
1aa0033b | 262 | #:env-vars env-vars |
1aa0033b | 263 | #:guile-for-build guile-for-build |
a328f66a LC |
264 | #:references-graphs references-graphs |
265 | #:substitutable? substitutable?))) | |
d9f0a237 | 266 | |
d03de6be MC |
267 | (define (has-guix-service-type? os) |
268 | "Return true if OS contains a service of the type GUIX-SERVICE-TYPE." | |
269 | (not (not (find (lambda (service) | |
270 | (eq? (service-kind service) guix-service-type)) | |
271 | (operating-system-services os))))) | |
272 | ||
d9f0a237 | 273 | (define* (qemu-image #:key |
04086015 LC |
274 | (name "qemu-image") |
275 | (system (%current-system)) | |
d4ddf22d | 276 | (target (%current-target-system)) |
06da1a6b | 277 | (qemu qemu-minimal) |
a8ac4f08 | 278 | (disk-image-size 'guess) |
c4a74364 | 279 | (disk-image-format "qcow2") |
03ddfaf5 | 280 | (file-system-type "ext4") |
4d1ff68d | 281 | (file-system-options '()) |
cd45d656 | 282 | (device-nodes 'linux) |
82782d8c | 283 | (extra-directives '()) |
ef9fc40d | 284 | file-system-label |
fd3b4b98 | 285 | file-system-uuid |
8bff7dc2 | 286 | os |
9121ce55 MO |
287 | bootcfg-drv |
288 | bootloader | |
d03de6be | 289 | (register-closures? (has-guix-service-type? os)) |
150e20dd | 290 | (inputs '()) |
a328f66a LC |
291 | copy-inputs? |
292 | (substitutable? #t)) | |
c4a74364 | 293 | "Return a bootable, stand-alone QEMU image of type DISK-IMAGE-FORMAT (e.g., |
ef9fc40d LC |
294 | 'qcow2' or 'raw'), with a root partition of type FILE-SYSTEM-TYPE. |
295 | Optionally, FILE-SYSTEM-LABEL can be specified as the volume name for the root | |
fd3b4b98 | 296 | partition; likewise FILE-SYSTEM-UUID, if true, specifies the UUID of the root |
4d1ff68d LC |
297 | partition (a UUID object). FILE-SYSTEM-OPTIONS is an optional list of |
298 | command-line options passed to 'mkfs.ext4' (or similar). | |
fd3b4b98 LC |
299 | |
300 | The returned image is a full disk image that runs OS-DERIVATION, | |
f2c403ea LC |
301 | with a GRUB installation that uses GRUB-CONFIGURATION as its configuration |
302 | file (GRUB-CONFIGURATION must be the name of a file in the VM.) | |
93d44bd8 | 303 | |
150e20dd LC |
304 | INPUTS is a list of inputs (as for packages). When COPY-INPUTS? is true, copy |
305 | all of INPUTS into the image being built. When REGISTER-CLOSURES? is true, | |
306 | register INPUTS in the store database of the image so that Guix can be used in | |
d03de6be MC |
307 | the image. By default, REGISTER-CLOSURES? is set to true only if a service of |
308 | type GUIX-SERVICE-TYPE is present in the services definition of the operating | |
82782d8c LC |
309 | system. |
310 | ||
cd45d656 LC |
311 | When DEVICE-NODES is 'linux, create Linux-device block and character devices |
312 | under /dev. When it is 'hurd, do Hurdish things. | |
313 | ||
82782d8c LC |
314 | EXTRA-DIRECTIVES is an optional list of directives to populate the root file |
315 | system that is passed to 'populate-root-file-system'." | |
c45477d2 LC |
316 | (define schema |
317 | (and register-closures? | |
318 | (local-file (search-path %load-path | |
319 | "guix/store/schema.sql")))) | |
320 | ||
b53833b2 LC |
321 | (expression->derivation-in-linux-vm |
322 | name | |
ca719424 | 323 | (with-extensions gcrypt-sqlite3&co |
c45477d2 LC |
324 | (with-imported-modules `(,@(source-module-closure '((gnu build vm) |
325 | (gnu build bootloader) | |
326 | (guix store database) | |
327 | (guix build utils)) | |
328 | #:select? not-config?) | |
ca719424 | 329 | ((guix config) => ,(make-config.scm))) |
c45477d2 LC |
330 | #~(begin |
331 | (use-modules (gnu build bootloader) | |
332 | (gnu build vm) | |
cd45d656 LC |
333 | ((gnu build linux-boot) |
334 | #:select (make-essential-device-nodes | |
335 | make-hurd-device-nodes)) | |
c45477d2 LC |
336 | (guix store database) |
337 | (guix build utils) | |
338 | (srfi srfi-26) | |
339 | (ice-9 binary-ports)) | |
340 | ||
341 | (sql-schema #$schema) | |
342 | ||
9c941364 LC |
343 | ;; Allow non-ASCII file names--e.g., 'nss-certs'--to be decoded. |
344 | (setenv "GUIX_LOCPATH" | |
345 | #+(file-append glibc-utf8-locales "/lib/locale")) | |
346 | (setlocale LC_ALL "en_US.utf8") | |
347 | ||
c45477d2 | 348 | (let ((inputs |
d4ddf22d | 349 | '#$(append (list parted e2fsprogs dosfstools) |
c45477d2 LC |
350 | (map canonical-package |
351 | (list sed grep coreutils findutils gawk)))) | |
352 | ||
353 | ;; This variable is unused but allows us to add INPUTS-TO-COPY | |
354 | ;; as inputs. | |
355 | (to-register | |
356 | '#$(map (match-lambda | |
357 | ((name thing) thing) | |
358 | ((name thing output) `(,thing ,output))) | |
359 | inputs))) | |
360 | ||
361 | (set-path-environment-variable "PATH" '("bin" "sbin") inputs) | |
362 | ||
363 | (let* ((graphs '#$(match inputs | |
364 | (((names . _) ...) | |
365 | names))) | |
366 | (initialize (root-partition-initializer | |
82782d8c | 367 | #:extra-directives '#$extra-directives |
c45477d2 LC |
368 | #:closures graphs |
369 | #:copy-closures? #$copy-inputs? | |
370 | #:register-closures? #$register-closures? | |
8bff7dc2 | 371 | #:system-directory #$os |
b5460d95 | 372 | |
cd45d656 LC |
373 | #:make-device-nodes |
374 | #$(match device-nodes | |
375 | ('linux #~make-essential-device-nodes) | |
376 | ('hurd #~make-hurd-device-nodes)) | |
377 | ||
b5460d95 LC |
378 | ;; Disable deduplication to speed things up, |
379 | ;; and because it doesn't help much for a | |
380 | ;; single system generation. | |
381 | #:deduplicate? #f)) | |
c45477d2 LC |
382 | (root-size #$(if (eq? 'guess disk-image-size) |
383 | #~(max | |
384 | ;; Minimum 20 MiB root size | |
385 | (* 20 (expt 2 20)) | |
386 | (estimated-partition-size | |
387 | (map (cut string-append "/xchg/" <>) | |
388 | graphs))) | |
389 | (- disk-image-size | |
390 | (* 50 (expt 2 20))))) | |
391 | (partitions | |
392 | (append | |
393 | (list (partition | |
394 | (size root-size) | |
395 | (label #$file-system-label) | |
396 | (uuid #$(and=> file-system-uuid | |
397 | uuid-bytevector)) | |
398 | (file-system #$file-system-type) | |
4d1ff68d | 399 | (file-system-options '#$file-system-options) |
c45477d2 LC |
400 | (flags '(boot)) |
401 | (initializer initialize))) | |
402 | ;; Append a small EFI System Partition for use with UEFI | |
403 | ;; bootloaders if we are not targeting ARM because UEFI | |
404 | ;; support in U-Boot is experimental. | |
405 | ;; | |
1ee72bb5 | 406 | ;; FIXME: ‘target-arm?’ may be not operate on the right |
c45477d2 LC |
407 | ;; system/target values. Rewrite using ‘let-system’ when |
408 | ;; available. | |
1ee72bb5 | 409 | (if #$(target-arm?) |
c45477d2 LC |
410 | '() |
411 | (list (partition | |
412 | ;; The standalone grub image is about 10MiB, but | |
413 | ;; leave some room for custom or multiple images. | |
414 | (size (* 40 (expt 2 20))) | |
415 | (label "GNU-ESP") ;cosmetic only | |
416 | ;; Use "vfat" here since this property is used | |
417 | ;; when mounting. The actual FAT-ness is based | |
418 | ;; on file system size (16 in this case). | |
419 | (file-system "vfat") | |
1ee72bb5 MO |
420 | (flags '(esp))))))) |
421 | (grub-efi #$(and (not (target-arm?)) grub-efi))) | |
c45477d2 LC |
422 | (initialize-hard-disk "/dev/vda" |
423 | #:partitions partitions | |
1ee72bb5 | 424 | #:grub-efi grub-efi |
c45477d2 LC |
425 | #:bootloader-package |
426 | #$(bootloader-package bootloader) | |
427 | #:bootcfg #$bootcfg-drv | |
428 | #:bootcfg-location | |
429 | #$(bootloader-configuration-file bootloader) | |
430 | #:bootloader-installer | |
431 | #$(bootloader-installer bootloader))))))) | |
b53833b2 | 432 | #:system system |
d4ddf22d | 433 | #:target target |
b53833b2 LC |
434 | #:make-disk-image? #t |
435 | #:disk-image-size disk-image-size | |
436 | #:disk-image-format disk-image-format | |
a328f66a LC |
437 | #:references-graphs inputs |
438 | #:substitutable? substitutable?)) | |
04086015 | 439 | |
a335f6fc CM |
440 | (define* (system-docker-image os |
441 | #:key | |
247649d4 | 442 | (name "guix-docker-image") |
d6c43d7b LC |
443 | (register-closures? (has-guix-service-type? os)) |
444 | shared-network?) | |
a335f6fc | 445 | "Build a docker image. OS is the desired <operating-system>. NAME is the |
d6c43d7b LC |
446 | base name to use for the output file. When SHARED-NETWORK? is true, assume |
447 | that the container will share network with the host and thus doesn't need a | |
448 | DHCP client, nscd, and so on. | |
449 | ||
450 | When REGISTER-CLOSURES? is true, register the closure of OS with Guix in the | |
451 | resulting Docker image. By default, REGISTER-CLOSURES? is set to true only if | |
452 | a service of type GUIX-SERVICE-TYPE is present in the services definition of | |
453 | the operating system." | |
c45477d2 LC |
454 | (define schema |
455 | (and register-closures? | |
456 | (local-file (search-path %load-path | |
457 | "guix/store/schema.sql")))) | |
9f160a0d | 458 | |
247649d4 LC |
459 | (define boot-program |
460 | ;; Program that runs the boot script of OS, which in turn starts shepherd. | |
461 | (program-file "boot-program" | |
462 | #~(let ((system (cadr (command-line)))) | |
463 | (setenv "GUIX_NEW_SYSTEM" system) | |
464 | (execl #$(file-append guile-2.2 "/bin/guile") | |
465 | "guile" "--no-auto-compile" | |
466 | (string-append system "/boot"))))) | |
467 | ||
468 | ||
469 | (let ((os (operating-system-with-gc-roots | |
d6c43d7b LC |
470 | (containerized-operating-system os '() |
471 | #:shared-network? | |
472 | shared-network?) | |
247649d4 | 473 | (list boot-program))) |
69cae3d3 LC |
474 | (name (string-append name ".tar.gz")) |
475 | (graph "system-graph")) | |
a335f6fc | 476 | (define build |
81c3dc32 | 477 | (with-extensions (cons guile-json-3 ;for (guix docker) |
ca719424 | 478 | gcrypt-sqlite3&co) ;for (guix store database) |
9f160a0d LC |
479 | (with-imported-modules `(,@(source-module-closure |
480 | '((guix docker) | |
c45477d2 | 481 | (guix store database) |
9f160a0d | 482 | (guix build utils) |
c45477d2 | 483 | (guix build store-copy) |
9f160a0d LC |
484 | (gnu build vm)) |
485 | #:select? not-config?) | |
ca719424 | 486 | ((guix config) => ,(make-config.scm))) |
9f160a0d LC |
487 | #~(begin |
488 | (use-modules (guix docker) | |
489 | (guix build utils) | |
490 | (gnu build vm) | |
491 | (srfi srfi-19) | |
c45477d2 LC |
492 | (guix build store-copy) |
493 | (guix store database)) | |
494 | ||
495 | ;; Set the SQL schema location. | |
496 | (sql-schema #$schema) | |
9f160a0d | 497 | |
ed504caf LC |
498 | ;; Allow non-ASCII file names--e.g., 'nss-certs'--to be decoded. |
499 | (setenv "GUIX_LOCPATH" | |
500 | #+(file-append glibc-utf8-locales "/lib/locale")) | |
501 | (setlocale LC_ALL "en_US.utf8") | |
502 | ||
c45477d2 | 503 | (let* (;; This initializer requires elevated privileges that are |
9f160a0d LC |
504 | ;; not normally available in the build environment (e.g., |
505 | ;; it needs to create device nodes). In order to obtain | |
506 | ;; such privileges, we run it as root in a VM. | |
507 | (initialize (root-partition-initializer | |
508 | #:closures '(#$graph) | |
509 | #:register-closures? #$register-closures? | |
69cae3d3 | 510 | #:system-directory #$os |
9f160a0d LC |
511 | ;; De-duplication would fail due to |
512 | ;; cross-device link errors, so don't do it. | |
513 | #:deduplicate? #f)) | |
514 | ;; Even as root in a VM, the initializer would fail due to | |
515 | ;; lack of privileges if we use a root-directory that is on | |
516 | ;; a file system that is shared with the host (e.g., /tmp). | |
517 | (root-directory "/guixsd-system-root")) | |
c45477d2 | 518 | (set-path-environment-variable "PATH" '("bin" "sbin") '(#+tar)) |
9f160a0d LC |
519 | (mkdir root-directory) |
520 | (initialize root-directory) | |
521 | (build-docker-image | |
522 | (string-append "/xchg/" #$name) ;; The output file. | |
523 | (cons* root-directory | |
6892f0a2 LC |
524 | (map store-info-item |
525 | (call-with-input-file | |
526 | (string-append "/xchg/" #$graph) | |
527 | read-reference-graph))) | |
69cae3d3 | 528 | #$os |
247649d4 | 529 | #:entry-point '(#$boot-program #$os) |
9f160a0d LC |
530 | #:compressor '(#+(file-append gzip "/bin/gzip") "-9n") |
531 | #:creation-time (make-time time-utc 0 1) | |
66ec3895 | 532 | #:transformations `((,root-directory -> "")))))))) |
247649d4 | 533 | |
a335f6fc | 534 | (expression->derivation-in-linux-vm |
be43c08b | 535 | name build |
a335f6fc CM |
536 | #:make-disk-image? #f |
537 | #:single-file-output? #t | |
69cae3d3 | 538 | #:references-graphs `((,graph ,os))))) |
a335f6fc | 539 | |
04086015 LC |
540 | \f |
541 | ;;; | |
1e77fedb | 542 | ;;; VM and disk images. |
04086015 LC |
543 | ;;; |
544 | ||
f19cf27c MO |
545 | (define* (system-disk-image-in-vm os |
546 | #:key | |
547 | (name "disk-image") | |
548 | (file-system-type "ext4") | |
549 | (disk-image-size (* 900 (expt 2 20))) | |
550 | (volatile? #t) | |
551 | (substitutable? #t)) | |
1e77fedb LC |
552 | "Return the derivation of a disk image of DISK-IMAGE-SIZE bytes of the |
553 | system described by OS. Said image can be copied on a USB stick as is. When | |
554 | VOLATILE? is true, the root file system is made volatile; this is useful | |
a328f66a LC |
555 | to USB sticks meant to be read-only. |
556 | ||
557 | SUBSTITUTABLE? determines whether the returned derivation should be marked as | |
558 | substitutable." | |
10ace2c4 | 559 | (define root-label |
77f52962 | 560 | "Guix_image") |
10ace2c4 | 561 | |
5be7aecd | 562 | (define (root-uuid os) |
5f7fe1c5 LC |
563 | ;; UUID of the root file system, computed in a deterministic fashion. |
564 | ;; This is what we use to locate the root file system so it has to be | |
565 | ;; different from the user's own file system UUIDs. | |
77f52962 | 566 | (operating-system-uuid os 'dce)) |
5f7fe1c5 | 567 | |
1e77fedb LC |
568 | (define file-systems-to-keep |
569 | (remove (lambda (fs) | |
570 | (string=? (file-system-mount-point fs) "/")) | |
571 | (operating-system-file-systems os))) | |
572 | ||
8bff7dc2 LC |
573 | (let* ((os (operating-system (inherit os) |
574 | ;; Since this is meant to be used on real hardware, don't | |
575 | ;; install QEMU networking or anything like that. Assume USB | |
576 | ;; mass storage devices (usb-storage.ko) are available. | |
577 | (initrd (lambda (file-systems . rest) | |
578 | (apply (operating-system-initrd os) | |
579 | file-systems | |
67ed5442 | 580 | #:volatile-root? volatile? |
8bff7dc2 LC |
581 | rest))) |
582 | ||
77f52962 | 583 | (bootloader (operating-system-bootloader os)) |
8bff7dc2 | 584 | |
5be7aecd LC |
585 | ;; Force our own root file system. (We need a "/" file system |
586 | ;; to call 'root-uuid'.) | |
8bff7dc2 LC |
587 | (file-systems (cons (file-system |
588 | (mount-point "/") | |
5be7aecd LC |
589 | (device "/dev/placeholder") |
590 | (type file-system-type)) | |
591 | file-systems-to-keep)))) | |
592 | (uuid (root-uuid os)) | |
593 | (os (operating-system | |
594 | (inherit os) | |
595 | (file-systems (cons (file-system | |
596 | (mount-point "/") | |
597 | (device uuid) | |
8bff7dc2 LC |
598 | (type file-system-type)) |
599 | file-systems-to-keep)))) | |
600 | (bootcfg (operating-system-bootcfg os))) | |
77f52962 MO |
601 | (qemu-image #:name name |
602 | #:os os | |
603 | #:bootcfg-drv bootcfg | |
604 | #:bootloader (bootloader-configuration-bootloader | |
605 | (operating-system-bootloader os)) | |
606 | #:disk-image-size disk-image-size | |
607 | #:disk-image-format "raw" | |
608 | #:file-system-type file-system-type | |
609 | #:file-system-label root-label | |
610 | #:file-system-uuid uuid | |
611 | #:copy-inputs? #t | |
612 | #:inputs `(("system" ,os) | |
613 | ("bootcfg" ,bootcfg)) | |
614 | #:substitutable? substitutable?))) | |
1e77fedb | 615 | |
0b14d1d7 | 616 | (define* (system-qemu-image os |
66f23d66 LC |
617 | #:key |
618 | (file-system-type "ext4") | |
619 | (disk-image-size (* 900 (expt 2 20)))) | |
620 | "Return the derivation of a freestanding QEMU image of DISK-IMAGE-SIZE bytes | |
621 | of the GNU system as described by OS." | |
1eeccc2f LC |
622 | (define file-systems-to-keep |
623 | ;; Keep only file systems other than root and not normally bound to real | |
624 | ;; devices. | |
625 | (remove (lambda (fs) | |
626 | (let ((target (file-system-mount-point fs)) | |
627 | (source (file-system-device fs))) | |
628 | (or (string=? target "/") | |
629 | (string-prefix? "/dev/" source)))) | |
630 | (operating-system-file-systems os))) | |
631 | ||
61b94b8c LC |
632 | (define root-uuid |
633 | ;; UUID of the root file system. | |
634 | (operating-system-uuid os | |
635 | (if (string=? file-system-type "iso9660") | |
636 | 'iso9660 | |
637 | 'dce))) | |
638 | ||
639 | ||
8bff7dc2 LC |
640 | (let* ((os (operating-system (inherit os) |
641 | ;; Assume we have an initrd with the whole QEMU shebang. | |
642 | ||
643 | ;; Force our own root file system. Refer to it by UUID so that | |
644 | ;; it works regardless of how the image is used ("qemu -hda", | |
645 | ;; Xen, etc.). | |
646 | (file-systems (cons (file-system | |
647 | (mount-point "/") | |
648 | (device root-uuid) | |
649 | (type file-system-type)) | |
650 | file-systems-to-keep)))) | |
651 | (bootcfg (operating-system-bootcfg os))) | |
652 | (qemu-image #:os os | |
653 | #:bootcfg-drv bootcfg | |
654 | #:bootloader (bootloader-configuration-bootloader | |
655 | (operating-system-bootloader os)) | |
656 | #:disk-image-size disk-image-size | |
657 | #:file-system-type file-system-type | |
658 | #:file-system-uuid root-uuid | |
659 | #:inputs `(("system" ,os) | |
660 | ("bootcfg" ,bootcfg)) | |
661 | #:copy-inputs? #t))) | |
04086015 | 662 | |
fcf63cf8 LC |
663 | \f |
664 | ;;; | |
665 | ;;; VMs that share file systems with the host. | |
666 | ;;; | |
667 | ||
96ffa27b LC |
668 | (define (file-system->mount-tag fs) |
669 | "Return a 9p mount tag for host file system FS." | |
dffc5ab5 LC |
670 | ;; QEMU mount tags must be ASCII, at most 31-byte long, cannot contain |
671 | ;; slashes, and cannot start with '_'. Compute an identifier that | |
672 | ;; corresponds to the rules. | |
96ffa27b | 673 | (string-append "TAG" |
dffc5ab5 LC |
674 | (string-drop (bytevector->base32-string |
675 | (sha1 (string->utf8 fs))) | |
676 | 4))) | |
96ffa27b | 677 | |
fcf63cf8 LC |
678 | (define (mapping->file-system mapping) |
679 | "Return a 9p file system that realizes MAPPING." | |
680 | (match mapping | |
681 | (($ <file-system-mapping> source target writable?) | |
682 | (file-system | |
683 | (mount-point target) | |
684 | (device (file-system->mount-tag source)) | |
685 | (type "9p") | |
686 | (flags (if writable? '() '(read-only))) | |
e0d96774 | 687 | (options "trans=virtio,cache=loose") |
fcf63cf8 LC |
688 | (check? #f) |
689 | (create-mount-point? #t))))) | |
690 | ||
909de139 | 691 | (define* (virtualized-operating-system os mappings #:optional (full-boot? #f)) |
83bcd0b8 | 692 | "Return an operating system based on OS suitable for use in a virtualized |
fcf63cf8 LC |
693 | environment with the store shared with the host. MAPPINGS is a list of |
694 | <file-system-mapping> to realize in the virtualized OS." | |
695 | (define user-file-systems | |
696 | ;; Remove file systems that conflict with those added below, or that are | |
697 | ;; normally bound to real devices. | |
698 | (remove (lambda (fs) | |
699 | (let ((target (file-system-mount-point fs)) | |
700 | (source (file-system-device fs))) | |
701 | (or (string=? target (%store-prefix)) | |
702 | (string=? target "/") | |
a5acc17a | 703 | (and (string? source) |
f00515b4 LC |
704 | (string-prefix? "/dev/" source)) |
705 | ||
706 | ;; Labels and UUIDs are necessarily invalid in the VM. | |
707 | (and (file-system-mount? fs) | |
a5acc17a | 708 | (or (file-system-label? source) |
f00515b4 | 709 | (uuid? source)))))) |
fcf63cf8 LC |
710 | (operating-system-file-systems os))) |
711 | ||
909de139 DC |
712 | (define virtual-file-systems |
713 | (cons (file-system | |
714 | (mount-point "/") | |
715 | (device "/dev/vda1") | |
716 | (type "ext4")) | |
717 | ||
718 | (append (map mapping->file-system mappings) | |
719 | user-file-systems))) | |
720 | ||
83bcd0b8 | 721 | (operating-system (inherit os) |
9b396c0c LC |
722 | |
723 | ;; XXX: Until we run QEMU with UEFI support (with the OVMF firmware), | |
724 | ;; force the traditional i386/BIOS method. | |
725 | ;; See <https://bugs.gnu.org/28768>. | |
726 | (bootloader (bootloader-configuration | |
132823c2 | 727 | (inherit (operating-system-bootloader os)) |
9b396c0c LC |
728 | (bootloader grub-bootloader) |
729 | (target "/dev/vda"))) | |
730 | ||
52ac153e | 731 | (initrd (lambda (file-systems . rest) |
b8e77811 MO |
732 | (apply (operating-system-initrd os) |
733 | file-systems | |
52ac153e | 734 | #:volatile-root? #t |
52ac153e | 735 | rest))) |
65fb4515 LC |
736 | |
737 | ;; Disable swap. | |
738 | (swap-devices '()) | |
739 | ||
909de139 DC |
740 | ;; XXX: When FULL-BOOT? is true, do not add a 9p mount for /gnu/store |
741 | ;; since that would lead the bootloader config to look for the kernel and | |
742 | ;; initrd in it. | |
743 | (file-systems (if full-boot? | |
744 | virtual-file-systems | |
745 | (cons | |
746 | (file-system | |
747 | (inherit (mapping->file-system %store-mapping)) | |
748 | (needed-for-boot? #t)) | |
749 | virtual-file-systems))))) | |
83bcd0b8 | 750 | |
fd3bfc44 | 751 | (define* (system-qemu-image/shared-store |
0b14d1d7 | 752 | os |
6aa260af LC |
753 | #:key |
754 | full-boot? | |
4c0416ae | 755 | (disk-image-size (* (if full-boot? 500 30) (expt 2 20)))) |
fd3bfc44 | 756 | "Return a derivation that builds a QEMU image of OS that shares its store |
6aa260af LC |
757 | with the host. |
758 | ||
759 | When FULL-BOOT? is true, return an image that does a complete boot sequence, | |
760 | bootloaded included; thus, make a disk image that contains everything the | |
761 | bootloader refers to: OS kernel, initrd, bootloader data, etc." | |
56f9d442 LC |
762 | (define root-uuid |
763 | ;; Use a fixed UUID to improve determinism. | |
764 | (operating-system-uuid os 'dce)) | |
765 | ||
8bff7dc2 LC |
766 | (define bootcfg |
767 | (operating-system-bootcfg os)) | |
768 | ||
769 | ;; XXX: When FULL-BOOT? is true, we end up creating an image that contains | |
770 | ;; BOOTCFG and all its dependencies, including the output of OS. | |
771 | ;; This is more than needed (we only need the kernel, initrd, GRUB for its | |
772 | ;; font, and the background image), but it's hard to filter that. | |
773 | (qemu-image #:os os | |
774 | #:bootcfg-drv bootcfg | |
775 | #:bootloader (bootloader-configuration-bootloader | |
776 | (operating-system-bootloader os)) | |
777 | #:disk-image-size disk-image-size | |
778 | #:file-system-uuid root-uuid | |
779 | #:inputs (if full-boot? | |
780 | `(("bootcfg" ,bootcfg)) | |
781 | '()) | |
782 | ||
783 | ;; XXX: Passing #t here is too slow, so let it off by default. | |
784 | #:register-closures? #f | |
785 | #:copy-inputs? full-boot?)) | |
fd3bfc44 | 786 | |
96ffa27b LC |
787 | (define* (common-qemu-options image shared-fs) |
788 | "Return the a string-value gexp with the common QEMU options to boot IMAGE, | |
789 | with '-virtfs' options for the host file systems listed in SHARED-FS." | |
26a076ed | 790 | |
96ffa27b | 791 | (define (virtfs-option fs) |
26a076ed DC |
792 | #~(format #f "-virtfs local,path=~s,security_model=none,mount_tag=~s" |
793 | #$fs #$(file-system->mount-tag fs))) | |
96ffa27b | 794 | |
26a076ed | 795 | #~(;; Only enable kvm if we see /dev/kvm exists. |
944d2b17 CAW |
796 | ;; This allows users without hardware virtualization to still use these |
797 | ;; commands. | |
26a076ed DC |
798 | #$@(if (file-exists? "/dev/kvm") |
799 | '("-enable-kvm") | |
800 | '()) | |
801 | ||
802 | "-no-reboot" | |
53793927 | 803 | "-nic" "user,model=virtio-net-pci" |
2ca712bd LF |
804 | "-object" "rng-random,filename=/dev/urandom,id=guixsd-vm-rng" |
805 | "-device" "virtio-rng-pci,rng=guixsd-vm-rng" | |
26a076ed DC |
806 | |
807 | #$@(map virtfs-option shared-fs) | |
808 | "-vga std" | |
809 | (format #f "-drive file=~a,if=virtio,cache=writeback,werror=report,readonly" | |
ebfb71d4 | 810 | #$image))) |
3c1f0e3b | 811 | |
ab11f0be LC |
812 | (define* (system-qemu-image/shared-store-script os |
813 | #:key | |
814 | (qemu qemu) | |
815 | (graphic? #t) | |
ebfb71d4 | 816 | (memory-size 256) |
fcf63cf8 | 817 | (mappings '()) |
6aa260af LC |
818 | full-boot? |
819 | (disk-image-size | |
9a1bfe76 | 820 | (* (if full-boot? 500 70) |
ed419fa0 LC |
821 | (expt 2 20))) |
822 | (options '())) | |
fd3bfc44 | 823 | "Return a derivation that builds a script to run a virtual machine image of |
ebfb71d4 JN |
824 | OS that shares its store with the host. The virtual machine runs with |
825 | MEMORY-SIZE MiB of memory. | |
6aa260af | 826 | |
fcf63cf8 LC |
827 | MAPPINGS is a list of <file-system-mapping> specifying mapping of host file |
828 | systems into the guest. | |
829 | ||
6aa260af LC |
830 | When FULL-BOOT? is true, the returned script runs everything starting from the |
831 | bootloader; otherwise it directly starts the operating system kernel. The | |
832 | DISK-IMAGE-SIZE parameter specifies the size in bytes of the root disk image; | |
833 | it is mostly useful when FULL-BOOT? is true." | |
909de139 | 834 | (mlet* %store-monad ((os -> (virtualized-operating-system os mappings full-boot?)) |
6aa260af LC |
835 | (image (system-qemu-image/shared-store |
836 | os | |
837 | #:full-boot? full-boot? | |
838 | #:disk-image-size disk-image-size))) | |
26a076ed | 839 | (define kernel-arguments |
83071b05 | 840 | #~(list #$@(if graphic? #~() #~("console=ttyS0")) |
a7ef45d9 | 841 | #+@(operating-system-kernel-arguments os "/dev/vda1"))) |
26a076ed DC |
842 | |
843 | (define qemu-exec | |
844 | #~(list (string-append #$qemu "/bin/" #$(qemu-command (%current-system))) | |
845 | #$@(if full-boot? | |
846 | #~() | |
847 | #~("-kernel" #$(operating-system-kernel-file os) | |
a7ef45d9 | 848 | "-initrd" #$(file-append os "/initrd") |
26a076ed DC |
849 | (format #f "-append ~s" |
850 | (string-join #$kernel-arguments " ")))) | |
851 | #$@(common-qemu-options image | |
852 | (map file-system-mapping-source | |
ebfb71d4 | 853 | (cons %store-mapping mappings))) |
ed419fa0 LC |
854 | "-m " (number->string #$memory-size) |
855 | #$@options)) | |
26a076ed | 856 | |
fd3bfc44 | 857 | (define builder |
02100028 LC |
858 | #~(call-with-output-file #$output |
859 | (lambda (port) | |
26a076ed DC |
860 | (format port "#!~a~% exec ~a \"$@\"~%" |
861 | #$(file-append bash "/bin/sh") | |
862 | (string-join #$qemu-exec " ")) | |
02100028 LC |
863 | (chmod port #o555)))) |
864 | ||
865 | (gexp->derivation "run-vm.sh" builder))) | |
fd3bfc44 | 866 | |
ed419fa0 LC |
867 | \f |
868 | ;;; | |
869 | ;;; High-level abstraction. | |
870 | ;;; | |
871 | ||
872 | (define-record-type* <virtual-machine> %virtual-machine | |
873 | make-virtual-machine | |
874 | virtual-machine? | |
875 | (operating-system virtual-machine-operating-system) ;<operating-system> | |
876 | (qemu virtual-machine-qemu ;<package> | |
877 | (default qemu)) | |
878 | (graphic? virtual-machine-graphic? ;Boolean | |
879 | (default #f)) | |
880 | (memory-size virtual-machine-memory-size ;integer (MiB) | |
881 | (default 256)) | |
eb152070 CB |
882 | (disk-image-size virtual-machine-disk-image-size ;integer (bytes) |
883 | (default 'guess)) | |
ed419fa0 LC |
884 | (port-forwardings virtual-machine-port-forwardings ;list of integer pairs |
885 | (default '()))) | |
886 | ||
887 | (define-syntax virtual-machine | |
888 | (syntax-rules () | |
889 | "Declare a virtual machine running the specified OS, with the given | |
890 | options." | |
891 | ((_ os) ;shortcut | |
892 | (%virtual-machine (operating-system os))) | |
893 | ((_ fields ...) | |
894 | (%virtual-machine fields ...)))) | |
895 | ||
896 | (define (port-forwardings->qemu-options forwardings) | |
897 | "Return the QEMU option for the given port FORWARDINGS as a string, where | |
898 | FORWARDINGS is a list of host-port/guest-port pairs." | |
899 | (string-join | |
900 | (map (match-lambda | |
901 | ((host-port . guest-port) | |
902 | (string-append "hostfwd=tcp::" | |
903 | (number->string host-port) | |
904 | "-:" (number->string guest-port)))) | |
905 | forwardings) | |
906 | ",")) | |
907 | ||
908 | (define-gexp-compiler (virtual-machine-compiler (vm <virtual-machine>) | |
909 | system target) | |
910 | ;; XXX: SYSTEM and TARGET are ignored. | |
911 | (match vm | |
eb152070 | 912 | (($ <virtual-machine> os qemu graphic? memory-size disk-image-size ()) |
ed419fa0 LC |
913 | (system-qemu-image/shared-store-script os |
914 | #:qemu qemu | |
915 | #:graphic? graphic? | |
eb152070 CB |
916 | #:memory-size memory-size |
917 | #:disk-image-size | |
918 | disk-image-size)) | |
919 | (($ <virtual-machine> os qemu graphic? memory-size disk-image-size | |
920 | forwardings) | |
ed419fa0 | 921 | (let ((options |
8e53fe2b MB |
922 | `("-nic" ,(string-append |
923 | "user,model=virtio-net-pci," | |
ed419fa0 LC |
924 | (port-forwardings->qemu-options forwardings))))) |
925 | (system-qemu-image/shared-store-script os | |
926 | #:qemu qemu | |
927 | #:graphic? graphic? | |
928 | #:memory-size memory-size | |
eb152070 CB |
929 | #:disk-image-size |
930 | disk-image-size | |
ed419fa0 LC |
931 | #:options options))))) |
932 | ||
04086015 | 933 | ;;; vm.scm ends here |