Commit 9617d41a authored by Yoann Beaugnon's avatar Yoann Beaugnon
Browse files

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
parent 7b5fb7cf
Loading
Loading
Loading
Loading
+71 −0
Original line number Diff line number Diff line
@@ -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.
      '';
    };
  };


@@ -335,6 +397,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 = {
+7 −0
Original line number Diff line number Diff line
@@ -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")
  '';
})