26ba25
From dab127e2324fdfbaf5d595144ccf0d0bfcf73074 Mon Sep 17 00:00:00 2001
26ba25
From: =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= <marcandre.lureau@redhat.com>
26ba25
Date: Sun, 4 Nov 2018 15:45:27 +0000
26ba25
Subject: [PATCH 02/35] qga: linux: report disk serial number
26ba25
MIME-Version: 1.0
26ba25
Content-Type: text/plain; charset=UTF-8
26ba25
Content-Transfer-Encoding: 8bit
26ba25
26ba25
RH-Author: Marc-André Lureau <marcandre.lureau@redhat.com>
26ba25
Message-id: <20181104154528.19241-3-marcandre.lureau@redhat.com>
26ba25
Patchwork-id: 82927
26ba25
O-Subject: [RHEL8/rhel qemu-kvm PATCH 2/3] qga: linux: report disk serial number
26ba25
Bugzilla: 1636185
26ba25
RH-Acked-by: Laurent Vivier <lvivier@redhat.com>
26ba25
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
26ba25
RH-Acked-by: Markus Armbruster <armbru@redhat.com>
26ba25
26ba25
From: Tomáš Golembiovský <tgolembi@redhat.com>
26ba25
26ba25
Add reporting of disk serial number on Linux guests. The feature depends
26ba25
on libudev.
26ba25
26ba25
Example:
26ba25
26ba25
    {
26ba25
      "name": "dm-2",
26ba25
      "mountpoint": "/",
26ba25
      ...
26ba25
      "disk": [
26ba25
        {
26ba25
          "serial": "SAMSUNG_MZ7LN512HCHP-000L1_S1ZKNXAG822493",
26ba25
          ...
26ba25
        }
26ba25
      ],
26ba25
    }
26ba25
26ba25
Signed-off-by: Tomáš Golembiovský <tgolembi@redhat.com>
26ba25
Signed-off-by: Michael Roth <mdroth@linux.vnet.ibm.com>
26ba25
26ba25
BZ: https://bugzilla.redhat.com/show_bug.cgi?id=1636185
26ba25
26ba25
(cherry picked from commit b616105a904bc350a409e12c39af0ae210900124)
26ba25
26ba25
Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
26ba25
Signed-off-by: Danilo C. L. de Paula <ddepaula@redhat.com>
26ba25
---
26ba25
 qga/Makefile.objs    |  1 +
26ba25
 qga/commands-posix.c | 32 ++++++++++++++++++++++++++++++--
26ba25
 qga/qapi-schema.json |  4 +++-
26ba25
 3 files changed, 34 insertions(+), 3 deletions(-)
26ba25
26ba25
diff --git a/qga/Makefile.objs b/qga/Makefile.objs
26ba25
index ed08c59..80e6bb3 100644
26ba25
--- a/qga/Makefile.objs
26ba25
+++ b/qga/Makefile.objs
26ba25
@@ -1,3 +1,4 @@
26ba25
+commands-posix.o-libs := $(LIBUDEV_LIBS)
26ba25
 qga-obj-y = commands.o guest-agent-command-state.o main.o
26ba25
 qga-obj-$(CONFIG_POSIX) += commands-posix.o channel-posix.o
26ba25
 qga-obj-$(CONFIG_WIN32) += commands-win32.o channel-win32.o service-win32.o
26ba25
diff --git a/qga/commands-posix.c b/qga/commands-posix.c
26ba25
index 0dc219d..c151891 100644
26ba25
--- a/qga/commands-posix.c
26ba25
+++ b/qga/commands-posix.c
26ba25
@@ -47,6 +47,10 @@ extern char **environ;
26ba25
 #include <sys socket.h="">
26ba25
 #include <net if.h="">
26ba25
 
26ba25
+#ifdef CONFIG_LIBUDEV
26ba25
+#include <libudev.h>
26ba25
+#endif
26ba25
+
26ba25
 #ifdef FIFREEZE
