yeahuh / rpms / qemu-kvm

Forked from rpms/qemu-kvm 2 years ago
Clone

Blame SOURCES/kvm-qga-add-implementation-of-guest-get-disks-for-Window.patch

c687bc
From 925163bf8498e26c19742dbd34b6b324e49c07b6 Mon Sep 17 00:00:00 2001
c687bc
From: =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= <marcandre.lureau@redhat.com>
c687bc
Date: Wed, 16 Dec 2020 16:06:13 -0500
c687bc
Subject: [PATCH 12/14] qga: add implementation of guest-get-disks for Windows
c687bc
MIME-Version: 1.0
c687bc
Content-Type: text/plain; charset=UTF-8
c687bc
Content-Transfer-Encoding: 8bit
c687bc
c687bc
RH-Author: Marc-André Lureau <marcandre.lureau@redhat.com>
c687bc
Message-id: <20201216160615.324213-9-marcandre.lureau@redhat.com>
c687bc
Patchwork-id: 100479
c687bc
O-Subject: [RHEL-8.4.0 qemu-kvm PATCH v2 08/10] qga: add implementation of guest-get-disks for Windows
c687bc
Bugzilla: 1859494
c687bc
RH-Acked-by: Danilo de Paula <ddepaula@redhat.com>
c687bc
RH-Acked-by: Sergio Lopez Pascual <slp@redhat.com>
c687bc
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
c687bc
c687bc
From: Tomáš Golembiovský <tgolembi@redhat.com>
c687bc
c687bc
The command lists all the physical disk drives. Unlike for Linux
c687bc
partitions and virtual volumes are not listed.
c687bc
c687bc
Example output:
c687bc
c687bc
{
c687bc
  "return": [
c687bc
    {
c687bc
      "name": "\\\\.\\PhysicalDrive0",
c687bc
      "partition": false,
c687bc
      "address": {
c687bc
        "serial": "QM00001",
c687bc
        "bus-type": "sata",
c687bc
        ...
c687bc
      },
c687bc
      "dependents": []
c687bc
    }
c687bc
  ]
c687bc
}
c687bc
c687bc
Signed-off-by: Tomáš Golembiovský <tgolembi@redhat.com>
c687bc
Signed-off-by: Michael Roth <michael.roth@amd.com>
c687bc
c687bc
(cherry picked from commit c67d2efd9d1771fd886e3b58771adaa62897f3d9)
c687bc
Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
c687bc
Signed-off-by: Danilo C. L. de Paula <ddepaula@redhat.com>
c687bc
---
c687bc
 qga/commands-win32.c | 107 ++++++++++++++++++++++++++++++++++++++++---
c687bc
 1 file changed, 101 insertions(+), 6 deletions(-)
c687bc
c687bc
diff --git a/qga/commands-win32.c b/qga/commands-win32.c
c687bc
index be63fa2b208..a07725e874b 100644
c687bc
--- a/qga/commands-win32.c
c687bc
+++ b/qga/commands-win32.c
c687bc
@@ -960,6 +960,101 @@ out:
c687bc
     return list;
c687bc
 }
c687bc
 
