Skip to content

Commit 65192d2

Browse files
authored
Merge pull request #588 from amluto/hostnqn
Use a systemd app-specific machine ID for hostnqn
2 parents 863ebca + 48c10db commit 65192d2

6 files changed

Lines changed: 102 additions & 6 deletions

File tree

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
nvme-show-hostnqn(1)
2+
===================
3+
4+
NAME
5+
----
6+
nvme-show-hostnqn - Generate a host NVMe Qualified Name
7+
8+
SYNOPSIS
9+
--------
10+
[verse]
11+
'nvme show-hostnqn'
12+
13+
DESCRIPTION
14+
-----------
15+
Show the host NQN configured for the system. If /etc/nvme/hostnqn is
16+
not present and systemd application-specific machine IDs are available,
17+
this will show the systemd-generated host NQN for the system.
18+
19+
OPTIONS
20+
-------
21+
No options needed
22+
23+
EXAMPLES
24+
--------
25+
nvme show-hostnqn
26+
27+
NVME
28+
----
29+
Part of the nvme-user suite

Makefile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ CFLAGS ?= -O2 -g -Wall -Werror
22
override CFLAGS += -std=gnu99 -I.
33
override CPPFLAGS += -D_GNU_SOURCE -D__CHECK_ENDIAN__
44
LIBUUID = $(shell $(LD) -o /dev/null -luuid >/dev/null 2>&1; echo $$?)
5+
HAVE_SYSTEMD = $(shell pkg-config --exists systemd --atleast-version=232; echo $$?)
56
NVME = nvme
67
INSTALL ?= install
78
DESTDIR =
@@ -22,6 +23,11 @@ endif
2223

2324
INC=-Iutil
2425

26+
ifeq ($(HAVE_SYSTEMD),0)
27+
override LDFLAGS += -lsystemd
28+
override CFLAGS += -DHAVE_SYSTEMD
29+
endif
30+
2531
RPMBUILD = rpmbuild
2632
TAR = tar
2733
RM = rm -f

fabrics.c

Lines changed: 49 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@
4343

4444
#include "common.h"
4545

46+
#ifdef HAVE_SYSTEMD
47+
#include <systemd/sd-id128.h>
48+
#define NVME_HOSTNQN_ID SD_ID128_MAKE(c7,f4,61,81,12,be,49,32,8c,83,10,6f,9d,dd,d8,6b)
49+
#endif
50+
4651
#define NVMF_HOSTID_SIZE 36
4752

4853
static struct config {
@@ -563,11 +568,11 @@ static void save_discovery_log(struct nvmf_disc_rsp_page_hdr *log, int numrec)
563568
close(fd);
564569
}
565570

566-
static int nvmf_hostnqn_file(void)
571+
static char *hostnqn_read_file(void)
567572
{
568573
FILE *f;
569574
char hostnqn[NVMF_NQN_SIZE];
570-
int ret = false;
575+
char *ret = NULL;
571576

572577
f = fopen(PATH_NVMF_HOSTNQN, "r");
573578
if (f == NULL)
@@ -576,16 +581,54 @@ static int nvmf_hostnqn_file(void)
576581
if (fgets(hostnqn, sizeof(hostnqn), f) == NULL)
577582
goto out;
578583

579-
cfg.hostnqn = strndup(hostnqn, strcspn(hostnqn, "\n"));
580-
if (!cfg.hostnqn)
581-
goto out;
584+
ret = strndup(hostnqn, strcspn(hostnqn, "\n"));
582585

583-
ret = true;
584586
out:
585587
fclose(f);
586588
return ret;
587589
}
588590

591+
static char *hostnqn_generate_systemd(void)
592+
{
593+
#ifdef HAVE_SYSTEMD
594+
sd_id128_t id;
595+
char *ret;
596+
597+
if (sd_id128_get_machine_app_specific(NVME_HOSTNQN_ID, &id) < 0)
598+
return NULL;
599+
600+
if (asprintf(&ret, "nqn.2014-08.org.nvmexpress:uuid:" SD_ID128_FORMAT_STR "\n", SD_ID128_FORMAT_VAL(id)) == -1)
601+
ret = NULL;
602+
603+
return ret;
604+
#else
605+
return NULL;
606+
#endif
607+
}
608+
609+
/* returns an allocated string or NULL */
610+
char *hostnqn_read(void)
611+
{
612+
char *ret;
613+
614+
ret = hostnqn_read_file();
615+
if (ret)
616+
return ret;
617+
618+
ret = hostnqn_generate_systemd();
619+
if (ret)
620+
return ret;
621+
622+
return NULL;
623+
}
624+
625+
static int nvmf_hostnqn_file(void)
626+
{
627+
cfg.hostnqn = hostnqn_read();
628+
629+
return cfg.hostnqn != NULL;
630+
}
631+
589632
static int nvmf_hostid_file(void)
590633
{
591634
FILE *f;

fabrics.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
#define NVMF_DEF_DISC_TMO 30
55

6+
extern char *hostnqn_read(void);
7+
68
extern int discover(const char *desc, int argc, char **argv, bool connect);
79
extern int connect(const char *desc, int argc, char **argv);
810
extern int disconnect(const char *desc, int argc, char **argv);

nvme-builtin.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ COMMAND_LIST(
7070
ENTRY("disconnect", "Disconnect from NVMeoF subsystem", disconnect_cmd)
7171
ENTRY("disconnect-all", "Disconnect from all connected NVMeoF subsystems", disconnect_all_cmd)
7272
ENTRY("gen-hostnqn", "Generate NVMeoF host NQN", gen_hostnqn_cmd)
73+
ENTRY("show-hostnqn", "Show NVMeoF host NQN", show_hostnqn_cmd)
7374
ENTRY("dir-receive", "Submit a Directive Receive command, return results", dir_receive)
7475
ENTRY("dir-send", "Submit a Directive Send command, return results", dir_send)
7576
ENTRY("virt-mgmt", "Manage Flexible Resources between Primary and Secondary Controller ", virtual_mgmt)

nvme.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5031,6 +5031,21 @@ static int gen_hostnqn_cmd(int argc, char **argv, struct command *command, struc
50315031
}
50325032
#endif
50335033

5034+
static int show_hostnqn_cmd(int argc, char **argv, struct command *command, struct plugin *plugin)
5035+
{
5036+
char *hostnqn;
5037+
5038+
hostnqn = hostnqn_read();
5039+
if (hostnqn) {
5040+
fputs(hostnqn, stdout);
5041+
free(hostnqn);
5042+
return 0;
5043+
} else {
5044+
fprintf(stderr, "hostnqn is not available -- use nvme gen-hostnqn\n");
5045+
return -ENOENT;
5046+
}
5047+
}
5048+
50345049
static int discover_cmd(int argc, char **argv, struct command *command, struct plugin *plugin)
50355050
{
50365051
const char *desc = "Send Get Log Page request to Discovery Controller.";

0 commit comments

Comments
 (0)