linux-nvme.lists.infradead.org archive mirror
 help / color / mirror / Atom feed
From: Andy Lutomirski <luto@kernel.org>
To: linux-nvme@lists.infradead.org
Cc: Andy Lutomirski <luto@kernel.org>
Subject: [PATCH 3/4] Use systemd-generated hostid if no hostid is configured
Date: Thu, 19 Dec 2019 19:31:04 -0800	[thread overview]
Message-ID: <b4feb87b30edfb30998a2b42de3b1e0618203700.1576726427.git.luto@kernel.org> (raw)
In-Reply-To: <cover.1576726427.git.luto@kernel.org>

This is just like the hostnqn support.  It adds a show-hostid command
for introspection.

Signed-off-by: Andy Lutomirski <luto@kernel.org>
---
 Documentation/nvme-show-hostid.txt | 29 +++++++++++++++
 fabrics.c                          | 57 ++++++++++++++++++++++++++----
 fabrics.h                          |  1 +
 nvme-builtin.h                     |  1 +
 nvme.c                             | 15 ++++++++
 5 files changed, 96 insertions(+), 7 deletions(-)
 create mode 100644 Documentation/nvme-show-hostid.txt

diff --git a/Documentation/nvme-show-hostid.txt b/Documentation/nvme-show-hostid.txt
new file mode 100644
index 000000000000..52bdc8a1f480
--- /dev/null
+++ b/Documentation/nvme-show-hostid.txt
@@ -0,0 +1,29 @@
+nvme-show-hostid(1)
+===================
+
+NAME
+----
+nvme-show-hostid - Generate a host NVMe ID
+
+SYNOPSIS
+--------
+[verse]
+'nvme show-hostid'
+
+DESCRIPTION
+-----------
+Show the host ID configured for the system.  If /etc/nvme/hostid is
+not present and systemd application-specific machine IDs are available,
+this will show the systemd-generated host ID for the system.
+
+OPTIONS
+-------
+No options needed
+
+EXAMPLES
+--------
+nvme show-hostid
+
+NVME
+----
+Part of the nvme-user suite
diff --git a/fabrics.c b/fabrics.c
index 4448416f9855..7c5b7efd5270 100644
--- a/fabrics.c
+++ b/fabrics.c
@@ -46,6 +46,7 @@
 #ifdef HAVE_SYSTEMD
 #include <systemd/sd-id128.h>
 #define NVME_HOSTNQN_ID SD_ID128_MAKE(c7,f4,61,81,12,be,49,32,8c,83,10,6f,9d,dd,d8,6b)
+#define NVME_HOSTID_ID SD_ID128_MAKE(df,66,bf,ec,f7,e4,21,0e,46,27,ac,a8,f2,8f,3b,98)
 #endif
 
 #define NVMF_HOSTID_SIZE	36
@@ -728,11 +729,11 @@ static int nvmf_hostnqn_load(void)
 	return cfg.hostnqn != NULL;
 }
 
-static int nvmf_hostid_file(void)
+static char *hostid_read_file(void)
 {
 	FILE *f;
 	char hostid[NVMF_HOSTID_SIZE + 1];
-	int ret = false;
+	char *ret = NULL;
 
 	f = fopen(PATH_NVMF_HOSTID, "r");
 	if (f == NULL)
@@ -741,16 +742,58 @@ static int nvmf_hostid_file(void)
 	if (fgets(hostid, sizeof(hostid), f) == NULL)
 		goto out;
 
-	cfg.hostid = strdup(hostid);
-	if (!cfg.hostid)
-		goto out;
+	ret = strdup(hostid);
+
 
-	ret = true;
 out:
 	fclose(f);
 	return ret;
 }
 
