From: Stephen Bates Date: Mon, 9 Feb 2015 04:16:39 +0000 (+0000) Subject: Added nvme-list command. X-Git-Tag: v0.1~71 X-Git-Url: https://www.infradead.org/git/?a=commitdiff_plain;h=d4919cc4764ffb4d0d4d8957d862f6560f145214;p=users%2Fsagi%2Fnvme-cli.git Added nvme-list command. 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. --- diff --git a/Documentation/nvme-list-ns.1 b/Documentation/nvme-list-ns.1 index 8879050b..0287632a 100644 --- a/Documentation/nvme-list-ns.1 +++ b/Documentation/nvme-list-ns.1 @@ -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 -.\" 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 index 00000000..2ecdcbfe --- /dev/null +++ b/Documentation/nvme-list.1 @@ -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 +.\" 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 index 00000000..edb77b7f --- /dev/null +++ b/Documentation/nvme-list.txt @@ -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 diff --git a/Makefile b/Makefile index 17ad324b..78d70767 100644 --- 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 d88027c5..7415d7ba 100644 --- a/nvme.c +++ b/nvme.c @@ -30,6 +30,9 @@ #include #include #include +#ifdef LIBUDEV_EXISTS +#include +#endif #include @@ -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;