summaryrefslogtreecommitdiff
path: root/customized/autofs.nix
blob: f4a1059d09f0da5a06a147ddc65c560bed0332a0 (plain)
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
{ config, lib, pkgs, ... }:

with lib;

let

  cfg = config.services.autofs;

  autoMaster = pkgs.writeText "auto.master" cfg.autoMaster;

in

{

  ###### interface

  options = {

    services.autofs = {

      enable = mkOption {
        default = false;
        description = "
          Mount filesystems on demand. Unmount them automatically.
          You may also be interested in afuese.
        ";
      };

      autoMaster = mkOption {
        example = literalExample ''
          autoMaster = let
            mapConf = pkgs.writeText "auto" '''
             kernel    -ro,soft,intr       ftp.kernel.org:/pub/linux
             boot      -fstype=ext2        :/dev/hda1
             windoze   -fstype=smbfs       ://windoze/c
             removable -fstype=ext2        :/dev/hdd
             cd        -fstype=iso9660,ro  :/dev/hdc
             floppy    -fstype=auto        :/dev/fd0
             server    -rw,hard,intr       / -ro myserver.me.org:/ \
                                           /usr myserver.me.org:/usr \
                                           /home myserver.me.org:/home
            ''';
          in '''
            /auto file:''${mapConf}
          '''
        '';
        description = "
          file contents of /etc/auto.master. See man auto.master
          See man 5 auto.master and man 5 autofs.
        ";
      };

      timeout = mkOption {
        default = 600;
        description = "Set the global minimum timeout, in seconds, until directories are unmounted";
      };

      debug = mkOption {
        default = false;
        description = "
        pass -d and -7 to automount and write log to /var/log/autofs
        ";
      };

    };

  };


  ###### implementation

  config = mkIf cfg.enable {

    environment.etc = singleton
      { target = "auto.master";
        source = pkgs.writeText "auto.master" cfg.autoMaster;
      };

    boot.kernelModules = [ "autofs4" ];

    jobs.autofs =
      { description = "Filesystem automounter";

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

        path = [ pkgs.nfs-utils pkgs.sshfsFuse ];

        preStop =
          ''
            set -e; while :; do pkill -TERM automount; sleep 1; done
          '';

        # automount doesn't clean up when receiving SIGKILL.
        # umount -l should unmount the directories recursively when they are no longer used
        # It does, but traces are left in /etc/mtab. So unmount recursively..
        postStop =
          ''
          PATH=${pkgs.gnused}/bin:${pkgs.coreutils}/bin
          exec &> /tmp/logss
          # double quote for sed:
          escapeSpaces(){ sed 's/ /\\\\040/g'; }
          unescapeSpaces(){ sed 's/\\040/ /g'; }
          sed -n 's@^\s*\(\([^\\ ]\|\\ \)*\)\s.*@\1@p' ${autoMaster} | sed 's/[\\]//' | while read mountPoint; do
            sed -n "s@[^ ]\+\s\+\($(echo "$mountPoint"| escapeSpaces)[^ ]*\).*@\1@p" /proc/mounts | sort -r | unescapeSpaces| while read smountP; do
              ${pkgs.utillinux}/bin/umount -l "$smountP" || true
            done
          done
          '';

        script =
          ''
            ${if cfg.debug then "exec &> /var/log/autofs" else ""}
            exec ${pkgs.autofs5}/sbin/automount ${if cfg.debug then "-d" else ""} -f -t ${builtins.toString cfg.timeout} "${autoMaster}" ${if cfg.debug then "-l7" else ""}
          '';
      };

  };

}