qemu-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Qemu-devel] [PATCH] qga: report disk size and free space


From: Tomáš Golembiovský
Subject: [Qemu-devel] [PATCH] qga: report disk size and free space
Date: Tue, 3 Jul 2018 10:31:20 +0200

Report total file system size and free space in output of command
"guest-get-fsinfo". Values are optional and it is not an error if they cannot
be retrieved for some reason.

Signed-off-by: Tomáš Golembiovský <address@hidden>
---
 qga/commands-posix.c | 18 ++++++++++++++++++
 qga/commands-win32.c | 16 ++++++++++++++++
 qga/qapi-schema.json |  5 ++++-
 3 files changed, 38 insertions(+), 1 deletion(-)

diff --git a/qga/commands-posix.c b/qga/commands-posix.c
index eae817191b..1f2fb25b91 100644
--- a/qga/commands-posix.c
+++ b/qga/commands-posix.c
@@ -13,6 +13,7 @@
 
 #include "qemu/osdep.h"
 #include <sys/ioctl.h>
+#include <sys/statvfs.h>
 #include <sys/utsname.h>
 #include <sys/wait.h>
 #include <dirent.h>
@@ -1074,11 +1075,28 @@ static GuestFilesystemInfo *build_guest_fsinfo(struct 
FsMount *mount,
     GuestFilesystemInfo *fs = g_malloc0(sizeof(*fs));
     char *devpath = g_strdup_printf("/sys/dev/block/%u:%u",
                                     mount->devmajor, mount->devminor);
+    struct statvfs vfs_stats;
 
     fs->mountpoint = g_strdup(mount->dirname);
     fs->type = g_strdup(mount->devtype);
     build_guest_fsinfo_for_device(devpath, fs, errp);
 
+    g_debug(" get filesystem statistics for '%s'", mount->dirname);
+    if (statvfs(mount->dirname, &vfs_stats) != 0) {
+        /* This is not fatal, just log this incident */
+        Error *local_err = NULL;
+        error_setg_errno(&local_err, errno, "statfs(\"%s\")",
+            mount->dirname);
+        slog("failed to stat filesystem: %s",
+            error_get_pretty(local_err));
+        error_free(local_err);
+    } else {
+        fs->size = vfs_stats.f_frsize * vfs_stats.f_blocks;
+        fs->has_size = true;
+        fs->free = vfs_stats.f_frsize * vfs_stats.f_bfree;
+        fs->has_free = true;
+    }
+
     g_free(devpath);
     return fs;
 }
diff --git a/qga/commands-win32.c b/qga/commands-win32.c
index 70ee5379f6..6d6ca05281 100644
--- a/qga/commands-win32.c
+++ b/qga/commands-win32.c
@@ -706,6 +706,22 @@ static GuestFilesystemInfo *build_guest_fsinfo(char *guid, 
Error **errp)
     }
     fs->type = g_strdup(fs_name);
     fs->disk = build_guest_disk_info(guid, errp);
+
+    if (len > 0) {
+        if (GetDiskFreeSpaceEx(mnt_point, 0, (PULARGE_INTEGER)&fs->size,
+                (PULARGE_INTEGER)&fs->free) != 0) {
+            /* This is not fatal, just log this incident */
+            Error *local_err = NULL;
+            error_setg_win32(&local_err, GetLastError(),
+                "failed to get free space on volume \"%s\"", mnt_point);
+            slog("%s", error_get_pretty(local_err));
+            error_free(local_err);
+        } else {
+            fs->has_size = true;
+            fs->has_free = true;
+        }
+    }
+
 free:
     g_free(mnt_point);
     return fs;
diff --git a/qga/qapi-schema.json b/qga/qapi-schema.json
index 17884c7c70..28a32444d3 100644
--- a/qga/qapi-schema.json
+++ b/qga/qapi-schema.json
@@ -848,12 +848,15 @@
 # @type: file system type string
 # @disk: an array of disk hardware information that the volume lies on,
 #        which may be empty if the disk type is not supported
+# @size: total number of bytes on the file system (Since 2.13)
+# @free: number of bytes available on the file system (Since 2.13)
 #
 # Since: 2.2
 ##
 { 'struct': 'GuestFilesystemInfo',
   'data': {'name': 'str', 'mountpoint': 'str', 'type': 'str',
-           'disk': ['GuestDiskAddress']} }
+           'disk': ['GuestDiskAddress'], '*size': 'uint64',
+           '*free': 'uint64'} }
 
 ##
 # @guest-get-fsinfo:
-- 
2.17.1




reply via email to

[Prev in Thread] Current Thread [Next in Thread]