aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIsmaël Bouya <ismael.bouya@normalesup.org>2021-06-25 10:31:40 +0200
committerIsmaël Bouya <ismael.bouya@normalesup.org>2021-06-25 10:31:40 +0200
commitc20a589de267dded1c096e1c1339b5efe5156f1f (patch)
treee83bb040232b36b8ed3cf84db0b3f72909bab9d3
parent200690c9aecec1f38c1a62a65916df2950e1afe7 (diff)
downloadNix-c20a589de267dded1c096e1c1339b5efe5156f1f.tar.gz
Nix-c20a589de267dded1c096e1c1339b5efe5156f1f.tar.zst
Nix-c20a589de267dded1c096e1c1339b5efe5156f1f.zip
Make libvirt pools declarative.
-rw-r--r--modules/private/system/dilion/vms.nix63
1 files changed, 51 insertions, 12 deletions
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 @@
1# inspired from https://nixos.wiki/wiki/Virtualization_in_NixOS 1# inspired from https://nixos.wiki/wiki/Virtualization_in_NixOS
2{ config, pkgs, lib, ... }@args: 2{ config, pkgs, lib, ... }@args:
3let 3let
4 pools = {
5 niximages = {
6 type = "dir";
7 target = "/etc/libvirtd/base-images";
8 };
9 buildbot-disks = rec {
10 preStart = ''
11 mkdir -p ${target}
12 '';
13 type = "dir";
14 target = "/var/lib/libvirt/images/buildbot-disks";
15 };
16 zfspool = {
17 # pool-define-as --name zfspool --source-name zpool/libvirt --type zfs
18 type = "zfs";
19 xml = ''
20 <source>
21 <name>zpool/libvirt</name>
22 </source>
23 '';
24 };
25 };
4 networks = { 26 networks = {
5 immae = { 27 immae = {
6 bridgeNumber = "1"; 28 bridgeNumber = "1";
@@ -28,15 +50,6 @@ let
28 network = "immae"; 50 network = "immae";
29 diskSize = "10GiB"; 51 diskSize = "10GiB";
30 destroyVolumeOnExit = true; 52 destroyVolumeOnExit = true;
31 preStart = ''
32 if ! ${pkgs.libvirt}/bin/virsh pool-info --pool niximages &> /dev/null; then
33 pool-create-as --name niximages --type dir --target /etc/libvirtd/base-images/
34 fi
35 if ! ${pkgs.libvirt}/bin/virsh pool-info --pool buildbot-disks &> /dev/null; then
36 mkdir -p /var/lib/libvirt/images/buildbot-disks
37 pool-create-as --name buildbot-disks --type dir --target /var/lib/libvirt/images/buildbot-disks
38 fi
39 '';
40 }; 53 };
41 }; 54 };
42 toImage = f: "${import ./vms/base_image.nix f (args // { myEnv = config.myEnv; })}/nixos.qcow2"; 55 toImage = f: "${import ./vms/base_image.nix f (args // { myEnv = config.myEnv; })}/nixos.qcow2";
@@ -45,8 +58,8 @@ in
45 environment.etc."libvirtd/base-images/nixos.qcow2".source = toImage ./vms/base_configuration.nix; 58 environment.etc."libvirtd/base-images/nixos.qcow2".source = toImage ./vms/base_configuration.nix;
46 environment.etc."libvirtd/base-images/buildbot.qcow2".source = toImage ./vms/buildbot_configuration.nix; 59 environment.etc."libvirtd/base-images/buildbot.qcow2".source = toImage ./vms/buildbot_configuration.nix;
47 systemd.services = lib.mapAttrs' (name: guest: lib.nameValuePair "libvirtd-guest-${name}" { 60 systemd.services = lib.mapAttrs' (name: guest: lib.nameValuePair "libvirtd-guest-${name}" {
48 after = [ "libvirtd.service" "libvirtd-network-${guest.network}.service" ]; 61 after = [ "libvirtd.service" "libvirtd-pool-${guest.pool}.service" "libvirtd-network-${guest.network}.service" ];
49 requires = [ "libvirtd.service" "libvirtd-network-${guest.network}.service" ]; 62 requires = [ "libvirtd.service" "libvirtd-pool-${guest.pool}.service" "libvirtd-network-${guest.network}.service" ];
50 wantedBy = [ "multi-user.target" ]; 63 wantedBy = [ "multi-user.target" ];
51 serviceConfig = { 64 serviceConfig = {
52 Type = "oneshot"; 65 Type = "oneshot";
@@ -142,5 +155,31 @@ in
142 preStop = '' 155 preStop = ''
143 ${pkgs.libvirt}/bin/virsh net-destroy '${name}' 156 ${pkgs.libvirt}/bin/virsh net-destroy '${name}'
144 ''; 157 '';
145 }) networks); 158 }) networks) // (lib.mapAttrs' (name: pool: lib.nameValuePair "libvirtd-pool-${name}" {
159 after = [ "libvirtd.service" ];
160 requires = [ "libvirtd.service" ];
161 wantedBy = [ "multi-user.target" ];
162 serviceConfig = {
163 Type = "oneshot";
164 RemainAfterExit = "yes";
165 };
166 script = let
167 xml = pkgs.writeText "libvirt-pool-${name}.xml" ''
168 <pool type="${pool.type}">
169 <name>${name}</name>
170 <uuid>UUID</uuid>
171 ${pool.xml or ""}
172 ${if pool ? target then ''
173 <target>
174 <path>${pool.target}</path>
175 </target>
176 '' else ""}
177 </pool>
178 '';
179 in pool.preStart or "" + ''
180 uuid="$(${pkgs.libvirt}/bin/virsh pool-uuid '${name}' || true)"
181 ${pkgs.libvirt}/bin/virsh pool-define <(sed "s/UUID/$uuid/" '${xml}')
182 ${pkgs.libvirt}/bin/virsh pool-start '${name}' || true
183 '';
184 }) pools);
146} 185}