about summary refs log tree commit diff
path: root/nixpkgs/nixos/modules/services/network-filesystems/beegfs.nix
blob: 86b1bb9160f10f645ef25e8c34cc6a0ad30193b8 (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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
{ config, lib, pkgs, ...} :

with lib;

let
  cfg = config.services.beegfs;

  # functions for the generations of config files

  configMgmtd = name: cfg: pkgs.writeText "mgmt-${name}.conf" ''
    storeMgmtdDirectory = ${cfg.mgmtd.storeDir}
    storeAllowFirstRunInit = false
    connAuthFile = ${cfg.connAuthFile}
    connPortShift = ${toString cfg.connPortShift}

    ${cfg.mgmtd.extraConfig}
  '';

  configAdmon = name: cfg: pkgs.writeText "admon-${name}.conf" ''
    sysMgmtdHost = ${cfg.mgmtdHost}
    connAuthFile = ${cfg.connAuthFile}
    connPortShift = ${toString cfg.connPortShift}

    ${cfg.admon.extraConfig}
  '';

  configMeta = name: cfg: pkgs.writeText "meta-${name}.conf" ''
    storeMetaDirectory = ${cfg.meta.storeDir}
    sysMgmtdHost = ${cfg.mgmtdHost}
    connAuthFile = ${cfg.connAuthFile}
    connPortShift = ${toString cfg.connPortShift}
    storeAllowFirstRunInit = false

    ${cfg.meta.extraConfig}
  '';

  configStorage = name: cfg: pkgs.writeText "storage-${name}.conf" ''
    storeStorageDirectory = ${cfg.storage.storeDir}
    sysMgmtdHost = ${cfg.mgmtdHost}
    connAuthFile = ${cfg.connAuthFile}
    connPortShift = ${toString cfg.connPortShift}
    storeAllowFirstRunInit = false

    ${cfg.storage.extraConfig}
  '';

  configHelperd = name: cfg: pkgs.writeText "helperd-${name}.conf" ''
    connAuthFile = ${cfg.connAuthFile}
    ${cfg.helperd.extraConfig}
  '';

  configClientFilename = name : "/etc/beegfs/client-${name}.conf";

  configClient = name: cfg: ''
    sysMgmtdHost = ${cfg.mgmtdHost}
    connAuthFile = ${cfg.connAuthFile}
    connPortShift = ${toString cfg.connPortShift}

    ${cfg.client.extraConfig}
  '';

  serviceList = [
    { service = "admon"; cfgFile = configAdmon; }
    { service = "meta"; cfgFile = configMeta; }
    { service = "mgmtd"; cfgFile = configMgmtd; }
    { service = "storage"; cfgFile = configStorage; }
  ];

  # functions to generate systemd.service entries

  systemdEntry = service: cfgFile: (mapAttrs' ( name: cfg:
    (nameValuePair "beegfs-${service}-${name}" (mkIf cfg."${service}".enable {
    wantedBy = [ "multi-user.target" ];
    requires = [ "network-online.target" ];
    after = [ "network-online.target" ];
    serviceConfig = rec {
      ExecStart = ''
        ${pkgs.beegfs}/bin/beegfs-${service} \
          cfgFile=${cfgFile name cfg} \
          pidFile=${PIDFile}
      '';
      PIDFile = "/run/beegfs-${service}-${name}.pid";
      TimeoutStopSec = "300";
    };
  }))) cfg);

  systemdHelperd =  mapAttrs' ( name: cfg:
    (nameValuePair "beegfs-helperd-${name}" (mkIf cfg.client.enable {
    wantedBy = [ "multi-user.target" ];
    requires = [ "network-online.target" ];
    after = [ "network-online.target" ];
    serviceConfig = rec {
      ExecStart = ''
        ${pkgs.beegfs}/bin/beegfs-helperd \
          cfgFile=${configHelperd name cfg} \
          pidFile=${PIDFile}
      '';
      PIDFile = "/run/beegfs-helperd-${name}.pid";
      TimeoutStopSec = "300";
    };
   }))) cfg;

  # wrappers to beegfs tools. Avoid typing path of config files
  utilWrappers = mapAttrsToList ( name: cfg:
    ( pkgs.runCommand "beegfs-utils-${name}" {
        nativeBuildInputs = [ pkgs.makeWrapper ];
        preferLocalBuild = true;
        } ''
        mkdir -p $out/bin

        makeWrapper ${pkgs.beegfs}/bin/beegfs-check-servers \
                    $out/bin/beegfs-check-servers-${name} \
                    --add-flags "-c ${configClientFilename name}" \
                    --prefix PATH : ${lib.makeBinPath [ pkgs.beegfs ]}

        makeWrapper ${pkgs.beegfs}/bin/beegfs-ctl \
                    $out/bin/beegfs-ctl-${name} \
                    --add-flags "--cfgFile=${configClientFilename name}"

        makeWrapper ${pkgs.beegfs}/bin/beegfs-ctl \
                    $out/bin/beegfs-df-${name} \
                    --add-flags "--cfgFile=${configClientFilename name}" \
                    --add-flags --listtargets  \
                    --add-flags --hidenodeid \
                    --add-flags --pools \
                    --add-flags --spaceinfo

        makeWrapper ${pkgs.beegfs}/bin/beegfs-fsck \
                    $out/bin/beegfs-fsck-${name} \
                    --add-flags "--cfgFile=${configClientFilename name}"
      ''
     )) cfg;
