diff mbox series

[v4,10/11] qga: return disk device in guest-get-fsinfo

Message ID fe8e4352f962e5f4b720338b2742a01c29674ee7.1538652143.git.tgolembi@redhat.com (mailing list archive)
State New, archived
Headers show
Series qga: report serial number and disk node | expand

Commit Message

Tomáš Golembiovský Oct. 4, 2018, 11:22 a.m. UTC
Report device node of the disk. It is implemented for Linux (needs
libudev) and Windows. The node is reported e.g. as "/dev/sda2" on Linux
and as "\\.\PhysicalDriveX" on Windows.

Signed-off-by: Tomáš Golembiovský <tgolembi@redhat.com>
---
 qga/commands-posix.c |  7 ++++++-
 qga/commands-win32.c | 21 +++++++++++----------
 qga/qapi-schema.json |  3 ++-
 3 files changed, 19 insertions(+), 12 deletions(-)

Comments

Marc-André Lureau Oct. 4, 2018, 1:20 p.m. UTC | #1
Hi

On Thu, Oct 4, 2018 at 3:22 PM Tomáš Golembiovský <tgolembi@redhat.com> wrote:
>
> Report device node of the disk. It is implemented for Linux (needs
> libudev) and Windows. The node is reported e.g. as "/dev/sda2" on Linux
> and as "\\.\PhysicalDriveX" on Windows.
>
> Signed-off-by: Tomáš Golembiovský <tgolembi@redhat.com>
> ---
>  qga/commands-posix.c |  7 ++++++-
>  qga/commands-win32.c | 21 +++++++++++----------
>  qga/qapi-schema.json |  3 ++-
>  3 files changed, 19 insertions(+), 12 deletions(-)
>
> diff --git a/qga/commands-posix.c b/qga/commands-posix.c
> index 4d324178f2..31952b07f4 100644
> --- a/qga/commands-posix.c
> +++ b/qga/commands-posix.c
> @@ -950,7 +950,12 @@ static void build_guest_fsinfo_for_real_device(char const *syspath,
>      if (udev == NULL || udevice == NULL) {
>          g_debug("failed to query udev");
>      } else {
> -        const char *serial;
> +        const char *devnode, *serial;
> +        devnode = udev_device_get_devnode(udevice);
> +        if (devnode != NULL) {
> +            disk->dev = g_strdup(devnode);
> +            disk->has_dev = true;
> +        }

ack the posix/linux part.

You may want to split the Windows part..

>          serial = udev_device_get_property_value(udevice, "ID_SERIAL");
>          if (serial != NULL && *serial != 0) {
>              disk->serial = g_strdup(serial);
> diff --git a/qga/commands-win32.c b/qga/commands-win32.c
> index a591d8221d..d0d969d0ce 100644
> --- a/qga/commands-win32.c
> +++ b/qga/commands-win32.c
> @@ -649,8 +649,7 @@ out_free:
>      return;
>  }
>
> -static void get_single_disk_info(char *name, GuestDiskAddress *disk,
> -    Error **errp)
> +static void get_single_disk_info(GuestDiskAddress *disk, Error **errp)
>  {
>      SCSI_ADDRESS addr, *scsi_ad;
>      DWORD len;
> @@ -659,8 +658,8 @@ static void get_single_disk_info(char *name, GuestDiskAddress *disk,
>
>      scsi_ad = &addr;
>
> -    g_debug("getting disk info for: %s", name);
> -    disk_h = CreateFile(name, 0, FILE_SHARE_READ, NULL, OPEN_EXISTING,
> +    g_debug("getting disk info for: %s", disk->dev);
> +    disk_h = CreateFile(disk->dev, 0, FILE_SHARE_READ, NULL, OPEN_EXISTING,
>                         0, NULL);
>      if (disk_h == INVALID_HANDLE_VALUE) {
>          error_setg_win32(errp, GetLastError(), "failed to open disk");
> @@ -692,7 +691,7 @@ static void get_single_disk_info(char *name, GuestDiskAddress *disk,
>              disk->bus = addr.PathId;
>              g_debug("unit=%lld target=%lld bus=%lld",
>                  disk->unit, disk->target, disk->bus);
> -            disk->pci_controller = get_pci_info(name, &local_err);
> +            disk->pci_controller = get_pci_info(disk->dev, &local_err);
>
>              if (local_err) {
>                  g_debug("failed to get PCI controller info: %s",
> @@ -771,7 +770,9 @@ static GuestDiskAddressList *build_guest_disk_info(char *guid, Error **errp)
>              /* Possibly CD-ROM or a shared drive. Try to pass the volume */
>              g_debug("volume not on disk");
>              disk = g_malloc0(sizeof(GuestDiskAddress));
> -            get_single_disk_info(name, disk, &local_err);
> +            disk->has_dev = true;
> +            disk->dev = g_strdup(name);
> +            get_single_disk_info(disk, &local_err);
>              if (local_err) {
>                  g_debug("failed to get disk info, ignoring error: %s",
>                      error_get_pretty(local_err));
> @@ -793,7 +794,6 @@ static GuestDiskAddressList *build_guest_disk_info(char *guid, Error **errp)
>
>      /* Go through each extent */
>      for (i = 0; i < extents->NumberOfDiskExtents; i++) {
> -        char *disk_name = NULL;
>          disk = g_malloc0(sizeof(GuestDiskAddress));
>
>          /* Disk numbers directly correspond to numbers used in UNCs
> @@ -804,10 +804,11 @@ static GuestDiskAddressList *build_guest_disk_info(char *guid, Error **errp)
>           * See also Naming Files, Paths and Namespaces:
>           * https://docs.microsoft.com/en-us/windows/desktop/FileIO/naming-a-file#win32-device-namespaces
>           */
> -        disk_name = g_strdup_printf("\\\\.\\PhysicalDrive%lu",
> +        disk->has_dev = true;
> +        disk->dev = g_strdup_printf("\\\\.\\PhysicalDrive%lu",
>              extents->Extents[i].DiskNumber);
> -        get_single_disk_info(disk_name, disk, &local_err);
> -        g_free(disk_name);
> +
> +        get_single_disk_info(disk, &local_err);
>          if (local_err) {
>              error_propagate(errp, local_err);
>              goto out;
> diff --git a/qga/qapi-schema.json b/qga/qapi-schema.json
> index 3bcda6257e..c6725b3ec8 100644
> --- a/qga/qapi-schema.json
> +++ b/qga/qapi-schema.json
> @@ -835,6 +835,7 @@
>  # @target: target id
>  # @unit: unit id
>  # @serial: serial number (since: 3.1)
> +# @dev: device node (POSIX) or device UNC (Windows) (since: 3.1)
>  #
>  # Since: 2.2
>  ##
> @@ -842,7 +843,7 @@
>    'data': {'pci-controller': 'GuestPCIAddress',
>             'bus-type': 'GuestDiskBusType',
>             'bus': 'int', 'target': 'int', 'unit': 'int',
> -           '*serial': 'str'} }
> +           '*serial': 'str', '*dev': 'str'} }
>
>  ##
>  # @GuestFilesystemInfo:
> --
> 2.19.0
>
diff mbox series

Patch

diff --git a/qga/commands-posix.c b/qga/commands-posix.c
index 4d324178f2..31952b07f4 100644
--- a/qga/commands-posix.c
+++ b/qga/commands-posix.c
@@ -950,7 +950,12 @@  static void build_guest_fsinfo_for_real_device(char const *syspath,
     if (udev == NULL || udevice == NULL) {
         g_debug("failed to query udev");
     } else {
-        const char *serial;
+        const char *devnode, *serial;
+        devnode = udev_device_get_devnode(udevice);
+        if (devnode != NULL) {
+            disk->dev = g_strdup(devnode);
+            disk->has_dev = true;
+        }
         serial = udev_device_get_property_value(udevice, "ID_SERIAL");
         if (serial != NULL && *serial != 0) {
             disk->serial = g_strdup(serial);
diff --git a/qga/commands-win32.c b/qga/commands-win32.c
index a591d8221d..d0d969d0ce 100644
--- a/qga/commands-win32.c
+++ b/qga/commands-win32.c
@@ -649,8 +649,7 @@  out_free:
     return;
 }
 
-static void get_single_disk_info(char *name, GuestDiskAddress *disk,
-    Error **errp)
+static void get_single_disk_info(GuestDiskAddress *disk, Error **errp)
 {
     SCSI_ADDRESS addr, *scsi_ad;
     DWORD len;
@@ -659,8 +658,8 @@  static void get_single_disk_info(char *name, GuestDiskAddress *disk,
 
     scsi_ad = &addr;
 
-    g_debug("getting disk info for: %s", name);
-    disk_h = CreateFile(name, 0, FILE_SHARE_READ, NULL, OPEN_EXISTING,
+    g_debug("getting disk info for: %s", disk->dev);
+    disk_h = CreateFile(disk->dev, 0, FILE_SHARE_READ, NULL, OPEN_EXISTING,
                        0, NULL);
     if (disk_h == INVALID_HANDLE_VALUE) {
         error_setg_win32(errp, GetLastError(), "failed to open disk");
@@ -692,7 +691,7 @@  static void get_single_disk_info(char *name, GuestDiskAddress *disk,
             disk->bus = addr.PathId;
             g_debug("unit=%lld target=%lld bus=%lld",
                 disk->unit, disk->target, disk->bus);
-            disk->pci_controller = get_pci_info(name, &local_err);
+            disk->pci_controller = get_pci_info(disk->dev, &local_err);
 
             if (local_err) {
                 g_debug("failed to get PCI controller info: %s",
@@ -771,7 +770,9 @@  static GuestDiskAddressList *build_guest_disk_info(char *guid, Error **errp)
             /* Possibly CD-ROM or a shared drive. Try to pass the volume */
             g_debug("volume not on disk");
             disk = g_malloc0(sizeof(GuestDiskAddress));
-            get_single_disk_info(name, disk, &local_err);
+            disk->has_dev = true;
+            disk->dev = g_strdup(name);
+            get_single_disk_info(disk, &local_err);
             if (local_err) {
                 g_debug("failed to get disk info, ignoring error: %s",
                     error_get_pretty(local_err));
@@ -793,7 +794,6 @@  static GuestDiskAddressList *build_guest_disk_info(char *guid, Error **errp)
 
     /* Go through each extent */
     for (i = 0; i < extents->NumberOfDiskExtents; i++) {
-        char *disk_name = NULL;
         disk = g_malloc0(sizeof(GuestDiskAddress));
 
         /* Disk numbers directly correspond to numbers used in UNCs
@@ -804,10 +804,11 @@  static GuestDiskAddressList *build_guest_disk_info(char *guid, Error **errp)
          * See also Naming Files, Paths and Namespaces:
          * https://docs.microsoft.com/en-us/windows/desktop/FileIO/naming-a-file#win32-device-namespaces
          */
-        disk_name = g_strdup_printf("\\\\.\\PhysicalDrive%lu",
+        disk->has_dev = true;
+        disk->dev = g_strdup_printf("\\\\.\\PhysicalDrive%lu",
             extents->Extents[i].DiskNumber);
-        get_single_disk_info(disk_name, disk, &local_err);
-        g_free(disk_name);
+
+        get_single_disk_info(disk, &local_err);
         if (local_err) {
             error_propagate(errp, local_err);
             goto out;
diff --git a/qga/qapi-schema.json b/qga/qapi-schema.json
index 3bcda6257e..c6725b3ec8 100644
--- a/qga/qapi-schema.json
+++ b/qga/qapi-schema.json
@@ -835,6 +835,7 @@ 
 # @target: target id
 # @unit: unit id
 # @serial: serial number (since: 3.1)
+# @dev: device node (POSIX) or device UNC (Windows) (since: 3.1)
 #
 # Since: 2.2
 ##
@@ -842,7 +843,7 @@ 
   'data': {'pci-controller': 'GuestPCIAddress',
            'bus-type': 'GuestDiskBusType',
            'bus': 'int', 'target': 'int', 'unit': 'int',
-           '*serial': 'str'} }
+           '*serial': 'str', '*dev': 'str'} }
 
 ##
 # @GuestFilesystemInfo: