[PATCH 1/3] Add virtioblk_id tool to extract drive serial numbers

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Use the 'VBID' virtio-blk ioctl to extract drive serial numbers
to be used for building disk/by-id symlinks.  After extracting
the serial number of the device it prints out the minimum info
needed in a similar format to `scsi_id --export` so that the
persistent-storage rules can process the serial information.

This program depends on the virtio-blk serial device patches posted
here[1] being applied to qemu and linux-kernel.

Here is what the output looks like:

% ./virtioblk_id /dev/vdb
ID_VIRTIO=1
ID_TYPE=disk
ID_VIRTIO_SERIAL=QM00001

1. http://lists.gnu.org/archive/html/qemu-devel/2010-03/msg01869.html

Signed-off-by: Ryan Harper <ryanh@xxxxxxxxxx>
---
 Makefile.am                        |    7 +++
 extras/virtioblk_id/virtioblk_id.c |   81 ++++++++++++++++++++++++++++++++++++
 2 files changed, 88 insertions(+), 0 deletions(-)
 create mode 100644 extras/virtioblk_id/virtioblk_id.c

diff --git a/Makefile.am b/Makefile.am
index b0eeaf1..d2d3036 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -250,6 +250,13 @@ extras_path_id_path_id_LDADD = libudev/libudev-private.la
 libexec_PROGRAMS += extras/path_id/path_id
 
 # ------------------------------------------------------------------------------
+# virtioblk_id - virtioblk inquery to extract disk serial numbers
+# ------------------------------------------------------------------------------
+extras_virtioblk_id_virtioblk_id_SOURCES = extras/virtioblk_id/virtioblk_id.c
+extras_virtioblk_id_virtioblk_id_LDADD = libudev/libudev-private.la
+libexec_PROGRAMS += extras/virtioblk_id/virtioblk_id
+
+# ------------------------------------------------------------------------------
 # fstab_import - import /etc/fstab entry for block device
 # ------------------------------------------------------------------------------
 extras_fstab_import_fstab_import_SOURCES = extras/fstab_import/fstab_import.c
diff --git a/extras/virtioblk_id/virtioblk_id.c b/extras/virtioblk_id/virtioblk_id.c
new file mode 100644
index 0000000..81afd03
--- /dev/null
+++ b/extras/virtioblk_id/virtioblk_id.c
@@ -0,0 +1,81 @@
+/*
+ * extract disk serial from virtio-blk devices
+ * and print enough values to allow udev to create disk/by-id/ symlinks
+ *
+ * Copyright IBM, Corp. 2010
+ * 
+ * Authors:
+ *  John Cooper <john.cooper@xxxxxxxxxx>
+ *  Ryan Harper <ryanh@xxxxxxxxxx>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2. See
+ * the COPYING file in the top-level directory.
+ */
+
+#include <stdio.h>
+#include <strings.h>
+#include <sys/types.h>
+#include <fcntl.h>
+#include <linux/hdreg.h>
+#include <getopt.h>
+
+#define IOCTL_CMD "VBID"
+
+int main(int argc, char **argv)
+{
+    int fd, rv;
+    const char *device;
+    char buf[255];
+
+    int opt_debug = 0;
+    static const char *option_string = "d";
+    static struct option options[] = {
+        {"debug", 0, NULL, 'd'},
+        {"help", 0, NULL, 'h'},
+        {0, 0, NULL, 0}
+    };
+
+    while (1) {
+        int option;
+        option = getopt_long(argc, argv, "dh", options, NULL);
+        if (option == -1)
+            break;
+        switch (option) {
+        case 'd':
+            opt_debug = 1;
+            break;
+        case 'h':
+            printf("Usage: virtioblk_id [--debug] [--help] <device>\n"
+                   "  --debug           print debugging information\n"
+                   "  --help            print this help text\n\n");
+        default:
+            rv = 1;
+            goto exit;
+        }
+    }
+
+    device = argv[optind];
+    if (device == NULL) {
+        fprintf(stderr, "no device specified\n");
+        rv = 1;
+        goto exit;
+    }
+
+
+    bzero(buf, sizeof (buf));
+    if ((fd = open(device, O_RDONLY)) < 0) {
+        if (opt_debug == 1) 
+            perror("open");
+    } else if ((rv = ioctl(fd, IOCTL_CMD, buf)) < 0) {
+        if (opt_debug == 1) 
+            perror("ioctl");
+    } else {
+        printf("ID_VIRTIO=1\n");
+        printf("ID_TYPE=disk\n");
+        printf("ID_VIRTIO_SERIAL=%s\n", buf);
+        rv = 0;
+    }
+
+ exit:
+    return rv;
+}
-- 
1.6.3.3

--
To unsubscribe from this list: send the line "unsubscribe linux-hotplug" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[Index of Archives]     [Linux Kernel]     [Linux DVB]     [Asterisk Internet PBX]     [DCCP]     [Netdev]     [X.org]     [Util Linux NG]     [Fedora Women]     [ALSA Devel]     [Linux USB]

  Powered by Linux