in
{
  ###### interface

  options = {
    services.beegfsEnable = mkEnableOption "BeeGFS";

    services.beegfs = mkOption {
      default = {};
      description = ''
        BeeGFS configurations. Every mount point requires a separate configuration.
      '';
      type = with types; attrsOf (submodule ({ ... } : {
        options = {
          mgmtdHost = mkOption {
            type = types.str;
            default = null;
            example = "master";
            description = ''Hostname of managament host.'';
          };

          connAuthFile = mkOption {
            type = types.str;
            default = "";
            example = "/etc/my.key";
            description = "File containing shared secret authentication.";
          };

          connPortShift = mkOption {
            type = types.int;
            default = 0;
            example = 5;
            description = ''
              For each additional beegfs configuration shift all
              service TCP/UDP ports by at least 5.
            '';
          };

          client = {
            enable = mkEnableOption "BeeGFS client";

            mount = mkOption {
              type = types.bool;
              default = true;
              description = "Create fstab entry automatically";
            };

            mountPoint = mkOption {
              type = types.str;
              default = "/run/beegfs";
              description = ''
                Mount point under which the beegfs filesytem should be mounted.
                If mounted manually the mount option specifing the config file is needed:
                cfgFile=/etc/beegfs/beegfs-client-<name>.conf
              '';
            };

            extraConfig = mkOption {
              type = types.lines;
              default = "";
              description = ''
                Additional lines for beegfs-client.conf.
                See documentation for further details.
             '';
            };
          };

          helperd = {
            enable = mkOption {
              type = types.bool;
              default = true;
              description = ''
                Enable the BeeGFS helperd.
                The helpered is need for logging purposes on the client.
                Disabling <literal>helperd</literal> allows for runing the client
                with <literal>allowUnfree = false</literal>.
              '';
            };

            extraConfig = mkOption {
              type = types.lines;
              default = "";
              description = ''
                Additional lines for beegfs-helperd.conf. See documentation
                for further details.
              '';
            };
          };

          mgmtd = {
            enable = mkEnableOption "BeeGFS mgmtd daemon";

            storeDir = mkOption {
              type = types.path;
              default = null;
              example = "/data/beegfs-mgmtd";
              description = ''
                Data directory for mgmtd.
                Must not be shared with other beegfs daemons.
                This directory must exist and it must be initialized
                with beegfs-setup-mgmtd, e.g. "beegfs-setup-mgmtd -C -p &lt;storeDir&gt;"
              '';
            };

            extraConfig = mkOption {
              type = types.lines;
              default = "";
              description = ''
                Additional lines for beegfs-mgmtd.conf. See documentation
                for further details.
              '';
            };
          };

          admon = {
            enable = mkEnableOption "BeeGFS admon daemon";

            extraConfig = mkOption {
              type = types.lines;
              default = "";
              description = ''
                Additional lines for beegfs-admon.conf. See documentation
                for further details.
              '';
            };
          };

          meta = {
            enable = mkEnableOption "BeeGFS meta data daemon";

            storeDir = mkOption {
              type = types.path;
              default = null;
              example = "/data/beegfs-meta";
              description = ''
                Data directory for meta data service.
                Must not be shared with other beegfs daemons.
                The underlying filesystem must be mounted with xattr turned on.
                This directory must exist and it must be initialized
                with beegfs-setup-meta, e.g.
                "beegfs-setup-meta -C -s &lt;serviceID&gt; -p &lt;storeDir&gt;"
              '';
            };

            extraConfig = mkOption {
              type = types.str;
              default = "";
              description = ''
                Additional lines for beegfs-meta.conf. See documentation
                for further details.
              '';
            };
          };

          storage = {
            enable = mkEnableOption "BeeGFS storage daemon";

            storeDir = mkOption {
              type = types.path;
              default = null;
              example = "/data/beegfs-storage";
              description = ''
                Data directories for storage service.
                Must not be shared with other beegfs daemons.
                The underlying filesystem must be mounted with xattr turned on.
                This directory must exist and it must be initialized
                with beegfs-setup-storage, e.g.
                "beegfs-setup-storage -C -s &lt;serviceID&gt; -i &lt;storageTargetID&gt; -p &lt;storeDir&gt;"
              '';
            };

            extraConfig = mkOption {
              type = types.str;
              default = "";
              description = ''
                Addional lines for beegfs-storage.conf. See documentation
                for further details.
              '';
            };
          };
        };
      }));
    };
  };

  ###### implementation

  config =
    mkIf config.services.beegfsEnable {

    environment.systemPackages = utilWrappers;

    # Put the client.conf files in /etc since they are needed
    # by the commandline tools
    environment.etc = mapAttrs' ( name: cfg:
      (nameValuePair "beegfs/client-${name}.conf" (mkIf (cfg.client.enable)
    {
      enable = true;
      text = configClient name cfg;
    }))) cfg;

    # Kernel module, we need it only once per host.
    boot = mkIf (
      foldr (a: b: a || b) false
        (map (x: x.client.enable) (collect (x: x ? client) cfg)))
    {
      kernelModules = [ "beegfs" ];
      extraModulePackages = [ pkgs.linuxPackages.beegfs-module ];
    };

    # generate fstab entries
    fileSystems = mapAttrs' (name: cfg:
      (nameValuePair cfg.client.mountPoint (optionalAttrs cfg.client.mount (mkIf cfg.client.enable {
      device = "beegfs_nodev";
      fsType = "beegfs";
      mountPoint = cfg.client.mountPoint;
      options = [ "cfgFile=${configClientFilename name}" "_netdev" ];
    })))) cfg;

    # generate systemd services
    systemd.services = systemdHelperd //
      foldr (a: b: a // b) {}
        (map (x: systemdEntry x.service x.cfgFile) serviceList);
  };
}