virtualization.scm 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2017 Ryan Moe <ryan.moe@gmail.com>
  3. ;;;
  4. ;;; This file is part of GNU Guix.
  5. ;;;
  6. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  7. ;;; under the terms of the GNU General Public License as published by
  8. ;;; the Free Software Foundation; either version 3 of the License, or (at
  9. ;;; your option) any later version.
  10. ;;;
  11. ;;; GNU Guix is distributed in the hope that it will be useful, but
  12. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;;; GNU General Public License for more details.
  15. ;;;
  16. ;;; You should have received a copy of the GNU General Public License
  17. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  18. (define-module (gnu services virtualization)
  19. #:use-module (gnu services)
  20. #:use-module (gnu services configuration)
  21. #:use-module (gnu services base)
  22. #:use-module (gnu services dbus)
  23. #:use-module (gnu services shepherd)
  24. #:use-module (gnu system shadow)
  25. #:use-module (gnu packages admin)
  26. #:use-module (gnu packages virtualization)
  27. #:use-module (guix records)
  28. #:use-module (guix gexp)
  29. #:use-module (guix packages)
  30. #:use-module (ice-9 match)
  31. #:export (libvirt-configuration
  32. libvirt-service-type
  33. virtlog-service-type))
  34. (define (uglify-field-name field-name)
  35. (let ((str (symbol->string field-name)))
  36. (string-join
  37. (string-split (string-delete #\? str) #\-)
  38. "_")))
  39. (define (quote-val val)
  40. (string-append "\"" val "\""))
  41. (define (serialize-field field-name val)
  42. (format #t "~a = ~a\n" (uglify-field-name field-name) val))
  43. (define (serialize-string field-name val)
  44. (serialize-field field-name (quote-val val)))
  45. (define (serialize-boolean field-name val)
  46. (serialize-field field-name (if val 1 0)))
  47. (define (serialize-integer field-name val)
  48. (serialize-field field-name val))
  49. (define (build-opt-list val)
  50. (string-append
  51. "["
  52. (string-join (map quote-val val) ",")
  53. "]"))
  54. (define optional-list? list?)
  55. (define optional-string? string?)
  56. (define (serialize-list field-name val)
  57. (serialize-field field-name (build-opt-list val)))
  58. (define (serialize-optional-list field-name val)
  59. (if (null? val)
  60. (format #t "# ~a = []\n" (uglify-field-name field-name))
  61. (serialize-list field-name val)))
  62. (define (serialize-optional-string field-name val)
  63. (if (string-null? val)
  64. (format #t "# ~a = \"\"\n" (uglify-field-name field-name))
  65. (serialize-string field-name val)))
  66. (define-configuration libvirt-configuration
  67. (libvirt
  68. (package libvirt)
  69. "Libvirt package.")
  70. (listen-tls?
  71. (boolean #t)
  72. "Flag listening for secure TLS connections on the public TCP/IP port.
  73. must set @code{listen} for this to have any effect.
  74. It is necessary to setup a CA and issue server certificates before
  75. using this capability.")
  76. (listen-tcp?
  77. (boolean #f)
  78. "Listen for unencrypted TCP connections on the public TCP/IP port.
  79. must set @code{listen} for this to have any effect.
  80. Using the TCP socket requires SASL authentication by default. Only
  81. SASL mechanisms which support data encryption are allowed. This is
  82. DIGEST_MD5 and GSSAPI (Kerberos5)")
  83. (tls-port
  84. (string "16514")
  85. "Port for accepting secure TLS connections This can be a port number,
  86. or service name")
  87. (tcp-port
  88. (string "16509")
  89. "Port for accepting insecure TCP connections This can be a port number,
  90. or service name")
  91. (listen-addr
  92. (string "0.0.0.0")
  93. "IP address or hostname used for client connections.")
  94. (mdns-adv?
  95. (boolean #f)
  96. "Flag toggling mDNS advertisement of the libvirt service.
  97. Alternatively can disable for all services on a host by
  98. stopping the Avahi daemon.")
  99. (mdns-name
  100. (string (string-append "Virtualization Host " (gethostname)))
  101. "Default mDNS advertisement name. This must be unique on the
  102. immediate broadcast network.")
  103. (unix-sock-group
  104. (string "root")
  105. "UNIX domain socket group ownership. This can be used to
  106. allow a 'trusted' set of users access to management capabilities
  107. without becoming root.")
  108. (unix-sock-ro-perms
  109. (string "0777")
  110. "UNIX socket permissions for the R/O socket. This is used
  111. for monitoring VM status only.")
  112. (unix-sock-rw-perms
  113. (string "0770")
  114. "UNIX socket permissions for the R/W socket. Default allows
  115. only root. If PolicyKit is enabled on the socket, the default
  116. will change to allow everyone (eg, 0777)")
  117. (unix-sock-admin-perms
  118. (string "0777")
  119. "UNIX socket permissions for the admin socket. Default allows
  120. only owner (root), do not change it unless you are sure to whom
  121. you are exposing the access to.")
  122. (unix-sock-dir
  123. (string "/var/run/libvirt")
  124. "The directory in which sockets will be found/created.")
  125. (auth-unix-ro
  126. (string "polkit")
  127. "Authentication scheme for UNIX read-only sockets. By default
  128. socket permissions allow anyone to connect")
  129. (auth-unix-rw
  130. (string "polkit")
  131. "Authentication scheme for UNIX read-write sockets. By default
  132. socket permissions only allow root. If PolicyKit support was compiled
  133. into libvirt, the default will be to use 'polkit' auth.")
  134. (auth-tcp
  135. (string "sasl")
  136. "Authentication scheme for TCP sockets. If you don't enable SASL,
  137. then all TCP traffic is cleartext. Don't do this outside of a dev/test
  138. scenario.")
  139. (auth-tls
  140. (string "none")
  141. "Authentication scheme for TLS sockets. TLS sockets already have
  142. encryption provided by the TLS layer, and limited authentication is
  143. done by certificates.
  144. It is possible to make use of any SASL authentication mechanism as
  145. well, by using 'sasl' for this option")
  146. (access-drivers
  147. (optional-list '())
  148. "API access control scheme.
  149. By default an authenticated user is allowed access to all APIs. Access
  150. drivers can place restrictions on this.")
  151. (key-file
  152. (string "")
  153. "Server key file path. If set to an empty string, then no private key
  154. is loaded.")
  155. (cert-file
  156. (string "")
  157. "Server key file path. If set to an empty string, then no certificate
  158. is loaded.")
  159. (ca-file
  160. (string "")
  161. "Server key file path. If set to an empty string, then no CA certificate
  162. is loaded.")
  163. (crl-file
  164. (string "")
  165. "Certificate revocation list path. If set to an empty string, then no
  166. CRL is loaded.")
  167. (tls-no-sanity-cert
  168. (boolean #f)
  169. "Disable verification of our own server certificates.
  170. When libvirtd starts it performs some sanity checks against its own
  171. certificates.")
  172. (tls-no-verify-cert
  173. (boolean #f)
  174. "Disable verification of client certificates.
  175. Client certificate verification is the primary authentication mechanism.
  176. Any client which does not present a certificate signed by the CA
  177. will be rejected.")
  178. (tls-allowed-dn-list
  179. (optional-list '())
  180. "Whitelist of allowed x509 Distinguished Name.")
  181. (sasl-allowed-usernames
  182. (optional-list '())
  183. "Whitelist of allowed SASL usernames. The format for username
  184. depends on the SASL authentication mechanism.")
  185. (tls-priority
  186. (string "NORMAL")
  187. "Override the compile time default TLS priority string. The
  188. default is usually \"NORMAL\" unless overridden at build time.
  189. Only set this is it is desired for libvirt to deviate from
  190. the global default settings.")
  191. (max-clients
  192. (integer 5000)
  193. "Maximum number of concurrent client connections to allow
  194. over all sockets combined.")
  195. (max-queued-clients
  196. (integer 1000)
  197. "Maximum length of queue of connections waiting to be
  198. accepted by the daemon. Note, that some protocols supporting
  199. retransmission may obey this so that a later reattempt at
  200. connection succeeds.")
  201. (max-anonymous-clients
  202. (integer 20)
  203. "Maximum length of queue of accepted but not yet authenticated
  204. clients. Set this to zero to turn this feature off")
  205. (min-workers
  206. (integer 5)
  207. "Number of workers to start up initially.")
  208. (max-workers
  209. (integer 20)
  210. "Maximum number of worker threads.
  211. If the number of active clients exceeds @code{min-workers},
  212. then more threads are spawned, up to max_workers limit.
  213. Typically you'd want max_workers to equal maximum number
  214. of clients allowed.")
  215. (prio-workers
  216. (integer 5)
  217. "Number of priority workers. If all workers from above
  218. pool are stuck, some calls marked as high priority
  219. (notably domainDestroy) can be executed in this pool.")
  220. (max-requests
  221. (integer 20)
  222. "Total global limit on concurrent RPC calls.")
  223. (max-client-requests
  224. (integer 5)
  225. "Limit on concurrent requests from a single client
  226. connection. To avoid one client monopolizing the server
  227. this should be a small fraction of the global max_requests
  228. and max_workers parameter.")
  229. (admin-min-workers
  230. (integer 1)
  231. "Same as @code{min-workers} but for the admin interface.")
  232. (admin-max-workers
  233. (integer 5)
  234. "Same as @code{max-workers} but for the admin interface.")
  235. (admin-max-clients
  236. (integer 5)
  237. "Same as @code{max-clients} but for the admin interface.")
  238. (admin-max-queued-clients
  239. (integer 5)
  240. "Same as @code{max-queued-clients} but for the admin interface.")
  241. (admin-max-client-requests
  242. (integer 5)
  243. "Same as @code{max-client-requests} but for the admin interface.")
  244. (log-level
  245. (integer 3)
  246. "Logging level. 4 errors, 3 warnings, 2 information, 1 debug.")
  247. (log-filters
  248. (string "3:remote 4:event")
  249. "Logging filters.
  250. A filter allows to select a different logging level for a given category
  251. of logs
  252. The format for a filter is one of:
  253. @itemize
  254. @item x:name
  255. @item x:+name
  256. @end itemize
  257. where @code{name} is a string which is matched against the category
  258. given in the @code{VIR_LOG_INIT()} at the top of each libvirt source
  259. file, e.g., \"remote\", \"qemu\", or \"util.json\" (the name in the
  260. filter can be a substring of the full category name, in order
  261. to match multiple similar categories), the optional \"+\" prefix
  262. tells libvirt to log stack trace for each message matching
  263. name, and @code{x} is the minimal level where matching messages should
  264. be logged:
  265. @itemize
  266. @item 1: DEBUG
  267. @item 2: INFO
  268. @item 3: WARNING
  269. @item 4: ERROR
  270. @end itemize
  271. Multiple filters can be defined in a single filters statement, they just
  272. need to be separated by spaces.")
  273. (log-outputs
  274. (string "3:stderr")
  275. "Logging outputs.
  276. An output is one of the places to save logging information
  277. The format for an output can be:
  278. @table @code
  279. @item x:stderr
  280. output goes to stderr
  281. @item x:syslog:name
  282. use syslog for the output and use the given name as the ident
  283. @item x:file:file_path
  284. output to a file, with the given filepath
  285. @item x:journald
  286. output to journald logging system
  287. @end table
  288. In all case the x prefix is the minimal level, acting as a filter
  289. @itemize
  290. @item 1: DEBUG
  291. @item 2: INFO
  292. @item 3: WARNING
  293. @item 4: ERROR
  294. @end itemize
  295. Multiple outputs can be defined, they just need to be separated by spaces.")
  296. (audit-level
  297. (integer 1)
  298. "Allows usage of the auditing subsystem to be altered
  299. @itemize
  300. @item 0: disable all auditing
  301. @item 1: enable auditing, only if enabled on host
  302. @item 2: enable auditing, and exit if disabled on host.
  303. @end itemize
  304. ")
  305. (audit-logging
  306. (boolean #f)
  307. "Send audit messages via libvirt logging infrastructure.")
  308. (host-uuid
  309. (optional-string "")
  310. "Host UUID. UUID must not have all digits be the same.")
  311. (host-uuid-source
  312. (string "smbios")
  313. "Source to read host UUID.
  314. @itemize
  315. @item @code{smbios}: fetch the UUID from @code{dmidecode -s system-uuid}
  316. @item @code{machine-id}: fetch the UUID from @code{/etc/machine-id}
  317. @end itemize
  318. If @code{dmidecode} does not provide a valid UUID a temporary UUID
  319. will be generated.")
  320. (keepalive-interval
  321. (integer 5)
  322. "A keepalive message is sent to a client after
  323. @code{keepalive_interval} seconds of inactivity to check if
  324. the client is still responding. If set to -1, libvirtd will
  325. never send keepalive requests; however clients can still send
  326. them and the daemon will send responses.")
  327. (keepalive-count
  328. (integer 5)
  329. "Maximum number of keepalive messages that are allowed to be sent
  330. to the client without getting any response before the connection is
  331. considered broken.
  332. In other words, the connection is automatically
  333. closed approximately after
  334. @code{keepalive_interval * (keepalive_count + 1)} seconds since the last
  335. message received from the client. When @code{keepalive-count} is
  336. set to 0, connections will be automatically closed after
  337. @code{keepalive-interval} seconds of inactivity without sending any
  338. keepalive messages.")
  339. (admin-keepalive-interval
  340. (integer 5)
  341. "Same as above but for admin interface.")
  342. (admin-keepalive-count
  343. (integer 5)
  344. "Same as above but for admin interface.")
  345. (ovs-timeout
  346. (integer 5)
  347. "Timeout for Open vSwitch calls.
  348. The @code{ovs-vsctl} utility is used for the configuration and
  349. its timeout option is set by default to 5 seconds to avoid
  350. potential infinite waits blocking libvirt."))
  351. (define* (libvirt-conf-file config)
  352. "Return a libvirtd config file."
  353. (plain-file "libvirtd.conf"
  354. (with-output-to-string
  355. (lambda ()
  356. (serialize-configuration config libvirt-configuration-fields)))))
  357. (define %libvirt-accounts
  358. (list (user-group (name "libvirt") (system? #t))))
  359. (define (%libvirt-activation config)
  360. (let ((sock-dir (libvirt-configuration-unix-sock-dir config)))
  361. #~(begin
  362. (use-modules (guix build utils))
  363. (mkdir-p #$sock-dir))))
  364. (define (libvirt-shepherd-service config)
  365. (let* ((config-file (libvirt-conf-file config))
  366. (libvirt (libvirt-configuration-libvirt config)))
  367. (list (shepherd-service
  368. (documentation "Run the libvirt daemon.")
  369. (provision '(libvirtd))
  370. (start #~(make-forkexec-constructor
  371. (list (string-append #$libvirt "/sbin/libvirtd")
  372. "-f" #$config-file)))
  373. (stop #~(make-kill-destructor))))))
  374. (define libvirt-service-type
  375. (service-type (name 'libvirt)
  376. (extensions
  377. (list
  378. (service-extension polkit-service-type
  379. (compose list libvirt-configuration-libvirt))
  380. (service-extension profile-service-type
  381. (compose list
  382. libvirt-configuration-libvirt))
  383. (service-extension activation-service-type
  384. %libvirt-activation)
  385. (service-extension shepherd-root-service-type
  386. libvirt-shepherd-service)
  387. (service-extension account-service-type
  388. (const %libvirt-accounts))))
  389. (default-value (libvirt-configuration))))
  390. (define-record-type* <virtlog-configuration>
  391. virtlog-configuration make-virtlog-configuration
  392. virtlog-configuration?
  393. (libvirt virtlog-configuration-libvirt
  394. (default libvirt))
  395. (log-level virtlog-configuration-log-level
  396. (default 3))
  397. (log-filters virtlog-configuration-log-filters
  398. (default "3:remote 4:event"))
  399. (log-outputs virtlog-configuration-log-outputs
  400. (default "3:syslog:virtlogd"))
  401. (max-clients virtlog-configuration-max-clients
  402. (default 1024))
  403. (max-size virtlog-configuration-max-size
  404. (default 2097152)) ;; 2MB
  405. (max-backups virtlog-configuration-max-backups
  406. (default 3)))
  407. (define* (virtlogd-conf-file config)
  408. "Return a virtlogd config file."
  409. (plain-file "virtlogd.conf"
  410. (string-append
  411. "log_level = " (number->string (virtlog-configuration-log-level config)) "\n"
  412. "log_filters = \"" (virtlog-configuration-log-filters config) "\"\n"
  413. "log_outputs = \"" (virtlog-configuration-log-outputs config) "\"\n"
  414. "max_clients = " (number->string (virtlog-configuration-max-clients config)) "\n"
  415. "max_size = " (number->string (virtlog-configuration-max-size config)) "\n"
  416. "max_backups = " (number->string (virtlog-configuration-max-backups config)) "\n")))
  417. (define (virtlogd-shepherd-service config)
  418. (let* ((config-file (virtlogd-conf-file config))
  419. (libvirt (virtlog-configuration-libvirt config)))
  420. (list (shepherd-service
  421. (documentation "Run the virtlog daemon.")
  422. (provision '(virtlogd))
  423. (start #~(make-forkexec-constructor
  424. (list (string-append #$libvirt "/sbin/virtlogd")
  425. "-f" #$config-file)))
  426. (stop #~(make-kill-destructor))))))
  427. (define virtlog-service-type
  428. (service-type (name 'virtlogd)
  429. (extensions
  430. (list
  431. (service-extension shepherd-root-service-type
  432. virtlogd-shepherd-service)))
  433. (default-value (virtlog-configuration))))
  434. (define (generate-libvirt-documentation)
  435. (generate-documentation
  436. `((libvirt-configuration ,libvirt-configuration-fields))
  437. 'libvirt-configuration))