at 25.11-pre 2.7 kB view raw
1{ 2 config, 3 lib, 4 pkgs, 5 ... 6}: 7with lib; 8let 9 cfg = config.services.openiscsi; 10in 11{ 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 17 ''; 18 discoverPortal = mkOption { 19 type = nullOr str; 20 default = null; 21 description = "Portal to discover targets on"; 22 }; 23 name = mkOption { 24 type = str; 25 description = "Name of this iscsi initiator"; 26 example = "iqn.2020-08.org.linux-iscsi.initiatorhost:example"; 27 }; 28 package = mkPackageOption pkgs "openiscsi" { }; 29 30 extraConfig = mkOption { 31 type = str; 32 default = ""; 33 description = "Lines to append to default iscsid.conf"; 34 }; 35 36 extraConfigFile = mkOption { 37 description = '' 38 Append an additional file's contents to /etc/iscsid.conf. Use a non-store path 39 and store passwords in this file. 40 ''; 41 default = null; 42 type = nullOr str; 43 }; 44 }; 45 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 49 cat << 'EOF' >> $out 50 ${cfg.extraConfig} 51 ${optionalString cfg.enableAutoLoginOut "node.startup = automatic"} 52 EOF 53 ''; 54 environment.etc."iscsi/initiatorname.iscsi".text = "InitiatorName=${cfg.name}"; 55 56 systemd.packages = [ cfg.package ]; 57 58 systemd.services."iscsid" = { 59 wantedBy = [ "multi-user.target" ]; 60 preStart = 61 let 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}" 66 else 67 echo "Warning: services.openiscsi.extraConfigFile ${cfg.extraConfigFile} does not exist!" >&2 68 fi 69 ''; 70 in 71 '' 72 ( 73 cat ${config.environment.etc."iscsi/iscsid.conf.fragment".source} 74 ${extraCfgDumper} 75 ) > /etc/iscsi/iscsid.conf 76 ''; 77 }; 78 systemd.sockets."iscsid".wantedBy = [ "sockets.target" ]; 79 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"; 85 }; 86 87 environment.systemPackages = [ cfg.package ]; 88 boot.kernelModules = [ "iscsi_tcp" ]; 89 }; 90}