nixos/libvirtd: add support for nixos managed libvirt hooks Libvirt support calling user defined hooks on certains events. Documentation can be found https://libvirt.org/hooks.html. This commit allow specifying these hooks via the virtualisation.libvirtd.hooks.<name>.* options

Changed files
+78
nixos
modules
virtualisation
tests
+71
nixos/modules/virtualisation/libvirtd.nix
···
};
};
};
+
+
hooksModule = types.submodule {
+
options = {
+
daemon = mkOption {
+
type = types.attrsOf types.path;
+
default = { };
+
description = lib.mdDoc ''
+
Hooks that will be placed under /var/lib/libvirt/hooks/daemon.d/
+
and called for daemon start/shutdown/SIGHUP events.
+
Please see https://libvirt.org/hooks.html for documentation.
+
'';
+
};
+
+
qemu = mkOption {
+
type = types.attrsOf types.path;
+
default = { };
+
description = lib.mdDoc ''
+
Hooks that will be placed under /var/lib/libvirt/hooks/qemu.d/
+
and called for qemu domains begin/end/migrate events.
+
Please see https://libvirt.org/hooks.html for documentation.
+
'';
+
};
+
+
lxc = mkOption {
+
type = types.attrsOf types.path;
+
default = { };
+
description = lib.mdDoc ''
+
Hooks that will be placed under /var/lib/libvirt/hooks/lxc.d/
+
and called for lxc domains begin/end events.
+
Please see https://libvirt.org/hooks.html for documentation.
+
'';
+
};
+
+
libxl = mkOption {
+
type = types.attrsOf types.path;
+
default = { };
+
description = lib.mdDoc ''
+
Hooks that will be placed under /var/lib/libvirt/hooks/libxl.d/
+
and called for libxl-handled xen domains begin/end events.
+
Please see https://libvirt.org/hooks.html for documentation.
+
'';
+
};
+
+
network = mkOption {
+
type = types.attrsOf types.path;
+
default = { };
+
description = lib.mdDoc ''
+
Hooks that will be placed under /var/lib/libvirt/hooks/lxc.d/
+
and called for networks begin/end events.
+
Please see https://libvirt.org/hooks.html for documentation.
+
'';
+
};
+
};
+
};
in
{
···
QEMU related options.
'';
};
+
+
hooks = mkOption {
+
type = hooksModule;
+
default = { };
+
description = lib.mdDoc ''
+
Hooks related options.
+
'';
+
};
};
···
ln -s --force ${ovmfpackage}/FV/AAVMF_VARS.fd /run/${dirName}/nix-ovmf/
ln -s --force ${ovmfpackage}/FV/OVMF_VARS.fd /run/${dirName}/nix-ovmf/
'')}
+
+
# Symlink hooks to /var/lib/libvirt
+
${concatStringsSep "\n" (map (driver:
+
''
+
mkdir -p /var/lib/${dirName}/hooks/${driver}.d
+
rm -rf /var/lib/${dirName}/hooks/${driver}.d/*
+
${concatStringsSep "\n" (mapAttrsToList (name: value:
+
"ln -s --force ${value} /var/lib/${dirName}/hooks/${driver}.d/${name}") cfg.hooks.${driver})}
+
'') (attrNames cfg.hooks))}
'';
serviceConfig = {
+7
nixos/tests/libvirtd.nix
···
memorySize = 2048;
libvirtd.enable = true;
+
libvirtd.hooks.qemu.is_working = "${pkgs.writeShellScript "testHook.sh" ''
+
touch /tmp/qemu_hook_is_working
+
''}";
};
boot.supportedFilesystems = [ "zfs" ];
networking.hostId = "deadbeef"; # needed for zfs
···
virthost.shutdown()
virthost.wait_for_unit("multi-user.target")
virthost.wait_until_succeeds("ping -c 1 nixos")
+
+
with subtest("test if hooks are linked and run"):
+
virthost.succeed("ls /var/lib/libvirt/hooks/qemu.d/is_working")
+
virthost.succeed("ls /tmp/qemu_hook_is_working")
'';
})