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

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