nixos/libvirt: Syntactic-only refactoring

(cherry picked from commit a0c0f63811)
This commit is contained in:
Rickard Nilsson 2016-06-27 13:10:05 +02:00
parent bb52e1449d
commit adc5760bb2

View File

@ -1,5 +1,3 @@
# Systemd services for libvirtd.
{ config, lib, pkgs, ... }: { config, lib, pkgs, ... }:
with lib; with lib;
@ -16,71 +14,59 @@ let
${cfg.extraConfig} ${cfg.extraConfig}
''; '';
in in {
{
###### interface ###### interface
options = { options = {
virtualisation.libvirtd.enable = virtualisation.libvirtd.enable = mkOption {
mkOption { type = types.bool;
type = types.bool; default = false;
default = false; description = ''
description = This option enables libvirtd, a daemon that manages
'' virtual machines. Users in the "libvirtd" group can interact with
This option enables libvirtd, a daemon that manages the daemon (e.g. to start or stop VMs) using the
virtual machines. Users in the "libvirtd" group can interact with <command>virsh</command> command line tool, among others.
the daemon (e.g. to start or stop VMs) using the '';
<command>virsh</command> command line tool, among others. };
'';
};
virtualisation.libvirtd.enableKVM = virtualisation.libvirtd.enableKVM = mkOption {
mkOption { type = types.bool;
type = types.bool; default = true;
default = true; description = ''
description = This option enables support for QEMU/KVM in libvirtd.
'' '';
This option enables support for QEMU/KVM in libvirtd. };
'';
};
virtualisation.libvirtd.extraConfig = virtualisation.libvirtd.extraConfig = mkOption {
mkOption { type = types.lines;
type = types.lines; default = "";
default = ""; description = ''
description = Extra contents appended to the libvirtd configuration file,
'' libvirtd.conf.
Extra contents appended to the libvirtd configuration file, '';
libvirtd.conf. };
'';
};
virtualisation.libvirtd.extraOptions = virtualisation.libvirtd.extraOptions = mkOption {
mkOption { type = types.listOf types.str;
type = types.listOf types.str; default = [ ];
default = [ ]; example = [ "--verbose" ];
example = [ "--verbose" ]; description = ''
description = Extra command line arguments passed to libvirtd on startup.
'' '';
Extra command line arguments passed to libvirtd on startup. };
'';
};
virtualisation.libvirtd.onShutdown =
mkOption {
type = types.enum ["shutdown" "suspend" ];
default = "suspend";
description =
''
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.
'';
};
virtualisation.libvirtd.onShutdown = mkOption {
type = types.enum ["shutdown" "suspend" ];
default = "suspend";
description = ''
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.
'';
};
}; };
@ -95,65 +81,66 @@ in
boot.kernelModules = [ "tun" ]; boot.kernelModules = [ "tun" ];
systemd.services.libvirtd = users.extraGroups.libvirtd.gid = config.ids.gids.libvirtd;
{ description = "Libvirt Virtual Machine Management Daemon";
wantedBy = [ "multi-user.target" ]; systemd.services.libvirtd = {
after = [ "systemd-udev-settle.service" ] description = "Libvirt Virtual Machine Management Daemon";
++ optional vswitch.enable "vswitchd.service";
path = [ wantedBy = [ "multi-user.target" ];
pkgs.bridge-utils after = [ "systemd-udev-settle.service" ]
pkgs.dmidecode ++ optional vswitch.enable "vswitchd.service";
pkgs.dnsmasq
pkgs.ebtables
]
++ optional cfg.enableKVM pkgs.qemu_kvm
++ optional vswitch.enable vswitch.package;
preStart = path = [
'' pkgs.bridge-utils
mkdir -p /var/log/libvirt/qemu -m 755 pkgs.dmidecode
rm -f /var/run/libvirtd.pid pkgs.dnsmasq
pkgs.ebtables
]
++ optional cfg.enableKVM pkgs.qemu_kvm
++ optional vswitch.enable vswitch.package;
mkdir -p /var/lib/libvirt preStart = ''
mkdir -p /var/lib/libvirt/dnsmasq mkdir -p /var/log/libvirt/qemu -m 755
rm -f /var/run/libvirtd.pid
chmod 755 /var/lib/libvirt mkdir -p /var/lib/libvirt
chmod 755 /var/lib/libvirt/dnsmasq mkdir -p /var/lib/libvirt/dnsmasq
# Copy default libvirt network config .xml files to /var/lib chmod 755 /var/lib/libvirt
# Files modified by the user will not be overwritten chmod 755 /var/lib/libvirt/dnsmasq
for i in $(cd ${pkgs.libvirt}/var/lib && echo \
libvirt/qemu/networks/*.xml libvirt/qemu/networks/autostart/*.xml \
libvirt/nwfilter/*.xml );
do
mkdir -p /var/lib/$(dirname $i) -m 755
cp -npd ${pkgs.libvirt}/var/lib/$i /var/lib/$i
done
# libvirtd puts the full path of the emulator binary in the machine # Copy default libvirt network config .xml files to /var/lib
# config file. But this path can unfortunately be garbage collected # Files modified by the user will not be overwritten
# while still being used by the virtual machine. So update the for i in $(cd ${pkgs.libvirt}/var/lib && echo \
# emulator path on each startup to something valid (re-scan $PATH). libvirt/qemu/networks/*.xml libvirt/qemu/networks/autostart/*.xml \
for file in /etc/libvirt/qemu/*.xml /etc/libvirt/lxc/*.xml; do libvirt/nwfilter/*.xml );
test -f "$file" || continue do
# get (old) emulator path from config file mkdir -p /var/lib/$(dirname $i) -m 755
emulator=$(grep "^[[:space:]]*<emulator>" "$file" | sed 's,^[[:space:]]*<emulator>\(.*\)</emulator>.*,\1,') cp -npd ${pkgs.libvirt}/var/lib/$i /var/lib/$i
# get a (definitely) working emulator path by re-scanning $PATH done
new_emulator=$(PATH=${pkgs.libvirt}/libexec:$PATH command -v $(basename "$emulator"))
# write back
sed -i "s,^[[:space:]]*<emulator>.*, <emulator>$new_emulator</emulator> <!-- WARNING: emulator dirname is auto-updated by the nixos libvirtd module -->," "$file"
done
''; # */
serviceConfig = { # libvirtd puts the full path of the emulator binary in the machine
ExecStart = ''@${pkgs.libvirt}/sbin/libvirtd libvirtd --config "${configFile}" --daemon ${concatStringsSep " " cfg.extraOptions}''; # config file. But this path can unfortunately be garbage collected
Type = "notify"; # while still being used by the virtual machine. So update the
KillMode = "process"; # when stopping, leave the VMs alone # emulator path on each startup to something valid (re-scan $PATH).
Restart = "on-failure"; for file in /etc/libvirt/qemu/*.xml /etc/libvirt/lxc/*.xml; do
}; test -f "$file" || continue
# get (old) emulator path from config file
emulator=$(grep "^[[:space:]]*<emulator>" "$file" | sed 's,^[[:space:]]*<emulator>\(.*\)</emulator>.*,\1,')
# get a (definitely) working emulator path by re-scanning $PATH
new_emulator=$(PATH=${pkgs.libvirt}/libexec:$PATH command -v $(basename "$emulator"))
# write back
sed -i "s,^[[:space:]]*<emulator>.*, <emulator>$new_emulator</emulator> <!-- WARNING: emulator dirname is auto-updated by the nixos libvirtd module -->," "$file"
done
''; # */
serviceConfig = {
ExecStart = ''@${pkgs.libvirt}/sbin/libvirtd libvirtd --config "${configFile}" --daemon ${concatStringsSep " " cfg.extraOptions}'';
Type = "notify";
KillMode = "process"; # when stopping, leave the VMs alone
Restart = "on-failure";
}; };
};
systemd.sockets.virtlogd = { systemd.sockets.virtlogd = {
description = "Virtual machine log manager socket"; description = "Virtual machine log manager socket";
@ -176,8 +163,5 @@ in
description = "Virtual machine lock manager"; description = "Virtual machine lock manager";
serviceConfig.ExecStart = "@${pkgs.libvirt}/sbin/virtlockd virtlockd"; serviceConfig.ExecStart = "@${pkgs.libvirt}/sbin/virtlockd virtlockd";
}; };
users.extraGroups.libvirtd.gid = config.ids.gids.libvirtd;
}; };
} }