]> www.infradead.org Git - users/sagi/nvme-cli.git/commitdiff
Added nvme-list command.
authorStephen Bates <stephen.bates@pmcs.com>
Mon, 9 Feb 2015 04:16:39 +0000 (04:16 +0000)
committerStephen Bates <stephen.bates@pmcs.com>
Mon, 9 Feb 2015 04:16:39 +0000 (04:16 +0000)
Added an new command called nvme-list which uses libudev to look for
NVM Express devices. It prints the /dev node for any detected devices
as well as some other summary information.

For now we use a Makefile hack to alter the build if libudev is not
installed. At a later date we could switch to autoconf and
./configure.

Documentation/nvme-list-ns.1
Documentation/nvme-list.1 [new file with mode: 0644]
Documentation/nvme-list.txt [new file with mode: 0644]
Makefile
nvme.c

index 8879050bb71f26dfe40ce4f9caffb0dc2214c747..0287632a8b7e8dd0f60ef9dd55538ca5d4d834fb 100644 (file)
@@ -2,12 +2,12 @@
 .\"     Title: nvme-id-ns
 .\"    Author: [FIXME: author] [see http://docbook.sf.net/el/author]
 .\" Generator: DocBook XSL Stylesheets v1.76.1 <http://docbook.sf.net/>
-.\"      Date: 02/02/2015
+.\"      Date: 02/09/2015
 .\"    Manual: \ \&
 .\"    Source: \ \&
 .\"  Language: English
 .\"
-.TH "NVME\-ID\-NS" "1" "02/02/2015" "\ \&" "\ \&"
+.TH "NVME\-ID\-NS" "1" "02/09/2015" "\ \&" "\ \&"
 .\" -----------------------------------------------------------------
 .\" * Define some portability stuff
 .\" -----------------------------------------------------------------
