diff mbox series

[v3] qga: add mountpoint usage to GuestFilesystemInfo

Message ID 20180531021924.4836-1-chen_han_xiao@126.com
State New
Headers show
Series [v3] qga: add mountpoint usage to GuestFilesystemInfo | expand

Commit Message

Chen Hanxiao May 31, 2018, 2:19 a.m. UTC
From: Chen Hanxiao <chenhanxiao@gmail.com>

This patch adds support for getting the usage of mounted
filesystem.
It's very useful when we try to monitor guest's filesystem.

Cc: Michael Roth <mdroth@linux.vnet.ibm.com>
Cc: Eric Blake <eblake@redhat.com>

Signed-off-by: Chen Hanxiao <chenhanxiao@gmail.com>
---
v2:
   add description in qapi-schema and version numbers
v3:
   use float for usage to get more precision.

 qga/commands-posix.c | 16 ++++++++++++++++
 qga/qapi-schema.json |  3 ++-
 2 files changed, 18 insertions(+), 1 deletion(-)

Comments

Eric Blake May 31, 2018, 2:03 p.m. UTC | #1
On 05/30/2018 09:19 PM, Chen Hanxiao wrote:
> From: Chen Hanxiao <chenhanxiao@gmail.com>
> 
> This patch adds support for getting the usage of mounted
> filesystem.
> It's very useful when we try to monitor guest's filesystem.
> 
> Cc: Michael Roth <mdroth@linux.vnet.ibm.com>
> Cc: Eric Blake <eblake@redhat.com>
> 
> Signed-off-by: Chen Hanxiao <chenhanxiao@gmail.com>
> ---

> @@ -1079,7 +1083,19 @@ static GuestFilesystemInfo *build_guest_fsinfo(struct FsMount *mount,
>       fs->type = g_strdup(mount->devtype);
>       build_guest_fsinfo_for_device(devpath, fs, errp);
>   
> +    if (statvfs(fs->mountpoint, &buf)) {
> +        error_setg_errno(errp, errno, "Failed to get statvfs");
> +        return NULL;
> +    }

Failing where we used to succeed is not nice.  Perhaps it would be 
better to make usage a best-effort query; and ignore statvfs() failure,

> +
> +    used = buf.f_blocks - buf.f_bfree;
> +    nonroot_total = used + buf.f_bavail;
> +    usage = (double) used / nonroot_total;
> +
> +    fs->usage = usage;
> +
>       g_free(devpath);
> +
>       return fs;
>   }
>   
> diff --git a/qga/qapi-schema.json b/qga/qapi-schema.json
> index 17884c7c70..68b9f60824 100644
> --- a/qga/qapi-schema.json
> +++ b/qga/qapi-schema.json
> @@ -846,13 +846,14 @@
>   # @name: disk name
>   # @mountpoint: mount point path
>   # @type: file system type string
> +# @usage: file system usage, fraction between 0 and 1 (since 3.0)
>   # @disk: an array of disk hardware information that the volume lies on,
>   #        which may be empty if the disk type is not supported
>   #
>   # Since: 2.2
>   ##
>   { 'struct': 'GuestFilesystemInfo',
> -  'data': {'name': 'str', 'mountpoint': 'str', 'type': 'str',
> +  'data': {'name': 'str', 'mountpoint': 'str', 'type': 'str', 'usage': 'number',

...by instead marking this optional, as in '*usage'; and only setting 
has_usage=true when statvfs() succeeds.

Also, hitting exactly 80 columns already feels long; and if you go with 
my suggestion, you'd be lengthening it even more, so wrap the new member 
to the next line to keep length under 80.
diff mbox series

Patch

diff --git a/qga/commands-posix.c b/qga/commands-posix.c
index 0dc219dbcf..7601978a11 100644
--- a/qga/commands-posix.c
+++ b/qga/commands-posix.c
@@ -46,6 +46,7 @@  extern char **environ;
 #include <arpa/inet.h>
 #include <sys/socket.h>
 #include <net/if.h>
+#include <sys/statvfs.h>
 
 #ifdef FIFREEZE
 #define CONFIG_FSFREEZE
@@ -1072,6 +1073,9 @@  static GuestFilesystemInfo *build_guest_fsinfo(struct FsMount *mount,
                                                Error **errp)
 {
     GuestFilesystemInfo *fs = g_malloc0(sizeof(*fs));
+    struct statvfs buf;
+    unsigned long used, nonroot_total;
+    double usage;
     char *devpath = g_strdup_printf("/sys/dev/block/%u:%u",
                                     mount->devmajor, mount->devminor);
 
@@ -1079,7 +1083,19 @@  static GuestFilesystemInfo *build_guest_fsinfo(struct FsMount *mount,
     fs->type = g_strdup(mount->devtype);
     build_guest_fsinfo_for_device(devpath, fs, errp);
 
+    if (statvfs(fs->mountpoint, &buf)) {
+        error_setg_errno(errp, errno, "Failed to get statvfs");
+        return NULL;
+    }
+
+    used = buf.f_blocks - buf.f_bfree;
+    nonroot_total = used + buf.f_bavail;
+    usage = (double) used / nonroot_total;
+
+    fs->usage = usage;
+
     g_free(devpath);
+
     return fs;
 }
 
diff --git a/qga/qapi-schema.json b/qga/qapi-schema.json
index 17884c7c70..68b9f60824 100644
--- a/qga/qapi-schema.json
+++ b/qga/qapi-schema.json
@@ -846,13 +846,14 @@ 
 # @name: disk name
 # @mountpoint: mount point path
 # @type: file system type string
+# @usage: file system usage, fraction between 0 and 1 (since 3.0)
 # @disk: an array of disk hardware information that the volume lies on,
 #        which may be empty if the disk type is not supported
 #
 # Since: 2.2
 ##
 { 'struct': 'GuestFilesystemInfo',
-  'data': {'name': 'str', 'mountpoint': 'str', 'type': 'str',
+  'data': {'name': 'str', 'mountpoint': 'str', 'type': 'str', 'usage': 'number',
            'disk': ['GuestDiskAddress']} }
 
 ##