summary refs log tree commit diff
path: root/nixos/modules/services/network-filesystems/openafs-client/default.nix
blob: 0297da9e865faf42d52b1c0670d55bdaf10b944b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
{ config, pkgs, lib, ... }:

let
  inherit (lib) mkOption mkIf;

  cfg = config.services.openafsClient;

  cellServDB = pkgs.fetchurl {
    url = http://dl.central.org/dl/cellservdb/CellServDB.2009-06-29;
    sha256 = "be566f850e88130333ab8bc3462872ad90c9482e025c60a92f728b5bac1b4fa9";
  };

  afsConfig = pkgs.runCommand "afsconfig" {} ''
    mkdir -p $out
    echo ${cfg.cellName} > $out/ThisCell
    cp ${cellServDB} $out/CellServDB
    echo "/afs:${cfg.cacheDirectory}:${cfg.cacheSize}" > $out/cacheinfo
  '';

  openafsPkgs = config.boot.kernelPackages.openafsClient;
in
{
  ###### interface

  options = {

    services.openafsClient = {

      enable = mkOption {
        default = false;
        description = "Whether to enable the OpenAFS client.";
      };

      cellName = mkOption {
        default = "grand.central.org";
        description = "Cell name.";
      };

      cacheSize = mkOption {
        default = "100000";
        description = "Cache size.";
      };

      cacheDirectory = mkOption {
        default = "/var/cache/openafs";
        description = "Cache directory.";
      };

      crypt = mkOption {
        default = false;
        description = "Whether to enable (weak) protocol encryption.";
      };

      sparse = mkOption {
        default = false;
        description = "Minimal cell list in /afs.";
      };

    };
  };


  ###### implementation

  config = mkIf cfg.enable {

    environment.systemPackages = [ openafsPkgs ];

    environment.etc = [
      { source = afsConfig;
        target = "openafs";
      }
    ];

    jobs.openafsClient =
      { name = "afsd";

        description = "AFS client";

        startOn = "started network-interfaces";
        stopOn = "stopping network-interfaces";

        preStart = ''
          mkdir -p -m 0755 /afs
          mkdir -m 0700 -p ${cfg.cacheDirectory}
          ${pkgs.module_init_tools}/sbin/insmod ${openafsPkgs}/lib/openafs/libafs-*.ko || true
          ${openafsPkgs}/sbin/afsd -confdir ${afsConfig} -cachedir ${cfg.cacheDirectory} ${if cfg.sparse then "-dynroot-sparse" else "-dynroot"} -fakestat -afsdb
          ${openafsPkgs}/bin/fs setcrypt ${if cfg.crypt then "on" else "off"}
        '';

        # Doing this in preStop, because after these commands AFS is basically
        # stopped, so systemd has nothing to do, just noticing it.  If done in
        # postStop, then we get a hang + kernel oops, because AFS can't be
        # stopped simply by sending signals to processes.
        preStop = ''
          ${pkgs.utillinux}/bin/umount /afs
          ${openafsPkgs}/sbin/afsd -shutdown
          ${pkgs.module_init_tools}/sbin/rmmod libafs
        '';

      };

  };

}