c687bc
+GuestDiskInfoList *qmp_guest_get_disks(Error **errp)
c687bc
+{
c687bc
+    ERRP_GUARD();
c687bc
+    GuestDiskInfoList *new = NULL, *ret = NULL;
c687bc
+    HDEVINFO dev_info;
c687bc
+    SP_DEVICE_INTERFACE_DATA dev_iface_data;
c687bc
+    int i;
c687bc
+
c687bc
+    dev_info = SetupDiGetClassDevs(&GUID_DEVINTERFACE_DISK, 0, 0,
c687bc
+        DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
c687bc
+    if (dev_info == INVALID_HANDLE_VALUE) {
c687bc
+        error_setg_win32(errp, GetLastError(), "failed to get device tree");
c687bc
+        return NULL;
c687bc
+    }
c687bc
+
c687bc
+    g_debug("enumerating devices");
c687bc
+    dev_iface_data.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
c687bc
+    for (i = 0;
c687bc
+        SetupDiEnumDeviceInterfaces(dev_info, NULL, &GUID_DEVINTERFACE_DISK,
c687bc
+            i, &dev_iface_data);
c687bc
+        i++) {
c687bc
+        GuestDiskAddress *address = NULL;
c687bc
+        GuestDiskInfo *disk = NULL;
c687bc
+        Error *local_err = NULL;
c687bc
+        g_autofree PSP_DEVICE_INTERFACE_DETAIL_DATA
c687bc
+            pdev_iface_detail_data = NULL;
c687bc
+        STORAGE_DEVICE_NUMBER sdn;
c687bc
+        HANDLE dev_file;
c687bc
+        DWORD size = 0;
c687bc
+        BOOL result;
c687bc
+        int attempt;
c687bc
+
c687bc
+        g_debug("  getting device path");
c687bc
+        for (attempt = 0, result = FALSE; attempt < 2 && !result; attempt++) {
c687bc
+            result = SetupDiGetDeviceInterfaceDetail(dev_info,
c687bc
+                &dev_iface_data, pdev_iface_detail_data, size, &size, NULL);
c687bc
+            if (result) {
c687bc
+                break;
c687bc
+            }
c687bc
+            if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
c687bc
+                pdev_iface_detail_data = g_realloc(pdev_iface_detail_data,
c687bc
+                    size);
c687bc
+                pdev_iface_detail_data->cbSize =
c687bc
+                    sizeof(*pdev_iface_detail_data);
c687bc
+            } else {
c687bc
+                g_debug("failed to get device interface details");
c687bc
+                break;
c687bc
+            }
c687bc
+        }
c687bc
+        if (!result) {
c687bc
+            g_debug("skipping device");
c687bc
+            continue;
c687bc
+        }
c687bc
+
c687bc
+        g_debug("  device: %s", pdev_iface_detail_data->DevicePath);
c687bc
+        dev_file = CreateFile(pdev_iface_detail_data->DevicePath, 0,
c687bc
+            FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
c687bc
+        if (!DeviceIoControl(dev_file, IOCTL_STORAGE_GET_DEVICE_NUMBER,
c687bc
+                NULL, 0, &sdn, sizeof(sdn), &size, NULL)) {
c687bc
+            CloseHandle(dev_file);
c687bc
+            debug_error("failed to get storage device number");
c687bc
+            continue;
c687bc
+        }
c687bc
+        CloseHandle(dev_file);
c687bc
+
c687bc
+        disk = g_new0(GuestDiskInfo, 1);
c687bc
+        disk->name = g_strdup_printf("\\\\.\\PhysicalDrive%lu",
c687bc
+            sdn.DeviceNumber);
c687bc
+
c687bc
+        g_debug("  number: %lu", sdn.DeviceNumber);
c687bc
+        address = g_malloc0(sizeof(GuestDiskAddress));
c687bc
+        address->has_dev = true;
c687bc
+        address->dev = g_strdup(disk->name);
c687bc
+        get_single_disk_info(sdn.DeviceNumber, address, &local_err);
c687bc
+        if (local_err) {
c687bc
+            g_debug("failed to get disk info: %s",
c687bc
+                error_get_pretty(local_err));
c687bc
+            error_free(local_err);
c687bc
+            qapi_free_GuestDiskAddress(address);
c687bc
+            address = NULL;
c687bc
+        } else {
c687bc
+            disk->address = address;
c687bc
+            disk->has_address = true;
c687bc
+        }
c687bc
+
c687bc
+        new = g_malloc0(sizeof(GuestDiskInfoList));
c687bc
+        new->value = disk;
c687bc
+        new->next = ret;
c687bc
+        ret = new;
c687bc
+    }
c687bc
+
c687bc
+    SetupDiDestroyDeviceInfoList(dev_info);
c687bc
+    return ret;
c687bc
+}
c687bc
+
c687bc
 #else
c687bc
 
c687bc
 static GuestDiskAddressList *build_guest_disk_info(char *guid, Error **errp)
c687bc
@@ -967,6 +1062,12 @@ static GuestDiskAddressList *build_guest_disk_info(char *guid, Error **errp)
c687bc
     return NULL;
c687bc
 }
c687bc
 
c687bc
+GuestDiskInfoList *qmp_guest_get_disks(Error **errp)
c687bc
+{
c687bc
+    error_setg(errp, QERR_UNSUPPORTED);
c687bc
+    return NULL;
c687bc
+}
c687bc
+
c687bc
 #endif /* CONFIG_QGA_NTDDSCSI */
c687bc
 
c687bc
 static GuestFilesystemInfo *build_guest_fsinfo(char *guid, Error **errp)
c687bc
@@ -2234,9 +2335,3 @@ GuestOSInfo *qmp_guest_get_osinfo(Error **errp)
c687bc
 
c687bc
     return info;
c687bc
 }
c687bc
-
c687bc
-GuestDiskInfoList *qmp_guest_get_disks(Error **errp)
c687bc
-{
c687bc
-    error_setg(errp, QERR_UNSUPPORTED);
c687bc
-    return NULL;
c687bc
-}
c687bc
-- 
c687bc
2.27.0
c687bc