Skip to contentSkip to conversation

auto generate hostnqn file on installation

PATCH New Sagi Grimberg 2 participants 5 messages Jan 12, 2017 – Jan 18, 2017

Subsystems (3)
lore
Share & tools
lore

Sagi Grimberg

author maintainer
#0 lore raw

v3 cover auto generate hostnqn file on installation

This set is designed to help the user by auto-generating a hostnqn and placing it in /etc/nvme/hostnqn file upon installation of nvme-cli (in case such a file doesn't exist).

The user can re-generate different hostnqns on demand by running:

$ nvme gen-hostnqn

Only the deb package is tested.

Changes from v2:
- fixed bash style
- added review tags
- added a documentation entry

Changes from v1:
- don't keep local hostnqn var for sprintf
- don't delete /etc/nvme if it contains other files other than what we installed (hostnqn)

Changes from v0 (rfc):
- Moved hostnqn generation to nvme with the command gen-hostnqn
- Cleanup hostnqn file only on purge in debian

Sagi Grimberg (3):
nvme-cli: Add nvme hostnqn generation option nvme.spec/debian: Auto generate host nqn as part of install
Documentation: Add entry for gen-hostnqn

 Documentation/nvme-gen-hostnqn.txt | 29 +++++++++++++++++++++++++++++
 Makefile                           |  1 +
 debian/postinst                    | 20 ++++++++++++++++++++
 debian/postrm                      | 10 ++++++++++
 nvme-builtin.h                     |  1 +
 nvme.c                             | 12 ++++++++++++
 nvme.spec.in                       | 17 +++++++++++++++++
 7 files changed, 90 insertions(+)
 create mode 100644 Documentation/nvme-gen-hostnqn.txt
 create mode 100644 debian/postinst
 create mode 100644 debian/postrm
Signature · 1 line
2.7.4
author maintainer
#1 lore raw

PATCH v3 1/3 nvme-cli: Add nvme hostnqn generation option

Add option to generate a NVMe qualified name of a given host (in the form of: nqn.2014-08.org.nvmexpress:NVMf:uuid:<some_uuid>). This hostnqn will be used for fabrics discovery and connect functions.

Signed-off-by Sagi Grimberg <sagi at grimberg.me>
Reviewed-by Christoph Hellwig <hch at lst.de>
Reviewed-by Jay Freyensee <james_p_freyensee at linux.intel.com>
---
 Makefile       |  1 +
 nvme-builtin.h |  1 +
 nvme.c         | 12 ++++++++++++
 3 files changed, 14 insertions(+)
Makefile +1 -0
index a8acd3fccab0..cb83001a7006 100644
@@ -1,6 +1,7 @@
 CFLAGS ?= -O2 -g -Wall -Werror
 CFLAGS += -std=gnu99
 CPPFLAGS += -D_GNU_SOURCE -D__CHECK_ENDIAN__
+LDFLAGS += -luuid
 NVME = nvme
 INSTALL ?= install
 DESTDIR =
nvme-builtin.h +1 -0
index 5a26f6926595..087fc1986b2f 100644
@@ -48,6 +48,7 @@ COMMAND_LIST(
 	ENTRY("connect-all", "Discover and Connect to NVMeoF subsystems", connect_all_cmd)
 	ENTRY("connect", "Connect to NVMeoF subsystem", connect_cmd)
 	ENTRY("disconnect", "Disconnect from NVMeoF subsystem", disconnect_cmd)
+	ENTRY("gen-hostnqn", "Generate NVMeoF host NQN", gen_hostnqn_cmd)
 );
 
 #endif
nvme.c +12 -0
index b97109b316ba..24fdce9593bc 100644
@@ -44,6 +44,7 @@
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <sys/time.h>
+#include <uuid/uuid.h>
 
 #include "nvme-print.h"
 #include "nvme-ioctl.h"
@@ -2671,6 +2672,17 @@ static int admin_passthru(int argc, char **argv, struct command *cmd, struct plu
 	return passthru(argc, argv, NVME_IOCTL_ADMIN_CMD, desc, cmd);
 }
 
+static int gen_hostnqn_cmd(int argc, char **argv, struct command *command, struct plugin *plugin)
+{
+	uuid_t uuid;
+	char uuid_str[37]; /* e.g. 1b4e28ba-2fa1-11d2-883f-0016d3cca427 + \0 */
+
+	uuid_generate_random(uuid);
+	uuid_unparse_lower(uuid, uuid_str);
+	printf("nqn.2014-08.org.nvmexpress:NVMf:uuid:%s\n", uuid_str);
+	return 0;
+}
+
 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.";
Signature · 1 line
2.7.4
author maintainer
#2 lore raw

PATCH v3 2/3 nvme.spec/debian: Auto generate host nqn as part of install

In reply to Sagi Grimberg

The installation will generate a hostnqn and store it in /etc/nvme/hostnqn file (in case it doesn't exist).
This file will be removed upon uninstallation (purge on for debian).

Signed-off-by Sagi Grimberg <sagi at grimberg.me>
Reviewed-by Christoph Hellwig <hch at lst.de>
Reviewed-by Jay Freyensee <james_p_freyensee at linux.intel.com>
---
 debian/postinst | 20 ++++++++++++++++++++
 debian/postrm   | 10 ++++++++++
 nvme.spec.in    | 17 +++++++++++++++++
 3 files changed, 47 insertions(+)
 create mode 100644 debian/postinst
 create mode 100644 debian/postrm
debian/postinst new file +20 -0
new file mode 100644
index 000000000000..cd497b7af90b
@@ -0,0 +1,20 @@
+#!/bin/sh
+
+set -e
+
+case "$1" in
+    configure|install)
+	if [ ! -f /etc/nvme/hostnqn ]; then
+		install -D /dev/null /etc/nvme/hostnqn
+		echo `nvme gen-hostnqn` > /etc/nvme/hostnqn
+        fi
+        ;;
+
+    upgrade|abort-upgrade)
+        ;;
+    *)
+        echo "postinst called with unknown argument \`$1'" >&2
+        exit 0
+        ;;
+esac
+exit 0
debian/postrm new file +10 -0
new file mode 100644
index 000000000000..c655ee47004a
@@ -0,0 +1,10 @@
+#!/bin/sh
+
+if [ "$1" = "purge" ]; then
+	if [ -d /etc/nvme ]; then
+		rm -f /etc/nvme/hostnqn
+		if [ ! -n "`ls -A /etc/nvme`" ]; then
+			rm -rf /etc/nvme
+		fi
+	fi
+fi
nvme.spec.in +17 -0
index a4718773a962..36c3b2141641 100644
@@ -33,6 +33,23 @@ make install DESTDIR=%{buildroot} PREFIX=/usr
 %clean
 rm -rf $RPM_BUILD_ROOT
 
