Skip to contentSkip to conversation

auto generate hostnqn file on installation

PATCH Superseded Sagi Grimberg 4 participants 7 messages Nov 8, 2016 – Nov 16, 2016

Subsystems (2)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Sagi Grimberg

author maintainer
#0 lore raw

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 v0 (rfc):
- Moved hostnqn generation to nvme with the command gen-hostnqn
- Cleanup hostnqn file only on purge in debian

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

 Makefile        |  1 +
 debian/postinst | 20 ++++++++++++++++++++
 debian/postrm   |  5 +++++
 nvme-builtin.h  |  1 +
 nvme.c          | 14 ++++++++++++++
 nvme.spec.in    | 12 ++++++++++++
 6 files changed, 53 insertions(+)
 create mode 100644 debian/postinst
 create mode 100644 debian/postrm
Signature · 1 line
2.7.4
author maintainer
#1 lore raw

PATCH 1/2 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>
---
 Makefile       |  1 +
 nvme-builtin.h |  1 +
 nvme.c         | 14 ++++++++++++++
 3 files changed, 16 insertions(+)
Makefile +1 -0
index 117cbbea886f..cc0245ad576d 100644
@@ -1,5 +1,6 @@
 CFLAGS += -std=gnu99 -O2 -g -Wall -Werror
 CPPFLAGS += -D_GNU_SOURCE -D__CHECK_ENDIAN__
+LDFLAGS += -luuid
 NVME = nvme
 INSTALL ?= install
 DESTDIR =
nvme-builtin.h +1 -0
index dc314cbbb371..b182b1e01f5b 100644
@@ -49,6 +49,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 +14 -0
index a9e78aae50a9..fa9765cddc79 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"
@@ -2704,6 +2705,19 @@ 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)
+{
+	char hostnqn[NVMF_NQN_SIZE];
+	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);
+	sprintf(hostnqn, "nqn.2014-08.org.nvmexpress:NVMf:uuid:%s", uuid_str);
+	printf("%s\n", hostnqn);
+	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
#2 lore raw

PATCH 1/2 nvme-cli: Add nvme hostnqn generation option

Quoted 67 lines · Sagi Grimberg

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>

---

?Makefile???????|??1 +
?nvme-builtin.h |??1 +
?nvme.c?????????| 14 ++++++++++++++

?3 files changed, 16 insertions(+)

Makefile +0 -0
index 117cbbea886f..cc0245ad576d 100644
@@ -1,5 +1,6 @@

?CFLAGS += -std=gnu99 -O2 -g -Wall -Werror
?CPPFLAGS += -D_GNU_SOURCE -D__CHECK_ENDIAN__
+LDFLAGS += -luuid
?NVME = nvme
?INSTALL ?= install
?DESTDIR =

nvme-builtin.h +0 -0
index dc314cbbb371..b182b1e01f5b 100644
@@ -49,6 +49,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)

nvme-builtin.h +1 -0
+	ENTRY("gen-hostnqn", "Generate NVMeoF host NQN",

gen_hostnqn_cmd)
?);
?
?#endif

nvme.c +0 -0
index a9e78aae50a9..fa9765cddc79 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"

nvme.c +0 -0
@@ -2704,6 +2705,19 @@ 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)

