diff mbox

[09/10] qga: added bus type and disk location path

Message ID 1436244020-27822-10-git-send-email-mdroth@linux.vnet.ibm.com
State New
Headers show

Commit Message

Michael Roth July 7, 2015, 4:40 a.m. UTC
From: Olga Krishtal <okrishtal@virtuozzo.com>

According to Microsoft disk location path can be obtained via
IOCTL_SCSI_GET_ADDRESS. Unfortunately this ioctl can not be used for all
devices. There are certain bus types which could be obtained with this
API. Please, refer to the following link for more details
https://technet.microsoft.com/en-us/library/ee851589(v=ws.10).aspx

Bus type could be obtained using IOCTL_STORAGE_QUERY_PROPERTY. Enum
STORAGE_BUS_TYPE describes all buses supported by OS.

Windows defines more bus types than Linux. Thus some values have been added
to GuestDiskBusType.

Signed-off-by: Olga Krishtal <okrishtal@virtuozzo.com>
Signed-off-by: Denis V. Lunev <den@openvz.org>
CC: Eric Blake <eblake@redhat.com>
CC: Michael Roth <mdroth@linux.vnet.ibm.com>
* fixed warning in CreateFile due to use of NULL instead of 0
Signed-off-by: Michael Roth <mdroth@linux.vnet.ibm.com>
---
 qga/commands-win32.c | 122 ++++++++++++++++++++++++++++++++++++++++++++++++++-
 qga/qapi-schema.json |  14 +++++-
 2 files changed, 134 insertions(+), 2 deletions(-)

Comments

Eric Blake July 20, 2015, 3:06 p.m. UTC | #1
On 07/06/2015 10:40 PM, Michael Roth wrote:
> From: Olga Krishtal <okrishtal@virtuozzo.com>
> 
> According to Microsoft disk location path can be obtained via
> IOCTL_SCSI_GET_ADDRESS. Unfortunately this ioctl can not be used for all
> devices. There are certain bus types which could be obtained with this
> API. Please, refer to the following link for more details
> https://technet.microsoft.com/en-us/library/ee851589(v=ws.10).aspx
> 
> Bus type could be obtained using IOCTL_STORAGE_QUERY_PROPERTY. Enum
> STORAGE_BUS_TYPE describes all buses supported by OS.
> 
> Windows defines more bus types than Linux. Thus some values have been added
> to GuestDiskBusType.
> 
> Signed-off-by: Olga Krishtal <okrishtal@virtuozzo.com>
> Signed-off-by: Denis V. Lunev <den@openvz.org>
> CC: Eric Blake <eblake@redhat.com>
> CC: Michael Roth <mdroth@linux.vnet.ibm.com>
> * fixed warning in CreateFile due to use of NULL instead of 0
> Signed-off-by: Michael Roth <mdroth@linux.vnet.ibm.com>
> ---

> +++ b/qga/qapi-schema.json
> @@ -703,12 +703,24 @@
>  # @uml: UML disks
>  # @sata: SATA disks
>  # @sd: SD cards
> +# @unknown: Unknown bus type
> +# @ieee1394: Win IEEE 1394 bus type
> +# @ssa: Win SSA bus type
> +# @fibre: Win fiber channel bus type
> +# @raid: Win RAID bus type
> +# @iscsi: Win iScsi bus type
> +# @sas: Win serial-attaches SCSI bus type
> +# @mmc: Win multimedia card (MMC) bus type
> +# @virtual: Win virtual bus type
> +# @file-backed virtual: Win file-backed bus type
>  #
>  # Since: 2.2

