Commit | Line | Data |
---|---|---|
e6051057 RM |
1 | ;;; GNU Guix --- Functional package management for GNU |
2 | ;;; Copyright © 2017 Ryan Moe <ryan.moe@gmail.com> | |
6738c29f | 3 | ;;; Copyright © 2018 Ludovic Courtès <ludo@gnu.org> |
e6051057 RM |
4 | ;;; |
5 | ;;; This file is part of GNU Guix. | |
6 | ;;; | |
7 | ;;; GNU Guix is free software; you can redistribute it and/or modify it | |
8 | ;;; under the terms of the GNU General Public License as published by | |
9 | ;;; the Free Software Foundation; either version 3 of the License, or (at | |
10 | ;;; your option) any later version. | |
11 | ;;; | |
12 | ;;; GNU Guix is distributed in the hope that it will be useful, but | |
13 | ;;; WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | ;;; GNU General Public License for more details. | |
16 | ;;; | |
17 | ;;; You should have received a copy of the GNU General Public License | |
18 | ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>. | |
19 | ||
20 | (define-module (gnu services virtualization) | |
21 | #:use-module (gnu services) | |
22 | #:use-module (gnu services configuration) | |
23 | #:use-module (gnu services base) | |
24 | #:use-module (gnu services dbus) | |
25 | #:use-module (gnu services shepherd) | |
26 | #:use-module (gnu system shadow) | |
6738c29f | 27 | #:use-module (gnu system file-systems) |
e6051057 RM |
28 | #:use-module (gnu packages admin) |
29 | #:use-module (gnu packages virtualization) | |
30 | #:use-module (guix records) | |
31 | #:use-module (guix gexp) | |
32 | #:use-module (guix packages) | |
6738c29f LC |
33 | #:use-module (srfi srfi-9) |
34 | #:use-module (srfi srfi-26) | |
35 | #:use-module (rnrs bytevectors) | |
e6051057 RM |
36 | #:use-module (ice-9 match) |
37 | ||
38 | #:export (libvirt-configuration | |
39 | libvirt-service-type | |
daf823ad | 40 | virtlog-configuration |
6738c29f LC |
41 | virtlog-service-type |
42 | ||
43 | %qemu-platforms | |
44 | lookup-qemu-platforms | |
45 | qemu-platform? | |
46 | qemu-platform-name | |
47 | ||
48 | qemu-binfmt-configuration | |
49 | qemu-binfmt-configuration? | |
50 | qemu-binfmt-service-type)) | |
e6051057 RM |
51 | |
52 | (define (uglify-field-name field-name) | |
53 | (let ((str (symbol->string field-name))) | |
54 | (string-join | |
55 | (string-split (string-delete #\? str) #\-) | |
56 | "_"))) | |
57 | ||
58 | (define (quote-val val) | |
59 | (string-append "\"" val "\"")) | |
60 | ||
61 | (define (serialize-field field-name val) | |
62 | (format #t "~a = ~a\n" (uglify-field-name field-name) val)) | |
63 | ||
64 | (define (serialize-string field-name val) | |
65 | (serialize-field field-name (quote-val val))) | |
66 | ||
67 | (define (serialize-boolean field-name val) | |
68 | (serialize-field field-name (if val 1 0))) | |
69 | ||
70 | (define (serialize-integer field-name val) | |
71 | (serialize-field field-name val)) | |
72 | ||
73 | (define (build-opt-list val) | |
74 | (string-append | |
75 | "[" | |
76 | (string-join (map quote-val val) ",") | |
77 | "]")) | |
78 | ||
79 | (define optional-list? list?) | |
80 | (define optional-string? string?) | |
81 | ||
82 | (define (serialize-list field-name val) | |
83 | (serialize-field field-name (build-opt-list val))) | |
84 | ||
85 | (define (serialize-optional-list field-name val) | |
86 | (if (null? val) | |
87 | (format #t "# ~a = []\n" (uglify-field-name field-name)) | |
88 | (serialize-list field-name val))) | |
89 | ||
90 | (define (serialize-optional-string field-name val) | |
91 | (if (string-null? val) | |
92 | (format #t "# ~a = \"\"\n" (uglify-field-name field-name)) | |
93 | (serialize-string field-name val))) | |
94 | ||
95 | (define-configuration libvirt-configuration | |
96 | (libvirt | |
97 | (package libvirt) | |
98 | "Libvirt package.") | |
99 | (listen-tls? | |
100 | (boolean #t) | |
101 | "Flag listening for secure TLS connections on the public TCP/IP port. | |
102 | must set @code{listen} for this to have any effect. | |
103 | ||
104 | It is necessary to setup a CA and issue server certificates before | |
105 | using this capability.") | |
106 | (listen-tcp? | |
107 | (boolean #f) | |
108 | "Listen for unencrypted TCP connections on the public TCP/IP port. | |
109 | must set @code{listen} for this to have any effect. | |
110 | ||
111 | Using the TCP socket requires SASL authentication by default. Only | |
112 | SASL mechanisms which support data encryption are allowed. This is | |
113 | DIGEST_MD5 and GSSAPI (Kerberos5)") | |
114 | (tls-port | |
115 | (string "16514") | |
116 | "Port for accepting secure TLS connections This can be a port number, | |
117 | or service name") | |
118 | (tcp-port | |
119 | (string "16509") | |
120 | "Port for accepting insecure TCP connections This can be a port number, | |
121 | or service name") | |
122 | (listen-addr | |
123 | (string "0.0.0.0") | |
124 | "IP address or hostname used for client connections.") | |
125 | (mdns-adv? | |
126 | (boolean #f) | |
127 | "Flag toggling mDNS advertisement of the libvirt service. | |
128 | ||
129 | Alternatively can disable for all services on a host by | |
130 | stopping the Avahi daemon.") | |
131 | (mdns-name | |
132 | (string (string-append "Virtualization Host " (gethostname))) | |
133 | "Default mDNS advertisement name. This must be unique on the | |
134 | immediate broadcast network.") | |
135 | (unix-sock-group | |
136 | (string "root") | |
137 | "UNIX domain socket group ownership. This can be used to | |
138 | allow a 'trusted' set of users access to management capabilities | |
139 | without becoming root.") | |
140 | (unix-sock-ro-perms | |
141 | (string "0777") | |
142 | "UNIX socket permissions for the R/O socket. This is used | |
143 | for monitoring VM status only.") | |
144 | (unix-sock-rw-perms | |
145 | (string "0770") | |
146 | "UNIX socket permissions for the R/W socket. Default allows | |
147 | only root. If PolicyKit is enabled on the socket, the default | |
148 | will change to allow everyone (eg, 0777)") | |
149 | (unix-sock-admin-perms | |
150 | (string "0777") | |
151 | "UNIX socket permissions for the admin socket. Default allows | |
152 | only owner (root), do not change it unless you are sure to whom | |
153 | you are exposing the access to.") | |
154 | (unix-sock-dir | |
155 | (string "/var/run/libvirt") | |
156 | "The directory in which sockets will be found/created.") | |
157 | (auth-unix-ro | |
158 | (string "polkit") | |
159 | "Authentication scheme for UNIX read-only sockets. By default | |
160 | socket permissions allow anyone to connect") | |
161 | (auth-unix-rw | |
162 | (string "polkit") | |
163 | "Authentication scheme for UNIX read-write sockets. By default | |
164 | socket permissions only allow root. If PolicyKit support was compiled | |
165 | into libvirt, the default will be to use 'polkit' auth.") | |
166 | (auth-tcp | |
167 | (string "sasl") | |
168 | "Authentication scheme for TCP sockets. If you don't enable SASL, | |
169 | then all TCP traffic is cleartext. Don't do this outside of a dev/test | |
170 | scenario.") | |
171 | (auth-tls | |
172 | (string "none") | |
173 | "Authentication scheme for TLS sockets. TLS sockets already have | |
174 | encryption provided by the TLS layer, and limited authentication is | |
175 | done by certificates. | |
176 | ||
177 | It is possible to make use of any SASL authentication mechanism as | |
178 | well, by using 'sasl' for this option") | |
179 | (access-drivers | |
180 | (optional-list '()) | |
181 | "API access control scheme. | |
182 | ||
183 | By default an authenticated user is allowed access to all APIs. Access | |
184 | drivers can place restrictions on this.") | |
185 | (key-file | |
186 | (string "") | |
187 | "Server key file path. If set to an empty string, then no private key | |
188 | is loaded.") | |
189 | (cert-file | |
190 | (string "") | |
191 | "Server key file path. If set to an empty string, then no certificate | |
192 | is loaded.") | |
193 | (ca-file | |
194 | (string "") | |
195 | "Server key file path. If set to an empty string, then no CA certificate | |
196 | is loaded.") | |
197 | (crl-file | |
198 | (string "") | |
199 | "Certificate revocation list path. If set to an empty string, then no | |
200 | CRL is loaded.") | |
201 | (tls-no-sanity-cert | |
202 | (boolean #f) | |
203 | "Disable verification of our own server certificates. | |
204 | ||
205 | When libvirtd starts it performs some sanity checks against its own | |
206 | certificates.") | |
207 | (tls-no-verify-cert | |
208 | (boolean #f) | |
209 | "Disable verification of client certificates. | |
210 | ||
211 | Client certificate verification is the primary authentication mechanism. | |
212 | Any client which does not present a certificate signed by the CA | |
213 | will be rejected.") | |
214 | (tls-allowed-dn-list | |
215 | (optional-list '()) | |
216 | "Whitelist of allowed x509 Distinguished Name.") | |
217 | (sasl-allowed-usernames | |
218 | (optional-list '()) | |
219 | "Whitelist of allowed SASL usernames. The format for username | |
220 | depends on the SASL authentication mechanism.") | |
221 | (tls-priority | |
222 | (string "NORMAL") | |
223 | "Override the compile time default TLS priority string. The | |
224 | default is usually \"NORMAL\" unless overridden at build time. | |
225 | Only set this is it is desired for libvirt to deviate from | |
226 | the global default settings.") | |
227 | (max-clients | |
228 | (integer 5000) | |
229 | "Maximum number of concurrent client connections to allow | |
230 | over all sockets combined.") | |
231 | (max-queued-clients | |
232 | (integer 1000) | |
233 | "Maximum length of queue of connections waiting to be | |
234 | accepted by the daemon. Note, that some protocols supporting | |
235 | retransmission may obey this so that a later reattempt at | |
236 | connection succeeds.") | |
237 | (max-anonymous-clients | |
238 | (integer 20) | |
239 | "Maximum length of queue of accepted but not yet authenticated | |
240 | clients. Set this to zero to turn this feature off") | |
241 | (min-workers | |
242 | (integer 5) | |
243 | "Number of workers to start up initially.") | |
244 | (max-workers | |
245 | (integer 20) | |
246 | "Maximum number of worker threads. | |
247 | ||
248 | If the number of active clients exceeds @code{min-workers}, | |
249 | then more threads are spawned, up to max_workers limit. | |
250 | Typically you'd want max_workers to equal maximum number | |
251 | of clients allowed.") | |
252 | (prio-workers | |
253 | (integer 5) | |
254 | "Number of priority workers. If all workers from above | |
255 | pool are stuck, some calls marked as high priority | |
256 | (notably domainDestroy) can be executed in this pool.") | |
257 | (max-requests | |
258 | (integer 20) | |
259 | "Total global limit on concurrent RPC calls.") | |
260 | (max-client-requests | |
261 | (integer 5) | |
262 | "Limit on concurrent requests from a single client | |
263 | connection. To avoid one client monopolizing the server | |
264 | this should be a small fraction of the global max_requests | |
265 | and max_workers parameter.") | |
266 | (admin-min-workers | |
267 | (integer 1) | |
268 | "Same as @code{min-workers} but for the admin interface.") | |
269 | (admin-max-workers | |
270 | (integer 5) | |
271 | "Same as @code{max-workers} but for the admin interface.") | |
272 | (admin-max-clients | |
273 | (integer 5) | |
274 | "Same as @code{max-clients} but for the admin interface.") | |
275 | (admin-max-queued-clients | |
276 | (integer 5) | |
277 | "Same as @code{max-queued-clients} but for the admin interface.") | |
278 | (admin-max-client-requests | |
279 | (integer 5) | |
280 | "Same as @code{max-client-requests} but for the admin interface.") | |
281 | (log-level | |
282 | (integer 3) | |
283 | "Logging level. 4 errors, 3 warnings, 2 information, 1 debug.") | |
284 | (log-filters | |
285 | (string "3:remote 4:event") | |
286 | "Logging filters. | |
287 | ||
288 | A filter allows to select a different logging level for a given category | |
289 | of logs | |
290 | The format for a filter is one of: | |
291 | @itemize | |
292 | @item x:name | |
293 | ||
294 | @item x:+name | |
295 | @end itemize | |
296 | ||
297 | where @code{name} is a string which is matched against the category | |
298 | given in the @code{VIR_LOG_INIT()} at the top of each libvirt source | |
299 | file, e.g., \"remote\", \"qemu\", or \"util.json\" (the name in the | |
300 | filter can be a substring of the full category name, in order | |
301 | to match multiple similar categories), the optional \"+\" prefix | |
302 | tells libvirt to log stack trace for each message matching | |
303 | name, and @code{x} is the minimal level where matching messages should | |
304 | be logged: | |
305 | ||
306 | @itemize | |
307 | @item 1: DEBUG | |
308 | @item 2: INFO | |
309 | @item 3: WARNING | |
310 | @item 4: ERROR | |
311 | @end itemize | |
312 | ||
313 | Multiple filters can be defined in a single filters statement, they just | |
314 | need to be separated by spaces.") | |
315 | (log-outputs | |
316 | (string "3:stderr") | |
317 | "Logging outputs. | |
318 | ||
319 | An output is one of the places to save logging information | |
320 | The format for an output can be: | |
321 | ||
322 | @table @code | |
323 | @item x:stderr | |
324 | output goes to stderr | |
325 | ||
326 | @item x:syslog:name | |
327 | use syslog for the output and use the given name as the ident | |
328 | ||
329 | @item x:file:file_path | |
330 | output to a file, with the given filepath | |
331 | ||
332 | @item x:journald | |
333 | output to journald logging system | |
334 | @end table | |
335 | ||
336 | In all case the x prefix is the minimal level, acting as a filter | |
337 | ||
338 | @itemize | |
339 | @item 1: DEBUG | |
340 | @item 2: INFO | |
341 | @item 3: WARNING | |
342 | @item 4: ERROR | |
343 | @end itemize | |
344 | ||
345 | Multiple outputs can be defined, they just need to be separated by spaces.") | |
346 | (audit-level | |
347 | (integer 1) | |
348 | "Allows usage of the auditing subsystem to be altered | |
349 | ||
350 | @itemize | |
351 | @item 0: disable all auditing | |
352 | @item 1: enable auditing, only if enabled on host | |
353 | @item 2: enable auditing, and exit if disabled on host. | |
354 | @end itemize | |
355 | ") | |
356 | (audit-logging | |
357 | (boolean #f) | |
358 | "Send audit messages via libvirt logging infrastructure.") | |
359 | (host-uuid | |
360 | (optional-string "") | |
361 | "Host UUID. UUID must not have all digits be the same.") | |
362 | (host-uuid-source | |
363 | (string "smbios") | |
364 | "Source to read host UUID. | |
365 | ||
366 | @itemize | |
367 | ||
368 | @item @code{smbios}: fetch the UUID from @code{dmidecode -s system-uuid} | |
369 | ||
370 | @item @code{machine-id}: fetch the UUID from @code{/etc/machine-id} | |
371 | ||
372 | @end itemize | |
373 | ||
374 | If @code{dmidecode} does not provide a valid UUID a temporary UUID | |
375 | will be generated.") | |
376 | (keepalive-interval | |
377 | (integer 5) | |
378 | "A keepalive message is sent to a client after | |
379 | @code{keepalive_interval} seconds of inactivity to check if | |
380 | the client is still responding. If set to -1, libvirtd will | |
381 | never send keepalive requests; however clients can still send | |
382 | them and the daemon will send responses.") | |
383 | (keepalive-count | |
384 | (integer 5) | |
385 | "Maximum number of keepalive messages that are allowed to be sent | |
386 | to the client without getting any response before the connection is | |
387 | considered broken. | |
388 | ||
389 | In other words, the connection is automatically | |
390 | closed approximately after | |
391 | @code{keepalive_interval * (keepalive_count + 1)} seconds since the last | |
392 | message received from the client. When @code{keepalive-count} is | |
393 | set to 0, connections will be automatically closed after | |
394 | @code{keepalive-interval} seconds of inactivity without sending any | |
395 | keepalive messages.") | |
396 | (admin-keepalive-interval | |
397 | (integer 5) | |
398 | "Same as above but for admin interface.") | |
399 | (admin-keepalive-count | |
400 | (integer 5) | |
401 | "Same as above but for admin interface.") | |
402 | (ovs-timeout | |
403 | (integer 5) | |
404 | "Timeout for Open vSwitch calls. | |
405 | ||
406 | The @code{ovs-vsctl} utility is used for the configuration and | |
407 | its timeout option is set by default to 5 seconds to avoid | |
408 | potential infinite waits blocking libvirt.")) | |
409 | ||
410 | (define* (libvirt-conf-file config) | |
411 | "Return a libvirtd config file." | |
412 | (plain-file "libvirtd.conf" | |
413 | (with-output-to-string | |
414 | (lambda () | |
415 | (serialize-configuration config libvirt-configuration-fields))))) | |
416 | ||
417 | (define %libvirt-accounts | |
418 | (list (user-group (name "libvirt") (system? #t)))) | |
419 | ||
420 | (define (%libvirt-activation config) | |
421 | (let ((sock-dir (libvirt-configuration-unix-sock-dir config))) | |
422 | #~(begin | |
423 | (use-modules (guix build utils)) | |
424 | (mkdir-p #$sock-dir)))) | |
425 | ||
426 | ||
427 | (define (libvirt-shepherd-service config) | |
428 | (let* ((config-file (libvirt-conf-file config)) | |
429 | (libvirt (libvirt-configuration-libvirt config))) | |
430 | (list (shepherd-service | |
431 | (documentation "Run the libvirt daemon.") | |
432 | (provision '(libvirtd)) | |
433 | (start #~(make-forkexec-constructor | |
434 | (list (string-append #$libvirt "/sbin/libvirtd") | |
435 | "-f" #$config-file))) | |
436 | (stop #~(make-kill-destructor)))))) | |
437 | ||
438 | (define libvirt-service-type | |
439 | (service-type (name 'libvirt) | |
440 | (extensions | |
441 | (list | |
442 | (service-extension polkit-service-type | |
443 | (compose list libvirt-configuration-libvirt)) | |
444 | (service-extension profile-service-type | |
445 | (compose list | |
446 | libvirt-configuration-libvirt)) | |
447 | (service-extension activation-service-type | |
448 | %libvirt-activation) | |
449 | (service-extension shepherd-root-service-type | |
450 | libvirt-shepherd-service) | |
451 | (service-extension account-service-type | |
452 | (const %libvirt-accounts)))) | |
453 | (default-value (libvirt-configuration)))) | |
454 | ||
455 | ||
456 | (define-record-type* <virtlog-configuration> | |
457 | virtlog-configuration make-virtlog-configuration | |
458 | virtlog-configuration? | |
459 | (libvirt virtlog-configuration-libvirt | |
460 | (default libvirt)) | |
461 | (log-level virtlog-configuration-log-level | |
462 | (default 3)) | |
463 | (log-filters virtlog-configuration-log-filters | |
464 | (default "3:remote 4:event")) | |
465 | (log-outputs virtlog-configuration-log-outputs | |
466 | (default "3:syslog:virtlogd")) | |
467 | (max-clients virtlog-configuration-max-clients | |
468 | (default 1024)) | |
469 | (max-size virtlog-configuration-max-size | |
470 | (default 2097152)) ;; 2MB | |
471 | (max-backups virtlog-configuration-max-backups | |
472 | (default 3))) | |
473 | ||
474 | (define* (virtlogd-conf-file config) | |
475 | "Return a virtlogd config file." | |
476 | (plain-file "virtlogd.conf" | |
477 | (string-append | |
478 | "log_level = " (number->string (virtlog-configuration-log-level config)) "\n" | |
479 | "log_filters = \"" (virtlog-configuration-log-filters config) "\"\n" | |
480 | "log_outputs = \"" (virtlog-configuration-log-outputs config) "\"\n" | |
481 | "max_clients = " (number->string (virtlog-configuration-max-clients config)) "\n" | |
482 | "max_size = " (number->string (virtlog-configuration-max-size config)) "\n" | |
483 | "max_backups = " (number->string (virtlog-configuration-max-backups config)) "\n"))) | |
484 | ||
485 | (define (virtlogd-shepherd-service config) | |
486 | (let* ((config-file (virtlogd-conf-file config)) | |
487 | (libvirt (virtlog-configuration-libvirt config))) | |
488 | (list (shepherd-service | |
489 | (documentation "Run the virtlog daemon.") | |
490 | (provision '(virtlogd)) | |
491 | (start #~(make-forkexec-constructor | |
492 | (list (string-append #$libvirt "/sbin/virtlogd") | |
493 | "-f" #$config-file))) | |
494 | (stop #~(make-kill-destructor)))))) | |
495 | ||
496 | (define virtlog-service-type | |
497 | (service-type (name 'virtlogd) | |
498 | (extensions | |
499 | (list | |
500 | (service-extension shepherd-root-service-type | |
501 | virtlogd-shepherd-service))) | |
502 | (default-value (virtlog-configuration)))) | |
503 | ||
504 | (define (generate-libvirt-documentation) | |
505 | (generate-documentation | |
506 | `((libvirt-configuration ,libvirt-configuration-fields)) | |
507 | 'libvirt-configuration)) | |
6738c29f LC |
508 | |
509 | \f | |
510 | ;;; | |
511 | ;;; Transparent QEMU emulation via binfmt_misc. | |
512 | ;;; | |
513 | ||
514 | ;; Platforms that QEMU can emulate. | |
515 | (define-record-type <qemu-platform> | |
516 | (qemu-platform name family magic mask) | |
517 | qemu-platform? | |
518 | (name qemu-platform-name) ;string | |
519 | (family qemu-platform-family) ;string | |
520 | (magic qemu-platform-magic) ;bytevector | |
521 | (mask qemu-platform-mask)) ;bytevector | |
522 | ||
523 | (define-syntax bv | |
524 | (lambda (s) | |
525 | "Expand the given string into a bytevector." | |
526 | (syntax-case s () | |
527 | ((_ str) | |
528 | (string? (syntax->datum #'str)) | |
529 | (let ((bv (u8-list->bytevector | |
530 | (map char->integer | |
531 | (string->list (syntax->datum #'str)))))) | |
532 | bv))))) | |
533 | ||
534 | ;;; The platform descriptions below are taken from | |
535 | ;;; 'scripts/qemu-binfmt-conf.sh' in QEMU. | |
536 | ||
537 | (define %i386 | |
538 | (qemu-platform "i386" "i386" | |
539 | (bv "\x7fELF\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x03\x00") | |
540 | (bv "\xff\xff\xff\xff\xff\xfe\xfe\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff"))) | |
541 | ||
542 | (define %i486 | |
543 | (qemu-platform "i486" "i386" | |
544 | (bv "\x7fELF\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x06\x00") | |
545 | (bv "\xff\xff\xff\xff\xff\xfe\xfe\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff"))) | |
546 | ||
547 | (define %alpha | |
548 | (qemu-platform "alpha" "alpha" | |
549 | (bv "\x7fELF\x02\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x26\x90") | |
550 | (bv "\xff\xff\xff\xff\xff\xfe\xfe\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff"))) | |
551 | ||
552 | (define %arm | |
553 | (qemu-platform "arm" "arm" | |
554 | (bv "\x7fELF\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x28\x00") | |
555 | (bv "\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff"))) | |
556 | ||
557 | (define %armeb | |
558 | (qemu-platform "armeb" "arm" | |
559 | (bv "\x7fELF\x01\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x28") | |
560 | (bv "\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff"))) | |
561 | ||
562 | (define %sparc | |
563 | (qemu-platform "sparc" "sparc" | |
564 | (bv "\x7fELF\x01\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x02") | |
565 | (bv "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff"))) | |
566 | ||
567 | (define %sparc32plus | |
568 | (qemu-platform "sparc32plus" "sparc" | |
569 | (bv "\x7fELF\x01\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x12") | |
570 | (bv "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff"))) | |
571 | ||
572 | (define %ppc | |
573 | (qemu-platform "ppc" "ppc" | |
574 | (bv "\x7fELF\x01\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x14") | |
575 | (bv "\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff"))) | |
576 | ||
577 | (define %ppc64 | |
578 | (qemu-platform "ppc64" "ppc" | |
579 | (bv "\x7fELF\x02\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x15") | |
580 | (bv "\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff"))) | |
581 | ||
582 | (define %ppc64le | |
583 | (qemu-platform "ppc64le" "ppcle" | |
584 | (bv "\x7fELF\x02\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x15\x00") | |
585 | (bv "\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\x00"))) | |
586 | ||
587 | (define %m68k | |
588 | (qemu-platform "m68k" "m68k" | |
589 | (bv "\x7fELF\x01\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x04") | |
590 | (bv "\xff\xff\xff\xff\xff\xff\xfe\xfe\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff"))) | |
591 | ||
592 | ;; XXX: We could use the other endianness on a MIPS host. | |
593 | (define %mips | |
594 | (qemu-platform "mips" "mips" | |
595 | (bv "\x7fELF\x01\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x08") | |
596 | (bv "\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff"))) | |
597 | ||
598 | (define %mipsel | |
599 | (qemu-platform "mipsel" "mips" | |
600 | (bv "\x7fELF\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x08\x00") | |
601 | (bv "\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff"))) | |
602 | ||
603 | (define %mipsn32 | |
604 | (qemu-platform "mipsn32" "mips" | |
605 | (bv "\x7fELF\x01\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x08") | |
606 | (bv "\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff"))) | |
607 | ||
608 | (define %mipsn32el | |
609 | (qemu-platform "mipsn32el" "mips" | |
610 | (bv "\x7fELF\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x08\x00") | |
611 | (bv "\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff"))) | |
612 | ||
613 | (define %mips64 | |
614 | (qemu-platform "mips64" "mips" | |
615 | (bv "\x7fELF\x02\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x08") | |
616 | (bv "\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff"))) | |
617 | ||
618 | (define %mips64el | |
619 | (qemu-platform "mips64el" "mips" | |
620 | (bv "\x7fELF\x02\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x08\x00") | |
621 | (bv "\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff"))) | |
622 | ||
623 | (define %sh4 | |
624 | (qemu-platform "sh4" "sh4" | |
625 | (bv "\x7fELF\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x2a\x00") | |
626 | (bv "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff"))) | |
627 | ||
628 | (define %sh4eb | |
629 | (qemu-platform "sh4eb" "sh4" | |
630 | (bv "\x7fELF\x01\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x2a") | |
631 | (bv "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff"))) | |
632 | ||
633 | (define %s390x | |
634 | (qemu-platform "s390x" "s390x" | |
635 | (bv "\x7fELF\x02\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x16") | |
636 | (bv "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff"))) | |
637 | ||
638 | (define %aarch64 | |
639 | (qemu-platform "aarch64" "arm" | |
640 | (bv "\x7fELF\x02\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\xb7\x00") | |
641 | (bv "\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff"))) | |
642 | ||
643 | (define %hppa | |
644 | (qemu-platform "hppa" "hppa" | |
645 | (bv "\x7f\x45\x4c\x46\x01\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x0f") | |
646 | (bv "\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff"))) | |
647 | ||
648 | (define %qemu-platforms | |
649 | (list %i386 %i486 %alpha %arm %sparc32plus %ppc %ppc64 %ppc64le %m68k | |
650 | %mips %mipsel %mipsn32 %mipsn32el %mips64 %mips64el | |
651 | %sh4 %sh4eb %s390x %aarch64 %hppa)) | |
652 | ||
653 | (define (lookup-qemu-platforms . names) | |
654 | "Return the list of QEMU platforms that match NAMES--a list of names such as | |
655 | \"arm\", \"hppa\", etc." | |
656 | (filter (lambda (platform) | |
657 | (member (qemu-platform-name platform) names)) | |
658 | %qemu-platforms)) | |
659 | ||
660 | (define-record-type* <qemu-binfmt-configuration> | |
661 | qemu-binfmt-configuration make-qemu-binfmt-configuration | |
662 | qemu-binfmt-configuration? | |
663 | (qemu qemu-binfmt-configuration-qemu | |
664 | (default qemu)) | |
665 | (platforms qemu-binfmt-configuration-platforms | |
71b98b9d LC |
666 | (default '())) ;safest default |
667 | (guix-support? qemu-binfmt-configuration-guix-support? | |
668 | (default #f))) | |
6738c29f LC |
669 | |
670 | (define (qemu-platform->binfmt qemu platform) | |
671 | "Return a gexp that evaluates to a binfmt string for PLATFORM, using the | |
672 | given QEMU package." | |
673 | (define (bytevector->binfmt-string bv) | |
674 | ;; Return a binfmt-friendly string representing BV. Hex-encode every | |
675 | ;; character, in particular because the doc notes "that you must escape | |
676 | ;; any NUL bytes; parsing halts at the first one". | |
677 | (string-concatenate | |
678 | (map (lambda (n) | |
679 | (string-append "\\x" | |
680 | (string-pad (number->string n 16) 2 #\0))) | |
681 | (bytevector->u8-list bv)))) | |
682 | ||
683 | (match platform | |
684 | (($ <qemu-platform> name family magic mask) | |
685 | ;; See 'Documentation/binfmt_misc.txt' in the kernel. | |
686 | #~(string-append ":qemu-" #$name ":M::" | |
687 | #$(bytevector->binfmt-string magic) | |
688 | ":" #$(bytevector->binfmt-string mask) | |
689 | ":" #$(file-append qemu "/bin/qemu-" name) | |
690 | ":" ;FLAGS go here | |
691 | )))) | |
692 | ||
693 | (define %binfmt-mount-point | |
694 | (file-system-mount-point %binary-format-file-system)) | |
695 | ||
696 | (define %binfmt-register-file | |
697 | (string-append %binfmt-mount-point "/register")) | |
698 | ||
699 | (define qemu-binfmt-shepherd-services | |
700 | (match-lambda | |
701 | (($ <qemu-binfmt-configuration> qemu platforms) | |
702 | (list (shepherd-service | |
703 | (provision '(qemu-binfmt)) | |
704 | (documentation "Install binfmt_misc handlers for QEMU.") | |
705 | (requirement '(file-system-/proc/sys/fs/binfmt_misc)) | |
706 | (start #~(lambda () | |
707 | ;; Register the handlers for all of PLATFORMS. | |
708 | (for-each (lambda (str) | |
709 | (call-with-output-file | |
710 | #$%binfmt-register-file | |
711 | (lambda (port) | |
712 | (display str port)))) | |
713 | (list | |
714 | #$@(map (cut qemu-platform->binfmt qemu | |
715 | <>) | |
716 | platforms))) | |
717 | #t)) | |
718 | (stop #~(lambda (_) | |
719 | ;; Unregister the handlers. | |
720 | (for-each (lambda (name) | |
721 | (let ((file (string-append | |
722 | #$%binfmt-mount-point | |
723 | "/qemu-" name))) | |
724 | (call-with-output-file file | |
725 | (lambda (port) | |
726 | (display "-1" port))))) | |
727 | '#$(map qemu-platform-name platforms)) | |
728 | #f))))))) | |
729 | ||
71b98b9d LC |
730 | (define qemu-binfmt-guix-chroot |
731 | (match-lambda | |
732 | ;; Add QEMU and its dependencies to the guix-daemon chroot so that our | |
733 | ;; binfmt_misc handlers work in the chroot (otherwise 'execve' would fail | |
734 | ;; with ENOENT.) | |
735 | ;; | |
736 | ;; The 'F' flag of binfmt_misc is meant to address this problem by loading | |
737 | ;; the interpreter upfront rather than lazily, but apparently that is | |
738 | ;; insufficient (perhaps it loads the 'qemu-ARCH' binary upfront but looks | |
739 | ;; up its dependencies lazily?). | |
740 | (($ <qemu-binfmt-configuration> qemu platforms guix?) | |
741 | (if guix? (list qemu) '())))) | |
742 | ||
6738c29f LC |
743 | (define qemu-binfmt-service-type |
744 | ;; TODO: Make a separate binfmt_misc service out of this? | |
745 | (service-type (name 'qemu-binfmt) | |
746 | (extensions | |
747 | (list (service-extension file-system-service-type | |
748 | (const | |
749 | (list %binary-format-file-system))) | |
750 | (service-extension shepherd-root-service-type | |
71b98b9d LC |
751 | qemu-binfmt-shepherd-services) |
752 | (service-extension guix-service-type | |
753 | qemu-binfmt-guix-chroot))) | |
6738c29f LC |
754 | (default-value (qemu-binfmt-configuration)) |
755 | (description | |
756 | "This service supports transparent emulation of binaries | |
757 | compiled for other architectures using QEMU and the @code{binfmt_misc} | |
758 | functionality of the kernel Linux."))) |