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
4853static 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;
584586out :
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+
589632static int nvmf_hostid_file (void )
590633{
591634 FILE * f ;
0 commit comments