1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2017 Ryan Moe <ryan.moe@gmail.com>
3 ;;; Copyright © 2018, 2020 Ludovic Courtès <ludo@gnu.org>
4 ;;; Copyright © 2020 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
6 ;;; This file is part of GNU Guix.
8 ;;; GNU Guix is free software; you can redistribute it and/or modify it
9 ;;; under the terms of the GNU General Public License as published by
10 ;;; the Free Software Foundation; either version 3 of the License, or (at
11 ;;; your option) any later version.
13 ;;; GNU Guix is distributed in the hope that it will be useful, but
14 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;;; GNU General Public License for more details.
18 ;;; You should have received a copy of the GNU General Public License
19 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
21 (define-module (gnu services virtualization)
22 #:use-module (gnu bootloader)
23 #:use-module (gnu bootloader grub)
24 #:use-module (gnu image)
25 #:use-module (gnu packages admin)
26 #:use-module (gnu packages gdb)
27 #:use-module (gnu packages package-management)
28 #:use-module (gnu packages ssh)
29 #:use-module (gnu packages virtualization)
30 #:use-module (gnu services base)
31 #:use-module (gnu services configuration)
32 #:use-module (gnu services dbus)
33 #:use-module (gnu services shepherd)
34 #:use-module (gnu services ssh)
35 #:use-module (gnu services)
36 #:use-module (gnu system file-systems)
37 #:use-module (gnu system hurd)
38 #:use-module (gnu system image)
39 #:use-module (gnu system images hurd)
40 #:use-module (gnu system shadow)
41 #:use-module (gnu system)
42 #:use-module (guix derivations)
43 #:use-module (guix gexp)
44 #:use-module (guix modules)
45 #:use-module (guix monads)
46 #:use-module (guix packages)
47 #:use-module (guix records)
48 #:use-module (guix store)
49 #:use-module (guix utils)
51 #:use-module (srfi srfi-9)
52 #:use-module (srfi srfi-26)
53 #:use-module (rnrs bytevectors)
54 #:use-module (ice-9 match)
56 #:export (%hurd-vm-operating-system
58 hurd-vm-configuration?
59 hurd-vm-configuration-os
60 hurd-vm-configuration-qemu
61 hurd-vm-configuration-image
62 hurd-vm-configuration-disk-size
63 hurd-vm-configuration-memory-size
64 hurd-vm-configuration-options
65 hurd-vm-configuration-id
66 hurd-vm-configuration-net-options
67 hurd-vm-configuration-secrets
84 qemu-binfmt-configuration
85 qemu-binfmt-configuration?
86 qemu-binfmt-service-type))
88 (define (uglify-field-name field-name)
89 (let ((str (symbol->string field-name)))
91 (string-split (string-delete #\? str) #\-)
94 (define (quote-val val)
95 (string-append "\"" val "\""))
97 (define (serialize-field field-name val)
98 (format #t "~a = ~a\n" (uglify-field-name field-name) val))
100 (define (serialize-string field-name val)
101 (serialize-field field-name (quote-val val)))
103 (define (serialize-boolean field-name val)
104 (serialize-field field-name (if val 1 0)))
106 (define (serialize-integer field-name val)
107 (serialize-field field-name val))
109 (define (build-opt-list val)
112 (string-join (map quote-val val) ",")
115 (define optional-list? list?)
116 (define optional-string? string?)
118 (define (serialize-list field-name val)
119 (serialize-field field-name (build-opt-list val)))
121 (define (serialize-optional-list field-name val)
123 (format #t "# ~a = []\n" (uglify-field-name field-name))
124 (serialize-list field-name val)))
126 (define (serialize-optional-string field-name val)
127 (if (string-null? val)
128 (format #t "# ~a = \"\"\n" (uglify-field-name field-name))
129 (serialize-string field-name val)))
131 (define-configuration libvirt-configuration
137 "Flag listening for secure TLS connections on the public TCP/IP port.
138 must set @code{listen} for this to have any effect.
140 It is necessary to setup a CA and issue server certificates before
141 using this capability.")
144 "Listen for unencrypted TCP connections on the public TCP/IP port.
145 must set @code{listen} for this to have any effect.
147 Using the TCP socket requires SASL authentication by default. Only
148 SASL mechanisms which support data encryption are allowed. This is
149 DIGEST_MD5 and GSSAPI (Kerberos5)")
152 "Port for accepting secure TLS connections This can be a port number,
156 "Port for accepting insecure TCP connections This can be a port number,
160 "IP address or hostname used for client connections.")
163 "Flag toggling mDNS advertisement of the libvirt service.
165 Alternatively can disable for all services on a host by
166 stopping the Avahi daemon.")
168 (string (string-append "Virtualization Host " (gethostname)))
169 "Default mDNS advertisement name. This must be unique on the
170 immediate broadcast network.")
173 "UNIX domain socket group ownership. This can be used to
174 allow a 'trusted' set of users access to management capabilities
175 without becoming root.")
178 "UNIX socket permissions for the R/O socket. This is used
179 for monitoring VM status only.")
182 "UNIX socket permissions for the R/W socket. Default allows
183 only root. If PolicyKit is enabled on the socket, the default
184 will change to allow everyone (eg, 0777)")
185 (unix-sock-admin-perms
187 "UNIX socket permissions for the admin socket. Default allows
188 only owner (root), do not change it unless you are sure to whom
189 you are exposing the access to.")
191 (string "/var/run/libvirt")
192 "The directory in which sockets will be found/created.")
195 "Authentication scheme for UNIX read-only sockets. By default
196 socket permissions allow anyone to connect")
199 "Authentication scheme for UNIX read-write sockets. By default
200 socket permissions only allow root. If PolicyKit support was compiled
201 into libvirt, the default will be to use 'polkit' auth.")
204 "Authentication scheme for TCP sockets. If you don't enable SASL,
205 then all TCP traffic is cleartext. Don't do this outside of a dev/test
209 "Authentication scheme for TLS sockets. TLS sockets already have
210 encryption provided by the TLS layer, and limited authentication is
211 done by certificates.
213 It is possible to make use of any SASL authentication mechanism as
214 well, by using 'sasl' for this option")
217 "API access control scheme.
219 By default an authenticated user is allowed access to all APIs. Access
220 drivers can place restrictions on this.")
223 "Server key file path. If set to an empty string, then no private key
227 "Server key file path. If set to an empty string, then no certificate
231 "Server key file path. If set to an empty string, then no CA certificate
235 "Certificate revocation list path. If set to an empty string, then no
239 "Disable verification of our own server certificates.
241 When libvirtd starts it performs some sanity checks against its own
245 "Disable verification of client certificates.
247 Client certificate verification is the primary authentication mechanism.
248 Any client which does not present a certificate signed by the CA
252 "Whitelist of allowed x509 Distinguished Name.")
253 (sasl-allowed-usernames
255 "Whitelist of allowed SASL usernames. The format for username
256 depends on the SASL authentication mechanism.")
259 "Override the compile time default TLS priority string. The
260 default is usually \"NORMAL\" unless overridden at build time.
261 Only set this is it is desired for libvirt to deviate from
262 the global default settings.")
265 "Maximum number of concurrent client connections to allow
266 over all sockets combined.")
269 "Maximum length of queue of connections waiting to be
270 accepted by the daemon. Note, that some protocols supporting
271 retransmission may obey this so that a later reattempt at
272 connection succeeds.")
273 (max-anonymous-clients
275 "Maximum length of queue of accepted but not yet authenticated
276 clients. Set this to zero to turn this feature off")
279 "Number of workers to start up initially.")
282 "Maximum number of worker threads.
284 If the number of active clients exceeds @code{min-workers},
285 then more threads are spawned, up to max_workers limit.
286 Typically you'd want max_workers to equal maximum number
287 of clients allowed.")
290 "Number of priority workers. If all workers from above
291 pool are stuck, some calls marked as high priority
292 (notably domainDestroy) can be executed in this pool.")
295 "Total global limit on concurrent RPC calls.")
298 "Limit on concurrent requests from a single client
299 connection. To avoid one client monopolizing the server
300 this should be a small fraction of the global max_requests
301 and max_workers parameter.")
304 "Same as @code{min-workers} but for the admin interface.")
307 "Same as @code{max-workers} but for the admin interface.")
310 "Same as @code{max-clients} but for the admin interface.")
311 (admin-max-queued-clients
313 "Same as @code{max-queued-clients} but for the admin interface.")
314 (admin-max-client-requests
316 "Same as @code{max-client-requests} but for the admin interface.")
319 "Logging level. 4 errors, 3 warnings, 2 information, 1 debug.")
321 (string "3:remote 4:event")
324 A filter allows selecting a different logging level for a given category
326 The format for a filter is one of:
333 where @code{name} is a string which is matched against the category
334 given in the @code{VIR_LOG_INIT()} at the top of each libvirt source
335 file, e.g., \"remote\", \"qemu\", or \"util.json\" (the name in the
336 filter can be a substring of the full category name, in order
337 to match multiple similar categories), the optional \"+\" prefix
338 tells libvirt to log stack trace for each message matching
339 name, and @code{x} is the minimal level where matching messages should
349 Multiple filters can be defined in a single filters statement, they just
350 need to be separated by spaces.")
352 (string "3:syslog:libvirtd")
355 An output is one of the places to save logging information
356 The format for an output can be:
360 output goes to stderr
363 use syslog for the output and use the given name as the ident
365 @item x:file:file_path
366 output to a file, with the given filepath
369 output to journald logging system
372 In all case the x prefix is the minimal level, acting as a filter
381 Multiple outputs can be defined, they just need to be separated by spaces.")
384 "Allows usage of the auditing subsystem to be altered
387 @item 0: disable all auditing
388 @item 1: enable auditing, only if enabled on host
389 @item 2: enable auditing, and exit if disabled on host.
394 "Send audit messages via libvirt logging infrastructure.")
397 "Host UUID. UUID must not have all digits be the same.")
400 "Source to read host UUID.
404 @item @code{smbios}: fetch the UUID from @code{dmidecode -s system-uuid}
406 @item @code{machine-id}: fetch the UUID from @code{/etc/machine-id}
410 If @code{dmidecode} does not provide a valid UUID a temporary UUID
414 "A keepalive message is sent to a client after
415 @code{keepalive_interval} seconds of inactivity to check if
416 the client is still responding. If set to -1, libvirtd will
417 never send keepalive requests; however clients can still send
418 them and the daemon will send responses.")
421 "Maximum number of keepalive messages that are allowed to be sent
422 to the client without getting any response before the connection is
425 In other words, the connection is automatically
426 closed approximately after
427 @code{keepalive_interval * (keepalive_count + 1)} seconds since the last
428 message received from the client. When @code{keepalive-count} is
429 set to 0, connections will be automatically closed after
430 @code{keepalive-interval} seconds of inactivity without sending any
431 keepalive messages.")
432 (admin-keepalive-interval
434 "Same as above but for admin interface.")
435 (admin-keepalive-count
437 "Same as above but for admin interface.")
440 "Timeout for Open vSwitch calls.
442 The @code{ovs-vsctl} utility is used for the configuration and
443 its timeout option is set by default to 5 seconds to avoid
444 potential infinite waits blocking libvirt."))
446 (define* (libvirt-conf-file config)
447 "Return a libvirtd config file."
448 (plain-file "libvirtd.conf"
449 (with-output-to-string
451 (serialize-configuration config libvirt-configuration-fields)))))
453 (define %libvirt-accounts
454 (list (user-group (name "libvirt") (system? #t))))
456 (define (%libvirt-activation config)
457 (let ((sock-dir (libvirt-configuration-unix-sock-dir config)))
459 (use-modules (guix build utils))
460 (mkdir-p #$sock-dir))))
463 (define (libvirt-shepherd-service config)
464 (let* ((config-file (libvirt-conf-file config))
465 (libvirt (libvirt-configuration-libvirt config)))
466 (list (shepherd-service
467 (documentation "Run the libvirt daemon.")
468 (provision '(libvirtd))
469 (start #~(make-forkexec-constructor
470 (list (string-append #$libvirt "/sbin/libvirtd")
472 ;; For finding qemu and ip binaries.
473 #:environment-variables
475 "PATH=/run/current-system/profile/bin:"
476 "/run/current-system/profile/sbin"))))
477 (stop #~(make-kill-destructor))))))
479 (define libvirt-service-type
480 (service-type (name 'libvirt)
483 (service-extension polkit-service-type
484 (compose list libvirt-configuration-libvirt))
485 (service-extension profile-service-type
488 (libvirt-configuration-libvirt config)
490 (service-extension activation-service-type
492 (service-extension shepherd-root-service-type
493 libvirt-shepherd-service)
494 (service-extension account-service-type
495 (const %libvirt-accounts))))
496 (default-value (libvirt-configuration))))
499 (define-record-type* <virtlog-configuration>
500 virtlog-configuration make-virtlog-configuration
501 virtlog-configuration?
502 (libvirt virtlog-configuration-libvirt
504 (log-level virtlog-configuration-log-level
506 (log-filters virtlog-configuration-log-filters
507 (default "3:remote 4:event"))
508 (log-outputs virtlog-configuration-log-outputs
509 (default "3:syslog:virtlogd"))
510 (max-clients virtlog-configuration-max-clients
512 (max-size virtlog-configuration-max-size
513 (default 2097152)) ;; 2MB
514 (max-backups virtlog-configuration-max-backups
517 (define* (virtlogd-conf-file config)
518 "Return a virtlogd config file."
519 (plain-file "virtlogd.conf"
521 "log_level = " (number->string (virtlog-configuration-log-level config)) "\n"
522 "log_filters = \"" (virtlog-configuration-log-filters config) "\"\n"
523 "log_outputs = \"" (virtlog-configuration-log-outputs config) "\"\n"
524 "max_clients = " (number->string (virtlog-configuration-max-clients config)) "\n"
525 "max_size = " (number->string (virtlog-configuration-max-size config)) "\n"
526 "max_backups = " (number->string (virtlog-configuration-max-backups config)) "\n")))
528 (define (virtlogd-shepherd-service config)
529 (let* ((config-file (virtlogd-conf-file config))
530 (libvirt (virtlog-configuration-libvirt config)))
531 (list (shepherd-service
532 (documentation "Run the virtlog daemon.")
533 (provision '(virtlogd))
534 (start #~(make-forkexec-constructor
535 (list (string-append #$libvirt "/sbin/virtlogd")
536 "-f" #$config-file)))
537 (stop #~(make-kill-destructor))))))
539 (define virtlog-service-type
540 (service-type (name 'virtlogd)
543 (service-extension shepherd-root-service-type
544 virtlogd-shepherd-service)))
545 (default-value (virtlog-configuration))))
547 (define (generate-libvirt-documentation)
548 (generate-documentation
549 `((libvirt-configuration ,libvirt-configuration-fields))
550 'libvirt-configuration))
554 ;;; Transparent QEMU emulation via binfmt_misc.
557 ;; Platforms that QEMU can emulate.
558 (define-record-type <qemu-platform>
559 (qemu-platform name family magic mask)
561 (name qemu-platform-name) ;string
562 (family qemu-platform-family) ;string
563 (magic qemu-platform-magic) ;bytevector
564 (mask qemu-platform-mask)) ;bytevector
568 "Expand the given string into a bytevector."
571 (string? (syntax->datum #'str))
572 (let ((bv (u8-list->bytevector
574 (string->list (syntax->datum #'str))))))
577 ;;; The platform descriptions below are taken from
578 ;;; 'scripts/qemu-binfmt-conf.sh' in QEMU.
581 (qemu-platform "i386" "i386"
582 (bv "\x7fELF\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x03\x00")
583 (bv "\xff\xff\xff\xff\xff\xfe\xfe\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff")))
586 (qemu-platform "i486" "i386"
587 (bv "\x7fELF\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x06\x00")
588 (bv "\xff\xff\xff\xff\xff\xfe\xfe\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff")))
591 (qemu-platform "alpha" "alpha"
592 (bv "\x7fELF\x02\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x26\x90")
593 (bv "\xff\xff\xff\xff\xff\xfe\xfe\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff")))
596 (qemu-platform "arm" "arm"
597 (bv "\x7fELF\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x28\x00")
598 (bv "\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff")))
601 (qemu-platform "armeb" "arm"
602 (bv "\x7fELF\x01\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x28")
603 (bv "\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff")))
606 (qemu-platform "sparc" "sparc"
607 (bv "\x7fELF\x01\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x02")
608 (bv "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff")))
611 (qemu-platform "sparc32plus" "sparc"
612 (bv "\x7fELF\x01\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x12")
613 (bv "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff")))
616 (qemu-platform "ppc" "ppc"
617 (bv "\x7fELF\x01\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x14")
618 (bv "\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff")))
621 (qemu-platform "ppc64" "ppc"
622 (bv "\x7fELF\x02\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x15")
623 (bv "\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff")))
626 (qemu-platform "ppc64le" "ppcle"
627 (bv "\x7fELF\x02\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x15\x00")
628 (bv "\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\x00")))
631 (qemu-platform "m68k" "m68k"
632 (bv "\x7fELF\x01\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x04")
633 (bv "\xff\xff\xff\xff\xff\xff\xfe\xfe\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff")))
635 ;; XXX: We could use the other endianness on a MIPS host.
637 (qemu-platform "mips" "mips"
638 (bv "\x7fELF\x01\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x08")
639 (bv "\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff")))
642 (qemu-platform "mipsel" "mips"
643 (bv "\x7fELF\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x08\x00")
644 (bv "\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff")))
647 (qemu-platform "mipsn32" "mips"
648 (bv "\x7fELF\x01\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x08")
649 (bv "\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff")))
652 (qemu-platform "mipsn32el" "mips"
653 (bv "\x7fELF\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x08\x00")
654 (bv "\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff")))
657 (qemu-platform "mips64" "mips"
658 (bv "\x7fELF\x02\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x08")
659 (bv "\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff")))
662 (qemu-platform "mips64el" "mips"
663 (bv "\x7fELF\x02\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x08\x00")
664 (bv "\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff")))
667 (qemu-platform "riscv32" "riscv"
668 (bv "\x7fELF\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\xf3\x00")
669 (bv "\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff")))
672 (qemu-platform "riscv64" "riscv"
673 (bv "\x7fELF\x02\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\xf3\x00")
674 (bv "\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff")))
677 (qemu-platform "sh4" "sh4"
678 (bv "\x7fELF\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x2a\x00")
679 (bv "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff")))
682 (qemu-platform "sh4eb" "sh4"
683 (bv "\x7fELF\x01\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x2a")
684 (bv "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff")))
687 (qemu-platform "s390x" "s390x"
688 (bv "\x7fELF\x02\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x16")
689 (bv "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff")))
692 (qemu-platform "aarch64" "arm"
693 (bv "\x7fELF\x02\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\xb7\x00")
694 (bv "\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff")))
697 (qemu-platform "hppa" "hppa"
698 (bv "\x7f\x45\x4c\x46\x01\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x0f")
699 (bv "\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff")))
701 (define %qemu-platforms
702 (list %i386 %i486 %alpha %arm %sparc32plus %ppc %ppc64 %ppc64le %m68k
703 %mips %mipsel %mipsn32 %mipsn32el %mips64 %mips64el
704 %riscv32 %riscv64 %sh4 %sh4eb %s390x %aarch64 %hppa))
706 (define (lookup-qemu-platforms . names)
707 "Return the list of QEMU platforms that match NAMES--a list of names such as
708 \"arm\", \"hppa\", etc."
709 (filter (lambda (platform)
710 (member (qemu-platform-name platform) names))
713 (define-record-type* <qemu-binfmt-configuration>
714 qemu-binfmt-configuration make-qemu-binfmt-configuration
715 qemu-binfmt-configuration?
716 (qemu qemu-binfmt-configuration-qemu
718 (platforms qemu-binfmt-configuration-platforms
719 (default '())) ;safest default
720 (guix-support? qemu-binfmt-configuration-guix-support?
723 (define (qemu-platform->binfmt qemu platform)
724 "Return a gexp that evaluates to a binfmt string for PLATFORM, using the
726 (define (bytevector->binfmt-string bv)
727 ;; Return a binfmt-friendly string representing BV. Hex-encode every
728 ;; character, in particular because the doc notes "that you must escape
729 ;; any NUL bytes; parsing halts at the first one".
733 (string-pad (number->string n 16) 2 #\0)))
734 (bytevector->u8-list bv))))
737 (($ <qemu-platform> name family magic mask)
738 ;; See 'Documentation/binfmt_misc.txt' in the kernel.
739 #~(string-append ":qemu-" #$name ":M::"
740 #$(bytevector->binfmt-string magic)
741 ":" #$(bytevector->binfmt-string mask)
742 ":" #$(file-append qemu "/bin/qemu-" name)
746 (define %binfmt-mount-point
747 (file-system-mount-point %binary-format-file-system))
749 (define %binfmt-register-file
750 (string-append %binfmt-mount-point "/register"))
752 (define qemu-binfmt-shepherd-services
754 (($ <qemu-binfmt-configuration> qemu platforms)
755 (list (shepherd-service
756 (provision '(qemu-binfmt))
757 (documentation "Install binfmt_misc handlers for QEMU.")
758 (requirement '(file-system-/proc/sys/fs/binfmt_misc))
760 ;; Register the handlers for all of PLATFORMS.
761 (for-each (lambda (str)
762 (call-with-output-file
763 #$%binfmt-register-file
765 (display str port))))
767 #$@(map (cut qemu-platform->binfmt qemu
772 ;; Unregister the handlers.
773 (for-each (lambda (name)
774 (let ((file (string-append
775 #$%binfmt-mount-point
777 (call-with-output-file file
779 (display "-1" port)))))
780 '#$(map qemu-platform-name platforms))
783 (define qemu-binfmt-guix-chroot
785 ;; Add QEMU and its dependencies to the guix-daemon chroot so that our
786 ;; binfmt_misc handlers work in the chroot (otherwise 'execve' would fail
789 ;; The 'F' flag of binfmt_misc is meant to address this problem by loading
790 ;; the interpreter upfront rather than lazily, but apparently that is
791 ;; insufficient (perhaps it loads the 'qemu-ARCH' binary upfront but looks
792 ;; up its dependencies lazily?).
793 (($ <qemu-binfmt-configuration> qemu platforms guix?)
794 (if guix? (list qemu) '()))))
796 (define qemu-binfmt-service-type
797 ;; TODO: Make a separate binfmt_misc service out of this?
798 (service-type (name 'qemu-binfmt)
800 (list (service-extension file-system-service-type
802 (list %binary-format-file-system)))
803 (service-extension shepherd-root-service-type
804 qemu-binfmt-shepherd-services)
805 (service-extension guix-service-type
806 qemu-binfmt-guix-chroot)))
807 (default-value (qemu-binfmt-configuration))
809 "This service supports transparent emulation of binaries
810 compiled for other architectures using QEMU and the @code{binfmt_misc}
811 functionality of the kernel Linux.")))
815 ;;; Secrets for guest VMs.
818 (define (secret-service-activation port)
819 "Return an activation snippet that fetches sensitive material at local PORT,
820 over TCP. Reboot upon failure."
821 (with-imported-modules '((gnu build secret-service)
824 (use-modules (gnu build secret-service))
825 (let ((sent (secret-service-receive-secrets #$port)))
830 (define secret-service-type
832 (name 'secret-service)
833 (extensions (list (service-extension activation-service-type
834 secret-service-activation)))
836 "This service fetches secret key and other sensitive material over TCP at
837 boot time. This service is meant to be used by virtual machines (VMs) that
838 can only be accessed by their host.")))
840 (define (secret-service-operating-system os)
841 "Return an operating system based on OS that includes the secret-service,
842 that will be listening to receive secret keys on port 1004, TCP."
845 ;; Arrange so that the secret service activation snippet shows up before
846 ;; the OpenSSH and Guix activation snippets. That way, we receive OpenSSH
847 ;; and Guix keys before the activation snippets try to generate fresh keys
849 (services (append (operating-system-user-services os)
850 (list (service secret-service-type 1004))))))
854 ;;; The Hurd in VM service: a Childhurd.
857 (define %hurd-vm-operating-system
859 (inherit %hurd-default-operating-system)
860 (host-name "childhurd")
861 (timezone "Europe/Amsterdam")
862 (bootloader (bootloader-configuration
863 (bootloader grub-minimal-bootloader)
866 (packages (cons* gdb-minimal
867 (operating-system-packages
868 %hurd-default-operating-system)))
870 (service openssh-service-type
871 (openssh-configuration
872 (openssh openssh-sans-x)
875 (permit-root-login #t)
876 (allow-empty-passwords? #t)
877 (password-authentication? #t)))
878 %base-services/hurd))))
880 (define-record-type* <hurd-vm-configuration>
881 hurd-vm-configuration make-hurd-vm-configuration
882 hurd-vm-configuration?
883 (os hurd-vm-configuration-os ;<operating-system>
884 (default %hurd-vm-operating-system))
885 (qemu hurd-vm-configuration-qemu ;<package>
886 (default qemu-minimal))
887 (image hurd-vm-configuration-image ;string
889 (default (hurd-vm-disk-image this-record)))
890 (disk-size hurd-vm-configuration-disk-size ;number or 'guess
892 (memory-size hurd-vm-configuration-memory-size ;number
894 (options hurd-vm-configuration-options ;list of string
895 (default `("--snapshot")))
896 (id hurd-vm-configuration-id ;#f or integer [1..]
898 (net-options hurd-vm-configuration-net-options ;list of string
900 (default (hurd-vm-net-options this-record)))
901 (secret-root hurd-vm-configuration-secret-root ;string
902 (default "/etc/childhurd")))
904 (define (hurd-vm-disk-image config)
905 "Return a disk-image for the Hurd according to CONFIG. The secret-service
906 is added to the OS specified in CONFIG."
907 (let ((os (secret-service-operating-system (hurd-vm-configuration-os config)))
908 (disk-size (hurd-vm-configuration-disk-size config)))
911 (inherit hurd-disk-image)
912 (format 'compressed-qcow2)
914 (operating-system os)))))
916 (define (hurd-vm-port config base)
917 "Return the forwarded vm port for this childhurd config."
918 (let ((id (or (hurd-vm-configuration-id config) 0)))
919 (+ base (* 1000 id))))
920 (define %hurd-vm-secrets-port 11004)
921 (define %hurd-vm-ssh-port 10022)
922 (define %hurd-vm-vnc-port 15900)
924 (define (hurd-vm-net-options config)
925 `("--device" "rtl8139,netdev=net0"
927 ,(string-append "user,id=net0"
928 ",hostfwd=tcp:127.0.0.1:"
929 (number->string (hurd-vm-port config %hurd-vm-secrets-port))
931 ",hostfwd=tcp:127.0.0.1:"
932 (number->string (hurd-vm-port config %hurd-vm-ssh-port))
934 ",hostfwd=tcp:127.0.0.1:"
935 (number->string (hurd-vm-port config %hurd-vm-vnc-port))
938 (define (hurd-vm-shepherd-service config)
939 "Return a <shepherd-service> for a Hurd in a Virtual Machine with CONFIG."
941 (let ((image (hurd-vm-configuration-image config))
942 (qemu (hurd-vm-configuration-qemu config))
943 (memory-size (hurd-vm-configuration-memory-size config))
944 (options (hurd-vm-configuration-options config))
945 (id (hurd-vm-configuration-id config))
946 (net-options (hurd-vm-configuration-net-options config))
947 (provisions '(hurd-vm childhurd)))
950 #~(append (list #$(file-append qemu "/bin/qemu-system-i386")
951 "-m" (number->string #$memory-size)
956 ;; Cause the service to be respawned if the guest
957 ;; reboots (it can reboot for instance if it did not
958 ;; receive valid secrets, or if it crashed.)
960 (if (file-exists? "/dev/kvm")
966 (documentation "Run the Hurd in a Virtual Machine: a Childhurd.")
969 (cute symbol-append <>
970 (string->symbol (number->string id)))
973 (requirement '(loopback networking user-processes))
975 (with-imported-modules
976 (source-module-closure '((gnu build secret-service)
979 (let ((pid (fork+exec-command #$vm-command
981 ;; XXX TODO: use "childhurd" after
984 #:environment-variables
985 ;; QEMU tries to write to /var/tmp
988 (port #$(hurd-vm-port config %hurd-vm-secrets-port))
989 (root #$(hurd-vm-configuration-secret-root config)))
992 ;; XXX: 'secret-service-send-secrets' won't complete until
993 ;; the guest has booted and its secret service server is
994 ;; running, which could take 20+ seconds during which PID 1
996 (if (secret-service-send-secrets port root)
999 (kill (- pid) SIGTERM)
1001 (lambda (key . args)
1002 (kill (- pid) SIGTERM)
1003 (apply throw key args)))))))
1004 (modules `((gnu build secret-service)
1006 ,@%default-modules))
1007 (stop #~(make-kill-destructor))))))
1009 (define %hurd-vm-accounts
1010 (list (user-group (name "childhurd") (system? #t))
1014 (supplementary-groups '("kvm"))
1015 (comment "Privilege separation user for the childhurd")
1016 (home-directory "/var/empty")
1017 (shell (file-append shadow "/sbin/nologin"))
1020 (define (initialize-hurd-vm-substitutes)
1021 "Initialize the Hurd VM's key pair and ACL and store it on the host."
1023 (with-imported-modules '((guix build utils))
1025 (use-modules (guix build utils)
1029 "/etc/guix/signing-key.pub")
1034 (match (command-line)
1035 ((_ guest-config-directory)
1036 (setenv "GUIX_CONFIGURATION_DIRECTORY"
1037 guest-config-directory)
1038 (invoke #+(file-append guix "/bin/guix") "archive"
1041 (when (file-exists? host-acl)
1042 ;; Copy the host ACL.
1044 (string-append guest-config-directory
1047 (when (file-exists? host-key)
1048 ;; Add the host key to the childhurd's ACL.
1049 (let ((key (open-fdes host-key O_RDONLY)))
1052 (execl #+(file-append guix "/bin/guix")
1053 "guix" "archive" "--authorize"))))))))
1055 (program-file "initialize-hurd-vm-substitutes" run))
1057 (define (hurd-vm-activation config)
1058 "Return a gexp to activate the Hurd VM according to CONFIG."
1059 (with-imported-modules '((guix build utils))
1061 (use-modules (guix build utils))
1063 (define secret-directory
1064 #$(hurd-vm-configuration-secret-root config))
1066 (define ssh-directory
1067 (string-append secret-directory "/etc/ssh"))
1069 (define guix-directory
1070 (string-append secret-directory "/etc/guix"))
1072 (unless (file-exists? ssh-directory)
1073 ;; Generate SSH host keys under SSH-DIRECTORY.
1074 (mkdir-p ssh-directory)
1075 (invoke #$(file-append openssh "/bin/ssh-keygen")
1076 "-A" "-f" secret-directory))
1078 (unless (file-exists? guix-directory)
1079 (invoke #$(initialize-hurd-vm-substitutes)
1082 (define hurd-vm-service-type
1085 (extensions (list (service-extension shepherd-root-service-type
1086 hurd-vm-shepherd-service)
1087 (service-extension account-service-type
1088 (const %hurd-vm-accounts))
1089 (service-extension activation-service-type
1090 hurd-vm-activation)))
1091 (default-value (hurd-vm-configuration))
1093 "Provide a virtual machine (VM) running GNU/Hurd, also known as a
1094 @dfn{childhurd}.")))