summary refs log tree commit diff
path: root/modules/services/network-filesystems/nfs-kernel.nix
blob: bc828d5bbb8625b18c8ec2fc4e5c703c71b2868c (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
{ config, pkgs, ... }:

with pkgs.lib;

let

  inherit (pkgs) writeText openssh;

  cfg = config.services.nfsKernel;

  exports = pkgs.writeText "exports" cfg.server.exports;

in

{

  ###### interface

  options = {
  
    services.nfsKernel = {

      client.enable = mkOption {
        default = any (fs: fs.fsType == "nfs" || fs.fsType == "nfs4") config.fileSystems;
        description = ''
          Whether to enable the kernel's NFS client daemons.
        '';
      };

      server = {
        enable = mkOption {
          default = false;
          description = ''
            Whether to enable the kernel's NFS server.
          '';
        };

        exports = mkOption {
          default = "";
          description = ''
            Contents of the /etc/exports file.  See
            <citerefentry><refentrytitle>exports</refentrytitle>
            <manvolnum>5</manvolnum></citerefentry> for the format.
          '';
        };

        hostName = mkOption {
          default = null;
          description = ''
            Hostname or address on which NFS requests will be accepted.
            Default is all.  See the <option>-H</option> option in
            <citerefentry><refentrytitle>nfsd</refentrytitle>
            <manvolnum>8</manvolnum></citerefentry>.
          '';
        };
      
        nproc = mkOption {
          default = 8;
          description = ''
            Number of NFS server threads.  Defaults to the recommended value of 8.
          '';
        };

        createMountPoints = mkOption {
          default = false;
          description = "Whether to create the mount points in the exports file at startup time.";
        };
      };
      
    };

  };


  ###### implementation

  config =
  mkAssert
    (cfg.client.enable || cfg.server.enable -> config.services.portmap.enable) "
    Please enable portmap (services.portmap.enable) to use nfs-kernel.
  " {

    services.portmap.enable = mkAlways (cfg.client.enable || cfg.server.enable);

    environment.etc = mkIf cfg.server.enable (singleton
      { source = exports;
        target = "exports";
      });

    jobs =
      optionalAttrs cfg.server.enable
        { nfs_kernel_exports = 
          { name = "nfs-kernel-exports";
      
            description = "Kernel NFS server";

            startOn = "started network-interfaces";

            preStart =
              ''
                export PATH=${pkgs.nfsUtils}/sbin:$PATH
                mkdir -p /var/lib/nfs
                
                ${config.system.sbin.modprobe}/sbin/modprobe nfsd || true

                ${pkgs.sysvtools}/bin/mountpoint -q /proc/fs/nfsd \
                || ${config.system.sbin.mount}/bin/mount -t nfsd none /proc/fs/nfsd

                ${optionalString cfg.server.createMountPoints
                  ''
                    # create export directories:
                    # skip comments, take first col which may either be a quoted
                    # "foo bar" or just foo (-> man export)
                    sed '/^#.*/d;s/^"\([^"]*\)".*/\1/;t;s/[ ].*//' ${exports} \
                    | xargs -d '\n' mkdir -p
                  ''
                }

                # exports file is ${exports}
                # keep this comment so that this job is restarted whenever exports changes!
                exportfs -ra
              '';
          };
        }
    
      // optionalAttrs cfg.server.enable
        { nfs_kernel_nfsd = 
          { name = "nfs-kernel-nfsd";

            description = "Kernel NFS server";

            startOn = "started nfs-kernel-exports and started nfs-kernel-mountd and started nfs-kernel-statd and started portmap";
            stopOn = "stopping nfs-kernel-exports";

            preStart = 
              ''
                # Create a state directory required by NFSv4.
                mkdir -p /var/lib/nfs/v4recovery

                ${pkgs.nfsUtils}/sbin/rpc.nfsd \
                  ${if cfg.server.hostName != null then "-H ${cfg.server.hostName}" else ""} \
                  ${builtins.toString cfg.server.nproc}
              '';

            postStop = "${pkgs.nfsUtils}/sbin/rpc.nfsd 0";
          };
        }

      // optionalAttrs cfg.server.enable
        { nfs_kernel_mountd =
          { name = "nfs-kernel-mountd";

            description = "Kernel NFS server - mount daemon";

            startOn = "started portmap and started nfs-kernel-exports";
            stopOn = "stopped nfs-kernel-nfsd";

            daemonType = "fork";

            exec = "${pkgs.nfsUtils}/sbin/rpc.mountd -f /etc/exports";
          };
        }

      // optionalAttrs (cfg.client.enable || cfg.server.enable)
        { nfs_kernel_statd = 
          { name = "nfs-kernel-statd";

            description = "Kernel NFS server - Network Status Monitor";

            startOn = "started portmap";
            stopOn = "never";

            preStart =
              ''	
                mkdir -p /var/lib/nfs
                mkdir -p /var/lib/nfs/sm
                mkdir -p /var/lib/nfs/sm.bak
              '';

            daemonType = "fork";

            exec = "${pkgs.nfsUtils}/sbin/rpc.statd --no-notify";

            postStart = "${pkgs.nfsUtils}/sbin/sm-notify -d";
          };
        };
      
  };
  
}