From: Hannes Reinecke <h...@suse.de>

If no source name has been specified in the config file (or no
config file is present) we should be falling back to read the
InitiatorName from /etc/iscsi/initiatorname.iscsi.
After all, the initiator name is supposed to be unique per
machine.

Signed-off-by: Hannes Reinecke <h...@suse.de>
---
 utils/open-isns/config.c          | 33 +++++++++++++++++++++++++++++++--
 utils/open-isns/doc/isns_config.5 |  6 ++++--
 utils/open-isns/isns.h            |  1 +
 utils/open-isns/isnsadm.c         | 13 +++++++++++--
 utils/open-isns/isnsd.c           | 10 ++++++++++
 utils/open-isns/isnsdd.c          | 13 ++++++++++++-
 utils/open-isns/paths.h           |  2 ++
 7 files changed, 71 insertions(+), 7 deletions(-)

diff --git a/utils/open-isns/config.c b/utils/open-isns/config.c
index 731858854650..c2cbdfbce773 100644
--- a/utils/open-isns/config.c
+++ b/utils/open-isns/config.c
@@ -92,6 +92,37 @@ __isns_config_defaults(void)
 }
 
 /*
+ * Read /etc/iscsi/initiatorname.iscsi
+ */
+int
+isns_read_initiatorname(const char *filename)
+{
+       FILE    *fp;
+       char    *name, *pos;
+
+       if ((fp = fopen(filename, "r")) == NULL) {
+               perror(filename);
+               return -1;
+       }
+
+       while ((pos = parser_get_next_line(fp)) != NULL) {
+               pos[strcspn(pos, "#")] = '\0';
+
+               if (!(name = parser_get_next_word(&pos)))
+                       continue;
+               if (strcmp(name, "InitiatorName"))
+                       continue;
+               if (pos[0] == '=')
+                       pos++;
+               if (!strncmp(pos, "iqn.", 4))
+                       isns_assign_string(&isns_config.ic_source_name, pos);
+       }
+
+       fclose(fp);
+       return 0;
+}
+
+/*
  * Read the iSNS configuration file
  */
 int
@@ -130,8 +161,6 @@ isns_read_config(const char *filename)
                        isns_config.ic_security = 0;
        }
 
-       isns_init_names();
-
        return 0;
 }
 
diff --git a/utils/open-isns/doc/isns_config.5 
b/utils/open-isns/doc/isns_config.5
index c04b97ec3599..bbee7591c1c6 100644
--- a/utils/open-isns/doc/isns_config.5
+++ b/utils/open-isns/doc/isns_config.5
@@ -62,8 +62,10 @@ When using DSA authentication, Open-iSNS currently requires 
the source
 name to match the key identifier (SPI) of the client's public
 key.
 .IP
-If left empty, the source name is derived from either from
-the client's hostname using the
+If left empty, the source name is derived from either from the default
+initiatorname in
+.BR /etc/iscsi/initiatorname.iscsi
+or, failing that, the client's hostname using the
 .BR IQNPrefix
 option to generate an iSCSI qualified name.
 .TP
diff --git a/utils/open-isns/isns.h b/utils/open-isns/isns.h
index 7adc57e56ecb..d17e18259f30 100644
--- a/utils/open-isns/isns.h
+++ b/utils/open-isns/isns.h
@@ -607,6 +607,7 @@ struct isns_config {
 };
 
 extern struct isns_config isns_config;
+extern int             isns_read_initiatorname(const char *);
 extern int             isns_read_config(const char *);
 extern int             isns_config_set(const char *, char *);
 
diff --git a/utils/open-isns/isnsadm.c b/utils/open-isns/isnsadm.c
index 5860463f6da3..c170595372da 100644
--- a/utils/open-isns/isnsadm.c
+++ b/utils/open-isns/isnsadm.c
@@ -167,8 +167,17 @@ main(int argc, char **argv)
                        usage(1, "Unknown option");
                }
        }
-       
-       isns_read_config(opt_configfile);
+
+       if (opt_configfile)
+               isns_read_config(opt_configfile);
+       if (!isns_config.ic_source_name) {
+               /*
+                * Try to read the source name from open-iscsi configuration
+                */
+               isns_read_initiatorname(ISCSI_DEFAULT_INITIATORNAME);
+       }
+
+       isns_init_names();
 
        if (!isns_config.ic_source_name)
                usage(1, "Please specify an iSNS source name");
diff --git a/utils/open-isns/isnsd.c b/utils/open-isns/isnsd.c
index 751dd08d70aa..47dbb69af600 100644
--- a/utils/open-isns/isnsd.c
+++ b/utils/open-isns/isnsd.c
@@ -109,8 +109,18 @@ main(int argc, char **argv)
 
        isns_read_config(opt_configfile);
 
+       if (!isns_config.ic_source_name) {
+               /*
+                * Try to read the source name from open-iscsi configuration
+                */
+               isns_read_initiatorname(ISCSI_DEFAULT_INITIATORNAME);
+       }
+
+       isns_init_names();
+
        if (!isns_config.ic_source_name)
                usage(1, "Please specify an iSNS source name");
+
        source = isns_source_create_iscsi(isns_config.ic_source_name);
 
        if (opt_mode == MODE_INIT)
diff --git a/utils/open-isns/isnsdd.c b/utils/open-isns/isnsdd.c
index 850060a53294..5b0dfb4c56fe 100644
--- a/utils/open-isns/isnsdd.c
+++ b/utils/open-isns/isnsdd.c
@@ -161,6 +161,7 @@ main(int argc, char **argv)
        if (optind != argc)
                usage(1, NULL);
 
+#if 0
        /* If the config code derives the source name
         * automatically, we want it to be distinct from
         * any other source name (chosen by eg the iSCSI
@@ -168,11 +169,21 @@ main(int argc, char **argv)
         * somewhat lame attempt.
         */
        isns_config.ic_source_suffix = "isns";
-
+#endif
        isns_read_config(opt_configfile);
 
+       if (!isns_config.ic_source_name) {
+               /*
+                * Try to read the source name from open-iscsi configuration
+                */
+               isns_read_initiatorname(ISCSI_DEFAULT_INITIATORNAME);
+       }
+
+       isns_init_names();
+
        if (!isns_config.ic_source_name)
                usage(1, "Please specify an iSNS source name");
+
        source = isns_source_create_iscsi(isns_config.ic_source_name);
 
        isns_write_pidfile(isns_config.ic_pidfile);
diff --git a/utils/open-isns/paths.h b/utils/open-isns/paths.h
index b54612c55479..d0deabd3bfaa 100644
--- a/utils/open-isns/paths.h
+++ b/utils/open-isns/paths.h
@@ -19,4 +19,6 @@
 #define ISNS_DEFAULT_ISNSADM_CONFIG    ISNS_ETCDIR "/isnsadm.conf"
 #define ISNS_DEFAULT_LOCAL_REGISTRY    ISNS_RUNDIR "/isns.registry"
 
+#define ISCSI_DEFAULT_INITIATORNAME    "/etc/iscsi/initiatorname.iscsi"
+
 #endif /* ISNS_CONFIG_H */
-- 
2.1.2

-- 
You received this message because you are subscribed to the Google Groups 
"open-iscsi" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to open-iscsi+unsubscr...@googlegroups.com.
To post to this group, send email to open-iscsi@googlegroups.com.
Visit this group at http://groups.google.com/group/open-iscsi.
For more options, visit https://groups.google.com/d/optout.

Reply via email to