virtualization.scm 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2017 Ryan Moe <ryan.moe@gmail.com>
  3. ;;; Copyright © 2018, 2020-2022 Ludovic Courtès <ludo@gnu.org>
  4. ;;; Copyright © 2020,2021 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
  5. ;;; Copyright © 2021 Timotej Lazar <timotej.lazar@araneo.si>
  6. ;;;
  7. ;;; This file is part of GNU Guix.
  8. ;;;
  9. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  10. ;;; under the terms of the GNU General Public License as published by
  11. ;;; the Free Software Foundation; either version 3 of the License, or (at
  12. ;;; your option) any later version.
  13. ;;;
  14. ;;; GNU Guix is distributed in the hope that it will be useful, but
  15. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  16. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. ;;; GNU General Public License for more details.
  18. ;;;
  19. ;;; You should have received a copy of the GNU General Public License
  20. ;;; 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 shadow)
  40. #:use-module (gnu system)
  41. #:use-module (guix derivations)
  42. #:use-module (guix gexp)
  43. #:use-module (guix modules)
  44. #:use-module (guix monads)
  45. #:use-module (guix packages)
  46. #:use-module (guix records)
  47. #:use-module (guix store)
  48. #:use-module (guix utils)
  49. #:use-module (srfi srfi-9)
  50. #:use-module (srfi srfi-26)
  51. #:use-module (rnrs bytevectors)
  52. #:use-module (ice-9 match)
  53. #:export (%hurd-vm-operating-system
  54. hurd-vm-configuration
  55. hurd-vm-configuration?
  56. hurd-vm-configuration-os
  57. hurd-vm-configuration-qemu
  58. hurd-vm-configuration-image
  59. hurd-vm-configuration-disk-size
  60. hurd-vm-configuration-memory-size
  61. hurd-vm-configuration-options
  62. hurd-vm-configuration-id
  63. hurd-vm-configuration-net-options
  64. hurd-vm-configuration-secrets
  65. hurd-vm-disk-image
  66. hurd-vm-port
  67. hurd-vm-net-options
  68. hurd-vm-service-type
  69. libvirt-configuration
  70. libvirt-service-type
  71. virtlog-configuration
  72. virtlog-service-type
  73. %qemu-platforms
  74. lookup-qemu-platforms
  75. qemu-platform?
  76. qemu-platform-name
  77. qemu-binfmt-configuration
  78. qemu-binfmt-configuration?
  79. qemu-binfmt-service-type
  80. qemu-guest-agent-configuration
  81. qemu-guest-agent-configuration?
  82. qemu-guest-agent-service-type))
  83. (define (uglify-field-name field-name)
  84. (let ((str (symbol->string field-name)))
  85. (string-join
  86. (string-split (string-delete #\? str) #\-)
  87. "_")))
  88. (define (quote-val val)
  89. (string-append "\"" val "\""))
  90. (define (serialize-field field-name val)
  91. (format #t "~a = ~a\n" (uglify-field-name field-name) val))
  92. (define (serialize-string field-name val)
  93. (serialize-field field-name (quote-val val)))
  94. (define (serialize-boolean field-name val)
  95. (serialize-field field-name (if val 1 0)))
  96. (define (serialize-integer field-name val)
  97. (serialize-field field-name val))
  98. (define (build-opt-list val)
  99. (string-append
  100. "["
  101. (string-join (map quote-val val) ",")
  102. "]"))
  103. (define optional-list? list?)
  104. (define optional-string? string?)
  105. (define (serialize-list field-name val)
  106. (serialize-field field-name (build-opt-list val)))
  107. (define (serialize-optional-list field-name val)
  108. (if (null? val)
  109. (format #t "# ~a = []\n" (uglify-field-name field-name))
  110. (serialize-list field-name val)))
  111. (define (serialize-optional-string field-name val)
  112. (if (string-null? val)
  113. (format #t "# ~a = \"\"\n" (uglify-field-name field-name))
  114. (serialize-string field-name val)))
  115. (define-configuration libvirt-configuration
  116. (libvirt
  117. (file-like libvirt)
  118. "Libvirt package.")
  119. (qemu
  120. (file-like qemu)
  121. "Qemu package.")
  122. (listen-tls?
  123. (boolean #t)
  124. "Flag listening for secure TLS connections on the public TCP/IP port.
  125. must set @code{listen} for this to have any effect.
  126. It is necessary to setup a CA and issue server certificates before
  127. using this capability.")
  128. (listen-tcp?
  129. (boolean #f)
  130. "Listen for unencrypted TCP connections on the public TCP/IP port.
  131. must set @code{listen} for this to have any effect.
  132. Using the TCP socket requires SASL authentication by default. Only
  133. SASL mechanisms which support data encryption are allowed. This is
  134. DIGEST_MD5 and GSSAPI (Kerberos5)")
  135. (tls-port
  136. (string "16514")
  137. "Port for accepting secure TLS connections This can be a port number,
  138. or service name")
  139. (tcp-port
  140. (string "16509")
  141. "Port for accepting insecure TCP connections This can be a port number,
  142. or service name")
  143. (listen-addr
  144. (string "0.0.0.0")
  145. "IP address or hostname used for client connections.")
  146. (mdns-adv?
  147. (boolean #f)
  148. "Flag toggling mDNS advertisement of the libvirt service.
  149. Alternatively can disable for all services on a host by
  150. stopping the Avahi daemon.")
  151. (mdns-name
  152. (string (string-append "Virtualization Host " (gethostname)))
  153. "Default mDNS advertisement name. This must be unique on the
  154. immediate broadcast network.")
  155. (unix-sock-group
  156. (string "libvirt")
  157. "UNIX domain socket group ownership. This can be used to
  158. allow a 'trusted' set of users access to management capabilities
  159. without becoming root.")
  160. (unix-sock-ro-perms
  161. (string "0777")
  162. "UNIX socket permissions for the R/O socket. This is used
  163. for monitoring VM status only.")
  164. (unix-sock-rw-perms
  165. (string "0770")
  166. "UNIX socket permissions for the R/W socket. Default allows
  167. only root. If PolicyKit is enabled on the socket, the default
  168. will change to allow everyone (eg, 0777)")
  169. (unix-sock-admin-perms
  170. (string "0777")
  171. "UNIX socket permissions for the admin socket. Default allows
  172. only owner (root), do not change it unless you are sure to whom
  173. you are exposing the access to.")
  174. (unix-sock-dir
  175. (string "/var/run/libvirt")
  176. "The directory in which sockets will be found/created.")
  177. (auth-unix-ro
  178. (string "polkit")
  179. "Authentication scheme for UNIX read-only sockets. By default
  180. socket permissions allow anyone to connect")
  181. (auth-unix-rw
  182. (string "polkit")
  183. "Authentication scheme for UNIX read-write sockets. By default
  184. socket permissions only allow root. If PolicyKit support was compiled
  185. into libvirt, the default will be to use 'polkit' auth.")
  186. (auth-tcp
  187. (string "sasl")
  188. "Authentication scheme for TCP sockets. If you don't enable SASL,
  189. then all TCP traffic is cleartext. Don't do this outside of a dev/test
  190. scenario.")
  191. (auth-tls
  192. (string "none")
  193. "Authentication scheme for TLS sockets. TLS sockets already have
  194. encryption provided by the TLS layer, and limited authentication is
  195. done by certificates.
  196. It is possible to make use of any SASL authentication mechanism as
  197. well, by using 'sasl' for this option")
  198. (access-drivers
  199. (optional-list '())
  200. "API access control scheme.
  201. By default an authenticated user is allowed access to all APIs. Access
  202. drivers can place restrictions on this.")
  203. (key-file
  204. (string "")
  205. "Server key file path. If set to an empty string, then no private key
  206. is loaded.")
  207. (cert-file
  208. (string "")
  209. "Server key file path. If set to an empty string, then no certificate
  210. is loaded.")
  211. (ca-file
  212. (string "")
  213. "Server key file path. If set to an empty string, then no CA certificate
  214. is loaded.")
  215. (crl-file
  216. (string "")
  217. "Certificate revocation list path. If set to an empty string, then no
  218. CRL is loaded.")
  219. (tls-no-sanity-cert
  220. (boolean #f)
  221. "Disable verification of our own server certificates.
  222. When libvirtd starts it performs some sanity checks against its own
  223. certificates.")
  224. (tls-no-verify-cert
  225. (boolean #f)
  226. "Disable verification of client certificates.
  227. Client certificate verification is the primary authentication mechanism.
  228. Any client which does not present a certificate signed by the CA
  229. will be rejected.")
  230. (tls-allowed-dn-list
  231. (optional-list '())
  232. "Whitelist of allowed x509 Distinguished Name.")
  233. (sasl-allowed-usernames
  234. (optional-list '())
  235. "Whitelist of allowed SASL usernames. The format for username
  236. depends on the SASL authentication mechanism.")
  237. (tls-priority
  238. (string "NORMAL")
  239. "Override the compile time default TLS priority string. The
  240. default is usually \"NORMAL\" unless overridden at build time.
  241. Only set this is it is desired for libvirt to deviate from
  242. the global default settings.")
  243. (max-clients
  244. (integer 5000)
  245. "Maximum number of concurrent client connections to allow
  246. over all sockets combined.")
  247. (max-queued-clients
  248. (integer 1000)
  249. "Maximum length of queue of connections waiting to be
  250. accepted by the daemon. Note, that some protocols supporting
  251. retransmission may obey this so that a later reattempt at
  252. connection succeeds.")
  253. (max-anonymous-clients
  254. (integer 20)
  255. "Maximum length of queue of accepted but not yet authenticated
  256. clients. Set this to zero to turn this feature off")
  257. (min-workers
  258. (integer 5)
  259. "Number of workers to start up initially.")
  260. (max-workers
  261. (integer 20)
  262. "Maximum number of worker threads.
  263. If the number of active clients exceeds @code{min-workers},
  264. then more threads are spawned, up to max_workers limit.
  265. Typically you'd want max_workers to equal maximum number
  266. of clients allowed.")
  267. (prio-workers
  268. (integer 5)
  269. "Number of priority workers. If all workers from above
  270. pool are stuck, some calls marked as high priority
  271. (notably domainDestroy) can be executed in this pool.")
  272. (max-requests
  273. (integer 20)
  274. "Total global limit on concurrent RPC calls.")
  275. (max-client-requests
  276. (integer 5)
  277. "Limit on concurrent requests from a single client
  278. connection. To avoid one client monopolizing the server
  279. this should be a small fraction of the global max_requests
  280. and max_workers parameter.")
  281. (admin-min-workers
  282. (integer 1)
  283. "Same as @code{min-workers} but for the admin interface.")
  284. (admin-max-workers
  285. (integer 5)
  286. "Same as @code{max-workers} but for the admin interface.")
  287. (admin-max-clients
  288. (integer 5)
  289. "Same as @code{max-clients} but for the admin interface.")
  290. (admin-max-queued-clients
  291. (integer 5)
  292. "Same as @code{max-queued-clients} but for the admin interface.")
  293. (admin-max-client-requests
  294. (integer 5)
  295. "Same as @code{max-client-requests} but for the admin interface.")
  296. (log-level
  297. (integer 3)
  298. "Logging level. 4 errors, 3 warnings, 2 information, 1 debug.")
  299. (log-filters
  300. (string "3:remote 4:event")
  301. "Logging filters.
  302. A filter allows selecting a different logging level for a given category
  303. of logs
  304. The format for a filter is one of:
  305. @itemize
  306. @item x:name
  307. @item x:+name
  308. @end itemize
  309. where @code{name} is a string which is matched against the category
  310. given in the @code{VIR_LOG_INIT()} at the top of each libvirt source
  311. file, e.g., \"remote\", \"qemu\", or \"util.json\" (the name in the
  312. filter can be a substring of the full category name, in order
  313. to match multiple similar categories), the optional \"+\" prefix
  314. tells libvirt to log stack trace for each message matching
  315. name, and @code{x} is the minimal level where matching messages should
  316. be logged:
  317. @itemize
  318. @item 1: DEBUG
  319. @item 2: INFO
  320. @item 3: WARNING
  321. @item 4: ERROR
  322. @end itemize
  323. Multiple filters can be defined in a single filters statement, they just
  324. need to be separated by spaces.")
  325. (log-outputs
  326. (string "3:syslog:libvirtd")
  327. "Logging outputs.
  328. An output is one of the places to save logging information
  329. The format for an output can be:
  330. @table @code
  331. @item x:stderr
  332. output goes to stderr
  333. @item x:syslog:name
  334. use syslog for the output and use the given name as the ident
  335. @item x:file:file_path
  336. output to a file, with the given filepath
  337. @item x:journald
  338. output to journald logging system
  339. @end table
  340. In all case the x prefix is the minimal level, acting as a filter
  341. @itemize
  342. @item 1: DEBUG
  343. @item 2: INFO
  344. @item 3: WARNING
  345. @item 4: ERROR
  346. @end itemize
  347. Multiple outputs can be defined, they just need to be separated by spaces.")
  348. (audit-level
  349. (integer 1)
  350. "Allows usage of the auditing subsystem to be altered
  351. @itemize
  352. @item 0: disable all auditing
  353. @item 1: enable auditing, only if enabled on host
  354. @item 2: enable auditing, and exit if disabled on host.
  355. @end itemize
  356. ")
  357. (audit-logging
  358. (boolean #f)
  359. "Send audit messages via libvirt logging infrastructure.")
  360. (host-uuid
  361. (optional-string "")
  362. "Host UUID. UUID must not have all digits be the same.")
  363. (host-uuid-source
  364. (string "smbios")
  365. "Source to read host UUID.
  366. @itemize
  367. @item @code{smbios}: fetch the UUID from @code{dmidecode -s system-uuid}
  368. @item @code{machine-id}: fetch the UUID from @code{/etc/machine-id}
  369. @end itemize
  370. If @code{dmidecode} does not provide a valid UUID a temporary UUID
  371. will be generated.")
  372. (keepalive-interval
  373. (integer 5)
  374. "A keepalive message is sent to a client after
  375. @code{keepalive_interval} seconds of inactivity to check if
  376. the client is still responding. If set to -1, libvirtd will
  377. never send keepalive requests; however clients can still send
  378. them and the daemon will send responses.")
  379. (keepalive-count
  380. (integer 5)
  381. "Maximum number of keepalive messages that are allowed to be sent
  382. to the client without getting any response before the connection is
  383. considered broken.
  384. In other words, the connection is automatically
  385. closed approximately after
  386. @code{keepalive_interval * (keepalive_count + 1)} seconds since the last
  387. message received from the client. When @code{keepalive-count} is
  388. set to 0, connections will be automatically closed after
  389. @code{keepalive-interval} seconds of inactivity without sending any
  390. keepalive messages.")
  391. (admin-keepalive-interval
  392. (integer 5)
  393. "Same as above but for admin interface.")
  394. (admin-keepalive-count
  395. (integer 5)
  396. "Same as above but for admin interface.")
  397. (ovs-timeout
  398. (integer 5)
  399. "Timeout for Open vSwitch calls.
  400. The @code{ovs-vsctl} utility is used for the configuration and
  401. its timeout option is set by default to 5 seconds to avoid
  402. potential infinite waits blocking libvirt."))
  403. (define* (libvirt-conf-file config)
  404. "Return a libvirtd config file."
  405. (plain-file "libvirtd.conf"
  406. (with-output-to-string
  407. (lambda ()
  408. (serialize-configuration config libvirt-configuration-fields)))))
  409. (define %libvirt-accounts
  410. (list (user-group (name "libvirt") (system? #t))))
  411. (define (%libvirt-activation config)
  412. (let ((sock-dir (libvirt-configuration-unix-sock-dir config)))
  413. #~(begin
  414. (use-modules (guix build utils))
  415. (mkdir-p #$sock-dir))))
  416. (define (libvirt-shepherd-service config)
  417. (let* ((config-file (libvirt-conf-file config))
  418. (libvirt (libvirt-configuration-libvirt config)))
  419. (list (shepherd-service
  420. (documentation "Run the libvirt daemon.")
  421. (provision '(libvirtd))
  422. (start #~(make-forkexec-constructor
  423. (list (string-append #$libvirt "/sbin/libvirtd")
  424. "-f" #$config-file)
  425. ;; For finding qemu and ip binaries.
  426. #:environment-variables
  427. (list (string-append
  428. "PATH=/run/current-system/profile/bin:"
  429. "/run/current-system/profile/sbin"))))
  430. (stop #~(make-kill-destructor))))))
  431. (define libvirt-service-type
  432. (service-type (name 'libvirt)
  433. (extensions
  434. (list
  435. (service-extension polkit-service-type
  436. (compose list libvirt-configuration-libvirt))
  437. (service-extension profile-service-type
  438. (lambda (config)
  439. (list
  440. (libvirt-configuration-libvirt config)
  441. (libvirt-configuration-qemu config))))
  442. (service-extension activation-service-type
  443. %libvirt-activation)
  444. (service-extension shepherd-root-service-type
  445. libvirt-shepherd-service)
  446. (service-extension account-service-type
  447. (const %libvirt-accounts))))
  448. (default-value (libvirt-configuration))))
  449. (define-record-type* <virtlog-configuration>
  450. virtlog-configuration make-virtlog-configuration
  451. virtlog-configuration?
  452. (libvirt virtlog-configuration-libvirt
  453. (default libvirt))
  454. (log-level virtlog-configuration-log-level
  455. (default 3))
  456. (log-filters virtlog-configuration-log-filters
  457. (default "3:remote 4:event"))
  458. (log-outputs virtlog-configuration-log-outputs
  459. (default "3:syslog:virtlogd"))
  460. (max-clients virtlog-configuration-max-clients
  461. (default 1024))
  462. (max-size virtlog-configuration-max-size
  463. (default 2097152)) ;; 2MB
  464. (max-backups virtlog-configuration-max-backups
  465. (default 3)))
  466. (define* (virtlogd-conf-file config)
  467. "Return a virtlogd config file."
  468. (plain-file "virtlogd.conf"
  469. (string-append
  470. "log_level = " (number->string (virtlog-configuration-log-level config)) "\n"
  471. "log_filters = \"" (virtlog-configuration-log-filters config) "\"\n"
  472. "log_outputs = \"" (virtlog-configuration-log-outputs config) "\"\n"
  473. "max_clients = " (number->string (virtlog-configuration-max-clients config)) "\n"
  474. "max_size = " (number->string (virtlog-configuration-max-size config)) "\n"
  475. "max_backups = " (number->string (virtlog-configuration-max-backups config)) "\n")))
  476. (define (virtlogd-shepherd-service config)
  477. (let* ((config-file (virtlogd-conf-file config))
  478. (libvirt (virtlog-configuration-libvirt config)))
  479. (list (shepherd-service
  480. (documentation "Run the virtlog daemon.")
  481. (provision '(virtlogd))
  482. (start #~(make-forkexec-constructor
  483. (list (string-append #$libvirt "/sbin/virtlogd")
  484. "-f" #$config-file)))
  485. (stop #~(make-kill-destructor))))))
  486. (define virtlog-service-type
  487. (service-type (name 'virtlogd)
  488. (extensions
  489. (list
  490. (service-extension shepherd-root-service-type
  491. virtlogd-shepherd-service)))
  492. (default-value (virtlog-configuration))))
  493. (define (generate-libvirt-documentation)
  494. (generate-documentation
  495. `((libvirt-configuration ,libvirt-configuration-fields))
  496. 'libvirt-configuration))
  497. ;;;
  498. ;;; Transparent QEMU emulation via binfmt_misc.
  499. ;;;
  500. ;; Platforms that QEMU can emulate.
  501. (define-record-type* <qemu-platform>
  502. qemu-platform make-qemu-platform
  503. qemu-platform?
  504. (name qemu-platform-name) ;string
  505. (family qemu-platform-family) ;string
  506. (magic qemu-platform-magic) ;bytevector
  507. (mask qemu-platform-mask) ;bytevector
  508. ;; Default flags:
  509. ;;
  510. ;; "F": fix binary. Open the qemu-user binary (statically linked) as soon
  511. ;; as binfmt_misc interpretation is handled.
  512. ;;
  513. ;; "P": preserve argv[0]. QEMU 6.0 detects whether it's started with this
  514. ;; flag and automatically does the right thing. Without this flag,
  515. ;; argv[0] is replaced by the absolute file name of the executable, an
  516. ;; observable difference that can cause discrepancies.
  517. (flags qemu-platform-flags (default "FP"))) ;string
  518. (define-syntax bv
  519. (lambda (s)
  520. "Expand the given string into a bytevector."
  521. (syntax-case s ()
  522. ((_ str)
  523. (string? (syntax->datum #'str))
  524. (let ((bv (u8-list->bytevector
  525. (map char->integer
  526. (string->list (syntax->datum #'str))))))
  527. bv)))))
  528. ;;; The platform descriptions below are taken from
  529. ;;; 'scripts/qemu-binfmt-conf.sh' in QEMU.
  530. (define %i386
  531. (qemu-platform
  532. (name "i386")
  533. (family "i386")
  534. (magic (bv "\x7fELF\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x03\x00"))
  535. (mask (bv "\xff\xff\xff\xff\xff\xfe\xfe\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff"))))
  536. (define %alpha
  537. (qemu-platform
  538. (name "alpha")
  539. (family "alpha")
  540. (magic (bv "\x7fELF\x02\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x26\x90"))
  541. (mask (bv "\xff\xff\xff\xff\xff\xfe\xfe\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff"))))
  542. (define %arm
  543. (qemu-platform
  544. (name "arm")
  545. (family "arm")
  546. (magic (bv "\x7fELF\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x28\x00"))
  547. (mask (bv "\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff"))))
  548. (define %armeb
  549. (qemu-platform
  550. (name "armeb")
  551. (family "arm")
  552. (magic (bv "\x7fELF\x01\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x28"))
  553. (mask (bv "\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff"))))
  554. (define %sparc
  555. (qemu-platform
  556. (name "sparc")
  557. (family "sparc")
  558. (magic (bv "\x7fELF\x01\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x02"))
  559. (mask (bv "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff"))))
  560. (define %sparc32plus
  561. (qemu-platform
  562. (name "sparc32plus")
  563. (family "sparc")
  564. (magic (bv "\x7fELF\x01\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x12"))
  565. (mask (bv "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff"))))
  566. (define %ppc
  567. (qemu-platform
  568. (name "ppc")
  569. (family "ppc")
  570. (magic (bv "\x7fELF\x01\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x14"))
  571. (mask (bv "\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff"))))
  572. (define %ppc64
  573. (qemu-platform
  574. (name "ppc64")
  575. (family "ppc")
  576. (magic (bv "\x7fELF\x02\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x15"))
  577. (mask (bv "\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff"))))
  578. (define %ppc64le
  579. (qemu-platform
  580. (name "ppc64le")
  581. (family "ppcle")
  582. (magic (bv "\x7fELF\x02\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x15\x00"))
  583. (mask (bv "\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\x00"))))
  584. (define %m68k
  585. (qemu-platform
  586. (name "m68k")
  587. (family "m68k")
  588. (magic (bv "\x7fELF\x01\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x04"))
  589. (mask (bv "\xff\xff\xff\xff\xff\xff\xfe\xfe\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff"))))
  590. ;; XXX: We could use the other endianness on a MIPS host.
  591. (define %mips
  592. (qemu-platform
  593. (name "mips")
  594. (family "mips")
  595. (magic (bv "\x7fELF\x01\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x08"))
  596. (mask (bv "\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff"))))
  597. (define %mipsel
  598. (qemu-platform
  599. (name "mipsel")
  600. (family "mips")
  601. (magic (bv "\x7fELF\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x08\x00"))
  602. (mask (bv "\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff"))))
  603. (define %mipsn32
  604. (qemu-platform
  605. (name "mipsn32")
  606. (family "mips")
  607. (magic (bv "\x7fELF\x01\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x08"))
  608. (mask (bv "\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff"))))
  609. (define %mipsn32el
  610. (qemu-platform
  611. (name "mipsn32el")
  612. (family "mips")
  613. (magic (bv "\x7fELF\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x08\x00"))
  614. (mask (bv "\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff"))))
  615. (define %mips64
  616. (qemu-platform
  617. (name "mips64")
  618. (family "mips")
  619. (magic (bv "\x7fELF\x02\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x08"))
  620. (mask (bv "\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff"))))
  621. (define %mips64el
  622. (qemu-platform
  623. (name "mips64el")
  624. (family "mips")
  625. (magic (bv "\x7fELF\x02\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x08\x00"))
  626. (mask (bv "\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff"))))
  627. (define %riscv32
  628. (qemu-platform
  629. (name "riscv32")
  630. (family "riscv")
  631. (magic (bv "\x7fELF\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\xf3\x00"))
  632. (mask (bv "\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff"))))
  633. (define %riscv64
  634. (qemu-platform
  635. (name "riscv64")
  636. (family "riscv")
  637. (magic (bv "\x7fELF\x02\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\xf3\x00"))
  638. (mask (bv "\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff"))))
  639. (define %sh4
  640. (qemu-platform
  641. (name "sh4")
  642. (family "sh4")
  643. (magic (bv "\x7fELF\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x2a\x00"))
  644. (mask (bv "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff"))))
  645. (define %sh4eb
  646. (qemu-platform
  647. (name "sh4eb")
  648. (family "sh4")
  649. (magic (bv "\x7fELF\x01\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x2a"))
  650. (mask (bv "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff"))))
  651. (define %s390x
  652. (qemu-platform
  653. (name "s390x")
  654. (family "s390x")
  655. (magic (bv "\x7fELF\x02\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x16"))
  656. (mask (bv "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff"))))
  657. (define %aarch64
  658. (qemu-platform
  659. (name "aarch64")
  660. (family "arm")
  661. (magic (bv "\x7fELF\x02\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\xb7\x00"))
  662. (mask (bv "\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff"))))
  663. (define %hppa
  664. (qemu-platform
  665. (name "hppa")
  666. (family "hppa")
  667. (magic (bv "\x7f\x45\x4c\x46\x01\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x0f"))
  668. (mask (bv "\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff"))))
  669. (define %qemu-platforms
  670. (list %i386 %alpha %arm %sparc32plus %ppc %ppc64 %ppc64le %m68k
  671. %mips %mipsel %mipsn32 %mipsn32el %mips64 %mips64el
  672. %riscv32 %riscv64 %sh4 %sh4eb %s390x %aarch64 %hppa))
  673. (define (lookup-qemu-platforms . names)
  674. "Return the list of QEMU platforms that match NAMES--a list of names such as
  675. \"arm\", \"hppa\", etc."
  676. (filter (lambda (platform)
  677. (member (qemu-platform-name platform) names))
  678. %qemu-platforms))
  679. (define-record-type* <qemu-binfmt-configuration>
  680. qemu-binfmt-configuration make-qemu-binfmt-configuration
  681. qemu-binfmt-configuration?
  682. (qemu qemu-binfmt-configuration-qemu
  683. (default qemu))
  684. (platforms qemu-binfmt-configuration-platforms
  685. (default '()))) ;safest default
  686. (define (qemu-platform->binfmt qemu platform)
  687. "Return a gexp that evaluates to a binfmt string for PLATFORM, using the
  688. given QEMU package."
  689. (define (bytevector->binfmt-string bv)
  690. ;; Return a binfmt-friendly string representing BV. Hex-encode every
  691. ;; character, in particular because the doc notes "that you must escape
  692. ;; any NUL bytes; parsing halts at the first one".
  693. (string-concatenate
  694. (map (lambda (n)
  695. (string-append "\\x"
  696. (string-pad (number->string n 16) 2 #\0)))
  697. (bytevector->u8-list bv))))
  698. (match platform
  699. (($ <qemu-platform> name family magic mask flags)
  700. ;; See 'Documentation/binfmt_misc.txt' in the kernel.
  701. #~(string-append ":qemu-" #$name ":M::"
  702. #$(bytevector->binfmt-string magic)
  703. ":" #$(bytevector->binfmt-string mask)
  704. ":" #$qemu:static "/bin/qemu-" #$name
  705. ":" #$flags))))
  706. (define %binfmt-mount-point
  707. (file-system-mount-point %binary-format-file-system))
  708. (define %binfmt-register-file
  709. (string-append %binfmt-mount-point "/register"))
  710. (define qemu-binfmt-shepherd-services
  711. (match-lambda
  712. (($ <qemu-binfmt-configuration> qemu platforms)
  713. (list (shepherd-service
  714. (provision '(qemu-binfmt))
  715. (documentation "Install binfmt_misc handlers for QEMU.")
  716. (requirement '(file-system-/proc/sys/fs/binfmt_misc))
  717. (start #~(lambda ()
  718. ;; Register the handlers for all of PLATFORMS.
  719. (for-each (lambda (str)
  720. (call-with-output-file
  721. #$%binfmt-register-file
  722. (lambda (port)
  723. (display str port))))
  724. (list
  725. #$@(map (cut qemu-platform->binfmt qemu
  726. <>)
  727. platforms)))
  728. #t))
  729. (stop #~(lambda (_)
  730. ;; Unregister the handlers.
  731. (for-each (lambda (name)
  732. (let ((file (string-append
  733. #$%binfmt-mount-point
  734. "/qemu-" name)))
  735. (call-with-output-file file
  736. (lambda (port)
  737. (display "-1" port)))))
  738. '#$(map qemu-platform-name platforms))
  739. #f)))))))
  740. (define qemu-binfmt-service-type
  741. ;; TODO: Make a separate binfmt_misc service out of this?
  742. (service-type (name 'qemu-binfmt)
  743. (extensions
  744. (list (service-extension file-system-service-type
  745. (const
  746. (list %binary-format-file-system)))
  747. (service-extension shepherd-root-service-type
  748. qemu-binfmt-shepherd-services)))
  749. (default-value (qemu-binfmt-configuration))
  750. (description
  751. "This service supports transparent emulation of binaries
  752. compiled for other architectures using QEMU and the @code{binfmt_misc}
  753. functionality of the kernel Linux.")))
  754. ;;;
  755. ;;; QEMU guest agent service.
  756. ;;;
  757. (define-configuration qemu-guest-agent-configuration
  758. (qemu
  759. (file-like qemu-minimal)
  760. "QEMU package.")
  761. (device
  762. (string "")
  763. "Path to device or socket used to communicate with the host. If not
  764. specified, the QEMU default path is used."))
  765. (define (qemu-guest-agent-shepherd-service config)
  766. (let ((qemu (qemu-guest-agent-configuration-qemu config))
  767. (device (qemu-guest-agent-configuration-device config)))
  768. (list
  769. (shepherd-service
  770. (provision '(qemu-guest-agent))
  771. (documentation "Run the QEMU guest agent.")
  772. (start #~(make-forkexec-constructor
  773. `(,(string-append #$qemu "/bin/qemu-ga") "--daemon"
  774. "--pidfile=/var/run/qemu-ga.pid"
  775. "--statedir=/var/run"
  776. ,@(if #$device
  777. (list (string-append "--path=" #$device))
  778. '()))
  779. #:pid-file "/var/run/qemu-ga.pid"
  780. #:log-file "/var/log/qemu-ga.log"))
  781. (stop #~(make-kill-destructor))))))
  782. (define qemu-guest-agent-service-type
  783. (service-type
  784. (name 'qemu-guest-agent)
  785. (extensions
  786. (list (service-extension shepherd-root-service-type
  787. qemu-guest-agent-shepherd-service)))
  788. (default-value (qemu-guest-agent-configuration))
  789. (description "Run the QEMU guest agent.")))
  790. ;;;
  791. ;;; Secrets for guest VMs.
  792. ;;;
  793. (define (secret-service-shepherd-services port)
  794. "Return a Shepherd service that fetches sensitive material at local PORT,
  795. over TCP. Reboot upon failure."
  796. ;; This is a Shepherd service, rather than an activation snippet, to make
  797. ;; sure it is started once 'networking' is up so it can accept incoming
  798. ;; connections.
  799. (list
  800. (shepherd-service
  801. (documentation "Fetch secrets from the host at startup time.")
  802. (provision '(secret-service-client))
  803. (requirement '(loopback networking))
  804. (modules '((gnu build secret-service)
  805. (guix build utils)))
  806. (start (with-imported-modules '((gnu build secret-service)
  807. (guix build utils))
  808. #~(lambda ()
  809. ;; Since shepherd's output port goes to /dev/log, write this
  810. ;; message to stderr so it's visible on the Mach console.
  811. (format (current-error-port)
  812. "receiving secrets from the host...~%")
  813. (force-output (current-error-port))
  814. (let ((sent (secret-service-receive-secrets #$port)))
  815. (unless sent
  816. (sleep 3)
  817. (reboot))))))
  818. (stop #~(const #f)))))
  819. (define secret-service-type
  820. (service-type
  821. (name 'secret-service)
  822. (extensions (list (service-extension shepherd-root-service-type
  823. secret-service-shepherd-services)
  824. ;; Make every Shepherd service depend on
  825. ;; 'secret-service-client'.
  826. (service-extension user-processes-service-type
  827. (const '(secret-service-client)))))
  828. (description
  829. "This service fetches secret key and other sensitive material over TCP at
  830. boot time. This service is meant to be used by virtual machines (VMs) that
  831. can only be accessed by their host.")))
  832. (define (secret-service-operating-system os)
  833. "Return an operating system based on OS that includes the secret-service,
  834. that will be listening to receive secret keys on port 1004, TCP."
  835. (operating-system
  836. (inherit os)
  837. ;; Arrange so that the secret service activation snippet shows up before
  838. ;; the OpenSSH and Guix activation snippets. That way, we receive OpenSSH
  839. ;; and Guix keys before the activation snippets try to generate fresh keys
  840. ;; for nothing.
  841. (services (append (operating-system-user-services os)
  842. (list (service secret-service-type 1004))))))
  843. ;;;
  844. ;;; The Hurd in VM service: a Childhurd.
  845. ;;;
  846. (define %hurd-vm-operating-system
  847. (operating-system
  848. (inherit %hurd-default-operating-system)
  849. (host-name "childhurd")
  850. (timezone "Europe/Amsterdam")
  851. (bootloader (bootloader-configuration
  852. (bootloader grub-minimal-bootloader)
  853. (targets '("/dev/vda"))
  854. (timeout 0)))
  855. (packages (cons* gdb-minimal
  856. (operating-system-packages
  857. %hurd-default-operating-system)))
  858. (services (cons*
  859. (service openssh-service-type
  860. (openssh-configuration
  861. (openssh openssh-sans-x)
  862. (use-pam? #f)
  863. (port-number 2222)
  864. (permit-root-login #t)
  865. (allow-empty-passwords? #t)
  866. (password-authentication? #t)))
  867. ;; By default, the secret service introduces a pre-initialized
  868. ;; /etc/guix/acl file in the childhurd. Thus, clear
  869. ;; 'authorize-key?' so that it's not overridden at activation
  870. ;; time.
  871. (modify-services %base-services/hurd
  872. (guix-service-type config =>
  873. (guix-configuration
  874. (inherit config)
  875. (authorize-key? #f))))))))
  876. (define-record-type* <hurd-vm-configuration>
  877. hurd-vm-configuration make-hurd-vm-configuration
  878. hurd-vm-configuration?
  879. (os hurd-vm-configuration-os ;<operating-system>
  880. (default %hurd-vm-operating-system))
  881. (qemu hurd-vm-configuration-qemu ;file-like
  882. (default qemu-minimal))
  883. (image hurd-vm-configuration-image ;string
  884. (thunked)
  885. (default (hurd-vm-disk-image this-record)))
  886. (disk-size hurd-vm-configuration-disk-size ;number or 'guess
  887. (default 'guess))
  888. (memory-size hurd-vm-configuration-memory-size ;number
  889. (default 512))
  890. (options hurd-vm-configuration-options ;list of string
  891. (default `("--snapshot")))
  892. (id hurd-vm-configuration-id ;#f or integer [1..]
  893. (default #f))
  894. (net-options hurd-vm-configuration-net-options ;list of string
  895. (thunked)
  896. (default (hurd-vm-net-options this-record)))
  897. (secret-root hurd-vm-configuration-secret-root ;string
  898. (default "/etc/childhurd")))
  899. (define (hurd-vm-disk-image config)
  900. "Return a disk-image for the Hurd according to CONFIG. The secret-service
  901. is added to the OS specified in CONFIG."
  902. (let* ((os (secret-service-operating-system
  903. (hurd-vm-configuration-os config)))
  904. (disk-size (hurd-vm-configuration-disk-size config))
  905. (type (lookup-image-type-by-name 'hurd-qcow2))
  906. (os->image (image-type-constructor type)))
  907. (system-image
  908. (image (inherit (os->image os))
  909. (size disk-size)))))
  910. (define (hurd-vm-port config base)
  911. "Return the forwarded vm port for this childhurd config."
  912. (let ((id (or (hurd-vm-configuration-id config) 0)))
  913. (+ base (* 1000 id))))
  914. (define %hurd-vm-secrets-port 11004)
  915. (define %hurd-vm-ssh-port 10022)
  916. (define %hurd-vm-vnc-port 15900)
  917. (define (hurd-vm-net-options config)
  918. `("--device" "rtl8139,netdev=net0"
  919. "--netdev"
  920. ,(string-append "user,id=net0"
  921. ",hostfwd=tcp:127.0.0.1:"
  922. (number->string (hurd-vm-port config %hurd-vm-secrets-port))
  923. "-:1004"
  924. ",hostfwd=tcp:127.0.0.1:"
  925. (number->string (hurd-vm-port config %hurd-vm-ssh-port))
  926. "-:2222"
  927. ",hostfwd=tcp:127.0.0.1:"
  928. (number->string (hurd-vm-port config %hurd-vm-vnc-port))
  929. "-:5900")))
  930. (define (hurd-vm-shepherd-service config)
  931. "Return a <shepherd-service> for a Hurd in a Virtual Machine with CONFIG."
  932. (let ((image (hurd-vm-configuration-image config))
  933. (qemu (hurd-vm-configuration-qemu config))
  934. (memory-size (hurd-vm-configuration-memory-size config))
  935. (options (hurd-vm-configuration-options config))
  936. (id (hurd-vm-configuration-id config))
  937. (net-options (hurd-vm-configuration-net-options config))
  938. (provisions '(hurd-vm childhurd)))
  939. (define vm-command
  940. #~(append (list #$(file-append qemu "/bin/qemu-system-i386")
  941. "-m" (number->string #$memory-size)
  942. #$@net-options
  943. #$@options
  944. "--hda" #+image
  945. ;; Cause the service to be respawned if the guest
  946. ;; reboots (it can reboot for instance if it did not
  947. ;; receive valid secrets, or if it crashed.)
  948. "--no-reboot")
  949. (if (file-exists? "/dev/kvm")
  950. '("--enable-kvm")
  951. '())))
  952. (list
  953. (shepherd-service
  954. (documentation "Run the Hurd in a Virtual Machine: a Childhurd.")
  955. (provision (if id
  956. (map
  957. (cute symbol-append <>
  958. (string->symbol (number->string id)))
  959. provisions)
  960. provisions))
  961. (requirement '(loopback networking user-processes))
  962. (start
  963. (with-imported-modules
  964. (source-module-closure '((gnu build secret-service)
  965. (guix build utils)))
  966. #~(lambda ()
  967. (let ((pid (fork+exec-command #$vm-command
  968. #:user "childhurd"
  969. ;; XXX TODO: use "childhurd" after
  970. ;; updating Shepherd
  971. #:group "kvm"
  972. #:environment-variables
  973. ;; QEMU tries to write to /var/tmp
  974. ;; by default.
  975. '("TMPDIR=/tmp")))
  976. (port #$(hurd-vm-port config %hurd-vm-secrets-port))
  977. (root #$(hurd-vm-configuration-secret-root config)))
  978. (catch #t
  979. (lambda _
  980. ;; XXX: 'secret-service-send-secrets' won't complete until
  981. ;; the guest has booted and its secret service server is
  982. ;; running, which could take 20+ seconds during which PID 1
  983. ;; is stuck waiting.
  984. (if (secret-service-send-secrets port root)
  985. pid
  986. (begin
  987. (kill (- pid) SIGTERM)
  988. #f)))
  989. (lambda (key . args)
  990. (kill (- pid) SIGTERM)
  991. (apply throw key args)))))))
  992. (modules `((gnu build secret-service)
  993. (guix build utils)
  994. ,@%default-modules))
  995. (stop #~(make-kill-destructor))))))
  996. (define %hurd-vm-accounts
  997. (list (user-group (name "childhurd") (system? #t))
  998. (user-account
  999. (name "childhurd")
  1000. (group "childhurd")
  1001. (supplementary-groups '("kvm"))
  1002. (comment "Privilege separation user for the childhurd")
  1003. (home-directory "/var/empty")
  1004. (shell (file-append shadow "/sbin/nologin"))
  1005. (system? #t))))
  1006. (define (initialize-hurd-vm-substitutes)
  1007. "Initialize the Hurd VM's key pair and ACL and store it on the host."
  1008. (define run
  1009. (with-imported-modules '((guix build utils))
  1010. #~(begin
  1011. (use-modules (guix build utils)
  1012. (ice-9 match))
  1013. (define host-key
  1014. "/etc/guix/signing-key.pub")
  1015. (define host-acl
  1016. "/etc/guix/acl")
  1017. (match (command-line)
  1018. ((_ guest-config-directory)
  1019. (setenv "GUIX_CONFIGURATION_DIRECTORY"
  1020. guest-config-directory)
  1021. (invoke #+(file-append guix "/bin/guix") "archive"
  1022. "--generate-key")
  1023. (when (file-exists? host-acl)
  1024. ;; Copy the host ACL.
  1025. (copy-file host-acl
  1026. (string-append guest-config-directory
  1027. "/acl")))
  1028. (when (file-exists? host-key)
  1029. ;; Add the host key to the childhurd's ACL.
  1030. (let ((key (open-fdes host-key O_RDONLY)))
  1031. (close-fdes 0)
  1032. (dup2 key 0)
  1033. (execl #+(file-append guix "/bin/guix")
  1034. "guix" "archive" "--authorize"))))))))
  1035. (program-file "initialize-hurd-vm-substitutes" run))
  1036. (define (hurd-vm-activation config)
  1037. "Return a gexp to activate the Hurd VM according to CONFIG."
  1038. (with-imported-modules '((guix build utils))
  1039. #~(begin
  1040. (use-modules (guix build utils))
  1041. (define secret-directory
  1042. #$(hurd-vm-configuration-secret-root config))
  1043. (define ssh-directory
  1044. (string-append secret-directory "/etc/ssh"))
  1045. (define guix-directory
  1046. (string-append secret-directory "/etc/guix"))
  1047. (unless (file-exists? ssh-directory)
  1048. ;; Generate SSH host keys under SSH-DIRECTORY.
  1049. (mkdir-p ssh-directory)
  1050. (invoke #$(file-append openssh "/bin/ssh-keygen")
  1051. "-A" "-f" secret-directory))
  1052. (unless (file-exists? guix-directory)
  1053. (invoke #$(initialize-hurd-vm-substitutes)
  1054. guix-directory)))))
  1055. (define hurd-vm-service-type
  1056. (service-type
  1057. (name 'hurd-vm)
  1058. (extensions (list (service-extension shepherd-root-service-type
  1059. hurd-vm-shepherd-service)
  1060. (service-extension account-service-type
  1061. (const %hurd-vm-accounts))
  1062. (service-extension activation-service-type
  1063. hurd-vm-activation)))
  1064. (default-value (hurd-vm-configuration))
  1065. (description
  1066. "Provide a virtual machine (VM) running GNU/Hurd, also known as a
  1067. @dfn{childhurd}.")))