It would be nice to have a followup patch (since it is doc-only, it
could still make 2.4) that mentions that all these new enum members were
added in 2.4.
Denis V. Lunev July 20, 2015, 3:19 p.m. UTC | #2
On 07/20/2015 06:06 PM, Eric Blake wrote:
> On 07/06/2015 10:40 PM, Michael Roth wrote:
>> From: Olga Krishtal <okrishtal@virtuozzo.com>
>>
>> According to Microsoft disk location path can be obtained via
>> IOCTL_SCSI_GET_ADDRESS. Unfortunately this ioctl can not be used for all
>> devices. There are certain bus types which could be obtained with this
>> API. Please, refer to the following link for more details
>> https://technet.microsoft.com/en-us/library/ee851589(v=ws.10).aspx
>>
>> Bus type could be obtained using IOCTL_STORAGE_QUERY_PROPERTY. Enum
>> STORAGE_BUS_TYPE describes all buses supported by OS.
>>
>> Windows defines more bus types than Linux. Thus some values have been added
>> to GuestDiskBusType.
>>
>> Signed-off-by: Olga Krishtal <okrishtal@virtuozzo.com>
>> Signed-off-by: Denis V. Lunev <den@openvz.org>
>> CC: Eric Blake <eblake@redhat.com>
>> CC: Michael Roth <mdroth@linux.vnet.ibm.com>
>> * fixed warning in CreateFile due to use of NULL instead of 0
>> Signed-off-by: Michael Roth <mdroth@linux.vnet.ibm.com>
>> ---
>> +++ b/qga/qapi-schema.json
>> @@ -703,12 +703,24 @@
>>   # @uml: UML disks
>>   # @sata: SATA disks
>>   # @sd: SD cards
>> +# @unknown: Unknown bus type
>> +# @ieee1394: Win IEEE 1394 bus type
>> +# @ssa: Win SSA bus type
>> +# @fibre: Win fiber channel bus type
>> +# @raid: Win RAID bus type
>> +# @iscsi: Win iScsi bus type
>> +# @sas: Win serial-attaches SCSI bus type
>> +# @mmc: Win multimedia card (MMC) bus type
>> +# @virtual: Win virtual bus type
>> +# @file-backed virtual: Win file-backed bus type
>>   #
>>   # Since: 2.2
> It would be nice to have a followup patch (since it is doc-only, it
> could still make 2.4) that mentions that all these new enum members were
> added in 2.4.
>
sure!
diff mbox

Patch

diff --git a/qga/commands-win32.c b/qga/commands-win32.c
index 60d9770..bffa766 100644
--- a/qga/commands-win32.c
+++ b/qga/commands-win32.c
@@ -21,6 +21,8 @@ 
 #include <ws2ipdef.h>
 #include <iptypes.h>
 #include <iphlpapi.h>
+#include <winioctl.h>
+#include <ntddscsi.h>
 #include "qga/guest-agent-core.h"
 #include "qga/vss-win32.h"
 #include "qga-qmp-commands.h"
@@ -90,6 +92,37 @@  static OpenFlags *find_open_flag(const char *mode_str)
     return NULL;
 }
 
+static STORAGE_BUS_TYPE win2qemu[] = {
+    [BusTypeUnknown] = GUEST_DISK_BUS_TYPE_UNKNOWN,
+    [BusTypeScsi] = GUEST_DISK_BUS_TYPE_SCSI,
+    [BusTypeAtapi] = GUEST_DISK_BUS_TYPE_IDE,
+    [BusTypeAta] = GUEST_DISK_BUS_TYPE_IDE,
+    [BusType1394] = GUEST_DISK_BUS_TYPE_IEEE1394,
+    [BusTypeSsa] = GUEST_DISK_BUS_TYPE_SSA,
+    [BusTypeFibre] = GUEST_DISK_BUS_TYPE_SSA,
+    [BusTypeUsb] = GUEST_DISK_BUS_TYPE_USB,
+    [BusTypeRAID] = GUEST_DISK_BUS_TYPE_RAID,
+#if (_WIN32_WINNT >= 0x0600)
+    [BusTypeiScsi] = GUEST_DISK_BUS_TYPE_ISCSI,
+    [BusTypeSas] = GUEST_DISK_BUS_TYPE_SAS,
+    [BusTypeSata] = GUEST_DISK_BUS_TYPE_SATA,
+    [BusTypeSd] =  GUEST_DISK_BUS_TYPE_SD,
+    [BusTypeMmc] = GUEST_DISK_BUS_TYPE_MMC,
+#endif
+#if (_WIN32_WINNT >= 0x0601)
+    [BusTypeVirtual] = GUEST_DISK_BUS_TYPE_VIRTUAL,
+    [BusTypeFileBackedVirtual] = GUEST_DISK_BUS_TYPE_FILE_BACKED_VIRTUAL,
+#endif
+};
+
+static GuestDiskBusType find_bus_type(STORAGE_BUS_TYPE bus)
+{
+    if (bus > ARRAY_SIZE(win2qemu) || (int)bus < 0) {
+        return GUEST_DISK_BUS_TYPE_UNKNOWN;
+    }
+    return win2qemu[(int)bus];
+}
+
 static int64_t guest_file_handle_add(HANDLE fh, Error **errp)
 {
     GuestFileHandle *gfh;
@@ -388,6 +421,93 @@  static void guest_file_init(void)
     QTAILQ_INIT(&guest_file_state.filehandles);
 }
 
