2014-04-14 15:26:48 +01:00
|
|
|
{ config, lib, pkgs, ... }:
|
2011-02-25 15:07:52 +00:00
|
|
|
|
2014-04-14 15:26:48 +01:00
|
|
|
with lib;
|
2011-02-25 15:07:52 +00:00
|
|
|
|
2011-09-14 19:20:50 +01:00
|
|
|
let
|
2011-02-25 15:07:52 +00:00
|
|
|
|
2011-09-14 19:20:50 +01:00
|
|
|
cfg = config.virtualisation.libvirtd;
|
2014-06-12 14:16:38 +01:00
|
|
|
vswitch = config.virtualisation.vswitch;
|
2013-08-15 20:50:16 +01:00
|
|
|
configFile = pkgs.writeText "libvirtd.conf" ''
|
2020-04-15 17:16:13 +01:00
|
|
|
auth_unix_ro = "polkit"
|
|
|
|
auth_unix_rw = "polkit"
|
2013-08-15 20:50:16 +01:00
|
|
|
${cfg.extraConfig}
|
|
|
|
'';
|
2017-02-21 12:26:23 +00:00
|
|
|
qemuConfigFile = pkgs.writeText "qemu.conf" ''
|
2021-10-17 22:47:36 +01:00
|
|
|
${optionalString cfg.qemu.ovmf.enable ''
|
2022-05-14 13:00:00 +01:00
|
|
|
nvram = [ "/run/libvirt/nix-ovmf/AAVMF_CODE.fd:/run/libvirt/nix-ovmf/AAVMF_VARS.fd", "/run/libvirt/nix-ovmf/OVMF_CODE.fd:/run/libvirt/nix-ovmf/OVMF_VARS.fd" ]
|
2017-05-01 16:21:26 +01:00
|
|
|
''}
|
2021-10-17 22:47:36 +01:00
|
|
|
${optionalString (!cfg.qemu.runAsRoot) ''
|
2018-03-17 23:22:53 +00:00
|
|
|
user = "qemu-libvirtd"
|
|
|
|
group = "qemu-libvirtd"
|
|
|
|
''}
|
2021-10-17 22:47:36 +01:00
|
|
|
${cfg.qemu.verbatimConfig}
|
2017-02-21 12:26:23 +00:00
|
|
|
'';
|
2019-01-02 13:35:58 +00:00
|
|
|
dirName = "libvirt";
|
|
|
|
subDirs = list: [ dirName ] ++ map (e: "${dirName}/${e}") list;
|
2011-02-25 15:07:52 +00:00
|
|
|
|
2021-10-17 22:47:36 +01:00
|
|
|
ovmfModule = types.submodule {
|
|
|
|
options = {
|
|
|
|
enable = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = true;
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc ''
|
2021-10-17 22:47:36 +01:00
|
|
|
Allows libvirtd to take advantage of OVMF when creating new
|
|
|
|
QEMU VMs with UEFI boot.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2022-05-14 13:00:00 +01:00
|
|
|
# mkRemovedOptionModule does not work in submodules, do it manually
|
2021-10-17 22:47:36 +01:00
|
|
|
package = mkOption {
|
2022-05-14 13:00:00 +01:00
|
|
|
type = types.nullOr types.package;
|
|
|
|
default = null;
|
|
|
|
internal = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
packages = mkOption {
|
|
|
|
type = types.listOf types.package;
|
|
|
|
default = [ pkgs.OVMF.fd ];
|
|
|
|
defaultText = literalExpression "[ pkgs.OVMF.fd ]";
|
|
|
|
example = literalExpression "[ pkgs.OVMFFull.fd pkgs.pkgsCross.aarch64-multiplatform.OVMF.fd ]";
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc ''
|
2022-05-14 13:00:00 +01:00
|
|
|
List of OVMF packages to use. Each listed package must contain files names FV/OVMF_CODE.fd and FV/OVMF_VARS.fd or FV/AAVMF_CODE.fd and FV/AAVMF_VARS.fd
|
2021-10-17 22:47:36 +01:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
swtpmModule = types.submodule {
|
|
|
|
options = {
|
|
|
|
enable = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc ''
|
2021-10-17 22:47:36 +01:00
|
|
|
Allows libvirtd to use swtpm to create an emulated TPM.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
package = mkOption {
|
|
|
|
type = types.package;
|
|
|
|
default = pkgs.swtpm;
|
|
|
|
defaultText = literalExpression "pkgs.swtpm";
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc ''
|
2021-10-17 22:47:36 +01:00
|
|
|
swtpm package to use.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
qemuModule = types.submodule {
|
|
|
|
options = {
|
|
|
|
package = mkOption {
|
|
|
|
type = types.package;
|
|
|
|
default = pkgs.qemu;
|
|
|
|
defaultText = literalExpression "pkgs.qemu";
|
2022-08-14 08:03:30 +01:00
|
|
|
description = lib.mdDoc ''
|
2021-10-17 22:47:36 +01:00
|
|
|
Qemu package to use with libvirt.
|
|
|
|
`pkgs.qemu` can emulate alien architectures (e.g. aarch64 on x86)
|
|
|
|
`pkgs.qemu_kvm` saves disk space allowing to emulate only host architectures.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
runAsRoot = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = true;
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc ''
|
2021-10-17 22:47:36 +01:00
|
|
|
If true, libvirtd runs qemu as root.
|
|
|
|
If false, libvirtd runs qemu as unprivileged user qemu-libvirtd.
|
|
|
|
Changing this option to false may cause file permission issues
|
|
|
|
for existing guests. To fix these, manually change ownership
|
|
|
|
of affected files in /var/lib/libvirt/qemu to qemu-libvirtd.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
verbatimConfig = mkOption {
|
|
|
|
type = types.lines;
|
|
|
|
default = ''
|
|
|
|
namespaces = []
|
|
|
|
'';
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc ''
|
2021-10-17 22:47:36 +01:00
|
|
|
Contents written to the qemu configuration file, qemu.conf.
|
|
|
|
Make sure to include a proper namespace configuration when
|
|
|
|
supplying custom configuration.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
ovmf = mkOption {
|
|
|
|
type = ovmfModule;
|
|
|
|
default = { };
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc ''
|
2021-10-17 22:47:36 +01:00
|
|
|
QEMU's OVMF options.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
swtpm = mkOption {
|
|
|
|
type = swtpmModule;
|
|
|
|
default = { };
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc ''
|
2021-10-17 22:47:36 +01:00
|
|
|
QEMU's swtpm options.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
in
|
|
|
|
{
|
2011-02-25 15:07:52 +00:00
|
|
|
|
2019-12-10 01:51:19 +00:00
|
|
|
imports = [
|
|
|
|
(mkRemovedOptionModule [ "virtualisation" "libvirtd" "enableKVM" ]
|
2021-10-17 22:47:36 +01:00
|
|
|
"Set the option `virtualisation.libvirtd.qemu.package' instead.")
|
|
|
|
(mkRenamedOptionModule
|
|
|
|
[ "virtualisation" "libvirtd" "qemuPackage" ]
|
|
|
|
[ "virtualisation" "libvirtd" "qemu" "package" ])
|
|
|
|
(mkRenamedOptionModule
|
|
|
|
[ "virtualisation" "libvirtd" "qemuRunAsRoot" ]
|
|
|
|
[ "virtualisation" "libvirtd" "qemu" "runAsRoot" ])
|
|
|
|
(mkRenamedOptionModule
|
|
|
|
[ "virtualisation" "libvirtd" "qemuVerbatimConfig" ]
|
|
|
|
[ "virtualisation" "libvirtd" "qemu" "verbatimConfig" ])
|
|
|
|
(mkRenamedOptionModule
|
|
|
|
[ "virtualisation" "libvirtd" "qemuOvmf" ]
|
|
|
|
[ "virtualisation" "libvirtd" "qemu" "ovmf" "enable" ])
|
2022-05-14 13:00:00 +01:00
|
|
|
(mkRemovedOptionModule
|
2021-10-17 22:47:36 +01:00
|
|
|
[ "virtualisation" "libvirtd" "qemuOvmfPackage" ]
|
2022-05-14 13:00:00 +01:00
|
|
|
"If this option was set to `foo`, set the option `virtualisation.libvirtd.qemu.ovmf.packages' to `[foo.fd]` instead.")
|
2021-10-17 22:47:36 +01:00
|
|
|
(mkRenamedOptionModule
|
|
|
|
[ "virtualisation" "libvirtd" "qemuSwtpm" ]
|
|
|
|
[ "virtualisation" "libvirtd" "qemu" "swtpm" "enable" ])
|
2019-12-10 01:51:19 +00:00
|
|
|
];
|
|
|
|
|
2011-02-25 15:07:52 +00:00
|
|
|
###### interface
|
|
|
|
|
2019-01-02 13:35:58 +00:00
|
|
|
options.virtualisation.libvirtd = {
|
2011-02-25 15:07:52 +00:00
|
|
|
|
2019-01-02 13:35:58 +00:00
|
|
|
enable = mkOption {
|
2016-06-27 12:10:05 +01:00
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc ''
|
2016-06-27 12:10:05 +01:00
|
|
|
This option enables libvirtd, a daemon that manages
|
|
|
|
virtual machines. Users in the "libvirtd" group can interact with
|
|
|
|
the daemon (e.g. to start or stop VMs) using the
|
2022-07-28 22:19:15 +01:00
|
|
|
{command}`virsh` command line tool, among others.
|
2016-06-27 12:10:05 +01:00
|
|
|
'';
|
|
|
|
};
|
2011-02-25 15:07:52 +00:00
|
|
|
|
2021-03-10 00:31:21 +00:00
|
|
|
package = mkOption {
|
|
|
|
type = types.package;
|
|
|
|
default = pkgs.libvirt;
|
2021-10-03 17:06:03 +01:00
|
|
|
defaultText = literalExpression "pkgs.libvirt";
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc ''
|
2021-03-10 00:31:21 +00:00
|
|
|
libvirt package to use.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2019-01-02 13:35:58 +00:00
|
|
|
extraConfig = mkOption {
|
2016-06-27 12:10:05 +01:00
|
|
|
type = types.lines;
|
|
|
|
default = "";
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc ''
|
2016-06-27 12:10:05 +01:00
|
|
|
Extra contents appended to the libvirtd configuration file,
|
|
|
|
libvirtd.conf.
|
|
|
|
'';
|
|
|
|
};
|
2015-05-20 08:51:42 +01:00
|
|
|
|
2019-01-02 13:35:58 +00:00
|
|
|
extraOptions = mkOption {
|
2016-06-27 12:10:05 +01:00
|
|
|
type = types.listOf types.str;
|
|
|
|
default = [ ];
|
|
|
|
example = [ "--verbose" ];
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc ''
|
2016-06-27 12:10:05 +01:00
|
|
|
Extra command line arguments passed to libvirtd on startup.
|
|
|
|
'';
|
|
|
|
};
|
2014-06-16 15:45:08 +01:00
|
|
|
|
2019-08-23 16:47:09 +01:00
|
|
|
onBoot = mkOption {
|
2021-10-17 22:47:36 +01:00
|
|
|
type = types.enum [ "start" "ignore" ];
|
2019-08-23 16:47:09 +01:00
|
|
|
default = "start";
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc ''
|
2019-08-23 16:47:09 +01:00
|
|
|
Specifies the action to be done to / on the guests when the host boots.
|
|
|
|
The "start" option starts all guests that were running prior to shutdown
|
|
|
|
regardless of their autostart settings. The "ignore" option will not
|
2020-05-31 07:38:09 +01:00
|
|
|
start the formerly running guest on boot. However, any guest marked as
|
2019-08-23 16:47:09 +01:00
|
|
|
autostart will still be automatically started by libvirtd.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2019-01-02 13:35:58 +00:00
|
|
|
onShutdown = mkOption {
|
2021-10-17 22:47:36 +01:00
|
|
|
type = types.enum [ "shutdown" "suspend" ];
|
2016-06-27 12:10:05 +01:00
|
|
|
default = "suspend";
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc ''
|
2016-06-27 12:10:05 +01:00
|
|
|
When shutting down / restarting the host what method should
|
|
|
|
be used to gracefully halt the guests. Setting to "shutdown"
|
|
|
|
will cause an ACPI shutdown of each guest. "suspend" will
|
|
|
|
attempt to save the state of the guests ready to restore on boot.
|
|
|
|
'';
|
|
|
|
};
|
2014-06-16 15:45:08 +01:00
|
|
|
|
2022-12-24 17:45:04 +00:00
|
|
|
parallelShutdown = mkOption {
|
|
|
|
type = types.ints.unsigned;
|
|
|
|
default = 0;
|
|
|
|
description = lib.mdDoc ''
|
|
|
|
Number of guests that will be shutdown concurrently, taking effect when onShutdown
|
|
|
|
is set to "shutdown". If set to 0, guests will be shutdown one after another.
|
|
|
|
Number of guests on shutdown at any time will not exceed number set in this
|
|
|
|
variable.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2019-01-02 13:35:58 +00:00
|
|
|
allowedBridges = mkOption {
|
|
|
|
type = types.listOf types.str;
|
|
|
|
default = [ "virbr0" ];
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc ''
|
2019-01-02 13:35:58 +00:00
|
|
|
List of bridge devices that can be used by qemu:///session
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2021-10-17 22:47:36 +01:00
|
|
|
qemu = mkOption {
|
|
|
|
type = qemuModule;
|
|
|
|
default = { };
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc ''
|
2021-10-17 22:47:36 +01:00
|
|
|
QEMU related options.
|
|
|
|
'';
|
|
|
|
};
|
2011-02-25 15:07:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
###### implementation
|
|
|
|
|
|
|
|
config = mkIf cfg.enable {
|
|
|
|
|
2020-08-07 16:47:27 +01:00
|
|
|
assertions = [
|
|
|
|
{
|
2022-05-14 13:00:00 +01:00
|
|
|
assertion = config.virtualisation.libvirtd.qemu.ovmf.package == null;
|
|
|
|
message = ''
|
|
|
|
The option virtualisation.libvirtd.qemu.ovmf.package is superseded by virtualisation.libvirtd.qemu.ovmf.packages.
|
|
|
|
If this option was set to `foo`, set the option `virtualisation.libvirtd.qemu.ovmf.packages' to `[foo.fd]` instead.
|
|
|
|
'';
|
2020-08-07 16:47:27 +01:00
|
|
|
}
|
2021-10-09 16:26:18 +01:00
|
|
|
{
|
2022-05-14 13:00:00 +01:00
|
|
|
assertion = config.security.polkit.enable;
|
|
|
|
message = "The libvirtd module currently requires Polkit to be enabled ('security.polkit.enable = true').";
|
2021-10-09 16:26:18 +01:00
|
|
|
}
|
2020-08-07 16:47:27 +01:00
|
|
|
];
|
|
|
|
|
2019-01-02 13:35:58 +00:00
|
|
|
environment = {
|
|
|
|
# this file is expected in /etc/qemu and not sysconfdir (/var/lib)
|
2021-10-17 22:47:36 +01:00
|
|
|
etc."qemu/bridge.conf".text = lib.concatMapStringsSep "\n"
|
|
|
|
(e:
|
|
|
|
"allow ${e}")
|
|
|
|
cfg.allowedBridges;
|
|
|
|
systemPackages = with pkgs; [ libressl.nc iptables cfg.package cfg.qemu.package ];
|
2021-11-02 09:45:19 +00:00
|
|
|
etc.ethertypes.source = "${pkgs.iptables}/etc/ethertypes";
|
2019-01-02 13:35:58 +00:00
|
|
|
};
|
2011-02-25 15:07:52 +00:00
|
|
|
|
2011-03-15 10:52:44 +00:00
|
|
|
boot.kernelModules = [ "tun" ];
|
|
|
|
|
2018-06-30 00:58:35 +01:00
|
|
|
users.groups.libvirtd.gid = config.ids.gids.libvirtd;
|
2016-06-27 12:10:05 +01:00
|
|
|
|
2018-03-17 23:22:53 +00:00
|
|
|
# libvirtd runs qemu as this user and group by default
|
|
|
|
users.extraGroups.qemu-libvirtd.gid = config.ids.gids.qemu-libvirtd;
|
|
|
|
users.extraUsers.qemu-libvirtd = {
|
|
|
|
uid = config.ids.uids.qemu-libvirtd;
|
|
|
|
isNormalUser = false;
|
|
|
|
group = "qemu-libvirtd";
|
|
|
|
};
|
|
|
|
|
2019-01-02 13:35:58 +00:00
|
|
|
security.wrappers.qemu-bridge-helper = {
|
2021-09-12 17:53:48 +01:00
|
|
|
setuid = true;
|
|
|
|
owner = "root";
|
|
|
|
group = "root";
|
2022-08-18 04:04:01 +01:00
|
|
|
source = "${cfg.qemu.package}/libexec/qemu-bridge-helper";
|
2019-01-02 13:35:58 +00:00
|
|
|
};
|
2016-06-27 12:10:05 +01:00
|
|
|
|
2021-03-10 00:31:21 +00:00
|
|
|
systemd.packages = [ cfg.package ];
|
2016-06-27 12:10:05 +01:00
|
|
|
|
2019-01-02 13:35:58 +00:00
|
|
|
systemd.services.libvirtd-config = {
|
|
|
|
description = "Libvirt Virtual Machine Management Daemon - configuration";
|
|
|
|
script = ''
|
2016-06-27 12:10:05 +01:00
|
|
|
# Copy default libvirt network config .xml files to /var/lib
|
|
|
|
# Files modified by the user will not be overwritten
|
2021-03-10 00:31:21 +00:00
|
|
|
for i in $(cd ${cfg.package}/var/lib && echo \
|
2022-08-13 00:16:14 +01:00
|
|
|
libvirt/qemu/networks/*.xml \
|
2016-06-27 12:10:05 +01:00
|
|
|
libvirt/nwfilter/*.xml );
|
|
|
|
do
|
|
|
|
mkdir -p /var/lib/$(dirname $i) -m 755
|
2021-03-10 00:31:21 +00:00
|
|
|
cp -npd ${cfg.package}/var/lib/$i /var/lib/$i
|
2016-06-27 12:10:05 +01:00
|
|
|
done
|
|
|
|
|
2017-02-21 12:26:23 +00:00
|
|
|
# Copy generated qemu config to libvirt directory
|
2019-01-02 13:35:58 +00:00
|
|
|
cp -f ${qemuConfigFile} /var/lib/${dirName}/qemu.conf
|
2017-02-21 12:26:23 +00:00
|
|
|
|
2017-08-02 02:18:57 +01:00
|
|
|
# stable (not GC'able as in /nix/store) paths for using in <emulator> section of xml configs
|
2021-10-17 22:47:36 +01:00
|
|
|
for emulator in ${cfg.package}/libexec/libvirt_lxc ${cfg.qemu.package}/bin/qemu-kvm ${cfg.qemu.package}/bin/qemu-system-*; do
|
2019-01-02 13:35:58 +00:00
|
|
|
ln -s --force "$emulator" /run/${dirName}/nix-emulators/
|
|
|
|
done
|
|
|
|
|
2022-08-18 04:04:01 +01:00
|
|
|
for helper in bin/qemu-pr-helper; do
|
2021-10-17 22:47:36 +01:00
|
|
|
ln -s --force ${cfg.qemu.package}/$helper /run/${dirName}/nix-helpers/
|
2017-12-06 04:33:45 +00:00
|
|
|
done
|
2017-08-02 02:18:57 +01:00
|
|
|
|
2022-05-14 13:00:00 +01:00
|
|
|
${optionalString cfg.qemu.ovmf.enable (let
|
|
|
|
ovmfpackage = pkgs.buildEnv {
|
|
|
|
name = "qemu-ovmf";
|
|
|
|
paths = cfg.qemu.ovmf.packages;
|
|
|
|
};
|
|
|
|
in
|
|
|
|
''
|
|
|
|
ln -s --force ${ovmfpackage}/FV/AAVMF_CODE.fd /run/${dirName}/nix-ovmf/
|
|
|
|
ln -s --force ${ovmfpackage}/FV/OVMF_CODE.fd /run/${dirName}/nix-ovmf/
|
|
|
|
ln -s --force ${ovmfpackage}/FV/AAVMF_VARS.fd /run/${dirName}/nix-ovmf/
|
|
|
|
ln -s --force ${ovmfpackage}/FV/OVMF_VARS.fd /run/${dirName}/nix-ovmf/
|
|
|
|
'')}
|
2017-08-02 02:18:57 +01:00
|
|
|
'';
|
2016-06-27 12:10:05 +01:00
|
|
|
|
2019-01-02 13:35:58 +00:00
|
|
|
serviceConfig = {
|
|
|
|
Type = "oneshot";
|
|
|
|
RuntimeDirectoryPreserve = "yes";
|
|
|
|
LogsDirectory = subDirs [ "qemu" ];
|
|
|
|
RuntimeDirectory = subDirs [ "nix-emulators" "nix-helpers" "nix-ovmf" ];
|
|
|
|
StateDirectory = subDirs [ "dnsmasq" ];
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
systemd.services.libvirtd = {
|
2022-11-11 00:10:37 +00:00
|
|
|
wantedBy = [ "multi-user.target" ];
|
2019-01-02 13:35:58 +00:00
|
|
|
requires = [ "libvirtd-config.service" ];
|
2021-03-01 12:50:29 +00:00
|
|
|
after = [ "libvirtd-config.service" ]
|
2021-10-17 22:47:36 +01:00
|
|
|
++ optional vswitch.enable "ovs-vswitchd.service";
|
2019-01-02 13:35:58 +00:00
|
|
|
|
2020-03-30 10:45:28 +01:00
|
|
|
environment.LIBVIRTD_ARGS = escapeShellArgs (
|
2021-10-17 22:47:36 +01:00
|
|
|
[
|
|
|
|
"--config"
|
|
|
|
configFile
|
|
|
|
"--timeout"
|
|
|
|
"120" # from ${libvirt}/var/lib/sysconfig/libvirtd
|
|
|
|
] ++ cfg.extraOptions
|
|
|
|
);
|
|
|
|
|
|
|
|
path = [ cfg.qemu.package ] # libvirtd requires qemu-img to manage disk images
|
|
|
|
++ optional vswitch.enable vswitch.package
|
|
|
|
++ optional cfg.qemu.swtpm.enable cfg.qemu.swtpm.package;
|
2019-01-02 13:35:58 +00:00
|
|
|
|
2016-06-27 12:10:05 +01:00
|
|
|
serviceConfig = {
|
|
|
|
Type = "notify";
|
|
|
|
KillMode = "process"; # when stopping, leave the VMs alone
|
2017-08-10 12:34:32 +01:00
|
|
|
Restart = "no";
|
2011-02-25 15:07:52 +00:00
|
|
|
};
|
2017-08-10 12:34:32 +01:00
|
|
|
restartIfChanged = false;
|
2016-06-27 12:10:05 +01:00
|
|
|
};
|
2011-02-25 15:07:52 +00:00
|
|
|
|
2022-04-08 12:19:10 +01:00
|
|
|
systemd.services.virtchd = {
|
|
|
|
path = [ pkgs.cloud-hypervisor ];
|
|
|
|
};
|
|
|
|
|
2017-03-25 13:59:01 +00:00
|
|
|
systemd.services.libvirt-guests = {
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
2021-03-10 00:31:21 +00:00
|
|
|
path = with pkgs; [ coreutils gawk cfg.package ];
|
2017-05-29 17:03:07 +01:00
|
|
|
restartIfChanged = false;
|
2018-10-30 23:01:00 +00:00
|
|
|
|
2019-08-23 16:47:09 +01:00
|
|
|
environment.ON_BOOT = "${cfg.onBoot}";
|
2018-10-30 23:01:00 +00:00
|
|
|
environment.ON_SHUTDOWN = "${cfg.onShutdown}";
|
2022-12-24 17:45:04 +00:00
|
|
|
environment.PARALLEL_SHUTDOWN = "${toString cfg.parallelShutdown}";
|
2017-03-25 13:59:01 +00:00
|
|
|
};
|
|
|
|
|
2016-06-27 12:02:47 +01:00
|
|
|
systemd.sockets.virtlogd = {
|
|
|
|
description = "Virtual machine log manager socket";
|
|
|
|
wantedBy = [ "sockets.target" ];
|
2019-01-02 13:35:58 +00:00
|
|
|
listenStreams = [ "/run/${dirName}/virtlogd-sock" ];
|
2016-06-27 12:02:47 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
systemd.services.virtlogd = {
|
|
|
|
description = "Virtual machine log manager";
|
2021-03-10 00:31:21 +00:00
|
|
|
serviceConfig.ExecStart = "@${cfg.package}/sbin/virtlogd virtlogd";
|
2017-08-10 12:34:32 +01:00
|
|
|
restartIfChanged = false;
|
2016-06-27 12:02:47 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
systemd.sockets.virtlockd = {
|
|
|
|
description = "Virtual machine lock manager socket";
|
|
|
|
wantedBy = [ "sockets.target" ];
|
2019-01-02 13:35:58 +00:00
|
|
|
listenStreams = [ "/run/${dirName}/virtlockd-sock" ];
|
2016-06-27 12:02:47 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
systemd.services.virtlockd = {
|
|
|
|
description = "Virtual machine lock manager";
|
2021-03-10 00:31:21 +00:00
|
|
|
serviceConfig.ExecStart = "@${cfg.package}/sbin/virtlockd virtlockd";
|
2017-08-10 12:34:32 +01:00
|
|
|
restartIfChanged = false;
|
2016-06-27 12:02:47 +01:00
|
|
|
};
|
2020-03-09 06:29:46 +00:00
|
|
|
|
2020-07-08 23:35:15 +01:00
|
|
|
# https://libvirt.org/daemons.html#monolithic-systemd-integration
|
|
|
|
systemd.sockets.libvirtd.wantedBy = [ "sockets.target" ];
|
2020-04-15 17:16:13 +01:00
|
|
|
|
|
|
|
security.polkit.extraConfig = ''
|
|
|
|
polkit.addRule(function(action, subject) {
|
|
|
|
if (action.id == "org.libvirt.unix.manage" &&
|
|
|
|
subject.isInGroup("libvirtd")) {
|
|
|
|
return polkit.Result.YES;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
'';
|
2011-02-25 15:07:52 +00:00
|
|
|
};
|
|
|
|
}
|