+%post
+if [ $1 = 1 ]; then # 1 : This package is being installed for the first time
+	if [ ! -f /etc/nvme/hostnqn ]; then
+		install -D /dev/null /etc/nvme/hostnqn
+		echo $(nvme gen-hostnqn) > /etc/nvme/hostnqn
+        fi
+fi
+
+%preun
+if [ "$1" = "remove" ]; then
+	if [ -d /etc/nvme ]; then
+		rm -f /etc/nvme/hostnqn
+		if [ ! -n "`ls -A /etc/nvme`" ]; then
+			rm -rf /etc/nvme
+		fi
+	fi
+fi
 %changelog
 * Thu Oct 15 2015 Keith Busch <keith.busch at intel.com>
 - Initial RPM spec
Signature · 1 line
2.7.4
author maintainer
#3 lore raw

PATCH v3 3/3 Documentation: Add entry for gen-hostnqn

In reply to Sagi Grimberg
Signed-off-by Sagi Grimberg <sagi at grimberg.me>
---
 Documentation/nvme-gen-hostnqn.txt | 29 +++++++++++++++++++++++++++++
 1 file changed, 29 insertions(+)
 create mode 100644 Documentation/nvme-gen-hostnqn.txt
Documentation/nvme-gen-hostnqn.txt new file +29 -0
new file mode 100644
index 000000000000..6d5346face5c
@@ -0,0 +1,29 @@
+nvme-gen-hostnqn(1)
+===================
+
+NAME
+----
+nvme-gen-hostnqn - Generate a host NVMe Qualified Name
+
+SYNOPSIS
+--------
+[verse]
+'nvme gen-hostnqn'
+
+DESCRIPTION
+-----------
+Generate a random host NQN in the form:
+nqn.2014-08.org.nvmexpress:NVMf:uuid:1b4e28ba-2fa1-11d2-883f-0016d3cca427
+and prints it to stdout.
+
+OPTIONS
+-------
+No options needed
+
+EXAMPLES
+--------
+nvme gen-hostnqn
+
+NVME
+----
+Part of the nvme-user suite
Signature · 1 line
2.7.4
#4 lore raw

v3 cover auto generate hostnqn file on installation

In reply to Sagi Grimberg
Quoted 6 lines · Sagi Grimberg

This set is designed to help the user by auto-generating a hostnqn and placing it in /etc/nvme/hostnqn file upon installation of nvme-cli (in case such a file doesn't exist).

The user can re-generate different hostnqns on demand by running:

$ nvme gen-hostnqn

Thanks, I've applied the series and regenerated the documentation to create the man page you've added.