From: Ismaƫl Bouya Date: Fri, 25 Jun 2021 08:31:40 +0000 (+0200) Subject: Make libvirt pools declarative. X-Git-Url: https://git.immae.eu/?a=commitdiff_plain;h=c20a589de267dded1c096e1c1339b5efe5156f1f;p=perso%2FImmae%2FConfig%2FNix.git Make libvirt pools declarative. --- diff --git a/modules/private/system/dilion/vms.nix b/modules/private/system/dilion/vms.nix index 8d5a57b..af96622 100644 --- a/modules/private/system/dilion/vms.nix +++ b/modules/private/system/dilion/vms.nix @@ -1,6 +1,28 @@ # inspired from https://nixos.wiki/wiki/Virtualization_in_NixOS { config, pkgs, lib, ... }@args: let + pools = { + niximages = { + type = "dir"; + target = "/etc/libvirtd/base-images"; + }; + buildbot-disks = rec { + preStart = '' + mkdir -p ${target} + ''; + type = "dir"; + target = "/var/lib/libvirt/images/buildbot-disks"; + }; + zfspool = { + # pool-define-as --name zfspool --source-name zpool/libvirt --type zfs + type = "zfs"; + xml = '' + + zpool/libvirt + + ''; + }; + }; networks = { immae = { bridgeNumber = "1"; @@ -28,15 +50,6 @@ let network = "immae"; diskSize = "10GiB"; destroyVolumeOnExit = true; - preStart = '' - if ! ${pkgs.libvirt}/bin/virsh pool-info --pool niximages &> /dev/null; then - pool-create-as --name niximages --type dir --target /etc/libvirtd/base-images/ - fi - if ! ${pkgs.libvirt}/bin/virsh pool-info --pool buildbot-disks &> /dev/null; then - mkdir -p /var/lib/libvirt/images/buildbot-disks - pool-create-as --name buildbot-disks --type dir --target /var/lib/libvirt/images/buildbot-disks - fi - ''; }; }; toImage = f: "${import ./vms/base_image.nix f (args // { myEnv = config.myEnv; })}/nixos.qcow2"; @@ -45,8 +58,8 @@ in environment.etc."libvirtd/base-images/nixos.qcow2".source = toImage ./vms/base_configuration.nix; environment.etc."libvirtd/base-images/buildbot.qcow2".source = toImage ./vms/buildbot_configuration.nix; systemd.services = lib.mapAttrs' (name: guest: lib.nameValuePair "libvirtd-guest-${name}" { - after = [ "libvirtd.service" "libvirtd-network-${guest.network}.service" ]; - requires = [ "libvirtd.service" "libvirtd-network-${guest.network}.service" ]; + after = [ "libvirtd.service" "libvirtd-pool-${guest.pool}.service" "libvirtd-network-${guest.network}.service" ]; + requires = [ "libvirtd.service" "libvirtd-pool-${guest.pool}.service" "libvirtd-network-${guest.network}.service" ]; wantedBy = [ "multi-user.target" ]; serviceConfig = { Type = "oneshot"; @@ -142,5 +155,31 @@ in preStop = '' ${pkgs.libvirt}/bin/virsh net-destroy '${name}' ''; - }) networks); + }) networks) // (lib.mapAttrs' (name: pool: lib.nameValuePair "libvirtd-pool-${name}" { + after = [ "libvirtd.service" ]; + requires = [ "libvirtd.service" ]; + wantedBy = [ "multi-user.target" ]; + serviceConfig = { + Type = "oneshot"; + RemainAfterExit = "yes"; + }; + script = let + xml = pkgs.writeText "libvirt-pool-${name}.xml" '' + + ${name} + UUID + ${pool.xml or ""} + ${if pool ? target then '' + + ${pool.target} + + '' else ""} + + ''; + in pool.preStart or "" + '' + uuid="$(${pkgs.libvirt}/bin/virsh pool-uuid '${name}' || true)" + ${pkgs.libvirt}/bin/virsh pool-define <(sed "s/UUID/$uuid/" '${xml}') + ${pkgs.libvirt}/bin/virsh pool-start '${name}' || true + ''; + }) pools); }