cryptospore / 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

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