+static char *hostid_generate_systemd(void)
+{
+#if defined(LIBUUID) && defined(HAVE_SYSTEMD)
+	sd_id128_t id;
+	char uuidstr[37];
+	char *ret;
+
+	if (sd_id128_get_machine_app_specific(NVME_HOSTID_ID, &id) < 0)
+		return NULL;
+
+	uuid_unparse_lower(id.bytes, uuidstr);
+
+	if (asprintf(&ret, "%s\n", uuidstr) == -1)
+		ret = NULL;
+
+	return ret;
+#else
+	return NULL;
+#endif
+}
+
+/* returns an allocated string or NULL */
+char *hostid_read(void)
+{
+	char *ret;
+
+	ret = hostid_read_file();
+	if (ret)
+		return ret;
+
+	ret = hostid_generate_systemd();
+	if (ret)
+		return ret;
+
+	return NULL;
+}
+
+static int nvmf_hostid_load(void)
+{
+	cfg.hostid = hostid_read();
+
+	return cfg.hostid != NULL;
+}
+
 static int
 add_bool_argument(char **argstr, int *max_len, char *arg_str, bool arg)
 {
@@ -829,7 +872,7 @@ static int build_options(char *argstr, int max_len, bool discover)
 	    add_argument(&argstr, &max_len, "trsvcid", cfg.trsvcid) ||
 	    ((cfg.hostnqn || nvmf_hostnqn_load()) &&
 		    add_argument(&argstr, &max_len, "hostnqn", cfg.hostnqn)) ||
-	    ((cfg.hostid || nvmf_hostid_file()) &&
+	    ((cfg.hostid || nvmf_hostid_load()) &&
 		    add_argument(&argstr, &max_len, "hostid", cfg.hostid)) ||
 	    (!discover &&
 	      add_int_argument(&argstr, &max_len, "nr_io_queues",
diff --git a/fabrics.h b/fabrics.h
index b8e53f492b53..64aede897535 100644
--- a/fabrics.h
+++ b/fabrics.h
@@ -4,6 +4,7 @@
 #define NVMF_DEF_DISC_TMO	30
 
 extern char *hostnqn_read(void);
+extern char *hostid_read(void);
 
 extern int discover(const char *desc, int argc, char **argv, bool connect);
 extern int connect(const char *desc, int argc, char **argv);
diff --git a/nvme-builtin.h b/nvme-builtin.h
index bfb907dff9ef..907d470cbeac 100644
--- a/nvme-builtin.h
+++ b/nvme-builtin.h
@@ -71,6 +71,7 @@ COMMAND_LIST(
 	ENTRY("disconnect-all", "Disconnect from all connected NVMeoF subsystems", disconnect_all_cmd)
 	ENTRY("gen-hostnqn", "Generate NVMeoF host NQN", gen_hostnqn_cmd)
 	ENTRY("show-hostnqn", "Show NVMeoF host NQN", show_hostnqn_cmd)
+	ENTRY("show-hostid", "Show NVMeoF host ID", show_hostid_cmd)
 	ENTRY("dir-receive", "Submit a Directive Receive command, return results", dir_receive)
 	ENTRY("dir-send", "Submit a Directive Send command, return results", dir_send)
 	ENTRY("virt-mgmt", "Manage Flexible Resources between Primary and Secondary Controller ", virtual_mgmt)
diff --git a/nvme.c b/nvme.c
index 0c23eee8a477..d81f8226b84d 100644
--- a/nvme.c
+++ b/nvme.c
@@ -4771,6 +4771,21 @@ static int show_hostnqn_cmd(int argc, char **argv, struct command *command, stru
 	}
 }
 
+static int show_hostid_cmd(int argc, char **argv, struct command *command, struct plugin *plugin)
+{
+	char *hostid;
+
+	hostid = hostid_read();
+	if (hostid) {
+		fputs(hostid, stdout);
+		free(hostid);
+		return 0;
+	} else {
+		fprintf(stderr, "hostid is not available -- generate /etc/nvme/hostid\n");
+		return -ENOENT;
+	}
+}
+
 static int discover_cmd(int argc, char **argv, struct command *command, struct plugin *plugin)
 {
 	const char *desc = "Send Get Log Page request to Discovery Controller.";
-- 
2.23.0


_______________________________________________
linux-nvme mailing list
linux-nvme@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-nvme

  parent reply	other threads:[~2019-12-20  3:32 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-12-20  3:31 [PATCH 0/4] systemd ID improvements Andy Lutomirski
2019-12-20  3:31 ` [PATCH 1/4] hostnqn: Fix the systemd-based NQN namespace to comply with the spec Andy Lutomirski
2019-12-20  3:31 ` [PATCH 2/4] fabrics: Rename nvmf_hostnqn_file() to nvmf_hostnqn_load() Andy Lutomirski
2019-12-20  3:31 ` Andy Lutomirski [this message]
2020-01-03 18:14   ` [PATCH 3/4] Use systemd-generated hostid if no hostid is configured David Milburn
2020-01-13 19:14     ` Andy Lutomirski
2019-12-20  3:31 ` [PATCH 4/4] Do not install /etc/nvme/hostid or /etc/nvme/hostnqn by default Andy Lutomirski

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=b4feb87b30edfb30998a2b42de3b1e0618203700.1576726427.git.luto@kernel.org \
    --to=luto@kernel.org \
    --cc=linux-nvme@lists.infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).