lol

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

+78
+71
nixos/modules/virtualisation/libvirtd.nix
··· 129 129 }; 130 130 }; 131 131 }; 132 + 133 + hooksModule = types.submodule { 134 + options = { 135 + daemon = mkOption { 136 + type = types.attrsOf types.path; 137 + default = { }; 138 + description = lib.mdDoc '' 139 + Hooks that will be placed under /var/lib/libvirt/hooks/daemon.d/ 140 + and called for daemon start/shutdown/SIGHUP events. 141 + Please see https://libvirt.org/hooks.html for documentation. 142 + ''; 143 + }; 144 + 145 + qemu = mkOption { 146 + type = types.attrsOf types.path; 147 + default = { }; 148 + description = lib.mdDoc '' 149 + Hooks that will be placed under /var/lib/libvirt/hooks/qemu.d/ 150 + and called for qemu domains begin/end/migrate events. 151 + Please see https://libvirt.org/hooks.html for documentation. 152 + ''; 153 + }; 154 + 155 + lxc = mkOption { 156 + type = types.attrsOf types.path; 157 + default = { }; 158 + description = lib.mdDoc '' 159 + Hooks that will be placed under /var/lib/libvirt/hooks/lxc.d/ 160 + and called for lxc domains begin/end events. 161 + Please see https://libvirt.org/hooks.html for documentation. 162 + ''; 163 + }; 164 + 165 + libxl = mkOption { 166 + type = types.attrsOf types.path; 167 + default = { }; 168 + description = lib.mdDoc '' 169 + Hooks that will be placed under /var/lib/libvirt/hooks/libxl.d/ 170 + and called for libxl-handled xen domains begin/end events. 171 + Please see https://libvirt.org/hooks.html for documentation. 172 + ''; 173 + }; 174 + 175 + network = mkOption { 176 + type = types.attrsOf types.path; 177 + default = { }; 178 + description = lib.mdDoc '' 179 + Hooks that will be placed under /var/lib/libvirt/hooks/lxc.d/ 180 + and called for networks begin/end events. 181 + Please see https://libvirt.org/hooks.html for documentation. 182 + ''; 183 + }; 184 + }; 185 + }; 132 186 in 133 187 { 134 188 ··· 246 300 QEMU related options. 247 301 ''; 248 302 }; 303 + 304 + hooks = mkOption { 305 + type = hooksModule; 306 + default = { }; 307 + description = lib.mdDoc '' 308 + Hooks related options. 309 + ''; 310 + }; 249 311 }; 250 312 251 313 ··· 335 397 ln -s --force ${ovmfpackage}/FV/AAVMF_VARS.fd /run/${dirName}/nix-ovmf/ 336 398 ln -s --force ${ovmfpackage}/FV/OVMF_VARS.fd /run/${dirName}/nix-ovmf/ 337 399 '')} 400 + 401 + # Symlink hooks to /var/lib/libvirt 402 + ${concatStringsSep "\n" (map (driver: 403 + '' 404 + mkdir -p /var/lib/${dirName}/hooks/${driver}.d 405 + rm -rf /var/lib/${dirName}/hooks/${driver}.d/* 406 + ${concatStringsSep "\n" (mapAttrsToList (name: value: 407 + "ln -s --force ${value} /var/lib/${dirName}/hooks/${driver}.d/${name}") cfg.hooks.${driver})} 408 + '') (attrNames cfg.hooks))} 338 409 ''; 339 410 340 411 serviceConfig = {
+7
nixos/tests/libvirtd.nix
··· 11 11 memorySize = 2048; 12 12 13 13 libvirtd.enable = true; 14 + libvirtd.hooks.qemu.is_working = "${pkgs.writeShellScript "testHook.sh" '' 15 + touch /tmp/qemu_hook_is_working 16 + ''}"; 14 17 }; 15 18 boot.supportedFilesystems = [ "zfs" ]; 16 19 networking.hostId = "deadbeef"; # needed for zfs ··· 57 60 virthost.shutdown() 58 61 virthost.wait_for_unit("multi-user.target") 59 62 virthost.wait_until_succeeds("ping -c 1 nixos") 63 + 64 + with subtest("test if hooks are linked and run"): 65 + virthost.succeed("ls /var/lib/libvirt/hooks/qemu.d/is_working") 66 + virthost.succeed("ls /tmp/qemu_hook_is_working") 60 67 ''; 61 68 })