+static GuestPCIAddress *get_pci_info(char *guid, Error **errp)
+{
+    return NULL;
+}
+
+static int get_disk_bus_type(HANDLE vol_h, Error **errp)
+{
+    STORAGE_PROPERTY_QUERY query;
+    STORAGE_DEVICE_DESCRIPTOR *dev_desc, buf;
+    DWORD received;
+
+    dev_desc = &buf;
+    dev_desc->Size = sizeof(buf);
+    query.PropertyId = StorageDeviceProperty;
+    query.QueryType = PropertyStandardQuery;
+
+    if (!DeviceIoControl(vol_h, IOCTL_STORAGE_QUERY_PROPERTY, &query,
+                         sizeof(STORAGE_PROPERTY_QUERY), dev_desc,
+                         dev_desc->Size, &received, NULL)) {
+        error_setg_win32(errp, GetLastError(), "failed to get bus type");
+        return -1;
+    }
+
+    return dev_desc->BusType;
+}
+
+/* VSS provider works with volumes, thus there is no difference if
+ * the volume consist of spanned disks. Info about the first disk in the
+ * volume is returned for the spanned disk group (LVM) */
+static GuestDiskAddressList *build_guest_disk_info(char *guid, Error **errp)
+{
+    GuestDiskAddressList *list = NULL;
+    GuestDiskAddress *disk;
+    SCSI_ADDRESS addr, *scsi_ad;
+    DWORD len;
+    int bus;
+    HANDLE vol_h;
+
+    scsi_ad = &addr;
+    char *name = g_strndup(guid, strlen(guid)-1);
+
+    vol_h = CreateFile(name, 0, FILE_SHARE_READ, NULL, OPEN_EXISTING,
+                       0, NULL);
+    if (vol_h == INVALID_HANDLE_VALUE) {
+        error_setg_win32(errp, GetLastError(), "failed to open volume");
+        goto out_free;
+    }
+
+    bus = get_disk_bus_type(vol_h, errp);
+    if (bus < 0) {
+        goto out_close;
+    }
+
+    disk = g_malloc0(sizeof(*disk));
+    disk->bus_type = find_bus_type(bus);
+    if (bus == BusTypeScsi || bus == BusTypeAta || bus == BusTypeRAID
+#if (_WIN32_WINNT >= 0x0600)
+            /* This bus type is not supported before Windows Server 2003 SP1 */
+            || bus == BusTypeSas
+#endif
+        ) {
+        /* We are able to use the same ioctls for different bus types
+         * according to Microsoft docs
+         * https://technet.microsoft.com/en-us/library/ee851589(v=ws.10).aspx */
+        if (DeviceIoControl(vol_h, IOCTL_SCSI_GET_ADDRESS, NULL, 0, scsi_ad,
+                            sizeof(SCSI_ADDRESS), &len, NULL)) {
+            disk->unit = addr.Lun;
+            disk->target = addr.TargetId;
+            disk->bus = addr.PathId;
+            disk->pci_controller = get_pci_info(name, errp);
+        }
+        /* We do not set error in this case, because we still have enough
+         * information about volume. */
+    } else {
+         disk->pci_controller = NULL;
+    }
+
+    list = g_malloc0(sizeof(*list));
+    list->value = disk;
+    list->next = NULL;
+out_close:
+    CloseHandle(vol_h);
+out_free:
+    g_free(name);
+    return list;
+}
+
 static GuestFilesystemInfo *build_guest_fsinfo(char *guid, Error **errp)
 {
     DWORD info_size;
@@ -430,7 +550,7 @@  static GuestFilesystemInfo *build_guest_fsinfo(char *guid, Error **errp)
         fs->mountpoint = g_strndup(mnt_point, len);
     }
     fs->type = g_strdup(fs_name);
-    fs->disk = NULL;
+    fs->disk = build_guest_disk_info(guid, errp);;
 free:
     g_free(mnt_point);
     return fs;
diff --git a/qga/qapi-schema.json b/qga/qapi-schema.json
index 58cbf66..8a9b818 100644
--- a/qga/qapi-schema.json
+++ b/qga/qapi-schema.json
@@ -703,12 +703,24 @@ 
 # @uml: UML disks
 # @sata: SATA disks
 # @sd: SD cards
+# @unknown: Unknown bus type
+# @ieee1394: Win IEEE 1394 bus type
+# @ssa: Win SSA bus type
+# @fibre: Win fiber channel bus type
+# @raid: Win RAID bus type
+# @iscsi: Win iScsi bus type
+# @sas: Win serial-attaches SCSI bus type
+# @mmc: Win multimedia card (MMC) bus type
+# @virtual: Win virtual bus type
+# @file-backed virtual: Win file-backed bus type
 #
 # Since: 2.2
 ##
 { 'enum': 'GuestDiskBusType',
   'data': [ 'ide', 'fdc', 'scsi', 'virtio', 'xen', 'usb', 'uml', 'sata',
-            'sd' ] }
+            'sd', 'unknown', 'ieee1394', 'ssa', 'fibre', 'raid', 'iscsi',
+            'sas', 'mmc', 'virtual', 'file-backed-virtual' ] }
+
 
 ##
 # @GuestPCIAddress: