1{ config, pkgs, lib, ... }: 2 3let 4 inherit (lib) mkOption mkIf; 5 6 cfg = config.services.openafsClient; 7 8 cellServDB = pkgs.fetchurl { 9 url = http://dl.central.org/dl/cellservdb/CellServDB.2009-06-29; 10 sha256 = "be566f850e88130333ab8bc3462872ad90c9482e025c60a92f728b5bac1b4fa9"; 11 }; 12 13 afsConfig = pkgs.runCommand "afsconfig" {} '' 14 mkdir -p $out 15 echo ${cfg.cellName} > $out/ThisCell 16 cp ${cellServDB} $out/CellServDB 17 echo "/afs:${cfg.cacheDirectory}:${cfg.cacheSize}" > $out/cacheinfo 18 ''; 19 20 openafsPkgs = config.boot.kernelPackages.openafsClient; 21in 22{ 23 ###### interface 24 25 options = { 26 27 services.openafsClient = { 28 29 enable = mkOption { 30 default = false; 31 description = "Whether to enable the OpenAFS client."; 32 }; 33 34 cellName = mkOption { 35 default = "grand.central.org"; 36 description = "Cell name."; 37 }; 38 39 cacheSize = mkOption { 40 default = "100000"; 41 description = "Cache size."; 42 }; 43 44 cacheDirectory = mkOption { 45 default = "/var/cache/openafs"; 46 description = "Cache directory."; 47 }; 48 49 crypt = mkOption { 50 default = false; 51 description = "Whether to enable (weak) protocol encryption."; 52 }; 53 54 sparse = mkOption { 55 default = false; 56 description = "Minimal cell list in /afs."; 57 }; 58 59 }; 60 }; 61 62 63 ###### implementation 64 65 config = mkIf cfg.enable { 66 67 environment.systemPackages = [ openafsPkgs ]; 68 69 environment.etc = [ 70 { source = afsConfig; 71 target = "openafs"; 72 } 73 ]; 74 75 systemd.services.afsd = { 76 description = "AFS client"; 77 wantedBy = [ "multi-user.target" ]; 78 after = [ "network.target" ]; 79 serviceConfig = { RemainAfterExit = true; }; 80 81 preStart = '' 82 mkdir -p -m 0755 /afs 83 mkdir -m 0700 -p ${cfg.cacheDirectory} 84 ${pkgs.kmod}/bin/insmod ${openafsPkgs}/lib/openafs/libafs-*.ko || true 85 ${openafsPkgs}/sbin/afsd -confdir ${afsConfig} -cachedir ${cfg.cacheDirectory} ${if cfg.sparse then "-dynroot-sparse" else "-dynroot"} -fakestat -afsdb 86 ${openafsPkgs}/bin/fs setcrypt ${if cfg.crypt then "on" else "off"} 87 ''; 88 89 # Doing this in preStop, because after these commands AFS is basically 90 # stopped, so systemd has nothing to do, just noticing it. If done in 91 # postStop, then we get a hang + kernel oops, because AFS can't be 92 # stopped simply by sending signals to processes. 93 preStop = '' 94 ${pkgs.utillinux}/bin/umount /afs 95 ${openafsPkgs}/sbin/afsd -shutdown 96 ${pkgs.kmod}/sbin/rmmod libafs 97 ''; 98 }; 99 }; 100}