[systemd-devel] 60-persistent-storage.rules: add NVMe disks and partitions (again)

Per Bergqvist per at bst.lu
Fri May 15 11:53:06 PDT 2015


Hej,

Please (finally) find a patched for v219 to add a nvme_id utility and add support for NVMe disks in 60-persistent-storage.rules. 

BR
Per

---
Makefile.am                       |  11 +++
rules/60-persistent-storage.rules |   5 ++
src/udev/nvme_id/nvme_id.c        | 155 ++++++++++++++++++++++++++++++++++++++
3 files changed, 171 insertions(+)
create mode 100644 src/udev/nvme_id/nvme_id.c

diff --git a/Makefile.am b/Makefile.am
index bf04d31..0de0d18 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -3904,6 +3904,17 @@ dist_udevrules_DATA += \
	rules/60-persistent-v4l.rules

# ------------------------------------------------------------------------------
+nvme_id_SOURCES = \
+	src/udev/nvme_id/nvme_id.c
+
+nvme_id_LDADD = \
+	libudev-internal.la \
+	libsystemd-shared.la
+
+udevlibexec_PROGRAMS += \
+	nvme_id
+
+# ------------------------------------------------------------------------------
accelerometer_SOURCES = \
	src/udev/accelerometer/accelerometer.c

diff --git a/rules/60-persistent-storage.rules b/rules/60-persistent-storage.rules
index 25b44a5..d3368a5 100644
--- a/rules/60-persistent-storage.rules
+++ b/rules/60-persistent-storage.rules
@@ -42,6 +42,11 @@ KERNEL=="cciss*", ENV{DEVTYPE}=="disk", ENV{ID_SERIAL}!="?*", IMPORT{program}="s
KERNEL=="sd*|sr*|cciss*", ENV{DEVTYPE}=="disk", ENV{ID_SERIAL}=="?*", SYMLINK+="disk/by-id/$env{ID_BUS}-$env{ID_SERIAL}"
KERNEL=="sd*|cciss*", ENV{DEVTYPE}=="partition", ENV{ID_SERIAL}=="?*", SYMLINK+="disk/by-id/$env{ID_BUS}-$env{ID_SERIAL}-part%n"

