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