nvme.c +4 -0
+{
+	char hostnqn[NVMF_NQN_SIZE];
+	uuid_t uuid;
+	char uuid_str[37]; /* e.g. 1b4e28ba-2fa1-11d2-883f-

0016d3cca427 + \0 */

nvme.c +4 -0
+
+	uuid_generate_random(uuid);
+	uuid_unparse_lower(uuid, uuid_str);
+	sprintf(hostnqn, "nqn.2014-08.org.nvmexpress:NVMf:uuid:%s",
Quoted 1 line

uuid_str);

Let's do what the driver does, use a snprintf() and limit the size via NVMF_NQN_SIZE used by hostnqn[].

#3 lore raw

PATCH 1/2 nvme-cli: Add nvme hostnqn generation option

In reply to Sagi Grimberg
Quoted 3 lines · Sagi Grimberg

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.

Keith - should we have any special prefixes for nvme-cli commands that don't map to NVMe commands?

nvme.c static int admin_passthru(int argc, char **argv, struct command *cmd, struct plu · line 2717

Quoted 10 lines
nvme.c +10 -0
+static int gen_hostnqn_cmd(int argc, char **argv, struct command *command, struct plugin *plugin)
+{
+	char hostnqn[NVMF_NQN_SIZE];
+	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);
+	sprintf(hostnqn, "nqn.2014-08.org.nvmexpress:NVMf:uuid:%s", uuid_str);
+	printf("%s\n", hostnqn);

Shouldn't the sprintf and printf get merged into one? No need for the local variable here. That would also take care of the snprintf nitpick from Jay ;-)

author maintainer
#4 lore raw

PATCH 2/2 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>
---
 debian/postinst | 20 ++++++++++++++++++++
 debian/postrm   |  5 +++++
 nvme.spec.in    | 12 ++++++++++++
 3 files changed, 37 insertions(+)
 create mode 100644 debian/postinst
 create mode 100644 debian/postrm
debian/postinst new file +20 -0
new file mode 100644
index 000000000000..b258cf569c3e
@@ -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 +5 -0
new file mode 100644
index 000000000000..d678fc8a90ac
@@ -0,0 +1,5 @@
+#!/bin/sh
+
+if [ "$1" = "purge" ]; then
+    rm -rf /etc/nvme
+fi
nvme.spec.in +12 -0
index a4718773a962..8bda97d819ba 100644
@@ -33,6 +33,18 @@ 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
+    rm -rf /etc/nvme
+fi
 %changelog
 * Thu Oct 15 2015 Keith Busch <keith.busch at intel.com>
 - Initial RPM spec
Signature · 1 line
2.7.4
#5 lore raw

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

Quoted 19 lines · 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>

---

?debian/postinst | 20 ++++++++++++++++++++
?debian/postrm???|??5 +++++
?nvme.spec.in????| 12 ++++++++++++

?3 files changed, 37 insertions(+)
?create mode 100644 debian/postinst
?create mode 100644 debian/postrm

debian/postinst new file +0 -0
new file mode 100644
index 000000000000..b258cf569c3e

debian/postinst line 8

Quoted 8 lines
debian/postinst new file +8 -0
@@ -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

Would it just be easier to do "mkdir -p /etc/nvme/hostnqn"? ?

Quoted 34 lines
debian/postinst new file +12 -0
+		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 +5 -0
new file mode 100644
index 000000000000..d678fc8a90ac
@@ -0,0 +1,5 @@
+#!/bin/sh
+
+if [ "$1" = "purge" ]; then
+????rm -rf /etc/nvme
+fi
nvme.spec.in +0 -0
index a4718773a962..8bda97d819ba 100644
@@ -33,6 +33,18 @@ 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

nvme.spec.in make install DESTDIR=%{buildroot} PREFIX=/usr

Quoted 9 lines
nvme.spec.in +9 -0
+	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
+????rm -rf /etc/nvme

This could remove a 'discovery.conf' file that is also located in /etc/nvme, which would be bad. ?Let's just remove the file /ete/nvme/hostnqn that this will generate.

#6 lore raw

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

In reply to Sagi Grimberg
Quoted 45 lines · 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>
---
 debian/postinst | 20 ++++++++++++++++++++
 debian/postrm   |  5 +++++
 nvme.spec.in    | 12 ++++++++++++
 3 files changed, 37 insertions(+)
 create mode 100644 debian/postinst
 create mode 100644 debian/postrm
debian/postinst new file +20 -0
new file mode 100644
index 000000000000..b258cf569c3e
@@ -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 +0 -0
new file mode 100644
index 000000000000..d678fc8a90ac

debian/postrm line 5

Quoted 5 lines
debian/postrm new file +5 -0
@@ -0,0 +1,5 @@
+#!/bin/sh
+
+if [ "$1" = "purge" ]; then
+    rm -rf /etc/nvme
+fi

Oh, here it is. Didn't see your v1 before responding to the rfc. :)

Quoted 23 lines
nvme.spec.in +12 -0
index a4718773a962..8bda97d819ba 100644
@@ -33,6 +33,18 @@ 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
+    rm -rf /etc/nvme
+fi
 %changelog
 * Thu Oct 15 2015 Keith Busch <keith.busch at intel.com>
 - Initial RPM spec
Signature · 1 line
Gabriel Krisman Bertazi