+# NVMe
+KERNEL=="nvme*", ENV{ID_SERIAL}!="?*", IMPORT{program}="nvme_id --export $devnode"
+KERNEL=="nvme*", ENV{DEVTYPE}=="disk", ENV{ID_SERIAL}=="?*", SYMLINK+="disk/by-id/nvme-$env{ID_SERIAL}"
+KERNEL=="nvme*", ENV{DEVTYPE}=="partition", ENV{ID_SERIAL}=="?*", SYMLINK+="disk/by-id/nvme-$env{ID_SERIAL}-part%n"
+
# firewire
KERNEL=="sd*[!0-9]|sr*", ATTRS{ieee1394_id}=="?*", SYMLINK+="disk/by-id/ieee1394-$attr{ieee1394_id}"
KERNEL=="sd*[0-9]", ATTRS{ieee1394_id}=="?*", SYMLINK+="disk/by-id/ieee1394-$attr{ieee1394_id}-part%n"
diff --git a/src/udev/nvme_id/nvme_id.c b/src/udev/nvme_id/nvme_id.c
new file mode 100644
index 0000000..aa0728b
--- /dev/null
+++ b/src/udev/nvme_id/nvme_id.c
@@ -0,0 +1,155 @@
+/*
+ * nvme_id - reads model/serial/firmware revision numbers from nvme drives
+ *
+ * Copyright (C) 2015 Per Bergqvist <per at bst.lu>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdint.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <ctype.h>
+#include <string.h>
+#include <errno.h>
+#include <getopt.h>
+#include <linux/nvme.h>
+#include <sys/ioctl.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+
+#include "libudev.h"
+#include "libudev-private.h"
+#include "udev-util.h"
+#include "log.h"
+
+static int nvme_identify(struct udev *udev,
+                         int fd,
+                         void *ptr)
+{
+	struct nvme_admin_cmd command;
+	memset(&command, 0, sizeof(command));
+	
+	command.opcode = nvme_admin_identify;
+	command.nsid = 0;
+	command.addr = (unsigned long)ptr;
+	command.data_len = sizeof(struct nvme_id_ctrl);
+        command.cdw10 = 1;
+
+        return ioctl(fd, NVME_IOCTL_ADMIN_CMD, &command);
+}
+
+int main(int argc, char *argv[])
+{
+        _cleanup_udev_unref_ struct udev *udev = NULL;
+
+	struct nvme_id_ctrl nvme_ctrl;
+	
+        char model[41];
+        char model_enc[256];
+        char serial[21];
+        char revision[9];
+	
+        const char *node = NULL;
+        int export = 0;
+	
+        _cleanup_close_ int fd = -1;
+
+        static const struct option options[] = {
+                { "export", no_argument, NULL, 'x' },
+                { "help", no_argument, NULL, 'h' },
+                {}
+        };
+
+        log_parse_environment();
+        log_open();
+
+        udev = udev_new();
+        if (udev == NULL)
+                return 0;
+
+        while (1) {
+                int option;
+
+                option = getopt_long(argc, argv, "xh", options, NULL);
+                if (option == -1)
+                        break;
+
+                switch (option) {
+                case 'x':
+                        export = 1;
+                        break;
+                case 'h':
+                        printf("Usage: nvme_id [--export] [--help] <device>\n"
+                               "  -x,--export    print values as environment keys\n"
+                               "  -h,--help      print this help text\n\n");
+                        return 0;
+                }
+        }
+
+        node = argv[optind];
+        if (node == NULL) {
+                log_error("no node specified");
+                return 1;
+        }
+
+        fd = open(node, O_RDONLY|O_NONBLOCK|O_CLOEXEC);
+        if (fd < 0) {
+                log_error("unable to open '%s'", node);
+                return 1;
+        }
+
+	memset(&nvme_ctrl, 0, sizeof(struct nvme_id_ctrl));
+	
+        if (nvme_identify(udev, fd, &nvme_ctrl) == 0) {
+	       int i;
+	       memcpy (model, nvme_ctrl.mn, 40);
+	       for(i=39;i>=0;i--) if (model[i]== ' ') model[i] = '\0'; else break;
+	       model[40] = '\0';
+	       udev_util_encode_string(model, model_enc, sizeof(model_enc));
+	       util_replace_whitespace((char *) nvme_ctrl.mn, model, 40);
+	       util_replace_chars(model, NULL);
+	       util_replace_whitespace((char *) nvme_ctrl.sn, serial, 20);
+	       util_replace_chars(serial, NULL);
+	       util_replace_whitespace((char *) nvme_ctrl.fr, revision, 8);
+	       util_replace_chars(revision, NULL);
+        } else {
+	       log_debug_errno(errno, "nvme_identify failed for '%s': %m", node);
+	       return 2;
+        }
+
+
+        if (export) {
+                printf("ID_TYPE=nvme\n");
+                printf("ID_MODEL=%s\n", model);
+                printf("ID_MODEL_ENC=%s\n", model_enc);
+                printf("ID_REVISION=%s\n", revision);
+                if (serial[0] != '\0') {
+                        printf("ID_SERIAL=%s_%s\n", model, serial);
+                        printf("ID_SERIAL_SHORT=%s\n", serial);
+                } else {
+                        printf("ID_SERIAL=%s\n", model);
+                }
+
+        } else {
+                if (serial[0] != '\0')
+                        printf("%s_%s\n", model, serial);
+                else
+                        printf("%s\n", model);
+        }
+
+        return 0;
+}
-- 
2.4.0







More information about the systemd-devel mailing list