26ba25
 #define CONFIG_FSFREEZE
26ba25
 #endif
26ba25
@@ -871,6 +875,10 @@ static void build_guest_fsinfo_for_real_device(char const *syspath,
26ba25
     GuestDiskAddressList *list = NULL;
26ba25
     bool has_ata = false, has_host = false, has_tgt = false;
26ba25
     char *p, *q, *driver = NULL;
26ba25
+#ifdef CONFIG_LIBUDEV
26ba25
+    struct udev *udev = NULL;
26ba25
+    struct udev_device *udevice = NULL;
26ba25
+#endif
26ba25
 
26ba25
     p = strstr(syspath, "/devices/pci");
26ba25
     if (!p || sscanf(p + 12, "%*x:%*x/%x:%x:%x.%x%n",
26ba25
@@ -919,6 +927,21 @@ static void build_guest_fsinfo_for_real_device(char const *syspath,
26ba25
     list = g_malloc0(sizeof(*list));
26ba25
     list->value = disk;
26ba25
 
26ba25
+#ifdef CONFIG_LIBUDEV
26ba25
+    udev = udev_new();
26ba25
+    udevice = udev_device_new_from_syspath(udev, syspath);
26ba25
+    if (udev == NULL || udevice == NULL) {
26ba25
+        g_debug("failed to query udev");
26ba25
+    } else {
26ba25
+        const char *serial;
26ba25
+        serial = udev_device_get_property_value(udevice, "ID_SERIAL");
26ba25
+        if (serial != NULL && *serial != 0) {
26ba25
+            disk->serial = g_strdup(serial);
26ba25
+            disk->has_serial = true;
26ba25
+        }
26ba25
+    }
26ba25
+#endif
26ba25
+
26ba25
     if (strcmp(driver, "ata_piix") == 0) {
26ba25
         /* a host per ide bus, target*:0:<unit>:0 */
26ba25
         if (!has_host || !has_tgt) {
26ba25
@@ -978,14 +1001,19 @@ static void build_guest_fsinfo_for_real_device(char const *syspath,
26ba25
 
26ba25
     list->next = fs->disk;
26ba25
     fs->disk = list;
26ba25
-    g_free(driver);
26ba25
-    return;
26ba25
+    goto out;
26ba25
 
26ba25
 cleanup:
26ba25
     if (list) {
26ba25
         qapi_free_GuestDiskAddressList(list);
26ba25
     }
26ba25
+out:
26ba25
     g_free(driver);
26ba25
+#ifdef CONFIG_LIBUDEV
26ba25
+    udev_unref(udev);
26ba25
+    udev_device_unref(udevice);
26ba25
+#endif
26ba25
+    return;
26ba25
 }
26ba25
 
26ba25
 static void build_guest_fsinfo_for_device(char const *devpath,
26ba25
diff --git a/qga/qapi-schema.json b/qga/qapi-schema.json
26ba25
index 17884c7..426528c 100644
26ba25
--- a/qga/qapi-schema.json
26ba25
+++ b/qga/qapi-schema.json
26ba25
@@ -832,13 +832,15 @@
26ba25
 # @bus: bus id
26ba25
 # @target: target id
26ba25
 # @unit: unit id
26ba25
+# @serial: serial number (since: 3.1)
26ba25
 #
26ba25
 # Since: 2.2
26ba25
 ##
26ba25
 { 'struct': 'GuestDiskAddress',
26ba25
   'data': {'pci-controller': 'GuestPCIAddress',
26ba25
            'bus-type': 'GuestDiskBusType',
26ba25
-           'bus': 'int', 'target': 'int', 'unit': 'int'} }
26ba25
+           'bus': 'int', 'target': 'int', 'unit': 'int',
26ba25
+           '*serial': 'str'} }
26ba25
 
26ba25
 ##
26ba25
 # @GuestFilesystemInfo:
26ba25
-- 
26ba25
1.8.3.1
26ba25