Blame SOURCES/qemuga-qga-linux-report-disk-serial-number.patch

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