diff --git a/Documentation/nvme-list.1 b/Documentation/nvme-list.1
new file mode 100644 (file)
index 0000000..2ecdcbf
--- /dev/null
@@ -0,0 +1,48 @@
+'\" t
+.\"     Title: nvme-list
+.\"    Author: [FIXME: author] [see http://docbook.sf.net/el/author]
+.\" Generator: DocBook XSL Stylesheets v1.76.1 <http://docbook.sf.net/>
+.\"      Date: 02/09/2015
+.\"    Manual: \ \&
+.\"    Source: \ \&
+.\"  Language: English
+.\"
+.TH "NVME\-LIST" "1" "02/09/2015" "\ \&" "\ \&"
+.\" -----------------------------------------------------------------
+.\" * Define some portability stuff
+.\" -----------------------------------------------------------------
+.\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+.\" http://bugs.debian.org/507673
+.\" http://lists.gnu.org/archive/html/groff/2009-02/msg00013.html
+.\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+.ie \n(.g .ds Aq \(aq
+.el       .ds Aq '
+.\" -----------------------------------------------------------------
+.\" * set default formatting
+.\" -----------------------------------------------------------------
+.\" disable hyphenation
+.nh
+.\" disable justification (adjust text to left margin only)
+.ad l
+.\" -----------------------------------------------------------------
+.\" * MAIN CONTENT STARTS HERE *
+.\" -----------------------------------------------------------------
+.SH "NAME"
+nvme-list \- List all recognized NVMe devices
+.SH "SYNOPSIS"
+.sp
+.nf
+\fInvme list\fR
+.fi
+.SH "DESCRIPTION"
+.sp
+Scan the sysfs tree for NVM Express devices and return the /dev node for those devices as well as some pertinant information about them\&.
+.SH "OPTIONS"
+.sp
+No options yet\&.
+.SH "EXAMPLES"
+.sp
+No examples yet\&.
+.SH "NVME"
+.sp
+Part of the nvme\-user suite
diff --git a/Documentation/nvme-list.txt b/Documentation/nvme-list.txt
new file mode 100644 (file)
index 0000000..edb77b7
--- /dev/null
@@ -0,0 +1,28 @@
+nvme-list(1)
+============
+
+NAME
+----
+nvme-list - List all recognized NVMe devices
+
+SYNOPSIS
+--------
+[verse]
+'nvme list' 
+
+DESCRIPTION
+-----------
+Scan the sysfs tree for NVM Express devices and return the /dev node
+for those devices as well as some pertinant information about them.
+
+OPTIONS
+-------
+No options yet.
+
+EXAMPLES
+--------
+No examples yet.
+
+NVME
+----
+Part of the nvme-user suite
index 17ad324b80692ccc9e3f77307b0ba28de1f77e01..78d70767a39df6025f34e3d51ee35b9c348976f7 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -3,6 +3,12 @@ LDFLAGS := -m64 -lm
 NVME = nvme
 INSTALL ?= install
 
+LIBUDEV:=$(shell ld -ludev > /dev/null 2>&1 ; echo $$?)
+ifeq ($(LIBUDEV),0)
+       LDFLAGS += -ludev
+       CFLAGS  += -DLIBUDEV_EXISTS
+endif
+
 default: $(NVME)
 
 doc: $(NVME)
@@ -21,3 +27,6 @@ install: default
        $(INSTALL) -m 755 nvme /usr/local/bin
 
 .PHONY: default all doc clean clobber install
+
+test:
+       @echo $(LIBUDEV)
\ No newline at end of file
diff --git a/nvme.c b/nvme.c
index d88027c5a82abcf81ec636dec96ba3b06723d34f..7415d7ba1de37f5ebeb160680cb97ebc6fb1da65 100644 (file)
--- a/nvme.c
+++ b/nvme.c
@@ -30,6 +30,9 @@
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
+#ifdef LIBUDEV_EXISTS
+#include <libudev.h>
+#endif
 
 #include <linux/fs.h>
 
@@ -45,6 +48,7 @@ static struct stat nvme_stat;
 static const char *devicename;
 
 #define COMMAND_LIST \
+       ENTRY(LIST, "list", "List all NVMe devices and namespaces on machine", list) \
        ENTRY(ID_CTRL, "id-ctrl", "Send NVMe Identify Controller", id_ctrl) \
        ENTRY(ID_NS, "id-ns", "Send NVMe Identify Namespace, display structure", id_ns) \
        ENTRY(LIST_NS, "list-ns", "Send NVMe Identify List, display structure", list_ns) \
@@ -757,6 +761,57 @@ static int list_ns(int argc, char **argv)
        return err;
 }
 
+#ifndef LIBUDEV_EXISTS
+static int list(int argc, char **argv)
+{
+  fprintf(stderr,"nvme-list: libudev not detected, install and rebuild.\n");
+  return -1;
+}
+#endif
+
+#ifdef LIBUDEV_EXISTS
+static int list(int argc, char **argv)
+{
+  struct udev *udev;
+  struct udev_enumerate *enumerate;
+  struct udev_list_entry *devices, *dev_list_entry;
+  struct udev_device *dev;
+  
+  udev = udev_new();
+  if (!udev) {
+    perror("nvme-list: Can not create udev context.");
+    return errno;
+  }
+  
+  enumerate = udev_enumerate_new(udev);
+  udev_enumerate_add_match_subsystem(enumerate, "block");
+  udev_enumerate_scan_devices(enumerate);
+  devices = udev_enumerate_get_list_entry(enumerate);
+  udev_list_entry_foreach(dev_list_entry, devices) {
+
+    const char *path, *node;
+    path = udev_list_entry_get_name(dev_list_entry);
+    dev = udev_device_new_from_syspath(udev, path);
+    node = udev_device_get_devnode(dev);
+    if (strstr(node,"nvme")!=NULL){
+      struct nvme_id_ctrl ctrl;
+
+      open_dev(node);
+      int err = identify(0, &ctrl, 1);
+      if (err > 0)
+       return err;
+      printf("  %s\t: NVM Express - %#x - %s - %x\n", node, 
+            ctrl.vid, ctrl.mn, ctrl.ver);
+    }
+
+  }
+  udev_enumerate_unref(enumerate);
+  udev_unref(udev);
+
+  return 0;
+}
+#endif
+
 static int id_ctrl(int argc, char **argv)
 {
        int opt, err, raw = 0, vs = 0, long_index = 0;