Merge pull request #232250 from YBeaugnon/libvirt-hooks

nixos/libvirtd: hooks support
This commit is contained in:
Franz Pletz 2023-08-09 15:07:52 +02:00 committed by GitHub
commit dcafb07ed8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 78 additions and 0 deletions

View file

@ -129,6 +129,60 @@ let
};
};
};
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
{
@ -246,6 +300,14 @@ in
QEMU related options.
'';
};
hooks = mkOption {
type = hooksModule;
default = { };
description = lib.mdDoc ''
Hooks related options.
'';
};
};
@ -337,6 +399,15 @@ in
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 = {

View file

@ -11,6 +11,9 @@ import ./make-test-python.nix ({ pkgs, ... }: {
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
@ -57,5 +60,9 @@ import ./make-test-python.nix ({ pkgs, ... }: {
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")
'';
})