9 cfg = config.services.openiscsi;
12 options.services.openiscsi = with types; {
13 enable = mkEnableOption "the openiscsi iscsi daemon";
14 enableAutoLoginOut = mkEnableOption ''
15 automatic login and logout of all automatic targets.
16 You probably do not want this
18 discoverPortal = mkOption {
21 description = "Portal to discover targets on";
25 description = "Name of this iscsi initiator";
26 example = "iqn.2020-08.org.linux-iscsi.initiatorhost:example";
28 package = mkPackageOption pkgs "openiscsi" { };
30 extraConfig = mkOption {
33 description = "Lines to append to default iscsid.conf";
36 extraConfigFile = mkOption {
38 Append an additional file's contents to /etc/iscsid.conf. Use a non-store path
39 and store passwords in this file.
46 config = mkIf cfg.enable {
47 environment.etc."iscsi/iscsid.conf.fragment".source = pkgs.runCommand "iscsid.conf" { } ''
48 cat "${cfg.package}/etc/iscsi/iscsid.conf" > $out
51 ${optionalString cfg.enableAutoLoginOut "node.startup = automatic"}
54 environment.etc."iscsi/initiatorname.iscsi".text = "InitiatorName=${cfg.name}";
56 systemd.packages = [ cfg.package ];
58 systemd.services."iscsid" = {
59 wantedBy = [ "multi-user.target" ];
62 extraCfgDumper = optionalString (cfg.extraConfigFile != null) ''
63 if [ -f "${cfg.extraConfigFile}" ]; then
64 printf "\n# The following is from ${cfg.extraConfigFile}:\n"
65 cat "${cfg.extraConfigFile}"
67 echo "Warning: services.openiscsi.extraConfigFile ${cfg.extraConfigFile} does not exist!" >&2
73 cat ${config.environment.etc."iscsi/iscsid.conf.fragment".source}
75 ) > /etc/iscsi/iscsid.conf
78 systemd.sockets."iscsid".wantedBy = [ "sockets.target" ];
80 systemd.services."iscsi" = mkIf cfg.enableAutoLoginOut {
81 wantedBy = [ "remote-fs.target" ];
82 serviceConfig.ExecStartPre =
83 mkIf (cfg.discoverPortal != null)
84 "${cfg.package}/bin/iscsiadm --mode discoverydb --type sendtargets --portal ${escapeShellArg cfg.discoverPortal} --discover";
87 environment.systemPackages = [ cfg.package ];
88 boot.kernelModules = [ "iscsi_tcp" ];