summary refs log tree commit diff
path: root/nixos/modules/services/networking/iscsi/initiator.nix
blob: d2865a660ead0a21e9c99e31c402f363c964050b (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
{ config, lib, pkgs, ... }: with lib;
let
  cfg = config.services.openiscsi;
in
{
  options.services.openiscsi = with types; {
    enable = mkEnableOption (lib.mdDoc "the openiscsi iscsi daemon");
    enableAutoLoginOut = mkEnableOption (lib.mdDoc ''
      automatic login and logout of all automatic targets.
      You probably do not want this.
    '');
    discoverPortal = mkOption {
      type = nullOr str;
      default = null;
      description = lib.mdDoc "Portal to discover targets on";
    };
    name = mkOption {
      type = str;
      description = lib.mdDoc "Name of this iscsi initiator";
      example = "iqn.2020-08.org.linux-iscsi.initiatorhost:example";
    };
    package = mkOption {
      type = package;
      description = lib.mdDoc "openiscsi package to use";
      default = pkgs.openiscsi;
      defaultText = literalExpression "pkgs.openiscsi";
    };

    extraConfig = mkOption {
      type = str;
      default = "";
      description = lib.mdDoc "Lines to append to default iscsid.conf";
    };

    extraConfigFile = mkOption {
      description = lib.mdDoc ''
        Append an additional file's contents to /etc/iscsid.conf. Use a non-store path
        and store passwords in this file.
      '';
      default = null;
      type = nullOr str;
    };
  };

  config = mkIf cfg.enable {
    environment.etc."iscsi/iscsid.conf.fragment".source = pkgs.runCommand "iscsid.conf" {} ''
      cat "${cfg.package}/etc/iscsi/iscsid.conf" > $out
      cat << 'EOF' >> $out
      ${cfg.extraConfig}
      ${optionalString cfg.enableAutoLoginOut "node.startup = automatic"}
      EOF
    '';
    environment.etc."iscsi/initiatorname.iscsi".text = "InitiatorName=${cfg.name}";

    system.activationScripts.iscsid = let
      extraCfgDumper = optionalString (cfg.extraConfigFile != null) ''
        if [ -f "${cfg.extraConfigFile}" ]; then
          printf "\n# The following is from ${cfg.extraConfigFile}:\n"
          cat "${cfg.extraConfigFile}"
        else
          echo "Warning: services.openiscsi.extraConfigFile ${cfg.extraConfigFile} does not exist!" >&2
        fi
      '';
    in ''
      (
        cat ${config.environment.etc."iscsi/iscsid.conf.fragment".source}
        ${extraCfgDumper}
      ) > /etc/iscsi/iscsid.conf
    '';

    systemd.packages = [ cfg.package ];

    systemd.services."iscsid".wantedBy = [ "multi-user.target" ];
    systemd.sockets."iscsid".wantedBy = [ "sockets.target" ];

    systemd.services."iscsi" = mkIf cfg.enableAutoLoginOut {
      wantedBy = [ "remote-fs.target" ];
      serviceConfig.ExecStartPre = mkIf (cfg.discoverPortal != null) "${cfg.package}/bin/iscsiadm --mode discoverydb --type sendtargets --portal ${escapeShellArg cfg.discoverPortal} --discover";
    };

    environment.systemPackages = [ cfg.package ];
    boot.kernelModules = [ "iscsi_tcp" ];
  };
}