| 1 | { pkgs, ... }: |
| 2 | let |
| 3 | root = "/srv/vm-disks"; |
| 4 | dataset = "work/vm-disks"; |
| 5 | limits = builtins.fromJSON (builtins.readFile ../tools/vm-ai-limits.json); |
| 6 | quota = toString (limits.host_total * (limits.disk_gib + limits.reserve_gib)); |
| 7 | in |
| 8 | { |
| 9 | environment.variables.SNOWGLOBE_VM_AI_DISKS_ROOT = root; |
| 10 | systemd.services.snowglobe-vm-storage = { |
| 11 | requires = [ "zfs-import-work.service" "dev-tpmrm0.device" ]; |
| 12 | after = [ "zfs-import-work.service" "zfs-mount.service" "dev-tpmrm0.device" ]; |
| 13 | serviceConfig = { |
| 14 | Type = "oneshot"; |
| 15 | RemainAfterExit = true; |
| 16 | UMask = "0077"; |
| 17 | LoadCredentialEncrypted = "zfs-clover:/etc/credstore.encrypted/zfs-clover"; |
| 18 | }; |
| 19 | script = '' |
| 20 | if ! ${pkgs.zfs}/bin/zfs list -H -o name ${dataset} >/dev/null 2>&1; then |
| 21 | ${pkgs.zfs}/bin/zfs create -u -o encryption=aes-256-gcm -o keyformat=hex \ |
| 22 | -o keylocation="file://$CREDENTIALS_DIRECTORY/zfs-clover" \ |
| 23 | -o mountpoint=${root} -o compression=lz4 -o atime=off ${dataset} |
| 24 | fi |
| 25 | test "$(${pkgs.zfs}/bin/zfs get -H -o value encryptionroot ${dataset})" = ${dataset} |
| 26 | test "$(${pkgs.zfs}/bin/zfs get -H -o value mountpoint ${dataset})" = ${root} |
| 27 | ${pkgs.zfs}/bin/zfs set keylocation=prompt quota=${quota}G ${dataset} |
| 28 | if test "$(${pkgs.zfs}/bin/zfs get -H -o value keystatus ${dataset})" = unavailable; then |
| 29 | ${pkgs.zfs}/bin/zfs load-key -L "file://$CREDENTIALS_DIRECTORY/zfs-clover" ${dataset} |
| 30 | fi |
| 31 | if test "$(${pkgs.zfs}/bin/zfs get -H -o value mounted ${dataset})" != yes; then |
| 32 | ${pkgs.zfs}/bin/zfs mount ${dataset} |
| 33 | fi |
| 34 | test "$(${pkgs.util-linux}/bin/findmnt -n -o SOURCE --mountpoint ${root})" = ${dataset} |
| 35 | chmod 0700 ${root} |
| 36 | ''; |
| 37 | }; |
| 38 | systemd.services.snowglobe-host = { |
| 39 | wants = [ "snowglobe-vm-storage.service" ]; |
| 40 | after = [ "snowglobe-vm-storage.service" ]; |
| 41 | serviceConfig.ReadWritePaths = [ "-${root}" ]; |
| 42 | }; |
| 43 | systemd.services.snowglobe-vm-expiry = { |
| 44 | wants = [ "snowglobe-vm-storage.service" ]; |
| 45 | after = [ "snowglobe-vm-storage.service" ]; |
| 46 | environment.SNOWGLOBE_VM_AI_DISKS_ROOT = root; |
| 47 | serviceConfig.ReadWritePaths = [ "-${root}" ]; |
| 48 | }; |
| 49 | } |